From c651bd968fb6b881e4c47d444efbe81bcf675413 Mon Sep 17 00:00:00 2001 From: Nicolas MASSE Date: Tue, 8 Feb 2022 09:24:48 +0100 Subject: [PATCH] factor common code --- main/CMakeLists.txt | 2 +- main/common.c | 2 ++ main/common.h | 16 ++++++++++++++++ main/main.c | 9 ++++++--- main/mqtt.c | 36 +++--------------------------------- main/mqtt.h | 1 - main/sntp.c | 34 ++-------------------------------- main/sntp.h | 1 - main/wifi.c | 40 +++------------------------------------- main/wifi.h | 1 - 10 files changed, 33 insertions(+), 109 deletions(-) create mode 100644 main/common.c create mode 100644 main/common.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 84da3ee..374c6d0 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "main.c" "tic.c" "wifi.c" "mqtt.c" "sntp.c" "libteleinfo.cpp" "libteleinfo/src/LibTeleinfo.cpp" +idf_component_register(SRCS "main.c" "tic.c" "wifi.c" "mqtt.c" "sntp.c" "common.c" "libteleinfo.cpp" "libteleinfo/src/LibTeleinfo.cpp" INCLUDE_DIRS ".") diff --git a/main/common.c b/main/common.c new file mode 100644 index 0000000..0425933 --- /dev/null +++ b/main/common.c @@ -0,0 +1,2 @@ +#include "common.h" +volatile EventGroupHandle_t services_event_group; diff --git a/main/common.h b/main/common.h new file mode 100644 index 0000000..b7143a4 --- /dev/null +++ b/main/common.h @@ -0,0 +1,16 @@ +#ifndef __COMMON_H__ +#define __COMMON_H__ + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" + +#define WIFI_CONNECTED_BIT BIT0 +#define MQTT_CONNECTED_BIT BIT1 +#define TIME_SYNC_BIT BIT2 + +extern volatile EventGroupHandle_t services_event_group; + +#define WAIT_FOR(flags) while ((xEventGroupWaitBits(services_event_group, flags, pdFALSE, pdTRUE, portMAX_DELAY) & (flags)) != (flags)) {} + +#endif diff --git a/main/main.c b/main/main.c index cfd80c4..e7c2705 100644 --- a/main/main.c +++ b/main/main.c @@ -13,6 +13,7 @@ #include "wifi.h" #include "mqtt.h" #include "sntp.h" +#include "common.h" // Embedded via component.mk extern const uint8_t cacert_pem_start[] asm("_binary_cacert_pem_start"); @@ -26,11 +27,13 @@ void app_main(void) { ESP_ERROR_CHECK(esp_tls_init_global_ca_store()); ESP_ERROR_CHECK(esp_tls_set_global_ca_store((const unsigned char *) cacert_pem_start, cacert_pem_end - cacert_pem_start)); + // Create an event group to keep track of service readiness + services_event_group = xEventGroupCreate(); + wifi_init_sta(); - wifi_wait_for_online(); + WAIT_FOR(WIFI_CONNECTED_BIT); mqtt_init(); sntp_start(); - mqtt_wait_for_readiness(); - sntp_wait_for_readiness(); + WAIT_FOR(MQTT_CONNECTED_BIT|TIME_SYNC_BIT); tic_uart_init(); } diff --git a/main/mqtt.c b/main/mqtt.c index 1b461bd..0b07a82 100644 --- a/main/mqtt.c +++ b/main/mqtt.c @@ -4,7 +4,6 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" @@ -16,17 +15,11 @@ #include "esp_tls.h" #include "esp_ota_ops.h" #include +#include "common.h" -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t s_mqtt_event_group; static esp_mqtt_client_config_t mqtt_cfg; static esp_mqtt_client_handle_t client; -/* The event group allows multiple bits for each event, but we only care about one event: - * - we are connected to the MQTT server - */ -#define MQTT_CONNECTED_BIT BIT0 - static const char *MQTT_LOGGER = "mqtt"; void mqtt_publish_data(char* key, char* value) { @@ -57,11 +50,11 @@ esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { switch (event->event_id) { case MQTT_EVENT_CONNECTED: ESP_LOGI(MQTT_LOGGER, "MQTT_EVENT_CONNECTED"); - xEventGroupSetBits(s_mqtt_event_group, MQTT_CONNECTED_BIT); + xEventGroupSetBits(services_event_group, MQTT_CONNECTED_BIT); break; case MQTT_EVENT_DISCONNECTED: ESP_LOGI(MQTT_LOGGER, "MQTT_EVENT_DISCONNECTED"); - xEventGroupClearBits(s_mqtt_event_group, MQTT_CONNECTED_BIT); + xEventGroupClearBits(services_event_group, MQTT_CONNECTED_BIT); break; case MQTT_EVENT_PUBLISHED: ESP_LOGI(MQTT_LOGGER, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); @@ -110,8 +103,6 @@ char* get_nvs_string(nvs_handle_t nvs, char* key) { } void mqtt_init(void) { - s_mqtt_event_group = xEventGroupCreate(); - nvs_handle_t nvs; esp_err_t err = nvs_open("mqtt", NVS_READONLY, &nvs); if (err != ESP_OK) { @@ -131,24 +122,3 @@ void mqtt_init(void) { esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); } - -void mqtt_wait_for_readiness(void) { - /* Waiting until the connection is established (MQTT_CONNECTED_BIT). - * The bits are set by mqtt_event_handler_cb() (see above) - */ - EventBits_t bits = xEventGroupWaitBits(s_mqtt_event_group, - MQTT_CONNECTED_BIT, - pdFALSE, - pdFALSE, - portMAX_DELAY); - - /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually - * happened. */ - if (bits & MQTT_CONNECTED_BIT) { - ESP_LOGI(MQTT_LOGGER, "Connected to the MQTT server!"); - } else { - ESP_LOGE(MQTT_LOGGER, "UNEXPECTED EVENT"); - } - - //vEventGroupDelete(s_mqtt_event_group); -} diff --git a/main/mqtt.h b/main/mqtt.h index 4a498af..03f7592 100644 --- a/main/mqtt.h +++ b/main/mqtt.h @@ -2,7 +2,6 @@ #define __MQTT_H__ void mqtt_init(); -void mqtt_wait_for_readiness(void); void mqtt_publish_data(char* key, char* value); void mqtt_publish_alert(uint8_t value); diff --git a/main/sntp.c b/main/sntp.c index 45b4403..44cb4ed 100644 --- a/main/sntp.c +++ b/main/sntp.c @@ -3,21 +3,13 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" #include "esp_event.h" #include "esp_log.h" #include "esp_attr.h" #include "esp_sntp.h" #include "sntp.h" - -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t s_sntp_event_group; - -/* The event group allows multiple bits for each event, but we only care about one event: - * - time is synchronized - */ -#define TIME_SYNC_BIT BIT0 +#include "common.h" static const char *SNTP_LOGGER = "sntp"; @@ -32,12 +24,11 @@ void sntp_callback(struct timeval *tv) { ESP_LOGI(SNTP_LOGGER, "Time synchronized: %s", strftime_buf); } sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH); - xEventGroupSetBits(s_sntp_event_group, TIME_SYNC_BIT); + xEventGroupSetBits(services_event_group, TIME_SYNC_BIT); } } void sntp_start() { - s_sntp_event_group = xEventGroupCreate(); ESP_LOGI(SNTP_LOGGER, "Initializing SNTP..."); sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setservername(0, "pool.ntp.org"); @@ -45,24 +36,3 @@ void sntp_start() { sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); sntp_init(); } - -void sntp_wait_for_readiness(void) { - /* Waiting until the connection is established (MQTT_CONNECTED_BIT). - * The bits are set by mqtt_event_handler_cb() (see above) - */ - EventBits_t bits = xEventGroupWaitBits(s_sntp_event_group, - TIME_SYNC_BIT, - pdFALSE, - pdFALSE, - portMAX_DELAY); - - /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually - * happened. */ - if (bits & TIME_SYNC_BIT) { - ESP_LOGI(SNTP_LOGGER, "Time is synchronized!"); - } else { - ESP_LOGE(SNTP_LOGGER, "UNEXPECTED EVENT"); - } - - //vEventGroupDelete(s_sntp_event_group); -} \ No newline at end of file diff --git a/main/sntp.h b/main/sntp.h index 4a19cb5..929f338 100644 --- a/main/sntp.h +++ b/main/sntp.h @@ -2,6 +2,5 @@ #define __SNTP_H__ void sntp_start(); -void sntp_wait_for_readiness(void); #endif diff --git a/main/wifi.c b/main/wifi.c index 71a2f9f..7f29c45 100644 --- a/main/wifi.c +++ b/main/wifi.c @@ -1,7 +1,6 @@ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" @@ -12,14 +11,7 @@ #include "lwip/sys.h" #include "wifi.h" - -/* FreeRTOS event group to signal when we are connected*/ -static EventGroupHandle_t s_wifi_event_group; - -/* The event group allows multiple bits for each event, but we only care about one event: - * - we are connected to the AP with an IP - */ -#define WIFI_CONNECTED_BIT BIT0 +#include "common.h" static const char *WIFI_LOGGER = "wifi"; @@ -29,20 +21,18 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, esp_wifi_connect(); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { ESP_LOGI(WIFI_LOGGER,"Connection to the AP failed!"); - xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + xEventGroupClearBits(services_event_group, WIFI_CONNECTED_BIT); ESP_LOGI(WIFI_LOGGER, "Retrying to connect to the AP..."); esp_wifi_connect(); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(WIFI_LOGGER, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); - xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + xEventGroupSetBits(services_event_group, WIFI_CONNECTED_BIT); } } void wifi_init_sta(void) { - s_wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); @@ -69,27 +59,3 @@ void wifi_init_sta(void) { ESP_LOGI(WIFI_LOGGER, "wifi_init_sta finished."); } - -void wifi_wait_for_online(void) { - /* Waiting until the connection is established (WIFI_CONNECTED_BIT). - * The bits are set by wifi_event_handler() (see above) - */ - EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, - WIFI_CONNECTED_BIT, - pdFALSE, - pdFALSE, - portMAX_DELAY); - - /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually - * happened. */ - if (bits & WIFI_CONNECTED_BIT) { - ESP_LOGI(WIFI_LOGGER, "Connected to Wifi!"); - } else { - ESP_LOGE(WIFI_LOGGER, "UNEXPECTED EVENT"); - } - - /* The event will not be processed after unregister */ - //ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip)); - //ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id)); - //vEventGroupDelete(s_wifi_event_group); -} diff --git a/main/wifi.h b/main/wifi.h index 875a29e..73d6f67 100644 --- a/main/wifi.h +++ b/main/wifi.h @@ -2,6 +2,5 @@ #define __WIFI_H__ void wifi_init_sta(void); -void wifi_wait_for_online(void); #endif