openharmony hi3518开发板实现自启动程序 原创 精华
目标:实现开机自动连接wifi
前期准备
修改源码
1、修改/applications/sample/camera/communication/wpa_supplicant/src/wpa_sample.c的main函数
int main(int argc, char *argv[])
{
g_wpaArgc = argc;
for (int i = 0; i < g_wpaArgc; i++) {
g_wpaArg[i] = argv[i];
}
g_wpaArgc=5;
g_wpaArg[1] = "-i";
g_wpaArg[2] = "wlan0";
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;
}
2、修改/applications/sample/camera/communication/wpa_supplicant/config/wpa_supplicant.conf文件
country=GB
ctrl_interface=udp
network={
ssid="你的wifi名"
psk="你的wifi密码"
}
修改配置
1、在/build/lite/components/applications.json里添加一个component
{
"component": "wpa_supplicant",
"description": "Communication related samples.",
"optional": "true",
"dirs": [
"applications/sample/camera/communication/wpa_supplicant"
],
"targets": [
"//applications/sample/camera/communication/wpa_supplicant:wpa_sample"
],
"rom": "",
"ram": "",
"output": [],
"adapted_kernel": [ "liteos_a" ],
"features": [],
"deps": {
"components": [],
"third_party": []
}
}
2、在/vendor/hisilicon/hispark_aries/config.json里添加一个component
{
"subsystem": "applications",
"components": [
{ "component": "camera_sample_app", "features":[] },
{ "component": "wpa_supplicant", "features":[] }
]
}
3、在/vendor/hisilicon/hispark_aries/init_configs/init_liteos_a_3518ev300.cfg里添加自启动项
{
"jobs" : [{
"name" : "pre-init",
"cmds" : [
"mkdir /storage/data/log",
"chmod 0755 /storage/data/log",
"chown 4 4 /storage/data/log",
"loadcfg /patch/fstab.cfg",
"mkdir /storage/data/softbus",
"chmod 0700 /storage/data/softbus",
"chown 7 7 /storage/data/softbus",
"mkdir /sdcard",
"chmod 0777 /sdcard",
"mount vfat /dev/mmcblk0 /sdcard rw,umask=000",
"mount vfat /dev/mmcblk1 /sdcard rw,umask=000"
]
}, {
"name" : "init",
"cmds" : [
"start shell",
"start apphilogcat",
"start foundation",
"start bundle_daemon",
"start appspawn",
"start media_server",
"start ai_server",
"start softbus_server",
"start wpa_supplicant"
]
}, {
"name" : "post-init",
"cmds" : [
"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/uartdev-0",
"chown 0 99 /dev/uartdev-1",
"chown 0 99 /dev/uartdev-2",
"chown 0 99 /dev/spidev0.0",
"chown 0 99 /dev/spidev1.0",
"chown 0 99 /dev/spidev1.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" : "media_server",
"path" : ["/bin/media_server"],
"uid" : 0,
"gid" : 0,
"once" : 1,
"importance" : 0,
"caps" : []
}, {
"name" : "wms_server",
"path" : ["/bin/wms_server"],
"uid" : 6,
"gid" : 6,
"once" : 1,
"importance" : 0,
"caps" : []
}, {
"name" : "bundle_daemon",
"path" : ["/bin/bundle_daemon"],
"uid" : 8,
"gid" : 8,
"once" : 0,
"importance" : 0,
"caps" : [0, 1]
}, {
"name" : "ai_server",
"path" : ["/bin/ai_server"],
"uid" : 2,
"gid" : 2,
"once" : 0,
"importance" : 0,
"caps" : []
}, {
"name" : "softbus_server",
"path" : ["/bin/softbus_server"],
"uid" : 0,
"gid" : 0,
"once" : 1,
"importance" : 0,
"caps" : []
}, {
"name": "wpa_supplicant",
"path": ["/bin/wpa_supplicant"],
"uid" : 0,
"gid" : 0,
"once" : 1,
"importance" : 0,
"caps": []
}
]
}
编译并烧录
1、在项目根目录下运行命令hb build -f进行编译
2、编译完成后,在/out/hispark_aries/ipcamera_hispark_aries下找到OHOS_Image.bin,rootfs_jffs2.img,userfs_jffs2.img这3个文件,和/device/hisilicon/hispark_aries/sdk_liteos/uboot/out/boot/u-boot-hi3518ev300.bin文件,使用烧录工具进行烧录。
检测结果
系统运行后执行命令ifconfig查看ip
1、如果连接失败
2、如果连接成功
遇到的问题
1、wpa_supplicant程序可能会运行太早,从而导致无法连接wifi,解决办法就是尽可能让这个程序晚点运行或者运行后等待一段时间再连接