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