1# Peripheral Driver Development
2
3## When to Use
4
5[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.
6
7
8You 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.
9Each 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).
10
11## Environment Setup
12
13Set up the environment following the instructions in [Peripheral Driver Client Development](externaldevice-guidelines.md).
14
15The following table lists the SDK version requirements.
16
17| NDK API| SDK Version|
18|---------|--------|
19| USB DDK | API version 10 or later|
20| HID DDK | API version 11 or later|
21
22## How to Develop
23
24To implement a driver, create a DriverExtensionAbility in the DevEco Studio project. The procedure is as follows:
25
261. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **driverextability**.
27
282. In the **driverextability** directory, right-click and choose **New > ArkTS File** to create a file named **DriverExtAbility.ets**.
29
303. Import the related kit, and define the request code.
31
32    ```ts
33    import { DriverExtensionAbility } from '@kit.DriverDevelopmentKit';
34    import { Want } from '@kit.AbilityKit';
35    import { rpc } from '@kit.IPCKit';
36
37    const REQUEST_CODE = 99; // Negotaite the request code with the peripheral client.
38    ```
39
404. 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.
41
42    ```ts
43    class StubTest extends rpc.RemoteObject {
44      // Receive a message from the application and return the processing result to the client.
45      onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence,
46        option: rpc.MessageOption) {
47        if (code === REQUEST_CODE) {
48          // Receive the data sent from the application.
49          // 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.
50          let optFir: string = data.readString();
51          // The driver returns the data processing result to the application.
52          // In the example, Hello is received and Hello World is returned to the  application.
53          reply.writeString(optFir + ` World`);
54        }
55        return true;
56      }
57    }
58    ```
59
605. 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.
61
62    ```ts
63    export default class DriverExtAbility extends DriverExtensionAbility {
64      onInit(want: Want) {
65        console.info('testTag', `onInit, want: ${want.abilityName}`);
66      }
67
68      onRelease() {
69        console.info('testTag', `onRelease`);
70      }
71
72      onConnect(want: Want) {
73        console.info('testTag', `onConnect, want: ${want.abilityName}`);
74        return new StubTest("test");
75      }
76
77      onDisconnect(want: Want) {
78        console.info('testTag', `onDisconnect, want: ${want.abilityName}`);
79      }
80
81      onDump(params: Array<string>) {
82        console.info('testTag', `onDump, params:` + JSON.stringify(params));
83        return ['params'];
84      }
85    }
86    ```
87
886. 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**.
89
90    ```json
91    {
92      "module": {
93        "name": "entry",
94        "type": "entry",
95        "description": "$string:module_desc",
96        "mainElement": "EntryAbility",
97        "deviceTypes": [
98          "default",
99          "tablet"
100        ],
101        "requestPermissions": [
102          {
103            "name": "ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER" // Peripheral-specific permission, which is mandatory.
104          }
105        ],
106        "deliveryWithInstall": true,
107        "installationFree": false,
108        "pages": "$profile:main_pages",
109        "abilities": [
110          {
111            "name": "EntryAbility",
112            "srcEntry": "./ets/entryability/EntryAbility.ets",
113            "description": "$string:EntryAbility_desc",
114            "icon": "$media:startIcon",
115            "label": "$string:EntryAbility_label",
116            "startWindowIcon": "$media:startIcon",
117            "startWindowBackground": "$color:start_window_background",
118            "exported": true,
119            "skills": [
120              {
121                "entities": [
122                  "entity.system.home"
123                ],
124                "actions": [
125                  "action.system.home"
126                ]
127              }
128            ]
129          }
130        ],
131        "extensionAbilities": [
132          {
133            "name": "DriverExtAbility",
134            "icon": "$media:startIcon",
135            "description": "driver",
136            "type": "driver",
137            "exported": true,
138            "srcEntry": "./ets/driverextability/DriverExtAbility.ets",
139            "metadata": [
140              {
141                "name": "bus", // The bus is mandatory.
142                "value": "USB"
143              },
144              {
145                "name": "desc", // Driver description, which is optional.
146                "value": "the sample of driverExtensionAbility"
147              },
148              {
149                "name": "vendor", // Driver vendor name, which is optional.
150                "value": "string"
151              },
152              {
153                "name": "vid," // List of USB vendor IDs. Enter a hex value. Here, the value is the hex value of 4817.
154                "value": "0x12D1"
155              },
156              {
157                "name": "pid," // List of USB product IDs. Enter a hex value. Here, the value is the hex value of 4258.
158                "value": "0x10A2"
159              }
160            ]
161          }
162        ]
163      }
164    }
165    ```
166
1677. 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-V5), and run **Hello** in the HAP to check whether **Hello world** is displayed. If yes, the IPC communication is ready for use.
168
169## Driver Development
170
171**DriverExtensionAbility** provides two development modes, namely, HID DDK and USB DDK, for driver development.
172
173Choose either mode depending on your need:
174
175* [HID DDK Development](https://gitee.com/openharmony/docs/blob/master/en/application-dev/napi/hid-ddk-guidelines.md)
176* [USB DDK Development](https://gitee.com/openharmony/docs/blob/master/en/application-dev/napi/usb-ddk-guidelines.md)
177
178## Application Signing
179
180You 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.
181
182If the HID DDK or USB DDK is used, configure the required permission as described above.
183
184Automatic signing: [Signing Your App/Service Automatically](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-signing-V5#section18815157237)
185
186Permission configuration: [Requesting ACL Permissions and Signing Your App/Service](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-signing-V5#section157591551175916).
187