Skip to content

Commit

Permalink
feat(mosq): Support for message callback
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Dec 11, 2024
1 parent 269351f commit 8855c6d
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
menu "Example Configuration"

menu "AP Configuration"
comment "AP Configuration"

config EXAMPLE_AP_SSID
string "Wi-Fi SSID"
default "myssid"
help
Set the SSID of Wi-Fi ap interface.

config EXAMPLE_AP_PASSWORD
string "Wi-Fi Password"
default "12345678"
help
Set the password of Wi-Fi ap interface.

endmenu

menu "STA Configuration"
comment "STA Configuration"

config EXAMPLE_STA_SSID
string "WiFi Station SSID"
default "mystationssid"
help
SSID for the example's sta to connect to.

config EXAMPLE_STA_PASSWORD
string "WiFi Station Password"
default "mystationpassword"
help
WiFi station password for the example to use.
endmenu

config EXAMPLE_MQTT_BROKER_URI
string "MQTT Broker URL"
default "mqtt://mqtt.eclipseprojects.io"
help
URL of the mqtt broker use for synchronisation and exchanging
ICE connect info (description and candidates).

config EXAMPLE_MQTT_SYNC_TOPIC
string "MQTT topic for synchronisation"
default "/topic/serverless_mqtt"
help
MQTT topic used fo synchronisation.

config EXAMPLE_STUN_SERVER
string "Hostname of STUN server"
default "stun.l.google.com"
help
STUN server hostname.

endmenu
109 changes: 109 additions & 0 deletions components/mosquitto/examples/serverless_mqtt/main/wifi_connect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_check.h"
#include "esp_wifi.h"
#include "esp_mac.h"

#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1

static const char *TAG = "serverless_wifi";
static EventGroupHandle_t s_wifi_events;
static int s_retry_num = 0;
static const int s_max_retry = 30;

static void wifi_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < s_max_retry) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_events, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG, "Connect to the AP fail");
} 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(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_events, WIFI_CONNECTED_BIT);
}
}

esp_err_t wifi_connect(void)
{
esp_err_t ret = ESP_OK;
ESP_GOTO_ON_FALSE(s_wifi_events = xEventGroupCreate(), ESP_ERR_NO_MEM, err, TAG, "Failed to create wifi_events");
ESP_GOTO_ON_ERROR(nvs_flash_init(), err, TAG, "Failed to init nvs flash");
ESP_GOTO_ON_ERROR(esp_netif_init(), err, TAG, "Failed to init esp_netif");
ESP_GOTO_ON_ERROR(esp_event_loop_create_default(), err, TAG, "Failed to create default event loop");
ESP_GOTO_ON_ERROR(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL),
err, TAG, "Failed to register WiFi event handler");
ESP_GOTO_ON_ERROR(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL),
err, TAG, "Failed to register IP event handler");

// Initialize WiFi
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_GOTO_ON_ERROR(esp_wifi_init(&cfg), err, TAG, "Failed to initialize WiFi");
ESP_GOTO_ON_ERROR(esp_wifi_set_mode(WIFI_MODE_APSTA), err, TAG, "Failed to set STA+AP mode");

// Initialize AP
esp_netif_t *ap = esp_netif_create_default_wifi_ap();
ESP_GOTO_ON_FALSE(ap, ESP_FAIL, err, TAG, "Failed to create AP network interface");
wifi_config_t wifi_ap_config = {
.ap = {
.ssid = CONFIG_EXAMPLE_AP_SSID,
.password = CONFIG_EXAMPLE_AP_SSID,
.max_connection = 4,
},
};
ESP_GOTO_ON_ERROR(esp_wifi_set_config(WIFI_IF_AP, &wifi_ap_config), err, TAG, "Failed to set AP config");


// Initialize STA
esp_netif_t *sta = esp_netif_create_default_wifi_sta();
ESP_GOTO_ON_FALSE(sta, ESP_FAIL, err, TAG, "Failed to create WiFi station network interface");
wifi_config_t wifi_sta_config = {
.sta = {
.ssid = CONFIG_EXAMPLE_STA_SSID,
.password = CONFIG_EXAMPLE_STA_PASSWORD,
},
};
ESP_GOTO_ON_ERROR(esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config), err, TAG, "Failed to set STA config");

// Start WiFi
ESP_GOTO_ON_ERROR(esp_wifi_start(), err, TAG, "Failed to start WiFi");

// Wait for connection
EventBits_t bits = xEventGroupWaitBits(s_wifi_events, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE, pdFALSE, pdMS_TO_TICKS(30000));
ESP_GOTO_ON_FALSE((bits & WIFI_CONNECTED_BIT) == WIFI_CONNECTED_BIT, ESP_FAIL, err,
TAG, "Failed to obtain IP address from WiFi station");
return ESP_OK;
err:
esp_wifi_stop();
esp_wifi_deinit();
nvs_flash_deinit();
esp_netif_deinit();
esp_event_loop_delete_default();
return ret;

}
5 changes: 5 additions & 0 deletions components/mosquitto/port/broker.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ void mosq_broker_stop(void)
run = 0;
}

extern mosq_message_cb_t g_mosq_message_callback;

int mosq_broker_run(struct mosq_broker_config *broker_config)
{

Expand All @@ -125,6 +127,9 @@ int mosq_broker_run(struct mosq_broker_config *broker_config)
if (broker_config->tls_cfg) {
net__set_tls_config(broker_config->tls_cfg);
}
if (broker_config->handle_message_cb) {
g_mosq_message_callback = broker_config->handle_message_cb;
}

db.config = &config;

Expand Down
5 changes: 5 additions & 0 deletions components/mosquitto/port/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include "util_mosq.h"
#include "utlist.h"
#include "lib_load.h"
#include "mosq_broker.h"

mosq_message_cb_t g_mosq_message_callback = NULL;

int mosquitto_callback_register(
mosquitto_plugin_id_t *identifier,
Expand Down Expand Up @@ -44,5 +46,8 @@ void plugin__handle_disconnect(struct mosquitto *context, int reason)

int plugin__handle_message(struct mosquitto *context, struct mosquitto_msg_store *stored)
{
if (g_mosq_message_callback) {
g_mosq_message_callback(context->id, stored->topic, stored->payload, stored->payloadlen, stored->qos, stored->retain);
}
return MOSQ_ERR_SUCCESS;
}
2 changes: 2 additions & 0 deletions components/mosquitto/port/include/mosq_broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

struct mosquitto__config;

typedef void (*mosq_message_cb_t)(char *client, char *topic, char *data, int len, int qos, int retain);
/**
* @brief Mosquitto configuration structure
*
Expand All @@ -24,6 +25,7 @@ struct mosq_broker_config {
* You can open the respective docs with this idf.py command:
* `idf.py docs -sp api-reference/protocols/esp_tls.html`
*/
void (*handle_message_cb)(char *client, char *topic, char *data, int len, int qos, int retain);
};

/**
Expand Down

0 comments on commit 8855c6d

Please sign in to comment.