feat(bt): 蓝牙P1 — netmgrd BT_CTL权威收口(hcitool con判连接/paired-devices补名/a2dp详情/connect-disconnect-pair-unpair-scan)+launcher蓝牙子页sub_page_t重做(8分组,数据全走netmgrd)+设置键audio.output_bt/bt.codec;修act冲突+sub悬垂;宿主82PASS+bt_ctl/bt_page全过

This commit is contained in:
AskaEth committed 2026-09-03 20:01:13 +08:00
1 parent 8c677f127f
commit eb07415f7b
8 files changed
+1178 -90

No files matched your search

+144 -19
View File
@@ -151,6 +151,15 @@ static void nm_decode_ssid(const char *src, char *dst, int dlen)
dst[o] = 0;
}
/* P1:拷一个「空格分隔的值 token」到 dst(a2dp 字段 extraction 用)。 */
static void nm_val_field(char **p, char *dst, int dlen)
{
while (**p == ' ' || **p == '\t') (*p)++;
int o = 0;
while (**p && **p != ' ' && **p != '\t' && **p != '\n' && o < dlen - 1) dst[o++] = *(*p)++;
dst[o] = 0;
}
/* —— netmgrd `wifi status` 单行摘要解析:`wifi state=X connected=N ssid=S ip=I rssi=R`。
* 已知字段以「空格+key=」分隔;ssid 可能含空格,需读到下一个已知字段为止。 —— */
static int nm_is_status_key(const char *p)
@@ -432,7 +441,9 @@ int nm_wifi_toggle(int on)
}
/* ================= BT(IPC 优先:on/off/status/paired;其余子命令 netmgrd 无,直连)================= */
/* ================= BT(IPC 优先:全量子命令权威收口 P1;不可达回退直连)================= */
static int nm_parse_devices(const char *txt, meow_net_t *out, int max); /* 前向声明 */
int nm_bt_powered(void)
{
@@ -478,28 +489,142 @@ int nm_bt_paired(meow_net_t *out, int max)
int rc = netmgr_bt("paired", NULL, text, sizeof text);
if (rc == NETMGR_UNREACH) return net_bt_paired(out, max);
if (rc != MEOW_OK) return 0;
return nm_parse_devices(text, out, max);
}
/* ---- P1 收口:连接/断开/配对/扫描/A2DP 全部迁 netmgrd(权威),不可达回退直连 ---- */
/* 通用 `Device <MAC> <Name>` 行解析(netmgrd bt {connected,paired,devices} 文本;launcher 只做客户端)。 */
static int nm_parse_devices(const char *txt, meow_net_t *out, int max)
{
int n = 0;
if (!txt || max <= 0) return 0;
if (max > S_NET_MAX) max = S_NET_MAX;
if (text[0]) {
char *line = text;
while (line && *line && n < max) {
char *eol = strchr(line, '\n');
if (eol) *eol = 0;
char *p = strstr(line, "Device ");
if (p) {
p += 7;
char *mac = p; while (*mac && *mac != ' ' && *mac != '\t') mac++;
char *nm2 = mac; while (*nm2 == ' ' || *nm2 == '\t') nm2++;
int ml = (int)(mac - p); if (ml > 47) ml = 47;
if (ml > 0 && ml < (int)sizeof out[n].mac) {
memcpy(out[n].mac, p, (size_t)ml); out[n].mac[ml] = 0;
}
snprintf(out[n].name, sizeof out[n].name, "%s", *nm2 ? nm2 : p);
out[n].id = -1; out[n].sec[0] = 0;
n++;
char *line = (char *)txt;
while (line && *line && n < max) {
char *eol = strchr(line, '\n'); if (eol) *eol = 0;
char *p = strstr(line, "Device ");
if (p) {
p += 7;
char *mac = p; while (*mac && *mac != ' ' && *mac != '\t') mac++;
char *nm2 = mac; while (*nm2 == ' ' || *nm2 == '\t') nm2++;
int ml = (int)(mac - p); if (ml > 47) ml = 47;
if (ml > 0 && ml < (int)sizeof out[n].mac) {
memcpy(out[n].mac, p, (size_t)ml); out[n].mac[ml] = 0;
}
line = eol ? eol + 1 : NULL;
snprintf(out[n].name, sizeof out[n].name, "%s", *nm2 ? nm2 : p);
out[n].id = -1; out[n].sec[0] = 0;
n++;
}
line = eol ? eol + 1 : (char *)0;
}
return n;
}
int nm_bt_connected(meow_net_t *out, int max)
{
char text[MEOW_IPC_MAX - 32];
int rc = netmgr_bt("connected", NULL, text, sizeof text);
if (rc == NETMGR_UNREACH) return net_bt_connected(out, max); /* 直连回退 */
if (rc != MEOW_OK) return 0;
return nm_parse_devices(text, out, max);
}
int nm_bt_devices(meow_net_t *out, int max)
{
char text[MEOW_IPC_MAX - 32];
int rc = netmgr_bt("devices", NULL, text, sizeof text);
if (rc == NETMGR_UNREACH) return net_bt_scan(out, max); /* 直连回退(读扫描缓存) */
if (rc != MEOW_OK) return 0;
return nm_parse_devices(text, out, max);
}
/* A2DP 端点解析:"A2DP <mac> codec=.. rate=.. channels=.. bits=.. acquired=.. role=.." */
int nm_bt_a2dp(bt_ep_t *eps, int max)
{
if (max <= 0) return 0;
char text[MEOW_IPC_MAX - 32];
int rc = netmgr_bt("a2dp", NULL, text, sizeof text);
if (rc == NETMGR_UNREACH || rc != MEOW_OK) return 0; /* A2DP 详情无直连回退(P2 bluealsa 才接) */
int n = 0;
char *line = text;
while (line && *line && n < max) {
char *eol = strchr(line, '\n'); if (eol) *eol = 0;
if (!strncmp(line, "A2DP ", 5)) {
char *p = line + 5;
bt_ep_t *e = &eps[n];
memset(e, 0, sizeof *e);
snprintf(e->role, sizeof e->role, "%s", "-");
e->acquired = 0;
char *mac = p; while (*mac && *mac != ' ' && *mac != '\t' && *mac != '\n') mac++;
int ml = (int)(mac - p); if (ml > 23) ml = 23;
if (ml > 0) { memcpy(e->mac, p, (size_t)ml); e->mac[ml] = 0; }
char *t = mac;
while (*t) {
while (*t == ' ' || *t == '\t') t++;
if (!strncmp(t, "codec=", 6)) { t += 6; nm_val_field(&t, e->codec, sizeof e->codec); }
else if (!strncmp(t, "rate=", 5)) { t += 5; nm_val_field(&t, e->rate, sizeof e->rate); }
else if (!strncmp(t, "channels=", 9)) { t += 9; nm_val_field(&t, e->chn, sizeof e->chn); }
else if (!strncmp(t, "bits=", 5)) { t += 5; nm_val_field(&t, e->bits, sizeof e->bits); }
else if (!strncmp(t, "acquired=", 9)) { t += 9; e->acquired = atoi(t); }
else if (!strncmp(t, "role=", 5)) { t += 5; nm_val_field(&t, e->role, sizeof e->role); }
while (*t && *t != ' ' && *t != '\t' && *t != '\n') t++;
}
n++;
}
line = eol ? eol + 1 : NULL;
}
return n;
}
/* 直连回退:bluetoothctl <sub> <mac>(带超时)。返回 MEOW_OK/MEOW_ERR_*。 */
static int bt_direct_cmd(const char *sub, const char *mac)
{
if (!mac || !mac[0]) return MEOW_ERR_INVAL;
char cmd[192];
snprintf(cmd, sizeof cmd, "PATH=/bin:/sbin:/usr/bin:/usr/sbin bluetoothctl %s %s 2>/dev/null", sub, mac);
char *o = popen_read_tmo(8000, cmd);
int ok = o && (strstr(o, "successful") || strstr(o, "Connected") ||
strstr(o, "already") || strstr(o, "Paired"));
if (o) free(o);
return ok ? MEOW_OK : MEOW_ERR_NODEV;
}
int nm_bt_connect(const char *mac)
{
char text[128];
int rc = netmgr_bt("connect", mac, text, sizeof text);
if (rc == NETMGR_UNREACH) return bt_direct_cmd("connect", mac);
return rc;
}
int nm_bt_disconnect(const char *mac)
{
char text[128];
int rc = netmgr_bt("disconnect", mac, text, sizeof text);
if (rc == NETMGR_UNREACH) return bt_direct_cmd("disconnect", mac);
return rc;
}
int nm_bt_pair(const char *mac)
{
char text[128];
int rc = netmgr_bt("pair", mac, text, sizeof text);
if (rc == NETMGR_UNREACH) return bt_direct_cmd("pair", mac);
return rc;
}
int nm_bt_unpair(const char *mac)
{
char text[128];
int rc = netmgr_bt("unpair", mac, text, sizeof text);
if (rc == NETMGR_UNREACH) return bt_direct_cmd("remove", mac);
return rc;
}
void nm_bt_scan_on(void)
{
if (!netmgr_ok()) { net_bt_scan_trigger(); return; } /* 直连回退 */
netmgr_bt("scan", "on", NULL, 0);
}
void nm_bt_scan_off(void)
{
if (!netmgr_ok()) return;
netmgr_bt("scan", "off", NULL, 0);
}