Skip to content

Commit b024b9e

Browse files
committed
Deduplicate NRF52 startOTAUpdate()
The startOTAUpdate() is the same for all NRF52 boards. Use a common implementation for all boards that currently have a specific implementation. The following boards currently have an empty startOTAUpdate() for whatever reasons and therefore are not inheriting NRF52BoardOTA to keep the same state: Nano G2 Ultra, Seeed SenseCAP T1000-E, Wio WM1110. Signed-off-by: Frieder Schrempf <[email protected]>
1 parent e3bb225 commit b024b9e

37 files changed

+132
-1143
lines changed

src/helpers/NRF52Board.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
#if defined(NRF52_PLATFORM)
22
#include "NRF52Board.h"
33

4+
#include <bluefruit.h>
5+
6+
static BLEDfu bledfu;
7+
8+
static void connect_callback(uint16_t conn_handle) {
9+
(void)conn_handle;
10+
MESH_DEBUG_PRINTLN("BLE client connected");
11+
}
12+
13+
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
14+
(void)conn_handle;
15+
(void)reason;
16+
17+
MESH_DEBUG_PRINTLN("BLE client disconnected");
18+
}
19+
420
void NRF52Board::begin() {
521
startup_reason = BD_STARTUP_NORMAL;
622
}
@@ -37,4 +53,52 @@ float NRF52Board::getMCUTemperature() {
3753

3854
return temp * 0.25f; // Convert to *C
3955
}
56+
57+
bool NRF52BoardOTA::startOTAUpdate(const char *id, char reply[]) {
58+
// Config the peripheral connection with maximum bandwidth
59+
// more SRAM required by SoftDevice
60+
// Note: All config***() function must be called before begin()
61+
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
62+
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
63+
64+
Bluefruit.begin(1, 0);
65+
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
66+
Bluefruit.setTxPower(4);
67+
// Set the BLE device name
68+
Bluefruit.setName(ota_name);
69+
70+
Bluefruit.Periph.setConnectCallback(connect_callback);
71+
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
72+
73+
// To be consistent OTA DFU should be added first if it exists
74+
bledfu.begin();
75+
76+
// Set up and start advertising
77+
// Advertising packet
78+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
79+
Bluefruit.Advertising.addTxPower();
80+
Bluefruit.Advertising.addName();
81+
82+
/* Start Advertising
83+
- Enable auto advertising if disconnected
84+
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
85+
- Timeout for fast mode is 30 seconds
86+
- Start(timeout) with timeout = 0 will advertise forever (until connected)
87+
88+
For recommended advertising interval
89+
https://developer.apple.com/library/content/qa/qa1931/_index.html
90+
*/
91+
Bluefruit.Advertising.restartOnDisconnect(true);
92+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
93+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
94+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
95+
96+
uint8_t mac_addr[6];
97+
memset(mac_addr, 0, sizeof(mac_addr));
98+
Bluefruit.getAddr(mac_addr);
99+
sprintf(reply, "OK - mac: %02X:%02X:%02X:%02X:%02X:%02X", mac_addr[5], mac_addr[4], mac_addr[3],
100+
mac_addr[2], mac_addr[1], mac_addr[0]);
101+
102+
return true;
103+
}
40104
#endif

src/helpers/NRF52Board.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ class NRF52BoardDCDC : virtual public NRF52Board {
2727
public:
2828
virtual void begin() override;
2929
};
30+
31+
class NRF52BoardOTA : virtual public NRF52Board {
32+
private:
33+
char *ota_name;
34+
35+
public:
36+
NRF52BoardOTA(char *name) : ota_name(name) {}
37+
virtual bool startOTAUpdate(const char *id, char reply[]) override;
38+
};
3039
#endif
Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
#include <Arduino.h>
2-
#include "MeshSolarBoard.h"
3-
4-
#include <bluefruit.h>
52
#include <Wire.h>
63

7-
static BLEDfu bledfu;
8-
9-
static void connect_callback(uint16_t conn_handle)
10-
{
11-
(void)conn_handle;
12-
MESH_DEBUG_PRINTLN("BLE client connected");
13-
}
14-
15-
static void disconnect_callback(uint16_t conn_handle, uint8_t reason)
16-
{
17-
(void)conn_handle;
18-
(void)reason;
19-
20-
MESH_DEBUG_PRINTLN("BLE client disconnected");
21-
}
4+
#include "MeshSolarBoard.h"
225

236
void MeshSolarBoard::begin() {
247
NRF52Board::begin();
@@ -31,46 +14,3 @@ void MeshSolarBoard::begin() {
3114

3215
Wire.begin();
3316
}
34-
35-
bool MeshSolarBoard::startOTAUpdate(const char* id, char reply[]) {
36-
// Config the peripheral connection with maximum bandwidth
37-
// more SRAM required by SoftDevice
38-
// Note: All config***() function must be called before begin()
39-
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
40-
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
41-
42-
Bluefruit.begin(1, 0);
43-
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
44-
Bluefruit.setTxPower(4);
45-
// Set the BLE device name
46-
Bluefruit.setName("MESH_SOLAR_OTA");
47-
48-
Bluefruit.Periph.setConnectCallback(connect_callback);
49-
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
50-
51-
// To be consistent OTA DFU should be added first if it exists
52-
bledfu.begin();
53-
54-
// Set up and start advertising
55-
// Advertising packet
56-
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
57-
Bluefruit.Advertising.addTxPower();
58-
Bluefruit.Advertising.addName();
59-
60-
/* Start Advertising
61-
- Enable auto advertising if disconnected
62-
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
63-
- Timeout for fast mode is 30 seconds
64-
- Start(timeout) with timeout = 0 will advertise forever (until connected)
65-
66-
For recommended advertising interval
67-
https://developer.apple.com/library/content/qa/qa1931/_index.html
68-
*/
69-
Bluefruit.Advertising.restartOnDisconnect(true);
70-
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
71-
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
72-
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
73-
74-
strcpy(reply, "OK - started");
75-
return true;
76-
}

variants/heltec_mesh_solar/MeshSolarBoard.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
#define SX126X_DIO2_AS_RF_SWITCH true
2121
#define SX126X_DIO3_TCXO_VOLTAGE 1.8
2222

23-
24-
class MeshSolarBoard : public NRF52Board {
23+
class MeshSolarBoard : public NRF52BoardOTA {
2524
public:
25+
MeshSolarBoard() : NRF52BoardOTA("MESH_SOLAR_OTA") {}
2626
void begin();
2727

2828
uint16_t getBattMilliVolts() override {
@@ -32,6 +32,4 @@ class MeshSolarBoard : public NRF52Board {
3232
const char* getManufacturerName() const override {
3333
return "Heltec Mesh Solar";
3434
}
35-
36-
bool startOTAUpdate(const char* id, char reply[]) override;
3735
};

variants/heltec_t114/T114Board.cpp

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@
22

33
#include <Arduino.h>
44
#include <Wire.h>
5-
#include <bluefruit.h>
6-
7-
static BLEDfu bledfu;
8-
9-
static void connect_callback(uint16_t conn_handle) {
10-
(void)conn_handle;
11-
MESH_DEBUG_PRINTLN("BLE client connected");
12-
}
13-
14-
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
15-
(void)conn_handle;
16-
(void)reason;
17-
18-
MESH_DEBUG_PRINTLN("BLE client disconnected");
19-
}
205

216
void T114Board::begin() {
227
NRF52Board::begin();
@@ -38,47 +23,4 @@ void T114Board::begin() {
3823
pinMode(SX126X_POWER_EN, OUTPUT);
3924
digitalWrite(SX126X_POWER_EN, HIGH);
4025
delay(10); // give sx1262 some time to power up
41-
}
42-
43-
bool T114Board::startOTAUpdate(const char *id, char reply[]) {
44-
// Config the peripheral connection with maximum bandwidth
45-
// more SRAM required by SoftDevice
46-
// Note: All config***() function must be called before begin()
47-
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
48-
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
49-
50-
Bluefruit.begin(1, 0);
51-
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
52-
Bluefruit.setTxPower(4);
53-
// Set the BLE device name
54-
Bluefruit.setName("T114_OTA");
55-
56-
Bluefruit.Periph.setConnectCallback(connect_callback);
57-
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
58-
59-
// To be consistent OTA DFU should be added first if it exists
60-
bledfu.begin();
61-
62-
// Set up and start advertising
63-
// Advertising packet
64-
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
65-
Bluefruit.Advertising.addTxPower();
66-
Bluefruit.Advertising.addName();
67-
68-
/* Start Advertising
69-
- Enable auto advertising if disconnected
70-
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
71-
- Timeout for fast mode is 30 seconds
72-
- Start(timeout) with timeout = 0 will advertise forever (until connected)
73-
74-
For recommended advertising interval
75-
https://developer.apple.com/library/content/qa/qa1931/_index.html
76-
*/
77-
Bluefruit.Advertising.restartOnDisconnect(true);
78-
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
79-
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
80-
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
81-
82-
strcpy(reply, "OK - started");
83-
return true;
84-
}
26+
}

variants/heltec_t114/T114Board.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#define PIN_BAT_CTL 6
1010
#define MV_LSB (3000.0F / 4096.0F) // 12-bit ADC with 3.0V input range
1111

12-
class T114Board : public NRF52Board {
12+
class T114Board : public NRF52BoardOTA {
1313
public:
14+
T114Board() : NRF52BoardOTA("T114_OTA") {}
1415
void begin();
1516

1617
#if defined(P_LORA_TX_LED)
@@ -50,6 +51,4 @@ class T114Board : public NRF52Board {
5051
#endif
5152
sd_power_system_off();
5253
}
53-
54-
bool startOTAUpdate(const char* id, char reply[]) override;
5554
};

variants/ikoka_handheld_nrf/IkokaNrf52Board.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,9 @@
22

33
#include <Arduino.h>
44
#include <Wire.h>
5-
#include <bluefruit.h>
65

76
#include "IkokaNrf52Board.h"
87

9-
static BLEDfu bledfu;
10-
11-
static void connect_callback(uint16_t conn_handle) {
12-
(void)conn_handle;
13-
MESH_DEBUG_PRINTLN("BLE client connected");
14-
}
15-
16-
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
17-
(void)conn_handle;
18-
(void)reason;
19-
20-
MESH_DEBUG_PRINTLN("BLE client disconnected");
21-
}
22-
238
void IkokaNrf52Board::begin() {
249
NRF52Board::begin();
2510

@@ -52,48 +37,4 @@ void IkokaNrf52Board::begin() {
5237
delay(10); // give sx1262 some time to power up
5338
}
5439

55-
bool IkokaNrf52Board::startOTAUpdate(const char *id, char reply[]) {
56-
// Config the peripheral connection with maximum bandwidth
57-
// more SRAM required by SoftDevice
58-
// Note: All config***() function must be called before begin()
59-
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
60-
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
61-
62-
Bluefruit.begin(1, 0);
63-
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
64-
Bluefruit.setTxPower(4);
65-
// Set the BLE device name
66-
Bluefruit.setName("XIAO_NRF52_OTA");
67-
68-
Bluefruit.Periph.setConnectCallback(connect_callback);
69-
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
70-
71-
// To be consistent OTA DFU should be added first if it exists
72-
bledfu.begin();
73-
74-
// Set up and start advertising
75-
// Advertising packet
76-
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
77-
Bluefruit.Advertising.addTxPower();
78-
Bluefruit.Advertising.addName();
79-
80-
/* Start Advertising
81-
- Enable auto advertising if disconnected
82-
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
83-
- Timeout for fast mode is 30 seconds
84-
- Start(timeout) with timeout = 0 will advertise forever (until connected)
85-
86-
For recommended advertising interval
87-
https://developer.apple.com/library/content/qa/qa1931/_index.html
88-
*/
89-
Bluefruit.Advertising.restartOnDisconnect(true);
90-
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
91-
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
92-
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
93-
94-
strcpy(reply, "OK - started");
95-
96-
return true;
97-
}
98-
9940
#endif

variants/ikoka_handheld_nrf/IkokaNrf52Board.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
#ifdef IKOKA_NRF52
88

9-
class IkokaNrf52Board : public NRF52Board {
9+
class IkokaNrf52Board : public NRF52BoardOTA {
1010
public:
11+
IkokaNrf52Board() : NRF52BoardOTA("XIAO_NRF52_OTA") {}
1112
void begin();
1213

1314
#if defined(P_LORA_TX_LED)
@@ -38,8 +39,6 @@ class IkokaNrf52Board : public NRF52Board {
3839
const char* getManufacturerName() const override {
3940
return "Ikoka Handheld E22 30dBm (Xiao_nrf52)";
4041
}
41-
42-
bool startOTAUpdate(const char* id, char reply[]) override;
4342
};
4443

4544
#endif

0 commit comments

Comments
 (0)