|
1 | 1 | #if defined(NRF52_PLATFORM) |
2 | 2 | #include "NRF52Board.h" |
3 | 3 |
|
| 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 | + |
4 | 20 | void NRF52Board::begin() { |
5 | 21 | startup_reason = BD_STARTUP_NORMAL; |
6 | 22 | } |
@@ -37,4 +53,52 @@ float NRF52Board::getMCUTemperature() { |
37 | 53 |
|
38 | 54 | return temp * 0.25f; // Convert to *C |
39 | 55 | } |
| 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 | +} |
40 | 104 | #endif |
0 commit comments