Skip to content

Instantly share code, notes, and snippets.

@ste2425

ste2425/BLE.cpp Secret

Last active June 2, 2023 20:51
Show Gist options
  • Save ste2425/c601fb16df8ece2c91c7a65b2f29344a to your computer and use it in GitHub Desktop.
Save ste2425/c601fb16df8ece2c91c7a65b2f29344a to your computer and use it in GitHub Desktop.
Bluepad32 ota failing to compile
#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;
}
#ifndef _BLE_H_
#define _BLE_H_
#include "Arduino.h"
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "esp_ota_ops.h"
#define SOFTWARE_VERSION_MAJOR 0
#define SOFTWARE_VERSION_MINOR 1
#define SOFTWARE_VERSION_PATCH 0
#define HARDWARE_VERSION_MAJOR 1
#define HARDWARE_VERSION_MINOR 2
class BLE; // forward declaration
class BLECustomServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
// deviceConnected = true;
// // code here for when device connects
};
void onDisconnect(BLEServer* pServer) {
// deviceConnected = false;
// // code here for when device disconnects
}
};
class otaCallback: public BLECharacteristicCallbacks {
public:
otaCallback(BLE* ble) {
_p_ble = ble;
}
BLE* _p_ble;
void onWrite(BLECharacteristic *pCharacteristic);
};
class BLE
{
public:
BLE(void);
~BLE(void);
bool begin(const char* localName);
private:
String local_name;
BLEServer *pServer = NULL;
BLEService *pESPOTAService = NULL;
BLECharacteristic *pESPOTAIdCharacteristic = NULL;
BLEService *pService = NULL;
BLECharacteristic *pVersionCharacteristic = NULL;
BLECharacteristic *pOtaCharacteristic = NULL;
};
#endif
In file included from C:\Users\stephen.cooper\Documents\Arduino\ota\ota\ota.ino:1:
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:21:59: error: expected class-name before '{' token
class BLECustomServerCallbacks: public BLEServerCallbacks {
^
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:22:20: error: 'BLEServer' has not been declared
void onConnect(BLEServer* pServer) {
^~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:27:23: error: 'BLEServer' has not been declared
void onDisconnect(BLEServer* pServer) {
^~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:33:54: error: expected class-name before '{' token
class otaCallback: public BLECharacteristicCallbacks {
^
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:40:18: error: 'BLECharacteristic' has not been declared
void onWrite(BLECharacteristic *pCharacteristic);
^~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:55:5: error: 'BLEServer' does not name a type; did you mean 'Server'?
BLEServer *pServer = NULL;
^~~~~~~~~
Server
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:57:5: error: 'BLEService' does not name a type
BLEService *pESPOTAService = NULL;
^~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:58:5: error: 'BLECharacteristic' does not name a type; did you mean 'Character_h'?
BLECharacteristic *pESPOTAIdCharacteristic = NULL;
^~~~~~~~~~~~~~~~~
Character_h
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:60:5: error: 'BLEService' does not name a type
BLEService *pService = NULL;
^~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:61:5: error: 'BLECharacteristic' does not name a type; did you mean 'Character_h'?
BLECharacteristic *pVersionCharacteristic = NULL;
^~~~~~~~~~~~~~~~~
Character_h
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:62:5: error: 'BLECharacteristic' does not name a type; did you mean 'Character_h'?
BLECharacteristic *pOtaCharacteristic = NULL;
^~~~~~~~~~~~~~~~~
Character_h
In file included from C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:1:
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:21:59: error: expected class-name before '{' token
class BLECustomServerCallbacks: public BLEServerCallbacks {
^
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:22:20: error: 'BLEServer' has not been declared
void onConnect(BLEServer* pServer) {
^~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:27:23: error: 'BLEServer' has not been declared
void onDisconnect(BLEServer* pServer) {
^~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:33:54: error: expected class-name before '{' token
class otaCallback: public BLECharacteristicCallbacks {
^
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:40:18: error: 'BLECharacteristic' has not been declared
void onWrite(BLECharacteristic *pCharacteristic);
^~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:55:5: error: 'BLEServer' does not name a type; did you mean 'Server'?
BLEServer *pServer = NULL;
^~~~~~~~~
Server
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:57:5: error: 'BLEService' does not name a type
BLEService *pESPOTAService = NULL;
^~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:58:5: error: 'BLECharacteristic' does not name a type; did you mean 'Character_h'?
BLECharacteristic *pESPOTAIdCharacteristic = NULL;
^~~~~~~~~~~~~~~~~
Character_h
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:60:5: error: 'BLEService' does not name a type
BLEService *pService = NULL;
^~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:61:5: error: 'BLECharacteristic' does not name a type; did you mean 'Character_h'?
BLECharacteristic *pVersionCharacteristic = NULL;
^~~~~~~~~~~~~~~~~
Character_h
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.h:62:5: error: 'BLECharacteristic' does not name a type; did you mean 'Character_h'?
BLECharacteristic *pOtaCharacteristic = NULL;
^~~~~~~~~~~~~~~~~
Character_h
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:20:27: error: variable or field 'onWrite' declared void
void otaCallback::onWrite(BLECharacteristic *pCharacteristic)
^~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:20:27: error: 'BLECharacteristic' was not declared in this scope
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:20:27: note: suggested alternative: 'Character_h'
void otaCallback::onWrite(BLECharacteristic *pCharacteristic)
^~~~~~~~~~~~~~~~~
Character_h
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:20:46: error: 'pCharacteristic' was not declared in this scope
void otaCallback::onWrite(BLECharacteristic *pCharacteristic)
^~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:20:46: note: suggested alternative: 'Character_h'
void otaCallback::onWrite(BLECharacteristic *pCharacteristic)
^~~~~~~~~~~~~~~
Character_h
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp: In member function 'bool BLE::begin(const char*)':
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:71:3: error: 'BLEDevice' has not been declared
BLEDevice::init(localName);
^~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:74:3: error: 'pServer' was not declared in this scope
pServer = BLEDevice::createServer();
^~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:74:3: note: suggested alternative: 'Server'
pServer = BLEDevice::createServer();
^~~~~~~
Server
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:74:13: error: 'BLEDevice' has not been declared
pServer = BLEDevice::createServer();
^~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:78:3: error: 'pESPOTAService' was not declared in this scope
pESPOTAService = pServer->createService(SERVICE_UUID_ESPOTA);
^~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:79:3: error: 'pService' was not declared in this scope
pService = pServer->createService(SERVICE_UUID_OTA);
^~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:79:3: note: suggested alternative: 'Serial'
pService = pServer->createService(SERVICE_UUID_OTA);
^~~~~~~~
Serial
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:82:3: error: 'pESPOTAIdCharacteristic' was not declared in this scope
pESPOTAIdCharacteristic = pESPOTAService->createCharacteristic(
^~~~~~~~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:84:40: error: 'BLECharacteristic' has not been declared
BLECharacteristic::PROPERTY_READ
^~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:87:3: error: 'pVersionCharacteristic' was not declared in this scope
pVersionCharacteristic = pService->createCharacteristic(
^~~~~~~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:89:30: error: 'BLECharacteristic' has not been declared
BLECharacteristic::PROPERTY_READ
^~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:92:3: error: 'pOtaCharacteristic' was not declared in this scope
pOtaCharacteristic = pService->createCharacteristic(
^~~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:92:3: note: suggested alternative: 'Character_h'
pOtaCharacteristic = pService->createCharacteristic(
^~~~~~~~~~~~~~~~~~
Character_h
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:94:26: error: 'BLECharacteristic' has not been declared
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE
^~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:94:63: error: 'BLECharacteristic' has not been declared
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE
^~~~~~~~~~~~~~~~~
C:\Users\stephen.cooper\Documents\Arduino\ota\ota\BLE.cpp:97:41: error: expected type-specifier before 'BLE2902'
pOtaCharacteristic->addDescriptor(new BLE2902());
^~~~~~~
exit status 1
Compilation error: expected class-name before '{' token
#include "BLE.h"
/**Bluetooth**/
BLE BT;
void setup(void) {
Serial.begin(115200);
Serial.println("Serial Begin");
Serial.println();
Serial.print("HW v");
Serial.print(HARDWARE_VERSION_MAJOR);
Serial.print(".");
Serial.println(HARDWARE_VERSION_MINOR);
Serial.print("SW v");
Serial.print(SOFTWARE_VERSION_MAJOR);
Serial.print(".");
Serial.print(SOFTWARE_VERSION_MINOR);
Serial.print(".");
Serial.println(SOFTWARE_VERSION_PATCH);
BT.begin("ESP32 OTA Updates");
}
void loop(void) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment