| 338 | // --------------------------------------------------------------------------- |
| 339 | |
| 340 | TransportState* createTransport(const char* deviceName) FL_NOEXCEPT { |
| 341 | auto uptr = fl::make_unique<TransportState>(); |
| 342 | auto* state = uptr.get(); |
| 343 | |
| 344 | // Set static state pointer for on_sync callback (which has no void* arg) |
| 345 | s_bleState = state; |
| 346 | |
| 347 | // Initialize NimBLE stack (IDF 5+ API) |
| 348 | int rc = nimble_port_init(); |
| 349 | if (rc != 0) { |
| 350 | FL_WARN("[BLE] nimble_port_init failed rc=" << rc); |
| 351 | s_bleState = nullptr; |
| 352 | return nullptr; |
| 353 | } |
| 354 | |
| 355 | // Configure NimBLE host |
| 356 | ble_hs_cfg.sync_cb = fl_ble_on_sync; |
| 357 | |
| 358 | // Initialize GAP and GATT services |
| 359 | ble_svc_gap_init(); |
| 360 | ble_svc_gatt_init(); |
| 361 | |
| 362 | // Patch GATT table with state pointers and register |
| 363 | fl_ble_patch_gatt_table(state); |
| 364 | rc = ble_gatts_count_cfg(fl_gatt_svr_svcs_mut); |
| 365 | if (rc != 0) { |
| 366 | FL_WARN("[BLE] gatts_count_cfg failed rc=" << rc); |
| 367 | nimble_port_deinit(); |
| 368 | s_bleState = nullptr; |
| 369 | return nullptr; |
| 370 | } |
| 371 | rc = ble_gatts_add_svcs(fl_gatt_svr_svcs_mut); |
| 372 | if (rc != 0) { |
| 373 | FL_WARN("[BLE] gatts_add_svcs failed rc=" << rc); |
| 374 | nimble_port_deinit(); |
| 375 | s_bleState = nullptr; |
| 376 | return nullptr; |
| 377 | } |
| 378 | |
| 379 | // Set device name |
| 380 | rc = ble_svc_gap_device_name_set(deviceName); |
| 381 | if (rc != 0) { |
| 382 | FL_WARN("[BLE] device_name_set failed rc=" << rc); |
| 383 | } |
| 384 | |
| 385 | // Start NimBLE host task on FreeRTOS |
| 386 | nimble_port_freertos_init(fl_ble_host_task); |
| 387 | |
| 388 | FL_WARN("[BLE] GATT server started: name=" << deviceName); |
| 389 | uptr.release(); // caller owns the state now |
| 390 | return state; |
| 391 | } |
| 392 | |
| 393 | void destroyTransport(TransportState* state) FL_NOEXCEPT { |
| 394 | if (!state) return; |
nothing calls this directly
no test coverage detected