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