Files
bolar_1/components/esp-ble-prov/include/ble_prov.h

117 lines
3.8 KiB
C++

#pragma once
#include <functional>
#include <string>
#include <vector>
// WiFi network info from scan
struct BleProvWifiNetwork {
std::string ssid;
int rssi;
int auth;
};
// Forward declarations for NimBLE types used in friend declarations
struct ble_gap_event;
struct ble_gatt_access_ctxt;
// BLE Provisioning GATT Server
// Replaces WifiConfigurationAp for device WiFi provisioning via BLE
class BleProv {
public:
static BleProv& GetInstance();
// Set device name suffix (MAC last 4 hex chars)
void SetDeviceName(const std::string& suffix);
// Set device info for BLE DeviceInfo characteristic
void SetDeviceInfo(const std::string& mac, const std::string& version);
// Callbacks
using WifiConfigCallback = std::function<void(const std::string& ssid, const std::string& password)>;
using RebootCallback = std::function<void()>;
void SetWifiConfigCallback(WifiConfigCallback cb);
void SetRebootCallback(RebootCallback cb);
// Lifecycle
void Start();
void Stop();
// Called by wifi_board after WiFi connect attempt
void NotifyWifiResult(bool success, const std::string& error);
// Called by wifi_board with scan results
void NotifyWifiScanResults(const std::vector<BleProvWifiNetwork>& networks);
// Pre-scan results pushed from wifi_board BEFORE BLE starts. Returned
// verbatim on every BLE WifiScan request to avoid the BLE/WiFi
// coexistence deadlock where notifications fail to deliver if a WiFi
// scan runs while the BLE connection is active.
void SetCachedNetworks(const std::vector<BleProvWifiNetwork>& networks);
size_t GetCachedNetworkCount() const { return cached_networks_.size(); }
bool IsRunning() const { return running_; }
// Called by NimBLE sync callback and GAP disconnect to (re)start advertising
void StartAdvertising();
// Allow static C callbacks to access internal state
friend int ble_prov_gap_event(struct ble_gap_event* event, void* arg);
friend int ble_prov_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt* ctxt, void* arg);
private:
BleProv() = default;
~BleProv() = default;
BleProv(const BleProv&) = delete;
BleProv& operator=(const BleProv&) = delete;
int HandleDeviceInfoRead(struct ble_gatt_access_ctxt* ctxt);
int HandleWifiScanWrite(struct ble_gatt_access_ctxt* ctxt);
int HandleWifiConfigWrite(struct ble_gatt_access_ctxt* ctxt);
int HandleCommandWrite(struct ble_gatt_access_ctxt* ctxt);
int HandleSavedWifiWrite(struct ble_gatt_access_ctxt* ctxt);
// Async task runners (avoid blocking NimBLE thread)
static void WifiScanTask(void* arg);
static void WifiConfigTask(void* arg);
static void SavedWifiListTask(void* arg);
static void SavedWifiDeleteTask(void* arg);
void SendChunkedNotification(uint16_t attr_handle, const std::string& json_data);
WifiConfigCallback wifi_config_cb_;
RebootCallback reboot_cb_;
std::string device_name_ = "Borla";
std::string device_mac_;
std::string device_version_;
bool running_ = false;
// BLE connection state
uint16_t conn_handle_ = 0;
bool connected_ = false;
// GATT attribute handles (set during registration)
uint16_t wifi_scan_notify_handle_ = 0;
uint16_t wifi_result_notify_handle_ = 0;
uint16_t saved_wifi_notify_handle_ = 0;
// Receive buffer for chunked writes
std::string wifi_config_buffer_;
// Pending WiFi config for async task
std::string pending_ssid_;
std::string pending_password_;
// Pending saved WiFi delete index
int pending_delete_index_ = -1;
// Negotiated MTU
uint16_t current_mtu_ = 23;
// Pre-scan WiFi network cache (populated before BLE starts)
std::vector<BleProvWifiNetwork> cached_networks_;
};