1# General Framework for Battery Kernel Node Read/Write 2 3## Overview 4 5### Introduction 6 7OpenHarmony supports differentiated configuration of charging features on different devices and therefore provides a general framework for reading and writing the battery kernel node. Specifically, OpenHarmony reads the kernel node based on the charging scenario to obtain the charging feature and then writes data to the kernel node to enable or start the feature. You can configure charging features based on the product specifications and manage the features through system APIs. 8 9### Constraints 10 11N/A 12 13## How to Develop 14 15### Setting Up the Environment 16 17**Hardware requirements:** 18 19Development board running the standard system, for example, the DAYU200 or Hi3516D V300 open source suite. 20 21**Environment requirements:** 22 23For details about the requirements on the Linux environment, see [Quick Start](../quick-start/quickstart-overview.md). 24 25### Getting Started with Development 26 27The following uses [DAYU200](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568) as an example to illustrate development of the general framework for reading/writing the battery kernel node. 28 291. Modify the `battery_config.json` file in [default battery service configuration folder](https://gitee.com/openharmony/drivers_peripheral/tree/master/battery/interfaces/hdi_service/profile). The configuration is as follows: 30 31```text 32profile 33├── BUILD.gn 34├── battery_config.json 35``` 36 37Example configuration: 38 39```json 40{ 41 // Add the new configuration after the original configuration. 42 "charger": { 43 ..... // Content omitted. 44 }, 45 // Add the new configuration. Do not omit the comma at the end of the previous line. 46 "charge_scene": { 47 "scene_name": { 48 "support": { 49 "path": "", 50 "type": "", 51 "expect_value": "" 52 }, 53 "get": { 54 "path": "" 55 }, 56 "set": { 57 "path": "" 58 } 59 } 60 } 61} 62``` 63 64Configuration description: 65 66- **charger**: original configuration. Add the new configuration after this configuration. 67 68- **charge_scene**: new configuration. It is the identifier of the charging scenario and cannot be changed. 69 70- **scene_name**: charging scenario. The **support**, **get**, and **set** attributes can be configured for each charging scenario. You can change the scenario name, but make sure that it must be the same as that in the system API request. You can also add a charging scenario. 71 72- **support**: whether the charging scenario is supported. **path** is the default attribute, which specifies the path of the kernel node that can be queried in the charging scenario. In addition, the **type** and **expect_value** attributes are supported. 73 74 - **type**: type of the kernel node path in the charging scenario. This field is mandatory and can only be set to **dir/file**. **dir** indicates that the kernel node path is a directory or file, and **file** indicates that the kernel node path is a file. 75 76 - **expect_value**: expected value that supports the charging scenario when type is set to **file**. 77 78- **get**: getter attribute of the charging scenario. **path** is the default attribute, which specifies the path of the kernel node that can be queried in the charging scenario. 79 80- **set**: setter attribute of the charging scenario. **path** is the default attribute, which specifies the path of the kernel node that can be set in the charging scenario. 81 822. Modify the API file `@ohos.batteryInfo.d.ts` in [interface_sdk-js/api](https://gitee.com/openharmony/interface_sdk-js/tree/master/api). 83 84``` 85profile 86├── ... 87├── @ohos.base.d.ts 88├── @ohos.batteryInfo.d.ts 89├── @ohos.batteryStatistics.d.ts 90├── ... 91``` 92 93The system API configuration is as follows: 94 95``` 96..... // Content omitted. 97 98declare namespace chargeScene { 99 /** 100 * Sets the battery config by scene name. 101 * 102 * @param { string } sceneName - Indicates the battery charging scene name. 103 * @param { string } sceneValue - Indicates the battery charging scene value. 104 * @returns { number } Return to set the charging configuration result. 105 * @throws { BusinessError } 201 - If the permission is denied. 106 * @throws { BusinessError } 202 - If the system permission is denied. 107 * @throws { BusinessError } 401 - If the reason is not valid. 108 * @throws { BusinessError } 4900101 - If connecting to the service failed. 109 * @syscap SystemCapability.PowerManager.BatteryManager.Core 110 * @systemapi 111 * @since 11 112 */ 113 function setBatteryConfig(sceneName: string, sceneValue: string): number; 114 115 /** 116 * Queries the battery config by scene name. 117 * 118 * @param { string } sceneName - Indicates the battery charging scene name. 119 * @returns { string } Returns the battery charging configuration, returns "" otherwise. 120 * @throws { BusinessError } 201 - If the permission is denied. 121 * @throws { BusinessError } 202 - If the system permission is denied. 122 * @throws { BusinessError } 401 - If the reason is not valid. 123 * @throws { BusinessError } 4900101 - If connecting to the service failed. 124 * @syscap SystemCapability.PowerManager.BatteryManager.Core 125 * @systemapi 126 * @since 11 127 */ 128 function getBatteryConfig(sceneName: string): string; 129 130 /** 131 * Checks the battery config is enable by scene name. 132 * 133 * @param { string } sceneName - Indicates the battery charging scene name. 134 * @returns { boolean } Returns true if the device supports the charging scene, returns false otherwise. 135 * @throws { BusinessError } 201 - If the permission is denied. 136 * @throws { BusinessError } 202 - If the system permission is denied. 137 * @throws { BusinessError } 401 - If the reason is not valid. 138 * @throws { BusinessError } 4900101 - If connecting to the service failed. 139 * @syscap SystemCapability.PowerManager.BatteryManager.Core 140 * @systemapi 141 * @since 11 142 */ 143 function isBatteryConfigSupported(sceneName: string): boolean; 144 145..... // Content omitted. 146``` 147 148System API description: 149 150- **setBatteryConfig**: Sets the battery configuration. **sceneName** and **sceneValue** indicates the name and value of the charging scenario, respectively. If the return result is **0**, the operation is successful. Otherwise, the operation fails. 151 152- **getBatteryConfig**: Queries the battery configuration. **sceneName** indicates the name of the charging scenario. The returned value indicates the value of the charging scenario. 153 154- **isBatteryConfigSupported**: Checks whether the battery configuration is supported. **sceneName** indicates the name of the charging scenario. If the return result is **true**, the charging scenario is supported. If the return result is **false**, the charging scenario is not supported. 155 1563. Build the customized version by referring to [Quick Start](../quick-start/quickstart-overview.md). 157 158```shell 159./build.sh --product-name rk3568 --ccache 160``` 161 1624. Burn the customized version to the RK3568 development board. 163 164### Debugging and Verification 165 166Upon device restarting, check whether the return results of the getter and setter APIs comply with the device configuration. If yes, the function test is successful. 167 168## Reference 169 170During development, you can refer to the default battery vibrator configuration: 171 172[Configuration file of the battery charging scenario](https://gitee.com/openharmony/drivers_peripheral/tree/master/battery/interfaces/hdi_service/profile) 173