• Home
Name Date Size #Lines LOC

..--

activity_recognition/07-Sep-2024-434406

audio/07-Sep-2024-6,1385,633

battery/07-Sep-2024-2,2292,053

bluetooth/07-Sep-2024-1,4861,360

camera/07-Sep-2024-9,4316,870

codec/07-Sep-2024-4,6654,314

connected_nfc_tag/07-Sep-2024-385349

display/07-Sep-2024-11,1208,174

distributed_audio/07-Sep-2024-1,3381,241

distributed_camera/07-Sep-2024-640603

drm/07-Sep-2024-579506

face_auth/07-Sep-2024-1,6431,551

figures/07-Sep-2024-

fingerprint_auth/07-Sep-2024-2,0861,962

huks/07-Sep-2024-650616

input/07-Sep-2024-1,3291,254

intelligent_voice/07-Sep-2024-1,1241,004

light/07-Sep-2024-364336

location/07-Sep-2024-5,3954,889

memorytracker/07-Sep-2024-179167

motion/07-Sep-2024-590552

nfc/07-Sep-2024-632569

nnrt/07-Sep-2024-10,1219,698

partitionslot/07-Sep-2024-169157

pin_auth/07-Sep-2024-1,6551,559

power/07-Sep-2024-1,6151,490

ril/07-Sep-2024-14,79313,098

secure_element/07-Sep-2024-539493

sensor/07-Sep-2024-1,3051,210

thermal/07-Sep-2024-732668

usb/07-Sep-2024-1,4981,367

user_auth/07-Sep-2024-3,0022,851

vibrator/07-Sep-2024-935879

wlan/07-Sep-2024-6,0235,559

LICENSED07-Sep-20249.9 KiB179150

OAT.xmlD07-Sep-20246.5 KiB10246

README.mdD07-Sep-20249 KiB217163

README_ZH.mdD07-Sep-20248.1 KiB206155

interface.gniD07-Sep-20241.9 KiB4644

README.md

1# Peripheral Driver HDI Definition
2
3## Introduction
4
5This repository is used to manage the hardware device interface (HDI) definition of each module. The HDIs are defined in the interface definition language (IDL) and saved in the `.idl` format.
6
7
8**Figure 1** HDI definition process
9
10![](figures/hdi-schematic.png)
11
12Define an HDI in the IDL and saved it in the `.idl` format. Then, the `.idl` file will be compiled and converted into the function interface declaration and IPC-related process code in C or C++. You only need to implement service functionalities based on the `ifoo.h` generated. The `//drivers/hdf_core/adapter/uhdf2/hdi.gni` template integrates code generation and compilation. You can write the `BUILD.gn` file based on this template to generate client and server code and compile the code into a shared library (.so file).
13
14## Directory Structure
15
16```
17├── README.en.md
18├── README.md
19├── sensor                          # Sensor HDI definition.
20│   └── v1_0                        # Sensor HDI v1.0 definition.
21│       ├── BUILD.gn                # Sensor idl build script.
22│       ├── ISensorCallback.idl     # Sensor callback interface definition.
23│       ├── ISensorInterface.idl    # Sensor interface definition.
24│       └── SensorTypes.idl         # Sensor data type definition.
25├── audio                           # Audio HDI definition.
26│   └── ...
27├── camera                          # Camera HDI definition.
28├── codec                           # Codec HDI definition.
29├── display                         # Display HDI definition.
30├── format                          # Format HDI definition.
31├── input                           # Input HDI definition.
32├── misc                            # Miscellaneous HDI definition.
33├── usb                             # USB HDI definition.
34└── wlan                            # WLAN HDI definition.
35```
36
37## How to Use
38
391. Create an `.idl` file in the IDL.
40
41    - Create the module/version interface directory by referring to the directory structure. The initial version is defined as `v1_0`, for example, `drivers/interface/foo/v1.0/`.
42
43    - Define the interface `IFoo.idl`.
44        ```
45        package ohos.hdi.foo.v1_0;
46
47        import ohos.hdi.foo.v1_0.IFooCallback;
48        import ohos.hdi.foo.v1_0.MyTypes;
49
50        interface IFoo {
51            Ping([in] String sendMsg, [out] String recvMsg);
52
53            GetData([out] struct FooInfo info);
54
55            SendCallbackObj([in] IFooCallback cbObj);
56        }
57        ```
58    - If customized data types are used in `interface`, define the data types in `MyTypes.idl`.
59        ```
60        package ohos.hdi.foo.v1_0;
61
62        enum FooType {
63            FOO_TYPE_ONE = 1,
64            FOO_TYPE_TWO,
65        };
66
67        struct FooInfo {
68            unsigned int id;
69            String name;
70            enum FooType type;
71        };
72        ```
73    - If a callback from the server is required, define the callback class `IFooCallback.idl`.
74        ```
75        package ohos.hdi.foo.v1_0;
76
77        [callback] interface IFooCallback {
78            PushData([in] String message);
79        }
80        ```
81
822. Write `BUILD.gn` for the `idl` file.
83    - Add the `BUILD.gn` file to the `drivers/interface/foo/v1.0/` directory. The file content is as follows:
84        ```
85        import("//drivers/hdf_core/adapter/uhdf2/hdi.gni")   # Template to be imported for compiling the .idl file.
86        hdi("foo") {                                # Target .so files (libfoo_client_v1.0.z.so and libfoo_stub_v1.0.z.so) to be generated.
87            package = "ohos.hdi.foo.v1_0"                    # Package name, which must match the .idl path.
88            module_name = "foo"                     # moduleName that determines the driver descriptor (struct HdfDriverEntry) in the driver file.
89            sources = [                             # .idl files to compile.
90                "IFoo.idl",                         # Interface .idl file.
91                "IFooCallback.idl",                 # .idl file for callbacks.
92                "MyTypes.idl",                      # .idl file for customized data types.
93            ]
94            language = "cpp"                        # Generate C or C++ code from the .idl files. You can select `c` or `cpp`.
95        }
96        ```
97
983. Implement the HDI service.
99
100    After the .idl files are compiled, intermediate code is generated in the `out/[product_name]/gen/drivers/interfaces/foo/v1_0` directory.
101
102    - Implement the HDI service APIs.
103
104        Implement the service interface based on the `foo_interface_service.h` file that is automatically generated, and compile the related source code to `FooService.z.so`.
105
106        Implement the service interface.
107        ```
108        namespace OHOS {
109        namespace HDI {
110        namespace Foo {
111        namespace V1_0 {
112
113        class FooService: public IFoo { // Inherit from the interface class and implement the interface.
114        public:
115            virtual ~FooService() {}
116
117            int32_t Ping(const std::string& sendMsg, std::string& recvMsg) override;
118            int32_t FooService::GetData(FooInfo& info) override;
119            int32_t FooService::SendCallbackObj(const sptr<IFooCallback>& cbObj) override;
120        };
121
122        } // namespace V1_0
123        } // namespace Foo
124        } // namespace Hdi
125        } // namespace OHOS
126        ```
127        Implement the service instantiation interface.
128        ```
129        #ifdef __cplusplus
130        extern "C" {
131        #endif /* __cplusplus */
132
133        Hdi::Foo::V1_0::IFooInterface *FooInterfaceServiceConstruct();
134
135        void FooInterfaceServiceRelease(Hdi::Foo::V1_0::IFooInterface *obj);
136
137        #ifdef __cplusplus
138        }
139        #endif /* __cplusplus */
140        ```
141
142    - Implement the driver entry.
143
144        The HDI services are published based on the user-mode Hardware Driver Foundation (HDF). Therefore, a driver entry needs to be implemented. The reference driver implementation code is generated in the **out** directory, for example, `out/gen/xxx/foo_interface_driver.cpp`. You can use this file or modify the file based on service requirements.
145        Then, compile the driver entry source code as `libfoo_driver.z.so`. (The .so file name must match that in the HDF configuration source.)
146
1474. Publish the HDI service.
148
149    Declare the HDI service in the HDF configuration source (HCS). The following uses the Hi3516D V300 board as an example. The HCS path is `vendor/hisilicon/Hi3516DV300/hdf_config/uhdf/device_info.hcs`. Add the following configuration:
150    ```
151    fooHost :: host {
152            hostName = "fooHost";
153            priority = 50;
154            fooDevice :: device {
155                device0 :: deviceNode {
156                    policy = 2;
157                    priority = 100;
158                    preload = 2;
159                    moduleName = "libfoo_driver.z.so";
160                    serviceName = "foo_service";
161                }
162            }
163        }
164    ```
165
1665. Invoke the HDI service.
167
168    - Add the following dependency to **BUILD.gn** on the client:
169    `//drivers/interface/foo/v1.0:libfoo_proxy_1.0"`
170
171    - Invoke the HDI interface in the code (CPP is used as an example.)
172        ```
173        #include <v1_0/ifoo_interface.h>
174
175        int WorkFunc(void) {
176            sptr<IFoo> foo = OHOS::HDI::Foo::V1_0::Foo::Get(); // Use the built-in static method of the Foo object to obtain the client instance of the service.
177            if (foo == nullptr) {
178                // If the HDI service does not exist, handle the error.
179            }
180
181            foo->Bar(); // Do interface call.
182        }
183        ```
184        If a service has multiple instances, you can use the `Hdi::Foo::V1_0::Foo::GetInstance(const std::string& serviceName)` method to obtain the instance.
185
186## Conventions
187
1881. Rules for naming .idl files
189
190    - Name an .idl file in UpperCamelCase style, which is the same as the interface name. Generally, the file name starts with the letter I.
191    - The interface description file name extension is .idl.
192
1931. Interface naming rules
194
195    | Type| Style|
196    | -----  | ------- |
197    | Class, struct, enum, union, and package names | UpperCamelCase|
198    | Methods| UpperCamelCase|
199    | Function parameters and member variables in a class, struct, or union| lowerCamelCase|
200    | Macros, constants (const), and enumerated values| All uppercase letters, separated by underscores (_)|
201
2021. Interface version naming rules
203
204    The HDI interface version is defined in [major].[minor] format.
205    - Different major versions indicate that the interfaces are incompatible.
206    - The same major version and different minor versions indicate that the interfaces are compatible. However, the interface name, parameter type/quantity, and return value type/quantity in an earlier minor version cannot be changed.
207
208## Repositories Involved
209
210[Drive Subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/driver.md)
211
212
213[HDF adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README.md)
214
215
216[Peripheral](https://gitee.com/openharmony/drivers_peripheral/blob/master/README.md)
217

README_ZH.md

1# 外设驱动HDI接口定义
2
3## 简介
4
5该仓库用于管理各模块HDI(Hardware Device Interface)接口定义,接口定义使用IDL语言描述并以`·idl`文件形式保存。
6
7
8**图 1**  HDI原理图
9
10![](figures/hdi-schematic.png)
11
12使用IDL语法描述HDI接口并保存为`.idl`文件,`.idl`文件在编译过程中转换为C/C++语言的函数接口声明、客户端与服务端IPC相关过程代码,开发者只需要基于生成的`ifoo.h`函数接口实现具体服务功能即可。代码生成与编译功能已经集成在`//drivers/hdf_core/adapter/uhdf2/hdi.gni`编译模板,基于该编译模板编写`idl`文件的`BUILD.gn`就可以简单的生成客户端、服务端代码并编译为共享库。
13
14## 目录
15
16```
17├── README.en.md
18├── README.md
19├── sensor                          #sensor HDI 接口定义
20│   └── v1_0                        #sensor HDI 接口 v1.0版本定义
21│       ├── BUILD.gn                #sensor idl文件编译脚本
22│       ├── ISensorCallback.idl     #sensor callback 接口定义idl文件
23│       ├── ISensorInterface.idl    #sensor interface 接口定义idl文件
24│       └── SensorTypes.idl         #sensor 数据类型定义idl文件
25├── audio                           #audio HDI 接口定义
26│   └── ...
27├── camera                          #camera HDI接口定义
28├── codec                           #codec HDI接口定义
29├── display                         #display HDI接口定义
30├── face_auth                       #faceauth HDI接口定义
31├── format                          #format HDI接口定义
32├── input                           #input HDI接口定义
33├── misc                            #misc HDI接口定义
34├── pinauth                         #pinauth HDI接口定义
35├── usb                             #usb HDI接口定义
36├── fingerprint_auth                #fingerprintauth HDI接口定义
37└── wlan                            #wlan HDI接口定义
38```
39
40## 使用说明
41
421. 使用IDL语法编写 `.idl` 文件
43
44    - 参考上节目录结构创建对应模块/版本接口目录,初始版本定义为`v1_0`,如 `drivers/interface/foo/v1.0/`
45
46    - 定义接口 `IFoo.idl`
47        ```
48        package ohos.hdi.foo.v1_0;
49
50        import ohos.hdi.foo.v1_0.IFooCallback;
51        import ohos.hdi.foo.v1_0.MyTypes;
52
53        interface IFoo {
54            Ping([in] String sendMsg, [out] String recvMsg);
55
56            GetData([out] struct FooInfo info);
57
58            SendCallbackObj([in] IFooCallback cbObj);
59        }
60        ```
61    - 如果`interface`中用到了自定义数据类型,将自定义类型定义到`MyTypes.idl`
62        ```
63        package ohos.hdi.foo.v1_0;
64
65        enum FooType {
66            FOO_TYPE_ONE = 1,
67            FOO_TYPE_TWO,
68        };
69
70        struct FooInfo {
71            unsigned int id;
72            String name;
73            enum FooType type;
74        };
75        ```
76    - 如果需要从服务端回调,可以定义`callback`接口类`IFooCallback.idl`
77        ```
78        package ohos.hdi.foo.v1_0;
79
80        [callback] interface IFooCallback {
81            PushData([in] String message);
82        }
83        ```
84
851. 编写 `idl`文件的`BUILD.gn`
86    - 在上述`drivers/interface/foo/v1.0/`目录中添加`BUILD.gn`文件,内容参考如下:
87        ```
88        import("//drivers/hdf_core/adapter/uhdf2/hdi.gni")   # 编译idl必须要导入的模板
89        hdi("foo") {                                # 目标名称,会生成两个so,分别对应 libfoo_client_v1.0.z.solibfoo_stub_v1.0.z.so
90            package = "ohos.hdi.foo.v1_0"           # 包名,必须与idl路径匹配
91            module_name = "foo"                     # module_name控制dirver文件中驱动描 述符(struct HdfDriverEntry)的moduleName
92            sources = [                             # 参与编译的idl文件
93                "IFoo.idl",                         # 接口idl
94                "IFooCallback.idl",                 # 用于回调的idl
95                "MyTypes.idl",                      # 自定义类型idl
96            ]
97            language = "cpp"                        # 控制idl生成c或c++代码 可选择`c`或`cpp`
98        }
99        ```
100
1012. 实现 HDI 服务
102
103    在上述步骤中idl编译后将在out目录`out/[product_name]/gen/drivers/interfaces/foo/v1_0`生成中间代码。
104
105    - 实现HDI服务接口
106
107        基于工具自动生成的`foo_interface_service.h`,实现其中的服务接口,并将相关源码编译为FooService.z.so108
109        实现服务业务接口:
110        ```
111        namespace OHOS {
112        namespace HDI {
113        namespace Foo {
114        namespace V1_0 {
115
116        class FooService : public IFoo {  //继承接口类,并重写接口
117        public:
118            virtual ~FooService() {}
119
120            int32_t Ping(const std::string& sendMsg, std::string& recvMsg) override;
121            int32_t FooService::GetData(FooInfo& info) override;
122            int32_t FooService::SendCallbackObj(const sptr<IFooCallback>& cbObj) override;
123        };
124
125        } // namespace V1_0
126        } // namespace Foo
127        } // namespace Hdi
128        } // namespace OHOS
129        ```
130
131    - 实现驱动入口
132
133        HDI服务发布是基于用户态HDF驱动框架,所以需要实现一个驱动入口。驱动实现代码参考已经在out目录中生成,如`out/gen/xxx/foo_interface_driver.cpp`,可以根据业务需要直接使用该文件或参考该文件按业务需要重新实现。
134        然后将驱动入口源码编译为`libfoo_driver.z.so`(该名称无强制规定,与hcs配置中配套即可)。
135
1363. 发布服务
137
138    在产品hcs配置中声明HDI服务,以标准系统Hi3516DV300单板为例,HDF设备配置路径为`vendor/hisilicon/Hi3516DV300/hdf_config/uhdf/device_info.hcs`,在其中新增以下配置:
139    ```
140    fooHost :: host {
141            hostName = "fooHost";
142            priority = 50;
143            fooDevice :: device {
144                device0 :: deviceNode {
145                    policy = 2;
146                    priority = 100;
147                    preload = 2;
148                    moduleName = "libfoo_driver.z.so";
149                    serviceName = "foo_service";
150                }
151            }
152        }
153    ```
154
1554. 调用HDI服务
156
157    - 客户端在BUILD.gn中增加依赖:
158    `//drivers/interface/foo/v1.0:libfoo_proxy_1.0"`
159
160    - 在代码中调用HDI接口(以CPP为例)
161        ```
162        #include <v1_0/ifoo_interface.h>
163
164        int WorkFunc(void) {
165            sptr<IFoo> foo = OHOS::HDI::Foo::V1_0::Foo::Get(); // 使用Foo对象的内置静态方法获取该服务客户端实例
166            if (foo == nullptr) {
167                // hdi service not exist, handle error
168            }
169
170            foo->Bar(); // do interface call
171        }
172        ```
173        如果服务存在多实例可以通过指定实例名称的方法获取对应实例`Hdi::Foo::V1_0::Foo::GetInstance(const std::string& serviceName)`;
174
175## 约定
176
1771. idl 文件命名规则
178
179    - idl 文件以大驼峰命名,与接口名称保持一致,一般以字母‘I’开头。
180    - 接口描述文件以`.idl`作为后缀。
181
1821. 接口命名规则
183
184    | 类型   | 命名风格  |
185    | -----  | ------- |
186    | 类、结构体、枚举、联合体等类型名,包名 | 大驼峰 |
187    | 方法 | 大驼峰 |
188    | 函数参数,类、结构体和联合体中的成员变量 | 小驼峰 |
189    | 宏,常量(const),枚举值 | 全大写,下划线分割 |
190
1911. 接口版本号命名规则
192
193    HDI接口版本号使用语义化版本号定义,即[major].[minor]。
194    - major版本号不同表示接口间不兼容;
195    - minor版本不同但是major版本号相同表示接口相互兼容,这种情况下不允许修改以前接口的接口名称、参数类型/个数、返回值类型/个数。
196
197## 相关仓
198
199[驱动子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E9%A9%B1%E5%8A%A8%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
200
201
202[drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README_zh.md)
203
204
205[drivers\_peripheral](https://gitee.com/openharmony/drivers_peripheral/blob/master/README_zh.md)
206