This commit is contained in:
aZholtikov committed 2026-10-31 09:25:15 +03:00
1 parent 30bbbc9303
commit 0daf4de19f
3 files changed
+101 -143

No files matched your search

+19 -15
View File
@@ -68,7 +68,6 @@ static const char *TAG = "NRF_GW";
#define NVS_NAMESPACE "zh_gw"
#define NVS_KEY_BINDINGS "bindings"
/* Valid id range for allocation */
#define BINDING_ID_MIN 1
#define BINDING_ID_MAX 254
@@ -205,14 +204,13 @@ static esp_err_t nrf_irq_init(void)
/* ============================================================
* NVS BINDING TABLE
* ============================================================ */
static esp_err_t bindings_load(void)
{
nvs_handle_t h;
esp_err_t err = nvs_open(NVS_NAMESPACE, NVS_READONLY, &h);
if (err != ESP_OK) {
memset(&s_ctx.bindings, 0, sizeof(s_ctx.bindings));
return ESP_OK; /* empty table on first run */
return ESP_OK;
}
size_t size = sizeof(s_ctx.bindings);
@@ -253,7 +251,6 @@ static esp_err_t bindings_save(void)
return err;
}
/* Find first free id in [BINDING_ID_MIN, BINDING_ID_MAX] for @p type. */
static uint8_t bindings_alloc_id(uint8_t type)
{
for (uint16_t id = BINDING_ID_MIN; id <= BINDING_ID_MAX; id++) {
@@ -299,7 +296,6 @@ static esp_err_t bindings_delete(uint8_t id, uint8_t type)
/* ============================================================
* BINDING TABLE PUBLIC API
* ============================================================ */
size_t zh_nrf_gateway_bindings_get(zh_nrf_gateway_binding_t *buf, size_t max_count)
{
if (!buf || max_count == 0 || !s_ctx.bindings_mutex) return 0;
@@ -338,8 +334,6 @@ bool zh_nrf_gateway_binding_exists(uint8_t id, uint8_t type)
esp_err_t zh_nrf_gateway_binding_delete(uint8_t id, uint8_t type)
{
if (!s_ctx.initialized) return ESP_ERR_INVALID_STATE;
/* Argument validation */
if (id == 0 || id == 0xFF) return ESP_ERR_INVALID_ARG;
if (zh_get_nrf24_sensor_payload_size(type) == 0) return ESP_ERR_INVALID_ARG;
@@ -382,11 +376,8 @@ int zh_nrf_gateway_bindings_delete_by_id(uint8_t id)
xSemaphoreGive(s_ctx.bindings_mutex);
if (err != ESP_OK) return -(int)err;
if (deleted > 0) {
ESP_LOGI(TAG, "Deleted %d binding(s) with id=%u", deleted, id);
} else {
ESP_LOGW(TAG, "No bindings with id=%u", id);
}
if (deleted > 0) ESP_LOGI(TAG, "Deleted %d binding(s) with id=%u", deleted, id);
else ESP_LOGW(TAG, "No bindings with id=%u", id);
return deleted;
}
@@ -491,7 +482,6 @@ static bool send_pairing_ack(uint8_t req_type, uint16_t token, uint8_t new_id)
/* ============================================================
* ОБРАБОТКА ПАКЕТОВ
* ============================================================ */
static void handle_pairing_request(const uint8_t *data)
{
uint8_t req_type = data[0];
@@ -533,8 +523,22 @@ static void handle_pairing_request(const uint8_t *data)
xSemaphoreTake(s_ctx.bindings_mutex, portMAX_DELAY);
bindings_delete(new_id, req_type);
xSemaphoreGive(s_ctx.bindings_mutex);
} else {
ESP_LOGI(TAG, "Pairing complete: id=%u type=%u", new_id, req_type);
return;
}
/* --- ACK delivered → binding is final. Notify the application. --- */
ESP_LOGI(TAG, "Pairing complete: id=%u type=%u", new_id, req_type);
zh_nrf_gateway_binding_t paired = {
.id = new_id,
.type = req_type,
};
esp_err_t post_err = esp_event_post(ZH_NRF_GATEWAY_EVENTS,
ZH_NRF_GATEWAY_EVENT_SENSOR_PAIRED,
&paired, sizeof(paired),
pdMS_TO_TICKS(100));
if (post_err != ESP_OK) {
ESP_LOGW(TAG, "esp_event_post(paired) failed: %s", esp_err_to_name(post_err));
}
}