Skip to content

Commit

Permalink
Added native esp32 lib (#31)
Browse files Browse the repository at this point in the history
* add support for ESP32 non-Nimble, add new begin() method, for env with existing ble server

* fix typos in Nimble files

* nano iot 33 ota fails, syntax error

* Update library.properties remove nimble-arduino

* Update ArduinoBleOtaClassESP32Ble.h

incorporate change of security.h filname

* migrate OTAESP32 to security and upload callbacks

* change hw/sw name defs to string type

* remove start advertising and adduuid from serfer and storage only  begin functions, leave that to the using app

* remove service start

* remove advertising and service start from ESP32 startup

* cleanup EnableUpload, add advertise option

* fix spurious comma typo esp32

* Changed defines

* Added defines header

* Added defines header

* Added new target

* Rename to USE_NATIVE_ESP32_BLE_LIB

* Removed unneded includes

* Fixed multiservice

* Update ArduinoOTA lib

---------

Co-authored-by: Sam Detweiler <[email protected]>
  • Loading branch information
vovagorodok and sdetweil authored Oct 24, 2023
1 parent a36d086 commit cfe9bf4
Show file tree
Hide file tree
Showing 15 changed files with 258 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pio
.vscode
.DS_Store
*.pyc
2 changes: 1 addition & 1 deletion examples/basic/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void setup() {
}

void loop() {
#ifndef USE_NIM_BLE_ARDUINO_LIB
#ifdef USE_ARDUINO_BLE_LIB
BLE.poll();
#endif
ArduinoBleOTA.pull();
Expand Down
2 changes: 1 addition & 1 deletion examples/full/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void setup() {
}

void loop() {
#ifndef USE_NIM_BLE_ARDUINO_LIB
#ifdef USE_ARDUINO_BLE_LIB
BLE.poll();
#endif
ArduinoBleOTA.pull();
Expand Down
4 changes: 2 additions & 2 deletions examples/multiservice/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
void setup() {
initBle(NAME);

#ifndef USE_NIM_BLE_ARDUINO_LIB
#ifdef USE_ARDUINO_BLE_LIB
static BLEService service(MY_SECOND_SERVICE_UUID);
BLE.addService(service);
#else
Expand All @@ -22,7 +22,7 @@ void setup() {
}

void loop() {
#ifndef USE_NIM_BLE_ARDUINO_LIB
#ifdef USE_ARDUINO_BLE_LIB
BLE.poll();
#endif
ArduinoBleOTA.pull();
Expand Down
2 changes: 1 addition & 1 deletion examples/security_disable_upload/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void setup() {
}

void loop() {
#ifndef USE_NIM_BLE_ARDUINO_LIB
#ifdef USE_ARDUINO_BLE_LIB
BLE.poll();
#endif
ArduinoBleOTA.pull();
Expand Down
2 changes: 1 addition & 1 deletion examples/security_pin/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void setup() {
}

void loop() {
#ifndef USE_NIM_BLE_ARDUINO_LIB
#ifdef USE_ARDUINO_BLE_LIB
BLE.poll();
#endif
ArduinoBleOTA.pull();
Expand Down
2 changes: 1 addition & 1 deletion examples/versioning/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void setup() {
}

void loop() {
#ifndef USE_NIM_BLE_ARDUINO_LIB
#ifdef USE_ARDUINO_BLE_LIB
BLE.poll();
#endif
ArduinoBleOTA.pull();
Expand Down
17 changes: 13 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ default_envs =
esp32dev
nano_33_iot
esp32dev_nimble
esp32dev_native

[env]
framework = arduino
lib_deps =
jandrassy/ArduinoOTA@^1.0.10
RobTillaart/CRC@^1.0.1
lib_deps =
jandrassy/ArduinoOTA@^1.0.12
RobTillaart/CRC@^1.0.2
rlogiacco/CircularBuffer@^1.3.3

[env:esp32dev]
Expand All @@ -40,8 +41,16 @@ lib_deps =
[env:esp32dev_nimble]
platform = espressif32
board = esp32dev
build_flags =
build_flags =
-D USE_NIM_BLE_ARDUINO_LIB
lib_deps =
${env.lib_deps}
h2zero/NimBLE-Arduino@^1.4.1

[env:esp32dev_native]
platform = espressif32
board = esp32dev
build_flags =
-D USE_NATIVE_ESP32_BLE_LIB
lib_deps =
${env.lib_deps}
3 changes: 3 additions & 0 deletions src/ArduinoBleOTA.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include "BleOtaDefines.h"
#ifdef USE_NIM_BLE_ARDUINO_LIB
#include "ArduinoBleOtaClassNimBle.h"
#elif USE_NATIVE_ESP32_BLE_LIB
#include "ArduinoBleOtaClassNativeESP32.h"
#else
#include "ArduinoBleOtaClass.h"
#endif
3 changes: 2 additions & 1 deletion src/ArduinoBleOtaClass.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#ifndef USE_NIM_BLE_ARDUINO_LIB
#include "BleOtaDefines.h"
#ifdef USE_ARDUINO_BLE_LIB
#include "ArduinoBleOtaClass.h"
#include "BleOtaUploader.h"
#include "BleOtaUuids.h"
Expand Down
138 changes: 138 additions & 0 deletions src/ArduinoBleOtaClassNativeESP32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "BleOtaDefines.h"
#ifdef USE_NATIVE_ESP32_BLE_LIB
#include "ArduinoBleOtaClassNativeESP32.h"
#include "BleOtaUploader.h"
#include "BleOtaUuids.h"
#include "BleOtaSizes.h"

namespace
{
static BleOtaSecurityCallbacks dummySecurityCallbacks{};
static BleOtaUploadCallbacks dummyUploadCallbacks{};
}

ArduinoBleOTAClass::ArduinoBleOTAClass() :
txCharacteristic(),
securityCallbacks(&dummySecurityCallbacks),
uploadCallbacks(&dummyUploadCallbacks)
{}

bool ArduinoBleOTAClass::begin(const std::string& deviceName, OTAStorage& storage,
const std::string& hwName, BleOtaVersion hwVersion,
const std::string& swName, BleOtaVersion swVersion,
bool enableUpload)
{
BLEDevice::init(deviceName);
auto* server = BLEDevice::createServer();

if(!begin(storage, hwName, hwVersion, swName, swVersion, enableUpload))
return false;

auto* advertising = server->getAdvertising();
advertising->setScanResponse(true);
advertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
advertising->setMaxPreferred(0x12);
advertising->start();
return true;
}

bool ArduinoBleOTAClass::begin(OTAStorage& storage,
const std::string& hwName, BleOtaVersion hwVersion,
const std::string& swName, BleOtaVersion swVersion,
bool enableUpload)
{
auto* server = BLEDevice::createServer();
BLEDevice::setMTU(BLE_OTA_MTU_SIZE);

bleOtaUploader.begin(storage);
bleOtaUploader.setEnabling(enableUpload);
auto* service = server->createService(BLE_OTA_SERVICE_UUID);

auto* rxCharacteristic = service->createCharacteristic(
BLE_OTA_CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE_NR
);
rxCharacteristic->setCallbacks(this);

auto* txCharacteristic = service->createCharacteristic(
BLE_OTA_CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
);
this->txCharacteristic = txCharacteristic;

begin(*service, hwName, hwVersion, swName, swVersion);

auto* advertising = server->getAdvertising();
advertising->addServiceUUID(BLE_OTA_SERVICE_UUID);
service->start();
return true;
}

void ArduinoBleOTAClass::begin(BLEService& service,
const std::string& hwName, BleOtaVersion hwVersion,
const std::string& swName, BleOtaVersion swVersion)
{
auto* hwNameCharacteristic = service.createCharacteristic(
BLE_OTA_CHARACTERISTIC_UUID_HW_NAME,
BLECharacteristic::PROPERTY_READ
);
hwNameCharacteristic->setValue(hwName);
auto* swNameCharacteristic = service.createCharacteristic(
BLE_OTA_CHARACTERISTIC_UUID_SW_NAME,
BLECharacteristic::PROPERTY_READ
);
swNameCharacteristic->setValue(swName);
auto* hwVerCharacteristic = service.createCharacteristic(
BLE_OTA_CHARACTERISTIC_UUID_HW_VER,
BLECharacteristic::PROPERTY_READ
);
hwVerCharacteristic->setValue((uint8_t*)(&hwVersion), sizeof(BleOtaVersion));
auto* swVerCharacteristic = service.createCharacteristic(
BLE_OTA_CHARACTERISTIC_UUID_SW_VER,
BLECharacteristic::PROPERTY_READ
);
swVerCharacteristic->setValue((uint8_t*)(&swVersion), sizeof(BleOtaVersion));
}

void ArduinoBleOTAClass::pull()
{
bleOtaUploader.pull();
}

void ArduinoBleOTAClass::enableUpload()
{
bleOtaUploader.setEnabling(true);
}

void ArduinoBleOTAClass::disableUpload()
{
bleOtaUploader.setEnabling(false);
}

void ArduinoBleOTAClass::setSecurityCallbacks(BleOtaSecurityCallbacks& cb)
{
securityCallbacks = &cb;
}

void ArduinoBleOTAClass::setUploadCallbacks(BleOtaUploadCallbacks& cb)
{
uploadCallbacks = &cb;
}

void ArduinoBleOTAClass::onWrite(BLECharacteristic* characteristic)
{
auto value = characteristic->getValue();
auto data = value.data();
auto length = value.length();

bleOtaUploader.onData((uint8_t*)data, length);
}

void ArduinoBleOTAClass::send(const uint8_t* data, size_t length)
{
txCharacteristic->setValue((uint8_t*)data, length);
txCharacteristic->notify();
}

ArduinoBleOTAClass ArduinoBleOTA{};
#endif
48 changes: 48 additions & 0 deletions src/ArduinoBleOtaClassNativeESP32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
#include "BleOtaStorage.h"
#include "BleOtaVersion.h"
#include "BleOtaSecurityCallbacks.h"
#include "BleOtaUploadCallbacks.h"
#include <BLEDevice.h>

class BleOtaUploader;

class ArduinoBleOTAClass: public BLECharacteristicCallbacks
{
public:
ArduinoBleOTAClass();

bool begin(const std::string& deviceName,
OTAStorage& storage,
const std::string& hwName = " ",
BleOtaVersion hwVersion = {},
const std::string& swName = " ",
BleOtaVersion swVersion = {},
bool enableUpload = true);
bool begin(OTAStorage& storage,
const std::string& hwName = " ",
BleOtaVersion hwVersion = {},
const std::string& swName = " ",
BleOtaVersion swVersion = {},
bool enableUpload = true);
void pull();

void enableUpload();
void disableUpload();
void setSecurityCallbacks(BleOtaSecurityCallbacks&);
void setUploadCallbacks(BleOtaUploadCallbacks&);

private:
friend BleOtaUploader;
void begin(BLEService& service,
const std::string& hwName, BleOtaVersion hwVersion,
const std::string& swName, BleOtaVersion swVersion);
void onWrite(BLECharacteristic* characteristic) override;
void send(const uint8_t* data, size_t length);

BLECharacteristic* txCharacteristic;
BleOtaSecurityCallbacks* securityCallbacks;
BleOtaUploadCallbacks* uploadCallbacks;
};

extern ArduinoBleOTAClass ArduinoBleOTA;
1 change: 1 addition & 0 deletions src/ArduinoBleOtaClassNimBle.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "BleOtaDefines.h"
#ifdef USE_NIM_BLE_ARDUINO_LIB
#include "ArduinoBleOtaClassNimBle.h"
#include "BleOtaUploader.h"
Expand Down
5 changes: 5 additions & 0 deletions src/BleOtaDefines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#if !defined(USE_NIM_BLE_ARDUINO_LIB) && !defined(USE_NATIVE_ESP32_BLE_LIB) && !defined(USE_ARDUINO_BLE_LIB)
#define USE_ARDUINO_BLE_LIB
#endif
Loading

0 comments on commit cfe9bf4

Please sign in to comment.