1e41f4b71Sopenharmony_ci# uevent Customization 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### Introduction 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciBy default, OpenHarmony is designed to take an action based on the uevent received by the battery management system. To address your diverse demands, OpenHarmony provides the uevent customization function, allowing you to customize the uevents for taking desired actions. The customization is simply done through modification of the configuration file. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## How to Develop 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci### Setting Up the Environment 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci**Hardware requirements:** 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciDevelopment board running the standard system, for example, the DAYU200 or Hi3516D V300 open source suite. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**Environment requirements:** 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciFor details about the requirements on the Linux environment, see [Quick Start](../quick-start/quickstart-overview.md). 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci### Getting Started with Development 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciThe following uses [DAYU200](https://gitee.com/openharmony/vendor_hihope/tree/master/rk3568) as an example to illustrate uevent customization. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci1. Modify the `battery_config.json` file in the battery service configuration folder of the [HDI layer](https://gitee.com/openharmony/drivers_peripheral/tree/master/battery/interfaces/hdi_service/profile) and the [service layer](https://gitee.com/openharmony/powermgr_battery_manager/tree/master/services/native/profile). The uevent configuration is as follows: 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci ```json 28e41f4b71Sopenharmony_ci { 29e41f4b71Sopenharmony_ci "uevent": { 30e41f4b71Sopenharmony_ci "SUBSYSTEM=hw_power": { 31e41f4b71Sopenharmony_ci "BATTERY_UNDER_VOLTAGE=1": "shutdown", 32e41f4b71Sopenharmony_ci "BATTERY_UNDER_CURRENT=2": "reboot", 33e41f4b71Sopenharmony_ci "BATTERY_UNDER_VOLTAGE=3": "sendcommonevent" 34e41f4b71Sopenharmony_ci } 35e41f4b71Sopenharmony_ci } 36e41f4b71Sopenharmony_ci } 37e41f4b71Sopenharmony_ci ``` 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci **Table 1** Subsystem configuration 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci | **Subsystem**| Description| 42e41f4b71Sopenharmony_ci | -------- | -------- | 43e41f4b71Sopenharmony_ci | SUBSYSTEM=hw_power | Subsystem: **hw_power**| 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci **Table 2** uevent description 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci | uevent| Description| 48e41f4b71Sopenharmony_ci | -------- | -------- | 49e41f4b71Sopenharmony_ci | BATTERY_UNDER_VOLTAGE=1 | uevent (regular expression supported) indicating that the battery voltage is less than 1.| 50e41f4b71Sopenharmony_ci | BATTERY_UNDER_CURRENT=2 | uevent (regular expression supported) indicating that the battery voltage is less than 2.| 51e41f4b71Sopenharmony_ci | BATTERY_UNDER_VOLTAGE=3 | uevent (regular expression supported) indicating that the battery voltage is less than 3.| 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci **Table 3** Action description 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci | Action| Type| Description| 56e41f4b71Sopenharmony_ci | -------- | -------- | -------- | 57e41f4b71Sopenharmony_ci | shutdown | string | Shut down the system.| 58e41f4b71Sopenharmony_ci | reboot | string | Restart the system.| 59e41f4b71Sopenharmony_ci | sendcommonevent | string | Send broadcast.| 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci2. Build the customized version by referring to [Quick Start](../quick-start/quickstart-overview.md). 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci ```shell 64e41f4b71Sopenharmony_ci ./build.sh --product-name rk3568 --ccache 65e41f4b71Sopenharmony_ci ``` 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci3. Burn the customized version to the DAYU200 development board. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci### Debugging and Verification 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci1. Change the uevent receiving code (https://gitee.com/openharmony/drivers_peripheral/blob/master/battery/interfaces/hdi_service/src/battery_thread.cpp) to forcibly receive the uevent event BATTERY_UNDER_VOLTAGE=1 in the configuration file. 72e41f4b71Sopenharmony_ci ```c++ 73e41f4b71Sopenharmony_ci void BatteryThread::UeventCallback(void* service) 74e41f4b71Sopenharmony_ci { 75e41f4b71Sopenharmony_ci char msg[UEVENT_MSG_LEN + UEVENT_RESERVED_SIZE] = { 0 }; 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci ssize_t len = recv(ueventFd_, msg, UEVENT_MSG_LEN, 0); 78e41f4b71Sopenharmony_ci if (len < 0 || len >= UEVENT_MSG_LEN) { 79e41f4b71Sopenharmony_ci BATTERY_HILOGI(COMP_HDI, "recv return msg is invalid, len: %{public}zd", len); 80e41f4b71Sopenharmony_ci return; 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci // msg separator 84e41f4b71Sopenharmony_ci msg[len] = '\0'; 85e41f4b71Sopenharmony_ci msg[len + 1] = '\0'; 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci std::string powerUevent; 88e41f4b71Sopenharmony_ci if (!MatchPowerUevent(msg, powerUevent)) { 89e41f4b71Sopenharmony_ci return; 90e41f4b71Sopenharmony_ci } 91e41f4b71Sopenharmony_ci powerUevent = "BATTERY_UNDER_VOLTAGE=1"; 92e41f4b71Sopenharmony_ci BATTERY_HILOGI(FEATURE_BATT_INFO, "PowerUevent msg:%{public}s", 93e41f4b71Sopenharmony_ci powerUevent.c_str()); 94e41f4b71Sopenharmony_ci UpdateBatteryInfo(service, powerUevent); 95e41f4b71Sopenharmony_ci } 96e41f4b71Sopenharmony_ci ``` 97e41f4b71Sopenharmony_ci2. Build the customized version by referring to [Quick Start](../quick-start/quickstart-overview.md). 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci ```shell 100e41f4b71Sopenharmony_ci ./build.sh --product-name rk3568 --ccache 101e41f4b71Sopenharmony_ci ``` 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci3. Burn the customized version to the DAYU200 development board. 104e41f4b71Sopenharmony_ci4. Check whether the device is powered off when it receives a uevent indicating that the battery voltage is less than 1. 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci## Reference 107e41f4b71Sopenharmony_ciDuring development, you can refer to the [default uevent configuration](https://gitee.com/openharmony/powermgr_battery_manager/tree/master/services/native/profile/), as shown below: 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciThe default configuration is as follows: 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci```json 112e41f4b71Sopenharmony_ci{ 113e41f4b71Sopenharmony_ci "uevent": { 114e41f4b71Sopenharmony_ci "SUBSYSTEM=hw_power": { 115e41f4b71Sopenharmony_ci "BATTERY_UNDER_VOLTAGE=1": "shutdown", 116e41f4b71Sopenharmony_ci "BATTERY_UNDER_VOLTAGE=2": "reboot", 117e41f4b71Sopenharmony_ci "BATTERY_UNDER_VOLTAGE=3": "sendcommonevent" 118e41f4b71Sopenharmony_ci } 119e41f4b71Sopenharmony_ci } 120e41f4b71Sopenharmony_ci} 121e41f4b71Sopenharmony_ci``` 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ciPacking path: /system/etc/battery 124