Name |
Date |
Size |
#Lines |
LOC |
||
---|---|---|---|---|---|---|
.. | - | - | ||||
battery/v1_0/ | 12-May-2024 | - | 161 | 143 | ||
figures/ | 12-May-2024 | - | ||||
light/v1_0/ | 12-May-2024 | - | 218 | 198 | ||
power/v1_0/ | 12-May-2024 | - | 124 | 109 | ||
sensor/v1_0/ | 12-May-2024 | - | 367 | 337 | ||
thermal/v1_0/ | 12-May-2024 | - | 110 | 97 | ||
vibrator/v1_0/ | 12-May-2024 | - | 147 | 134 | ||
LICENSE | D | 12-May-2024 | 9.9 KiB | 179 | 150 | |
OAT.xml | D | 12-May-2024 | 6.5 KiB | 102 | 46 | |
README.md | D | 12-May-2024 | 9 KiB | 217 | 163 | |
README_ZH.md | D | 12-May-2024 | 7.9 KiB | 203 | 152 |
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 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/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/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-subsystem.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 11 12使用IDL语法描述HDI接口并保存为`.idl`文件,`.idl`文件在编译过程中转换为C/C++语言的函数接口声明、客户端与服务端IPC相关过程代码,开发者只需要基于生成的`ifoo.h`函数接口实现具体服务功能即可。代码生成与编译功能已经集成在`//drivers/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├── format #format HDI接口定义 31├── input #input HDI接口定义 32├── misc #misc HDI接口定义 33├── usb #usb HDI接口定义 34└── wlan #wlan HDI接口定义 35``` 36 37## 使用说明 38 391. 使用IDL语法编写 `.idl` 文件 40 41 - 参考上节目录结构创建对应模块/版本接口目录,初始版本定义为`v1_0`,如 `drivers/interface/foo/v1.0/` 42 43 - 定义接口 `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 - 如果`interface`中用到了自定义数据类型,将自定义类型定义到`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 - 如果需要从服务端回调,可以定义`callback`接口类`IFooCallback.idl` 74 ``` 75 package ohos.hdi.foo.v1_0; 76 77 [callback] interface IFooCallback { 78 PushData([in] String message); 79 } 80 ``` 81 821. 编写 `idl`文件的`BUILD.gn` 83 - 在上述`drivers/interface/foo/v1.0/`目录中添加`BUILD.gn`文件,内容参考如下: 84 ``` 85 import("//drivers/adapter/uhdf2/hdi.gni") # 编译idl必须要导入的模板 86 hdi("foo") { # 目标名称,会生成两个so,分别对应 libfoo_client_v1.0.z.so 和 libfoo_stub_v1.0.z.so 87 package = "ohos.hdi.foo.v1_0" # 包名,必须与idl路径匹配 88 module_name = "foo" # module_name控制dirver文件中驱动描 述符(struct HdfDriverEntry)的moduleName 89 sources = [ # 参与编译的idl文件 90 "IFoo.idl", # 接口idl 91 "IFooCallback.idl", # 用于回调的idl 92 "MyTypes.idl", # 自定义类型idl 93 ] 94 language = "cpp" # 控制idl生成c或c++代码 可选择`c`或`cpp` 95 } 96 ``` 97 982. 实现 HDI 服务 99 100 在上述步骤中idl编译后将在out目录`out/[product_name]/gen/drivers/interfaces/foo/v1_0`生成中间代码。 101 102 - 实现HDI服务接口 103 104 基于工具自动生成的`foo_interface_service.h`,实现其中的服务接口,并将相关源码编译为FooService.z.so。 105 106 实现服务业务接口: 107 ``` 108 namespace OHOS { 109 namespace HDI { 110 namespace Foo { 111 namespace V1_0 { 112 113 class FooService : public IFoo { //继承接口类,并重写接口 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 128 - 实现驱动入口 129 130 HDI服务发布是基于用户态HDF驱动框架,所以需要实现一个驱动入口。驱动实现代码参考已经在out目录中生成,如`out/gen/xxx/foo_interface_driver.cpp`,可以根据业务需要直接使用该文件或参考该文件按业务需要重新实现。 131 然后将驱动入口源码编译为`libfoo_driver.z.so`(该名称无强制规定,与hcs配置中配套即可)。 132 1333. 发布服务 134 135 在产品hcs配置中声明HDI服务,以标准系统Hi3516DV300单板为例,HDF设备配置路径为`vendor/hisilicon/Hi3516DV300/hdf_config/uhdf/device_info.hcs`,在其中新增以下配置: 136 ``` 137 fooHost :: host { 138 hostName = "fooHost"; 139 priority = 50; 140 fooDevice :: device { 141 device0 :: deviceNode { 142 policy = 2; 143 priority = 100; 144 preload = 2; 145 moduleName = "libfoo_driver.z.so"; 146 serviceName = "foo_service"; 147 } 148 } 149 } 150 ``` 151 1524. 调用HDI服务 153 154 - 客户端在BUILD.gn中增加依赖: 155 `//drivers/interface/foo/v1.0:libfoo_proxy_1.0"` 156 157 - 在代码中调用HDI接口(以CPP为例) 158 ``` 159 #include <v1_0/ifoo_interface.h> 160 161 int WorkFunc(void) { 162 sptr<IFoo> foo = OHOS::HDI::Foo::V1_0::Foo::Get(); // 使用Foo对象的内置静态方法获取该服务客户端实例 163 if (foo == nullptr) { 164 // hdi service not exist, handle error 165 } 166 167 foo->Bar(); // do interface call 168 } 169 ``` 170 如果服务存在多实例可以通过指定实例名称的方法获取对应实例`Hdi::Foo::V1_0::Foo::GetInstance(const std::string& serviceName)`; 171 172## 约定 173 1741. idl 文件命名规则 175 176 - idl 文件以大驼峰命名,与接口名称保持一致,一般以字母‘I’开头。 177 - 接口描述文件以`.idl`作为后缀。 178 1791. 接口命名规则 180 181 | 类型 | 命名风格 | 182 | ----- | ------- | 183 | 类、结构体、枚举、联合体等类型名,包名 | 大驼峰 | 184 | 方法 | 大驼峰 | 185 | 函数参数,类、结构体和联合体中的成员变量 | 小驼峰 | 186 | 宏,常量(const),枚举值 | 全大写,下划线分割 | 187 1881. 接口版本号命名规则 189 190 HDI接口版本号使用语义化版本号定义,即[major].[minor]。 191 - major版本号不同表示接口间不兼容; 192 - minor版本不同但是major版本号相同表示接口相互兼容,这种情况下不允许修改以前接口的接口名称、参数类型/个数、返回值类型/个数。 193 194## 相关仓 195 196[驱动子系统](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) 197 198 199[drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README_zh.md) 200 201 202[drivers\_peripheral](https://gitee.com/openharmony/drivers_peripheral/blob/master/README_zh.md) 203