diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ecae15c5..ff62b6015 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Added `hw bwm powersave` - show/set the BWM (ESP32) power-save switch: DFS, light sleep and slow advertising after 30 s, or the stock always-on behaviour, persisted on the module (@Msprg) - Added `hw powersave` - PM5 low-power idle, on by default: between commands the core drops to 48 MHz with the PLL off, the FPGA clock stopped and the CPU halted (WFI); USB now runs crystal-less off HICK. Ported from the Fantasi firmware (@Msprg) - Fixed `hf mf autopwn` on FM11RF08S - static encrypted nonce is now detected via the backdoor key (@iceman1001) - Added `hf mfu view -v` - prints the UL-C 3DES key or UL-AES key stored in the dump file (@iceman1001) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 76db985f5..26fe723e9 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -597,11 +597,23 @@ static void SendStatus(uint32_t wait) { { // Read the ESP firmware version so hw status shows what the BWM runs // (and lets you confirm an OTA took: the string flips after a reflash). + // Keep the wait short: the client gives hw status about 2 s in total. One + // retry - a slow light-sleep wake eats the first frame but leaves the module up. uint8_t bwm_ver[64] = {0}; uint16_t bwm_ver_len = sizeof(bwm_ver) - 1; - if (bwm_esp_get_version(bwm_ver, &bwm_ver_len) == PM3_SUCCESS) { + int vres = bwm_esp_get_version(bwm_ver, &bwm_ver_len, 800); + if (vres == PM3_ETIMEOUT) { + bwm_ver_len = sizeof(bwm_ver) - 1; + vres = bwm_esp_get_version(bwm_ver, &bwm_ver_len, 800); + } + if (vres == PM3_SUCCESS) { bwm_ver[bwm_ver_len] = 0x00; Dbprintf(" BWM fw version...... " _YELLOW_("%s"), bwm_ver); + // Only once the ESP answered; older fw lacks these, so keep the wait short. + uint8_t ps = 0; + if (bwm_esp_get_power_save(&ps, 300) == PM3_SUCCESS) { + Dbprintf(" BWM power save...... " _YELLOW_("%s"), ps ? "on" : "off"); + } } else { Dbprintf(" BWM fw version...... " _YELLOW_("%s"), "unknown"); } @@ -4197,7 +4209,7 @@ static void PacketReceived(PacketCommandNG *packet) { // ack is lost). Replies here with a payload, unlike the others. uint8_t ver[64]; uint16_t vlen = sizeof(ver); - res = bwm_esp_get_version(ver, &vlen); + res = bwm_esp_get_version(ver, &vlen, 3000); reply_ng(CMD_PM5_BWM_ESP_OTA, res, ver, (res == PM3_SUCCESS) ? vlen : 0); replied = true; break; @@ -4281,6 +4293,30 @@ static void PacketReceived(PacketCommandNG *packet) { } #else reply_ng(CMD_PM5_BWM_BLE_NAME, PM3_ENOTIMPL, NULL, 0); +#endif + break; + } + case CMD_PM5_BWM_POWERSAVE: { +#ifdef WITH_BWM_FORWARD + // Payload / reply layout: pm3_cmd.h. The ESP persists it; nothing kept here. + if (packet->length < 1) { + reply_ng(CMD_PM5_BWM_POWERSAVE, PM3_EINVARG, NULL, 0); + break; + } + uint8_t action = packet->data.asBytes[0]; + uint8_t state = 0; + int res; + if (action == BWM_POWERSAVE_ACTION_GET) { + res = bwm_esp_get_power_save(&state, 3000); + } else if (action == BWM_POWERSAVE_ACTION_SET && packet->length >= 2) { + res = bwm_esp_set_power_save(packet->data.asBytes[1] != 0, &state); + } else { + reply_ng(CMD_PM5_BWM_POWERSAVE, PM3_EINVARG, NULL, 0); + break; + } + reply_ng(CMD_PM5_BWM_POWERSAVE, res, &state, (res == PM3_SUCCESS) ? 1 : 0); +#else + reply_ng(CMD_PM5_BWM_POWERSAVE, PM3_ENOTIMPL, NULL, 0); #endif break; } diff --git a/armsrc/bwm_wifi.c b/armsrc/bwm_wifi.c index 81d74224f..3fa2b8686 100644 --- a/armsrc/bwm_wifi.c +++ b/armsrc/bwm_wifi.c @@ -313,8 +313,29 @@ int bwm_wifi_forward_down(void) { // boot slot, so those get generous timeouts. // --------------------------------------------------------------------------- // Read the ESP's running firmware version string (APP_CMD_GET_VERSION_INFO). -int bwm_esp_get_version(uint8_t *buf, uint16_t *buflen) { - return bwm_cmd(BWM_CMD_GET_VERSION_INFO, NULL, 0, buf, buflen, 3000); +int bwm_esp_get_version(uint8_t *buf, uint16_t *buflen, uint32_t timeout_ms) { + return bwm_cmd(BWM_CMD_GET_VERSION_INFO, NULL, 0, buf, buflen, timeout_ms); +} + +int bwm_esp_get_power_save(uint8_t *state, uint32_t timeout_ms) { + uint16_t len = 1; + int r = bwm_cmd(BWM_CMD_GET_SYS_POWER_SAVE, NULL, 0, state, &len, timeout_ms); + if (r == PM3_SUCCESS && len < 1) { + r = PM3_EFAILED; + } + return r; +} + +int bwm_esp_set_power_save(bool on, uint8_t *state) { + // Applied at once on the ESP (no reboot) and saved to its NVS; the reply + // carries the state the ESP ended up in. + uint8_t v = on ? 1 : 0; + uint16_t len = 1; + int r = bwm_cmd(BWM_CMD_SET_SYS_POWER_SAVE, &v, 1, state, &len, 3000); + if (r == PM3_SUCCESS && len < 1) { + r = PM3_EFAILED; + } + return r; } int bwm_esp_get_ble_name(uint8_t *buf, uint16_t *buflen) { diff --git a/armsrc/bwm_wifi.h b/armsrc/bwm_wifi.h index 9d7b08a1c..934bb61dd 100644 --- a/armsrc/bwm_wifi.h +++ b/armsrc/bwm_wifi.h @@ -88,7 +88,13 @@ int bwm_wifi_forward_status(uint8_t *state, uint32_t *ip_out); #define BWM_CMD_START_BLE_SPP 4021 // no payload: restore BLE after OTA #define BWM_CMD_SET_BLE_DEVICE_NAME 4002 // req: name bytes #define BWM_CMD_GET_BLE_DEVICE_NAME 4003 // resp: current BLE device name string -int bwm_esp_get_version(uint8_t *buf, uint16_t *buflen); +// ESP power-save switch (DFS + light sleep + low-duty advertising), persisted on +// the ESP. Both return the ESP's applied state in *state. +#define BWM_CMD_SET_SYS_POWER_SAVE 1019 // req: u8 0=off 1=on; resp: u8 applied state +#define BWM_CMD_GET_SYS_POWER_SAVE 1020 // resp: u8 state +int bwm_esp_get_power_save(uint8_t *state, uint32_t timeout_ms); +int bwm_esp_set_power_save(bool on, uint8_t *state); +int bwm_esp_get_version(uint8_t *buf, uint16_t *buflen, uint32_t timeout_ms); int bwm_esp_get_ble_name(uint8_t *buf, uint16_t *buflen); int bwm_esp_set_ble_name(const uint8_t *name, uint16_t len); int bwm_esp_ota_begin(uint32_t total_size); diff --git a/client/src/cmdbwm.c b/client/src/cmdbwm.c index ebbcdfca5..07714cbfc 100644 --- a/client/src/cmdbwm.c +++ b/client/src/cmdbwm.c @@ -697,11 +697,64 @@ static int CmdBwmName(const char *Cmd) { return PM3_SUCCESS; } +static int CmdBwmPowerSave(const char *Cmd) { + // Positional sub-action (no dashes): hw bwm powersave [on | off]; no verb shows the state + char verb[16] = {0}; + sscanf(Cmd, "%15s", verb); + bool on = (strcmp(verb, "on") == 0); + bool off = (strcmp(verb, "off") == 0); + bool show = (verb[0] == 0); + + if (!on && !off && !show) { + // Not a recognised sub-action: render help (also serves -h), or error on + // a stray token, then stop. + CLIParserContext *ctx; + CLIParserInit(&ctx, "hw bwm powersave", + "Show or set the BWM (ESP32) power-save switch, stored on the BWM in NVS.\n" + "Default is " _GREEN_("on") ": the ESP scales its clock down, light-sleeps between\n" + "link traffic, and after 30 s of fast advertising (boot, disconnect) advertises\n" + "once a second. " _YELLOW_("off") " pins it at full clock, no sleep, fast advertising\n" + "throughout. Applies at once and survives reboots. PM5 only.", + "hw bwm powersave --> show the current state\n" + "hw bwm powersave off --> stock always-on behaviour\n" + "hw bwm powersave on --> re-enable power saving"); + void *argtable[] = { + arg_param_begin, + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, true); + CLIParserFree(ctx); + PrintAndLogEx(WARNING, "specify " _YELLOW_("on") ", " _YELLOW_("off") " or nothing to show the state"); + return PM3_EINVARG; + } + + uint8_t payload[2] = { show ? BWM_POWERSAVE_ACTION_GET : BWM_POWERSAVE_ACTION_SET, on ? 1 : 0 }; + clearCommandBuffer(); + SendCommandNG(CMD_PM5_BWM_POWERSAVE, payload, show ? 1 : 2); + PacketResponseNG resp; + if (WaitForResponseTimeout(CMD_PM5_BWM_POWERSAVE, &resp, 5000) == false) { + PrintAndLogEx(WARNING, "command timeout (is this a PM5 with a responsive BWM?)"); + return PM3_ETIMEOUT; + } + if (resp.status == PM3_ENOTIMPL) { + PrintAndLogEx(WARNING, "firmware built without BWM link support"); + return resp.status; + } + if (resp.status != PM3_SUCCESS || resp.length < 1) { + PrintAndLogEx(FAILED, "BWM did not answer the power-save command (BWM firmware too old?)"); + return (resp.status != PM3_SUCCESS) ? resp.status : PM3_EFAILED; + } + bool state = (resp.data.asBytes[0] != 0); + PrintAndLogEx(SUCCESS, "BWM power save..... %s", state ? _GREEN_("on") : _YELLOW_("off")); + return PM3_SUCCESS; +} + static command_t BwmCommandTable[] = { - {"help", CmdHelpBwm, IfBwm, "This help"}, + {"help", CmdHelpBwm, AlwaysAvailable, "This help"}, {"autooff", CmdBwmAutoOff, IfBwm, "Toggle auto power-off on USB unplug"}, {"charge", CmdBwmCharge, IfBwm, "Enable/disable battery charging (one-shot)"}, {"name", CmdBwmName, IfBwm, "Get/set the BWM BLE advertising name"}, + {"powersave", CmdBwmPowerSave, IfBwm, "Show/set the BWM power-save switch (DFS, light sleep, slow adv)"}, {"setcap", CmdBwmSetCap, IfBwm, "Set fuel-gauge design capacity (run once after battery change)"}, {"upgrade", CmdBWMUpgrade, IfBwm, "Reflash BWM (ESP32) firmware over the BWM link, no header"}, {"vchg", CmdBwmVchg, IfBwm, "Set charger charge-voltage target (default 4100 mV)"}, diff --git a/doc/commands.json b/doc/commands.json index 62c0d4381..66197e779 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -11045,12 +11045,12 @@ }, "hw bwm help": { "command": "hw bwm help", - "description": "--------------------------------------------------------------------------------------- hw bwm autooff available offline: no Toggle automatic power-off when the PM5 is unplugged from USB (BWM only). Default is on. When on, the board powers itself down ~10s after USB is removed, so a BWM-equipped PM5 doesn't silently drain the battery. Button power-on is unaffected. Disable for standalone/BLE use on battery. Runtime only: resets to on at each boot.", + "description": "help This help --------------------------------------------------------------------------------------- hw bwm autooff available offline: no Toggle automatic power-off when the PM5 is unplugged from USB (BWM only). Default is on. When on, the board powers itself down ~10s after USB is removed, so a BWM-equipped PM5 doesn't silently drain the battery. Button power-on is unaffected. Disable for standalone/BLE use on battery. Runtime only: resets to on at each boot.", "notes": [ "hw bwm autooff off -> disable auto power-off", "hw bwm autooff on -> re-enable auto power-off" ], - "offline": false, + "offline": true, "options": [ "-h, --help This help" ], @@ -11070,6 +11070,20 @@ ], "usage": "hw bwm name [-h] [--set ]" }, + "hw bwm powersave": { + "command": "hw bwm powersave", + "description": "Show or set the BWM (ESP32) power-save switch, stored on the BWM in NVS. Default is on: the ESP scales its clock down, light-sleeps between link traffic, and after 30 s of fast advertising (boot, disconnect) advertises once a second. off pins it at full clock, no sleep, fast advertising throughout. Applies at once and survives reboots. PM5 only.", + "notes": [ + "hw bwm powersave -> show the current state", + "hw bwm powersave off -> stock always-on behaviour", + "hw bwm powersave on -> re-enable power saving" + ], + "offline": false, + "options": [ + "-h, --help This help" + ], + "usage": "hw bwm powersave [-h]" + }, "hw bwm setcap": { "command": "hw bwm setcap", "description": "Program the BWM fuel gauge (BQ27427) Design Capacity for the fitted cell. Run ONCE after fitting or replacing the battery. This triggers a gauge config-update; do not run it repeatedly, as that disrupts the Impedance Track learning cycle. PM5 only.", @@ -16664,8 +16678,8 @@ } }, "metadata": { - "commands_extracted": 927, + "commands_extracted": 928, "extracted_by": "PM3Help2JSON v1.00", - "extracted_on": "2026-09-24T08:29:44+00:00" + "extracted_on": "2026-09-24T08:30:13+00:00" } } diff --git a/doc/commands.md b/doc/commands.md index 1bd8490aa..690ab782a 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -1047,10 +1047,11 @@ Check column "offline" for their availability. |command |offline |description |------- |------- |----------- -|`hw bwm help `|N |`This help` +|`hw bwm help `|Y |`This help` |`hw bwm autooff `|N |`Toggle auto power-off on USB unplug` |`hw bwm charge `|N |`Enable/disable battery charging (one-shot)` |`hw bwm name `|N |`Get/set the BWM BLE advertising name` +|`hw bwm powersave `|N |`Show/set the BWM power-save switch (DFS, light sleep, slow adv)` |`hw bwm setcap `|N |`Set fuel-gauge design capacity (run once after battery change)` |`hw bwm upgrade `|N |`Reflash BWM (ESP32) firmware over the BWM link, no header` |`hw bwm vchg `|N |`Set charger charge-voltage target (default 4100 mV)` diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index ed48c11e8..0de708f5f 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -926,6 +926,11 @@ typedef struct { #define BWM_BLE_NAME_MAX_LEN 15 // usable chars; ESP name buffer is 16 incl NUL // PM5, toggle the power-save idle (48 MHz + WFI when idle). Used by `hw powersave`. #define CMD_PM5_POWERSAVE 0x0180 +// PM5, BWM power-save switch on the ESP (DFS, light sleep, low-duty advertising; +// persisted on the BWM). Used by `hw bwm powersave`. +#define CMD_PM5_BWM_POWERSAVE 0x0181 // payload: [action:u8][state:u8 if SET]; resp: u8 applied state +#define BWM_POWERSAVE_ACTION_GET 0x00 +#define BWM_POWERSAVE_ACTION_SET 0x01 #define BWM_OTA_ACTION_BEGIN 0x00 #define BWM_OTA_ACTION_WRITE 0x01 #define BWM_OTA_ACTION_END 0x02