1e41f4b71Sopenharmony_ci# General Framework for Battery Kernel Node Read/Write 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### Introduction 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciOpenHarmony 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. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Constraints 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciN/A 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## How to Develop 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### Setting Up the Environment 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**Hardware requirements:** 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciDevelopment board running the standard system, for example, the DAYU200 or Hi3516D V300 open source suite. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**Environment requirements:** 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciFor details about the requirements on the Linux environment, see [Quick Start](../quick-start/quickstart-overview.md). 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci### Getting Started with Development 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciThe 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. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci1. 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: 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci```text 32e41f4b71Sopenharmony_ciprofile 33e41f4b71Sopenharmony_ci├── BUILD.gn 34e41f4b71Sopenharmony_ci├── battery_config.json 35e41f4b71Sopenharmony_ci``` 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciExample configuration: 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci```json 40e41f4b71Sopenharmony_ci{ 41e41f4b71Sopenharmony_ci // Add the new configuration after the original configuration. 42e41f4b71Sopenharmony_ci "charger": { 43e41f4b71Sopenharmony_ci ..... // Content omitted. 44e41f4b71Sopenharmony_ci }, 45e41f4b71Sopenharmony_ci // Add the new configuration. Do not omit the comma at the end of the previous line. 46e41f4b71Sopenharmony_ci "charge_scene": { 47e41f4b71Sopenharmony_ci "scene_name": { 48e41f4b71Sopenharmony_ci "support": { 49e41f4b71Sopenharmony_ci "path": "", 50e41f4b71Sopenharmony_ci "type": "", 51e41f4b71Sopenharmony_ci "expect_value": "" 52e41f4b71Sopenharmony_ci }, 53e41f4b71Sopenharmony_ci "get": { 54e41f4b71Sopenharmony_ci "path": "" 55e41f4b71Sopenharmony_ci }, 56e41f4b71Sopenharmony_ci "set": { 57e41f4b71Sopenharmony_ci "path": "" 58e41f4b71Sopenharmony_ci } 59e41f4b71Sopenharmony_ci } 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci} 62e41f4b71Sopenharmony_ci``` 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ciConfiguration description: 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci- **charger**: original configuration. Add the new configuration after this configuration. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci- **charge_scene**: new configuration. It is the identifier of the charging scenario and cannot be changed. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci- **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. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci- **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. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci - **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. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci - **expect_value**: expected value that supports the charging scenario when type is set to **file**. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci- **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. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci- **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. 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci2. Modify the API file `@ohos.batteryInfo.d.ts` in [interface_sdk-js/api](https://gitee.com/openharmony/interface_sdk-js/tree/master/api). 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci``` 85e41f4b71Sopenharmony_ciprofile 86e41f4b71Sopenharmony_ci├── ... 87e41f4b71Sopenharmony_ci├── @ohos.base.d.ts 88e41f4b71Sopenharmony_ci├── @ohos.batteryInfo.d.ts 89e41f4b71Sopenharmony_ci├── @ohos.batteryStatistics.d.ts 90e41f4b71Sopenharmony_ci├── ... 91e41f4b71Sopenharmony_ci``` 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ciThe system API configuration is as follows: 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci``` 96e41f4b71Sopenharmony_ci..... // Content omitted. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_cideclare namespace chargeScene { 99e41f4b71Sopenharmony_ci /** 100e41f4b71Sopenharmony_ci * Sets the battery config by scene name. 101e41f4b71Sopenharmony_ci * 102e41f4b71Sopenharmony_ci * @param { string } sceneName - Indicates the battery charging scene name. 103e41f4b71Sopenharmony_ci * @param { string } sceneValue - Indicates the battery charging scene value. 104e41f4b71Sopenharmony_ci * @returns { number } Return to set the charging configuration result. 105e41f4b71Sopenharmony_ci * @throws { BusinessError } 201 - If the permission is denied. 106e41f4b71Sopenharmony_ci * @throws { BusinessError } 202 - If the system permission is denied. 107e41f4b71Sopenharmony_ci * @throws { BusinessError } 401 - If the reason is not valid. 108e41f4b71Sopenharmony_ci * @throws { BusinessError } 4900101 - If connecting to the service failed. 109e41f4b71Sopenharmony_ci * @syscap SystemCapability.PowerManager.BatteryManager.Core 110e41f4b71Sopenharmony_ci * @systemapi 111e41f4b71Sopenharmony_ci * @since 11 112e41f4b71Sopenharmony_ci */ 113e41f4b71Sopenharmony_ci function setBatteryConfig(sceneName: string, sceneValue: string): number; 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci /** 116e41f4b71Sopenharmony_ci * Queries the battery config by scene name. 117e41f4b71Sopenharmony_ci * 118e41f4b71Sopenharmony_ci * @param { string } sceneName - Indicates the battery charging scene name. 119e41f4b71Sopenharmony_ci * @returns { string } Returns the battery charging configuration, returns "" otherwise. 120e41f4b71Sopenharmony_ci * @throws { BusinessError } 201 - If the permission is denied. 121e41f4b71Sopenharmony_ci * @throws { BusinessError } 202 - If the system permission is denied. 122e41f4b71Sopenharmony_ci * @throws { BusinessError } 401 - If the reason is not valid. 123e41f4b71Sopenharmony_ci * @throws { BusinessError } 4900101 - If connecting to the service failed. 124e41f4b71Sopenharmony_ci * @syscap SystemCapability.PowerManager.BatteryManager.Core 125e41f4b71Sopenharmony_ci * @systemapi 126e41f4b71Sopenharmony_ci * @since 11 127e41f4b71Sopenharmony_ci */ 128e41f4b71Sopenharmony_ci function getBatteryConfig(sceneName: string): string; 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci /** 131e41f4b71Sopenharmony_ci * Checks the battery config is enable by scene name. 132e41f4b71Sopenharmony_ci * 133e41f4b71Sopenharmony_ci * @param { string } sceneName - Indicates the battery charging scene name. 134e41f4b71Sopenharmony_ci * @returns { boolean } Returns true if the device supports the charging scene, returns false otherwise. 135e41f4b71Sopenharmony_ci * @throws { BusinessError } 201 - If the permission is denied. 136e41f4b71Sopenharmony_ci * @throws { BusinessError } 202 - If the system permission is denied. 137e41f4b71Sopenharmony_ci * @throws { BusinessError } 401 - If the reason is not valid. 138e41f4b71Sopenharmony_ci * @throws { BusinessError } 4900101 - If connecting to the service failed. 139e41f4b71Sopenharmony_ci * @syscap SystemCapability.PowerManager.BatteryManager.Core 140e41f4b71Sopenharmony_ci * @systemapi 141e41f4b71Sopenharmony_ci * @since 11 142e41f4b71Sopenharmony_ci */ 143e41f4b71Sopenharmony_ci function isBatteryConfigSupported(sceneName: string): boolean; 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci..... // Content omitted. 146e41f4b71Sopenharmony_ci``` 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ciSystem API description: 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci- **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. 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci- **getBatteryConfig**: Queries the battery configuration. **sceneName** indicates the name of the charging scenario. The returned value indicates the value of the charging scenario. 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci- **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. 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci3. Build the customized version by referring to [Quick Start](../quick-start/quickstart-overview.md). 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci```shell 159e41f4b71Sopenharmony_ci./build.sh --product-name rk3568 --ccache 160e41f4b71Sopenharmony_ci``` 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci4. Burn the customized version to the RK3568 development board. 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci### Debugging and Verification 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciUpon 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. 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci## Reference 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ciDuring development, you can refer to the default battery vibrator configuration: 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci[Configuration file of the battery charging scenario](https://gitee.com/openharmony/drivers_peripheral/tree/master/battery/interfaces/hdi_service/profile) 173