1# 电池读写内核节点通用框架开发指导
2
3## 概述
4
5### 简介
6
7OpenHarmony支持在不同设备上多种充电特性进行差异化配置,为此OpenHarmony提供了读写电池内核节点的通用框架:根据充电场景读取内核节点来获取特性详情,根据充电场景写入内核节点来开启或进入特性。系统开发者可根据产品的设计规格进行相应的配置,并通过系统API的进行管控。
8
9### 约束与限制
10
1112
13## 开发指导
14
15### 搭建环境
16
17设备要求:
18
19标准系统开发板,如DAYU200/Hi3516DV300开源套件。
20
21环境要求:
22
23Linux调测环境,相关要求和配置可参考《[快速入门](../quick-start/quickstart-overview.md)》。
24
25### 开发步骤
26
27本文以RK3568开发板为例,介绍如何实现配置
28
291. 修改[drivers_peripheral/battery/interfaces/hdi_service/profile](https://gitee.com/openharmony/drivers_peripheral/tree/master/battery/interfaces/hdi_service/profile)路径下的电池配置文件battery_config.json30
31```text
32profile
33├── BUILD.gn
34├── battery_config.json
35```
36
37以如下配置为例:
38
39```json
40{
41    // 原有配置,新增配置在该条配置后增加
42    "charger": {
43        ..... // 省略内容
44    },
45    // 新增配置,注意不要遗漏上一行结尾的逗号
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
64配置说明:
65
66- charger:原有配置,新增配置在该条配置后增加
67
68- charge_scene:新增配置,充电场景配置标识符,不可更改
69
70- scene_name:充电场景名称,每个充电场景可以配置support、get、set三条属性。可以修改场景名称,但需要与系统API接口请求中场景名称保持一致;可以增加新的场景。
71
72- support:充电场景的支持属性,path为其默认存在的属性,表示充电场景对应的支持查询的内核节点路径。除外还支持type和expect_value两个属性:
73
74  ​- type:充电场景的内核节点路径的类型,必填字段,只能填充dir/file。dir:表示内核节点路径为目录或者文件;file:表示内核节点路径为文件
75
76  ​- expect_value:type为file时,支持该充电场景的预期值
77
78- get:充电场景的查询属性,path为其默认存在的属性,表示充电场景对应的查询内核节点路径
79
80- set:充电场景的设置属性,path为其默认存在的属性,表示充电场景对应的设置内核节点路径
81
822. 修改[interface_sdk-js/api](https://gitee.com/openharmony/interface_sdk-js/tree/master/api)路径下的api文件@ohos.batteryInfo.d.ts83
84```
85profile
86├── ...
87├── @ohos.base.d.ts
88├── @ohos.batteryInfo.d.ts
89├── @ohos.batteryStatistics.d.ts
90├── ...
91```
92
93系统API接口配置如下:
94
95```
96... // 省略内容
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... // 省略内容
146```
147
148系统API说明:
149
150- setBatteryConfig:设置电池配置,sceneName表示充电场景名称,sceneValue表示充电场景配置值,返回值为0表示设置成功,其他表示设置失败
151
152- getBatteryConfig:查询电池配置,sceneName表示充电场景名称,返回值表示充电场景配置值
153
154- isBatteryConfigSupported:是否支持电池配置,sceneName表示充电场景名称,返回值为true表示支持该充电场景,返回值为false表示不支持该充电场景
155
1563. 参考《[快速入门](../quick-start/quickstart-overview.md)》编译定制版本,编译命令如下:
157
158```shell
159./build.sh --product-name rk3568 --ccache
160```
161
1624. 将定制版本烧录到RK3568开发板中。
163
164### 调测验证
165
166烧录版本后重启设备,验证TS接口查询/设置结果是否符合设备配置情况,符合则功能测试成功。
167
168## 参考
169
170开发过程中可参考的配置文件路径:
171
172[电池充电场景配置文件路径](https://gitee.com/openharmony/drivers_peripheral/tree/master/battery/interfaces/hdi_service/profile)