Skip to content

Commit 1fc7ade

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 89350fb commit 1fc7ade

37 files changed

+130
-1141
lines changed

src/helpers/nrf52/NRF52Board.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
#include "NRF52Board.h"
2+
#include <bluefruit.h>
3+
4+
static BLEDfu bledfu;
5+
6+
static void connect_callback(uint16_t conn_handle) {
7+
(void)conn_handle;
8+
MESH_DEBUG_PRINTLN("BLE client connected");
9+
}
10+
11+
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
12+
(void)conn_handle;
13+
(void)reason;
14+
15+
MESH_DEBUG_PRINTLN("BLE client disconnected");
16+
}
217

318
void NRF52Board::begin() {
419
startup_reason = BD_STARTUP_NORMAL;
@@ -15,4 +30,52 @@ void NRF52BoardDCDC::begin() {
1530
} else {
1631
NRF_POWER->DCDCEN = 1;
1732
}
33+
}
34+
35+
bool NRF52BoardOTA::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(ota_name);
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+
uint8_t mac_addr[6];
75+
memset(mac_addr, 0, sizeof(mac_addr));
76+
Bluefruit.getAddr(mac_addr);
77+
sprintf(reply, "OK - mac: %02X:%02X:%02X:%02X:%02X:%02X",
78+
mac_addr[5], mac_addr[4], mac_addr[3], mac_addr[2], mac_addr[1], mac_addr[0]);
79+
80+
return true;
1881
}

src/helpers/nrf52/NRF52Board.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ class NRF52Board : public mesh::MainBoard {
2929
class NRF52BoardDCDC : virtual public NRF52Board {
3030
public:
3131
virtual void begin() override;
32+
};
33+
34+
class NRF52BoardOTA : virtual public NRF52Board {
35+
private:
36+
char *ota_name;
37+
public:
38+
NRF52BoardOTA(char *name) : ota_name(name) {}
39+
virtual bool startOTAUpdate(const char *id, char reply[]) override;
3240
};
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#define SX126X_DIO3_TCXO_VOLTAGE 1.8
2222

2323

24-
class MeshSolarBoard : public NRF52Board {
24+
class MeshSolarBoard : public NRF52BoardOTA {
2525
public:
26+
MeshSolarBoard() : NRF52BoardOTA("MESH_SOLAR_OTA") {}
2627
void begin();
2728

2829
uint16_t getBattMilliVolts() override {
@@ -32,6 +33,4 @@ class MeshSolarBoard : public NRF52Board {
3233
const char* getManufacturerName() const override {
3334
return "Heltec Mesh Solar";
3435
}
35-
36-
bool startOTAUpdate(const char* id, char reply[]) override;
3736
};

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)