BK7258 原生 Windows 开发(五)创建开放热点并查询 SoftAP 状态

创建开放热点并查询 SoftAP 状态,附完整工程源码、构建记录和实板串口日志。

本文目录

上电后自动打开一个 Wi-Fi 热点,是检验 BK7258 双镜像工程的一个小例子。这里给热点取名 BK7258-DEMO-1202,方便在手机上与旧固件的 test 区分。本文从官方 SDK 的 projects/app 起步,完成 Windows 本机编译与整包校验。文末附有同一镜像的实板串口记录和本次验证范围。

从官方 app 工程开始

本文使用博通 Armino SMP release/v3.1.1.10,对应提交 398cab28。复制模板时保留 ap/config/bk7258_ap、cp/config/bk7258、partitions/bk7258 和 pj_config.mk,这样可以直接复用这个版本的 GPIO、网络、CLI 和分区配置。外部工程目录名取 wifi_test_hotspot,根目录的 CMakeLists.txt 把项目名改为 project(wifi_test_hotspot),保留模板原有的 project.cmake 引用。博通 SDK 标签

工程目录里的 AP 表示应用处理器(Application Processor)。Wi-Fi 术语里的 AP 表示接入点(Access Point),下文用 SoftAP 指代热点。BK7258 上,CPU0 运行 CP 镜像,CPU1 和 CPU2 运行同一份 AP 镜像,各自的镜像入口放在 cp_main.c 和 ap_main.c。调用 Wi-Fi API 的业务放在 AP 一侧。立创双镜像架构说明

在 CP 入口启动应用处理器

把下面的代码保存为 wifi_test_hotspot/cp/cp_main.c。CP 在 bk_init() 前注册用户入口。系统调用这个入口时,先打印 CP 启动日志,再通过电源管理接口请求启动 AP。SDK 使用 CP1 命名这个电源域。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include "bk_private/bk_init.h"
#include <components/log.h>
#include <driver/pwr_clk.h>
#include <os/os.h>

extern void rtos_set_user_app_entry(beken_thread_function_t entry);

static void start_application_processor(beken_thread_arg_t arg)
{
    (void)arg;
    BK_LOGI("hotspot", "READY: BK7258 CP startup\r\n");
    BK_LOG_ON_ERR(bk_pm_module_vote_boot_cp1_ctrl(PM_BOOT_CP1_MODULE_NAME_APP,
                                                  PM_POWER_MODULE_STATE_ON));
}

int main(void)
{
    rtos_set_user_app_entry(start_application_processor);
    return bk_init();
}

用一行 cp/CMakeLists.txt 将这个入口注册为 CP 组件。

1
armino_component_register(SRCS cp_main.c INCLUDE_DIRS . PRIV_REQUIRES bk_init)

在 AP 入口配置 SoftAP

把下面的代码保存为 wifi_test_hotspot/ap/ap_main.c。AP 的 bk_init() 会初始化网络接口和 Wi-Fi 子系统。接下来给热点接口设置 IPv4 参数、设置无线参数,最后调用 bk_wifi_ap_start()。前两步任何一步返回错误,程序都会把错误码交给 main() 打印。只有启动 API 成功返回,程序才打印 READY。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "bk_private/bk_init.h"
#include <components/log.h>
#include <components/netif.h>
#include <modules/wifi.h>

#define HOTSPOT_SSID "BK7258-DEMO-1202"
#define HOTSPOT_IP   "192.168.188.1"

static char tag[] = "hotspot";

static bk_err_t start_hotspot(void)
{
    const netif_ip4_config_t address = {
        .ip = HOTSPOT_IP,
        .mask = "255.255.255.0",
        .gateway = HOTSPOT_IP,
        .dns = HOTSPOT_IP,
    };
    const wifi_ap_config_t hotspot = {
        .ssid = HOTSPOT_SSID,
        .channel = 11,
        .security = WIFI_SECURITY_NONE,
    };

    BK_RETURN_ON_ERR(bk_netif_set_ip4_config(NETIF_IF_AP, &address));
    BK_RETURN_ON_ERR(bk_wifi_ap_set_config(&hotspot));
    return bk_wifi_ap_start();
}

int main(void)
{
    int ret = bk_init();
    if (ret != BK_OK) {
        BK_LOGE(tag, "AP initialization failed (%d)\r\n", ret);
        return ret;
    }

    ret = start_hotspot();
    if (ret != BK_OK) {
        BK_LOGE(tag, "SoftAP startup failed (%d)\r\n", ret);
        return ret;
    }

    BK_LOGI(tag, "READY: SoftAP ssid=%s ip=%s\r\n", HOTSPOT_SSID, HOTSPOT_IP);
    return BK_OK;
}

在 ap/CMakeLists.txt 注册入口,并列出代码用到的三个组件。

1
armino_component_register(SRCS ap_main.c INCLUDE_DIRS . PRIV_REQUIRES bk_init bk_netif bk_wifi)
参数 本例设置 修改位置
热点名 BK7258-DEMO-1202 HOTSPOT_SSID。配置和 READY 日志共用这个宏。
热点地址、网关和 DNS 192.168.188.1 HOTSPOT_IP
子网掩码 255.255.255.0 address.mask
信道 11 hotspot.channel
认证 开放,无密码 hotspot.security = WIFI_SECURITY_NONE

首次编译遇到的 const 错误

第一版把日志标签定义成 static const char *const tag = "hotspot";。编译停在 ap_main.c 的日志调用处,GCC 提示传参会丢弃 const 限定。沿着 ap/include/components/log.h 里的 BK_LOGI 和 BK_LOGE 展开宏,最终调用的是 ap/include/components/system.h 中的 bk_printf_ext()。

1
void bk_printf_ext(int level, char *tag, const char *fmt, ...);

第二个参数要求可写的字符指针,而 const char *const 指向只读字符。工程使用 -Werror,这条类型诊断足以终止构建。把标签改成前面完整代码中的 static char tag[] = "hotspot"; 后,传参类型匹配,AP 重新编译通过。

为保留可核对的诊断,本次又从这个 AP 的 compile_commands.json 提取真实编译命令,仅在临时副本中还原原先的声明。GCC 在源文件第 34、40、44 行的三个日志调用上再次报错。下面是独立复现的完整输出,首次失败发生在更早的完整工程构建中。

BK7258 Wi-Fi hotspot | independent fault reproduction
Captured 2026-12-02T01:39:28+08:00
Compiler: arm-none-eabi-gcc.exe (GNU Arm Embedded Toolchain 10.3-2021.10) 10.3.1 20210824 (release)
Compilation database: E:\Code\BK7258_demo\wifi_test_hotspot\build\bk7258\wifi_test_hotspot\bk7258_ap\compile_commands.json
Matched translation unit: E:\Code\BK7258_demo\wifi_test_hotspot\ap\ap_main.c
Temporary translation unit: D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c
Reintroduced declaration: static const char *const tag = "hotspot";
Preserved target, preprocessor, include and warning options from the matched entry.
Invoked -fsyntax-only in the original compilation directory, without object or dependency output.
Compiler exit code: 1

In file included from D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:2:
D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c: In function 'main':
D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:34:17: error: passing argument 2 of 'bk_printf_ext' discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
   34 |         BK_LOGE(tag, "AP initialization failed (%d)\r\n", ret);
      |                 ^~~
D:/BK/bk_avdk_smp_sdk/ap/include/components/log.h:58:74: note: in definition of macro 'BK_LOGE'
   58 | #define BK_LOGE( tag, format, ... )         _BK_LOG_PRINTF(BK_LOG_ERROR, tag, format,  ##__VA_ARGS__)
      |                                                                          ^~~
In file included from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_assert.h:18,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_generic.h:23,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_include.h:23,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/os/os.h:17,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/components/log.h:21,
                 from D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:2:
D:/BK/bk_avdk_smp_sdk/ap/include/components/system.h:155:38: note: expected 'char *' but argument is of type 'const char * const'
  155 | void bk_printf_ext(int level, char * tag, const char *fmt, ...);
      |                               ~~~~~~~^~~
In file included from D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:2:
D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:40:17: error: passing argument 2 of 'bk_printf_ext' discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
   40 |         BK_LOGE(tag, "SoftAP startup failed (%d)\r\n", ret);
      |                 ^~~
D:/BK/bk_avdk_smp_sdk/ap/include/components/log.h:58:74: note: in definition of macro 'BK_LOGE'
   58 | #define BK_LOGE( tag, format, ... )         _BK_LOG_PRINTF(BK_LOG_ERROR, tag, format,  ##__VA_ARGS__)
      |                                                                          ^~~
In file included from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_assert.h:18,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_generic.h:23,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_include.h:23,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/os/os.h:17,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/components/log.h:21,
                 from D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:2:
D:/BK/bk_avdk_smp_sdk/ap/include/components/system.h:155:38: note: expected 'char *' but argument is of type 'const char * const'
  155 | void bk_printf_ext(int level, char * tag, const char *fmt, ...);
      |                               ~~~~~~~^~~
In file included from D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:2:
D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:44:13: error: passing argument 2 of 'bk_printf_ext' discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
   44 |     BK_LOGI(tag, "READY: SoftAP ssid=%s ip=%s\r\n", HOTSPOT_SSID, HOTSPOT_IP);
      |             ^~~
D:/BK/bk_avdk_smp_sdk/ap/include/components/log.h:74:73: note: in definition of macro 'BK_LOGI'
   74 | #define BK_LOGI( tag, format, ... )         _BK_LOG_PRINTF(BK_LOG_INFO, tag, format,  ##__VA_ARGS__)
      |                                                                         ^~~
In file included from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_assert.h:18,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_generic.h:23,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/common/bk_include.h:23,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/os/os.h:17,
                 from D:/BK/bk_avdk_smp_sdk/ap/include/components/log.h:21,
                 from D:\BK\article-drafts\hotspot-const-repro-ftgqptl_\ap_main.c:2:
D:/BK/bk_avdk_smp_sdk/ap/include/components/system.h:155:38: note: expected 'char *' but argument is of type 'const char * const'
  155 | void bk_printf_ext(int level, char * tag, const char *fmt, ...);
      |                               ~~~~~~~^~~
cc1.exe: all warnings being treated as errors

编译并找到整包

本例的 Windows 编译入口位于工程上一级目录。环境搭好后,可以在 PowerShell 运行入口脚本并查看日志末行。

1
2
3
cd E:\Code\BK7258_demo
.\build.bat
Get-Content .\wifi_test_hotspot\build\build.log -Tail 1

这次日志末行为 EXIT=0。用于整包下载的文件位于 wifi_test_hotspot\build\bk7258\wifi_test_hotspot\package\all-app.bin。本次实测镜像的大小和 SHA-256 见文末记录。两次构建的工具版本、配置和构建时间都可能影响结果,这个摘要用于核对本文这一次产物。

我另外按照本次生成的分区表,用原始 bootloader、CP 和 AP 二进制重建了一份完整镜像。bootloader 连同 OTA 分区表从 0x00000000 开始,CP 从 0x00011000 开始,AP 从 0x00165000 开始。每 32 字节数据附加两字节 CRC16,并按 SDK 规则填充分区空隙和文件末尾。重建结果与 all-app.bin 逐字节一致,本次 AP 二进制也含有新 SSID。

上板后怎样认出这份固件

使用设备管理器中开发板对应的 CH340 端口,烧录整包 all-app.bin,起始地址为 0x0。烧录程序释放端口后,以 115200、8N1、无流控 打开同一端口,DTR、RTS 保持未勾选,再复位开发板。立创固件烧录说明

启动日志中的 READY 表示热点启动 API 已成功返回。若要在已经运行的板上查询当前状态,可以发送 ap_cmd state 并附加换行(CRLF)。ap_cmd 将命令转发给 AP,回复会列出 SSID、信道和加密方式。手机连接后还可检查分配到的 IP 和默认网关。文末的实测结论只列出本次实际完成的检查。

源码与相关篇目

下载五个工程的完整源码 ZIP。源码包 SHA-256 为 2bbcb691ee9a178bfa7da3c6ff0721109dbbf990378f2cc62d2b5a473e399086。

系列其他篇目:在原生 Windows 上搭建环境并编译 AP、CP 双镜像固件 · 用串口心跳确认 CP 和 AP 均已启动 · 用 RTOS 队列在两个线程之间传递消息 · 扫描周围热点,读取信道与 RSSI · 接入小智服务,验证应用层握手。

本次实板验证

开发板报告 SoftAP 已启动,串口命令 ap_cmd state 返回热点名 BK7258-DEMO-1202、信道 11、cipher_type=NONE 和 CMDRSP:OK。Windows 独立扫描也在 2462 MHz 发现同名开放热点。

验证时间为 2026-12-02T01:37:08.992+08:00,板卡为 立创·实战派 BK7258,串口为 COM10。本次所用 all-app.bin 共 2,095,168 字节,SHA-256 为 e05aa3d3d5a11a45498d6df0246565eac5a3301ae53bc7c7590afb172b8f3ff0。

下载完整实板串口日志,已脱敏 · 下载完整编译日志,已脱敏 · 下载完整烧录日志 · 下载主机操作时间记录 · 下载Windows 无线扫描记录 · 下载GCC 类型错误的独立复现记录

下面逐行摘自主记录。

hotspot:I(114):READY: BK7258 CP startup
wifid:I(491):[KW:]mm_set_vif_state,vif=1,vif_type=2,is_active=1, aid=0x0,rssi=0
ap0:hotspot:I(382):READY: SoftAP ssid=BK7258-DEMO-1202 ip=192.168.188.1
ap0:wifi_cli:I(7699):[KW:]softap: ssid=BK7258-DEMO-1202, channel=11, cipher_type=NONE
CMDRSP:OK
展开本次完整串口日志(已脱敏)
reboot

se ef:I(5):ENV start address is 0x007FA000, size is 8192 bytes.
ef:I(7):EasyFlash V4.1.0 is initialize success.
I(10):driver_init: psram init ok
sensor:W(10):uncali sdmadc value:[个体校准值]
rwnx_cal_set_rfconfig(0x102) phy on; rf off
cal:W(18):temp in otp is:[个体校准值]
cal:E(110):load polar tab magic code error 0xffffffff
xtal_cali:[个体校准值]
cal:I(110):idx:[个体校准值],r:[个体校准值],xtal:[个体校准值],pwr_gain:[个体校准值]
wifi:I(111):wifi inited(1) ret(0)
hotspot:I(114):READY: BK7258 CP startup
ap1:ef:I(0):ENV start address is 0x007FC000, size is 8192 bytes.
ap0:ef:I(171):EasyFlash V4.1.0 is initialize success.
ap1:WDRV:W(260):wdrv_tx_cmd_buffer_init, addr[0]=0x28011d30, pattern_addr=0x28011d2c
ap1:WDRV:W(260):wdrv_tx_cmd_buffer_init, addr[1]=0x28012050, pattern_addr=0x2801204c
ap1:WDRV:W(260):wdrv_tx_cmd_buffer_init, addr[2]=0x28012370, pattern_addr=0x2801236c

$wakeup
bluetoot:W(386):bk_bluetooth_init bluetooth already initialised
wakeup
ap0:WDRV:W(354):the max conn num is zero, set it is default
wifi:W(475):the max conn num is zero, set it is default
wifid:I(486):mm_add_if_req: vif_idx=1, type=2, p2p=0, status=0
wifid:I(487):chan_ctxt_add: CTXT0,freq2462MHz,bw20MHz,pwr0dBm
wifid:I(487):chan_reg_fix:VIF1,CTXT0,type2,ctxt_s0,nb_vif0
wifid:I(491):[KW:]mm_set_vif_state,vif=1,vif_type=2,is_active=1, aid=0x0,rssi=0
hitf:I(492):start apm success, vif1, channel0, bcmc3
ap0:hotspot:I(382):READY: SoftAP ssid=BK7258-DEMO-1202 ip=192.168.188.1
ap_cmd state


$ap0:wifi_cli:I(7698):[KW:]sta: 0, ap: 1, b/g/n
ap0:wifi_cli:I(7699):[KW:]softap: ssid=BK7258-DEMO-1202, channel=11, cipher_type=NONE
CMDRSP:OK

$
$
世界是你们
使用 Hugo 构建
主题 Stack 由 Jimmy 设计