1# Power Wakeup Action Source Customization
2
3## Overview
4
5### Introduction
6
7OpenHarmony provides the function of waking up the system in sleep mode to take expected actions. For example, when the battery level is low, OpenHarmony can wake up the system to shut down the device. When the system is in the sleep state, it can be woken up based on the configuration policy to perform the scheduled action. However, supported application scenarios may vary according to products. For example, wakeup at low battery level may not be supported by certain products. To address this issue, OpenHarmony provides the function that allows you to customize power wakeup action sources depending on your product specifications.
8
9### Constraints
10
11
12Required adaptation:
13
14- Triggering a power button event when the wakeup condition is met (for example, the battery level is lower than the specified threshold).
15- Saving the wakeup reason (for example, wakeup due to low battery level) to the kernel node.
16
17Configuration rule:
18The configuration path for power wakeup action source customization is subject to the [configuration policy](https://gitee.com/openharmony/customization_config_policy). In this development guide, `/vendor` is used as an example of the configuration path. During actual development, you need to modify the customization path based on the product configuration policy.
19
20## How to Develop
21
22### Setting Up the Environment
23
24Hardware requirements:
25
26Development board running the standard system, for example, the DAYU200 or Hi3516D V300 open source suite.
27
28Environment requirements:
29
30For details about the requirements on the Linux environment, see [Quick Start](../quick-start/quickstart-overview.md).
31
32### Getting Started with Development
33
34The following uses [DAYU200](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568) as an example to illustrate post-wakeup system action customization.
35
361. Create the `power_manager` folder in the product directory [/vendor/hihope/rk3568](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568).
37
382. Create a target folder by referring to the [default folder of power wakeup action source configuration](https://gitee.com/openharmony/powermgr_power_manager/tree/master/services/native/profile), and install it in `/vendor/hihope/rk3568/power_manager`. The content is as follows:
39
40    ```text
41    profile
42    ├── BUILD.gn
43    ├── power_wakeup_action.json
44    ```
45
463. Write the custom `power_wakeup_action.json` file, which contains the custom wakeup reasons and actions as follows:
47
48    ```json
49    {
50        "53": {
51            "scene": "LowCapacity",
52            "action": 2,
53            "description": "(such as)53 is a uniquely wakeup reason by reading node through HDI interface(GetWakeupReason)"
54        }
55    }
56    ```
57
58    **Table 1** Description of wakeup sources
59
60    | Wakeup Source| Description|
61    | -------- | -------- |
62    | 53 | Wakeup due to low battery level.|
63
64    The wakeup source can be obtained by reading node data or by other ways.
65
66    **Table 2** Description of wakeup scenarios
67
68    | Wakeup Scenario| Description|
69    | -------- | -------- |
70    | LowCapacity | Low battery level.|
71
72    **Table 3** Description of actions
73
74    | action | Value| Description|
75    | -------- | -------- | -------- |
76    | ACTION_NONE | 0 | No action|
77    | ACTION_HIBERNATE | 1 | Hibernation|
78    | ACTION_SHUTDOWN | 2 | Shutdown|
79
804. Write the `BUILD.gn` file by referring to the [BUILD.gn](https://gitee.com/openharmony/powermgr_power_manager/blob/master/services/native/profile/BUILD.gn) file in the default folder of power wakeup action source configuration to pack the `power_wakeup_action.json` file to the `/vendor/etc/power_wakeup_action` directory. The configuration is as follows:
81
82    ```shell
83    import("//build/ohos.gni")               # Reference build/ohos.gni.
84
85    ohos_prebuilt_etc("wakeup_action_config") {
86        source = "power_wakeup_action.json"
87        relative_install_dir = "power_config"
88        install_images = [ chipset_base_dir ] # Required configuration for installing the battery_config.json file in the vendor directory.
89        part_name = "product_rk3568"          # Set part_name to product_rk3568 for subsequent build.
90    }
91    ```
92
935. Add the build target to `module_list` in [ohos.build](https://gitee.com/openharmony/vendor_hihope/blob/master/rk3568/ohos.build) in the `/vendor/hihope/rk3568` directory. For example:
94
95    ```json
96    {
97        "parts": {
98            "product_rk3568": {
99                "module_list": [
100                    "//vendor/hihope/rk3568/default_app_config:default_app_config",
101                    "//vendor/hihope/rk3568/image_conf:custom_image_conf",
102                    "//vendor/hihope/rk3568/preinstall-config:preinstall-config",
103                    "//vendor/hihope/rk3568/resourceschedule:resourceschedule",
104                    "//vendor/hihope/rk3568/etc:product_etc_conf",
105                    "//vendor/hihope/rk3568/power_manager/profile:wakeup_action_config" // Add the configuration for building of wakeup_action_config.
106                ]
107            }
108        },
109        "subsystem": "product_hihope"
110    }
111    ```
112    In the preceding code, `//vendor/hihope/rk3568/power_manager/` is the folder path, `profile` is the folder name, and `wakeup_action_config` is the build target.
113
1146. Build the customized version by referring to [Quick Start](../quick-start/quickstart-overview.md).
115
116    ```shell
117    ./build.sh --product-name rk3568 --ccache
118    ```
119
1207. Burn the customized version to the DAYU200 development board.
121
122### Debugging and Verification
123
1241. Customize power wakeup action sources in the new configuration file.
125    ```json
126    {
127        "53": {
128            "scene": "LowCapacity",
129            "action": 2,
130            "description": "(such as)53 is a uniquely wakeup reason by reading node through HDI interface(GetWakeupReason)"
131        }
132    }
133    ```
134
1352. In the [powermgr.gni](https://gitee.com/openharmony/powermgr_power_manager/blob/master/powermgr.gni) file, set **power_manager_feature_wakeup_action** to **true** to enable the feature.
136    ```
137    power_manager_feature_wakeup_action = true
138    ```
139
1403. Add the following configuration to the [battery_config.json](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/services/native/profile/battery_config.json) file:
141    ```json
142    "charge_scene": {
143        "low_battery_thers": {
144          "set": {
145            "path": "xxx"
146          }
147        }
148    }
149    ```
150    In the preceding configuration, **path** indicates the path of the node that stores the low battery threshold.
151
1524. In the [batterymgr.gni](https://gitee.com/openharmony/powermgr_battery_manager/blob/master/batterymgr.gni) file, set **battery_manager_feature_set_low_capacity_threshold** to **true** to enable the feature.
153    ```
154    battery_manager_feature_set_low_capacity_threshold = true
155    ```
1565. Add the following configuration to the [power_config.json](https://gitee.com/openharmony/drivers_peripheral/blob/master/power/interfaces/hdi_service/profile/power_config.json) file:
157    ```json
158    {
159        "scene" :{
160            "wakeuo_cause": {
161                "get": {
162                    "path": "yyy"
163                }
164            }
165        }
166    }
167    ```
168    In the preceding configuration, **get** indicates the path of the node that stores the low battery wakeup reason.
169
1705. In the [power.gni](https://gitee.com/openharmony/drivers_peripheral/blob/master/power/power.gni) file, set **drivers_peripheral_power_wakeup_cause_path** to **true** to enable the feature.
171    ```
172    drivers_peripheral_power_wakeup_cause_path = true
173    ```
1746. Change the node permission to **system** in the **pre-init** field of the [hdf_peripheral.cfg](https://gitee.com/openharmony/drivers_peripheral/blob/master/base/hdf_peripheral.cfg) file.
175    ```
176    "chown system system xxx",
177    "chown system system yyy",
178    ```
1797. Decrease the battery level to fall below the specified threshold when the system is in the sleep state.
180
181    The device is shut down.
182