本文以HelloWorld为例,讲述给Openharmony添加一个开机服务的过程
在third_party目录创建一个helloworl目录,并在目录下实现helloworld.c
#include
int main() {
printf("Hello, World!\n");
return 0;
}
然后再在当前目录创建一个BUILD.gn文件, 来定义如何编译构建helloworld模块
import("//build/ohos.gni")
ohos_executable("helloworld") {
sources = [
"helloworld.c",
]
# 如果需要定义编译标志
cflags = [ "-Wall" ]
# 安装到系统bin目录
install_enable = true
install_images = [ "system" ]
#output_dir = "bin"
part_name = "helloworld"
subsystem_name = "thirdparty"
}
再在当前目录创建一个元数据文件bound.json. 用来注册和描述helloworld组件,以便helloworld组件可以被集成进整个系统
{
"name": "helloworld",
"version": "1.0.0",
"publishAs": "code-segment",
"component": {
"name": "helloworld",
"subsystem": "thirdparty",
"syscap": [],
"features": [],
"adapted_system_type": ["standard"],
"rom": "100KB",
"ram": "100KB",
"deps": {
"components": [],
"third_party": []
},
"build": {
"sub_component": [
"//third_party/helloworld:helloworld"
],
"inner_kits": [],
"test": []
}
}
}
至此helloworld服务源码就实现好了,其在third_party目录的结构如下
third_party/helloworld/
├── BUILD.gn
├── bundle.json
└── helloworld.c
0 directories, 3 files
这里以DAYU200为例,其平台代码的位置为:vendor/hihope/rk3568/config.json
我们需要修改该文件添加以下内容:
@@ -264,6 +264,16 @@
]
}
]
+ },
+ {
+ "subsystem": "thirdparty",
+ "components": [
+ {
+ "component": "helloworld",
+ "features": []
+ }
+ ]
+
}
]
}
我们需要修改base/startup/init/services/etc/init.cfg文件,注册和声明该服务.最后在late-fs中启动它
@@ -151,7 +151,8 @@
}, {
"name" : "late-fs",
"cmds" : [
- "chmod 0755 /sys/kernel/debug/tracing"
+ "chmod 0755 /sys/kernel/debug/tracing",
+ "start helloworld"
]
}, {
"name" : "post-fs-data",
@@ -215,5 +216,13 @@
"init_trace start"
]
}
- ]
+ ],
+ "services" : [{
+ "name" : "helloworld",
+ "path" : ["/system/bin/helloworld"],
+ "uid" : "root",
+ "gid" : "root",
+ "once" : 1,
+ "importance" : 0
+ }]
}