1+ #if  defined(ARDUINO_NICLA)
2+ 
13#include  " BLEHandler.h" 
24#include  " DeviceInfo.h" 
35
@@ -10,6 +12,13 @@ auto sensorConfigUuid = "34c2e3bd-34aa-11eb-adc1-0242ac120002";
1012BLECharacteristic sensorDataCharacteristic (sensorDataUuid, (BLERead | BLENotify), sizeof(SensorDataPacket));
1113BLECharacteristic sensorConfigCharacteristic (sensorConfigUuid, BLEWrite, sizeof (SensorConfigurationPacket));
1214
15+ //  DFU channels
16+ BLEService dfuService (" 34c2e3b8-34aa-11eb-adc1-0242ac120002" 
17+ auto  dfuInternalUuid = " 34c2e3b9-34aa-11eb-adc1-0242ac120002" 
18+ auto  dfuExternalUuid = " 34c2e3ba-34aa-11eb-adc1-0242ac120002" 
19+ BLECharacteristic dfuInternalCharacteristic (dfuInternalUuid, BLEWrite, sizeof (DFUPacket), true);
20+ BLECharacteristic dfuExternalCharacteristic (dfuExternalUuid, BLEWrite, sizeof (DFUPacket), true);
21+ 
1322//  Device information channel
1423BLEService deviceInfoService (" 45622510-6468-465a-b141-0b9b0f96b468" 
1524auto  deviceIdentifierUuid = " 45622511-6468-465a-b141-0b9b0f96b468" 
@@ -67,11 +76,50 @@ bool BLEHandler::begin()
6776  deviceIdentifierCharacteristic.writeValue (deviceIdentifier);
6877  deviceGenerationCharacteristic.writeValue (deviceGeneration);
6978
70-   // 
79+   //  DFU channel
80+   BLE.setAdvertisedService (dfuService);
81+   dfuService.addCharacteristic (dfuInternalCharacteristic);
82+   dfuService.addCharacteristic (dfuExternalCharacteristic);
83+   BLE.addService (dfuService);
84+   dfuInternalCharacteristic.setEventHandler (BLEWritten, receivedInternalDFU);
85+   dfuExternalCharacteristic.setEventHandler (BLEWritten, receivedExternalDFU);
86+ 
87+ 
7188  BLE.advertise ();
7289  return  true ;
7390}
7491
92+ //  DFU channel
93+ void  BLEHandler::processDFUPacket (DFUType dfuType, BLECharacteristic characteristic) 
94+ {
95+   uint8_t  data[sizeof (DFUPacket)];
96+   characteristic.readValue (data, sizeof (data));
97+   if  (_debug) {
98+     _debug->print (" Size of data: " 
99+     _debug->println (sizeof (data));
100+   }
101+   dfuManager.processPacket (bleDFU, dfuType, data);
102+ 
103+   if  (data[0 ]) {
104+     // Last packet
105+     _lastDfuPack = true ;
106+     dfuManager.closeDfu ();
107+   }
108+ }
109+ 
110+ void  BLEHandler::receivedInternalDFU (BLEDevice central, BLECharacteristic characteristic)
111+ {
112+   if  (_debug) {
113+     _debug->println (" receivedInternalDFU" 
114+   }
115+   bleHandler.processDFUPacket (DFU_INTERNAL, characteristic);
116+ }
117+ 
118+ void  BLEHandler::receivedExternalDFU (BLEDevice central, BLECharacteristic characteristic)
119+ {
120+   bleHandler.processDFUPacket (DFU_EXTERNAL, characteristic);
121+ }
122+ 
75123BLECharacteristic *BLEHandler::getSensorDataCharacteristic ()
76124{
77125  return  &sensorDataCharacteristic;
@@ -103,3 +151,5 @@ void BLEHandler::debug(Stream &stream)
103151}
104152
105153BLEHandler bleHandler;
154+ 
155+ #endif 
0 commit comments