1# Usage Guide
2
3## 简介
4在OpenHarmony系统中,上层应用或服务层通过调用HDF框架提供的HDI接口,能够以一种标准化和抽象化的方式与底层硬件设备进行交互。使用h2hdf工具,用户只需提供一个drivername,工具会自动生成整个框架的代码,包含驱动配置文件、idl接口、驱动程序driver和驱动服务框架。
5
6## 约束
7系统:建议Ubuntu 20.04或者Windows 10
8
9依赖版本:VS Code 1.62.0
10
11## 使用方法
12
13### 生成
14
151.安装typescript:在napi_generator/src/cli/h2hdf/src目录下执行命令:
16
17	npm i typescript
18
192.安装stdio:在napi_generator/src/cli/h2hdf目录下执行命令: 
20
21	npm i stdio
22
233.在napi_generator/src/cli/h2hdf/src下执行以下命令:
24
25```
26node main.js -n hello
27```
28
29其中,参数详情如下:
30
31  -n, drivername,例如:hello
32
33  -v, 可选参数,版本,默认为4.1
34
35  -o, 可选参数,默认为当前目录,指定生成框架代码输出路径。
36
376.执行成功后在napi_generator/src/cli/h2hdf/src/下生成hellohdf文件夹,文件夹中目录结构如下所示:
38
39```
40hellohdf
41├── HcsConfig                                      # hcs配置文件
42│   ├── device_info.hcs                            # 内容配置到源码vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs文件中
43├── IdlInterface                                                             
44│   ├── hello                                      # 拷贝到源码drivers/interface          
45│   │   ├── v1_0              
46│   │   │   ├── BUILD.gn                           
47│   │   │   ├── IHelloInterface.idl                # idl接口               
48│   │   ├── bundle.json
49├── Peripheral                                     # 拷贝到源码drivers/peripheral
50│   ├── hello                                             
51│   │   ├── hal                                           
52│   │   │   ├── include
53│   │   │   │   ├── hello_dump.h                          
54│   │   │   ├── BUILD.gn
55│   │   │   ├── hello_dump.c                       # hidump实现              
56│   │   ├── hdi_service                            # hdi_service
57│   │   │   ├── BUILD.gn                           # 编译两个动态库:libhello_driver、libhello_interface_service_1.0
58│   │   │   ├── hello_interface_driver.cpp         # driver:定义驱动入口的对象,将驱动入口注册到HDF框架中;在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动;当Init调用异常时,HDF框架会调用Release释放驱动资源并退出
59│   │   │   ├── hello_interface_service.cpp        # 驱动服务
60│   │   │   ├── hello_interface_service.h
61│   │   ├── utils/interface
62│   │   │   ├── hello_log.h                        # 日志文件
63│   │   ├── BUILD.gn                               
64│   │   ├── bundle.json
65```
66
67### 编译
68
691. **拷目录:** 将hellohdf/Peripheral文件夹下的hello文件夹拷贝到源码drivers/peripheral目录下
70
71```
72cp hellohdf/Peripheral/hello 源码/drivers/peripheral -r
73```
74
752. **拷目录:** 将hellohdf/IdlInterface文件夹下的hello文件夹拷贝到源码drivers/interface目录下
76
77```
78cp hellohdf/IdlInterface/hello 源码/drivers/interface -r
79```
80
813. **改文件:** 将hellohdf/HcsConfig/device_info.hcs中的内容拷贝到源码vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs文件中,如下所示:
82
83```
84 root {
85    device_info {
86       ...
87       // 增加以下内容
88       hello :: host {
89            hostName = "hello_host";
90            priority = 50;
91            hello_device :: device {
92                device0 :: deviceNode {
93                    preload = 0;
94                    policy = 2;
95                    priority = 100;
96                    moduleName = "libhello_driver.z.so";
97                    serviceName = "hello_interface_service";
98                }
99            }
100        }
101        // 增加上述内容
102        ...
103     }
104 }
105```
106
1074. **配置产品:** 以rk3568为例,在源码vendor/hihope/rk3568/config.json文件中hdf子系统的components中增加以下内容:
108
109```
110{
111  "component": "drivers_interface_hello",
112  "features": []
113},
114{
115  "component": "drivers_peripheral_hello",
116  "features": []
117}
118```
119
120"drivers_interface_hello"为生成的drivers/interface/hello/v1_0/BUILD.gn中的part_name,其中"drivers_interface_"为固定格式。drivers_peripheral_hello"为生成的drivers/peripheral/hello/bundle.json中的component,"drivers_peripheral_"为固定格式。
121
1225. **编译:** 在源码下执行以下命令进行编译:
123
124```
125./build.sh --product-name rk3568
126```
127
128编译成功后,镜像位置在out/rk3568/packages/phone/image目录下
129
130### 验证
131
132#### 动态加载
133
1341.查看hostId:镜像烧录后,在vendor/etc/init/hdf_devhost.cfg文件里查看hostId
135
136```
137cat vendor/etc/init/hdf_devhost.cfg
138```
139
140![image-20240724093743837](./figures/pic_show_hostid.png)
141
1422.加载hello_host,命令如下:
143
144```
145./vendor/bin/hdf_devhost 14 hello_host
146```
147
148![image-20240903114845035](./figures/pic_show_exe.png)
149
150注意 :不可将进程kill
151
1523.查看host是否加载:新开一个命令行窗口,hdc进入开发板,执行以下命令查看进程是否拉起:
153
154```
155ps -A | grep host
156```
157
158屏幕显示hello_host进程号,则表明host已被拉起
159
160![image-20240724093743837](./figures/pic_show_devhostPid.png)
161
1624.使用hidumper查看更多细节信息:
163
164通过DeviceServiceManager查询用户空间的设备信息
165
166```
167 hidumper -s HdfDeviceServiceManager -a "-query"
168```
169
170![image-20240724093543096](./figures/pic_show_host.png)
171
172通过DeviceServiceManager查询hello_host提供什么功能
173
174```
175hidumper -s HdfDeviceServiceManager -a "-host hello_host -h"
176```
177
178-h:打印dump help信息
179
180![image-20240724093543096](./figures/pic_show_dumph.png)
181
182通过DeviceServiceManager查询hello_host提供的helloworld功能
183
184```
185hidumper -s HdfDeviceServiceManager -a "-host hello_host -c"
186```
187
188-c:打印出Hello, World!
189
190![image-20240724093535915](./figures/pic_show_dump.png)
191
192其中,-h、-c定义在生成的hellohdf/Peripheral/hello/hal/hello_dump.c中:
193
194![image-20240724093543096](./figures/pic_show_dumpc.png)
195
196#### 静态加载
197
198// todo 待补充
199
200