fbiego / ESP32_BLE_OTA_Arduino

OTA update on ESP32 via BLE
MIT License
206 stars 42 forks source link

Create simple ArduinoBleOTA library #19

Open vovagorodok opened 2 years ago

vovagorodok commented 2 years ago

Thanks for Your fullstack solution (from esp32 to android app)! I have forked this repository in order to create PlatformIO Arduino library: https://github.com/vovagorodok/ArduinoBleOTA

At the and will be nice to move it to: https://github.com/arduino-libraries

I'm trying to make it as simple as possible with reusing of NimBLE-Arduino and ArduinoOTA(support only WIFI) libraries. Proposed interface will be similar to ArduinoOTA:

#include <ArduinoBleOTA.h>
void setup() {
  ArduinoBleOTA.begin("ArduinoBleOTA", InternalStorage);  // or SDStorage or SerialFlashStorage
}

Or if bluetooth is used not only for OTA:

#include <ArduinoBleOTA.h>
void setup() {
    BLEDevice::init("MyBleDevice");
    auto* server = BLEDevice::createServer();
    ArduinoBleOTA.begin(server, InternalStorage);
    // add your BLE services here
    server->startAdvertising();
}

Additionally I have plan to add support of Atmel based boards using ArduinoBLE library.

I see that @Vincent-Stragier begin with that. Lets connect our forces and create easy library with simple/lightweight protocol.

Draft protocol proposal will have small number of commands (names and numbers can be another like 0x01 or 0xFE):

SIZE 1
PACKAGE 2
INSTALL 3
OK 4
NOK 5

Designations for examples:\ -> - recived from phone\ <- - send to phone

Sunny day scenario:

-> SIZE <size>
<- OK % open ota storage
-> PACKAGE <data>
<- OK
...
-> PACKAGE <data>
<- OK
-> INSTALL % full <data> size == <size>, apply binary
<- OK

Size to big:

-> SIZE <size>
<- NOK % do nothing

Connection terminated:

-> SIZE <size>
<- OK
-> PACKAGE <data>
<- OK
...
% termination
-> SIZE <size>
<- OK % reopen ota storage and write from scratch
...

Unexpected package:

...
-> PACKAGE <data> % full <data> size > <size>, close ota storage
<- NOK

Instalation error:

...
-> INSTALL % full <data> size < <size>
<- NOK

I thing that most/all corner cases are covered or something is missed? @fbiego can you describe scenarios(corner cases) that are currently covered with command names (because from code I see some magic numbers 0xFB, 0xFC, 0xFD, FE ..)? What do you think generally about this idea?

Thanks!

Vincent-Stragier commented 2 years ago

@vovagorodok,

If you take a look at: https://github.com/fbiego/ESP32_BLE_OTA_Arduino/blob/fa90638f2997d7965880c2cd6aca1e9b05c83ba2/esp32_ble_ota_lib_compact/ota_updater.py#L26-L175

Reading the above, you will have a rough idea of how the process works (I'm using a slightly different method compared to the main code).

As you can see, no security mechanism has been implemented in the code. No pin code for the pairing, no checksum at the end of the upload, no version number hard-coded—sometimes, I had a version date and git version hash at compilation time, but not here —, etc. That could also be something to implement in your library.

I do not have much time to help you at the moment, but I like your idea, even if there are still some mechanisms to be thought.

Regards, Vincent

vovagorodok commented 2 years ago

Ok, just let me know if you find corner case or bug in my considerations. Will be nice to implement minimal working version and than extend it. For example checksum can be added in future by optional command:

-> CHECKSUM <checksum>
<- OK

before INSTALL command. And developer of mobile app will decide if send it or not. But lets consider it after basic implementation.

I am also interested in @fbiego's point of view

vovagorodok commented 2 years ago

Hi guys,\ New library created: https://github.com/vovagorodok/ArduinoBleOTA \ It has built in checksum integrity protection and software/hardware type/version indication.\ Security/Authorization protection decided to not implement here, because it can be implemented as separate library (for whole bluetooth connection, not for one OTA service).

Library can be simply added to any project like this:\ https://github.com/vovagorodok/ArduinoBleOTA/blob/main/examples/basic/main.ino Or if there is any additional service:\ https://github.com/vovagorodok/ArduinoBleOTA/blob/main/examples/multiservice/main.ino

Next steps:

  1. Add new not ESP32 platforms (will be easy to add samd and other using ArduinoBLE library)
  2. Increase upload speed. Currently I have ~3kB/s. According to ~12kB/s FFat does it or we can achieve it with SPIFFS (default i gues)?
  3. Mobile application. Currently it updates only by 'updater.py' script. I'm not a mobile developer, some help/integration here will be nice
vovagorodok commented 2 years ago

According to step 2.\ Achieved ~12kB/s (usually ~10kB/s) even on default.csv. Now data partition type doesn't meter, because uploading does directly to ota partition. Problem was only on bluetooth side, because responses was sent after each package. I'll update main branch tomorrow

vovagorodok commented 2 years ago

Step 1. Done. Added new (not esp32/NimBLE-Arduino lib) platforms like samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla based on ArduinoBLE library. Plans to add stm32 and other

Step 2. Done. Works fine with BLE_OTA_ATTRIBUTE_SIZE=512 and BLE_OTA_BUFFER_SIZE~=512 * 10. Can be tuned or buffer can be completely disabled in order to save static memory: https://github.com/vovagorodok/ArduinoBleOTA/blob/main/doc/ADVANCED.md

Step3. Still open

sdetweil commented 1 year ago

I was referred to this URL on BLE performance.. https://punchthrough.com/ble-throughput-part-4/ one reported 50k/sec thruput..