1# UI-free Driver Development 2 3## When to Use 4 5Basic UI-free drivers are applicable to simple devices that do not require setting of driver capabilities via a UI, such as mouse devices and keyboards. These drivers are designed to ensure that these devices can be used immediately upon connection, enabling seamless plug-and-play functionality. You can use **DriverExtensionAbility** to develop such applications. 6 7## Basic Concepts 8 9 - DriverExtensionAbility 10 11 [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. You 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. 12 Each 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). 13 14## Environment Setup 15 16Before you get started, make necessary preparations by following instructions in [Environment Preparation](environmental-preparation.md). 17 18## How to Develop 19 20To implement a driver, create a DriverExtensionAbility in the DevEco Studio project. The procedure is as follows: 21 221. Create an OpenHarmony project. For details, see [Creating a Project] (https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-create-new-project). (If a project has been created in [UI-based Driver Development](externaldevice-guidelines.md), skip this step.) 23 242. In the **ets** directory of the project, right-click **New** > **Directory** to create a directory named **driverextability**. 25 263. In the **driverextability** directory, right-click and choose **New > ArkTS File** to create a file named **DriverExtAbility.ets**. 27 284. Import the related kit, and define the request code. 29 30 ```ts 31 import { DriverExtensionAbility } from '@kit.DriverDevelopmentKit'; 32 import { Want } from '@kit.AbilityKit'; 33 import { rpc } from '@kit.IPCKit'; 34 35 const REQUEST_CODE = 99; // Negotiate the request code with the peripheral client. 36 ``` 37 385. 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_CODE** is used to verify the service request code sent by the application. 39 40 ```ts 41 class StubTest extends rpc.RemoteObject { 42 // Receive a message from the application and return the processing result to the client. 43 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, 44 option: rpc.MessageOption) { 45 if (code === REQUEST_CODE) { 46 // Receive the data sent from the application. 47 // 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. 48 let optFir: string = data.readString(); 49 // The driver returns the data processing result to the application. 50 // In the example, Hello is received and Hello World is returned to the application. 51 reply.writeString(optFir + ` World`); 52 } 53 return true; 54 } 55 } 56 ``` 57 586. 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. 59 60 ```ts 61 export default class DriverExtAbility extends DriverExtensionAbility { 62 onInit(want: Want) { 63 console.info('testTag', `onInit, want: ${want.abilityName}`); 64 } 65 66 onRelease() { 67 console.info('testTag', `onRelease`); 68 } 69 70 onConnect(want: Want) { 71 console.info('testTag', `onConnect, want: ${want.abilityName}`); 72 return new StubTest("test"); 73 } 74 75 onDisconnect(want: Want) { 76 console.info('testTag', `onDisconnect, want: ${want.abilityName}`); 77 } 78 79 onDump(params: Array<string>) { 80 console.info('testTag', `onDump, params:` + JSON.stringify(params)); 81 return ['params']; 82 } 83 } 84 ``` 85 867. 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**. 87 88 ```json 89 { 90 "module": { 91 "name": "entry", 92 "type": "entry", 93 "description": "$string:module_desc", 94 "mainElement": "EntryAbility", 95 "deviceTypes": [ 96 "default", 97 "tablet" 98 ], 99 "requestPermissions": [ 100 { 101 "name": "ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER" // Peripheral-specific permission, which is mandatory. 102 }, 103 { 104 "name": "ohos.permission.ACCESS_DDK_DRIVERS" // Peripheral access permission, which is mandatory in API version 18 or later. 105 } 106 ], 107 "deliveryWithInstall": true, 108 "installationFree": false, 109 "pages": "$profile:main_pages", 110 "abilities": [ 111 { 112 "name": "EntryAbility", 113 "srcEntry": "./ets/entryability/EntryAbility.ets", 114 "description": "$string:EntryAbility_desc", 115 "icon": "$media:startIcon", 116 "label": "$string:EntryAbility_label", 117 "startWindowIcon": "$media:startIcon", 118 "startWindowBackground": "$color:start_window_background", 119 "exported": true, 120 "skills": [ 121 { 122 "entities": [ 123 "entity.system.home" 124 ], 125 "actions": [ 126 "action.system.home" 127 ] 128 } 129 ] 130 } 131 ], 132 "extensionAbilities": [ 133 { 134 "name": "DriverExtAbility", 135 "icon": "$media:startIcon", 136 "description": "driver", 137 "type": "driver", 138 "exported": true, 139 "srcEntry": "./ets/driverextability/DriverExtAbility.ets", 140 "metadata": [ 141 { 142 "name": "bus", // Bus, which is mandatory. 143 "value": "USB" 144 }, 145 { 146 "name": "desc", // Driver description, which is optional. 147 "value": "the sample of driverExtensionAbility" 148 }, 149 { 150 "name": "vendor", // Driver vendor name, which is optional. 151 "value": "string" 152 }, 153 { 154 "name": "vid," // List of USB vendor IDs. Enter a hex value. Here, the value is the hex value of 4817. 155 "value": "0x12D1" 156 }, 157 { 158 "name": "pid," // List of USB product IDs. Enter a hex value. Here, the value is the hex value of 4258. 159 "value": "0x10A2" 160 }, 161 { 162 "name": "launchOnBind," // Whether to enable delayed driver startup. This parameter is optional. The value true indicates delayed startup, and the value false indicates immediate startup. The value is false by default if the specified value is incorrect or the value is left unspecified. 163 "value": "true" 164 }, 165 { 166 "name": "ohos.permission.ACCESS_DDK_ALLOWED," // Whether to allow DDK access. This parameter is optional. The value true indicates that DDK access is allowed, and the value false indicates the opposite. The default value is false. 167 "value": "true" 168 } 169 ] 170 } 171 ] 172 } 173 } 174 ``` 175 1768. 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-V13/ide-run-device-V13), and run **Hello** in the HAP to check whether **Hello world** is displayed. If yes, the IPC communication is ready for use. 177 178## Driver Development 179 180Currently, **DriverExtensionAbility** provides four capabilities: HID DDK, USB DDK, USB serial DDK, and SCSI peripheral DDK, which are used to develop dedicated drivers for extended peripherals. Choose either mode depending on your need: 181 182* [HID DDK Development](hid-ddk-guidelines.md) 183* [USB DDK Development](usb-ddk-guidelines.md) 184* [USB Serial DDK Development](usb-serial-ddk-guidelines.md) 185* [SCSI Peripheral DDK Development](scsi-peripheral-ddk-guidelines.md) 186 187<!--RP1--> 188## Application Signing 189 190**NOTE**<br>Configure the permission before enabling automatic signing. 191 192You 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** and **ohos.permission.ACCESS_DDK_DRIVERS** permissions for the peripheral. 193- ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER (This permission is required for API version 10 or later.) 194 195 To obtain authorization on this access permission, [declare it](../../security/AccessToken/declare-permissions.md) in the **requestPermissions** field in the **module.json5** file. 196 197- ohos.permission.ACCESS_DDK_DRIVERS (This permission is required for API version 18 or later.) 198 199 1. [Declare the required permissions](../../security/AccessToken/declare-permissions.md) in the **requestPermissions** field in the **module.json5** file. 200 2. Modify the **acls** field in the **HarmonyAppProvision** configuration file to request permissions via ACL. For details, see [Requesting Restricted Permissions](../../security/AccessToken/declare-permissions-in-acl.md). 201 3. In the **HarmonyAppProvision** configuration file (that is, **Sdk/openharmony/_{Version} _/toolchains /lib/UnsgnedReleasedProfileTemplate.json** file), configure the bundle name of the driver server to connect. If there are multiple servers, separate their bundle names with a comma. 202 203 The configuration procedure is as follows: 204 205 Add the **app-services-capabilities** node to the root node of the file and configure the node as follows: 206 ```json 207 "app-services-capabilities": { 208 "ohos.permission.ACCESS_DDK_DRIVERS": {"bundleNames": "bundleName0,bundleName1,bundleName2"} 209 } 210 ``` 211 212Automatic signing: [Signing Your App/Service Automatically](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V13/ide-signing-V13#section18815157237) 213<!--RP1End--> 214