Ver Fonte

[fix] 更新core_mqtt_config.h

Yaochenger há 6 meses atrás
pai
commit
ea935ce210
3 ficheiros alterados com 86 adições e 21 exclusões
  1. 5 5
      api/mqtt_api.c
  2. 73 13
      core_mqtt_config.h
  3. 8 3
      demo/demo.c

+ 5 - 5
api/mqtt_api.c

@@ -13,11 +13,11 @@
 
 #include "mqtt_api.h"
 
-static MQTTFixedBuffer_t mqttBuffer = { .pBuffer = RT_NULL, .size = 1024 };
+static MQTTFixedBuffer_t mqttBuffer = { .pBuffer = RT_NULL, .size = MQTT_BUF_SIZE };
 static MQTTContext_t mqttContext;
 static TransportInterface_t transportInterface;
 static NetworkContext_t networkContext;
-static MQTTPubAckInfo_t outgoingPublishes[MQTT_OUTgoing_PublishCount];
+static MQTTPubAckInfo_t outgoingPublishes[MQTT_OUTGOING_PUBLISH_COUNT];
 
 MQTTStatus_t mqttInit(NetworkContext_t *networkContext, MQTTEventCallback_t userCallback)
 {
@@ -27,7 +27,7 @@ MQTTStatus_t mqttInit(NetworkContext_t *networkContext, MQTTEventCallback_t user
     transportInterface.send = transportSend;
     transportInterface.recv = transportRecv;
 
-    mqttBuffer.pBuffer = rt_malloc(mqttBuffer.size); // 缓存
+    mqttBuffer.pBuffer = rt_malloc(mqttBuffer.size);
     if (mqttBuffer.pBuffer == RT_NULL)
     {
         MQTT_PRINT("Failed to allocate MQTT buffer\n");
@@ -43,7 +43,7 @@ MQTTStatus_t mqttInit(NetworkContext_t *networkContext, MQTTEventCallback_t user
     }
     else
     {
-        status = MQTT_InitStatefulQoS(&mqttContext, outgoingPublishes, MQTT_OUTgoing_PublishCount, NULL, 0);
+        status = MQTT_InitStatefulQoS(&mqttContext, outgoingPublishes, MQTT_OUTGOING_PUBLISH_COUNT, NULL, 0);
         MQTT_PRINT("MQTT client initialized successfully\n");
     }
 
@@ -203,7 +203,7 @@ void mqttClientTask(void *parameter)
     uint32_t backoffMs = INITIAL_BACKOFF_MS;
     bool isConnected = false;
 
-    if (mqttInit(&networkContext, mqttEventCallback) != MQTTSuccess)
+    if (mqttInit(&networkContext, MQTT_USER_CALLBACK) != MQTTSuccess)
     {
         MQTT_PRINT("MQTT initialization failed\n");
         return;

+ 73 - 13
core_mqtt_config.h

@@ -7,25 +7,85 @@
  * Date           Author       Notes
  * 2025-06-10     RV          the first version
  */
+
 #ifndef APPLICATIONS_FIREMQTT_PORT_CONFIG_H_
 #define APPLICATIONS_FIREMQTT_PORT_CONFIG_H_
+
 #include "mqtt_api.h"
-#define MQTT_BROKER_ADDRESS  "broker.emqx.io"       // MQTT 代理地址
-#define MQTT_BROKER_PORT     1883                   // MQTT 代理端口
-#define MQTT_CLIENT_ID       "rtthread_mqtt_client" // 客户端 ID
-#define MQTT_TOPIC_SUB       "rtthread/test/sub"    // 订阅主题
-#define MQTT_TOPIC_PUB       "rtthread/test/pub"    // 发布主题
+
+/* MQTT Broker Address */
+#ifndef MQTT_BROKER_ADDRESS
+#define MQTT_BROKER_ADDRESS  "broker.emqx.io"
+#endif
+
+/* MQTT Broker Port */
+#ifndef MQTT_BROKER_PORT
+#define MQTT_BROKER_PORT     1883
+#endif
+
+/* MQTT Client ID */
+#ifndef MQTT_CLIENT_ID
+#define MQTT_CLIENT_ID       "rtthread_mqtt_client"
+#endif
+
+/* MQTT Subscription Topic */
+#ifndef MQTT_TOPIC_SUB
+#define MQTT_TOPIC_SUB       "rtthread/test/sub"
+#endif
+
+/* MQTT Publish Topic */
+#ifndef MQTT_TOPIC_PUB
+#define MQTT_TOPIC_PUB       "rtthread/test/pub"
+#endif
+
+/* MQTT Keep Alive Time (seconds) */
+#ifndef MQTT_KEEP_ALIVE
 #define MQTT_KEEP_ALIVE      60
+#endif
+
+/* MQTT Thread Yield Time */
+#ifndef MQTT_LOOP_CNT
 #define MQTT_LOOP_CNT        60
-#define MQTT_RECV_POLLING_TIMEOUT_MS    ( 0U )
-#define MQTT_PINGRESP_TIMEOUT_MS    ( 10000U )
-#define MQTT_OUTgoing_PublishCount 30
+#endif
+
+/* MQTT Receive Polling Timeout (milliseconds) */
+#ifndef MQTT_RECV_POLLING_TIMEOUT_MS
+#define MQTT_RECV_POLLING_TIMEOUT_MS    (0U)
+#endif
+
+/* MQTT PINGRESP Timeout (milliseconds) */
+#ifndef MQTT_PINGRESP_TIMEOUT_MS
+#define MQTT_PINGRESP_TIMEOUT_MS        (10000U)
+#endif
+
+/* MQTT Outgoing Publish Count */
+#ifndef MQTT_OUTGOING_PUBLISH_COUNT
+#define MQTT_OUTGOING_PUBLISH_COUNT     30
+#endif
+
+/* MQTT Buffer Size */
+#ifndef MQTT_BUF_SIZE
+#define MQTT_BUF_SIZE                   4096
+#endif
+
+/* Maximum Retry Attempts */
+#ifndef MAX_RETRY_ATTEMPTS
+#define MAX_RETRY_ATTEMPTS              5
+#endif
+
+/* Initial Backoff Time (milliseconds) */
+#ifndef INITIAL_BACKOFF_MS
+#define INITIAL_BACKOFF_MS              1000
+#endif
 
-#define MAX_RETRY_ATTEMPTS   5                      // 最大重试次数
-#define INITIAL_BACKOFF_MS   1000                   // 初始重连退避时间(毫秒)
-#define MAX_BACKOFF_MS       60000
-//#define LogDebug(args) do { rt_kprintf args; rt_kprintf("\n"); } while (0)
+/* Maximum Backoff Time (milliseconds) */
+#ifndef MAX_BACKOFF_MS
+#define MAX_BACKOFF_MS                  60000
+#endif
 
-#define MQTT_USERCALLBACK mqttEventCallback
+/* MQTT User Callback */
+#ifndef MQTT_USER_CALLBACK
+#define MQTT_USER_CALLBACK               mqttEventCallback
+#endif
 
 #endif /* APPLICATIONS_FIREMQTT_PORT_CONFIG_H_ */

+ 8 - 3
demo/demo.c

@@ -6,12 +6,17 @@
 
 #include "mqtt_api.h"
 
+#define CORE_MQTTT_STACK_SIZE       4096
+#define CORE_MQTTT_PRIORITY         10
+#define CORE_MQTTT_TIMESLICE        20
+
 void mqtt_client_start(void)
 {
-    rt_wlan_unregister_event_handler(RT_WLAN_EVT_READY);
-
     rt_thread_t tid = rt_thread_create("mqtt", mqttClientTask,
-    RT_NULL, 4096, 10, 20);
+                                               RT_NULL,
+                                               CORE_MQTTT_STACK_SIZE,
+                                               CORE_MQTTT_PRIORITY,
+                                               CORE_MQTTT_TIMESLICE);
     if (tid != RT_NULL)
     {
         rt_thread_startup(tid);