Skip to content

Commit

Permalink
Merge pull request #161 from aneeshdurg/formatter
Browse files Browse the repository at this point in the history
Introduce and use a consistent style
  • Loading branch information
sqfmi authored May 7, 2022
2 parents 48c796e + 8f55e2f commit 433050a
Show file tree
Hide file tree
Showing 16 changed files with 7,814 additions and 6,538 deletions.
129 changes: 53 additions & 76 deletions src/BLE.cpp
Original file line number Diff line number Diff line change
@@ -1,99 +1,85 @@
#include "BLE.h"

#define SERVICE_UUID_ESPOTA "cd77498e-1ac8-48b6-aba8-4161c7342fce"
#define CHARACTERISTIC_UUID_ID "cd77498f-1ac8-48b6-aba8-4161c7342fce"
#define SERVICE_UUID_ESPOTA "cd77498e-1ac8-48b6-aba8-4161c7342fce"
#define CHARACTERISTIC_UUID_ID "cd77498f-1ac8-48b6-aba8-4161c7342fce"

#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"
#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"

#define FULL_PACKET 512
#define FULL_PACKET 512
#define CHARPOS_UPDATE_FLAG 5

#define STATUS_CONNECTED 0
#define STATUS_CONNECTED 0
#define STATUS_DISCONNECTED 4
#define STATUS_UPDATING 1
#define STATUS_READY 2
#define STATUS_UPDATING 1
#define STATUS_READY 2

esp_ota_handle_t otaHandler = 0;

int status = -1;
int status = -1;
int bytesReceived = 0;
bool updateFlag = false;
bool updateFlag = false;

class BLECustomServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) { status = STATUS_CONNECTED; };

class BLECustomServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
status = STATUS_CONNECTED;
};

void onDisconnect(BLEServer* pServer) {
status = STATUS_DISCONNECTED;
}
void onDisconnect(BLEServer *pServer) { status = STATUS_DISCONNECTED; }
};

class otaCallback: public BLECharacteristicCallbacks {
public:
otaCallback(BLE* ble) {
_p_ble = ble;
}
BLE* _p_ble;
class otaCallback : public BLECharacteristicCallbacks {
public:
otaCallback(BLE *ble) { _p_ble = ble; }
BLE *_p_ble;

void onWrite(BLECharacteristic *pCharacteristic);
void onWrite(BLECharacteristic *pCharacteristic);
};

void otaCallback::onWrite(BLECharacteristic *pCharacteristic)
{
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("Begin FW Update");
esp_ota_begin(esp_ota_get_next_update_partition(NULL), OTA_SIZE_UNKNOWN, &otaHandler);
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);
updateFlag = true;
status = STATUS_UPDATING;
status = STATUS_UPDATING;
}
if (_p_ble != NULL)
{
if (rxData.length() > 0)
{
if (_p_ble != NULL) {
if (rxData.length() > 0) {
esp_ota_write(otaHandler, rxData.c_str(), rxData.length());
bytesReceived = bytesReceived + rxData.length();
if (rxData.length() != FULL_PACKET)
{
if (rxData.length() != FULL_PACKET) {
esp_ota_end(otaHandler);
//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");
// 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");
}
}
}
}

uint8_t txData[5] = {1, 2, 3, 4, 5};
//delay(1000);
pCharacteristic->setValue((uint8_t*)txData, 5);
// delay(1000);
pCharacteristic->setValue((uint8_t *)txData, 5);
pCharacteristic->notify();
}

//
// Constructor
BLE::BLE(void) {

}
BLE::BLE(void) {}

//
// Destructor
BLE::~BLE(void)
{

}
BLE::~BLE(void) {}

//
// begin
bool BLE::begin(const char* localName = "Watchy BLE OTA") {
bool BLE::begin(const char *localName = "Watchy BLE OTA") {
// Create the BLE Device
BLEDevice::init(localName);

Expand All @@ -103,28 +89,21 @@ bool BLE::begin(const char* localName = "Watchy BLE OTA") {

// Create the BLE Service
pESPOTAService = pServer->createService(SERVICE_UUID_ESPOTA);
pService = pServer->createService(SERVICE_UUID_OTA);
pService = pServer->createService(SERVICE_UUID_OTA);

// Create a BLE Characteristic
pESPOTAIdCharacteristic = pESPOTAService->createCharacteristic(
CHARACTERISTIC_UUID_ID,
BLECharacteristic::PROPERTY_READ
);
CHARACTERISTIC_UUID_ID, BLECharacteristic::PROPERTY_READ);

pVersionCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_HW_VERSION,
BLECharacteristic::PROPERTY_READ
);
CHARACTERISTIC_UUID_HW_VERSION, BLECharacteristic::PROPERTY_READ);

pWatchFaceNameCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_WATCHFACE_NAME,
BLECharacteristic::PROPERTY_READ
);
CHARACTERISTIC_UUID_WATCHFACE_NAME, BLECharacteristic::PROPERTY_READ);

pOtaCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_FW,
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE
);
CHARACTERISTIC_UUID_FW,
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE);

pOtaCharacteristic->addDescriptor(new BLE2902());
pOtaCharacteristic->setCallbacks(new otaCallback(this));
Expand All @@ -137,17 +116,15 @@ bool BLE::begin(const char* localName = "Watchy BLE OTA") {
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);
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);
pWatchFaceNameCharacteristic->setValue("Watchy 7 Segment");

return true;
}

int BLE::updateStatus(){
return status;
}
int BLE::updateStatus() { return status; }

int BLE::howManyBytes(){
return bytesReceived;
}
int BLE::howManyBytes() { return bytesReceived; }
36 changes: 17 additions & 19 deletions src/BLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,38 @@

#include "Arduino.h"

#include <BLE2902.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

#include "esp_ota_ops.h"

#include "config.h"

class BLE;

class BLE
{
public:
class BLE {
public:
BLE(void);
~BLE(void);

BLE(void);
~BLE(void);
bool begin(const char *localName);
int updateStatus();
int howManyBytes();

bool begin(const char* localName);
int updateStatus();
int howManyBytes();

private:
String local_name;
private:
String local_name;

BLEServer *pServer = NULL;
BLEServer *pServer = NULL;

BLEService *pESPOTAService = NULL;
BLECharacteristic * pESPOTAIdCharacteristic = NULL;
BLEService *pESPOTAService = NULL;
BLECharacteristic *pESPOTAIdCharacteristic = NULL;

BLEService *pService = NULL;
BLECharacteristic * pVersionCharacteristic = NULL;
BLECharacteristic * pOtaCharacteristic = NULL;
BLECharacteristic * pWatchFaceNameCharacteristic = NULL;
BLEService *pService = NULL;
BLECharacteristic *pVersionCharacteristic = NULL;
BLECharacteristic *pOtaCharacteristic = NULL;
BLECharacteristic *pWatchFaceNameCharacteristic = NULL;
};

#endif
Loading

0 comments on commit 433050a

Please sign in to comment.