bolar_1 date 2026-7-1 d:/proc
This commit is contained in:
56
main/ble/friend_exchange.h
Normal file
56
main/ble/friend_exchange.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
/**
|
||||
* BLE Friend Exchange module.
|
||||
*
|
||||
* Handles BLE advertise/scan based device-to-device friend pairing.
|
||||
* Flow:
|
||||
* 1. Triple-click → StartFriendMode()
|
||||
* 2. Advertise as "BorlaF-XXXX" + scan for "BorlaF-*"
|
||||
* 3. On discovery: derive peer identity from BorlaF-* advertisement
|
||||
* 4. Report to server via WebSocket friend_add message
|
||||
* 5. Auto-close BLE after timeout (30s)
|
||||
*
|
||||
* This module intentionally does not register the BLE provisioning GATT
|
||||
* service. Double-click provisioning owns that service in BleProv.
|
||||
*/
|
||||
|
||||
struct FriendInfo {
|
||||
std::string mac;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
// Callback when friend exchange completes successfully
|
||||
using FriendExchangeCallback = std::function<void(const FriendInfo& local, const FriendInfo& remote)>;
|
||||
|
||||
// Callback fired when friend mode exits — either after a successful pairing
|
||||
// (right after FriendExchangeCallback) or after a 30s timeout. Always called
|
||||
// exactly once per StartFriendMode invocation, on the esp_timer task.
|
||||
// Caller uses this to restore resources it disabled at start time (e.g. WS).
|
||||
using FriendExchangeOnStopped = std::function<void(bool paired)>;
|
||||
|
||||
class FriendExchange {
|
||||
public:
|
||||
/**
|
||||
* Start BLE friend discovery mode.
|
||||
* Advertises and scans simultaneously for 30 seconds.
|
||||
* Calls on_complete when a friend is successfully exchanged.
|
||||
* Calls on_stopped at the end (paired or timeout) — always once.
|
||||
*/
|
||||
static void StartFriendMode(const FriendInfo& local_info,
|
||||
FriendExchangeCallback on_complete,
|
||||
FriendExchangeOnStopped on_stopped = nullptr);
|
||||
|
||||
/**
|
||||
* Stop friend mode early (e.g., button press to cancel).
|
||||
*/
|
||||
static void StopFriendMode();
|
||||
|
||||
/**
|
||||
* Check if friend mode is currently active.
|
||||
*/
|
||||
static bool IsActive();
|
||||
};
|
||||
Reference in New Issue
Block a user