1e41f4b71Sopenharmony_ci# Power Wakeup Action Source Customization
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Introduction
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciOpenHarmony 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.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci### Constraints
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci 
12e41f4b71Sopenharmony_ciRequired adaptation:
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci- Triggering a power button event when the wakeup condition is met (for example, the battery level is lower than the specified threshold).
15e41f4b71Sopenharmony_ci- Saving the wakeup reason (for example, wakeup due to low battery level) to the kernel node.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciConfiguration rule:
18e41f4b71Sopenharmony_ciThe 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.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci## How to Develop
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci### Setting Up the Environment
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciHardware requirements:
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ciDevelopment board running the standard system, for example, the DAYU200 or Hi3516D V300 open source suite.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ciEnvironment requirements:
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ciFor details about the requirements on the Linux environment, see [Quick Start](../quick-start/quickstart-overview.md).
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci### Getting Started with Development
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ciThe following uses [DAYU200](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568) as an example to illustrate post-wakeup system action customization.
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci1. Create the `power_manager` folder in the product directory [/vendor/hihope/rk3568](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568).
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci2. 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:
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci    ```text
41e41f4b71Sopenharmony_ci    profile
42e41f4b71Sopenharmony_ci    ├── BUILD.gn
43e41f4b71Sopenharmony_ci    ├── power_wakeup_action.json
44e41f4b71Sopenharmony_ci    ```
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci3. Write the custom `power_wakeup_action.json` file, which contains the custom wakeup reasons and actions as follows:
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci    ```json
49e41f4b71Sopenharmony_ci    {
50e41f4b71Sopenharmony_ci        "53": {
51e41f4b71Sopenharmony_ci            "scene": "LowCapacity",
52e41f4b71Sopenharmony_ci            "action": 2,
53e41f4b71Sopenharmony_ci            "description": "(such as)53 is a uniquely wakeup reason by reading node through HDI interface(GetWakeupReason)"
54e41f4b71Sopenharmony_ci        }
55e41f4b71Sopenharmony_ci    }
56e41f4b71Sopenharmony_ci    ```
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci    **Table 1** Description of wakeup sources
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci    | Wakeup Source| Description|
61e41f4b71Sopenharmony_ci    | -------- | -------- |
62e41f4b71Sopenharmony_ci    | 53 | Wakeup due to low battery level.|
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci    The wakeup source can be obtained by reading node data or by other ways.
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci    **Table 2** Description of wakeup scenarios
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci    | Wakeup Scenario| Description|
69e41f4b71Sopenharmony_ci    | -------- | -------- |
70e41f4b71Sopenharmony_ci    | LowCapacity | Low battery level.|
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci    **Table 3** Description of actions
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci    | action | Value| Description|
75e41f4b71Sopenharmony_ci    | -------- | -------- | -------- |
76e41f4b71Sopenharmony_ci    | ACTION_NONE | 0 | No action|
77e41f4b71Sopenharmony_ci    | ACTION_HIBERNATE | 1 | Hibernation|
78e41f4b71Sopenharmony_ci    | ACTION_SHUTDOWN | 2 | Shutdown|
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci4. 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:
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci    ```shell
83e41f4b71Sopenharmony_ci    import("//build/ohos.gni")               # Reference build/ohos.gni.
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci    ohos_prebuilt_etc("wakeup_action_config") {
86e41f4b71Sopenharmony_ci        source = "power_wakeup_action.json"
87e41f4b71Sopenharmony_ci        relative_install_dir = "power_config"
88e41f4b71Sopenharmony_ci        install_images = [ chipset_base_dir ] # Required configuration for installing the battery_config.json file in the vendor directory.
89e41f4b71Sopenharmony_ci        part_name = "product_rk3568"          # Set part_name to product_rk3568 for subsequent build.
90e41f4b71Sopenharmony_ci    }
91e41f4b71Sopenharmony_ci    ```
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci5. 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:
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci    ```json
96e41f4b71Sopenharmony_ci    {
97e41f4b71Sopenharmony_ci        "parts": {
98e41f4b71Sopenharmony_ci            "product_rk3568": {
99e41f4b71Sopenharmony_ci                "module_list": [
100e41f4b71Sopenharmony_ci                    "//vendor/hihope/rk3568/default_app_config:default_app_config",
101e41f4b71Sopenharmony_ci                    "//vendor/hihope/rk3568/image_conf:custom_image_conf",
102e41f4b71Sopenharmony_ci                    "//vendor/hihope/rk3568/preinstall-config:preinstall-config",
103e41f4b71Sopenharmony_ci                    "//vendor/hihope/rk3568/resourceschedule:resourceschedule",
104e41f4b71Sopenharmony_ci                    "//vendor/hihope/rk3568/etc:product_etc_conf",
105e41f4b71Sopenharmony_ci                    "//vendor/hihope/rk3568/power_manager/profile:wakeup_action_config" // Add the configuration for building of wakeup_action_config.
106e41f4b71Sopenharmony_ci                ]
107e41f4b71Sopenharmony_ci            }
108e41f4b71Sopenharmony_ci        },
109e41f4b71Sopenharmony_ci        "subsystem": "product_hihope"
110e41f4b71Sopenharmony_ci    }
111e41f4b71Sopenharmony_ci    ```
112e41f4b71Sopenharmony_ci    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.
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci6. Build the customized version by referring to [Quick Start](../quick-start/quickstart-overview.md).
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci    ```shell
117e41f4b71Sopenharmony_ci    ./build.sh --product-name rk3568 --ccache
118e41f4b71Sopenharmony_ci    ```
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci7. Burn the customized version to the DAYU200 development board.
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci### Debugging and Verification
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci1. Customize power wakeup action sources in the new configuration file.
125e41f4b71Sopenharmony_ci    ```json
126e41f4b71Sopenharmony_ci    {
127e41f4b71Sopenharmony_ci        "53": {
128e41f4b71Sopenharmony_ci            "scene": "LowCapacity",
129e41f4b71Sopenharmony_ci            "action": 2,
130e41f4b71Sopenharmony_ci            "description": "(such as)53 is a uniquely wakeup reason by reading node through HDI interface(GetWakeupReason)"
131e41f4b71Sopenharmony_ci        }
132e41f4b71Sopenharmony_ci    }
133e41f4b71Sopenharmony_ci    ```
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci2. 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.
136e41f4b71Sopenharmony_ci    ```
137e41f4b71Sopenharmony_ci    power_manager_feature_wakeup_action = true
138e41f4b71Sopenharmony_ci    ```
139e41f4b71Sopenharmony_ci    
140e41f4b71Sopenharmony_ci3. 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:
141e41f4b71Sopenharmony_ci    ```json
142e41f4b71Sopenharmony_ci    "charge_scene": {
143e41f4b71Sopenharmony_ci        "low_battery_thers": {
144e41f4b71Sopenharmony_ci          "set": {
145e41f4b71Sopenharmony_ci            "path": "xxx"
146e41f4b71Sopenharmony_ci          }
147e41f4b71Sopenharmony_ci        }
148e41f4b71Sopenharmony_ci    }
149e41f4b71Sopenharmony_ci    ```
150e41f4b71Sopenharmony_ci    In the preceding configuration, **path** indicates the path of the node that stores the low battery threshold.
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci4. 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.
153e41f4b71Sopenharmony_ci    ```
154e41f4b71Sopenharmony_ci    battery_manager_feature_set_low_capacity_threshold = true
155e41f4b71Sopenharmony_ci    ```    
156e41f4b71Sopenharmony_ci5. 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:
157e41f4b71Sopenharmony_ci    ```json
158e41f4b71Sopenharmony_ci    {
159e41f4b71Sopenharmony_ci        "scene" :{
160e41f4b71Sopenharmony_ci            "wakeuo_cause": {
161e41f4b71Sopenharmony_ci                "get": {
162e41f4b71Sopenharmony_ci                    "path": "yyy"
163e41f4b71Sopenharmony_ci                }
164e41f4b71Sopenharmony_ci            }
165e41f4b71Sopenharmony_ci        }
166e41f4b71Sopenharmony_ci    }
167e41f4b71Sopenharmony_ci    ```
168e41f4b71Sopenharmony_ci    In the preceding configuration, **get** indicates the path of the node that stores the low battery wakeup reason.
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci5. 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.
171e41f4b71Sopenharmony_ci    ```
172e41f4b71Sopenharmony_ci    drivers_peripheral_power_wakeup_cause_path = true
173e41f4b71Sopenharmony_ci    ```
174e41f4b71Sopenharmony_ci6. 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.
175e41f4b71Sopenharmony_ci    ```
176e41f4b71Sopenharmony_ci    "chown system system xxx",
177e41f4b71Sopenharmony_ci    "chown system system yyy",
178e41f4b71Sopenharmony_ci    ```
179e41f4b71Sopenharmony_ci7. Decrease the battery level to fall below the specified threshold when the system is in the sleep state.
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci    The device is shut down.
182