(目录)
1.前言
项目开发需要联网传输数据,每次开机都要事先运行WiFi程序。于是想办法能不能板子开机的时候就能自动启动运行WiFi程序,不需要每次都命令行输入。
2.开发例程
2.1准备工作
找到启动WiFi的程序,文件目录:/home/nebula/bearpi/bearpi-hm_micro_small/applications/BearPi/BearPi-HM_Micro/samples/communication/wpa_supplicant/src/wpa_sample.c ::: hljs-center 以下代码为主函数代码???? :::
int main(int argc, char *argv[])
{
printf("------------------------\n");
printf("------------------------\n");
printf("------------------------\n");
printf("--------test------------\n");
printf("------------------------\n");
printf("------------------------\n");
printf("------------------------\n");
g_wpaArgc = argc;
for (int i = 0; i < g_wpaArgc; i++)
{
g_wpaArg[i] = argv[i];
}
sleep(30);
g_wpaArgc = 5;
g_wpaArg[0] = "-i";
g_wpaArg[1] = "wlan0";
g_wpaArg[2] = "-d";
g_wpaArg[3] = "-c";
g_wpaArg[4] = "/etc/wpa_supplicant.conf";
int ret = pthread_create(&g_wpaThread, NULL, ThreadMain, NULL);
if (ret != 0)
{
printf("[WpaSample]create thread failed error:%s.\n", strerror(ret));
return 1;
}
pthread_join(g_wpaThread, NULL);
return 0;
}
依照官方执行WiFi程序的命令./bin/wpa_supplicant -i wlan0 -d -c /etc/wpa_supplicant.conf,把程序写死 特别注意:经过后期的教训终结,==一定要 sleep(30),等待开发板初始化网卡才能连接==。
2.2查找资料&确定方向
干货!openharmony L1级开机启动项服务设置 虽然没有找到完全适合Micro的,但都是OpenHarmony的架构,大同小异。 可以发现配置开机启动项是在代码根目录下的==vender==(内核)文件夹里配置的。找到含有类似init_config.cfg.的文件名。 具体在Bearpi-Micro的详细地址如下:vendor/bearpi/bearpi_hm_micro/init_configs/ 好了,问题来了,这两个除了名字不一样,内容一模一样的文件到底该改哪一个? 经过大量的试错(修改了八九次)发现只能修改init_bearpi_hm_micro.cfg这个文件 ::: hljs-center
详细的配置如下????
:::
{
"jobs" : [{
"name" : "pre-init",
"cmds" : [
"mkdir /storage/data/log",
"chmod 0755 /storage/data/log",
"chmod 0777 /dev/hdf",
"chown 4 4 /storage/data/log",
"mkdir /storage/data/softbus",
"mkdir /sdcard",
"chmod 0777 /sdcard",
"start foundation",
"start bundle_daemon",
"start appspawn",
"start wms_server",
"start shell"
]
}, {
"name" : "init",
"cmds" : [
"start apphilogcat",
"start wpa_supplicant"
]
}, {
"name" : "post-init",
"cmds" : [
"chown 0 99 /dev/hdf",
"chown 0 99 /dev/hdf/dev_mgr",
"chown 0 99 /dev/hdf/hdfwifi",
"chown 0 99 /dev/gpio",
"chown 0 99 /dev/i2c-0",
"chown 0 99 /dev/i2c-1",
"chown 0 99 /dev/i2c-2",
"chown 0 99 /dev/i2c-3",
"chown 0 99 /dev/i2c-4",
"chown 0 99 /dev/i2c-5",
"chown 0 99 /dev/i2c-6",
"chown 0 99 /dev/i2c-7",
"chown 0 99 /dev/uartdev-0",
"chown 0 99 /dev/uartdev-1",
"chown 0 99 /dev/uartdev-2",
"chown 0 99 /dev/uartdev-3",
"chown 0 99 /dev/spidev0.0",
"chown 0 99 /dev/spidev1.0",
"chown 0 99 /dev/spidev2.0",
"chown 0 99 /dev/spidev2.1"
]
}
],
"services" : [{
"name" : "foundation",
"path" : ["/bin/foundation"],
"uid" : 7,
"gid" : 7,
"once" : 0,
"importance" : 1,
"caps" : [10, 11, 12, 13]
}, {
"name" : "shell",
"path" : ["/bin/shell"],
"uid" : 2,
"gid" : 2,
"once" : 0,
"importance" : 0,
"caps" : [4294967295]
}, {
"name" : "appspawn",
"path" : ["/bin/appspawn"],
"uid" : 1,
"gid" : 1,
"once" : 0,
"importance" : 0,
"caps" : [2, 6, 7, 8, 11, 23]
}, {
"name" : "apphilogcat",
"path" : ["/bin/apphilogcat", "-L", "auto"],
"uid" : 4,
"gid" : 4,
"once" : 1,
"importance" : 0,
"caps" : []
}, {
"name" : "wpa_supplicant",
"path" : ["/bin/wpa_supplicant","-i","wlan0","-d","-c","/etc/wpa_supplicant.conf"],
"uid" : 8,
"gid" : 8,
"once" : 0,
"importance" : 0,
"caps" : [0,1]
},{
"name" : "wms_server",
"path" : ["/bin/wms_server"],
"uid" : 0,
"gid" : 0,
"once" : 1,
"importance" : 0,
"caps" : []
}, {
"name" : "bundle_daemon",
"path" : ["/bin/bundle_daemon"],
"uid" : 8,
"gid" : 8,
"once" : 0,
"importance" : 0,
"caps" : [0, 1]
}
]
}
在OpenHarmony 中init进程通过读取/etc/init.cfg,根据配置执行指定命令,或启动指定进程添加的代码有: Init配置文件基于JSON格式,用来配置系统启动时必要的命令和服务。Init在系统启动时解析配置文件,并根据配置文件执行对应的命令,启动相应的服务。
3.效果
4.总结
看似简单,实则配置文件的格式要求==十分十分严谨==,多一个逗号不行,少一个逗号也不行!!! 都是在不断的试错、尝试中总结的经验。 其实在OpenHarmony设备开发的官方文档引导启动配置文件中也有说到: ==cfg文件是严格按照JSON格式编写的,当添加服务或命令未生效时,可以优先排查添加内容的格式是否正确/(ㄒoㄒ)/~~。==
本文作者:Z·y