Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions main/ble2mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#define MAX_TOPIC_LEN 256
static const char *TAG = "BLE2MQTT";

static bool ble_retain_connection_without_mqtt = false;
static bool ble_publish_values_without_mqtt = false;
static bool mqtt_connected = false;

typedef struct {
mac_addr_t mac;
ble_uuid_t service;
Expand Down Expand Up @@ -207,8 +211,10 @@ static void management_unsubscribe(void)

static void cleanup(void)
{
ble_disconnect_all();
ble_scan_stop();
if(!ble_retain_connection_without_mqtt){
ble_disconnect_all();
ble_scan_stop();
}
ota_unsubscribe();
management_unsubscribe();
}
Expand Down Expand Up @@ -243,6 +249,7 @@ static void network_on_disconnected(void)
static void mqtt_on_connected(void)
{
ESP_LOGI(TAG, "Connected to MQTT, scanning for BLE devices");
mqtt_connected = true;
self_publish();
ota_subscribe();
management_subscribe();
Expand All @@ -252,6 +259,7 @@ static void mqtt_on_connected(void)
static void mqtt_on_disconnected(void)
{
static uint8_t num_disconnections = 0;
mqtt_connected = false;

ESP_LOGI(TAG, "Disconnected from MQTT, stopping BLE");
cleanup();
Expand Down Expand Up @@ -495,6 +503,10 @@ static void ble_on_device_characteristic_value(mac_addr_t mac,
ble_uuid_t service, ble_uuid_t characteristic, uint8_t index,
uint8_t *value, size_t value_len)
{
if(!mqtt_connected && !ble_publish_values_without_mqtt){
// ignore values, no mqtt
return;
}
char *topic = ble_topic(mac, service, characteristic, index);
char *payload = chartoa(characteristic, value, value_len);
size_t payload_len = strlen(payload);
Expand Down Expand Up @@ -988,6 +1000,9 @@ void app_main()
return;
}

ble_retain_connection_without_mqtt = config_ble_retain_connection_without_mqtt_get();
ble_publish_values_without_mqtt = config_ble_publish_values_without_mqtt_get();

switch (config_network_type_get())
{
case NETWORK_TYPE_ETH:
Expand Down
14 changes: 14 additions & 0 deletions main/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ uint8_t config_mqtt_retained_get(void)
return cJSON_IsTrue(retain);
}

uint8_t config_ble_retain_connection_without_mqtt_get(void){
cJSON *ble = cJSON_GetObjectItemCaseSensitive(config, "ble");
cJSON *keep = cJSON_GetObjectItemCaseSensitive(ble, "retain_connection_without_mqtt");

return cJSON_IsTrue(keep);
}

uint8_t config_ble_publish_values_without_mqtt_get(void){
cJSON *ble = cJSON_GetObjectItemCaseSensitive(config, "ble");
cJSON *publish = cJSON_GetObjectItemCaseSensitive(ble, "publish_values_without_mqtt");

return cJSON_IsTrue(publish);
}

const char *config_mqtt_topics_get(const char *param_name, const char *def)
{
cJSON *mqtt = cJSON_GetObjectItemCaseSensitive(config, "mqtt");
Expand Down
2 changes: 2 additions & 0 deletions main/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const char *config_mqtt_username_get(void);
const char *config_mqtt_password_get(void);
uint8_t config_mqtt_qos_get(void);
uint8_t config_mqtt_retained_get(void);
uint8_t config_ble_retain_connection_without_mqtt_get(void);
uint8_t config_ble_publish_values_without_mqtt_get(void);
const char *config_mqtt_prefix_get(void);
const char *config_mqtt_get_suffix_get(void);
const char *config_mqtt_set_suffix_get(void);
Expand Down
23 changes: 17 additions & 6 deletions main/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,30 @@ static void mqtt_subscription_remove(mqtt_subscription_t **list,

static mqtt_publications_t *mqtt_publication_add(mqtt_publications_t **list,
const char *topic, uint8_t *payload, size_t len, int qos, uint8_t retained)
{
mqtt_publications_t *pub = malloc(sizeof(*pub));
{
// not initializing to NULL since the following loop will leave it at NULL
// when there is no match
mqtt_publications_t *pub;
for(pub = *list; pub != NULL; pub = pub->next){
if(strcmp(topic, pub->topic) == 0){
free(pub->payload);
break;
}
}

if(pub == NULL){
pub = malloc(sizeof(mqtt_publications_t));
pub->topic = strdup(topic);
pub->next = *list;
*list = pub;
}

pub->topic = strdup(topic);
pub->payload = malloc(len);
memcpy(pub->payload, payload, len);
pub->len = len;
pub->qos = qos;
pub->retained = retained;

pub->next = *list;
*list = pub;

return pub;
}

Expand Down