Răsfoiți Sursa

MQTT: Add build only test for using mqtt API from C++

David Cermak 6 ani în urmă
părinte
comite
6176b5fbe6

+ 10 - 0
tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt

@@ -0,0 +1,10 @@
+# The following four lines of boilerplate have to be in your project's CMakeLists
+# in this exact order for cmake to work correctly
+cmake_minimum_required(VERSION 3.5)
+
+# (Not part of the boilerplate)
+# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
+set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
+
+include($ENV{IDF_PATH}/tools/cmake/project.cmake)
+project(mqtt_tcp)

+ 3 - 0
tools/test_apps/protocols/mqtt/build_test/README.md

@@ -0,0 +1,3 @@
+# Build only test for C++
+
+This test app ensures that calling all mqtt-client API could be called from C++ 

+ 2 - 0
tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt

@@ -0,0 +1,2 @@
+idf_component_register(SRCS "mqtt_app.cpp"
+                    INCLUDE_DIRS ".")

+ 101 - 0
tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp

@@ -0,0 +1,101 @@
+/* Build only example to check mqtt client API from C++
+
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+#include <stdint.h>
+#include "esp_system.h"
+#include "nvs_flash.h"
+#include "esp_event.h"
+#include "esp_netif.h"
+#include "esp_log.h"
+#include "mqtt_client.h"
+
+static const char *TAG = "MQTT_EXAMPLE";
+
+
+static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
+{
+    switch (event->event_id) {
+        case MQTT_EVENT_CONNECTED:
+            ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
+            break;
+        case MQTT_EVENT_DISCONNECTED:
+            ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
+            break;
+
+        case MQTT_EVENT_SUBSCRIBED:
+            ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
+            break;
+        case MQTT_EVENT_UNSUBSCRIBED:
+            ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
+            break;
+        case MQTT_EVENT_PUBLISHED:
+            ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
+            break;
+        case MQTT_EVENT_DATA:
+            ESP_LOGI(TAG, "MQTT_EVENT_DATA");
+            break;
+        case MQTT_EVENT_ERROR:
+            ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
+            break;
+        default:
+            ESP_LOGI(TAG, "Other event id:%d", event->event_id);
+            break;
+    }
+    return ESP_OK;
+}
+
+static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
+    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
+    mqtt_event_handler_cb((esp_mqtt_event_handle_t)event_data);
+}
+
+static void mqtt_app_start(void)
+{
+    esp_mqtt_client_config_t mqtt_cfg = { };
+
+    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
+    esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, client);
+    esp_mqtt_client_start(client);
+    esp_mqtt_set_config(client, &mqtt_cfg);
+    int msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data", 0, 0, 0);
+    ESP_LOGI(TAG, "mqtt api returned %d", msg_id);
+    msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
+    ESP_LOGI(TAG, "mqtt api returned %d", msg_id);
+    msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
+    ESP_LOGI(TAG, "mqtt api returned %d", msg_id);
+    msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
+    ESP_LOGI(TAG, "mqtt api returned %d", msg_id);
+    esp_err_t err = esp_mqtt_client_set_uri(client, "mqtt://localhost:1883");
+    ESP_LOGI(TAG, "mqtt api returned %d", err);
+    err = esp_mqtt_client_reconnect(client);
+    err = esp_mqtt_client_disconnect(client);
+    err = esp_mqtt_client_stop(client);
+    err = esp_mqtt_client_destroy(client);
+}
+
+extern "C" void app_main(void)
+{
+    ESP_LOGI(TAG, "[APP] Startup..");
+    ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
+    ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
+
+    esp_log_level_set("*", ESP_LOG_INFO);
+    esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
+    esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
+    esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE);
+    esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE);
+    esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
+    esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
+
+    ESP_ERROR_CHECK(nvs_flash_init());
+    ESP_ERROR_CHECK(esp_netif_init());
+    ESP_ERROR_CHECK(esp_event_loop_create_default());
+
+    mqtt_app_start();
+}