From e7696442fda2da9377a89e8b801c0e250d4efa9f Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Thu, 31 Oct 2024 08:50:36 -0300 Subject: [PATCH 1/2] refact: move ciot_ble_scn_handle_adv_report to ciot_ble_scn_base.c --- include/ciot_ble_scn.h | 1 + src/common/ciot_ble_scn_base.c | 47 +++++++++++++++++++++++++ src/nrf/ciot_ble_scn.c | 64 ++++++++++++++-------------------- 3 files changed, 74 insertions(+), 38 deletions(-) diff --git a/include/ciot_ble_scn.h b/include/ciot_ble_scn.h index 7786c10..d92a149 100644 --- a/include/ciot_ble_scn.h +++ b/include/ciot_ble_scn.h @@ -79,6 +79,7 @@ ciot_err_t ciot_ble_scn_get_cfg(ciot_ble_scn_t self, ciot_ble_scn_cfg_t *cfg); ciot_err_t ciot_ble_scn_get_status(ciot_ble_scn_t self, ciot_ble_scn_status_t *status); ciot_err_t ciot_ble_scn_task(ciot_ble_scn_t self); ciot_err_t ciot_ble_scn_base_task(ciot_ble_scn_t self); +void ciot_ble_scn_handle_adv_report(ciot_ble_scn_t self, ciot_ble_scn_adv_t *adv); ciot_err_t ciot_ble_scn_handle_event(ciot_ble_scn_t self, void *event, void *event_args); ciot_err_t ciot_ble_scn_set_filter(ciot_ble_scn_t self, ciot_ble_scn_filter_fn *filter, void *args); diff --git a/src/common/ciot_ble_scn_base.c b/src/common/ciot_ble_scn_base.c index 465866e..4973a32 100644 --- a/src/common/ciot_ble_scn_base.c +++ b/src/common/ciot_ble_scn_base.c @@ -10,6 +10,7 @@ */ #include +#include #include "ciot_ble_scn.h" #include "ciot_config.h" @@ -158,6 +159,52 @@ ciot_err_t ciot_ble_scn_base_task(ciot_ble_scn_t self) return CIOT__ERR__OK; } +void ciot_ble_scn_handle_adv_report(ciot_ble_scn_t self, ciot_ble_scn_adv_t *adv) +{ + ciot_ble_scn_base_t *base = (ciot_ble_scn_base_t*)self; +#ifdef CIOT_CONFIG_BLE_SCN_ADV_FIFO_SIZE + ciot_ble_scn_adv_fifo_t *adv_fifo = &base->adv_fifo; + if(adv_fifo->list[adv_fifo->wp].info->rssi == 0) + { + adv_fifo->list[adv_fifo->wp].info->rssi = adv->info->rssi; + memcpy(adv_fifo->list[adv_fifo->wp].info->mac.data, adv->info->mac.data, adv->info->mac.len); + adv_fifo->list[adv_fifo->wp].info->mac.len = adv->info->mac.len; + memcpy(adv_fifo->list[adv_fifo->wp].payload.data, adv->payload.data, adv->payload.len); + adv_fifo->list[adv_fifo->wp].payload.len = adv->payload.len; + adv_fifo->wp++; + base->status.fifo_len++; + if(base->status.fifo_len > base->status.fifo_max) + { + base->status.fifo_max = base->status.fifo_len; + } + if(adv_fifo->wp == CIOT_CONFIG_BLE_SCN_ADV_FIFO_SIZE) + { + adv_fifo->wp = 0; + } + } + else + { + adv_fifo->rp = adv_fifo->wp; + base->status.advs_losted++; + base->status.err_code = CIOT__ERR__DATA_LOSS; + if(base->status.fifo_len == 0) + { + base->status.err_code = CIOT__ERR__INVALID_SIZE; + for (size_t i = 0; i < BOARD_BLE_SCN_ADV_FIFO_SIZE; i++) + { + if(adv_fifo->list[i].info->rssi != 0) base->status.fifo_len++; + } + } + } +#else + ciot_iface_event_t event = {0}; + event.type = CIOT_IFACE_EVENT_DATA; + event.data = (uint8_t*)adv; + ciot_iface_send_event(&base->iface, &event); +#endif +} + + ciot_err_t ciot_ble_scn_set_filter(ciot_ble_scn_t self, ciot_ble_scn_filter_fn *filter, void *args) { CIOT_ERR_NULL_CHECK(self); diff --git a/src/nrf/ciot_ble_scn.c b/src/nrf/ciot_ble_scn.c index 66b0978..43ae3b0 100644 --- a/src/nrf/ciot_ble_scn.c +++ b/src/nrf/ciot_ble_scn.c @@ -37,7 +37,7 @@ struct ciot_ble_scn }; static void ciot_ble_scn_copy_mac(uint8_t destiny[6], uint8_t source[6], bool reverse); -static void ciot_ble_scn_handle_adv_report(ciot_ble_scn_t self, ciot_ble_scn_adv_t *adv); +static ciot_err_t ciot_ble_scn_get_error(uint32_t nrf_error); // static const char *TAG = "hg_ble_scn"; @@ -77,7 +77,7 @@ ciot_err_t ciot_ble_scn_start(ciot_ble_scn_t self, ciot_ble_scn_cfg_t *cfg) self->scan_params.timeout = base->cfg.timeout; #if NRF_SD_BLE_API_VERSION == 2 || NRF_SD_BLE_API_VERSION == 3 - error = sd_ble_gap_scan_start(&self->scan_params); + err = sd_ble_gap_scan_start(&self->scan_params); #else self->scan_buffer.p_data = self->buffer; self->scan_buffer.len = BLE_GAP_SCAN_BUFFER_MIN; @@ -95,7 +95,7 @@ ciot_err_t ciot_ble_scn_start(ciot_ble_scn_t self, ciot_ble_scn_cfg_t *cfg) ciot_iface_send_event(&base->iface, &iface_event); } - base->status.err_code = err; + base->status.err_code = ciot_ble_scn_get_error(err); return err; } @@ -144,7 +144,10 @@ ciot_err_t ciot_ble_scn_handle_event(ciot_ble_scn_t self, void *event, void *eve #else base->recv.payload.data = ev->evt.gap_evt.params.adv_report.data.p_data; base->recv.payload.len = ev->evt.gap_evt.params.adv_report.data.len; - base->status.err_code = sd_ble_gap_scan_start(NULL, &self->scan_buffer); + uint32_t error = sd_ble_gap_scan_start(NULL, &self->scan_buffer); + if(error) { + base->status.err_code = ciot_ble_scn_get_error(error); + } #endif if (base->filter.handler == NULL || base->filter.handler(self, &base->recv, base->filter.args)) { @@ -157,46 +160,31 @@ ciot_err_t ciot_ble_scn_handle_event(ciot_ble_scn_t self, void *event, void *eve return CIOT__ERR__OK; } -static void ciot_ble_scn_copy_mac(uint8_t destiny[6], uint8_t source[6], bool reverse) +static ciot_err_t ciot_ble_scn_get_error(uint32_t nrf_error) { - for (size_t i = 0; i < 6; i++) + switch (nrf_error) { - destiny[i] = reverse ? source[5 - i] : source[i]; + case NRF_ERROR_INVALID_ADDR: + return CIOT__ERR__INVALID_ADDR; + case NRF_ERROR_INVALID_STATE: + return CIOT__ERR__INVALID_STATE; + case NRF_ERROR_INVALID_PARAM: + return CIOT__ERR__INVALID_ARG; + case NRF_ERROR_NOT_SUPPORTED: + return CIOT__ERR__NOT_SUPPORTED; + case NRF_ERROR_INVALID_LENGTH: + return CIOT__ERR__INVALID_SIZE; + case NRF_ERROR_RESOURCES: + return CIOT__ERR__RESOURCES; + default: + return CIOT__ERR__UNKNOWN; } } -static void ciot_ble_scn_handle_adv_report(ciot_ble_scn_t self, ciot_ble_scn_adv_t *adv) +static void ciot_ble_scn_copy_mac(uint8_t destiny[6], uint8_t source[6], bool reverse) { - ciot_ble_scn_base_t *base = &self->base; -#ifdef CIOT_CONFIG_BLE_SCN_ADV_FIFO_SIZE - ciot_ble_scn_adv_fifo_t *adv_fifo = &base->adv_fifo; - if(adv_fifo->list[adv_fifo->wp].info->rssi == 0) - { - adv_fifo->list[adv_fifo->wp].info->rssi = adv->info->rssi; - memcpy(adv_fifo->list[adv_fifo->wp].info->mac.data, adv->info->mac.data, adv->info->mac.len); - adv_fifo->list[adv_fifo->wp].info->mac.len = adv->info->mac.len; - memcpy(adv_fifo->list[adv_fifo->wp].payload.data, adv->payload.data, adv->payload.len); - adv_fifo->list[adv_fifo->wp].payload.len = adv->payload.len; - adv_fifo->wp++; - base->status.fifo_len++; - if(base->status.fifo_len > base->status.fifo_max) - { - base->status.fifo_max = base->status.fifo_len; - } - if(adv_fifo->wp == CIOT_CONFIG_BLE_SCN_ADV_FIFO_SIZE) - { - adv_fifo->wp = 0; - } - } - else + for (size_t i = 0; i < 6; i++) { - base->status.advs_losted++; - base->status.err_code = CIOT__ERR__DATA_LOSS; + destiny[i] = reverse ? source[5 - i] : source[i]; } -#else - ciot_iface_event_t event = {0}; - event.type = CIOT_IFACE_EVENT_DATA; - event.data = (uint8_t*)adv; - ciot_iface_send_event(&base->iface, &event); -#endif } From 1227abfdd8fff9f77cfce6731b72efde87d7de5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wesley=20Jos=C3=A9=20Santos?= Date: Tue, 5 Nov 2024 08:13:36 -0300 Subject: [PATCH 2/2] feat: updated enum members --- proto | 2 +- src/common/ciot_mqtt_client_base.c | 2 +- src/mg/ciot_mqtt_client.c | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/proto b/proto index 41df548..7d3e2a4 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 41df548f4599460141afe534f117a206e3753127 +Subproject commit 7d3e2a4bf9058b36205f42d2ee8818150744da45 diff --git a/src/common/ciot_mqtt_client_base.c b/src/common/ciot_mqtt_client_base.c index b7a324e..c5639ac 100644 --- a/src/common/ciot_mqtt_client_base.c +++ b/src/common/ciot_mqtt_client_base.c @@ -174,7 +174,7 @@ ciot_err_t ciot_mqtt_client_set_subtopic(ciot_mqtt_client_t self, char *subtopic ciot_err_t ciot_mqtt_client_update_data_rate(ciot_mqtt_client_t self, int bytes_sended) { ciot_mqtt_client_base_t *base = (ciot_mqtt_client_base_t*)self; - if(base->status.state == CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED) + if(base->status.state == CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_CONNECTED) { base->data_rate_aux += bytes_sended; if(ciot_timer_now() != base->status.last_msg_time) diff --git a/src/mg/ciot_mqtt_client.c b/src/mg/ciot_mqtt_client.c index 3be468d..c7e6eea 100644 --- a/src/mg/ciot_mqtt_client.c +++ b/src/mg/ciot_mqtt_client.c @@ -81,7 +81,7 @@ ciot_err_t ciot_mqtt_client_start(ciot_mqtt_client_t self, ciot_mqtt_client_cfg_ mg_mqtt_disconnect(self->connection, &opts); self->reconecting = true; } - base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTING; + base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_CONNECTING; self->connection = mg_mqtt_connect(self->mgr, base->url, &opts, ciot_mqtt_client_event_handler, self); return CIOT__ERR__OK; @@ -94,7 +94,7 @@ ciot_err_t ciot_mqtt_client_stop(ciot_mqtt_client_t self) if(self->connection != NULL) { struct mg_mqtt_opts opts = {0}; - self->base.status.state = CIOT__MQTT_CLIENT_STATE__MQTT_STATE_DISCONNECTING; + self->base.status.state = CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_DISCONNECTING; mg_mqtt_disconnect(self->connection, &opts); } return CIOT__ERR__OK; @@ -118,7 +118,7 @@ ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *d CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(topic); CIOT_ERR_NULL_CHECK(self->connection); - CIOT_ERR_VALUE_CHECK(self->base.status.state, CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED, CIOT__ERR__INVALID_STATE); + CIOT_ERR_VALUE_CHECK(self->base.status.state, CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_CONNECTED, CIOT__ERR__INVALID_STATE); CIOT_ERR_EMPTY_STRING_CHECK(topic); struct mg_mqtt_opts opts = {0}; struct mg_str msg = {0}; @@ -145,14 +145,14 @@ static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void case MG_EV_ERROR: { CIOT_LOGE(TAG, "MG_EV_ERROR (%s)", (char *)ev_data); - base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_STATE_ERROR; + base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_ERROR; base->status.error->code = atoi(&((char*)ev_data)[9]); ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_ERROR); break; } case MG_EV_OPEN: CIOT_LOGD(TAG, "MG_EV_OPEN url:%s", base->url); - base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTING; + base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_CONNECTING; iface_event.type = CIOT_IFACE_EVENT_INTERNAL; ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_INTERNAL); break; @@ -171,7 +171,7 @@ static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void } case MG_EV_POLL: { - if(base->status.state == CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED && + if(base->status.state == CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_CONNECTED && ciot_timer_now() >= self->last_ping + 10) { CIOT_LOGD(TAG, "MQTT_EV_PING"); @@ -182,11 +182,11 @@ static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void } case MG_EV_MQTT_OPEN: { - if(base->status.state != CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED) + if(base->status.state != CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_CONNECTED) { CIOT_LOGI(TAG, "MG_EV_MQTT_OPEN url:%s", base->url); base->status.conn_count++; - base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED; + base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_CONNECTED; iface_event.type = CIOT_IFACE_EVENT_STARTED; if(base->topic_sub[0] != '\0') { @@ -225,7 +225,7 @@ static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void CIOT_LOGI(TAG, "MG_EV_CLOSE"); if(!self->reconecting) { - base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_STATE_DISCONNECTED; + base->status.state = CIOT__MQTT_CLIENT_STATE__MQTT_CLIENT_STATE_DISCONNECTED; ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_STOPPED); } self->reconecting = false;