|
| 1 | +/* |
| 2 | + The code in this file uses open source libraries provided by thecoderscorner |
| 3 | +
|
| 4 | + DO NOT EDIT THIS FILE, IT WILL BE GENERATED EVERY TIME YOU USE THE UI DESIGNER |
| 5 | + INSTEAD EITHER PUT CODE IN YOUR SKETCH OR CREATE ANOTHER SOURCE FILE. |
| 6 | +
|
| 7 | + All the variables you may need access to are marked extern in this file for easy |
| 8 | + use elsewhere. |
| 9 | + */ |
| 10 | + |
| 11 | +#include "tcMenuBLERemoteConnector.h" |
| 12 | + |
| 13 | +using namespace tcremote; |
| 14 | + |
| 15 | +BLEDevice tcremote::central; |
| 16 | +BLEService tcremote::bleApiService(bleServiceIdentifier); |
| 17 | +BLECharacteristic tcremote::toApiCharacteristic(characteristicDeviceToApi, BLERead | BLENotify, 255, false); |
| 18 | +BLECharacteristic tcremote::fromApiCharacteristic(characteristicApiToDevice, BLEWrite, 255, false); |
| 19 | +BLECharacteristic tcremote::deviceSequenceCharacteristic(characteristicDeviceSeqCounter, BLERead | BLENotify, 4, true); |
| 20 | +BLECharacteristic tcremote::apiSequenceCharacteristic(characteristicApiSeqCounter, BLEWrite, 4, true); |
| 21 | + |
| 22 | +inline uint32_t read32From(uint8_t* buf) { |
| 23 | + return (uint32_t)buf[0] << 24 | (uint32_t)buf[1] << 16 | (uint32_t)buf[2] << 8 | (uint32_t)buf[3]; |
| 24 | +} |
| 25 | + |
| 26 | +inline void writeToBuffer(uint8_t* buffer, uint32_t sequence) { |
| 27 | + buffer[0] = (sequence >> 24); |
| 28 | + buffer[1] = (sequence >> 16) & 0xFF; |
| 29 | + buffer[2] = (sequence >> 8) & 0xFF; |
| 30 | + buffer[3] = sequence & 0xFF; |
| 31 | +} |
| 32 | + |
| 33 | +BLETagValTransport::BLETagValTransport() : BaseBufferedRemoteTransport(BUFFER_ONE_MESSAGE, 255, 255) { |
| 34 | + writeBufferPos = 4; |
| 35 | + writeToBuffer(writeBuffer, currDeviceSequence + 1); |
| 36 | +} |
| 37 | + |
| 38 | +bool BLETagValTransport::readAvailable() { |
| 39 | + if(readBufferAvail && readBufferPos < readBufferAvail) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + fillReadBuffer(readBuffer, readBufferSize); |
| 44 | + return readBufferPos < readBufferAvail; |
| 45 | +} |
| 46 | + |
| 47 | +int BLETagValTransport::fillReadBuffer(uint8_t *dataBuffer, int maxSize) { |
| 48 | + if(!central.connected() || !fromApiCharacteristic.written()) { |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + readBufferAvail = fromApiCharacteristic.readValue(readBuffer, readBufferSize); |
| 53 | + if(readBufferAvail <= 4) |
| 54 | + { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + serlogHexDump(SER_NETWORK_DEBUG, "fromApi ", readBuffer, readBufferAvail); |
| 59 | + |
| 60 | + size_t i=4; |
| 61 | + while(readBuffer[i] != 0 && i < readBufferAvail) { |
| 62 | + i++; |
| 63 | + } |
| 64 | + readBufferAvail = i; |
| 65 | + uint32_t rxSequence = read32From(readBuffer); |
| 66 | + serlogF3(SER_NETWORK_DEBUG, "RxSeq ", rxSequence, readBufferAvail); |
| 67 | + |
| 68 | + uint8_t buffer[4]; |
| 69 | + writeToBuffer(buffer, rxSequence); |
| 70 | + bool writtenSeqOk = deviceSequenceCharacteristic.writeValue(buffer, 4) != 0; |
| 71 | + serlogF2(SER_NETWORK_DEBUG, "RxSeqOK ", writtenSeqOk); |
| 72 | + |
| 73 | + readBufferPos = 4; |
| 74 | + return readBufferAvail; |
| 75 | +} |
| 76 | + |
| 77 | +void BLETagValTransport::flush() { |
| 78 | + if(apiSequenceCharacteristic.written()) { |
| 79 | + uint8_t buffer[4]; |
| 80 | + apiSequenceCharacteristic.readValue(buffer, sizeof(buffer)); |
| 81 | + currApiSequence = read32From(buffer); |
| 82 | + serlogF3(SER_NETWORK_DEBUG, "ApiSeq rd ", currApiSequence, currDeviceSequence) |
| 83 | + } |
| 84 | + if(central.connected() && currApiSequence == currDeviceSequence) { |
| 85 | + // bump the device sequence |
| 86 | + currDeviceSequence += 1; |
| 87 | + |
| 88 | + // zero terminate if not full size of buffer |
| 89 | + if((writeBufferSize - writeBufferPos) > 1) { |
| 90 | + writeBuffer[writeBufferPos] = 0; |
| 91 | + writeBufferPos++; |
| 92 | + } |
| 93 | + |
| 94 | + // write the value out and make sure it all writes. |
| 95 | + int written = toApiCharacteristic.writeValue(writeBuffer, writeBufferPos); |
| 96 | + if(written != 0) { |
| 97 | + serlogF3(SER_NETWORK_DEBUG, "ApiToDevice ", writeBufferPos, currDeviceSequence) |
| 98 | + } else { |
| 99 | + central.disconnect(); |
| 100 | + } |
| 101 | + writeBufferPos = 4; |
| 102 | + writeToBuffer(writeBuffer, currDeviceSequence); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +bool BLETagValTransport::available() { |
| 107 | + return central.connected(); |
| 108 | +} |
| 109 | + |
| 110 | +bool BLETagValTransport::connected() { |
| 111 | + return central.connected(); |
| 112 | +} |
| 113 | + |
| 114 | +void BLETagValTransport::connectedClient() { |
| 115 | + currApiSequence = currDeviceSequence = 0; |
| 116 | + uint8_t zeroBuffer[] = {0}; |
| 117 | + deviceSequenceCharacteristic.writeValue(zeroBuffer, 4); |
| 118 | +} |
| 119 | + |
| 120 | +bool BLEDeviceInitialisation::attemptInitialisation() { |
| 121 | + serlogF(SER_NETWORK_INFO, "BLE Starting"); |
| 122 | + if(!initialised && BLE.begin() == 0) return false; |
| 123 | + initialised = true; |
| 124 | + if (!BLE.begin()) { |
| 125 | + serlogF(SER_ERROR, "BLE failed!"); |
| 126 | + } |
| 127 | + |
| 128 | + bleApiService.addCharacteristic(toApiCharacteristic); |
| 129 | + bleApiService.addCharacteristic(fromApiCharacteristic); |
| 130 | + bleApiService.addCharacteristic(apiSequenceCharacteristic); |
| 131 | + bleApiService.addCharacteristic(deviceSequenceCharacteristic); |
| 132 | + uint8_t buffer[4] = {0}; |
| 133 | + apiSequenceCharacteristic.writeValue(buffer, 4); |
| 134 | + deviceSequenceCharacteristic.writeValue(buffer, 4); |
| 135 | + |
| 136 | + BLE.addService(bleApiService); |
| 137 | + BLE.setLocalName(applicationInfo.name); |
| 138 | + BLE.setAdvertisedService(bleApiService); |
| 139 | + BLE.advertise(); |
| 140 | + |
| 141 | + serlogF(SER_NETWORK_INFO, "BLE Up"); |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
| 145 | +bool BLEDeviceInitialisation::attemptNewConnection(BaseRemoteServerConnection *transport) { |
| 146 | + central = BLE.central(); |
| 147 | + if(central.connected()) { |
| 148 | + reinterpret_cast<BLETagValTransport*>(transport)->connectedClient(); |
| 149 | + return true; |
| 150 | + } |
| 151 | + return false; |
| 152 | +} |
0 commit comments