feat(client): report UUID and version to server after ECDH handshake

This commit is contained in:
eddy 2026-08-04 22:12:48 +02:00
commit 420db0fcdf

View file

@ -37,7 +37,13 @@ CRGB leds[NUM_LEDS];
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361f26d8"
#define UUID_CHAR_UUID "beb5483e-36e1-4688-b7f5-ea07361f26d9"
#define ECDH_UUID "beb5483e-36e1-4688-b7f5-ea07361f26dc"
#define CLIENT_REGISTRY_UUID "beb5483e-36e1-4688-b7f5-ea07361f26dd"
#define CLIENT_CONTROL_UUID "beb5483e-36e1-4688-b7f5-ea07361f26de"
#define ESP_UUID_LEN 16
#define BROKER_PLAIN_SIZE 20
#define BROKER_PKT_SIZE (4 + 8 + BROKER_PLAIN_SIZE + 16)
#define BROKER_CMD_UPDATE_AVAILABLE 0x10
#define BROKER_CMD_CLIENT_READY 0x20
#define NVS_NAMESPACE "syl"
#define NVS_PSK_NAME "psk"
#define NVS_UUID_NAME "uuid"
@ -321,6 +327,53 @@ static bool connectToServer() {
relay1SendState = false;
relay2SendState = false;
ESP_LOGI(TAG, "BLE: server UUID verified, ECDH done, ready");
NimBLERemoteCharacteristic *pRegChar = pSvc->getCharacteristic(CLIENT_REGISTRY_UUID);
if (pRegChar == nullptr) {
ESP_LOGW(TAG, "BLE: CLIENT_REGISTRY characteristic not found");
} else {
uint8_t plainPayload[BROKER_PLAIN_SIZE];
memset(plainPayload, 0, BROKER_PLAIN_SIZE);
plainPayload[0] = 0x00;
memcpy(plainPayload + 1, deviceUuid, ESP_UUID_LEN);
uint16_t clientVersion = (uint16_t)atoi(FIRMWARE_VERSION_CLIENT);
plainPayload[1 + ESP_UUID_LEN] = (clientVersion >> 8) & 0xFF;
plainPayload[1 + ESP_UUID_LEN + 1] = clientVersion & 0xFF;
uint8_t pkt[BROKER_PKT_SIZE];
memset(pkt, 0, BROKER_PKT_SIZE);
uint32_t s = ++seq;
pkt[0] = (s >> 24) & 0xFF;
pkt[1] = (s >> 16) & 0xFF;
pkt[2] = (s >> 8) & 0xFF;
pkt[3] = s & 0xFF;
for (int i = 0; i < 8; i++) pkt[4 + i] = (uint8_t)(esp_random() & 0xFF);
uint8_t nonce[12];
memcpy(nonce, pkt, 4);
memcpy(nonce + 4, pkt + 4, 8);
mbedtls_gcm_context gcm;
mbedtls_gcm_init(&gcm);
int ret = mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, aesKey, AES_KEY_LEN * 8);
if (ret != 0) {
ESP_LOGE(TAG, "REGISTRY: setkey failed (ret=%d)", ret);
mbedtls_gcm_free(&gcm);
} else {
mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT,
BROKER_PLAIN_SIZE, nonce, 12, NULL, 0,
plainPayload, pkt + 12, 16, pkt + 12 + BROKER_PLAIN_SIZE);
mbedtls_gcm_free(&gcm);
if (pRegChar->writeValue(pkt, BROKER_PKT_SIZE, false)) {
ESP_LOGI(TAG, "REGISTRY: reported uuid + version %d to server", FIRMWARE_VERSION_CLIENT);
} else {
ESP_LOGW(TAG, "REGISTRY: write failed");
}
}
}
return true;
}