1e41f4b71Sopenharmony_ci# Peripheral Driver Development
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## When to Use
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci[DriverExtensionAbility](../../reference/apis-driverdevelopment-kit/js-apis-app-ability-driverExtensionAbility.md) is an **ExtensionAbility** of the driver type that provides the driver-related extension framework. If the capabilities of a device can be expanded by inserting an external hardware module, you can install the driver of the hardware module through an application. **DriverExtensionAbility** can be used to develop such applications.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ciYou can bind a [DriverExtensionAbility](../../reference/apis-driverdevelopment-kit/js-apis-app-ability-driverExtensionAbility.md) object to an application through **DriverExtensionManager** so that related transactions can be processed in the background based on the application request information.
9e41f4b71Sopenharmony_ciEach type of **ExtensionAbility** has its own context. The **DriverExtensionAbility** provides related capabilities through the [DriverExtensionContext](../../reference/apis-driverdevelopment-kit/js-apis-inner-application-driverExtensionContext.md).
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Environment Setup
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciSet up the environment following the instructions in [Peripheral Driver Client Development](externaldevice-guidelines.md).
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciThe following table lists the SDK version requirements.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci| NDK API| SDK Version|
18e41f4b71Sopenharmony_ci|---------|--------|
19e41f4b71Sopenharmony_ci| USB DDK | API version 10 or later|
20e41f4b71Sopenharmony_ci| HID DDK | API version 11 or later|
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci## How to Develop
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciTo implement a driver, create a DriverExtensionAbility in the DevEco Studio project. The procedure is as follows:
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci1. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **driverextability**.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci2. In the **driverextability** directory, right-click and choose **New > ArkTS File** to create a file named **DriverExtAbility.ets**.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci3. Import the related kit, and define the request code.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci    ```ts
33e41f4b71Sopenharmony_ci    import { DriverExtensionAbility } from '@kit.DriverDevelopmentKit';
34e41f4b71Sopenharmony_ci    import { Want } from '@kit.AbilityKit';
35e41f4b71Sopenharmony_ci    import { rpc } from '@kit.IPCKit';
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci    const REQUEST_CODE = 99; // Negotaite the request code with the peripheral client.
38e41f4b71Sopenharmony_ci    ```
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci4. Open the **DriverExtAbility.ets** file, import the [RPC module](../../reference/apis-ipc-kit/js-apis-rpc.md), and overload the **onRemoteMessageRequest()** method to receive messages from the application and return the processing result to the application. **REQUEST_VALUE** is used to verify the service request code sent by the application.
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci    ```ts
43e41f4b71Sopenharmony_ci    class StubTest extends rpc.RemoteObject {
44e41f4b71Sopenharmony_ci      // Receive a message from the application and return the processing result to the client.
45e41f4b71Sopenharmony_ci      onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
46e41f4b71Sopenharmony_ci        option: rpc.MessageOption) {
47e41f4b71Sopenharmony_ci        if (code === REQUEST_CODE) {
48e41f4b71Sopenharmony_ci          // Receive the data sent from the application.
49e41f4b71Sopenharmony_ci          // When the application calls data.writeString() multiple times to write data, the driver can receive the corresponding data by calling data.readString() for multiple times.
50e41f4b71Sopenharmony_ci          let optFir: string = data.readString();
51e41f4b71Sopenharmony_ci          // The driver returns the data processing result to the application.
52e41f4b71Sopenharmony_ci          // In the example, Hello is received and Hello World is returned to the  application.
53e41f4b71Sopenharmony_ci          reply.writeString(optFir + ` World`);
54e41f4b71Sopenharmony_ci        }
55e41f4b71Sopenharmony_ci        return true;
56e41f4b71Sopenharmony_ci      }
57e41f4b71Sopenharmony_ci    }
58e41f4b71Sopenharmony_ci    ```
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci5. In the **DriverExtAbility.ets** file, import the dependency package [DriverExtensionAbility](../../reference/apis-driverdevelopment-kit/js-apis-app-ability-driverExtensionAbility.md), which provides the **onInit()**, **onRelease()**, **onConnect()**, and **onDisconnect()** lifecycle callbacks. Then, customize a class to inherit from [DriverExtensionAbility](../../reference/apis-driverdevelopment-kit/js-apis-app-ability-driverExtensionAbility.md) and override the lifecycle callbacks as required.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci    ```ts
63e41f4b71Sopenharmony_ci    export default class DriverExtAbility extends DriverExtensionAbility {
64e41f4b71Sopenharmony_ci      onInit(want: Want) {
65e41f4b71Sopenharmony_ci        console.info('testTag', `onInit, want: ${want.abilityName}`);
66e41f4b71Sopenharmony_ci      }
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci      onRelease() {
69e41f4b71Sopenharmony_ci        console.info('testTag', `onRelease`);
70e41f4b71Sopenharmony_ci      }
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci      onConnect(want: Want) {
73e41f4b71Sopenharmony_ci        console.info('testTag', `onConnect, want: ${want.abilityName}`);
74e41f4b71Sopenharmony_ci        return new StubTest("test");
75e41f4b71Sopenharmony_ci      }
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci      onDisconnect(want: Want) {
78e41f4b71Sopenharmony_ci        console.info('testTag', `onDisconnect, want: ${want.abilityName}`);
79e41f4b71Sopenharmony_ci      }
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci      onDump(params: Array<string>) {
82e41f4b71Sopenharmony_ci        console.info('testTag', `onDump, params:` + JSON.stringify(params));
83e41f4b71Sopenharmony_ci        return ['params'];
84e41f4b71Sopenharmony_ci      }
85e41f4b71Sopenharmony_ci    }
86e41f4b71Sopenharmony_ci    ```
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci6. Register **DriverExtensionAbility** in the [**module.json5** file](../../quick-start/module-configuration-file.md) of the module in the project. Set **type** to **service** and **srcEntry** to the code path of **DriverExtensionAbility**.
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci    ```json
91e41f4b71Sopenharmony_ci    {
92e41f4b71Sopenharmony_ci      "module": {
93e41f4b71Sopenharmony_ci        "name": "entry",
94e41f4b71Sopenharmony_ci        "type": "entry",
95e41f4b71Sopenharmony_ci        "description": "$string:module_desc",
96e41f4b71Sopenharmony_ci        "mainElement": "EntryAbility",
97e41f4b71Sopenharmony_ci        "deviceTypes": [
98e41f4b71Sopenharmony_ci          "default",
99e41f4b71Sopenharmony_ci          "tablet"
100e41f4b71Sopenharmony_ci        ],
101e41f4b71Sopenharmony_ci        "requestPermissions": [
102e41f4b71Sopenharmony_ci          {
103e41f4b71Sopenharmony_ci            "name": "ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER" // Peripheral-specific permission, which is mandatory.
104e41f4b71Sopenharmony_ci          }
105e41f4b71Sopenharmony_ci        ],
106e41f4b71Sopenharmony_ci        "deliveryWithInstall": true,
107e41f4b71Sopenharmony_ci        "installationFree": false,
108e41f4b71Sopenharmony_ci        "pages": "$profile:main_pages",
109e41f4b71Sopenharmony_ci        "abilities": [
110e41f4b71Sopenharmony_ci          {
111e41f4b71Sopenharmony_ci            "name": "EntryAbility",
112e41f4b71Sopenharmony_ci            "srcEntry": "./ets/entryability/EntryAbility.ets",
113e41f4b71Sopenharmony_ci            "description": "$string:EntryAbility_desc",
114e41f4b71Sopenharmony_ci            "icon": "$media:startIcon",
115e41f4b71Sopenharmony_ci            "label": "$string:EntryAbility_label",
116e41f4b71Sopenharmony_ci            "startWindowIcon": "$media:startIcon",
117e41f4b71Sopenharmony_ci            "startWindowBackground": "$color:start_window_background",
118e41f4b71Sopenharmony_ci            "exported": true,
119e41f4b71Sopenharmony_ci            "skills": [
120e41f4b71Sopenharmony_ci              {
121e41f4b71Sopenharmony_ci                "entities": [
122e41f4b71Sopenharmony_ci                  "entity.system.home"
123e41f4b71Sopenharmony_ci                ],
124e41f4b71Sopenharmony_ci                "actions": [
125e41f4b71Sopenharmony_ci                  "action.system.home"
126e41f4b71Sopenharmony_ci                ]
127e41f4b71Sopenharmony_ci              }
128e41f4b71Sopenharmony_ci            ]
129e41f4b71Sopenharmony_ci          }
130e41f4b71Sopenharmony_ci        ],
131e41f4b71Sopenharmony_ci        "extensionAbilities": [
132e41f4b71Sopenharmony_ci          {
133e41f4b71Sopenharmony_ci            "name": "DriverExtAbility",
134e41f4b71Sopenharmony_ci            "icon": "$media:startIcon",
135e41f4b71Sopenharmony_ci            "description": "driver",
136e41f4b71Sopenharmony_ci            "type": "driver",
137e41f4b71Sopenharmony_ci            "exported": true,
138e41f4b71Sopenharmony_ci            "srcEntry": "./ets/driverextability/DriverExtAbility.ets",
139e41f4b71Sopenharmony_ci            "metadata": [
140e41f4b71Sopenharmony_ci              {
141e41f4b71Sopenharmony_ci                "name": "bus", // The bus is mandatory.
142e41f4b71Sopenharmony_ci                "value": "USB"
143e41f4b71Sopenharmony_ci              },
144e41f4b71Sopenharmony_ci              {
145e41f4b71Sopenharmony_ci                "name": "desc", // Driver description, which is optional.
146e41f4b71Sopenharmony_ci                "value": "the sample of driverExtensionAbility"
147e41f4b71Sopenharmony_ci              },
148e41f4b71Sopenharmony_ci              {
149e41f4b71Sopenharmony_ci                "name": "vendor", // Driver vendor name, which is optional.
150e41f4b71Sopenharmony_ci                "value": "string"
151e41f4b71Sopenharmony_ci              },
152e41f4b71Sopenharmony_ci              {
153e41f4b71Sopenharmony_ci                "name": "vid," // List of USB vendor IDs. Enter a hex value. Here, the value is the hex value of 4817.
154e41f4b71Sopenharmony_ci                "value": "0x12D1"
155e41f4b71Sopenharmony_ci              },
156e41f4b71Sopenharmony_ci              {
157e41f4b71Sopenharmony_ci                "name": "pid," // List of USB product IDs. Enter a hex value. Here, the value is the hex value of 4258.
158e41f4b71Sopenharmony_ci                "value": "0x10A2"
159e41f4b71Sopenharmony_ci              }
160e41f4b71Sopenharmony_ci            ]
161e41f4b71Sopenharmony_ci          }
162e41f4b71Sopenharmony_ci        ]
163e41f4b71Sopenharmony_ci      }
164e41f4b71Sopenharmony_ci    }
165e41f4b71Sopenharmony_ci    ```
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci7. After completing development of the client and driver sample code, import the HAP to the device by following instructions in [Running Your App/Service on a Local Real Device](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-run-device-0000001670539800-V5), and run **Hello** in the HAP to check whether **Hello world** is displayed. If yes, the IPC communication is ready for use.
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci## Driver Development
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci**DriverExtensionAbility** provides two development modes, namely, HID DDK and USB DDK, for driver development.
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ciChoose either mode depending on your need:
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci* [HID DDK Development](https://gitee.com/openharmony/docs/blob/master/en/application-dev/napi/hid-ddk-guidelines.md)
176e41f4b71Sopenharmony_ci* [USB DDK Development](https://gitee.com/openharmony/docs/blob/master/en/application-dev/napi/usb-ddk-guidelines.md)
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci## Application Signing
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ciYou need to configure a signature file for your application to run on a device. Besides, to develop a peripheral driver client, you need to declare the **ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER** permission for the peripheral.
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ciIf the HID DDK or USB DDK is used, configure the required permission as described above.
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ciAutomatic signing: [Signing Your App/Service Automatically](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-signing-V5#section18815157237)
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ciPermission configuration: [Requesting ACL Permissions and Signing Your App/Service](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-signing-V5#section157591551175916).
187