/ RNode_Firmware_CE_G2 / BLESerial.h
BLESerial.h
  1  // Copyright (C) 2024, Mark Qvist
  2  
  3  // This program is free software: you can redistribute it and/or modify
  4  // it under the terms of the GNU General Public License as published by
  5  // the Free Software Foundation, either version 3 of the License, or
  6  // (at your option) any later version.
  7  
  8  // This program is distributed in the hope that it will be useful,
  9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 11  // GNU General Public License for more details.
 12  
 13  // You should have received a copy of the GNU General Public License
 14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 15  
 16  #include "Boards.h"
 17  
 18  #if PLATFORM != PLATFORM_NRF52
 19  #if HAS_BLE
 20  
 21  #include <Arduino.h>
 22  
 23  #include <BLEDevice.h>
 24  #include <BLEUtils.h>
 25  #include <BLEServer.h>
 26  #include <BLE2902.h>
 27  
 28  template <size_t n>
 29  class BLEFIFO {
 30  private:
 31  	uint8_t buffer[n];
 32  	int head = 0;
 33  	int tail = 0;
 34  
 35  public:
 36  	void push(uint8_t value) {
 37  		buffer[head] = value;
 38  		head = (head + 1) % n;
 39  		if (head == tail) { tail = (tail + 1) % n; }
 40  	}
 41  
 42  	int pop() {
 43  		if (head == tail) {
 44  			return -1;
 45  		} else {
 46  			uint8_t value = buffer[tail];
 47  			tail = (tail + 1) % n;
 48  			return value;
 49  		}
 50  	}
 51  
 52  	void clear() { head = 0; tail = 0; }
 53  
 54  	int get(size_t index) {
 55  		if (index >= this->getLength()) {
 56  			return -1;
 57  		} else {
 58  			return buffer[(tail + index) % n];
 59  		}
 60  	}
 61  
 62  	size_t getLength() {
 63  		if (head >= tail) {
 64  			return head - tail;
 65  		} else {
 66  			return n - tail + head;
 67  		}
 68  	}
 69  };
 70  
 71  #define RX_BUFFER_SIZE 6144
 72  #define BLE_BUFFER_SIZE 512 // Must fit in max GATT attribute length
 73  #define MIN_MTU 50
 74  
 75  class BLESerial : public BLECharacteristicCallbacks, public BLEServerCallbacks, public BLESecurityCallbacks, public Stream {
 76  public:
 77    BLESerial();
 78  
 79    void begin(const char *name);
 80    void end();
 81    void disconnect();
 82    void startAdvertising();
 83    void stopAdvertising();
 84    void onWrite(BLECharacteristic *characteristic);
 85    int available();
 86    int peek();
 87    int read();
 88    size_t readBytes(uint8_t *buffer, size_t bufferSize);
 89    size_t write(uint8_t byte);
 90    size_t write(const uint8_t *buffer, size_t bufferSize);
 91    size_t print(const char *value);
 92    void flush();
 93    void onConnect(BLEServer *server);
 94    void onDisconnect(BLEServer *server);
 95  
 96    uint32_t onPassKeyRequest();
 97    void onPassKeyNotify(uint32_t passkey);
 98    bool onSecurityRequest();
 99    void onAuthenticationComplete(esp_ble_auth_cmpl_t);
100    bool onConfirmPIN(uint32_t pin);
101  
102    bool connected();
103  
104    BLEServer *ble_server;
105    BLEAdvertising *ble_adv;
106    BLEService *SerialService;
107    BLECharacteristic *TxCharacteristic;
108    BLECharacteristic *RxCharacteristic;
109    size_t transmitBufferLength;
110    unsigned long long lastFlushTime;
111  
112  private:
113    BLESerial(BLESerial const &other) = delete;
114    void operator=(BLESerial const &other) = delete;
115  
116    BLEFIFO<RX_BUFFER_SIZE> rx_buffer;
117    size_t numAvailableLines;
118    uint8_t transmitBuffer[BLE_BUFFER_SIZE];
119  
120    int ConnectedDeviceCount;
121    void SetupSerialService();
122  
123    uint16_t peerMTU;
124    uint16_t maxTransferSize = BLE_BUFFER_SIZE;
125  
126    bool checkMTU();
127  
128    const char *BLE_SERIAL_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
129    const char *BLE_RX_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e";
130    const char *BLE_TX_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";
131  
132    bool started = false;
133  };
134  
135  #endif
136  #endif