|
|
@ -60,13 +60,15 @@ esp_err_t XD0MQTT::mqtt_event_handler_cb(esp_mqtt_event_handle_t event) |
|
|
|
ESP_LOGI(TAG, "TOPIC=%.*s\r\n", event->topic_len, event->topic); |
|
|
|
ESP_LOGI(TAG, "DATA=%.*s\r\n", event->data_len, event->data); |
|
|
|
for (const auto &subscription : subscriptions_) { |
|
|
|
char topic[128]; |
|
|
|
strncpy(topic, event->topic, event->topic_len); topic[event->topic_len] = '\0'; |
|
|
|
// literal match
|
|
|
|
if (strcmp(event->topic, subscription.topic) == 0) { |
|
|
|
subscription.cb(event->topic, event->data); |
|
|
|
if (event->topic_len == strlen(subscription.topic) && strncmp(event->topic, subscription.topic, event->topic_len) == 0) { |
|
|
|
subscription.cb(topic, event->data, event->data_len); |
|
|
|
// '#' wildcard
|
|
|
|
} else if (subscription.topic[strlen(subscription.topic)-1] == '#'){ |
|
|
|
if (strncmp(event->topic, subscription.topic, strlen(subscription.topic)-1) == 0) { |
|
|
|
subscription.cb(event->topic, event->data); |
|
|
|
if (event->topic_len >= strlen(subscription.topic) && strncmp(event->topic, subscription.topic, strlen(subscription.topic)-1) == 0) { |
|
|
|
subscription.cb(topic, event->data, event->data_len); |
|
|
|
} |
|
|
|
} |
|
|
|
// ToDo: '+' wildcard
|
|
|
@ -128,6 +130,10 @@ void XD0MQTT::end(void) { |
|
|
|
esp_mqtt_client_destroy(client); |
|
|
|
} |
|
|
|
|
|
|
|
bool XD0MQTT::isConnected(void) { |
|
|
|
return connected; |
|
|
|
} |
|
|
|
|
|
|
|
bool XD0MQTT::publish(const char* topic, const char* data, int len, int qos, int retain) { |
|
|
|
int msg_id = esp_mqtt_client_publish(client, topic, data, len, qos, retain); |
|
|
|
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); |
|
|
|