Merge pull request #3620 from nieldk/master

Add BWM set BLE name
This commit is contained in:
Iceman authored and GitHub committed 2026-09-12 19:03:24 +07:00
commit a7c6116258
6 files changed
+141

No files matched your search

+38
View File
@@ -4093,6 +4093,44 @@ static void PacketReceived(PacketCommandNG *packet) {
}
#else
reply_ng(CMD_PM5_BWM_ESP_OTA, PM3_ENOTIMPL, NULL, 0);
#endif
break;
}
case CMD_PM5_BWM_BLE_NAME: {
#ifdef WITH_BWM_FORWARD
// Payload: [action:u8][name bytes if SET]. GET replies with the current
// BLE device name string. SET stores it on the ESP (persisted to NVS) and
// then reboots the ESP so the new advertising name takes effect - the ESP
// only applies the name at BLE startup, and rebooting is easier for users
// than a physical power-cycle.
if (packet->length < 1) {
reply_ng(CMD_PM5_BWM_BLE_NAME, PM3_EINVARG, NULL, 0);
break;
}
uint8_t action = packet->data.asBytes[0];
if (action == BWM_BLE_NAME_ACTION_GET) {
uint8_t name[BWM_BLE_NAME_MAX_LEN + 1] = {0};
uint16_t nlen = sizeof(name);
int res = bwm_esp_get_ble_name(name, &nlen);
reply_ng(CMD_PM5_BWM_BLE_NAME, res, name, (res == PM3_SUCCESS) ? nlen : 0);
} else if (action == BWM_BLE_NAME_ACTION_SET) {
uint16_t nlen = packet->length - 1;
if (nlen == 0 || nlen > BWM_BLE_NAME_MAX_LEN) {
reply_ng(CMD_PM5_BWM_BLE_NAME, PM3_EINVARG, NULL, 0);
break;
}
int res = bwm_esp_set_ble_name(packet->data.asBytes + 1, nlen);
if (res == PM3_SUCCESS) {
// Best-effort: the name is already saved to NVS, so even if the
// reboot ack is lost it applies on the next BLE startup anyway.
(void)bwm_esp_reboot();
}
reply_ng(CMD_PM5_BWM_BLE_NAME, res, NULL, 0);
} else {
reply_ng(CMD_PM5_BWM_BLE_NAME, PM3_EINVARG, NULL, 0);
}
#else
reply_ng(CMD_PM5_BWM_BLE_NAME, PM3_ENOTIMPL, NULL, 0);
#endif
break;
}
+10
View File
@@ -317,6 +317,16 @@ 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_ble_name(uint8_t *buf, uint16_t *buflen) {
return bwm_cmd(BWM_CMD_GET_BLE_DEVICE_NAME, NULL, 0, buf, buflen, 3000);
}
int bwm_esp_set_ble_name(const uint8_t *name, uint16_t len) {
// The ESP validates length and stores the name in NVS; it applies at BLE
// startup, so the caller reboots the ESP afterwards to make it take effect.
return bwm_cmd(BWM_CMD_SET_BLE_DEVICE_NAME, name, len, NULL, NULL, 3000);
}
int bwm_esp_ota_begin(uint32_t total_size) {
// SILENCE ESP log forwarding for the OTA. With it on, the ESP's background
// log broadcasts (WiFi/coex/BLE) interleave with the per-chunk acks across
+4
View File
@@ -86,7 +86,11 @@ int bwm_wifi_forward_status(uint8_t *state, uint32_t *ip_out);
#define BWM_CMD_GET_VERSION_INFO 1000 // resp: running firmware version string
#define BWM_CMD_STOP_BLE_SPP 4022 // no payload: stop BLE during OTA (flash contention)
#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);
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);
int bwm_esp_ota_write(const uint8_t *data, uint16_t len);
int bwm_esp_ota_end(void);
+60
View File
@@ -2643,10 +2643,70 @@ static int CmdBWMUpgrade(const char *Cmd) {
}
static int CmdHelpBwm(const char *Cmd);
static int CmdBwmName(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hw bwm name",
"Get or set the BWM BLE advertising name (stored on the BWM, in NVS).\n"
"With no --set, prints the current name. Setting a name stores it and reboots\n"
"the BWM to apply it - this briefly drops a BLE/WiFi connection; reconnect after\n"
"a few seconds. Over USB the reboot is not noticeable.",
"hw bwm name --> show current BLE name\n"
"hw bwm name --set MyPM5 --> set BLE name to 'MyPM5'");
void *argtable[] = {
arg_param_begin,
arg_str0(NULL, "set", "<name>", "new BLE name (1-31 chars); omit to read current name"),
arg_param_end,
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
uint8_t name[64] = {0};
int nlen = sizeof(name) - 1; // CLIGetStrWithReturn does not guarantee NUL-termination
CLIGetStrWithReturn(ctx, 1, name, &nlen);
CLIParserFree(ctx);
if (nlen == 0) {
// GET
uint8_t a[1] = { BWM_BLE_NAME_ACTION_GET };
clearCommandBuffer();
SendCommandNG(CMD_PM5_BWM_BLE_NAME, a, sizeof(a));
PacketResponseNG r;
if ((WaitForResponseTimeout(CMD_PM5_BWM_BLE_NAME, &r, 5000) == false) || (r.status != PM3_SUCCESS)) {
PrintAndLogEx(FAILED, "failed to read BWM BLE name (is a responsive BWM fitted?)");
return PM3_EFAILED;
}
char out[BWM_BLE_NAME_MAX_LEN + 1] = {0};
uint16_t n = (r.length < sizeof(out) - 1) ? r.length : (uint16_t)(sizeof(out) - 1);
memcpy(out, r.data.asBytes, n);
PrintAndLogEx(SUCCESS, "BWM BLE name..... " _YELLOW_("%s"), out);
return PM3_SUCCESS;
}
// SET
if (nlen > BWM_BLE_NAME_MAX_LEN) {
PrintAndLogEx(FAILED, "name too long: %d chars (max " _YELLOW_("%d") ")", nlen, BWM_BLE_NAME_MAX_LEN);
return PM3_EINVARG;
}
uint8_t payload[1 + BWM_BLE_NAME_MAX_LEN];
payload[0] = BWM_BLE_NAME_ACTION_SET;
memcpy(payload + 1, name, nlen);
clearCommandBuffer();
SendCommandNG(CMD_PM5_BWM_BLE_NAME, payload, (uint16_t)(1 + nlen));
PacketResponseNG r;
if ((WaitForResponseTimeout(CMD_PM5_BWM_BLE_NAME, &r, 5000) == false) || (r.status != PM3_SUCCESS)) {
PrintAndLogEx(FAILED, "failed to set BWM BLE name (is a responsive BWM fitted?)");
return PM3_EFAILED;
}
PrintAndLogEx(SUCCESS, "BWM BLE name set to " _YELLOW_("%s"), name);
PrintAndLogEx(INFO, "BWM is rebooting to apply the new name...");
PrintAndLogEx(HINT, "If you are connected over BLE or WiFi the link will drop briefly - reconnect after a few seconds");
return PM3_SUCCESS;
}
static command_t BwmCommandTable[] = {
{"help", CmdHelpBwm, AlwaysAvailable, "This help"},
{"autooff", CmdBwmAutoOff, IfPm5, "Toggle auto power-off on USB unplug"},
{"charge", CmdBwmCharge, IfPm5, "Enable/disable battery charging (one-shot)"},
{"name", CmdBwmName, IfPm5, "Get/set the BWM BLE advertising name"},
{"setcap", CmdBwmSetCap, IfPm5, "Set fuel-gauge design capacity (run once after battery change)"},
{"upgrade", CmdBWMUpgrade, IfPm5, "Reflash BWM (ESP32) firmware over the BWM link, no header"},
{"vchg", CmdBwmVchg, IfPm5, "Set charger charge-voltage target (default 4100 mV)"},
+25
View File
@@ -158,6 +158,31 @@ There are three ways to connect: a **native transport** (Linux), a **cross-platf
Python bridge** (Linux / macOS / Windows / WSL / iOS), and a **bridge app** on Android
([2.4](#24-android--termux--ble-bridge-app)).
### `hw bwm name` — get/set the BLE advertising name
Reads or changes the name the BWM advertises over BLE. With no argument it prints
the current name; `--set` changes it (1–31 characters).
| Argument | Description |
| --------------- | ---------------------------------------------------- |
| `--set <name>` | New BLE name, 1–31 chars; omit to read current name |
```
hw bwm name # show the current BLE name
hw bwm name --set MyPM5 # set the BLE name to 'MyPM5'
```
The name is stored on the BWM in NVS, so it persists across power cycles. The BWM
applies the advertised name at BLE start-up, so setting a name reboots the BWM to
make it take effect — this briefly drops a BLE or WiFi connection, so reconnect
after a few seconds. Over USB the reboot is not noticeable, since the client stays
connected to the PM5 while only the BWM's ESP restarts.
> [!NOTE]
> If a device is connected over BLE when you change the name, the running
> advertisement can't change mid-connection; the new name shows up on the next
> scan after the reboot.
### 2.1 Native BLE transport — Linux only, no bridge
Uses a raw L2CAP/ATT socket — no `bleak`, no `bluetoothd` daemon dependency.
+4
View File
@@ -895,6 +895,10 @@ typedef struct {
#define CMD_PM5_BWM_WIFI 0x017C
#define CMD_PM5_BWM_SET_VCHG 0x017D
#define CMD_PM5_BWM_ESP_OTA 0x017E
#define CMD_PM5_BWM_BLE_NAME 0x017F // payload: [action:u8][name bytes if SET]
#define BWM_BLE_NAME_ACTION_GET 0x00 // resp: current BLE device name string
#define BWM_BLE_NAME_ACTION_SET 0x01 // req: name bytes follow the action byte; BWM reboots to apply
#define BWM_BLE_NAME_MAX_LEN 31 // usable chars; ESP name buffer is 32 incl NUL
#define BWM_OTA_ACTION_BEGIN 0x00
#define BWM_OTA_ACTION_WRITE 0x01
#define BWM_OTA_ACTION_END 0x02