diff --git a/src/cli/h2dts/h2dts_README_ZH.md b/src/cli/h2dts/h2dts_README_ZH.md index 6ca97837c63b59ae3cee9343f1e7a40fcfb6323d..689e6c9f7d2dec4010c3c611c6009a7c10037ec3 100644 --- a/src/cli/h2dts/h2dts_README_ZH.md +++ b/src/cli/h2dts/h2dts_README_ZH.md @@ -24,7 +24,7 @@ pip install CppHeaderParser npm i stdio -4.将待转换的文件tsTest.h拷贝到napi_generator/src/cli/h2dts/src/tsGen下;TsGenTest.h文件如下所示: +4.将待转换的文件TsGenTest.h拷贝到napi_generator/src/cli/h2dts/src/tsGen下;TsGenTest.h文件如下所示: ``` #ifndef TSGENTEST_H diff --git a/src/cli/h2hdf/docs/DEVELOP.md b/src/cli/h2hdf/docs/DEVELOP.md new file mode 100644 index 0000000000000000000000000000000000000000..c9ef14562a38b2cc5cf82906ca1093a9dcbc9a16 --- /dev/null +++ b/src/cli/h2hdf/docs/DEVELOP.md @@ -0,0 +1,283 @@ +# Develop Guide + +## h2hdf工具使用场景 + +在OpenHarmony系统中,上层应用或服务层通过调用HDF框架提供的HDI接口,能够以一种标准化和抽象化的方式与底层硬件设备进行交互。使用h2hdf工具,用户只需提供一个drivername,工具会自动生成整个框架的代码,包含驱动配置文件、idl接口、驱动程序driver和驱动服务框架。 + +![image-20240724093743837](./figures/pic_code_frame.png) + +## h2hdf工具代码框架说明 + +``` +h2hdf +├── docs # 文档 +│ ├── figures # 图片资源 +│ ├── usage.md # 使用文档 +│ ├── develop.md # 设计文档 +├── src +│ ├── templete # 模板文件 +│ │ ├── HcsconfigTemplete +│ │ │ ├── hcsconfigTemplete.gen # hcs配置模板 +│ │ ├── IdlInterfaceTemplete +│ │ │ ├── buildgnTemplete.gen # idl接口BUILD.gn模板 +│ │ │ ├── v4_1 +│ │ │ │ ├── bundlejsonTemplete.gen # idl接口bundle.json模板 +│ │ │ ├── idlInterfaceTemplete.gen # idl接口定义文件模板 +│ │ ├── PeripheralTemplete +│ │ │ ├── DumpExampleTemplete # dump示例 +│ │ │ │ ├── v4_1 +│ │ │ │ │ ├── buildgnTemplete.gen # BUILD.gn模板 +│ │ │ │ ├── dumpCTemplete.gen # dump实现示例模板 +│ │ │ │ ├── dumpHTemplete.gen # dump h文件模板 +│ │ │ ├── HdiServiceTemplete # hdi_service 模板 +│ │ │ │ ├── v4_1 +│ │ │ │ │ ├── buildgnTemplete.gen # BUILD.gn模板 +│ │ │ │ ├── driverTemplete.gen # driver模板 +│ │ │ │ ├── logHTemplte.gen # 日志文件模板 +│ │ │ │ ├── serviceCppTemplete.gen # 驱动服务模板 +│ │ │ │ ├── serviceHTemplete.gen # 驱动服务 h 文件模板 +│ │ │ ├── buildgnTemplete.gen # hdi service BUILD.gn模板 +│ │ │ ├── v4_1 +│ │ │ │ ├── bundlejsonTemplete.gen # hdi service bundle.json模板 +│ │ ├── framework.json # 存储模板对应相对路径 +│ ├── generate.js # 使用templete中对应的模板生成代码。 +│ ├── main.js # 工具入口文件,定义输入参数,调用generate.js来启动代码生成过程。 +├── package.json # Node.js打包配置文件 +``` + +运行逻辑 + +![image-20240724093743837](./figures/pic_code_frame.png) + +main.js为脚本入口,其中使用stdio.getopt获取参数,参数分别为: -n, drivername,例如:hello;-v, 可选参数,版本,默认为4.1;-o, 可选参数,默认为当前目录,指定生成框架代码输出路径。 + +``` +let ops = stdio.getopt({ + // 输入driver name ,输入一个字符串,默认为hello + 'drivername': { key: 'n', args: 1, description: 'driver name', default: 'hello' }, + // 输入版本号 + 'version': { key: 'v', args: 1, description: 'source version', default: '4.1' }, + // 输出文件夹路径 + 'out': { key: 'o', args: 1, description: 'output directory', default: '' }, +}); +``` + +对输入的参数值进行校验:checkInput对输入的drivername进行校验,输入的drivername必须符合命名规范;isValidValue对输入的版本号进行校验,输入的版本号必须包含在版本号数组中,该数组后续可持续更新: + +``` +... +const allowedVersion = ['4.0', '4.1', '5.0', '5.1']; +function isValidValue(value, allowedVersion) { + return allowedVersion.includes(value); +} + +function checkInput(input) { + const regex = /\b[a-zA-Z_][a-zA-Z0-9_]*\b/; + return regex.test(input); +} +``` + +使用getJsonCfg读取json文件中hdf框架模板的路径: + +``` +function getJsonCfg(jsonFilePath) { + let jsonCfg = null; + let jsonFile = fs.readFileSync(jsonFilePath, { encoding: 'utf8' }); + jsonCfg = JSON.parse(jsonFile); + return jsonCfg; +} +``` + +获取到每个模板的路径后,根据路径读取模板文件,并替换模板文件中的drivername等: + +``` +/* 根据用户输入的driver名字生成framework框架 + * drivername:用户输入的驱动名,frameworkJson: 模板内容,out:生成框架路径 + * 替换模板中的名字并写文件输出 + */ +function genDriverFramework(driverName, frameworkJson, version, out = '') { + ... + // 生成Hcs配置文件 + genHcsconfigFile(frameworkJson, driverName, frameworkPath); + // 生成Idl接口 + genInterface(frameworkPath, frameworkJson, rootInfo); + // 生成hdi_service + genPeripheral(frameworkPath, frameworkJson, rootInfo); +} +``` + +## 工具使用方法说明 + +### 生成 + +1.安装typescript:在napi_generator/src/cli/h2hdf/src目录下执行命令: + + npm i typescript + +2.安装stdio:在napi_generator/src/cli/h2hdf目录下执行命令: + + npm i stdio + +3.在napi_generator/src/cli/h2hdf/src下执行以下命令生成ts声明文件: + +``` +node main.js -n hello +``` + +其中,参数详情如下: + + -n, drivername,例如:hello + + -v, 可选参数,版本,默认为4.1 + + -o, 可选参数,默认为当前目录,指定生成框架代码输出路径。 + +6.执行成功后在napi_generator/src/cli/h2hdf/src/下生成hellohdf文件夹,文件夹中目录结构如下所示: + +``` +hellohdf +├── HcsConfig # hcs配置文件 +│ ├── device_info.hcs # 内容配置到源码vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs文件中 +├── IdlInterface +│ ├── hello # 拷贝到源码drivers/interface +│ │ ├── v1_0 +│ │ │ ├── BUILD.gn +│ │ │ ├── IHelloInterface.idl # idl接口 +│ │ ├── bundle.json +├── Peripheral # 拷贝到源码drivers/peripheral +│ ├── hello +│ │ ├── hal +│ │ │ ├── include +│ │ │ │ ├── hello_dump.h +│ │ │ ├── BUILD.gn +│ │ │ ├── hello_dump.c # hidump实现 +│ │ ├── hdi_service # hdi_service +│ │ │ ├── BUILD.gn # 编译两个动态库:libhello_driver、libhello_interface_service_1.0 +│ │ │ ├── hello_interface_driver.cpp # driver:定义驱动入口的对象,将驱动入口注册到HDF框架中;在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动;当Init调用异常时,HDF框架会调用Release释放驱动资源并退出 +│ │ │ ├── hello_interface_service.cpp # 驱动服务 +│ │ │ ├── hello_interface_service.h +│ │ ├── utils/interface +│ │ │ ├── hello_log.h # 日志文件 +│ │ ├── BUILD.gn +│ │ ├── bundle.json +``` + +### 编译 + +1.将hellohdf/Peripheral文件夹下的hello文件夹拷贝到源码drivers/peripheral目录下 + +``` +cp hellohdf/Peripheral/hello 源码/drivers/peripheral +``` + +将hellohdf/IdlInterface文件夹下的hello文件夹拷贝到源码drivers/interface目录下 + +``` +cp hellohdf/IdlInterface/hello 源码/drivers/interface +``` + +将hellohdf/HcsConfig/device_info.hcs中的内容拷贝到源码vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs文件中,如下所示: + +``` + root { + device_info { + ... + hello :: host { + hostName = "hello_host"; + priority = 50; + hello_device :: device { + device0 :: deviceNode { + preload = 0; + policy = 2; + priority = 100; + moduleName = "libhello_driver.z.so"; + serviceName = "hello_interface_service"; + } + } + } + ... + } + } +``` + +2.配置产品:以rk3568为例,在源码vendor/hihope/rk3568/config.json文件中hdf子系统的components中增加以下内容: + +``` +{ + "component": "drivers_interface_hello", + "features": [] +}, +{ + "component": "drivers_peripheral_hello", + "features": [] +} +``` + +注意:drivers_interface_hello为drivers/interface/hello/v1_0/BUILD.gn中的part_name。drivers_peripheral_hello为drivers/peripheral/hello/bundle.json中的component。 + +3.编译,在源码下执行以下命令进行编译: + +``` +./build.sh --product-name rk3568 +``` + +编译成功后,将源码下out/rk3568/packages/phone/image镜像烧录在dayu200开发板上 + +### 验证 + +#### 动态加载 + +1.查看hostId:hdc连接开发板,进入/vendor/etc/init路径下,并查看hdf_devhost.cfg文件,使用hdc命令如下: + +``` +cat hdf_devhost.cfg +``` + +根据hostName找到对应hostId,如本例的hostName为hello_host,对应找到“name”为“hello_host”那一项,查看“path”的第二个参数,则为hostName对应的hostId,即14,如下所示: + +![image-20240724093743837](./figures/pic_show_hostid.png) + +2.运行可执行文件hdf_devhost,手动拉起host:进入/vendor/bin路径下,运行可执行文件hdf_devhost,传入一个参数为hostId,第二个参数为hostName;运行命令如下所示: + +``` +./hdf_devhost 14 hello_host +``` + +![image-20240903114845035](./figures/pic_show_exe.png) + +注意 :不可将进程kill + +3.查看host是否加载:新开一个命令行窗口,hdc进入开发板,执行以下命令查看进程是否拉起: + +``` +ps -A | grep host +``` + +屏幕显示hello_host进程号,则表明host已被拉起 + +![image-20240724093743837](./figures/pic_show_devhostPid.png) + +4.使用hidumper查看更多细节信息: + +查询所有正在运行的host + +``` + hidumper -s HdfDeviceServiceManager -a "-query" +``` + +![image-20240724093543096](./figures/pic_show_host.png) + +使用hidumper查看更多信息 + +``` +hidumper -s HdfDeviceServiceManager -a "-host hello_host -c" +``` + +打印出Hello, World! + +![image-20240724093535915](./figures/pic_show_dump.png) + +#### 静态加载 + +// todo 待补充 + diff --git a/src/cli/h2hdf/docs/figures/pic_code_frame.png b/src/cli/h2hdf/docs/figures/pic_code_frame.png new file mode 100644 index 0000000000000000000000000000000000000000..1f77649365e6174e8f482ffef45c86f8dc0d9c05 Binary files /dev/null and b/src/cli/h2hdf/docs/figures/pic_code_frame.png differ diff --git a/src/cli/h2hdf/docs/figures/pic_frame.png b/src/cli/h2hdf/docs/figures/pic_frame.png new file mode 100644 index 0000000000000000000000000000000000000000..5d6a068355c912d8c0350e663f794674a6ad6515 Binary files /dev/null and b/src/cli/h2hdf/docs/figures/pic_frame.png differ diff --git a/src/cli/h2hdf/docs/figures/pic_show_devhostPid.png b/src/cli/h2hdf/docs/figures/pic_show_devhostPid.png new file mode 100644 index 0000000000000000000000000000000000000000..51eec1f7f391a574301dda4d3c7fa1f5af4c1c5b Binary files /dev/null and b/src/cli/h2hdf/docs/figures/pic_show_devhostPid.png differ diff --git a/src/cli/h2hdf/docs/figures/pic_show_dump.png b/src/cli/h2hdf/docs/figures/pic_show_dump.png new file mode 100644 index 0000000000000000000000000000000000000000..e2c3a50e69593a71be913b75c0357c0aa8ad701e Binary files /dev/null and b/src/cli/h2hdf/docs/figures/pic_show_dump.png differ diff --git a/src/cli/h2hdf/docs/figures/pic_show_exe.png b/src/cli/h2hdf/docs/figures/pic_show_exe.png new file mode 100644 index 0000000000000000000000000000000000000000..d63fc90ae6b621654350bc608bf014cae1384ba7 Binary files /dev/null and b/src/cli/h2hdf/docs/figures/pic_show_exe.png differ diff --git a/src/cli/h2hdf/docs/figures/pic_show_host.png b/src/cli/h2hdf/docs/figures/pic_show_host.png new file mode 100644 index 0000000000000000000000000000000000000000..9ac593afdf4eb7e7d3c11264d2f53976cea535f4 Binary files /dev/null and b/src/cli/h2hdf/docs/figures/pic_show_host.png differ diff --git a/src/cli/h2hdf/docs/figures/pic_show_hostid.png b/src/cli/h2hdf/docs/figures/pic_show_hostid.png new file mode 100644 index 0000000000000000000000000000000000000000..7faaa1dbfd96a47ed8a1745b242428d0c458756e Binary files /dev/null and b/src/cli/h2hdf/docs/figures/pic_show_hostid.png differ diff --git a/src/cli/h2hdf/docs/usage.md b/src/cli/h2hdf/docs/usage.md new file mode 100644 index 0000000000000000000000000000000000000000..dedb587bb0d12980579a81316a9a5b98455f191d --- /dev/null +++ b/src/cli/h2hdf/docs/usage.md @@ -0,0 +1,187 @@ +# Usage guide + +## 简介 +在OpenHarmony系统中,上层应用或服务层通过调用HDF框架提供的HDI接口,能够以一种标准化和抽象化的方式与底层硬件设备进行交互。使用h2hdf工具,用户只需提供一个drivername,工具会自动生成整个框架的代码,包含驱动配置文件、idl接口、驱动程序driver和驱动服务框架。 + +![image-20240724093743837](./figures/pic_code_frame.png) + +## 约束 +系统:建议Ubuntu 20.04或者Windows 10 + +依赖版本:VS Code 1.62.0 + +## 使用方法 + +### 生成 + +1.安装typescript:在napi_generator/src/cli/h2hdf/src目录下执行命令: + + npm i typescript + +2.安装stdio:在napi_generator/src/cli/h2hdf目录下执行命令: + + npm i stdio + +3.在napi_generator/src/cli/h2hdf/src下执行以下命令生成ts声明文件: + +``` +node main.js -n hello +``` + +其中,参数详情如下: + + -n, drivername,例如:hello + + -v, 可选参数,版本,默认为4.1 + + -o, 可选参数,默认为当前目录,指定生成框架代码输出路径。 + +6.执行成功后在napi_generator/src/cli/h2hdf/src/下生成hellohdf文件夹,文件夹中目录结构如下所示: + +``` +hellohdf +├── HcsConfig # hcs配置文件 +│ ├── device_info.hcs # 内容配置到源码vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs文件中 +├── IdlInterface +│ ├── hello # 拷贝到源码drivers/interface +│ │ ├── v1_0 +│ │ │ ├── BUILD.gn +│ │ │ ├── IHelloInterface.idl # idl接口 +│ │ ├── bundle.json +├── Peripheral # 拷贝到源码drivers/peripheral +│ ├── hello +│ │ ├── hal +│ │ │ ├── include +│ │ │ │ ├── hello_dump.h +│ │ │ ├── BUILD.gn +│ │ │ ├── hello_dump.c # hidump实现 +│ │ ├── hdi_service # hdi_service +│ │ │ ├── BUILD.gn # 编译两个动态库:libhello_driver、libhello_interface_service_1.0 +│ │ │ ├── hello_interface_driver.cpp # driver:定义驱动入口的对象,将驱动入口注册到HDF框架中;在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动;当Init调用异常时,HDF框架会调用Release释放驱动资源并退出 +│ │ │ ├── hello_interface_service.cpp # 驱动服务 +│ │ │ ├── hello_interface_service.h +│ │ ├── utils/interface +│ │ │ ├── hello_log.h # 日志文件 +│ │ ├── BUILD.gn +│ │ ├── bundle.json +``` + +### 编译 + +1.将hellohdf/Peripheral文件夹下的hello文件夹拷贝到源码drivers/peripheral目录下 + +``` +cp hellohdf/Peripheral/hello 源码/drivers/peripheral +``` + +将hellohdf/IdlInterface文件夹下的hello文件夹拷贝到源码drivers/interface目录下 + +``` +cp hellohdf/IdlInterface/hello 源码/drivers/interface +``` + +将hellohdf/HcsConfig/device_info.hcs中的内容拷贝到源码vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs文件中,如下所示: + +``` + root { + device_info { + ... + hello :: host { + hostName = "hello_host"; + priority = 50; + hello_device :: device { + device0 :: deviceNode { + preload = 0; + policy = 2; + priority = 100; + moduleName = "libhello_driver.z.so"; + serviceName = "hello_interface_service"; + } + } + } + ... + } + } +``` + +2.配置产品:以rk3568为例,在源码vendor/hihope/rk3568/config.json文件中hdf子系统的components中增加以下内容: + +``` +{ + "component": "drivers_interface_hello", + "features": [] +}, +{ + "component": "drivers_peripheral_hello", + "features": [] +} +``` + +注意:drivers_interface_hello为drivers/interface/hello/v1_0/BUILD.gn中的part_name。drivers_peripheral_hello为drivers/peripheral/hello/bundle.json中的component。 + +3.编译,在源码下执行以下命令进行编译: + +``` +./build.sh --product-name rk3568 +``` + +编译成功后,将源码下out/rk3568/packages/phone/image镜像烧录在dayu200开发板上 + +### 验证 + +#### 动态加载 + +1.查看hostId:hdc连接开发板,进入/vendor/etc/init路径下,并查看hdf_devhost.cfg文件,使用hdc命令如下: + +``` +cat hdf_devhost.cfg +``` + +根据hostName找到对应hostId,如本例的hostName为hello_host,对应找到“name”为“hello_host”那一项,查看“path”的第二个参数,则为hostName对应的hostId,即14,如下所示: + +![image-20240724093743837](./figures/pic_show_hostid.png) + +2.运行可执行文件hdf_devhost,手动拉起host:进入/vendor/bin路径下,运行可执行文件hdf_devhost,传入一个参数为hostId,第二个参数为hostName;运行命令如下所示: + +``` +./hdf_devhost 14 hello_host +``` + +![image-20240903114845035](./figures/pic_show_exe.png) + +注意 :不可将进程kill + +3.查看host是否加载:新开一个命令行窗口,hdc进入开发板,执行以下命令查看进程是否拉起: + +``` +ps -A | grep host +``` + +屏幕显示hello_host进程号,则表明host已被拉起 + +![image-20240724093743837](./figures/pic_show_devhostPid.png) + +4.使用hidumper查看更多细节信息: + +查询所有正在运行的host + +``` + hidumper -s HdfDeviceServiceManager -a "-query" +``` + +![image-20240724093543096](./figures/pic_show_host.png) + +使用hidumper查看更多信息 + +``` +hidumper -s HdfDeviceServiceManager -a "-host hello_host -c" +``` + +打印出Hello, World! + +![image-20240724093535915](./figures/pic_show_dump.png) + +#### 静态加载 + +// todo 待补充 + diff --git a/src/cli/h2hdf/package.json b/src/cli/h2hdf/package.json new file mode 100644 index 0000000000000000000000000000000000000000..98faddb5dcbe806bf4e3ee95bd5d727aeec8fb63 --- /dev/null +++ b/src/cli/h2hdf/package.json @@ -0,0 +1,34 @@ +{ + "name": "hdf-gen", + "version": "1.0.0", + "description": "", + "main": "main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "stdio": "^2.1.3", + "typescript": "^4.9.5" + }, + "bin": "./src/main.js", + "pkg": { + "assets": [ + "./src/templete/HcsconfigTemplete/hcsconfigTemplete.gen", + "./src/templete/IdlInterfaceTemplete/buildgnTemplete.gen", + "./src/templete/v4_1/IdlInterfaceTemplete/bundlejsonTemplete.gen", + "./src/templete/IdlInterfaceTemplete/idlInterfaceTemplete.gen", + "./src/templete/PeripheralTemplete/DumpExampleTemplete/v4_1/buildgnTemplete.gen", + "./src/templete/PeripheralTemplete/DumpExampleTemplete/dumpCTemplete.gen", + "./src/templete/PeripheralTemplete/DumpExampleTemplete/dumpHTemplete.gen", + "./src/templete/PeripheralTemplete/HdiServiceTemplete/v4_1/buildgnTemplete.gen", + "./src/templete/PeripheralTemplete/HdiServiceTemplete/driverTemplete.gen", + "./src/templete/PeripheralTemplete/HdiServiceTemplete/logHTemplete.gen", + "./src/templete/PeripheralTemplete/HdiServiceTemplete/serviceCppTemplete.gen", + "./src/templete/PeripheralTemplete/HdiServiceTemplete/serviceHTemplete.gen", + "./src/templete/PeripheralTemplete/buildgnTemplete.gen", + "./src/templete/PeripheralTemplete/v4_1/bundlejsonTemplete.gen" + ] + } +} diff --git a/src/cli/h2hdf/src/generate.js b/src/cli/h2hdf/src/generate.js new file mode 100644 index 0000000000000000000000000000000000000000..f7d6d4e9349173e8dc54abf01dea36f40b3bd734 --- /dev/null +++ b/src/cli/h2hdf/src/generate.js @@ -0,0 +1,282 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the 'License'); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an 'AS IS' BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +const fs = require('fs'); +const path = require('path'); + +/* 根据用户输入的driver名字生成framework框架 + * drivername:用户输入的驱动名,frameworkJson: 模板内容,out:生成框架路径 + * 替换模板中的名字并写文件输出 + */ +function genDriverFramework(driverName, frameworkJson, version, out = '') { + let frameworkPath = pathJoin(out, driverName + 'hdf'); + + let namespaceName = driverName.substring(0, 1).toUpperCase() + driverName.substring(1, driverName.length); + let idlFileName = 'I' + namespaceName + 'Interface'; + let rootInfo = { + 'version': version, + 'driverName': driverName, + 'namespaceName': namespaceName, + 'idlFileName': idlFileName, + }; + + // 生成Hcs配置文件 + genHcsconfigFile(frameworkJson, driverName, frameworkPath); + + // 生成Idl接口 + genInterface(frameworkPath, frameworkJson, rootInfo); + + // 生成hdi_service + genPeripheral(frameworkPath, frameworkJson, rootInfo); +} + +function genPeripheral(frameworkPath, frameworkJson, rootInfo) { + + let peripheralPath = pathJoin(frameworkPath, 'Peripheral/' + rootInfo.driverName); + createDirectorySync(peripheralPath); + + // dump文件路径:dump.c 与 BUILD.gn + // build.gn out/hdf/Peripheral/xxx/hal/BUILD.gn + genExampleDumpfile(peripheralPath, frameworkJson, rootInfo); + + // hdi路径 + genHdiService(peripheralPath, frameworkJson, rootInfo); + + // 日志文件路径 out/hdf/Peripheral/xxx/utils/interface/xxx_log.h + genLogFile(peripheralPath, rootInfo, frameworkJson); + + // 生成编译文件bundle.json和BUILD.gn文件 + genBuildFile(peripheralPath, frameworkJson, rootInfo); +} + +function genBuildFile(peripheralPath, frameworkJson, rootInfo) { + // out/hdf/Peripheral/xxx/bundle.json + let genBundlejsonPath = pathJoin(peripheralPath, 'bundle.json'); + let bundlejsonPath = ''; + if (rootInfo.version === 'v4_1') { + bundlejsonPath = path.join(__dirname, frameworkJson.PeripheralTemplete.bundlejsonTemplete.v4_1); + } + let bundlejsonContent = readFile(bundlejsonPath); + bundlejsonContent = replaceAll(bundlejsonContent, '[driver_name]', rootInfo.driverName); + writeFile(genBundlejsonPath, bundlejsonContent); + // out/hdf/Peripheral/xxx/BUILD.gn + let genBuildgnPath = pathJoin(peripheralPath, 'BUILD.gn'); + let buildgnPath = path.join(__dirname, frameworkJson.PeripheralTemplete.buildgnTemplete); + let buildgnContent = readFile(buildgnPath); + buildgnContent = replaceAll(buildgnContent, '[driver_name]', rootInfo.driverName); + writeFile(genBuildgnPath, buildgnContent); +} + +function genLogFile(peripheralPath, rootInfo, frameworkJson) { + let genLogPath = pathJoin(peripheralPath, 'utils/interface'); + createDirectorySync(genLogPath); + let genLogFile = pathJoin(genLogPath, rootInfo.driverName + '_log.h'); + let logPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.logHTemplete); + let logContent = readFile(logPath); + logContent = replaceAll(logContent, '[upper_driver_name]', rootInfo.driverName.toUpperCase()); + writeFile(genLogFile, logContent); +} + +function genHdiService(peripheralPath, frameworkJson, rootInfo) { + let hdiPath = pathJoin(peripheralPath, 'hdi_service'); + let driverInterName = rootInfo.namespaceName + 'Interface'; + // 创建hdi_service文件夹 + createDirectorySync(hdiPath); + // out/hdf/Peripheral/xxx/hdi_service/xxx_interface_driver.cpp + let genHdiDriverPath = pathJoin(hdiPath, rootInfo.driverName + '_interface_driver.cpp'); + let driverPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.driverTemplete); + let driverContent = readFile(driverPath); + driverContent = replaceAll(driverContent, '[driver_name]', rootInfo.driverName); + driverContent = replaceAll(driverContent, '[driver_idl_name]', rootInfo.idlFileName); + driverContent = replaceAll(driverContent, '[driver_inter_name]', driverInterName); + driverContent = replaceAll(driverContent, '[driver_namespace_name]', rootInfo.namespaceName); + writeFile(genHdiDriverPath, driverContent); + + // out/hdf/Peripheral/xxx/hdi_service/xxx_interface_service.cpp + let genHdiServiceCppPath = pathJoin(hdiPath, rootInfo.driverName + '_interface_service.cpp'); + let serviceCppPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.serviceCppTemplete); + let serviceCppContent = readFile(serviceCppPath); + serviceCppContent = replaceAll(serviceCppContent, '[driver_name]', rootInfo.driverName); + serviceCppContent = replaceAll(serviceCppContent, '[driver_idl_name]', rootInfo.idlFileName); + serviceCppContent = replaceAll(serviceCppContent, '[driver_inter_name]', driverInterName); + serviceCppContent = replaceAll(serviceCppContent, '[driver_namespace_name]', rootInfo.namespaceName); + writeFile(genHdiServiceCppPath, serviceCppContent); + + // out/hdf/Peripheral/xxx/hdi_service/xxx_interface_service.h + let genHdiServiceHPath = pathJoin(hdiPath, rootInfo.driverName + '_interface_service.h'); + let serviceHPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.serviceHTemplete); + let serviceHContent = readFile(serviceHPath); + serviceHContent = replaceAll(serviceHContent, '[driver_name]', rootInfo.driverName); + serviceHContent = replaceAll(serviceHContent, '[driver_idl_name]', rootInfo.idlFileName); + serviceHContent = replaceAll(serviceHContent, '[driver_inter_name]', driverInterName); + serviceHContent = replaceAll(serviceHContent, '[driver_namespace_name]', rootInfo.namespaceName); + serviceHContent = replaceAll(serviceHContent, '[upper_driver_name]', rootInfo.driverName.toUpperCase()); + writeFile(genHdiServiceHPath, serviceHContent); + + // 生成hdi_service下面的BUILD.gn: out/hdf/Peripheral/xxx/hdi_service/ + let genHdiServiceGnPath = pathJoin(hdiPath, 'BUILD.gn'); + let serviceGnPath = ''; + if (rootInfo.version === 'v4_1') { + serviceGnPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.buildgnTemplete.v4_1); + } + + let serviceGnContent = readFile(serviceGnPath); + serviceGnContent = replaceAll(serviceGnContent, '[driver_name]', rootInfo.driverName); + writeFile(genHdiServiceGnPath, serviceGnContent); +} + +function genExampleDumpfile(peripheralPath, frameworkJson, rootInfo) { + let dumpExamplePath = pathJoin(peripheralPath, 'hal'); + createDirectorySync(dumpExamplePath); + let genDumpExampleGnPath = pathJoin(dumpExamplePath, 'BUILD.gn'); + let dumpExampleGnPath = ''; + if (rootInfo.version === 'v4_1') { + dumpExampleGnPath = path.join(__dirname, frameworkJson.PeripheralTemplete.DumpExampleTemplete.buildgnTemplete.v4_1); + } + let dumpExampleGnContent = readFile(dumpExampleGnPath); + dumpExampleGnContent = replaceAll(dumpExampleGnContent, '[driver_name]', rootInfo.driverName); + writeFile(genDumpExampleGnPath, dumpExampleGnContent); + + // dump.c out/hdf/Peripheral/xxx/hal/xxx_dump.c + let genDumpCExamplePath = pathJoin(dumpExamplePath, rootInfo.driverName + '_dump.c'); + let dumpCExamplePath = path.join(__dirname, frameworkJson.PeripheralTemplete.DumpExampleTemplete.dumpCTemplete); + let dumpCExampleContent = readFile(dumpCExamplePath); + dumpCExampleContent = replaceAll(dumpCExampleContent, '[driver_name]', rootInfo.driverName); + dumpCExampleContent = replaceAll(dumpCExampleContent, '[driver_func_name]', rootInfo.namespaceName); + writeFile(genDumpCExamplePath, dumpCExampleContent); + // dump.h out/hdf/Peripheral/xxx/hal/include/xxx_dump.h + let genDumpHExamplePath = pathJoin(dumpExamplePath, 'include'); + createDirectorySync(genDumpHExamplePath); + let dumpHExamplePath = path.join(__dirname, frameworkJson.PeripheralTemplete.DumpExampleTemplete.dumpHTemplete); + let dumpHExampleContent = readFile(dumpHExamplePath); + dumpHExampleContent = replaceAll(dumpHExampleContent, '[driver_name_toupper]', rootInfo.driverName.toUpperCase()); + dumpHExampleContent = replaceAll(dumpHExampleContent, '[driver_func_name]', rootInfo.namespaceName); + let genDumpHExampleFile = pathJoin(genDumpHExamplePath, rootInfo.driverName + '_dump.h'); + writeFile(genDumpHExampleFile, dumpHExampleContent); +} + +function genInterface(frameworkPath, frameworkJson, rootInfo) { + // idl文件路径 out/hdf/IdlInterface/foo/v1_0/IXxxInterface.idl + let idlPath = pathJoin(frameworkPath, 'IdlInterface/' + rootInfo.driverName); + let genIdlFilepath = pathJoin(idlPath, 'v1_0/'); + createDirectorySync(genIdlFilepath); + let idlFilepath = path.join(__dirname, frameworkJson.IdlInterfaceTemplete.idlInterfaceTemplete); + let idlFileContent = readFile(idlFilepath); + idlFileContent = replaceAll(idlFileContent, '[driver_name]', rootInfo.driverName); + idlFileContent = replaceAll(idlFileContent, '[driver_idl_name]', rootInfo.idlFileName); + let genIdlFile = pathJoin(genIdlFilepath, rootInfo.idlFileName + '.idl'); + writeFile(genIdlFile, idlFileContent); + + // idl接口bundlejson路径 out/hdf/IdlInterface/foo/bundle.json + let genIdlBundlejsonPath = pathJoin(idlPath, 'bundle.json'); + let idlBundlejsonPath = ''; + if (rootInfo.version === 'v4_1') { + idlBundlejsonPath = path.join(__dirname, frameworkJson.IdlInterfaceTemplete.bundlejsonTemplete.v4_1); + } + + let idlBundlejsonContent = readFile(idlBundlejsonPath); + idlBundlejsonContent = replaceAll(idlBundlejsonContent, '[driver_name]', rootInfo.driverName); + writeFile(genIdlBundlejsonPath, idlBundlejsonContent); + + // idl接口BUILD.gn路径 out/hdf/IdlInterface/foo/v1_0/BUILD.gn + let genIdlBuildgnPath = pathJoin(genIdlFilepath, 'BUILD.gn'); + let idlBuildgnPath = path.join(__dirname, frameworkJson.IdlInterfaceTemplete.buildgnTemplete); + let idlBuildgnContent = readFile(idlBuildgnPath); + idlBuildgnContent = replaceAll(idlBuildgnContent, '[driver_name]', rootInfo.driverName); + idlBuildgnContent = replaceAll(idlBuildgnContent, '[driver_idl_name]', rootInfo.idlFileName); + writeFile(genIdlBuildgnPath, idlBuildgnContent); +} + +function genHcsconfigFile(frameworkJson, driverName, frameworkPath) { + // 读取hcs配置文件模板 hcs配置文件 + let hcsconfigPath = path.join(__dirname, frameworkJson.HcsconfigTemplete); + let hcsconfigContent = readFile(hcsconfigPath); + // 生成hcs config文件内容: 根据用户输入的drivername配置hcs文件 + hcsconfigContent = replaceAll(hcsconfigContent, '[driver_name]', driverName); + // 生成device_info.hcs 配置文件路径 out/hdf/HcsConfig/device_info.hcs + let genHcsconfigPath = pathJoin(frameworkPath, 'HcsConfig'); + createDirectorySync(genHcsconfigPath); + let genHcsconfigFile = pathJoin(genHcsconfigPath, 'device_info.hcs'); + writeFile(genHcsconfigFile, hcsconfigContent); +} + +function replaceAll(s, sfrom, sto) { + while (s.indexOf(sfrom) >= 0) { + s = s.replace(sfrom, sto); + } + return s; +} + +function pathJoin(...args) { + return path.join(...args); +} + +function utf8ArrayToStr(array) { + let char2; + let char3; + let outStr = ''; + let len = array.length; + let i = 0; + while (i < len) { + let ch = array[i++]; + switch (ch >> 4) { + case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: + // 0xxxxxxx + outStr += String.fromCharCode(ch); + break; + case 12: case 13: + // 110x xxxx 10xx xxxx + char2 = array[i++]; + outStr += String.fromCharCode(((ch & 0x1F) << 6) | (char2 & 0x3F)); + break; + case 14: + // 1110 xxxx 10xx xxxx 10xx xxxx + char2 = array[i++]; + char3 = array[i++]; + outStr += String.fromCharCode(((ch & 0x0F) << 12) | + ((char2 & 0x3F) << 6) | + ((char3 & 0x3F) << 0)); + break; + } + } + + return outStr; +} + +function readFile(fn) { + if (!fs.existsSync(fn)) { + return ''; + } + let data = fs.readFileSync(fn); + data = utf8ArrayToStr(data); + return data; +} + +function createDirectorySync(directoryPath) { + try { + if (!fs.existsSync(directoryPath)) { + fs.mkdirSync(directoryPath, { recursive: true }); + } + } catch (err) { + console.error(`无法创建文件夹 ${directoryPath}: ${err}`); + } +} + +function writeFile(fn, str) { + fs.writeFileSync(fn, str, { encoding: 'utf8' }); +} + +module.exports = { + genDriverFramework +}; \ No newline at end of file diff --git a/src/cli/h2hdf/src/main.js b/src/cli/h2hdf/src/main.js new file mode 100644 index 0000000000000000000000000000000000000000..50f5b1c571dae2f822e7626e1bdd4cf16e3deb81 --- /dev/null +++ b/src/cli/h2hdf/src/main.js @@ -0,0 +1,72 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the 'License'); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an 'AS IS' BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +const fs = require('fs'); +const path = require('path'); +const stdio = require('stdio'); +const main = require('./generate'); + +let ops = stdio.getopt({ + // 输入driver name ,输入一个字符串,默认为hello + 'drivername': { key: 'n', args: 1, description: 'driver name', default: 'hello' }, + // 输入版本号 + 'version': { key: 'v', args: 1, description: 'source version', default: '4.1' }, + // 输出文件夹路径 + 'out': { key: 'o', args: 1, description: 'output directory', default: '' }, +}); + +const allowedVersion = ['4.1', '4-1', '4_1', '4.0', '5.0', '5.1']; + +let drivername = ops.drivername; +let version = ops.version; +// 若drivername不为空,则生成,否则打印错误信息 +if (drivername.trim().length !== 0 && checkInput(drivername)) { + if (!isValidValue(version, allowedVersion)) { + // 版本号不符合规则 请输入正确的版本号 + console.log('请输入正确的版本号!如:4.1'); + return; + } + // 在这里读取cfg文件 + let frameworkJsonPath = path.join(__dirname, './templete/framework.json'); + let frameworkJson = getJsonCfg(frameworkJsonPath); + if (version === '4.1' || version === '4-1' || version === '4_1') { + version = 'v4_1'; + } else { + console.log('其他版本暂不支持...'); + return; + } + + // 然后再调用templete生成模板 + main.genDriverFramework(drivername, frameworkJson, version, ops.out); + console.log('Generate Success'); +} else { + // 输入的名字不符合规则 + console.log('请输入正确的drivername!'); +} + +function isValidValue(value, allowedVersion) { + return allowedVersion.includes(value); +} + +function checkInput(input) { + const regex = /\b[a-zA-Z_][a-zA-Z0-9_]*\b/; + return regex.test(input); +} + +function getJsonCfg(jsonFilePath) { + let jsonCfg = null; + let jsonFile = fs.readFileSync(jsonFilePath, { encoding: 'utf8' }); + jsonCfg = JSON.parse(jsonFile); + return jsonCfg; +} diff --git a/src/cli/h2hdf/src/templete/HcsconfigTemplete/hcsconfigTemplete.gen b/src/cli/h2hdf/src/templete/HcsconfigTemplete/hcsconfigTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..fc2ff6a59b6d3a63b321628508b53a99bbfcb509 --- /dev/null +++ b/src/cli/h2hdf/src/templete/HcsconfigTemplete/hcsconfigTemplete.gen @@ -0,0 +1,13 @@ +[driver_name] :: host { + hostName = "[driver_name]_host"; + priority = 50; + [driver_name]_device :: device { + device0 :: deviceNode { + preload = 0; + policy = 2; + priority = 100; + moduleName = "lib[driver_name]_driver.z.so"; + serviceName = "[driver_name]_interface_service"; + } + } +} \ No newline at end of file diff --git a/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/buildgnTemplete.gen b/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/buildgnTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..6ae05cb2d7bb6e292c06f0eebd46820fb32880d4 --- /dev/null +++ b/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/buildgnTemplete.gen @@ -0,0 +1,23 @@ +# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//drivers/hdf_core/adapter/uhdf2/hdi.gni") # 编译idl必须要导入的模板 +hdi("[driver_name]") { # 目标名称,会生成两个so,分别对应 lib[driver_name]_client_v1.0.z.so 和 lib[driver_name]_stub_v1.0.z.so + module_name = "[driver_name]_service" # module_name控制dirver文件中驱动描 述符(struct HdfDriverEntry)的moduleName + sources = [ # 参与编译的idl文件 + "[driver_idl_name].idl", # 接口idl + ] + language = "cpp" # 控制idl生成c或c++代码 可选择`c`或`cpp` + subsystem_name = "hdf" # 子系统名 + part_name = "drivers_interface_[driver_name]" # 组件名 +} diff --git a/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/idlInterfaceTemplete.gen b/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/idlInterfaceTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..fa767b19f6cb665f3f20e0bf8843076cd6bfd9b2 --- /dev/null +++ b/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/idlInterfaceTemplete.gen @@ -0,0 +1,5 @@ +package ohos.hdi.[driver_name].v1_0; + +interface [driver_idl_name] { + Helloworld([in] String sendMsg, [out] String recvMsg); +} \ No newline at end of file diff --git a/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/v4_1/bundlejsonTemplete.gen b/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/v4_1/bundlejsonTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..f38d3b4498dc5d644100e5f6c350f76473df7100 --- /dev/null +++ b/src/cli/h2hdf/src/templete/IdlInterfaceTemplete/v4_1/bundlejsonTemplete.gen @@ -0,0 +1,62 @@ +{ + "name": "@ohos/drivers_interface_[driver_name]", + "description": "[driver_name] device driver interface", + "version": "4.1", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "drivers/interface/[driver_name]" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "drivers_interface_[driver_name]", + "subsystem": "hdf", + "syscap": [], + "adapted_system_type": ["standard"], + "rom": "675KB", + "ram": "1024KB", + "deps": { + "components": [ + "ipc", + "hdf_core", + "hilog", + "c_utils" + ], + "third_party": [] + }, + "build": { + "sub_component": [ + "//drivers/interface/[driver_name]/v1_0:[driver_name]_idl_target" + ], + "test": [ + ], + "inner_kits": [ + { + "name": "//drivers/interface/[driver_name]/v1_0:lib[driver_name]_proxy_1.0", + "header": { + "header_files": [ + ], + "header_base": "//drivers/interface/[driver_name]" + } + }, + { + "name": "//drivers/interface/[driver_name]/v1_0:lib[driver_name]_stub_1.0", + "header": { + "header_files": [ + ], + "header_base": "//drivers/interface/[driver_name]" + } + }, + { + "name": "//drivers/interface/[driver_name]/v1_0:[driver_name]_idl_headers", + "header": { + "header_files": [ + ], + "header_base": "//drivers/interface/[driver_name]" + } + } + ] + } + } + } \ No newline at end of file diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/dumpCTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/dumpCTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..b3f17584664346b71ee79cf83604f8fea81b932e --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/dumpCTemplete.gen @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "[driver_name]_dump.h" +#include +#include +#include "devhost_dump_reg.h" +#include "hdf_base.h" +#include "[driver_name]_log.h" + +#define HDF_LOG_TAG uhdf_[driver_name]_service + +// -c dump the helloworld info +static const char *g_dumpHelp = + " usage:\n" + " -h, --help: dump help\n" + " -c, --channel: dump the [driver_name] channel info\n"; + +static uint32_t ShowHelloworldInfo(struct HdfSBuf *reply) +{ + int32_t ret; + const char *helloWorldMessage = "Hello, World!"; + + ret = HdfSbufWriteString(reply, helloWorldMessage); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: write hello world info failed", __func__); + return HDF_FAILURE; + } + + HDF_LOGI("%{public}s: [driver_name]dump: print hello world !", __func__); + + return HDF_SUCCESS; + +} + +static int32_t Dump[driver_func_name]Channel(struct HdfSBuf *reply) +{ + int32_t ret; + HDF_LOGI("%{public}s: get [driver_name] dump channel begin", __func__); + ret = ShowHelloworldInfo(reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: show hello world info failed", __func__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t [driver_func_name]DriverDump(struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint32_t i; + uint32_t argv = 0; + HDF_LOGI("%{public}s: get [driver_name] dump begin xx", __func__); + if (data == NULL || reply == NULL) { + return HDF_FAILURE; + } + + if (!HdfSbufReadUint32(data, &argv)) { + HDF_LOGE("%{public}s: read argv failed", __func__); + return HDF_FAILURE; + } + + if (argv == 0) { + if (!HdfSbufWriteString(reply, g_dumpHelp)) { + HDF_LOGE("%{public}s: write -h failed", __func__); + return HDF_FAILURE; + } + } + + for (i = 0; i < argv; i++) { + const char *value = HdfSbufReadString(data); + if (value == NULL) { + HDF_LOGE("%{public}s value is invalid", __func__); + return HDF_FAILURE; + } + + if (strcmp(value, "-h") == HDF_SUCCESS) { + if (!HdfSbufWriteString(reply, g_dumpHelp)) { + HDF_LOGE("%{public}s: write -h failed", __func__); + return HDF_FAILURE; + } + continue; + } else if (strcmp(value, "-c") == HDF_SUCCESS) { + Dump[driver_func_name]Channel(reply); + continue; + } + } + + return HDF_SUCCESS; +} + +int32_t Get[driver_func_name]Dump(struct HdfSBuf *data, struct HdfSBuf *reply) +{ + HDF_LOGI("%{public}s: get [driver_name] dump begin", __func__); + int32_t ret = [driver_func_name]DriverDump(data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get [driver_name] dump failed", __func__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/dumpHTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/dumpHTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..018647c5d03982ec46764e1a5ff74f39ae366096 --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/dumpHTemplete.gen @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef [driver_name_toupper]_DUMP_H +#define [driver_name_toupper]_DUMP_H + +#include +#include +#include "hdf_sbuf.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +int32_t Get[driver_func_name]Dump(struct HdfSBuf *data, struct HdfSBuf *reply); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* HDI_[driver_name_toupper]_DUMP_H */ \ No newline at end of file diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/v4_1/buildgnTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/v4_1/buildgnTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..c724378bdd00246a18ad1785214d9ea6f9cd6d95 --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/DumpExampleTemplete/v4_1/buildgnTemplete.gen @@ -0,0 +1,51 @@ +#Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +#Licensed under the Apache License, Version 2.0 (the "License"); +#you may not use this file except in compliance with the License. +#You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +#Unless required by applicable law or agreed to in writing, software +#distributed under the License is distributed on an "AS IS" BASIS, +#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +#See the License for the specific language governing permissions and +#limitations under the License. +# + +import("//build/ohos.gni") + +config("libhdi_[driver_name]_pub_config") { + visibility = [ ":*" ] +} +ohos_shared_library("hdi_[driver_name]") { + public_configs = [ ":libhdi_[driver_name]_pub_config" ] + sources = [ + "[driver_name]_dump.c", + ] + include_dirs = [ + "include", + "../utils/interface", + "//third_party/bounds_checking_function/include", + ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "drivers_peripheral_[driver_name]" + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "hdf_core:libhdf_host", + "hdf_core:libhdf_utils", + "hilog:libhilog", + ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/driverTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/driverTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..deda4b245a06900bdd99be3e1875653cbbe75f81 --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/driverTemplete.gen @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "v1_0/[driver_name]_interface_stub.h" + +#define HDF_LOG_TAG [driver_name]_interface_driver + +using namespace OHOS::HDI::[driver_namespace_name]::V1_0; + +struct Hdf[driver_inter_name]Host { + struct IDeviceIoService ioService; + OHOS::sptr stub; +}; + +/* + * 处理客户端请求的Dispatch方法: 处理来自客户端的IO请求 + * client:指向HdfDeviceIoClient结构体的指针,表示发起请求的客户端。 + * cmdId:命令ID,标识了要执行的命令或操作。 + * data:指向HdfSBuf结构体的指针,包含了请求的数据。 + * reply:指向另一个HdfSBuf结构体的指针,用于存放响应的数据。 + */ +static int32_t [driver_inter_name]DriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, + struct HdfSBuf *reply) +{ + auto *hdf[driver_inter_name]Host = CONTAINER_OF(client->device->service, struct Hdf[driver_inter_name]Host, ioService); + + // 声明两个MessageParcel对象,用于序列化和反序列化IPC通信中的数据 + OHOS::MessageParcel *dataParcel = nullptr; + OHOS::MessageParcel *replyParcel = nullptr; + // 创建一个MessageOption对象,用于设置IPC通信的选项。 + OHOS::MessageOption option; + + // 响应序列化:将HdfSBuf中的数据转换为MessageParcel对象。如果转换失败,记录错误并返回错误代码。 + if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__); + return HDF_ERR_INVALID_PARAM; + } + + // 数据序列化:尝试将响应数据的HdfSBuf转换为MessageParcel对象。如果失败,也记录错误并返回错误代码。 + if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__); + return HDF_ERR_INVALID_PARAM; + } + + // 调用stub对象的SendRequest方法,发送请求。这个方法执行实际的IPC调用,将cmdId和序列化后的请求数据dataParcel发送给服务端,并将响应数据反序列化到replyParcel中。 + return hdf[driver_inter_name]Host->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option); +} + +// 驱动自身业务初始化的接口 +static int Hdf[driver_inter_name]DriverInit(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGI("%{public}s: driver init start", __func__); + return HDF_SUCCESS; +} + +// 将驱动对外提供的服务能力接口绑定到HDF框架 +static int Hdf[driver_inter_name]DriverBind(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGI("%{public}s: driver bind start", __func__); + // 创建对象:该对象是驱动服务的具体实现 + auto *hdf[driver_inter_name]Host = new (std::nothrow) Hdf[driver_inter_name]Host; + if (hdf[driver_inter_name]Host == nullptr) { + HDF_LOGE("%{public}s: failed to create create Hdf[driver_inter_name]Host object", __func__); + return HDF_FAILURE; + } + + // 为ioService结构体设置回调函数:设置的Dispatch函数用于处理IO请求 + hdf[driver_inter_name]Host->ioService.Dispatch = [driver_inter_name]DriverDispatch; + hdf[driver_inter_name]Host->ioService.Open = NULL; + hdf[driver_inter_name]Host->ioService.Release = NULL; + + auto serviceImpl = OHOS::HDI::[driver_namespace_name]::V1_0::[driver_idl_name]::Get(true); + if (serviceImpl == nullptr) { + HDF_LOGE("%{public}s: failed to get of implement service", __func__); + delete hdf[driver_inter_name]Host; + return HDF_FAILURE; + } + + // 使用ObjectCollector的GetOrNewObject方法获取或创建一个Stub对象。Stub对象是服务接口的客户端代理,用于发起远程调用。 + hdf[driver_inter_name]Host->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, + OHOS::HDI::[driver_namespace_name]::V1_0::[driver_idl_name]::GetDescriptor()); + if (hdf[driver_inter_name]Host->stub == nullptr) { + HDF_LOGE("%{public}s: failed to get stub object", __func__); + delete hdf[driver_inter_name]Host; + return HDF_FAILURE; + } + + // 将ioService绑定到deviceObject,这样HDF框架就可以通过deviceObject来访问服务 + deviceObject->service = &hdf[driver_inter_name]Host->ioService; + HDF_LOGI("%{public}s: driver bind end", __func__); + return HDF_SUCCESS; +} + +// 驱动释放资源的接口 +static void Hdf[driver_inter_name]DriverRelease(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGI("%{public}s: driver release start", __func__); + if (deviceObject->service == nullptr) { + return; + } + + auto *hdf[driver_inter_name]Host = CONTAINER_OF(deviceObject->service, struct Hdf[driver_inter_name]Host, ioService); + if (hdf[driver_inter_name]Host != nullptr) { + delete hdf[driver_inter_name]Host; + } +} + +/* + * 定义驱动入口的对象,必须为HdfDriverEntry(在hdf_device_desc.h中定义)类型的全局变量。 + */ +struct HdfDriverEntry g_[driver_name]interfaceDriverEntry = { + .moduleVersion = 1, + .moduleName = "[driver_name]_service", + .Bind = Hdf[driver_inter_name]DriverBind, + .Init = Hdf[driver_inter_name]DriverInit, + .Release = Hdf[driver_inter_name]DriverRelease, +}; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * 调用HDF_INIT将驱动入口注册到HDF框架中。 + * 在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动;当Init调用异常时,HDF框架会调用Release释放驱动资源并退出。 + */ +HDF_INIT(g_[driver_name]interfaceDriverEntry); +#ifdef __cplusplus +} +#endif /* __cplusplus */ diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/logHTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/logHTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..d186466de248b54c8348e5d2299497e290807ba8 --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/logHTemplete.gen @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef [upper_driver_name]_UHDF_LOG_H +#define [upper_driver_name]_UHDF_LOG_H + +#include "hdf_log.h" + +#ifdef LOG_DOMAIN +#undef LOG_DOMAIN +#endif +#define LOG_DOMAIN 0xD002516 + +#endif //[upper_driver_name]_UHDF_LOG_H \ No newline at end of file diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/serviceCppTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/serviceCppTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..75c39f8a347cac0dfb2d7a608d8be2e53f00aae4 --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/serviceCppTemplete.gen @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "v1_0/[driver_name]_interface_service.h" +#include +#include "[driver_name]_log.h" +#include "devhost_dump_reg.h" +#include "[driver_name]_dump.h" + +#define HDF_LOG_TAG [driver_name]_interface_service + +namespace OHOS { +namespace HDI { +namespace [driver_namespace_name] { +namespace V1_0 { +extern "C" [driver_idl_name] *[driver_inter_name]ImplGetInstance(void) +{ + // עhidumper + DevHostRegisterDumpHost(Get[driver_namespace_name]Dump); + // [hdf-gen] Todo + HDF_LOGI("%{public}s: [driver_idl_name] init", __func__); + return new (std::nothrow) [driver_inter_name]Service(); +} + +int32_t [driver_inter_name]Service::Helloworld(const std::string& sendMsg, std::string& recvMsg) +{ + // [hdf-gen] Todo + HDF_LOGI("%{public}s: [driver_namespace_name]Service::Helloworld print", __func__); + return HDF_SUCCESS; +} + +} // V1_0 +} // [driver_namespace_name] +} // HDI +} // OHOS diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/serviceHTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/serviceHTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..804b9d501b8e3f4254a08c629d85ec805b9c9058 --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/serviceHTemplete.gen @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_HDI_[upper_driver_name]_V1_0_[upper_driver_name]INTERFACESERVICE_H +#define OHOS_HDI_[upper_driver_name]_V1_0_[upper_driver_name]INTERFACESERVICE_H + +#include "v1_0/i[driver_name]_interface.h" + +namespace OHOS { +namespace HDI { +namespace [driver_namespace_name] { +namespace V1_0 { +class [driver_inter_name]Service : public OHOS::HDI::[driver_namespace_name]::V1_0::[driver_idl_name] { +public: + [driver_inter_name]Service() = default; + virtual ~[driver_inter_name]Service() = default; + + int32_t Helloworld(const std::string& sendMsg, std::string& recvMsg) override; + +}; +} // V1_0 +} // [driver_namespace_name] +} // HDI +} // OHOS + +#endif // OHOS_HDI_[upper_driver_name]_V1_0_[upper_driver_name]INTERFACESERVICE_H \ No newline at end of file diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/v4_1/buildgnTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/v4_1/buildgnTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..c3cbb02e784d848b4c4ee6103d135dc73719698a --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/HdiServiceTemplete/v4_1/buildgnTemplete.gen @@ -0,0 +1,98 @@ +# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +HDF_CORE_PATH = "../../../hdf_core" +import("//build/ohos.gni") +import("$HDF_CORE_PATH/adapter/uhdf2/uhdf.gni") + +ohos_shared_library("lib[driver_name]_interface_service_1.0") { + include_dirs = [ + ".", + "../utils/interface", + "../hal/include" + ] + + sources = [ "[driver_name]_interface_service.cpp"] + deps = [ "../hal:hdi_[driver_name]" ] + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "drivers_interface_[driver_name]:[driver_name]_idl_headers", + "hdf_core:libhdf_host", + "hilog:libhilog", + "hitrace:hitrace_meter", + ] + } else { + external_deps = [ "hilog:libhilog" ] + } + external_deps += [ "ipc:ipc_single" ] + + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "drivers_peripheral_[driver_name]" +} + +ohos_shared_library("lib[driver_name]_driver") { + include_dirs = [ + ] + sources = [ "[driver_name]_interface_driver.cpp" ] + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "drivers_interface_[driver_name]:lib[driver_name]_stub_1.0", + "hdf_core:libhdf_host", + "hdf_core:libhdf_ipc_adapter", + "hdf_core:libhdf_utils", + "hdf_core:libhdi", + "hilog:libhilog", + "ipc:ipc_single", + ] + } else { + external_deps = [ + "hilog:libhilog", + "ipc:ipc_single", + ] + } + + shlib_type = "hdi" + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "drivers_peripheral_[driver_name]" +} + +group("hdf_[driver_name]_service") { + deps = [ + ":lib[driver_name]_driver", + ":lib[driver_name]_interface_service_1.0", + ] +} diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/buildgnTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/buildgnTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..c93922c2afc9de410758a32549e16dd9724740fb --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/buildgnTemplete.gen @@ -0,0 +1,18 @@ +# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group("[driver_name]_entry") { + deps = [ "hdi_service:hdf_[driver_name]_service" ] +} + + diff --git a/src/cli/h2hdf/src/templete/PeripheralTemplete/v4_1/bundlejsonTemplete.gen b/src/cli/h2hdf/src/templete/PeripheralTemplete/v4_1/bundlejsonTemplete.gen new file mode 100644 index 0000000000000000000000000000000000000000..a7ddf03bcbd9f0bab42db9b8fac14e7c8f428532 --- /dev/null +++ b/src/cli/h2hdf/src/templete/PeripheralTemplete/v4_1/bundlejsonTemplete.gen @@ -0,0 +1,46 @@ +{ + "name": "@ohos/drivers_peripheral_[driver_name]", + "description": "[driver_name] device driver", + "version": "4.1", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "drivers/peripheral/[driver_name]" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "drivers_peripheral_[driver_name]", + "subsystem": "hdf", + "features": [ + ], + "syscap": [], + "adapted_system_type": ["standard"], + "rom": "675KB", + "ram": "7400KB", + "deps": { + "components": [ + "ipc", + "hdf_core", + "hilog", + "c_utils", + "drivers_interface_[driver_name]", + "hitrace", + "hilog_lite" + ], + "third_party": [ + "bounds_checking_function" + ] + }, + "build": { + "sub_component": [ + "//drivers/peripheral/[driver_name]:[driver_name]_entry" + ], + "test": [ + ], + "inner_kits": [ + + ] + } + } +} diff --git a/src/cli/h2hdf/src/templete/framework.json b/src/cli/h2hdf/src/templete/framework.json new file mode 100644 index 0000000000000000000000000000000000000000..d4979baac7d232f26f4e3a21ac5313b86c75886c --- /dev/null +++ b/src/cli/h2hdf/src/templete/framework.json @@ -0,0 +1,32 @@ +{ + "HcsconfigTemplete":"./templete/HcsconfigTemplete/hcsconfigTemplete.gen", + "IdlInterfaceTemplete":{ + "buildgnTemplete":"./templete/IdlInterfaceTemplete/buildgnTemplete.gen", + "bundlejsonTemplete":{ + "v4_1": "./templete/IdlInterfaceTemplete/v4_1/bundlejsonTemplete.gen" + }, + "idlInterfaceTemplete":"./templete/IdlInterfaceTemplete/idlInterfaceTemplete.gen" + }, + "PeripheralTemplete": { + "DumpExampleTemplete": { + "buildgnTemplete":{ + "v4_1": "./templete/PeripheralTemplete/DumpExampleTemplete/v4_1/buildgnTemplete.gen" + }, + "dumpCTemplete":"./templete/PeripheralTemplete/DumpExampleTemplete/dumpCTemplete.gen", + "dumpHTemplete":"./templete/PeripheralTemplete/DumpExampleTemplete/dumpHTemplete.gen" + }, + "HdiServiceTemplete": { + "buildgnTemplete":{ + "v4_1": "./templete/PeripheralTemplete/HdiServiceTemplete/v4_1/buildgnTemplete.gen" + }, + "driverTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/driverTemplete.gen", + "logHTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/logHTemplete.gen", + "serviceCppTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/serviceCppTemplete.gen", + "serviceHTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/serviceHTemplete.gen" + }, + "buildgnTemplete":"./templete/PeripheralTemplete/buildgnTemplete.gen", + "bundlejsonTemplete":{ + "v4_1": "./templete/PeripheralTemplete/v4_1/bundlejsonTemplete.gen" + } + } +} \ No newline at end of file diff --git a/src/cli/h2sa/docs/usage/h2sa_INSTRUCTION_ZH.md b/src/cli/h2sa/docs/usage/h2sa_INSTRUCTION_ZH.md index c11a89ce3a86037afbff4ff123ace84f00ba372b..47134ff4fe1688a570164e59a4f60f0b9a4857d7 100644 --- a/src/cli/h2sa/docs/usage/h2sa_INSTRUCTION_ZH.md +++ b/src/cli/h2sa/docs/usage/h2sa_INSTRUCTION_ZH.md @@ -13,11 +13,7 @@ h2sa工具,即SERVICE框架生成工具,该工具支持命令行和VS Code 下载python脚本可执行程序header_parser.exe(linux系统为header_parser),下载链接如下: -[下载链接1](http://ftpkaihongdigi.i234.me:5000/sharing/kBG1c7CvT) - -[下载链接2](http://ftp.kaihong.com:5000/sharing/kBG1c7CvT) - -[下载链接3](http://ftp.kaihongdigi.com:5000/sharing/kBG1c7CvT) +// 下载header_parser工具 从发行版下载的链接 获取命令行可执行程序service-gen-win.exe、service-gen-linux,用户可根据以下步骤生成命令行可执行程序: diff --git a/src/tool/api/docs/scan_DEVELOP_ZH.md b/src/tool/api/docs/scan_DEVELOP_ZH.md index 3bd5c64e1c00e3332e9ebd873fdbbae0fba951b7..ccccc1e1154e644b8318943342b394a09784e45e 100644 --- a/src/tool/api/docs/scan_DEVELOP_ZH.md +++ b/src/tool/api/docs/scan_DEVELOP_ZH.md @@ -13,9 +13,11 @@ #### 开发步骤 ##### Linux -1.下载Andr_N_Games_api.xlsx文件,并放置在napi_generator/src/tool/api/src文件夹下,下载链接如下: +1.下载Andr_N_Games_api.xlsx文件,下载链接如下: -//待增加链接 +https://gitee.com/openharmony/napi_generator/releases/download/napigen_resource/napi_resouce.zip + +下载之后解压,进入scan/depend目录下获取Andr_N_Games_api.xlsx文件,并放置在napi_generator/src/tool/api/src文件夹下。 2.安装typescript:在napi_generator/src/tool/api/src目录下执行命令: @@ -54,9 +56,11 @@ ##### Windows -1.下载Andr_N_Games_api.xlsx文件,并放置在napi_generator/src/tool/api/src文件夹下,下载链接如下: +1.下载Andr_N_Games_api.xlsx文件,下载链接如下: + +https://gitee.com/openharmony/napi_generator/releases/download/napigen_resource/napi_resouce.zip -//待增加链接 +下载之后解压,进入scan/depend目录下获取Andr_N_Games_api.xlsx文件,并放置在napi_generator/src/tool/api/src文件夹下。 2.使用管理员身份进入终端: diff --git a/src/tool/api/scan_README_ZH.md b/src/tool/api/scan_README_ZH.md index bd352c7f96d728b5772e02971f85ce4916778d2f..2a76797c0781f566263f7eda9910aa309e1afe3f 100644 --- a/src/tool/api/scan_README_ZH.md +++ b/src/tool/api/scan_README_ZH.md @@ -11,9 +11,11 @@ scan工具可以扫描三方库中包含OpenHarmony源码不包含的接口, ## 使用方法 -1.下载Andr_N_Games_api.xlsx文件,并放置在napi_generator/src/tool/api/src文件夹下,下载链接如下: +1.下载Andr_N_Games_api.xlsx文件,下载链接如下: -// todo +https://gitee.com/openharmony/napi_generator/releases/download/napigen_resource/napi_resouce.zip + +下载之后解压,进入scan/depend目录下获取Andr_N_Games_api.xlsx文件,并放置在napi_generator/src/tool/api/src文件夹下。 2.安装typescript:在napi_generator/src/tool/api/src目录下执行命令: diff --git a/test/h2hdf/hellohdf/HcsConfig/device_info.hcs.gen b/test/h2hdf/hellohdf/HcsConfig/device_info.hcs.gen new file mode 100644 index 0000000000000000000000000000000000000000..6d9453ec7a375fe1fc06e1ecdba099fbb48c533d --- /dev/null +++ b/test/h2hdf/hellohdf/HcsConfig/device_info.hcs.gen @@ -0,0 +1,13 @@ +hello :: host { + hostName = "hello_host"; + priority = 50; + hello_device :: device { + device0 :: deviceNode { + preload = 0; + policy = 2; + priority = 100; + moduleName = "libhello_driver.z.so"; + serviceName = "hello_interface_service"; + } + } +} \ No newline at end of file diff --git a/test/h2hdf/hellohdf/IdlInterface/hello/bundle.json.gen b/test/h2hdf/hellohdf/IdlInterface/hello/bundle.json.gen new file mode 100644 index 0000000000000000000000000000000000000000..ffb2ce0eedf697feec614c8a88fadb47501e3034 --- /dev/null +++ b/test/h2hdf/hellohdf/IdlInterface/hello/bundle.json.gen @@ -0,0 +1,62 @@ +{ + "name": "@ohos/drivers_interface_hello", + "description": "hello device driver interface", + "version": "4.1", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "drivers/interface/hello" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "drivers_interface_hello", + "subsystem": "hdf", + "syscap": [], + "adapted_system_type": ["standard"], + "rom": "675KB", + "ram": "1024KB", + "deps": { + "components": [ + "ipc", + "hdf_core", + "hilog", + "c_utils" + ], + "third_party": [] + }, + "build": { + "sub_component": [ + "//drivers/interface/hello/v1_0:hello_idl_target" + ], + "test": [ + ], + "inner_kits": [ + { + "name": "//drivers/interface/hello/v1_0:libhello_proxy_1.0", + "header": { + "header_files": [ + ], + "header_base": "//drivers/interface/hello" + } + }, + { + "name": "//drivers/interface/hello/v1_0:libhello_stub_1.0", + "header": { + "header_files": [ + ], + "header_base": "//drivers/interface/hello" + } + }, + { + "name": "//drivers/interface/hello/v1_0:hello_idl_headers", + "header": { + "header_files": [ + ], + "header_base": "//drivers/interface/hello" + } + } + ] + } + } + } \ No newline at end of file diff --git a/test/h2hdf/hellohdf/IdlInterface/hello/v1_0/BUILD.gn.gen b/test/h2hdf/hellohdf/IdlInterface/hello/v1_0/BUILD.gn.gen new file mode 100644 index 0000000000000000000000000000000000000000..721cea1ea58583c7c002f1c3cfc44d2731dea593 --- /dev/null +++ b/test/h2hdf/hellohdf/IdlInterface/hello/v1_0/BUILD.gn.gen @@ -0,0 +1,23 @@ +# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//drivers/hdf_core/adapter/uhdf2/hdi.gni") # 编译idl必须要导入的模板 +hdi("hello") { # 目标名称,会生成两个so,分别对应 libhello_client_v1.0.z.so 和 libhello_stub_v1.0.z.so + module_name = "hello_service" # module_name控制dirver文件中驱动描 述符(struct HdfDriverEntry)的moduleName + sources = [ # 参与编译的idl文件 + "IHelloInterface.idl", # 接口idl + ] + language = "cpp" # 控制idl生成c或c++代码 可选择`c`或`cpp` + subsystem_name = "hdf" # 子系统名 + part_name = "drivers_interface_hello" # 组件名 +} diff --git a/test/h2hdf/hellohdf/IdlInterface/hello/v1_0/IHelloInterface.idl.gen b/test/h2hdf/hellohdf/IdlInterface/hello/v1_0/IHelloInterface.idl.gen new file mode 100644 index 0000000000000000000000000000000000000000..01d1979443ff3ee5accf594c868444bac14d58df --- /dev/null +++ b/test/h2hdf/hellohdf/IdlInterface/hello/v1_0/IHelloInterface.idl.gen @@ -0,0 +1,5 @@ +package ohos.hdi.hello.v1_0; + +interface IHelloInterface { + Helloworld([in] String sendMsg, [out] String recvMsg); +} \ No newline at end of file diff --git a/test/h2hdf/hellohdf/Peripheral/hello/BUILD.gn.gen b/test/h2hdf/hellohdf/Peripheral/hello/BUILD.gn.gen new file mode 100644 index 0000000000000000000000000000000000000000..c17c002376c41e036f6e957e9bd4d6a4fefe47ca --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/BUILD.gn.gen @@ -0,0 +1,18 @@ +# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group("hello_entry") { + deps = [ "hdi_service:hdf_hello_service" ] +} + + diff --git a/test/h2hdf/hellohdf/Peripheral/hello/bundle.json.gen b/test/h2hdf/hellohdf/Peripheral/hello/bundle.json.gen new file mode 100644 index 0000000000000000000000000000000000000000..97cf13236ccab2e3062ffcfb96b3972cf023dc37 --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/bundle.json.gen @@ -0,0 +1,46 @@ +{ + "name": "@ohos/drivers_peripheral_hello", + "description": "hello device driver", + "version": "4.1", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "drivers/peripheral/hello" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "drivers_peripheral_hello", + "subsystem": "hdf", + "features": [ + ], + "syscap": [], + "adapted_system_type": ["standard"], + "rom": "675KB", + "ram": "7400KB", + "deps": { + "components": [ + "ipc", + "hdf_core", + "hilog", + "c_utils", + "drivers_interface_hello", + "hitrace", + "hilog_lite" + ], + "third_party": [ + "bounds_checking_function" + ] + }, + "build": { + "sub_component": [ + "//drivers/peripheral/hello:hello_entry" + ], + "test": [ + ], + "inner_kits": [ + + ] + } + } +} diff --git a/test/h2hdf/hellohdf/Peripheral/hello/hal/BUILD.gn.gen b/test/h2hdf/hellohdf/Peripheral/hello/hal/BUILD.gn.gen new file mode 100644 index 0000000000000000000000000000000000000000..10b081ed78bfcde1fa0abc42f388d25c11910aaf --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/hal/BUILD.gn.gen @@ -0,0 +1,51 @@ +#Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +#Licensed under the Apache License, Version 2.0 (the "License"); +#you may not use this file except in compliance with the License. +#You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +#Unless required by applicable law or agreed to in writing, software +#distributed under the License is distributed on an "AS IS" BASIS, +#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +#See the License for the specific language governing permissions and +#limitations under the License. +# + +import("//build/ohos.gni") + +config("libhdi_hello_pub_config") { + visibility = [ ":*" ] +} +ohos_shared_library("hdi_hello") { + public_configs = [ ":libhdi_hello_pub_config" ] + sources = [ + "hello_dump.c", + ] + include_dirs = [ + "include", + "../utils/interface", + "//third_party/bounds_checking_function/include", + ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "drivers_peripheral_hello" + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "hdf_core:libhdf_host", + "hdf_core:libhdf_utils", + "hilog:libhilog", + ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} diff --git a/test/h2hdf/hellohdf/Peripheral/hello/hal/hello_dump.c.gen b/test/h2hdf/hellohdf/Peripheral/hello/hal/hello_dump.c.gen new file mode 100644 index 0000000000000000000000000000000000000000..88aa0580aa879184f57f379e6ffca5b7bd816755 --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/hal/hello_dump.c.gen @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hello_dump.h" +#include +#include +#include "devhost_dump_reg.h" +#include "hdf_base.h" +#include "hello_log.h" + +#define HDF_LOG_TAG uhdf_hello_service + +// -c dump the helloworld info +static const char *g_dumpHelp = + " usage:\n" + " -h, --help: dump help\n" + " -c, --channel: dump the hello channel info\n"; + +static uint32_t ShowHelloworldInfo(struct HdfSBuf *reply) +{ + int32_t ret; + const char *helloWorldMessage = "Hello, World!"; + + ret = HdfSbufWriteString(reply, helloWorldMessage); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: write hello world info failed", __func__); + return HDF_FAILURE; + } + + HDF_LOGI("%{public}s: hellodump: print hello world !", __func__); + + return HDF_SUCCESS; + +} + +static int32_t DumpHelloChannel(struct HdfSBuf *reply) +{ + int32_t ret; + HDF_LOGI("%{public}s: get hello dump channel begin", __func__); + ret = ShowHelloworldInfo(reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: show hello world info failed", __func__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t HelloDriverDump(struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint32_t i; + uint32_t argv = 0; + HDF_LOGI("%{public}s: get hello dump begin xx", __func__); + if (data == NULL || reply == NULL) { + return HDF_FAILURE; + } + + if (!HdfSbufReadUint32(data, &argv)) { + HDF_LOGE("%{public}s: read argv failed", __func__); + return HDF_FAILURE; + } + + if (argv == 0) { + if (!HdfSbufWriteString(reply, g_dumpHelp)) { + HDF_LOGE("%{public}s: write -h failed", __func__); + return HDF_FAILURE; + } + } + + for (i = 0; i < argv; i++) { + const char *value = HdfSbufReadString(data); + if (value == NULL) { + HDF_LOGE("%{public}s value is invalid", __func__); + return HDF_FAILURE; + } + + if (strcmp(value, "-h") == HDF_SUCCESS) { + if (!HdfSbufWriteString(reply, g_dumpHelp)) { + HDF_LOGE("%{public}s: write -h failed", __func__); + return HDF_FAILURE; + } + continue; + } else if (strcmp(value, "-c") == HDF_SUCCESS) { + DumpHelloChannel(reply); + continue; + } + } + + return HDF_SUCCESS; +} + +int32_t GetHelloDump(struct HdfSBuf *data, struct HdfSBuf *reply) +{ + HDF_LOGI("%{public}s: get hello dump begin", __func__); + int32_t ret = HelloDriverDump(data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get hello dump failed", __func__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} diff --git a/test/h2hdf/hellohdf/Peripheral/hello/hal/include/hello_dump.h.gen b/test/h2hdf/hellohdf/Peripheral/hello/hal/include/hello_dump.h.gen new file mode 100644 index 0000000000000000000000000000000000000000..cd422189f28cabf5fcef96ec40b1735ec5222311 --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/hal/include/hello_dump.h.gen @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HELLO_DUMP_H +#define HELLO_DUMP_H + +#include +#include +#include "hdf_sbuf.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +int32_t GetHelloDump(struct HdfSBuf *data, struct HdfSBuf *reply); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* HDI_HELLO_DUMP_H */ \ No newline at end of file diff --git a/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/BUILD.gn.gen b/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/BUILD.gn.gen new file mode 100644 index 0000000000000000000000000000000000000000..c019335a50d918b9099c2347195f30ddf56c0976 --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/BUILD.gn.gen @@ -0,0 +1,98 @@ +# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +HDF_CORE_PATH = "../../../hdf_core" +import("//build/ohos.gni") +import("$HDF_CORE_PATH/adapter/uhdf2/uhdf.gni") + +ohos_shared_library("libhello_interface_service_1.0") { + include_dirs = [ + ".", + "../utils/interface", + "../hal/include" + ] + + sources = [ "hello_interface_service.cpp"] + deps = [ "../hal:hdi_hello" ] + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "drivers_interface_hello:hello_idl_headers", + "hdf_core:libhdf_host", + "hilog:libhilog", + "hitrace:hitrace_meter", + ] + } else { + external_deps = [ "hilog:libhilog" ] + } + external_deps += [ "ipc:ipc_single" ] + + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "drivers_peripheral_hello" +} + +ohos_shared_library("libhello_driver") { + include_dirs = [ + ] + sources = [ "hello_interface_driver.cpp" ] + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "drivers_interface_hello:libhello_stub_1.0", + "hdf_core:libhdf_host", + "hdf_core:libhdf_ipc_adapter", + "hdf_core:libhdf_utils", + "hdf_core:libhdi", + "hilog:libhilog", + "ipc:ipc_single", + ] + } else { + external_deps = [ + "hilog:libhilog", + "ipc:ipc_single", + ] + } + + shlib_type = "hdi" + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "drivers_peripheral_hello" +} + +group("hdf_hello_service") { + deps = [ + ":libhello_driver", + ":libhello_interface_service_1.0", + ] +} diff --git a/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_driver.cpp.gen b/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_driver.cpp.gen new file mode 100644 index 0000000000000000000000000000000000000000..7388758546118b619a5ffb57627854172b9b7af9 --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_driver.cpp.gen @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "v1_0/hello_interface_stub.h" + +#define HDF_LOG_TAG hello_interface_driver + +using namespace OHOS::HDI::Hello::V1_0; + +struct HdfHelloInterfaceHost { + struct IDeviceIoService ioService; + OHOS::sptr stub; +}; + +/* + * 处理客户端请求的Dispatch方法: 处理来自客户端的IO请求 + * client:指向HdfDeviceIoClient结构体的指针,表示发起请求的客户端。 + * cmdId:命令ID,标识了要执行的命令或操作。 + * data:指向HdfSBuf结构体的指针,包含了请求的数据。 + * reply:指向另一个HdfSBuf结构体的指针,用于存放响应的数据。 + */ +static int32_t HelloInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, + struct HdfSBuf *reply) +{ + auto *hdfHelloInterfaceHost = CONTAINER_OF(client->device->service, struct HdfHelloInterfaceHost, ioService); + + // 声明两个MessageParcel对象,用于序列化和反序列化IPC通信中的数据 + OHOS::MessageParcel *dataParcel = nullptr; + OHOS::MessageParcel *replyParcel = nullptr; + // 创建一个MessageOption对象,用于设置IPC通信的选项。 + OHOS::MessageOption option; + + // 响应序列化:将HdfSBuf中的数据转换为MessageParcel对象。如果转换失败,记录错误并返回错误代码。 + if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__); + return HDF_ERR_INVALID_PARAM; + } + + // 数据序列化:尝试将响应数据的HdfSBuf转换为MessageParcel对象。如果失败,也记录错误并返回错误代码。 + if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__); + return HDF_ERR_INVALID_PARAM; + } + + // 调用stub对象的SendRequest方法,发送请求。这个方法执行实际的IPC调用,将cmdId和序列化后的请求数据dataParcel发送给服务端,并将响应数据反序列化到replyParcel中。 + return hdfHelloInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option); +} + +// 驱动自身业务初始化的接口 +static int HdfHelloInterfaceDriverInit(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGI("%{public}s: driver init start", __func__); + return HDF_SUCCESS; +} + +// 将驱动对外提供的服务能力接口绑定到HDF框架 +static int HdfHelloInterfaceDriverBind(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGI("%{public}s: driver bind start", __func__); + // 创建对象:该对象是驱动服务的具体实现 + auto *hdfHelloInterfaceHost = new (std::nothrow) HdfHelloInterfaceHost; + if (hdfHelloInterfaceHost == nullptr) { + HDF_LOGE("%{public}s: failed to create create HdfHelloInterfaceHost object", __func__); + return HDF_FAILURE; + } + + // 为ioService结构体设置回调函数:设置的Dispatch函数用于处理IO请求 + hdfHelloInterfaceHost->ioService.Dispatch = HelloInterfaceDriverDispatch; + hdfHelloInterfaceHost->ioService.Open = NULL; + hdfHelloInterfaceHost->ioService.Release = NULL; + + auto serviceImpl = OHOS::HDI::Hello::V1_0::IHelloInterface::Get(true); + if (serviceImpl == nullptr) { + HDF_LOGE("%{public}s: failed to get of implement service", __func__); + delete hdfHelloInterfaceHost; + return HDF_FAILURE; + } + + // 使用ObjectCollector的GetOrNewObject方法获取或创建一个Stub对象。Stub对象是服务接口的客户端代理,用于发起远程调用。 + hdfHelloInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, + OHOS::HDI::Hello::V1_0::IHelloInterface::GetDescriptor()); + if (hdfHelloInterfaceHost->stub == nullptr) { + HDF_LOGE("%{public}s: failed to get stub object", __func__); + delete hdfHelloInterfaceHost; + return HDF_FAILURE; + } + + // 将ioService绑定到deviceObject,这样HDF框架就可以通过deviceObject来访问服务 + deviceObject->service = &hdfHelloInterfaceHost->ioService; + HDF_LOGI("%{public}s: driver bind end", __func__); + return HDF_SUCCESS; +} + +// 驱动释放资源的接口 +static void HdfHelloInterfaceDriverRelease(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGI("%{public}s: driver release start", __func__); + if (deviceObject->service == nullptr) { + return; + } + + auto *hdfHelloInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfHelloInterfaceHost, ioService); + if (hdfHelloInterfaceHost != nullptr) { + delete hdfHelloInterfaceHost; + } +} + +/* + * 定义驱动入口的对象,必须为HdfDriverEntry(在hdf_device_desc.h中定义)类型的全局变量。 + */ +struct HdfDriverEntry g_hellointerfaceDriverEntry = { + .moduleVersion = 1, + .moduleName = "hello_service", + .Bind = HdfHelloInterfaceDriverBind, + .Init = HdfHelloInterfaceDriverInit, + .Release = HdfHelloInterfaceDriverRelease, +}; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * 调用HDF_INIT将驱动入口注册到HDF框架中。 + * 在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动;当Init调用异常时,HDF框架会调用Release释放驱动资源并退出。 + */ +HDF_INIT(g_hellointerfaceDriverEntry); +#ifdef __cplusplus +} +#endif /* __cplusplus */ diff --git a/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_service.cpp.gen b/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_service.cpp.gen new file mode 100644 index 0000000000000000000000000000000000000000..0dc14b3856d54fb07b282bc93a01adf6a0594e2c --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_service.cpp.gen @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "v1_0/hello_interface_service.h" +#include +#include "hello_log.h" +#include "devhost_dump_reg.h" +#include "hello_dump.h" + +#define HDF_LOG_TAG hello_interface_service + +namespace OHOS { +namespace HDI { +namespace Hello { +namespace V1_0 { +extern "C" IHelloInterface *HelloInterfaceImplGetInstance(void) +{ + // עᨩdumper + DevHostRegisterDumpHost(GetHelloDump); + // [hdf-gen] Todo + HDF_LOGI("%{public}s: IHelloInterface init", __func__); + return new (std::nothrow) HelloInterfaceService(); +} + +int32_t HelloInterfaceService::Helloworld(const std::string& sendMsg, std::string& recvMsg) +{ + // [hdf-gen] Todo + HDF_LOGI("%{public}s: HelloService::Helloworld print", __func__); + return HDF_SUCCESS; +} + +} // V1_0 +} // Hello +} // HDI +} // OHOS diff --git a/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_service.h.gen b/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_service.h.gen new file mode 100644 index 0000000000000000000000000000000000000000..3872b699ffd391ce2a97dd74126b5334f152c9a1 --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/hdi_service/hello_interface_service.h.gen @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_HDI_HELLO_V1_0_HELLOINTERFACESERVICE_H +#define OHOS_HDI_HELLO_V1_0_HELLOINTERFACESERVICE_H + +#include "v1_0/ihello_interface.h" + +namespace OHOS { +namespace HDI { +namespace Hello { +namespace V1_0 { +class HelloInterfaceService : public OHOS::HDI::Hello::V1_0::IHelloInterface { +public: + HelloInterfaceService() = default; + virtual ~HelloInterfaceService() = default; + + int32_t Helloworld(const std::string& sendMsg, std::string& recvMsg) override; + +}; +} // V1_0 +} // Hello +} // HDI +} // OHOS + +#endif // OHOS_HDI_HELLO_V1_0_HELLOINTERFACESERVICE_H \ No newline at end of file diff --git a/test/h2hdf/hellohdf/Peripheral/hello/utils/interface/hello_log.h.gen b/test/h2hdf/hellohdf/Peripheral/hello/utils/interface/hello_log.h.gen new file mode 100644 index 0000000000000000000000000000000000000000..9f6d5c6751275f8235ad9eb734d86d31df5415d5 --- /dev/null +++ b/test/h2hdf/hellohdf/Peripheral/hello/utils/interface/hello_log.h.gen @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HELLO_UHDF_LOG_H +#define HELLO_UHDF_LOG_H + +#include "hdf_log.h" + +#ifdef LOG_DOMAIN +#undef LOG_DOMAIN +#endif +#define LOG_DOMAIN 0xD002516 + +#endif //HELLO_UHDF_LOG_H \ No newline at end of file