-
Notifications
You must be signed in to change notification settings - Fork 16
/
BLE.cpp
111 lines (90 loc) · 3.26 KB
/
BLE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "BLE.h"
#define SERVICE_UUID_ESPOTA "d804b643-6ce7-4e81-9f8a-ce0f699085eb"
#define CHARACTERISTIC_UUID_ID "d804b644-6ce7-4e81-9f8a-ce0f699085eb"
#define SERVICE_UUID_OTA "c8659210-af91-4ad3-a995-a58d6fd26145" // UART service UUID
#define CHARACTERISTIC_UUID_FW "c8659211-af91-4ad3-a995-a58d6fd26145"
#define CHARACTERISTIC_UUID_HW_VERSION "c8659212-af91-4ad3-a995-a58d6fd26145"
#define FULL_PACKET 512
#define CHARPOS_UPDATE_FLAG 5
esp_ota_handle_t otaHandler = 0;
bool updateFlag = false;
bool readyFlag = false;
int bytesReceived = 0;
int timesWritten = 0;
void otaCallback::onWrite(BLECharacteristic *pCharacteristic)
{
std::string rxData = pCharacteristic->getValue();
if (!updateFlag) { //If it's the first packet of OTA since bootup, begin OTA
Serial.println("BeginOTA");
esp_ota_begin(esp_ota_get_next_update_partition(NULL), OTA_SIZE_UNKNOWN, &otaHandler);
updateFlag = true;
}
if (_p_ble != NULL)
{
if (rxData.length() > 0)
{
esp_ota_write(otaHandler, rxData.c_str(), rxData.length());
if (rxData.length() != FULL_PACKET)
{
esp_ota_end(otaHandler);
Serial.println("EndOTA");
if (ESP_OK == esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL))) {
delay(2000);
esp_restart();
}
else {
Serial.println("Upload Error");
}
}
}
}
uint8_t txData[5] = {1, 2, 3, 4, 5};
//delay(1000);
pCharacteristic->setValue((uint8_t*)txData, 5);
pCharacteristic->notify();
}
//
// Constructor
BLE::BLE(void) {
}
//
// Destructor
BLE::~BLE(void)
{
}
//
// begin
bool BLE::begin(const char* localName = "UART Service") {
// Create the BLE Device
BLEDevice::init(localName);
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new BLECustomServerCallbacks());
// Create the BLE Service
pESPOTAService = pServer->createService(SERVICE_UUID_ESPOTA);
pService = pServer->createService(SERVICE_UUID_OTA);
// Create a BLE Characteristic
pESPOTAIdCharacteristic = pESPOTAService->createCharacteristic(
CHARACTERISTIC_UUID_ID,
BLECharacteristic::PROPERTY_READ
);
pVersionCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_HW_VERSION,
BLECharacteristic::PROPERTY_READ
);
pOtaCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_FW,
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE
);
pOtaCharacteristic->addDescriptor(new BLE2902());
pOtaCharacteristic->setCallbacks(new otaCallback(this));
// Start the service(s)
pESPOTAService->start();
pService->start();
// Start advertising
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID_ESPOTA);
pServer->getAdvertising()->start();
uint8_t hardwareVersion[5] = {HARDWARE_VERSION_MAJOR, HARDWARE_VERSION_MINOR, SOFTWARE_VERSION_MAJOR, SOFTWARE_VERSION_MINOR, SOFTWARE_VERSION_PATCH};
pVersionCharacteristic->setValue((uint8_t*)hardwareVersion, 5);
return true;
}