|
| 1 | +#include "ble_init.h" |
| 2 | +#include "structs.h" |
| 3 | +#include "encryption.h" |
| 4 | +#include "display_service.h" |
| 5 | + |
| 6 | +#ifdef TARGET_NRF |
| 7 | +#include <bluefruit.h> |
| 8 | +extern "C" { |
| 9 | +#include "nrf_soc.h" |
| 10 | +} |
| 11 | +extern BLEDfu bledfu; |
| 12 | +extern BLEService imageService; |
| 13 | +extern BLECharacteristic imageCharacteristic; |
| 14 | +extern struct GlobalConfig globalConfig; |
| 15 | +void connect_callback(uint16_t conn_handle); |
| 16 | +void disconnect_callback(uint16_t conn_handle, uint8_t reason); |
| 17 | +void imageDataWritten(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len); |
| 18 | +String getChipIdHex(); |
| 19 | +void writeSerial(String message, bool newLine = true); |
| 20 | +#endif |
| 21 | + |
| 22 | +#ifdef TARGET_ESP32 |
| 23 | +#include <BLEDevice.h> |
| 24 | +#include <BLEServer.h> |
| 25 | +#include <BLEUtils.h> |
| 26 | +#include <BLEAdvertising.h> |
| 27 | + |
| 28 | +String getChipIdHex(); |
| 29 | +void writeSerial(String message, bool newLine = true); |
| 30 | +#include "esp32_ble_callbacks.h" |
| 31 | + |
| 32 | +extern struct GlobalConfig globalConfig; |
| 33 | +extern BLEServer* pServer; |
| 34 | +extern BLEService* pService; |
| 35 | +extern BLECharacteristic* pTxCharacteristic; |
| 36 | +extern BLECharacteristic* pRxCharacteristic; |
| 37 | +extern BLEAdvertisementData* advertisementData; |
| 38 | +extern MyBLEServerCallbacks staticServerCallbacks; |
| 39 | +extern MyBLECharacteristicCallbacks staticCharCallbacks; |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifdef TARGET_NRF |
| 43 | +void ble_nrf_stack_init() { |
| 44 | + Bluefruit.configCentralBandwidth(BANDWIDTH_MAX); |
| 45 | + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
| 46 | + Bluefruit.autoConnLed(false); |
| 47 | + Bluefruit.setTxPower(globalConfig.power_option.tx_power); |
| 48 | + Bluefruit.begin(1, 0); |
| 49 | + if (!isEncryptionEnabled()) { |
| 50 | + bledfu.begin(); |
| 51 | + writeSerial("BLE DFU initialized successfully (encryption disabled)"); |
| 52 | + } else { |
| 53 | + writeSerial("BLE DFU service NOT initialized (encryption enabled - use CMD_ENTER_DFU)"); |
| 54 | + } |
| 55 | + writeSerial("BLE initialized successfully"); |
| 56 | + writeSerial("Setting up BLE service 0x2446..."); |
| 57 | + imageService.begin(); |
| 58 | + writeSerial("BLE service started"); |
| 59 | + imageCharacteristic.setWriteCallback(imageDataWritten); |
| 60 | + writeSerial("BLE write callback set"); |
| 61 | + imageCharacteristic.begin(); |
| 62 | + writeSerial("BLE characteristic started"); |
| 63 | + Bluefruit.Periph.setConnectCallback(connect_callback); |
| 64 | + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
| 65 | + writeSerial("BLE callbacks registered"); |
| 66 | + String deviceName = "OD" + getChipIdHex(); |
| 67 | + Bluefruit.setName(deviceName.c_str()); |
| 68 | + writeSerial("Device name set to: " + deviceName); |
| 69 | + writeSerial("Configuring power management..."); |
| 70 | + sd_power_mode_set(NRF_POWER_MODE_LOWPWR); |
| 71 | + sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE); |
| 72 | + writeSerial("Power management configured"); |
| 73 | +} |
| 74 | + |
| 75 | +void ble_nrf_advertising_start() { |
| 76 | + writeSerial("Configuring BLE advertising..."); |
| 77 | + Bluefruit.Advertising.clearData(); |
| 78 | + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
| 79 | + Bluefruit.Advertising.addName(); |
| 80 | + updatemsdata(); |
| 81 | + Bluefruit.Advertising.restartOnDisconnect(true); |
| 82 | + Bluefruit.Advertising.setInterval(32, 200); |
| 83 | + Bluefruit.Advertising.setFastTimeout(10); |
| 84 | + writeSerial("Starting BLE advertising..."); |
| 85 | + Bluefruit.Advertising.start(0); |
| 86 | +} |
| 87 | +#endif |
| 88 | + |
| 89 | +#ifdef TARGET_ESP32 |
| 90 | +void ble_init() { |
| 91 | + ble_init_esp32(true); |
| 92 | +} |
| 93 | +#endif |
| 94 | + |
| 95 | +#ifdef TARGET_ESP32 |
| 96 | +void ble_init_esp32(bool update_manufacturer_data) { |
| 97 | + writeSerial("=== Initializing ESP32 BLE ==="); |
| 98 | + String deviceName = "OD" + getChipIdHex(); |
| 99 | + writeSerial("Device name will be: " + deviceName); |
| 100 | + BLEDevice::init(deviceName.c_str()); |
| 101 | + writeSerial("Setting BLE MTU to 512..."); |
| 102 | + BLEDevice::setMTU(512); |
| 103 | + pServer = BLEDevice::createServer(); |
| 104 | + if (pServer == nullptr) { |
| 105 | + writeSerial("ERROR: Failed to create BLE server"); |
| 106 | + return; |
| 107 | + } |
| 108 | + pServer->setCallbacks(&staticServerCallbacks); |
| 109 | + writeSerial("Server callbacks configured"); |
| 110 | + BLEUUID serviceUUID("00002446-0000-1000-8000-00805F9B34FB"); |
| 111 | + pService = pServer->createService(serviceUUID); |
| 112 | + if (pService == nullptr) { |
| 113 | + writeSerial("ERROR: Failed to create BLE service"); |
| 114 | + return; |
| 115 | + } |
| 116 | + writeSerial("BLE service 0x2446 created successfully"); |
| 117 | + BLEUUID charUUID("00002446-0000-1000-8000-00805F9B34FB"); |
| 118 | + pTxCharacteristic = pService->createCharacteristic( |
| 119 | + charUUID, |
| 120 | + BLECharacteristic::PROPERTY_READ | |
| 121 | + BLECharacteristic::PROPERTY_NOTIFY | |
| 122 | + BLECharacteristic::PROPERTY_WRITE | |
| 123 | + BLECharacteristic::PROPERTY_WRITE_NR |
| 124 | + ); |
| 125 | + if (pTxCharacteristic == nullptr) { |
| 126 | + writeSerial("ERROR: Failed to create BLE characteristic"); |
| 127 | + return; |
| 128 | + } |
| 129 | + writeSerial("Characteristic created with properties: READ, NOTIFY, WRITE, WRITE_NR"); |
| 130 | + pTxCharacteristic->setCallbacks(&staticCharCallbacks); |
| 131 | + pRxCharacteristic = pTxCharacteristic; |
| 132 | + pService->start(); |
| 133 | + BLEAdvertising* pAdvertising = BLEDevice::getAdvertising(); |
| 134 | + if (pAdvertising == nullptr) { |
| 135 | + writeSerial("ERROR: Failed to get advertising object"); |
| 136 | + return; |
| 137 | + } |
| 138 | + pAdvertising->addServiceUUID(serviceUUID); |
| 139 | + writeSerial("Service UUID added to advertising"); |
| 140 | + advertisementData->setName(deviceName); |
| 141 | + advertisementData->setFlags(0x06); |
| 142 | + writeSerial("Device name added to advertising"); |
| 143 | + if (update_manufacturer_data) { |
| 144 | + updatemsdata(); |
| 145 | + } |
| 146 | + pAdvertising->setAdvertisementData(*advertisementData); |
| 147 | + pAdvertising->setScanResponse(false); |
| 148 | + pAdvertising->setMinPreferred(0x0006); |
| 149 | + pAdvertising->setMinPreferred(0x0012); |
| 150 | + writeSerial("Advertising intervals set"); |
| 151 | + pServer->getAdvertising()->setMinPreferred(0x06); |
| 152 | + pServer->getAdvertising()->setMinPreferred(0x12); |
| 153 | + pServer->getAdvertising()->start(); |
| 154 | + writeSerial("=== BLE advertising started successfully ==="); |
| 155 | + writeSerial("Device ready: " + deviceName); |
| 156 | + writeSerial("Waiting for BLE connections..."); |
| 157 | +} |
| 158 | +#endif |
0 commit comments