2021-01-05 00:03:49 -05:00
|
|
|
#include "BLE.h"
|
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
#define SERVICE_UUID_ESPOTA "cd77498e-1ac8-48b6-aba8-4161c7342fce"
|
|
|
|
#define CHARACTERISTIC_UUID_ID "cd77498f-1ac8-48b6-aba8-4161c7342fce"
|
2021-01-05 00:03:49 -05:00
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
#define SERVICE_UUID_OTA "86b12865-4b70-4893-8ce6-9864fc00374d"
|
|
|
|
#define CHARACTERISTIC_UUID_FW "86b12866-4b70-4893-8ce6-9864fc00374d"
|
|
|
|
#define CHARACTERISTIC_UUID_HW_VERSION "86b12867-4b70-4893-8ce6-9864fc00374d"
|
|
|
|
#define CHARACTERISTIC_UUID_WATCHFACE_NAME \
|
|
|
|
"86b12868-4b70-4893-8ce6-9864fc00374d"
|
2021-01-05 00:03:49 -05:00
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
#define FULL_PACKET 512
|
2021-01-05 00:03:49 -05:00
|
|
|
#define CHARPOS_UPDATE_FLAG 5
|
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
#define STATUS_CONNECTED 0
|
2021-01-05 00:03:49 -05:00
|
|
|
#define STATUS_DISCONNECTED 4
|
2022-04-28 21:17:00 -04:00
|
|
|
#define STATUS_UPDATING 1
|
|
|
|
#define STATUS_READY 2
|
2021-01-05 00:03:49 -05:00
|
|
|
|
|
|
|
esp_ota_handle_t otaHandler = 0;
|
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
int status = -1;
|
2021-01-05 00:03:49 -05:00
|
|
|
int bytesReceived = 0;
|
2022-04-28 21:17:00 -04:00
|
|
|
bool updateFlag = false;
|
2021-01-05 00:03:49 -05:00
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
class BLECustomServerCallbacks : public BLEServerCallbacks {
|
|
|
|
void onConnect(BLEServer *pServer) { status = STATUS_CONNECTED; };
|
2021-01-05 00:03:49 -05:00
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
void onDisconnect(BLEServer *pServer) { status = STATUS_DISCONNECTED; }
|
2021-01-05 00:03:49 -05:00
|
|
|
};
|
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
class otaCallback : public BLECharacteristicCallbacks {
|
|
|
|
public:
|
|
|
|
otaCallback(BLE *ble) { _p_ble = ble; }
|
|
|
|
BLE *_p_ble;
|
2021-01-05 00:03:49 -05:00
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
void onWrite(BLECharacteristic *pCharacteristic);
|
2021-01-05 00:03:49 -05:00
|
|
|
};
|
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
void otaCallback::onWrite(BLECharacteristic *pCharacteristic) {
|
2021-01-05 00:03:49 -05:00
|
|
|
std::string rxData = pCharacteristic->getValue();
|
2022-04-28 21:17:00 -04:00
|
|
|
if (!updateFlag) { // If it's the first packet of OTA since bootup, begin OTA
|
|
|
|
// Serial.println("Begin FW Update");
|
|
|
|
esp_ota_begin(esp_ota_get_next_update_partition(NULL), OTA_SIZE_UNKNOWN,
|
|
|
|
&otaHandler);
|
2021-01-05 00:03:49 -05:00
|
|
|
updateFlag = true;
|
2022-04-28 21:17:00 -04:00
|
|
|
status = STATUS_UPDATING;
|
2021-01-05 00:03:49 -05:00
|
|
|
}
|
2022-04-28 21:17:00 -04:00
|
|
|
if (_p_ble != NULL) {
|
|
|
|
if (rxData.length() > 0) {
|
2021-01-05 00:03:49 -05:00
|
|
|
esp_ota_write(otaHandler, rxData.c_str(), rxData.length());
|
|
|
|
bytesReceived = bytesReceived + rxData.length();
|
2022-04-28 21:17:00 -04:00
|
|
|
if (rxData.length() != FULL_PACKET) {
|
2021-01-05 00:03:49 -05:00
|
|
|
esp_ota_end(otaHandler);
|
2022-04-28 21:17:00 -04:00
|
|
|
// Serial.println("End FW Update");
|
|
|
|
if (ESP_OK == esp_ota_set_boot_partition(
|
|
|
|
esp_ota_get_next_update_partition(NULL))) {
|
|
|
|
status = STATUS_READY;
|
|
|
|
} else {
|
|
|
|
// Serial.println("Upload Error");
|
2021-01-05 00:03:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t txData[5] = {1, 2, 3, 4, 5};
|
2022-04-28 21:17:00 -04:00
|
|
|
// delay(1000);
|
|
|
|
pCharacteristic->setValue((uint8_t *)txData, 5);
|
2021-01-05 00:03:49 -05:00
|
|
|
pCharacteristic->notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Constructor
|
2022-04-28 21:17:00 -04:00
|
|
|
BLE::BLE(void) {}
|
2021-01-05 00:03:49 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// Destructor
|
2022-04-28 21:17:00 -04:00
|
|
|
BLE::~BLE(void) {}
|
2021-01-05 00:03:49 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// begin
|
2022-04-28 21:17:00 -04:00
|
|
|
bool BLE::begin(const char *localName = "Watchy BLE OTA") {
|
2021-01-05 00:03:49 -05:00
|
|
|
// 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);
|
2022-04-28 21:17:00 -04:00
|
|
|
pService = pServer->createService(SERVICE_UUID_OTA);
|
2021-01-05 00:03:49 -05:00
|
|
|
|
|
|
|
// Create a BLE Characteristic
|
|
|
|
pESPOTAIdCharacteristic = pESPOTAService->createCharacteristic(
|
2022-04-28 21:17:00 -04:00
|
|
|
CHARACTERISTIC_UUID_ID, BLECharacteristic::PROPERTY_READ);
|
2021-01-05 00:03:49 -05:00
|
|
|
|
|
|
|
pVersionCharacteristic = pService->createCharacteristic(
|
2022-04-28 21:17:00 -04:00
|
|
|
CHARACTERISTIC_UUID_HW_VERSION, BLECharacteristic::PROPERTY_READ);
|
2021-01-05 00:03:49 -05:00
|
|
|
|
|
|
|
pWatchFaceNameCharacteristic = pService->createCharacteristic(
|
2022-04-28 21:17:00 -04:00
|
|
|
CHARACTERISTIC_UUID_WATCHFACE_NAME, BLECharacteristic::PROPERTY_READ);
|
2021-01-05 00:03:49 -05:00
|
|
|
|
|
|
|
pOtaCharacteristic = pService->createCharacteristic(
|
2022-04-28 21:17:00 -04:00
|
|
|
CHARACTERISTIC_UUID_FW,
|
|
|
|
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE);
|
2021-01-05 00:03:49 -05:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
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);
|
2021-01-05 00:03:49 -05:00
|
|
|
pWatchFaceNameCharacteristic->setValue("Watchy 7 Segment");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
int BLE::updateStatus() { return status; }
|
2021-01-05 00:03:49 -05:00
|
|
|
|
2022-04-28 21:17:00 -04:00
|
|
|
int BLE::howManyBytes() { return bytesReceived; }
|