Skip to content

Commit

Permalink
Merge pull request #29 from hedrosistemas/develop
Browse files Browse the repository at this point in the history
merge from hedrosistemas
  • Loading branch information
wjsan authored Nov 5, 2024
2 parents bd3d609 + 0b345ed commit ec7ecca
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 49 deletions.
1 change: 1 addition & 0 deletions include/ciot_ble_scn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion proto
47 changes: 47 additions & 0 deletions src/common/ciot_ble_scn_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

#include <stdlib.h>
#include <string.h>
#include "ciot_ble_scn.h"
#include "ciot_config.h"

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/common/ciot_mqtt_client_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions src/mg/ciot_mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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};
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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')
{
Expand Down Expand Up @@ -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;
Expand Down
64 changes: 26 additions & 38 deletions src/nrf/ciot_ble_scn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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))
{
Expand All @@ -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
}

0 comments on commit ec7ecca

Please sign in to comment.