bolar_1 date 2026-7-1 d:/proc
This commit is contained in:
235
main/button_config.cc
Normal file
235
main/button_config.cc
Normal file
@@ -0,0 +1,235 @@
|
||||
#include "button_config.h"
|
||||
|
||||
#include "application.h"
|
||||
#include "settings.h"
|
||||
#include "system_info.h"
|
||||
#include "ble/friend_exchange.h"
|
||||
#include "boards/board.h"
|
||||
#include "boards/wifi_board.h"
|
||||
#include "boards/dual_network_board.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_system.h>
|
||||
#include <esp_timer.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <cstdio>
|
||||
#include <set>
|
||||
#define TAG "BtnCfg"
|
||||
|
||||
namespace {
|
||||
constexpr const char* kNvsNs = "btn_cfg";
|
||||
|
||||
// Whitelist of server-settable actions. Anything outside is rejected.
|
||||
const std::set<std::string>& AllowedActions() {
|
||||
static const std::set<std::string> s = {
|
||||
"friend_mode",
|
||||
"network_switch",
|
||||
"nothing",
|
||||
};
|
||||
return s;
|
||||
}
|
||||
|
||||
const char* LegacyDefault(int count) {
|
||||
switch (count) {
|
||||
case 3: return "friend_mode";
|
||||
case 4: return "network_switch";
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
std::string NvsKey(int count) {
|
||||
char key[4];
|
||||
snprintf(key, sizeof(key), "c%d", count);
|
||||
return std::string(key);
|
||||
}
|
||||
|
||||
void WriteNvs(int count, const char* action) {
|
||||
Settings s(kNvsNs, true);
|
||||
s.SetString(NvsKey(count).c_str(), std::string(action));
|
||||
}
|
||||
|
||||
std::string ReadNvs(int count) {
|
||||
Settings s(kNvsNs, false);
|
||||
return s.GetString(NvsKey(count).c_str());
|
||||
}
|
||||
|
||||
// ─── Friend mode WS gating ─────────────────────────────────────────────
|
||||
// 三连击进 friend mode 时立即关闭 WS 通道,避免 BLE 占用 task slot 期间
|
||||
// server 推流到 web_socket.cc:OnTcpData 创 std::thread 失败 abort。
|
||||
// friend mode 退出(成功 / 30s 超时)后立即重新打开 WS。
|
||||
// pending_friend_add_json:成功配对时缓存待发的 friend_add 消息,等 WS 重连
|
||||
// 后再发出去(因为 cb 触发时 WS 还没连上)。
|
||||
namespace {
|
||||
std::string g_pending_friend_add_json;
|
||||
}
|
||||
|
||||
void DoFriendMode() {
|
||||
auto& app = Application::GetInstance();
|
||||
|
||||
// 互斥 1:BLE 配网中不允许进 friend mode(NimBLE 单实例资源)
|
||||
auto* dual = dynamic_cast<DualNetworkBoard*>(&Board::GetInstance());
|
||||
auto* wifi_board = dynamic_cast<WifiBoard*>(&Board::GetInstance());
|
||||
bool in_provisioning = (dual && dual->IsWifiConfigMode()) ||
|
||||
(wifi_board && wifi_board->IsWifiConfigMode());
|
||||
if (in_provisioning) {
|
||||
ESP_LOGW(TAG, "Friend mode rejected: device in BLE provisioning");
|
||||
return;
|
||||
}
|
||||
// 互斥 2:friend mode 已激活则忽略重复触发
|
||||
if (FriendExchange::IsActive()) {
|
||||
ESP_LOGW(TAG, "Friend mode already active, ignoring triple-click");
|
||||
return;
|
||||
}
|
||||
|
||||
app.audio_service_.PlayConfigSound();
|
||||
|
||||
// 通知 server 进入 friend 状态。这里不能用 SendTextMessage(),它只是异步
|
||||
// Schedule;如果随后立刻 CloseAudioChannel,消息会在主循环里被跳过。
|
||||
if (app.GetProtocol() && app.GetProtocol()->IsAudioChannelOpened()) {
|
||||
app.GetProtocol()->SendText("{\"type\":\"friend_mode\",\"action\":\"start\"}");
|
||||
}
|
||||
|
||||
// 关闭 WS:BLE 期间 server 不会推流,避免 OnTcpData 创 std::thread 崩
|
||||
if (app.GetProtocol() && app.GetProtocol()->IsAudioChannelOpened()) {
|
||||
ESP_LOGI(TAG, "Friend mode: closing WS to free task slots for BLE");
|
||||
app.GetProtocol()->CloseAudioChannel();
|
||||
}
|
||||
g_pending_friend_add_json.clear();
|
||||
|
||||
FriendInfo local;
|
||||
local.mac = SystemInfo::GetMacAddress();
|
||||
// BLE adv name stays "BorlaF-XXXX" so the scan filter in friend_exchange.cc
|
||||
// (`name.find("BorlaF-") != 0`) still matches — otherwise two nicknamed
|
||||
// devices can never see each other.
|
||||
local.name = std::string("BorlaF-")
|
||||
+ local.mac.substr(local.mac.size() - 5, 2)
|
||||
+ local.mac.substr(local.mac.size() - 2, 2);
|
||||
|
||||
// User-set nickname (cached in NVS from ServerHello.device_name). Carried
|
||||
// over the friend_add WS message so backend stores the miniprogram-side
|
||||
// nickname as friend_name, which is what ASR "给XX打电话" resolves against.
|
||||
// Server falls back to backend.device.name for the remote side's nickname.
|
||||
std::string local_friendly = Settings("device", false).GetString("name");
|
||||
if (local_friendly.empty()) local_friendly = local.name;
|
||||
|
||||
FriendExchange::StartFriendMode(
|
||||
local,
|
||||
// on_complete:配对成功 — 保存到 NVS,缓存 friend_add 消息(等 WS 重连后发)
|
||||
[local_friendly](const FriendInfo& me, const FriendInfo& peer) {
|
||||
ESP_LOGI(TAG, "Friend paired: %s (base MAC: %s)", peer.name.c_str(), peer.mac.c_str());
|
||||
Settings friends_store("friends", true);
|
||||
int count = friends_store.GetInt("count", 0);
|
||||
bool exists = false;
|
||||
for (int i = 0; i < count; i++) {
|
||||
std::string key = "f_" + std::to_string(i);
|
||||
std::string val = friends_store.GetString(key.c_str());
|
||||
if (val.find(peer.mac) != std::string::npos) {
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!exists && count < 20) {
|
||||
char json[128];
|
||||
snprintf(json, sizeof(json), "{\"mac\":\"%s\",\"name\":\"%s\"}",
|
||||
peer.mac.c_str(), peer.name.c_str());
|
||||
std::string slot_key = "f_" + std::to_string(count);
|
||||
friends_store.SetString(slot_key.c_str(), json);
|
||||
friends_store.SetInt("count", count + 1);
|
||||
ESP_LOGI(TAG, "Friend saved to NVS: slot %d", count);
|
||||
}
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf),
|
||||
"{\"type\":\"friend_add\",\"local_mac\":\"%s\",\"local_name\":\"%s\","
|
||||
"\"remote_mac\":\"%s\",\"remote_name\":\"%s\"}",
|
||||
me.mac.c_str(), local_friendly.c_str(),
|
||||
peer.mac.c_str(), peer.name.c_str());
|
||||
// 缓存到全局,等 on_stopped 里 WS 重连后再发
|
||||
g_pending_friend_add_json = std::string(buf);
|
||||
Application::GetInstance().audio_service_.PlayConfigSound();
|
||||
},
|
||||
// on_stopped:友好或超时退出都触发;恢复 WS,必要时发送 friend_add
|
||||
[](bool paired) {
|
||||
ESP_LOGI(TAG, "Friend mode exited (paired=%d), reopening WS", paired ? 1 : 0);
|
||||
auto& app2 = Application::GetInstance();
|
||||
// 异步在主事件循环里 reconnect,避免在 esp_timer task 里做 SSL 握手
|
||||
app2.Schedule([paired]() {
|
||||
auto& app3 = Application::GetInstance();
|
||||
if (app3.GetProtocol() && !app3.GetProtocol()->IsAudioChannelOpened()) {
|
||||
if (!app3.GetProtocol()->OpenAudioChannel()) {
|
||||
ESP_LOGW(TAG, "WS reopen failed after friend mode (lazy retry will pick up)");
|
||||
}
|
||||
}
|
||||
if (paired && !g_pending_friend_add_json.empty() &&
|
||||
app3.GetProtocol() && app3.GetProtocol()->IsAudioChannelOpened()) {
|
||||
app3.GetProtocol()->SendText(g_pending_friend_add_json);
|
||||
g_pending_friend_add_json.clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void DoNetworkSwitch() {
|
||||
auto& app = Application::GetInstance();
|
||||
app.audio_service_.PlayConfigSound();
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
//2026-6-14 ckkj debug
|
||||
// 四击实现网络切换
|
||||
auto& board = Board::GetInstance();
|
||||
auto* dual = dynamic_cast<DualNetworkBoard*>(&board);
|
||||
if (dual) {
|
||||
dual->SwitchNetworkType(); // 可以4g-->wifi-->4g
|
||||
}
|
||||
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
void DoNothing() {
|
||||
ESP_LOGI(TAG, "Action 'nothing' — no-op");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace button_config {
|
||||
|
||||
void UpdateFromHello(const char* action_for_3, const char* action_for_4) {
|
||||
const auto& allowed = AllowedActions();
|
||||
auto apply = [&allowed](int count, const char* action) {
|
||||
if (!action || !*action) return;
|
||||
if (allowed.count(action) == 0) {
|
||||
ESP_LOGW(TAG, "Rejected unknown action for click=%d: %s", count, action);
|
||||
return;
|
||||
}
|
||||
WriteNvs(count, action);
|
||||
ESP_LOGI(TAG, "button_map[%d] = %s", count, action);
|
||||
};
|
||||
apply(3, action_for_3);
|
||||
apply(4, action_for_4);
|
||||
}
|
||||
|
||||
std::string GetAction(int count) {
|
||||
if (count != 3 && count != 4) return "";
|
||||
|
||||
std::string v = ReadNvs(count);
|
||||
if (!v.empty() && AllowedActions().count(v) > 0) {
|
||||
return v;
|
||||
}
|
||||
|
||||
// Fallback to legacy hardcoded default
|
||||
const char* def = LegacyDefault(count);
|
||||
return def ? std::string(def) : std::string();
|
||||
}
|
||||
|
||||
void Execute(const std::string& action) {
|
||||
if (action == "friend_mode") {
|
||||
DoFriendMode();
|
||||
} else if (action == "network_switch") {
|
||||
DoNetworkSwitch();
|
||||
} else if (action == "nothing") {
|
||||
DoNothing();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unknown action '%s' — ignored", action.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace button_config
|
||||
Reference in New Issue
Block a user