├── Client ├── Cfg │ └── Template │ │ └── mqtt-c_cfg.h ├── Examples │ ├── app_mqtt-c.h │ ├── app_mqtt-c_echo.c │ ├── app_mqtt-c_init.c │ ├── app_mqtt-c_publish.c │ └── app_mqtt-c_subscribe.c └── Source │ ├── mqtt-c.c │ ├── mqtt-c.h │ ├── mqtt-c_sock.c │ └── mqtt-c_sock.h ├── Common └── mqtt.h ├── LICENSE ├── NOTICE └── readme.md /Client/Cfg/Template/mqtt-c_cfg.h: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/MQTTc 4 | * Message Queue Telemetry Transport Client 5 | * 6 | * Copyright 2014-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * MQTT CLIENT CONFIGURATION FILE 21 | * 22 | * TEMPLATE 23 | * 24 | * Filename : mqtt-c_cfg.h 25 | * Version : V1.02.00 26 | ********************************************************************************************************* 27 | */ 28 | 29 | /* 30 | ********************************************************************************************************* 31 | ********************************************************************************************************* 32 | * INCLUDE FILES 33 | ********************************************************************************************************* 34 | ********************************************************************************************************* 35 | */ 36 | 37 | #include 38 | 39 | 40 | /* 41 | ********************************************************************************************************* 42 | ********************************************************************************************************* 43 | * CONFIGURATION DEFINES 44 | ********************************************************************************************************* 45 | ********************************************************************************************************* 46 | */ 47 | 48 | /* 49 | ********************************************************************************************************* 50 | * ARG CHK DEFINE 51 | ********************************************************************************************************* 52 | */ 53 | /* Enable to add extra checks on ext vars. */ 54 | #define MQTTc_CFG_ARG_CHK_EXT_EN DEF_DISABLED 55 | 56 | 57 | /* 58 | ********************************************************************************************************* 59 | * DBG DEFINES 60 | ********************************************************************************************************* 61 | */ 62 | 63 | /* ------------------- TRACE DEFINES ------------------ */ 64 | /* Set trace to function to output trace data. */ 65 | #define MQTTc_CFG_DBG_TRACE /*printf*/ 66 | /* Set trace level to higher than OFF, to obtain data. */ 67 | #define MQTTc_CFG_DBG_TRACE_LEVEL TRACE_LEVEL_OFF 68 | 69 | /* -------------- GLOBAL DBG BUF DEFINES -------------- */ 70 | /* Enables dbg buf where data is copied at checkpoints. */ 71 | #define MQTTc_CFG_DBG_GLOBAL_BUF_EN DEF_DISABLED 72 | /* Size of dbg buf. */ 73 | #define MQTTc_CFG_DBG_GLOBAL_BUF_LEN 512u 74 | 75 | -------------------------------------------------------------------------------- /Client/Examples/app_mqtt-c.h: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * EXAMPLE CODE 4 | * 5 | * This file is provided as an example on how to use Micrium products. 6 | * 7 | * Please feel free to use any application code labeled as 'EXAMPLE CODE' in 8 | * your application products. Example code may be used as is, in whole or in 9 | * part, or may be used as a reference only. This file can be modified as 10 | * required to meet the end-product requirements. 11 | * 12 | ********************************************************************************************************* 13 | */ 14 | 15 | /* 16 | ********************************************************************************************************* 17 | * 18 | * MQTTc APPLICATION 19 | * 20 | * Filename : app_mqtt-c.h 21 | * Version : V1.02.00 22 | ********************************************************************************************************* 23 | */ 24 | 25 | /* 26 | ********************************************************************************************************* 27 | ********************************************************************************************************* 28 | * MODULE 29 | ********************************************************************************************************* 30 | ********************************************************************************************************* 31 | */ 32 | 33 | #ifndef MQTTc_APP_MODULE_PRESENT 34 | #define MQTTc_APP_MODULE_PRESENT 35 | 36 | 37 | /* 38 | ********************************************************************************************************* 39 | ********************************************************************************************************* 40 | * INCLUDE FILES 41 | ********************************************************************************************************* 42 | ********************************************************************************************************* 43 | */ 44 | 45 | #include 46 | 47 | 48 | /* 49 | ********************************************************************************************************* 50 | ********************************************************************************************************* 51 | * DEFINES 52 | ********************************************************************************************************* 53 | ********************************************************************************************************* 54 | */ 55 | 56 | /* 57 | ********************************************************************************************************* 58 | * BROKER DEFINES 59 | ********************************************************************************************************* 60 | */ 61 | 62 | /* Broker's host name or IP address. */ 63 | /* TODO : Specifiy address/name of the broker. */ 64 | #define APP_MQTTc_BROKER_NAME "broker.mqttdashboard.com" 65 | 66 | /* Username from MQTT server portal, if any. */ 67 | /* TODO : Specify your own username. */ 68 | #define APP_MQTTc_USERNAME "user@domain.org" 69 | /* Password or MD5 hash of your password. */ 70 | #define APP_MQTTc_PASSWORD "password" /* TODO : Specify your own password. */ 71 | 72 | #define APP_MQTTc_CLIENT_ID_NAME "App_MQTT_TestClientID" 73 | 74 | 75 | /* 76 | ********************************************************************************************************* 77 | * INTERNAL TASK DEFINES 78 | ********************************************************************************************************* 79 | */ 80 | 81 | #define APP_MQTTc_TASK_STK_SIZE 2048u 82 | #define APP_MQTTc_TASK_PRIO 8u 83 | 84 | #define APP_MQTTc_INTERNAL_TASK_DLY 0u 85 | 86 | 87 | /* 88 | ********************************************************************************************************* 89 | * SOCKET DEFINES 90 | ********************************************************************************************************* 91 | */ 92 | 93 | #define APP_MQTTc_INACTIVITY_TIMEOUT_s 30u 94 | 95 | 96 | /* 97 | ********************************************************************************************************* 98 | ********************************************************************************************************* 99 | * FUNCTION PROTOTYPES 100 | ********************************************************************************************************* 101 | ********************************************************************************************************* 102 | */ 103 | 104 | CPU_BOOLEAN AppMQTTc_Init (void); 105 | 106 | 107 | /* 108 | ********************************************************************************************************* 109 | ********************************************************************************************************* 110 | * MODULE END 111 | ********************************************************************************************************* 112 | ********************************************************************************************************* 113 | */ 114 | 115 | #endif /* MQTTc_APP_MODULE_PRESENT */ 116 | 117 | -------------------------------------------------------------------------------- /Client/Examples/app_mqtt-c_echo.c: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * EXAMPLE CODE 4 | * 5 | * This file is provided as an example on how to use Micrium products. 6 | * 7 | * Please feel free to use any application code labeled as 'EXAMPLE CODE' in 8 | * your application products. Example code may be used as is, in whole or in 9 | * part, or may be used as a reference only. This file can be modified as 10 | * required to meet the end-product requirements. 11 | * 12 | ********************************************************************************************************* 13 | */ 14 | 15 | /* 16 | ********************************************************************************************************* 17 | * 18 | * MQTTc APPLICATION 19 | * 20 | * Filename : app_mqtt-c_echo.c 21 | * Version : V1.02.00 22 | ********************************************************************************************************* 23 | */ 24 | 25 | /* 26 | ********************************************************************************************************* 27 | ********************************************************************************************************* 28 | * INCLUDE FILES 29 | ********************************************************************************************************* 30 | ********************************************************************************************************* 31 | */ 32 | 33 | #define APP_MQTTc_ECHO_MODULE 34 | 35 | #include 36 | #include 37 | 38 | #include "app_mqtt-c.h" 39 | 40 | #include 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | #include 48 | 49 | #include 50 | 51 | #include 52 | 53 | 54 | /* 55 | ********************************************************************************************************* 56 | ********************************************************************************************************* 57 | * LOCAL DEFINES 58 | ********************************************************************************************************* 59 | ********************************************************************************************************* 60 | */ 61 | 62 | #define APP_MQTTc_MSG_QTY 3u 63 | #define APP_MQTTc_MSG_LEN_MAX 1024u 64 | #define APP_MQTTc_PAYLOAD_LEN_MAX 64u 65 | 66 | #define APP_MQTTc_DOMAIN_PUBLISH_STATUS "domain/status" 67 | #define APP_MQTTc_DOMAIN_PUBLISH_STATUS_QoS 2u 68 | 69 | #define APP_MQTTc_DOMAIN_PUBLISH_ECHO "domain/echo" 70 | #define APP_MQTTc_DOMAIN_PUBLISH_ECHO_QoS 0u 71 | 72 | #define APP_MQTTc_DOMAIN_SUBSCRIBE_LISTEN "domain/listen" 73 | #define APP_MQTTc_DOMAIN_SUBSCRIBE_LISTEN_QoS 1u 74 | 75 | 76 | /* 77 | ********************************************************************************************************* 78 | ********************************************************************************************************* 79 | * LOCAL GLOBAL VARIABLES 80 | ********************************************************************************************************* 81 | ********************************************************************************************************* 82 | */ 83 | 84 | static CPU_INT08U AppMQTTc_TaskStk[APP_MQTTc_TASK_STK_SIZE]; 85 | 86 | static MQTTc_CONN AppMQTTc_Conn; 87 | static MQTTc_MSG AppMQTTc_StatusMsg; 88 | static MQTTc_MSG AppMQTTc_EchoMsg; 89 | static MQTTc_MSG AppMQTTc_ListenRxMsg; 90 | static CPU_INT08U AppMQTTc_BufTbl[APP_MQTTc_MSG_QTY][APP_MQTTc_MSG_LEN_MAX]; 91 | static CPU_CHAR AppMQTTc_Payload[APP_MQTTc_PAYLOAD_LEN_MAX]; 92 | 93 | static CPU_BOOLEAN App_MQTTc_StatusMsgIsAvail; 94 | static CPU_BOOLEAN App_MQTTc_EchoMsgIsAvail; 95 | 96 | 97 | const NET_TASK_CFG AppMQTTc_TaskCfg = { /* Cfg for MQTTc internal task. */ 98 | APP_MQTTc_TASK_PRIO, /* MQTTc internal task prio. */ 99 | APP_MQTTc_TASK_STK_SIZE, /* MQTTc internal task stack size. */ 100 | AppMQTTc_TaskStk /* Ptr to start of MQTTc internal stack. */ 101 | }; 102 | 103 | 104 | const MQTTc_CFG AppMQTTc_Cfg = { 105 | APP_MQTTc_MSG_QTY, 106 | APP_MQTTc_INACTIVITY_TIMEOUT_s, 107 | APP_MQTTc_INTERNAL_TASK_DLY 108 | }; 109 | 110 | 111 | /* 112 | ********************************************************************************************************* 113 | ********************************************************************************************************* 114 | * LOCAL FUNCTION PROTOTYPES 115 | ********************************************************************************************************* 116 | ********************************************************************************************************* 117 | */ 118 | 119 | static void AppMQTTc_OnCmplCallbackFnct ( MQTTc_CONN *p_conn, 120 | MQTTc_MSG *p_msg, 121 | void *p_arg, 122 | MQTTc_ERR err); 123 | 124 | static void AppMQTTc_OnConnectCmplCallbackFnct ( MQTTc_CONN *p_conn, 125 | MQTTc_MSG *p_msg, 126 | void *p_arg, 127 | MQTTc_ERR err); 128 | 129 | static void AppMQTTc_OnPublishCmplCallbackFnct ( MQTTc_CONN *p_conn, 130 | MQTTc_MSG *p_msg, 131 | void *p_arg, 132 | MQTTc_ERR err); 133 | 134 | static void AppMQTTc_OnSubscribeCmplCallbackFnct( MQTTc_CONN *p_conn, 135 | MQTTc_MSG *p_msg, 136 | void *p_arg, 137 | MQTTc_ERR err); 138 | 139 | static void AppMQTTc_OnPublishRxCallbackFnct ( MQTTc_CONN *p_conn, 140 | const CPU_CHAR *topic_name_str, 141 | CPU_INT32U topic_len, 142 | const CPU_CHAR *p_payload, 143 | CPU_INT32U payload_len, 144 | void *p_arg, 145 | MQTTc_ERR err); 146 | 147 | static void AppMQTTc_OnErrCallbackFnct ( MQTTc_CONN *p_conn, 148 | void *p_arg, 149 | MQTTc_ERR err); 150 | 151 | 152 | /* 153 | ********************************************************************************************************* 154 | * AppMQTTc_Init() 155 | * 156 | * Description : Initialize the MQTT-client module. 157 | * 158 | * Arguments : none. 159 | * 160 | * Return(s) : DEF_OK, if NO error(s), 161 | * DEF_FAIL, otherwise. 162 | * 163 | * Caller(s) : Application. 164 | * 165 | * Note(s) : none. 166 | ********************************************************************************************************* 167 | */ 168 | 169 | CPU_BOOLEAN AppMQTTc_Init (void) 170 | { 171 | MQTTc_ERR err_mqttc; 172 | 173 | 174 | App_MQTTc_StatusMsgIsAvail = DEF_YES; 175 | App_MQTTc_EchoMsgIsAvail = DEF_YES; 176 | 177 | MQTTc_Init(&AppMQTTc_Cfg, 178 | &AppMQTTc_TaskCfg, 179 | DEF_NULL, 180 | &err_mqttc); 181 | if (err_mqttc != MQTTc_ERR_NONE) { 182 | printf("!!! APP ERROR !!! Failed to init MQTTc module. Err: %i\n\r.", err_mqttc); 183 | return (DEF_FAIL); 184 | } 185 | 186 | MQTTc_MsgClr(&AppMQTTc_StatusMsg, &err_mqttc); 187 | MQTTc_MsgSetParam(&AppMQTTc_StatusMsg, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_BufTbl[0u], &err_mqttc); 188 | MQTTc_MsgSetParam(&AppMQTTc_StatusMsg, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *) APP_MQTTc_MSG_LEN_MAX, &err_mqttc); 189 | 190 | MQTTc_MsgClr(&AppMQTTc_EchoMsg, &err_mqttc); 191 | MQTTc_MsgSetParam(&AppMQTTc_EchoMsg, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_BufTbl[1u], &err_mqttc); 192 | MQTTc_MsgSetParam(&AppMQTTc_EchoMsg, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *) APP_MQTTc_MSG_LEN_MAX, &err_mqttc); 193 | 194 | MQTTc_MsgClr(&AppMQTTc_ListenRxMsg, &err_mqttc); 195 | MQTTc_MsgSetParam(&AppMQTTc_ListenRxMsg, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_BufTbl[2u], &err_mqttc); 196 | MQTTc_MsgSetParam(&AppMQTTc_ListenRxMsg, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *) APP_MQTTc_MSG_LEN_MAX, &err_mqttc); 197 | 198 | 199 | MQTTc_ConnClr(&AppMQTTc_Conn, 200 | &err_mqttc); 201 | if (err_mqttc != MQTTc_ERR_NONE) { 202 | printf("!!! APP ERROR !!! Failed to clr MQTTc connection object. Err: %i\n\r.", err_mqttc); 203 | return (DEF_FAIL); 204 | } 205 | 206 | /* Err handling should be done, in your application. */ 207 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_BROKER_NAME, (void *) APP_MQTTc_BROKER_NAME, &err_mqttc); 208 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CLIENT_ID_STR, (void *)"App_MQTT_TestClientID", &err_mqttc); 209 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_USERNAME_STR, (void *) APP_MQTTc_USERNAME, &err_mqttc); 210 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_PASSWORD_STR, (void *) APP_MQTTc_PASSWORD, &err_mqttc); 211 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_KEEP_ALIVE_TMR_SEC, (void *) 1000u, &err_mqttc); 212 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_COMPL, (void *) AppMQTTc_OnCmplCallbackFnct, &err_mqttc); 213 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_CONNECT_CMPL, (void *) AppMQTTc_OnConnectCmplCallbackFnct, &err_mqttc); 214 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_CMPL, (void *) AppMQTTc_OnPublishCmplCallbackFnct, &err_mqttc); 215 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_SUBSCRIBE_CMPL, (void *) AppMQTTc_OnSubscribeCmplCallbackFnct, &err_mqttc); 216 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_RX, (void *) AppMQTTc_OnPublishRxCallbackFnct, &err_mqttc); 217 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_ERR_CALLBACK, (void *) AppMQTTc_OnErrCallbackFnct, &err_mqttc); 218 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_TIMEOUT_MS, (void *) 30000u, &err_mqttc); 219 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_PUBLISH_RX_MSG_PTR, (void *)&AppMQTTc_ListenRxMsg, &err_mqttc); 220 | 221 | printf("Done setting params.\r\n"); 222 | 223 | MQTTc_ConnOpen(&AppMQTTc_Conn, /* Open conn to MQTT server with parameters set in Conn.*/ 224 | MQTTc_FLAGS_NONE, 225 | &err_mqttc); 226 | if (err_mqttc != MQTTc_ERR_NONE) { 227 | printf("!!! APP ERROR !!! Failed to open TCP connection to MQTT server. Err: %i\n\r.", err_mqttc); 228 | return (DEF_FAIL); /* Failed to open TCP connection to MQTT server. */ 229 | } 230 | printf("Done opening conn.\r\n"); 231 | 232 | App_MQTTc_StatusMsgIsAvail = DEF_NO; 233 | MQTTc_Connect(&AppMQTTc_Conn, /* Send CONNECT msg to MQTT server. */ 234 | &AppMQTTc_StatusMsg, 235 | &err_mqttc); 236 | if (err_mqttc != MQTTc_ERR_NONE) { 237 | printf("!!! APP ERROR !!! Failed to process Connect msg req. Err: %i\n\r.", err_mqttc); 238 | return (DEF_FAIL); /* Failed to process MQTT CONNECT msg. */ 239 | } 240 | printf("Done calling MQTTc_Connect().\r\n"); 241 | 242 | return (DEF_OK); 243 | } 244 | 245 | 246 | /* 247 | ********************************************************************************************************* 248 | * AppMQTTc_OnCmplCallbackFnct() 249 | * 250 | * Description : Generic callback function for MQTTc module. 251 | * 252 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 253 | * 254 | * p_msg Pointer to MQTTc Message object used for operation. 255 | * 256 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 257 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 258 | * 259 | * err Error code from processing message. 260 | * 261 | * Return(s) : none. 262 | * 263 | * Caller(s) : MQTTc module. 264 | * 265 | * Note(s) : none. 266 | ********************************************************************************************************* 267 | */ 268 | 269 | static void AppMQTTc_OnCmplCallbackFnct (MQTTc_CONN *p_conn, 270 | MQTTc_MSG *p_msg, 271 | void *p_arg, 272 | MQTTc_ERR err) 273 | { 274 | (void)&p_conn; 275 | (void)&p_arg; 276 | 277 | if (err != MQTTc_ERR_NONE) { 278 | printf("Operation completed with err (%i). ", err); 279 | } 280 | 281 | switch (p_msg->Type) { 282 | case MQTTc_MSG_TYPE_CONNECT: /* Gen callback called for event type: Connect Cmpl. */ 283 | printf("Gen callback called for event type: Connect Cmpl.\n\r"); 284 | break; 285 | 286 | 287 | case MQTTc_MSG_TYPE_PUBLISH: /* Gen callback called for event type: Publish Cmpl. */ 288 | printf("Gen callback called for event type: Publish Cmpl.\n\r"); 289 | break; 290 | 291 | 292 | case MQTTc_MSG_TYPE_SUBSCRIBE: /* Gen callback called for event type: Subscribe Cmpl. */ 293 | printf("Gen callback called for event type: Subscribe Cmpl.\n\r"); 294 | break; 295 | 296 | 297 | case MQTTc_MSG_TYPE_UNSUBSCRIBE: /* Gen callback called for event type: Unsubscribe Cmpl.*/ 298 | printf("Gen callback called for event type: Unsubscribe Cmpl.\n\r"); 299 | break; 300 | 301 | 302 | case MQTTc_MSG_TYPE_PINGREQ: /* Gen callback called for event type: PingReq Cmpl. */ 303 | printf("Gen callback called for event type: PingReq Cmpl.\n\r"); 304 | break; 305 | 306 | 307 | case MQTTc_MSG_TYPE_DISCONNECT: /* Gen callback called for event type: Disconnect Cmpl. */ 308 | printf("Gen callback called for event type: Disconnect Cmpl.\n\r"); 309 | break; 310 | 311 | 312 | default: 313 | printf("Gen callback called for event type: default. !!! ERROR !!! %i\n\r", p_msg->Type); 314 | break; 315 | } 316 | } 317 | 318 | 319 | /* 320 | ********************************************************************************************************* 321 | * AppMQTTc_OnConnectCmplCallbackFnct() 322 | * 323 | * Description : Callback function for MQTTc module called when a CONNECT operation has completed. 324 | * 325 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 326 | * 327 | * p_msg Pointer to MQTTc Message object used for operation. 328 | * 329 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 330 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 331 | * 332 | * err Error code from processing CONNECT message. 333 | * 334 | * Return(s) : none. 335 | * 336 | * Caller(s) : MQTTc module. 337 | * 338 | * Note(s) : none. 339 | ********************************************************************************************************* 340 | */ 341 | 342 | static void AppMQTTc_OnConnectCmplCallbackFnct (MQTTc_CONN *p_conn, 343 | MQTTc_MSG *p_msg, 344 | void *p_arg, 345 | MQTTc_ERR err) 346 | { 347 | (void)&p_arg; 348 | 349 | if (err != MQTTc_ERR_NONE) { 350 | printf("ConnectCmpl callback called with err (%i).\n\r", err); 351 | } else { 352 | printf("ConnectCmpl callback called without err, ready to send/receive messages.\n\r"); 353 | 354 | MQTTc_Subscribe(p_conn, 355 | p_msg, /* Re-using msg used by completed CONNECT msg. */ 356 | APP_MQTTc_DOMAIN_SUBSCRIBE_LISTEN, 357 | APP_MQTTc_DOMAIN_SUBSCRIBE_LISTEN_QoS, 358 | &err); 359 | if (err != MQTTc_ERR_NONE) { 360 | printf("!!! APP ERROR !!! Subscribe failed. Err: %i\n\r.", err); 361 | } 362 | } 363 | } 364 | 365 | 366 | /* 367 | ********************************************************************************************************* 368 | * AppMQTTc_OnPublishCmplCallbackFnct() 369 | * 370 | * Description : Callback function for MQTTc module called when a PUBLISH operation has completed. 371 | * 372 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 373 | * 374 | * p_msg Pointer to MQTTc Message object used for operation. 375 | * 376 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 377 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 378 | * 379 | * err Error code from processing PUBLISH message. 380 | * 381 | * Return(s) : none. 382 | * 383 | * Caller(s) : MQTTc module. 384 | * 385 | * Note(s) : none. 386 | ********************************************************************************************************* 387 | */ 388 | 389 | static void AppMQTTc_OnPublishCmplCallbackFnct (MQTTc_CONN *p_conn, 390 | MQTTc_MSG *p_msg, 391 | void *p_arg, 392 | MQTTc_ERR err) 393 | { 394 | (void)&p_conn; 395 | (void)&p_arg; 396 | 397 | if (err != MQTTc_ERR_NONE) { 398 | printf("PublishCmpl callback called with error (%i). CANNOT continue.\n\r", err); 399 | } else { 400 | if (p_msg == &AppMQTTc_StatusMsg) { 401 | printf("PublishCmpl callback called for status. Marking message as available.\n\r"); 402 | App_MQTTc_StatusMsgIsAvail = DEF_YES; /* Mark msg as re-available. */ 403 | } else { 404 | printf("PublishCmpl callback called for status. Marking message as available.\n\r"); 405 | App_MQTTc_EchoMsgIsAvail = DEF_YES; /* Mark msg as re-available. */ 406 | } 407 | } 408 | } 409 | 410 | 411 | /* 412 | ********************************************************************************************************* 413 | * AppMQTTc_OnSubscribeCmplCallbackFnct() 414 | * 415 | * Description : Callback function for MQTTc module called when a SUBSCRIBE operation has completed. 416 | * 417 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 418 | * 419 | * p_msg Pointer to MQTTc Message object used for operation. 420 | * 421 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 422 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 423 | * 424 | * err Error code from processing SUBSCRIBE message. 425 | * 426 | * Return(s) : none. 427 | * 428 | * Caller(s) : MQTTc module. 429 | * 430 | * Note(s) : none. 431 | ********************************************************************************************************* 432 | */ 433 | 434 | static void AppMQTTc_OnSubscribeCmplCallbackFnct (MQTTc_CONN *p_conn, 435 | MQTTc_MSG *p_msg, 436 | void *p_arg, 437 | MQTTc_ERR err) 438 | { 439 | CPU_INT16U payload_len; 440 | CPU_CHAR *p_payload = &AppMQTTc_Payload[0]; 441 | 442 | 443 | (void)&p_arg; 444 | 445 | if (err != MQTTc_ERR_NONE) { 446 | printf("SubscribeCmpl callback called with error (%i). CANNOT continue.\n\r", err); 447 | } else { 448 | printf("SubscribeCmpl callback called. Publishing status.\n\r"); 449 | 450 | Str_Copy(p_payload, /* Copy the string to publish to the payload buffer */ 451 | "Now listening on specified topic"); 452 | payload_len = Str_Len(p_payload); /* Determine the length of the string we're publishing */ 453 | 454 | MQTTc_Publish(p_conn, 455 | p_msg, /* Re-using msg used by completed SUBSCRIBE msg. */ 456 | APP_MQTTc_DOMAIN_PUBLISH_STATUS, 457 | APP_MQTTc_DOMAIN_PUBLISH_STATUS_QoS, 458 | DEF_NO, 459 | p_payload, 460 | payload_len, 461 | &err); 462 | if (err != MQTTc_ERR_NONE) { 463 | printf("!!! APP ERROR !!! Failed to Publish Status. Err: %i\n\r.", err); 464 | } 465 | } 466 | } 467 | 468 | 469 | /* 470 | ********************************************************************************************************* 471 | * AppMQTTc_OnPublishRxCallbackFnct() 472 | * 473 | * Description : Callback function for MQTTc module called when a PUBLISH message has been received. 474 | * 475 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 476 | * 477 | * topic_name_str String containing the topic of the message received. NOT NULL-terminated. 478 | * 479 | * topic_len Length of the topic. 480 | * 481 | * p_payload NULL-terminated buffer containing the payload received. 482 | * 483 | * payload_len Length of the received payload 484 | * 485 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 486 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 487 | * 488 | * Return(s) : none. 489 | * 490 | * Caller(s) : MQTTc module. 491 | * 492 | * Note(s) : none. 493 | ********************************************************************************************************* 494 | */ 495 | 496 | static void AppMQTTc_OnPublishRxCallbackFnct ( MQTTc_CONN *p_conn, 497 | const CPU_CHAR *topic_name_str, 498 | CPU_INT32U topic_len, 499 | const CPU_CHAR *p_payload, 500 | CPU_INT32U payload_len, 501 | void *p_arg, 502 | MQTTc_ERR err) 503 | { 504 | CPU_INT16U payload_len; 505 | CPU_CHAR *p_payload = &AppMQTTc_Payload[0]; 506 | 507 | 508 | (void)&p_arg; 509 | 510 | if (err != MQTTc_ERR_NONE) { 511 | printf("!!! APP ERROR !!! Err detected when receiving a PUBLISH message (%i). NOT echoing.\n\r", err); 512 | return; 513 | } 514 | 515 | printf("Received PUBLISH message from server. Topic is %.*s.", topic_len, topic_name_str); 516 | printf(" Message is %s.\n\r", p_payload); 517 | 518 | if (App_MQTTc_EchoMsgIsAvail == DEF_YES) { 519 | App_MQTTc_EchoMsgIsAvail = DEF_NO; 520 | MQTTc_Publish(p_conn, 521 | &AppMQTTc_EchoMsg, 522 | APP_MQTTc_DOMAIN_PUBLISH_STATUS, 523 | APP_MQTTc_DOMAIN_PUBLISH_STATUS_QoS, 524 | DEF_NO, 525 | p_payload, 526 | Str_Len(p_payload), 527 | &err); 528 | if (err != MQTTc_ERR_NONE) { 529 | printf("!!! APP ERROR !!! Failed to Echo received message. Err: %i\n\r.", err); 530 | } 531 | } else if (App_MQTTc_StatusMsgIsAvail == DEF_YES) { 532 | App_MQTTc_StatusMsgIsAvail = DEF_NO; 533 | 534 | Str_Copy(p_payload, /* Copy the string to publish to the payload buffer */ 535 | "Unable to send echo msg: msg unavailable."); 536 | payload_len = Str_Len(p_payload); /* Determine the length of the string we're publishing */ 537 | 538 | MQTTc_Publish(p_conn, 539 | &AppMQTTc_StatusMsg, 540 | APP_MQTTc_DOMAIN_PUBLISH_STATUS, 541 | APP_MQTTc_DOMAIN_PUBLISH_STATUS_QoS, 542 | DEF_NO, 543 | p_payload, 544 | payload_len, 545 | &err); 546 | if (err != MQTTc_ERR_NONE) { 547 | printf("!!! APP ERROR !!! Failed to Publish Status. Err: %i\n\r.", err); 548 | } 549 | } else { 550 | printf("!!! APP ERROR !!! Echo and Status messages are both unavailable. Cannot send either Echo or Status to broker.\r\n"); 551 | } 552 | } 553 | 554 | 555 | /* 556 | ********************************************************************************************************* 557 | * AppMQTTc_OnErrCallbackFnct() 558 | * 559 | * Description : Callback function for MQTTc module called when an error occurs. 560 | * 561 | * Arguments : p_conn Pointer to MQTTc Connection object on which error occurred. 562 | * 563 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 564 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 565 | * 566 | * err Error code. 567 | * 568 | * Return(s) : none. 569 | * 570 | * Caller(s) : MQTTc module. 571 | * 572 | * Note(s) : none. 573 | ********************************************************************************************************* 574 | */ 575 | 576 | static void AppMQTTc_OnErrCallbackFnct (MQTTc_CONN *p_conn, 577 | void *p_arg, 578 | MQTTc_ERR err) 579 | { 580 | CPU_INT16U payload_len; 581 | CPU_CHAR *p_payload = &AppMQTTc_Payload[0]; 582 | 583 | 584 | (void)&p_conn; 585 | (void)&p_arg; 586 | 587 | printf("!!! APP ERROR !!! Err detected via OnErr callback. Err = %i.", err); 588 | 589 | if (App_MQTTc_StatusMsgIsAvail == DEF_YES) { 590 | App_MQTTc_StatusMsgIsAvail = DEF_NO; 591 | 592 | Str_Copy(p_payload, /* Copy the string to publish to the payload buffer */ 593 | "Err detected"); 594 | payload_len = Str_Len(p_payload); /* Determine the length of the string we're publishing */ 595 | 596 | printf("Sending status.\r\n"); 597 | MQTTc_Publish(p_conn, 598 | &AppMQTTc_StatusMsg, 599 | APP_MQTTc_DOMAIN_PUBLISH_STATUS, 600 | APP_MQTTc_DOMAIN_PUBLISH_STATUS_QoS, 601 | DEF_NO, 602 | p_payload, 603 | payload_len, 604 | &err); 605 | if (err != MQTTc_ERR_NONE) { 606 | printf("!!! APP ERROR !!! Failed to Publish Status. Err: %i\n\r.", err); 607 | } 608 | } else { 609 | printf("Unable to send status, message is not available.\r\n"); 610 | } 611 | } 612 | -------------------------------------------------------------------------------- /Client/Examples/app_mqtt-c_init.c: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * EXAMPLE CODE 4 | * 5 | * This file is provided as an example on how to use Micrium products. 6 | * 7 | * Please feel free to use any application code labeled as 'EXAMPLE CODE' in 8 | * your application products. Example code may be used as is, in whole or in 9 | * part, or may be used as a reference only. This file can be modified as 10 | * required to meet the end-product requirements. 11 | * 12 | ********************************************************************************************************* 13 | */ 14 | 15 | /* 16 | ********************************************************************************************************* 17 | * 18 | * MQTTc APPLICATION 19 | * 20 | * Filename : app_mqtt-c_init.c 21 | * Version : V1.02.00 22 | ********************************************************************************************************* 23 | */ 24 | 25 | /* 26 | ********************************************************************************************************* 27 | ********************************************************************************************************* 28 | * INCLUDE FILES 29 | ********************************************************************************************************* 30 | ********************************************************************************************************* 31 | */ 32 | 33 | #define APP_MQTTc_INIT_MODULE 34 | 35 | #include 36 | #include 37 | 38 | #include "app_mqtt-c.h" 39 | 40 | #include 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | #include 48 | 49 | #include 50 | 51 | #include 52 | 53 | 54 | /* 55 | ********************************************************************************************************* 56 | ********************************************************************************************************* 57 | * LOCAL DEFINES 58 | ********************************************************************************************************* 59 | ********************************************************************************************************* 60 | */ 61 | 62 | #define APP_MQTTc_MSG_QTY 2u 63 | 64 | #define APP_MQTTc_MSG_LEN_MAX 128u 65 | #define APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX 128u 66 | 67 | 68 | /* 69 | ********************************************************************************************************* 70 | ********************************************************************************************************* 71 | * LOCAL GLOBAL VARIABLES 72 | ********************************************************************************************************* 73 | ********************************************************************************************************* 74 | */ 75 | 76 | static CPU_INT08U AppMQTTc_TaskStk[APP_MQTTc_TASK_STK_SIZE]; 77 | 78 | static MQTTc_CONN AppMQTTc_Conn; 79 | 80 | static MQTTc_MSG AppMQTTc_Msg; 81 | static CPU_INT08U AppMQTTc_MsgBuf[APP_MQTTc_MSG_LEN_MAX]; 82 | 83 | static MQTTc_MSG AppMQTTc_MsgPublishRx; 84 | static MQTTc_MSG AppMQTTc_MsgPublishRxBuf[APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX]; 85 | 86 | 87 | const NET_TASK_CFG AppMQTTc_TaskCfg = { /* Cfg for MQTTc internal task. */ 88 | APP_MQTTc_TASK_PRIO, /* MQTTc internal task prio. */ 89 | APP_MQTTc_TASK_STK_SIZE, /* MQTTc internal task stack size. */ 90 | AppMQTTc_TaskStk /* Ptr to start of MQTTc internal stack. */ 91 | }; 92 | 93 | 94 | const MQTTc_CFG AppMQTTc_Cfg = { 95 | APP_MQTTc_MSG_QTY, 96 | APP_MQTTc_INACTIVITY_TIMEOUT_s, 97 | APP_MQTTc_INTERNAL_TASK_DLY 98 | }; 99 | 100 | 101 | /* 102 | ********************************************************************************************************* 103 | ********************************************************************************************************* 104 | * LOCAL FUNCTION PROTOTYPES 105 | ********************************************************************************************************* 106 | ********************************************************************************************************* 107 | */ 108 | 109 | static void AppMQTTc_OnCmplCallbackFnct (MQTTc_CONN *p_conn, 110 | MQTTc_MSG *p_msg, 111 | void *p_arg, 112 | MQTTc_ERR err); 113 | 114 | static void AppMQTTc_OnConnectCmplCallbackFnct(MQTTc_CONN *p_conn, 115 | MQTTc_MSG *p_msg, 116 | void *p_arg, 117 | MQTTc_ERR err); 118 | 119 | static void AppMQTTc_OnErrCallbackFnct (MQTTc_CONN *p_conn, 120 | void *p_arg, 121 | MQTTc_ERR err); 122 | 123 | 124 | /* 125 | ********************************************************************************************************* 126 | * AppMQTTc_Init() 127 | * 128 | * Description : Initialize the application MQTT-client module. 129 | * 130 | * Arguments : none. 131 | * 132 | * Return(s) : DEF_OK, if NO error(s), 133 | * DEF_FAIL, otherwise. 134 | * 135 | * Caller(s) : Application. 136 | * 137 | * Note(s) : none. 138 | ********************************************************************************************************* 139 | */ 140 | 141 | CPU_BOOLEAN AppMQTTc_Init (void) 142 | { 143 | MQTTc_ERR err_mqttc; 144 | 145 | 146 | MQTTc_Init(&AppMQTTc_Cfg, 147 | &AppMQTTc_TaskCfg, 148 | DEF_NULL, 149 | &err_mqttc); 150 | if (err_mqttc != MQTTc_ERR_NONE) { 151 | printf("ERROR - Failed to init MQTTc module. Err: %i\n\r.", err_mqttc); 152 | return (DEF_FAIL); 153 | } 154 | 155 | MQTTc_MsgClr(&AppMQTTc_Msg, &err_mqttc); 156 | if (err_mqttc != MQTTc_ERR_NONE) { 157 | printf("ERROR - Failed to clr msg object. Err: %i\n\r.", err_mqttc); 158 | return (DEF_FAIL); 159 | } 160 | 161 | MQTTc_MsgSetParam(&AppMQTTc_Msg, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_MsgBuf[0u], &err_mqttc); 162 | if (err_mqttc != MQTTc_ERR_NONE) { 163 | printf("ERROR - Failed to set buf ptr param. Err: %i\n\r.", err_mqttc); 164 | return (DEF_FAIL); 165 | } 166 | 167 | MQTTc_MsgSetParam(&AppMQTTc_Msg, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *)APP_MQTTc_MSG_LEN_MAX, &err_mqttc); 168 | if (err_mqttc != MQTTc_ERR_NONE) { 169 | printf("ERROR - Failed to set buf len param. Err: %i\n\r.", err_mqttc); 170 | return (DEF_FAIL); 171 | } 172 | 173 | MQTTc_MsgClr(&AppMQTTc_MsgPublishRx, &err_mqttc); 174 | if (err_mqttc != MQTTc_ERR_NONE) { 175 | printf("ERROR - Failed to clr msg object. Err: %i\n\r.", err_mqttc); 176 | return (DEF_FAIL); 177 | } 178 | 179 | MQTTc_MsgSetParam(&AppMQTTc_MsgPublishRx, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_MsgPublishRxBuf[0u], &err_mqttc); 180 | if (err_mqttc != MQTTc_ERR_NONE) { 181 | printf("ERROR - Failed to set buf ptr param. Err: %i\n\r.", err_mqttc); 182 | return (DEF_FAIL); 183 | } 184 | 185 | MQTTc_MsgSetParam(&AppMQTTc_MsgPublishRx, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *)APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX, &err_mqttc); 186 | if (err_mqttc != MQTTc_ERR_NONE) { 187 | printf("ERROR - Failed to set buf len param. Err: %i\n\r.", err_mqttc); 188 | return (DEF_FAIL); 189 | } 190 | 191 | MQTTc_ConnClr(&AppMQTTc_Conn, 192 | &err_mqttc); 193 | if (err_mqttc != MQTTc_ERR_NONE) { 194 | printf("ERROR - Failed to clr MQTTc connection object. Err: %i\n\r.", err_mqttc); 195 | return (DEF_FAIL); 196 | } 197 | 198 | /* Err handling should be done in your application. */ 199 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_BROKER_NAME, (void *) APP_MQTTc_BROKER_NAME, &err_mqttc); 200 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CLIENT_ID_STR, (void *) APP_MQTTc_CLIENT_ID_NAME, &err_mqttc); 201 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_USERNAME_STR, (void *) APP_MQTTc_USERNAME, &err_mqttc); 202 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_PASSWORD_STR, (void *) APP_MQTTc_PASSWORD, &err_mqttc); 203 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_KEEP_ALIVE_TMR_SEC, (void *) 1000u, &err_mqttc); 204 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_COMPL, (void *) AppMQTTc_OnCmplCallbackFnct, &err_mqttc); 205 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_CONNECT_CMPL, (void *) AppMQTTc_OnConnectCmplCallbackFnct, &err_mqttc); 206 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_ERR_CALLBACK, (void *) AppMQTTc_OnErrCallbackFnct, &err_mqttc); 207 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_PUBLISH_RX_MSG_PTR, (void *)&AppMQTTc_MsgPublishRx, &err_mqttc); 208 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_TIMEOUT_MS, (void *) 30000u, &err_mqttc); 209 | 210 | MQTTc_ConnOpen(&AppMQTTc_Conn, /* Open conn to MQTT server with parameters set in Conn.*/ 211 | MQTTc_FLAGS_NONE, 212 | &err_mqttc); 213 | if (err_mqttc != MQTTc_ERR_NONE) { 214 | printf("!!! APP ERROR !!! Failed to open TCP connection to MQTT server. Err: %i\n\r.", err_mqttc); 215 | return (DEF_FAIL); /* Failed to open TCP connection to MQTT server. */ 216 | } 217 | 218 | MQTTc_Connect(&AppMQTTc_Conn, /* Send CONNECT msg to MQTT server. */ 219 | &AppMQTTc_Msg, 220 | &err_mqttc); 221 | if (err_mqttc != MQTTc_ERR_NONE) { 222 | printf("!!! APP ERROR !!! Failed to process Connect msg req. Err: %i\n\r.", err_mqttc); 223 | return (DEF_FAIL); /* Failed to process MQTT CONNECT msg. */ 224 | } 225 | 226 | printf("Initialization and CONNECT to server successful.\r\n"); 227 | 228 | return (DEF_OK); 229 | } 230 | 231 | 232 | /* 233 | ********************************************************************************************************* 234 | * AppMQTTc_OnCmplCallbackFnct() 235 | * 236 | * Description : Generic callback function for MQTTc module. 237 | * 238 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 239 | * 240 | * p_msg Pointer to MQTTc Message object used for operation. 241 | * 242 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 243 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 244 | * 245 | * err Error code from processing message. 246 | * 247 | * Return(s) : none. 248 | * 249 | * Caller(s) : MQTTc module. 250 | * 251 | * Note(s) : none. 252 | ********************************************************************************************************* 253 | */ 254 | 255 | static void AppMQTTc_OnCmplCallbackFnct (MQTTc_CONN *p_conn, 256 | MQTTc_MSG *p_msg, 257 | void *p_arg, 258 | MQTTc_ERR err) 259 | { 260 | (void)&p_conn; 261 | (void)&p_arg; 262 | 263 | if (err != MQTTc_ERR_NONE) { 264 | printf("Operation completed with err (%i). ", err); 265 | } 266 | 267 | switch (p_msg->Type) { 268 | case MQTTc_MSG_TYPE_CONNECT: /* Gen callback called for event type: Connect Cmpl. */ 269 | printf("Gen callback called for event type: Connect Cmpl.\n\r"); 270 | break; 271 | 272 | 273 | case MQTTc_MSG_TYPE_PUBLISH: /* Gen callback called for event type: Publish Cmpl. */ 274 | printf("Gen callback called for event type: Publish Cmpl.\n\r"); 275 | break; 276 | 277 | 278 | case MQTTc_MSG_TYPE_SUBSCRIBE: /* Gen callback called for event type: Subscribe Cmpl. */ 279 | printf("Gen callback called for event type: Subscribe Cmpl.\n\r"); 280 | break; 281 | 282 | 283 | case MQTTc_MSG_TYPE_UNSUBSCRIBE: /* Gen callback called for event type: Unsubscribe Cmpl.*/ 284 | printf("Gen callback called for event type: Unsubscribe Cmpl.\n\r"); 285 | break; 286 | 287 | 288 | case MQTTc_MSG_TYPE_PINGREQ: /* Gen callback called for event type: PingReq Cmpl. */ 289 | printf("Gen callback called for event type: PingReq Cmpl.\n\r"); 290 | break; 291 | 292 | 293 | case MQTTc_MSG_TYPE_DISCONNECT: /* Gen callback called for event type: Disconnect Cmpl. */ 294 | printf("Gen callback called for event type: Disconnect Cmpl.\n\r"); 295 | break; 296 | 297 | 298 | default: 299 | printf("Gen callback called for event type: default. !!! ERROR !!! %i\n\r", p_msg->Type); 300 | break; 301 | } 302 | } 303 | 304 | 305 | /* 306 | ********************************************************************************************************* 307 | * AppMQTTc_OnConnectCmplCallbackFnct() 308 | * 309 | * Description : Callback function for MQTTc module called when a CONNECT operation has completed. 310 | * 311 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 312 | * 313 | * p_msg Pointer to MQTTc Message object used for operation. 314 | * 315 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 316 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 317 | * 318 | * err Error code from processing CONNECT message. 319 | * 320 | * Return(s) : none. 321 | * 322 | * Caller(s) : MQTTc module. 323 | * 324 | * Note(s) : none. 325 | ********************************************************************************************************* 326 | */ 327 | 328 | static void AppMQTTc_OnConnectCmplCallbackFnct (MQTTc_CONN *p_conn, 329 | MQTTc_MSG *p_msg, 330 | void *p_arg, 331 | MQTTc_ERR err) 332 | { 333 | (void)&p_conn; 334 | (void)&p_msg; 335 | (void)&p_arg; 336 | 337 | if (err != MQTTc_ERR_NONE) { 338 | printf("ConnectCmpl callback called with err (%i).\n\r", err); 339 | } else { 340 | printf("ConnectCmpl callback called without err, ready to send/receive messages.\n\r"); 341 | } 342 | } 343 | 344 | 345 | /* 346 | ********************************************************************************************************* 347 | * AppMQTTc_OnErrCallbackFnct() 348 | * 349 | * Description : Callback function for MQTTc module called when an error occurs. 350 | * 351 | * Arguments : p_conn Pointer to MQTTc Connection object on which error occurred. 352 | * 353 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 354 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 355 | * 356 | * err Error code. 357 | * 358 | * Return(s) : none. 359 | * 360 | * Caller(s) : MQTTc module. 361 | * 362 | * Note(s) : none. 363 | ********************************************************************************************************* 364 | */ 365 | 366 | static void AppMQTTc_OnErrCallbackFnct (MQTTc_CONN *p_conn, 367 | void *p_arg, 368 | MQTTc_ERR err) 369 | { 370 | (void)&p_conn; 371 | (void)&p_arg; 372 | 373 | printf("!!! APP ERROR !!! Err detected via OnErr callback. Err = %i.\n\r", err); 374 | } 375 | -------------------------------------------------------------------------------- /Client/Examples/app_mqtt-c_publish.c: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * EXAMPLE CODE 4 | * 5 | * This file is provided as an example on how to use Micrium products. 6 | * 7 | * Please feel free to use any application code labeled as 'EXAMPLE CODE' in 8 | * your application products. Example code may be used as is, in whole or in 9 | * part, or may be used as a reference only. This file can be modified as 10 | * required to meet the end-product requirements. 11 | * 12 | ********************************************************************************************************* 13 | */ 14 | 15 | /* 16 | ********************************************************************************************************* 17 | * 18 | * MQTTc APPLICATION 19 | * 20 | * Filename : app_mqtt-c_publish.c 21 | * Version : V1.02.00 22 | ********************************************************************************************************* 23 | */ 24 | 25 | /* 26 | ********************************************************************************************************* 27 | ********************************************************************************************************* 28 | * INCLUDE FILES 29 | ********************************************************************************************************* 30 | ********************************************************************************************************* 31 | */ 32 | 33 | #define APP_MQTTc_MODULE 34 | 35 | #include 36 | #include 37 | 38 | #include "app_mqtt-c.h" 39 | 40 | #include 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | #include 48 | 49 | #include 50 | 51 | #include 52 | 53 | 54 | /* 55 | ********************************************************************************************************* 56 | ********************************************************************************************************* 57 | * LOCAL DEFINES 58 | ********************************************************************************************************* 59 | ********************************************************************************************************* 60 | */ 61 | 62 | #define APP_MQTTc_MSG_QTY 1u 63 | 64 | #define APP_MQTTc_MSG_LEN_MAX 128u 65 | #define APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX 512u 66 | 67 | /* Domain to which to publish. */ 68 | #define APP_MQTTc_DOMAIN_PUBLISH "domain/publish_topic" 69 | 70 | #define APP_MQTTc_PUBLISH_TEST_MSG "test publish" 71 | #define APP_MQTTc_PUBLISH_TEST_QoS 2u 72 | 73 | 74 | /* 75 | ********************************************************************************************************* 76 | ********************************************************************************************************* 77 | * LOCAL GLOBAL VARIABLES 78 | ********************************************************************************************************* 79 | ********************************************************************************************************* 80 | */ 81 | 82 | static CPU_INT08U AppMQTTc_TaskStk[APP_MQTTc_TASK_STK_SIZE]; 83 | 84 | static MQTTc_CONN AppMQTTc_Conn; 85 | 86 | static MQTTc_MSG AppMQTTc_Msg; 87 | static CPU_INT08U AppMQTTc_MsgBuf[APP_MQTTc_MSG_LEN_MAX]; 88 | static MQTTc_MSG AppMQTTc_MsgPublishRx; 89 | static CPU_INT08U AppMQTTc_MsgPublishRxBuf[APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX]; 90 | 91 | const NET_TASK_CFG AppMQTTc_TaskCfg = { /* Cfg for MQTTc internal task. */ 92 | APP_MQTTc_TASK_PRIO, /* MQTTc internal task prio. */ 93 | APP_MQTTc_TASK_STK_SIZE, /* MQTTc internal task stack size. */ 94 | AppMQTTc_TaskStk /* Ptr to start of MQTTc internal stack. */ 95 | }; 96 | 97 | 98 | const MQTTc_CFG AppMQTTc_Cfg = { 99 | APP_MQTTc_MSG_QTY, 100 | APP_MQTTc_INACTIVITY_TIMEOUT_s, 101 | APP_MQTTc_INTERNAL_TASK_DLY 102 | }; 103 | 104 | 105 | /* 106 | ********************************************************************************************************* 107 | ********************************************************************************************************* 108 | * LOCAL FUNCTION PROTOTYPES 109 | ********************************************************************************************************* 110 | ********************************************************************************************************* 111 | */ 112 | 113 | static void AppMQTTc_OnCmplCallbackFnct (MQTTc_CONN *p_conn, 114 | MQTTc_MSG *p_msg, 115 | void *p_arg, 116 | MQTTc_ERR err); 117 | 118 | static void AppMQTTc_OnConnectCmplCallbackFnct(MQTTc_CONN *p_conn, 119 | MQTTc_MSG *p_msg, 120 | void *p_arg, 121 | MQTTc_ERR err); 122 | 123 | static void AppMQTTc_OnPublishCmplCallbackFnct(MQTTc_CONN *p_conn, 124 | MQTTc_MSG *p_msg, 125 | void *p_arg, 126 | MQTTc_ERR err); 127 | 128 | static void AppMQTTc_OnErrCallbackFnct (MQTTc_CONN *p_conn, 129 | void *p_arg, 130 | MQTTc_ERR err); 131 | 132 | 133 | /* 134 | ********************************************************************************************************* 135 | * AppMQTTc_Init() 136 | * 137 | * Description : Initialize the application MQTT-client module. 138 | * 139 | * Arguments : none. 140 | * 141 | * Return(s) : DEF_OK, if NO error(s), 142 | * DEF_FAIL, otherwise. 143 | * 144 | * Caller(s) : Application. 145 | * 146 | * Note(s) : none. 147 | ********************************************************************************************************* 148 | */ 149 | 150 | CPU_BOOLEAN AppMQTTc_Init (void) 151 | { 152 | MQTTc_ERR err_mqttc; 153 | 154 | 155 | MQTTc_Init(&AppMQTTc_Cfg, 156 | &AppMQTTc_TaskCfg, 157 | DEF_NULL, 158 | &err_mqttc); 159 | if (err_mqttc != MQTTc_ERR_NONE) { 160 | printf("ERROR - Failed to init MQTTc module. Err: %i\n\r.", err_mqttc); 161 | return (DEF_FAIL); 162 | } 163 | 164 | MQTTc_MsgClr(&AppMQTTc_Msg, &err_mqttc); 165 | if (err_mqttc != MQTTc_ERR_NONE) { 166 | printf("ERROR - Failed to clr msg object. Err: %i\n\r.", err_mqttc); 167 | return (DEF_FAIL); 168 | } 169 | 170 | MQTTc_MsgSetParam(&AppMQTTc_Msg, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_MsgBuf[0u], &err_mqttc); 171 | if (err_mqttc != MQTTc_ERR_NONE) { 172 | printf("ERROR - Failed to set buf ptr param. Err: %i\n\r.", err_mqttc); 173 | return (DEF_FAIL); 174 | } 175 | 176 | MQTTc_MsgSetParam(&AppMQTTc_Msg, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *)APP_MQTTc_MSG_LEN_MAX, &err_mqttc); 177 | if (err_mqttc != MQTTc_ERR_NONE) { 178 | printf("ERROR - Failed to set buf len param. Err: %i\n\r.", err_mqttc); 179 | return (DEF_FAIL); 180 | } 181 | 182 | MQTTc_MsgClr(&AppMQTTc_MsgPublishRx, &err_mqttc); 183 | if (err_mqttc != MQTTc_ERR_NONE) { 184 | printf("ERROR - Failed to clr msg object. Err: %i\n\r.", err_mqttc); 185 | return (DEF_FAIL); 186 | } 187 | 188 | MQTTc_MsgSetParam(&AppMQTTc_MsgPublishRx, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_MsgPublishRxBuf[0u], &err_mqttc); 189 | if (err_mqttc != MQTTc_ERR_NONE) { 190 | printf("ERROR - Failed to set buf ptr param. Err: %i\n\r.", err_mqttc); 191 | return (DEF_FAIL); 192 | } 193 | 194 | MQTTc_MsgSetParam(&AppMQTTc_MsgPublishRx, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *)APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX, &err_mqttc); 195 | if (err_mqttc != MQTTc_ERR_NONE) { 196 | printf("ERROR - Failed to set buf len param. Err: %i\n\r.", err_mqttc); 197 | return (DEF_FAIL); 198 | } 199 | 200 | MQTTc_ConnClr(&AppMQTTc_Conn, 201 | &err_mqttc); 202 | if (err_mqttc != MQTTc_ERR_NONE) { 203 | printf("ERROR - Failed to clr MQTTc connection object. Err: %i\n\r.", err_mqttc); 204 | return (DEF_FAIL); 205 | } 206 | 207 | /* Err handling should be done in your application. */ 208 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_BROKER_NAME, (void *) APP_MQTTc_BROKER_NAME, &err_mqttc); 209 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CLIENT_ID_STR, (void *) APP_MQTTc_CLIENT_ID_NAME, &err_mqttc); 210 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_USERNAME_STR, (void *) APP_MQTTc_USERNAME, &err_mqttc); 211 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_KEEP_ALIVE_TMR_SEC, (void *) 1000u, &err_mqttc); 212 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_COMPL, (void *) AppMQTTc_OnCmplCallbackFnct, &err_mqttc); 213 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_CONNECT_CMPL, (void *) AppMQTTc_OnConnectCmplCallbackFnct, &err_mqttc); 214 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_CMPL, (void *) AppMQTTc_OnPublishCmplCallbackFnct, &err_mqttc); 215 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_ERR_CALLBACK, (void *) AppMQTTc_OnErrCallbackFnct, &err_mqttc); 216 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_PUBLISH_RX_MSG_PTR, (void *)&AppMQTTc_MsgPublishRx, &err_mqttc); 217 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_TIMEOUT_MS, (void *) 30000u, &err_mqttc); 218 | 219 | MQTTc_ConnOpen(&AppMQTTc_Conn, /* Open conn to MQTT server with parameters set in Conn.*/ 220 | MQTTc_FLAGS_NONE, 221 | &err_mqttc); 222 | if (err_mqttc != MQTTc_ERR_NONE) { 223 | printf("!!! APP ERROR !!! Failed to open TCP connection to MQTT server. Err: %i\n\r.", err_mqttc); 224 | return (DEF_FAIL); /* Failed to open TCP connection to MQTT server. */ 225 | } 226 | 227 | MQTTc_Connect(&AppMQTTc_Conn, /* Send CONNECT msg to MQTT server. */ 228 | &AppMQTTc_Msg, 229 | &err_mqttc); 230 | if (err_mqttc != MQTTc_ERR_NONE) { 231 | printf("!!! APP ERROR !!! Failed to process Connect msg req. Err: %i\n\r.", err_mqttc); 232 | return (DEF_FAIL); /* Failed to process MQTT CONNECT msg. */ 233 | } 234 | 235 | printf("Initialization and CONNECT to server successful.\r\n"); 236 | 237 | return (DEF_OK); 238 | } 239 | 240 | 241 | /* 242 | ********************************************************************************************************* 243 | * AppMQTTc_OnCmplCallbackFnct() 244 | * 245 | * Description : Generic callback function for MQTTc module. 246 | * 247 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 248 | * 249 | * p_msg Pointer to MQTTc Message object used for operation. 250 | * 251 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 252 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 253 | * 254 | * err Error code from processing message. 255 | * 256 | * Return(s) : none. 257 | * 258 | * Caller(s) : MQTTc module. 259 | * 260 | * Note(s) : none. 261 | ********************************************************************************************************* 262 | */ 263 | 264 | static void AppMQTTc_OnCmplCallbackFnct (MQTTc_CONN *p_conn, 265 | MQTTc_MSG *p_msg, 266 | void *p_arg, 267 | MQTTc_ERR err) 268 | { 269 | (void)&p_conn; 270 | (void)&p_arg; 271 | 272 | if (err != MQTTc_ERR_NONE) { 273 | printf("Operation completed with err (%i). ", err); 274 | } 275 | 276 | switch (p_msg->Type) { 277 | case MQTTc_MSG_TYPE_CONNECT: /* Gen callback called for event type: Connect Cmpl. */ 278 | printf("Gen callback called for event type: Connect Cmpl.\n\r"); 279 | break; 280 | 281 | 282 | case MQTTc_MSG_TYPE_PUBLISH: /* Gen callback called for event type: Publish Cmpl. */ 283 | printf("Gen callback called for event type: Publish Cmpl.\n\r"); 284 | break; 285 | 286 | 287 | case MQTTc_MSG_TYPE_SUBSCRIBE: /* Gen callback called for event type: Subscribe Cmpl. */ 288 | printf("Gen callback called for event type: Subscribe Cmpl.\n\r"); 289 | break; 290 | 291 | 292 | case MQTTc_MSG_TYPE_UNSUBSCRIBE: /* Gen callback called for event type: Unsubscribe Cmpl.*/ 293 | printf("Gen callback called for event type: Unsubscribe Cmpl.\n\r"); 294 | break; 295 | 296 | 297 | case MQTTc_MSG_TYPE_PINGREQ: /* Gen callback called for event type: PingReq Cmpl. */ 298 | printf("Gen callback called for event type: PingReq Cmpl.\n\r"); 299 | break; 300 | 301 | 302 | case MQTTc_MSG_TYPE_DISCONNECT: /* Gen callback called for event type: Disconnect Cmpl. */ 303 | printf("Gen callback called for event type: Disconnect Cmpl.\n\r"); 304 | break; 305 | 306 | 307 | default: 308 | printf("Gen callback called for event type: default. !!! ERROR !!! %i\n\r", p_msg->Type); 309 | break; 310 | } 311 | } 312 | 313 | 314 | /* 315 | ********************************************************************************************************* 316 | * AppMQTTc_OnConnectCmplCallbackFnct() 317 | * 318 | * Description : Callback function for MQTTc module called when a CONNECT operation has completed. 319 | * 320 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 321 | * 322 | * p_msg Pointer to MQTTc Message object used for operation. 323 | * 324 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 325 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 326 | * 327 | * err Error code from processing CONNECT message. 328 | * 329 | * Return(s) : none. 330 | * 331 | * Caller(s) : MQTTc module. 332 | * 333 | * Note(s) : none. 334 | ********************************************************************************************************* 335 | */ 336 | 337 | static void AppMQTTc_OnConnectCmplCallbackFnct (MQTTc_CONN *p_conn, 338 | MQTTc_MSG *p_msg, 339 | void *p_arg, 340 | MQTTc_ERR err) 341 | { 342 | MQTTc_ERR err_mqttc; 343 | 344 | 345 | (void)&p_arg; 346 | 347 | if (err != MQTTc_ERR_NONE) { 348 | printf("ConnectCmpl callback called with err (%i). NOT sending PUBLISH message.\n\r", err); 349 | } else { 350 | printf("ConnectCmpl callback called. Sending PUBLISH message.\n\r"); 351 | 352 | MQTTc_Publish(p_conn, 353 | p_msg, 354 | APP_MQTTc_DOMAIN_PUBLISH, 355 | APP_MQTTc_PUBLISH_TEST_QoS, 356 | DEF_YES, 357 | APP_MQTTc_PUBLISH_TEST_MSG, 358 | Str_Len(APP_MQTTc_PUBLISH_TEST_MSG), 359 | &err_mqttc); 360 | if (err_mqttc != MQTTc_ERR_NONE) { 361 | printf("!!! APP ERROR !!! Failed to Publish test string. Err: %i\n\r.", err_mqttc); 362 | } 363 | } 364 | } 365 | 366 | 367 | /* 368 | ********************************************************************************************************* 369 | * AppMQTTc_OnPublishCmplCallbackFnct() 370 | * 371 | * Description : Callback function for MQTTc module called when a PUBLISH operation has completed. 372 | * 373 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 374 | * 375 | * p_msg Pointer to MQTTc Message object used for operation. 376 | * 377 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 378 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 379 | * 380 | * err Error code from processing PUBLISH message. 381 | * 382 | * Return(s) : none. 383 | * 384 | * Caller(s) : MQTTc module. 385 | * 386 | * Note(s) : none. 387 | ********************************************************************************************************* 388 | */ 389 | 390 | static void AppMQTTc_OnPublishCmplCallbackFnct (MQTTc_CONN *p_conn, 391 | MQTTc_MSG *p_msg, 392 | void *p_arg, 393 | MQTTc_ERR err) 394 | { 395 | MQTTc_ERR err_mqttc; 396 | 397 | 398 | (void)&p_arg; 399 | 400 | if (err != MQTTc_ERR_NONE) { 401 | printf("PublishCmpl callback called with error (%i). CANNOT re-send message.\n\r", err); 402 | } else { 403 | printf("PublishCmpl callback called. Re-sending same message.\n\r"); 404 | 405 | MQTTc_Publish(p_conn, 406 | p_msg, 407 | APP_MQTTc_DOMAIN_PUBLISH, 408 | APP_MQTTc_PUBLISH_TEST_QoS, 409 | DEF_YES, 410 | APP_MQTTc_PUBLISH_TEST_MSG, 411 | Str_Len(APP_MQTTc_PUBLISH_TEST_MSG), 412 | &err_mqttc); 413 | if (err_mqttc != MQTTc_ERR_NONE) { 414 | printf("!!! APP ERROR !!! Failed to Publish test string. Err: %i\n\r.", err_mqttc); 415 | } 416 | } 417 | } 418 | 419 | 420 | /* 421 | ********************************************************************************************************* 422 | * AppMQTTc_OnErrCallbackFnct() 423 | * 424 | * Description : Callback function for MQTTc module called when an error occurs. 425 | * 426 | * Arguments : p_conn Pointer to MQTTc Connection object on which error occurred. 427 | * 428 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 429 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 430 | * 431 | * err Error code. 432 | * 433 | * Return(s) : none. 434 | * 435 | * Caller(s) : MQTTc module. 436 | * 437 | * Note(s) : none. 438 | ********************************************************************************************************* 439 | */ 440 | 441 | static void AppMQTTc_OnErrCallbackFnct (MQTTc_CONN *p_conn, 442 | void *p_arg, 443 | MQTTc_ERR err) 444 | { 445 | (void)&p_conn; 446 | (void)&p_arg; 447 | 448 | printf("!!! APP ERROR !!! Err detected via OnErr callback. Err = %i.\n\r", err); 449 | } 450 | -------------------------------------------------------------------------------- /Client/Examples/app_mqtt-c_subscribe.c: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * EXAMPLE CODE 4 | * 5 | * This file is provided as an example on how to use Micrium products. 6 | * 7 | * Please feel free to use any application code labeled as 'EXAMPLE CODE' in 8 | * your application products. Example code may be used as is, in whole or in 9 | * part, or may be used as a reference only. This file can be modified as 10 | * required to meet the end-product requirements. 11 | * 12 | ********************************************************************************************************* 13 | */ 14 | 15 | /* 16 | ********************************************************************************************************* 17 | * 18 | * MQTTc APPLICATION 19 | * 20 | * Filename : app_mqtt-c_subscribe.c 21 | * Version : V1.02.00 22 | ********************************************************************************************************* 23 | */ 24 | 25 | /* 26 | ********************************************************************************************************* 27 | ********************************************************************************************************* 28 | * INCLUDE FILES 29 | ********************************************************************************************************* 30 | ********************************************************************************************************* 31 | */ 32 | 33 | #define APP_MQTTc_MODULE 34 | 35 | #include 36 | #include 37 | 38 | #include "app_mqtt-c.h" 39 | 40 | #include 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | #include 48 | 49 | #include 50 | 51 | #include 52 | 53 | 54 | /* 55 | ********************************************************************************************************* 56 | ********************************************************************************************************* 57 | * LOCAL DEFINES 58 | ********************************************************************************************************* 59 | ********************************************************************************************************* 60 | */ 61 | 62 | #define APP_MQTTc_MSG_QTY 2u 63 | 64 | #define APP_MQTTc_MSG_LEN_MAX 128u 65 | #define APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX 512u 66 | 67 | /* Domain to which to subscribe. */ 68 | #define APP_MQTTc_DOMAIN_SUBSCRIBE "domain/subscribe_topic" 69 | #define APP_MQTTc_DOMAIN_SUBSCRIBE_QoS 2u 70 | 71 | 72 | /* 73 | ********************************************************************************************************* 74 | ********************************************************************************************************* 75 | * LOCAL GLOBAL VARIABLES 76 | ********************************************************************************************************* 77 | ********************************************************************************************************* 78 | */ 79 | 80 | static CPU_INT08U AppMQTTc_TaskStk[APP_MQTTc_TASK_STK_SIZE]; 81 | 82 | static MQTTc_CONN AppMQTTc_Conn; 83 | 84 | static MQTTc_MSG AppMQTTc_Msg; 85 | static CPU_INT08U AppMQTTc_MsgBuf[APP_MQTTc_MSG_LEN_MAX]; 86 | 87 | static MQTTc_MSG AppMQTTc_MsgPublishRx; 88 | static MQTTc_MSG AppMQTTc_MsgPublishRxBuf[APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX]; 89 | 90 | 91 | const NET_TASK_CFG AppMQTTc_TaskCfg = { /* Cfg for MQTTc internal task. */ 92 | APP_MQTTc_TASK_PRIO, /* MQTTc internal task prio. */ 93 | APP_MQTTc_TASK_STK_SIZE, /* MQTTc internal task stack size. */ 94 | AppMQTTc_TaskStk /* Ptr to start of MQTTc internal stack. */ 95 | }; 96 | 97 | 98 | const MQTTc_CFG AppMQTTc_Cfg = { 99 | APP_MQTTc_MSG_QTY, 100 | APP_MQTTc_INACTIVITY_TIMEOUT_s, 101 | APP_MQTTc_INTERNAL_TASK_DLY 102 | }; 103 | 104 | 105 | /* 106 | ********************************************************************************************************* 107 | ********************************************************************************************************* 108 | * LOCAL FUNCTION PROTOTYPES 109 | ********************************************************************************************************* 110 | ********************************************************************************************************* 111 | */ 112 | 113 | static void AppMQTTc_OnCmplCallbackFnct ( MQTTc_CONN *p_conn, 114 | MQTTc_MSG *p_msg, 115 | void *p_arg, 116 | MQTTc_ERR err); 117 | 118 | static void AppMQTTc_OnConnectCmplCallbackFnct ( MQTTc_CONN *p_conn, 119 | MQTTc_MSG *p_msg, 120 | void *p_arg, 121 | MQTTc_ERR err); 122 | 123 | static void AppMQTTc_OnSubscribeCmplCallbackFnct( MQTTc_CONN *p_conn, 124 | MQTTc_MSG *p_msg, 125 | void *p_arg, 126 | MQTTc_ERR err); 127 | 128 | static void AppMQTTc_OnPublishRxCallbackFnct ( MQTTc_CONN *p_conn, 129 | const CPU_CHAR *topic_name_str, 130 | CPU_INT32U topic_len, 131 | const CPU_CHAR *p_payload, 132 | CPU_INT32U payload_len, 133 | void *p_arg, 134 | MQTTc_ERR err); 135 | 136 | 137 | /* 138 | ********************************************************************************************************* 139 | * AppMQTTc_Init() 140 | * 141 | * Description : Initialize the MQTT-client module. 142 | * 143 | * Arguments : none. 144 | * 145 | * Return(s) : DEF_OK, if NO error(s), 146 | * DEF_FAIL, otherwise. 147 | * 148 | * Caller(s) : Application. 149 | * 150 | * Note(s) : none. 151 | ********************************************************************************************************* 152 | */ 153 | 154 | CPU_BOOLEAN AppMQTTc_Init (void) 155 | { 156 | MQTTc_ERR err_mqttc; 157 | 158 | 159 | MQTTc_Init(&AppMQTTc_Cfg, 160 | &AppMQTTc_TaskCfg, 161 | DEF_NULL, 162 | &err_mqttc); 163 | if (err_mqttc != MQTTc_ERR_NONE) { 164 | printf("ERROR - Failed to init MQTTc module. Err: %i\n\r.", err_mqttc); 165 | return (DEF_FAIL); 166 | } 167 | 168 | MQTTc_MsgClr(&AppMQTTc_Msg, &err_mqttc); 169 | if (err_mqttc != MQTTc_ERR_NONE) { 170 | printf("ERROR - Failed to clr msg object. Err: %i\n\r.", err_mqttc); 171 | return (DEF_FAIL); 172 | } 173 | 174 | MQTTc_MsgSetParam(&AppMQTTc_Msg, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_MsgBuf[0u], &err_mqttc); 175 | if (err_mqttc != MQTTc_ERR_NONE) { 176 | printf("ERROR - Failed to set buf ptr param. Err: %i\n\r.", err_mqttc); 177 | return (DEF_FAIL); 178 | } 179 | 180 | MQTTc_MsgSetParam(&AppMQTTc_Msg, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *)APP_MQTTc_MSG_LEN_MAX, &err_mqttc); 181 | if (err_mqttc != MQTTc_ERR_NONE) { 182 | printf("ERROR - Failed to set buf len param. Err: %i\n\r.", err_mqttc); 183 | return (DEF_FAIL); 184 | } 185 | 186 | MQTTc_MsgClr(&AppMQTTc_MsgPublishRx, &err_mqttc); 187 | if (err_mqttc != MQTTc_ERR_NONE) { 188 | printf("ERROR - Failed to clr msg object. Err: %i\n\r.", err_mqttc); 189 | return (DEF_FAIL); 190 | } 191 | 192 | MQTTc_MsgSetParam(&AppMQTTc_MsgPublishRx, MQTTc_PARAM_TYPE_MSG_BUF_PTR, (void *)&AppMQTTc_MsgPublishRxBuf[0u], &err_mqttc); 193 | if (err_mqttc != MQTTc_ERR_NONE) { 194 | printf("ERROR - Failed to set buf ptr param. Err: %i\n\r.", err_mqttc); 195 | return (DEF_FAIL); 196 | } 197 | 198 | MQTTc_MsgSetParam(&AppMQTTc_MsgPublishRx, MQTTc_PARAM_TYPE_MSG_BUF_LEN, (void *)APP_MQTTc_PUBLISH_RX_MSG_LEN_MAX, &err_mqttc); 199 | if (err_mqttc != MQTTc_ERR_NONE) { 200 | printf("ERROR - Failed to set buf len param. Err: %i\n\r.", err_mqttc); 201 | return (DEF_FAIL); 202 | } 203 | 204 | MQTTc_ConnClr(&AppMQTTc_Conn, 205 | &err_mqttc); 206 | if (err_mqttc != MQTTc_ERR_NONE) { 207 | printf("ERROR - Failed to clr MQTTc connection object. Err: %i\n\r.", err_mqttc); 208 | return (DEF_FAIL); 209 | } 210 | 211 | /* Err handling should be done in your application. */ 212 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_BROKER_NAME, (void *) APP_MQTTc_BROKER_NAME, &err_mqttc); 213 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CLIENT_ID_STR, (void *) APP_MQTTc_CLIENT_ID_NAME, &err_mqttc); 214 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_USERNAME_STR, (void *) APP_MQTTc_USERNAME, &err_mqttc); 215 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_PASSWORD_STR, (void *) APP_MQTTc_PASSWORD, &err_mqttc); 216 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_KEEP_ALIVE_TMR_SEC, (void *) 1000u, &err_mqttc); 217 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_COMPL, (void *) AppMQTTc_OnCmplCallbackFnct, &err_mqttc); 218 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_CONNECT_CMPL, (void *) AppMQTTc_OnConnectCmplCallbackFnct, &err_mqttc); 219 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_SUBSCRIBE_CMPL, (void *) AppMQTTc_OnSubscribeCmplCallbackFnct, &err_mqttc); 220 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_PUBLISH_RX_MSG_PTR, (void *)&AppMQTTc_MsgPublishRx, &err_mqttc); 221 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_RX, (void *) AppMQTTc_OnPublishRxCallbackFnct, &err_mqttc); 222 | MQTTc_ConnSetParam(&AppMQTTc_Conn, MQTTc_PARAM_TYPE_TIMEOUT_MS, (void *) 30000u, &err_mqttc); 223 | 224 | MQTTc_ConnOpen(&AppMQTTc_Conn, /* Open conn to MQTT server with parameters set in Conn.*/ 225 | MQTTc_FLAGS_NONE, 226 | &err_mqttc); 227 | if (err_mqttc != MQTTc_ERR_NONE) { 228 | printf("!!! APP ERROR !!! Failed to open TCP connection to MQTT server. Err: %i\n\r.", err_mqttc); 229 | return (DEF_FAIL); /* Failed to open TCP connection to MQTT server. */ 230 | } 231 | 232 | MQTTc_Connect(&AppMQTTc_Conn, /* Send CONNECT msg to MQTT server. */ 233 | &AppMQTTc_Msg, 234 | &err_mqttc); 235 | if (err_mqttc != MQTTc_ERR_NONE) { 236 | printf("!!! APP ERROR !!! Failed to process Connect msg req. Err: %i\n\r.", err_mqttc); 237 | return (DEF_FAIL); /* Failed to process MQTT CONNECT msg. */ 238 | } 239 | 240 | printf("Initialization and CONNECT to server successful.\r\n"); 241 | 242 | return (DEF_OK); 243 | } 244 | 245 | 246 | /* 247 | ********************************************************************************************************* 248 | * AppMQTTc_OnCmplCallbackFnct() 249 | * 250 | * Description : Generic callback function for MQTTc module. 251 | * 252 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 253 | * 254 | * p_msg Pointer to MQTTc Message object used for operation. 255 | * 256 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 257 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 258 | * 259 | * err Error code from processing message. 260 | * 261 | * Return(s) : none. 262 | * 263 | * Caller(s) : MQTTc module. 264 | * 265 | * Note(s) : none. 266 | ********************************************************************************************************* 267 | */ 268 | 269 | static void AppMQTTc_OnCmplCallbackFnct (MQTTc_CONN *p_conn, 270 | MQTTc_MSG *p_msg, 271 | void *p_arg, 272 | MQTTc_ERR err) 273 | { 274 | (void)&p_conn; 275 | (void)&p_arg; 276 | 277 | if (err != MQTTc_ERR_NONE) { 278 | printf("Operation completed with err (%i). ", err); 279 | } 280 | 281 | switch (p_msg->Type) { 282 | case MQTTc_MSG_TYPE_CONNECT: /* Gen callback called for event type: Connect Cmpl. */ 283 | printf("Gen callback called for event type: Connect Cmpl.\n\r"); 284 | break; 285 | 286 | 287 | case MQTTc_MSG_TYPE_PUBLISH: /* Gen callback called for event type: Publish Cmpl. */ 288 | printf("Gen callback called for event type: Publish Cmpl.\n\r"); 289 | break; 290 | 291 | 292 | case MQTTc_MSG_TYPE_SUBSCRIBE: /* Gen callback called for event type: Subscribe Cmpl. */ 293 | printf("Gen callback called for event type: Subscribe Cmpl.\n\r"); 294 | break; 295 | 296 | 297 | case MQTTc_MSG_TYPE_UNSUBSCRIBE: /* Gen callback called for event type: Unsubscribe Cmpl.*/ 298 | printf("Gen callback called for event type: Unsubscribe Cmpl.\n\r"); 299 | break; 300 | 301 | 302 | case MQTTc_MSG_TYPE_PINGREQ: /* Gen callback called for event type: PingReq Cmpl. */ 303 | printf("Gen callback called for event type: PingReq Cmpl.\n\r"); 304 | break; 305 | 306 | 307 | case MQTTc_MSG_TYPE_DISCONNECT: /* Gen callback called for event type: Disconnect Cmpl. */ 308 | printf("Gen callback called for event type: Disconnect Cmpl.\n\r"); 309 | break; 310 | 311 | 312 | default: 313 | printf("Gen callback called for event type: default. !!! ERROR !!! %i\n\r", p_msg->Type); 314 | break; 315 | } 316 | } 317 | 318 | 319 | /* 320 | ********************************************************************************************************* 321 | * AppMQTTc_OnConnectCmplCallbackFnct() 322 | * 323 | * Description : Callback function for MQTTc module called when a CONNECT operation has completed. 324 | * 325 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 326 | * 327 | * p_msg Pointer to MQTTc Message object used for operation. 328 | * 329 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 330 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 331 | * 332 | * err Error code from processing CONNECT message. 333 | * 334 | * Return(s) : none. 335 | * 336 | * Caller(s) : MQTTc module. 337 | * 338 | * Note(s) : none. 339 | ********************************************************************************************************* 340 | */ 341 | 342 | static void AppMQTTc_OnConnectCmplCallbackFnct (MQTTc_CONN *p_conn, 343 | MQTTc_MSG *p_msg, 344 | void *p_arg, 345 | MQTTc_ERR err) 346 | { 347 | (void)&p_arg; 348 | 349 | if (err != MQTTc_ERR_NONE) { 350 | printf("ConnectCmpl callback called with err (%i).\n\r", err); 351 | } else { 352 | printf("ConnectCmpl callback called without err, sending SUBSCRIBE message.\n\r"); 353 | 354 | 355 | MQTTc_Subscribe(p_conn, 356 | p_msg, 357 | APP_MQTTc_DOMAIN_SUBSCRIBE, 358 | APP_MQTTc_DOMAIN_SUBSCRIBE_QoS, 359 | &err); 360 | if (err != MQTTc_ERR_NONE) { 361 | printf("!!! APP ERROR !!! Subscribe failed. Err: %i\n\r.", err); 362 | } 363 | } 364 | } 365 | 366 | 367 | /* 368 | ********************************************************************************************************* 369 | * AppMQTTc_OnSubscribeCmplCallbackFnct() 370 | * 371 | * Description : Callback function for MQTTc module called when a SUBSCRIBE operation has completed. 372 | * 373 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 374 | * 375 | * p_msg Pointer to MQTTc Message object used for operation. 376 | * 377 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 378 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 379 | * 380 | * err Error code from processing SUBSCRIBE message. 381 | * 382 | * Return(s) : none. 383 | * 384 | * Caller(s) : MQTTc module. 385 | * 386 | * Note(s) : none. 387 | ********************************************************************************************************* 388 | */ 389 | 390 | static void AppMQTTc_OnSubscribeCmplCallbackFnct (MQTTc_CONN *p_conn, 391 | MQTTc_MSG *p_msg, 392 | void *p_arg, 393 | MQTTc_ERR err) 394 | { 395 | (void)&p_conn; 396 | (void)&p_msg; 397 | (void)&p_arg; 398 | 399 | if (err != MQTTc_ERR_NONE) { 400 | printf("SubscribeCmpl callback called with error (%i). CANNOT continue.\n\r", err); 401 | } else { 402 | printf("SubscribeCmpl callback called. Can now receive PUBLISHed messages from server.\n\r"); 403 | } 404 | } 405 | 406 | 407 | /* 408 | ********************************************************************************************************* 409 | * AppMQTTc_OnPublishRxCallbackFnct() 410 | * 411 | * Description : Callback function for MQTTc module called when a PUBLISH message has been received. 412 | * 413 | * Arguments : p_conn Pointer to MQTTc Connection object for which operation has completed. 414 | * 415 | * topic_name_str String containing the topic of the message received. NOT NULL-terminated. 416 | * 417 | * topic_len Length of the topic. 418 | * 419 | * p_payload NULL-terminated buffer containing the message received. 420 | * 421 | * payload_len Length of the payload 422 | * 423 | * p_arg Pointer to argument set in MQTTc Connection using the parameter type 424 | * MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR. 425 | * 426 | * Return(s) : none. 427 | * 428 | * Caller(s) : MQTTc module. 429 | * 430 | * Note(s) : none. 431 | ********************************************************************************************************* 432 | */ 433 | 434 | static void AppMQTTc_OnPublishRxCallbackFnct ( MQTTc_CONN *p_conn, 435 | const CPU_CHAR *topic_name_str, 436 | CPU_INT32U topic_len, 437 | const CPU_CHAR *p_payload, 438 | CPU_INT32U payload_len, 439 | void *p_arg, 440 | MQTTc_ERR err) 441 | { 442 | (void)&p_conn; 443 | (void)&p_arg; 444 | 445 | if (err != MQTTc_ERR_NONE) { 446 | printf("!!! APP ERROR !!! Err detected when receiving a PUBLISH message (%i).\n\r", err); 447 | } 448 | 449 | printf("Received PUBLISH message from server. Topic is %.*s.", topic_len, topic_name_str); 450 | printf(" Message is %s.\n\r", p_payload); 451 | } 452 | -------------------------------------------------------------------------------- /Client/Source/mqtt-c.h: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/MQTTc 4 | * Message Queue Telemetry Transport Client 5 | * 6 | * Copyright 2014-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * MQTT CLIENT 21 | * 22 | * Filename : mqtt-c.h 23 | * Version : V1.02.00 24 | ********************************************************************************************************* 25 | */ 26 | 27 | /* 28 | ********************************************************************************************************* 29 | ********************************************************************************************************* 30 | * MODULE 31 | * 32 | * Note(s) : (1) This main network protocol suite header file is protected from multiple pre-processor 33 | * inclusion through use of the MQTTc module present pre-processor macro definition. 34 | ********************************************************************************************************* 35 | ********************************************************************************************************* 36 | */ 37 | 38 | #ifndef MQTTc_MODULE_PRESENT 39 | #define MQTTc_MODULE_PRESENT 40 | 41 | 42 | /* 43 | ********************************************************************************************************* 44 | ********************************************************************************************************* 45 | * MQTTc VERSION NUMBER 46 | * 47 | * Note(s) : (1) (a) The MQTTc module software version is denoted as follows : 48 | * 49 | * Vx.yy.zz 50 | * 51 | * where 52 | * V denotes 'Version' label 53 | * x denotes major software version revision number 54 | * yy denotes minor software version revision number 55 | * zz denotes sub-minor software version revision number 56 | * 57 | * (b) The MQTTc software version label #define is formatted as follows : 58 | * 59 | * ver = x.yyzz * 100 * 100 60 | * 61 | * where 62 | * ver denotes software version number scaled as an integer value 63 | * x.yyzz denotes software version number, where the unscaled integer 64 | * portion denotes the major version number & the unscaled 65 | * fractional portion denotes the (concatenated) minor 66 | * version numbers 67 | ********************************************************************************************************* 68 | ********************************************************************************************************* 69 | */ 70 | 71 | #define MQTTc_VERSION 10200u /* See Note #1. */ 72 | 73 | 74 | /* 75 | ********************************************************************************************************* 76 | ********************************************************************************************************* 77 | * INCLUDE FILES 78 | ********************************************************************************************************* 79 | ********************************************************************************************************* 80 | */ 81 | 82 | #include 83 | 84 | #include 85 | #include 86 | 87 | 88 | /* 89 | ********************************************************************************************************* 90 | ********************************************************************************************************* 91 | * DEFINES 92 | ********************************************************************************************************* 93 | ********************************************************************************************************* 94 | */ 95 | 96 | #define MQTTc_FLAGS_NONE DEF_BIT_NONE /* Reserved for future usage. */ 97 | 98 | 99 | /* 100 | ********************************************************************************************************* 101 | * TRACING 102 | ********************************************************************************************************* 103 | */ 104 | 105 | #ifndef TRACE_LEVEL_OFF 106 | #define TRACE_LEVEL_OFF 0u 107 | #endif 108 | 109 | #ifndef TRACE_LEVEL_INFO 110 | #define TRACE_LEVEL_INFO 1u 111 | #endif 112 | 113 | #ifndef TRACE_LEVEL_DBG 114 | #define TRACE_LEVEL_DBG 2u 115 | #endif 116 | 117 | #ifndef MQTTc_CFG_DBG_TRACE_LEVEL 118 | #define MQTTc_CFG_DBG_TRACE_LEVEL TRACE_LEVEL_OFF 119 | #endif 120 | 121 | #ifndef MQTTc_CFG_DBG_TRACE 122 | #define MQTTc_CFG_DBG_TRACE printf 123 | #endif 124 | 125 | #if ((defined(MQTTc_CFG_DBG_TRACE)) && \ 126 | (defined(MQTTc_CFG_DBG_TRACE_LEVEL)) && \ 127 | (MQTTc_CFG_DBG_TRACE_LEVEL >= TRACE_LEVEL_INFO)) 128 | 129 | #if (MQTTc_CFG_DBG_TRACE_LEVEL >= TRACE_LEVEL_LOG) 130 | #define MQTTc_DBG_TRACE_LOG(msg) MQTTc_CFG_DBG_TRACE msg 131 | #else 132 | #define MQTTc_DBG_TRACE_LOG(msg) 133 | #endif 134 | 135 | #if (MQTTc_CFG_DBG_TRACE_LEVEL >= TRACE_LEVEL_DBG) 136 | #define MQTTc_DBG_TRACE_DBG(msg) MQTTc_CFG_DBG_TRACE msg 137 | #else 138 | #define MQTTc_DBG_TRACE_DBG(msg) 139 | #endif 140 | 141 | #define MQTTc_DBG_TRACE_INFO(msg) MQTTc_CFG_DBG_TRACE msg 142 | #else 143 | #define MQTTc_DBG_TRACE_LOG(msg) 144 | #define MQTTc_DBG_TRACE_DBG(msg) 145 | #define MQTTc_DBG_TRACE_INFO(msg) 146 | #endif 147 | 148 | 149 | /* 150 | ********************************************************************************************************* 151 | ********************************************************************************************************* 152 | * DATA TYPES 153 | ********************************************************************************************************* 154 | ********************************************************************************************************* 155 | */ 156 | 157 | typedef struct mqttc_conn MQTTc_CONN; /* Forward declaration of MQTTc_CONN. */ 158 | typedef struct mqttc_msg MQTTc_MSG; /* Forward declaration of MQTTc_MSG. */ 159 | 160 | 161 | /* 162 | ********************************************************************************************************* 163 | * MQTTc PARAM TYPE 164 | ********************************************************************************************************* 165 | */ 166 | 167 | typedef enum mqttc_param_type { 168 | MQTTc_PARAM_TYPE_BROKER_IP_ADDR, /* Conn's broker's IP addr. */ 169 | MQTTc_PARAM_TYPE_BROKER_NAME, /* Conn's broker's name. */ 170 | MQTTc_PARAM_TYPE_BROKER_PORT_NBR, /* Conn's broker's port nbr. */ 171 | MQTTc_PARAM_TYPE_INACTIVITY_TIMEOUT_S, /* Conn's inactivity timeout, in seconds. */ 172 | MQTTc_PARAM_TYPE_CLIENT_ID_STR, /* Conn's client ID str. */ 173 | MQTTc_PARAM_TYPE_USERNAME_STR, /* Conn's client username str. */ 174 | MQTTc_PARAM_TYPE_PASSWORD_STR, /* Conn's client password str. */ 175 | MQTTc_PARAM_TYPE_KEEP_ALIVE_TMR_SEC, /* Conn's keep alive tmr, in seconds. */ 176 | MQTTc_PARAM_TYPE_WILL_CFG_PTR, /* Conn's will cfg ptr, if any. */ 177 | MQTTc_PARAM_TYPE_SECURE_CFG_PTR, /* Conn's ptr to secure cfg struct. */ 178 | 179 | MQTTc_PARAM_TYPE_CALLBACK_ON_COMPL, /* Conn's generic on cmpl callback. */ 180 | MQTTc_PARAM_TYPE_CALLBACK_ON_CONNECT_CMPL, /* Conn's on connect cmpl callback. */ 181 | MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_CMPL, /* Conn's on publish cmpl callback. */ 182 | MQTTc_PARAM_TYPE_CALLBACK_ON_SUBSCRIBE_CMPL, /* Conn's on subscribe cmpl callback. */ 183 | MQTTc_PARAM_TYPE_CALLBACK_ON_UNSUBSCRIBE_CMPL, /* Conn's on unsubscribe cmpl callback. */ 184 | MQTTc_PARAM_TYPE_CALLBACK_ON_PINGREQ_CMPL, /* Conn's on pingreq cmpl callback. */ 185 | MQTTc_PARAM_TYPE_CALLBACK_ON_DISCONNECT_CMPL, /* Conn's on disconnect cmpl callback. */ 186 | MQTTc_PARAM_TYPE_CALLBACK_ON_ERR_CALLBACK, /* Conn's on err callback. */ 187 | 188 | MQTTc_PARAM_TYPE_CALLBACK_ON_PUBLISH_RX, /* Conn's on publish rx'd callback. */ 189 | 190 | MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR, /* Conn's ptr on arg passed to callback. */ 191 | 192 | MQTTc_PARAM_TYPE_TIMEOUT_MS, /* Conn's 'Open' timeout, in milliseconds. */ 193 | 194 | MQTTc_PARAM_TYPE_PUBLISH_RX_MSG_PTR, /* Conn's ptr on msg that is used to rx publish msg. */ 195 | 196 | MQTTc_PARAM_TYPE_MSG_BUF_PTR, /* Msg's buf ptr. */ 197 | MQTTc_PARAM_TYPE_MSG_BUF_LEN /* Msg's buf len. */ 198 | } MQTTc_PARAM_TYPE; 199 | 200 | 201 | /* 202 | ********************************************************************************************************* 203 | * MQTTc ERR 204 | ********************************************************************************************************* 205 | */ 206 | 207 | typedef enum mqttc_err { 208 | MQTTc_ERR_NONE, /* No err. */ 209 | MQTTc_ERR_FATAL, /* Fatal err. Sock must be closed. */ 210 | MQTTc_ERR_FAIL, /* Generic fail. */ 211 | MQTTc_ERR_NOT_INIT, /* MQTTc module not init yet. */ 212 | MQTTc_ERR_ALLOC, /* Allocation of resource failed. */ 213 | MQTTc_ERR_OS_FAIL, /* OS/KAL operation failed. */ 214 | MQTTc_ERR_CONN_IS_CLOSED, /* Connection used is/has been closed. */ 215 | MQTTc_ERR_CONNACK_FAIL, /* CONNACK msg rx'd was not successful. */ 216 | MQTTc_ERR_INVALID_ARG, /* Invalid arg passed to function. */ 217 | MQTTc_ERR_NULL_PTR, /* Unexpected null ptr passed to function. */ 218 | MQTTc_ERR_INVALID_BUF_SIZE, /* Invalid size of buf passed to function. */ 219 | MQTTc_ERR_BUF_OVERFLOW, /* Operation would overflow buf. */ 220 | MQTTc_ERR_QoS_LEVEL_NOT_GRANTED, /* QoS level requested was not granted by server. */ 221 | MQTTc_ERR_UNEXPECTED_MSG, /* Rx'd unexpected message. No need to re-open conn. */ 222 | 223 | MQTTc_ERR_RX, /* Generic Rx err. */ 224 | MQTTc_ERR_RX_BUF_EMPTY, /* No more bytes can be read at the moment. */ 225 | MQTTc_ERR_TX, /* Generic Tx err. */ 226 | MQTTc_ERR_SEL, /* Generic Sel err. */ 227 | MQTTc_ERR_TIMEOUT, /* Operation timed out. */ 228 | MQTTc_ERR_SOCK_FAIL, /* Operation on sock failed. */ 229 | } MQTTc_ERR; 230 | 231 | 232 | /* 233 | ********************************************************************************************************* 234 | * MQTTc MSG TYPE 235 | ********************************************************************************************************* 236 | */ 237 | 238 | typedef enum mqttc_msg_type { 239 | MQTTc_MSG_TYPE_NONE, 240 | MQTTc_MSG_TYPE_CONNECT, 241 | MQTTc_MSG_TYPE_CONNACK, 242 | MQTTc_MSG_TYPE_PUBLISH, 243 | MQTTc_MSG_TYPE_PUBACK, 244 | MQTTc_MSG_TYPE_PUBREC, 245 | MQTTc_MSG_TYPE_PUBREL, 246 | MQTTc_MSG_TYPE_PUBCOMP, 247 | MQTTc_MSG_TYPE_SUBSCRIBE, 248 | MQTTc_MSG_TYPE_SUBACK, 249 | MQTTc_MSG_TYPE_UNSUBSCRIBE, 250 | MQTTc_MSG_TYPE_UNSUBACK, 251 | MQTTc_MSG_TYPE_PINGREQ, 252 | MQTTc_MSG_TYPE_PINGRESP, 253 | MQTTc_MSG_TYPE_DISCONNECT, 254 | 255 | MQTTc_MSG_TYPE_REQ_CLOSE 256 | } MQTTc_MSG_TYPE; 257 | 258 | 259 | /* 260 | ********************************************************************************************************* 261 | * MQTTc MSG STATE 262 | ********************************************************************************************************* 263 | */ 264 | 265 | typedef enum mqttc_msg_state { 266 | MQTTc_MSG_STATE_NONE, /* Msg is in 'Idle'/'No' state. */ 267 | MQTTc_MSG_STATE_CMPL, /* Msg has cmpl'd. */ 268 | 269 | MQTTc_MSG_STATE_MUST_TX, /* Msg must be tx'd. */ 270 | MQTTc_MSG_STATE_WAIT_TX_CMPL, /* Msg is waiting for tx to cmpl. */ 271 | 272 | MQTTc_MSG_STATE_WAIT_RX /* Msg is waiting to rx. */ 273 | } MQTTc_MSG_STATE; 274 | 275 | 276 | /* 277 | ********************************************************************************************************* 278 | * MQTTc CALLBACK TYPES 279 | ********************************************************************************************************* 280 | */ 281 | 282 | /* Type of callback exec'd when user-req'd oper cmpl. */ 283 | typedef void (*MQTTc_CMPL_CALLBACK) ( MQTTc_CONN *p_conn, 284 | MQTTc_MSG *p_msg, 285 | void *p_arg, 286 | MQTTc_ERR err); 287 | 288 | /* Type of callback exec'd when err occurs/conn closes. */ 289 | typedef void (*MQTTc_ERR_CALLBACK) ( MQTTc_CONN *p_conn, 290 | void *p_arg, 291 | MQTTc_ERR err); 292 | 293 | /* Type of callback exec'd when a publish is rx'd. */ 294 | typedef void (*MQTTc_PUBLISH_RX_CALLBACK) ( MQTTc_CONN *p_conn, 295 | const CPU_CHAR *topic_name_str, 296 | CPU_INT32U topic_len, 297 | const CPU_CHAR *p_payload, 298 | CPU_INT32U payload_len, 299 | void *p_arg, 300 | MQTTc_ERR err); 301 | 302 | 303 | /* 304 | ********************************************************************************************************* 305 | * MQTTc FLAG TYPE 306 | ********************************************************************************************************* 307 | */ 308 | 309 | typedef CPU_INT08U MQTTc_FLAGS; /* No flags implemented, reserved for future usage. */ 310 | 311 | 312 | /* 313 | ********************************************************************************************************* 314 | * MQTTc WILL CFG TYPE 315 | ********************************************************************************************************* 316 | */ 317 | 318 | typedef struct mqttc_will_cfg { 319 | CPU_CHAR *WillTopic; /* Will's topic. */ 320 | CPU_CHAR *WillMessage; /* Will's msg. */ 321 | CPU_BOOLEAN WillRetain; /* Flag indicating if will must be retained. */ 322 | CPU_INT08U WillQoS; /* Will's QoS level. */ 323 | } MQTTc_WILL_CFG; 324 | 325 | 326 | /* 327 | ********************************************************************************************************* 328 | * MQTTc MSG TYPE 329 | ********************************************************************************************************* 330 | */ 331 | 332 | struct mqttc_msg { 333 | MQTTc_CONN *ConnPtr; /* Ptr to MQTTc_CONN associated. */ 334 | MQTTc_MSG_TYPE Type; /* Msg's type. */ 335 | MQTTc_MSG_STATE State; /* Msg's state. */ 336 | CPU_INT08U QoS; /* Msg's QoS. */ 337 | 338 | CPU_INT16U MsgID; /* Msg ID used by msg. */ 339 | 340 | /* Ptr to rx/tx buf in case of mqttc msg, ptr to sem ... */ 341 | void *ArgPtr; /* to post, in case of 'close' msg. */ 342 | CPU_INT32U BufLen; /* Avail buf len for msg. */ 343 | CPU_INT32U XferLen; /* Len of xfer. */ 344 | 345 | MQTTc_ERR Err; /* Err associated to processing of msg. */ 346 | 347 | MQTTc_MSG *NextPtr; /* Ptr to next msg. */ 348 | }; 349 | 350 | 351 | /* 352 | ********************************************************************************************************* 353 | * MQTTc CONN TYPE 354 | ********************************************************************************************************* 355 | */ 356 | 357 | struct mqttc_conn { 358 | NET_SOCK_ID SockId; /* Connection's socket ID. */ 359 | CPU_INT08U SockSelFlags; /* Flags to identify which oper must be checked in Sel. */ 360 | 361 | CPU_CHAR *BrokerNamePtr; /* MQTT broker's name. */ 362 | CPU_INT16U BrokerPortNbr; /* MQTT broker's port nbr. */ 363 | CPU_INT16U InactivityTimeout_s; /* Inactivity timeout, in seconds. */ 364 | 365 | CPU_CHAR *ClientID_Str; /* Client ID str. */ 366 | CPU_CHAR *UsernameStr; /* Username str. */ 367 | CPU_CHAR *PasswordStr; /* Password str. */ 368 | 369 | CPU_INT16U KeepAliveTimerSec; /* Keep alive timer duration, in seconds. */ 370 | MQTTc_WILL_CFG *WillCfgPtr; /* Ptr to will cfg, if any. */ 371 | 372 | NET_APP_SOCK_SECURE_CFG *SecureCfgPtr; /* Ptr to secure will cfg, if any. */ 373 | 374 | /* -------------------- CALLBACKS --------------------- */ 375 | MQTTc_CMPL_CALLBACK OnCmpl; /* Generic, on cmpl callback. */ 376 | MQTTc_CMPL_CALLBACK OnConnectCmpl; /* On connect cmpl callback. */ 377 | MQTTc_CMPL_CALLBACK OnPublishCmpl; /* On publish cmpl callback. */ 378 | MQTTc_CMPL_CALLBACK OnSubscribeCmpl; /* On subscribe cmpl callback. */ 379 | MQTTc_CMPL_CALLBACK OnUnsubscribeCmpl; /* On unsubscribe cmpl callback. */ 380 | MQTTc_CMPL_CALLBACK OnPingReqCmpl; /* On ping req cmpl callback. */ 381 | MQTTc_CMPL_CALLBACK OnDisconnectCmpl; /* On disconnect cmpl callback. */ 382 | MQTTc_ERR_CALLBACK OnErrCallback; /* On err or conn lost callback. Conn must be re-opened.*/ 383 | MQTTc_PUBLISH_RX_CALLBACK OnPublishRx; /* On publish rx'd cmpl callback. */ 384 | void *ArgPtr; /* Ptr to arg that will be provided to callbacks. */ 385 | 386 | CPU_INT32U TimeoutMs; /* Timeout for 'Open' operation, in milliseconds. */ 387 | 388 | /* ----------------- NEXT MSG VALUES ------------------ */ 389 | CPU_INT08U NextMsgHeader; /* Header of next msg to parse. */ 390 | CPU_INT32U NextMsgRxLen; /* Rx len of next msg. */ 391 | MQTTc_MSG_TYPE NextMsgType; /* Next msg's type. */ 392 | CPU_INT32U NextMsgLen; /* Len remaining to rx for next msg. */ 393 | CPU_BOOLEAN NextMsgLenIsCmpl; /* Flag indicating if next msg's len value is rx'd. */ 394 | CPU_INT16U NextMsgMsgID; /* ID of next msg, if any. */ 395 | CPU_BOOLEAN NextMsgMsgID_IsCmpl; /* Flag indicating if next msg's ID has been rx'd. */ 396 | MQTTc_MSG *NextMsgPtr; /* Ptr to next msg, if known. */ 397 | 398 | MQTTc_MSG *PublishRxMsgPtr; /* Ptr to msg that is used to rx publish from server. */ 399 | 400 | MQTTc_MSG *TxMsgHeadPtr; /* Ptr to head of msg needing to tx or waiting reply. */ 401 | CPU_INT32U NextTxMsgTxLen; /* Len of already xfer'd data. */ 402 | 403 | MQTTc_CONN *NextPtr; /* Ptr to next conn. */ 404 | }; 405 | 406 | 407 | /* 408 | ********************************************************************************************************* 409 | * MQTTc CFG TYPE 410 | ********************************************************************************************************* 411 | */ 412 | 413 | typedef struct mqttc_cfg { 414 | /* Max nbr of msgs that will need to be processed ... */ 415 | CPU_INT16U MaxMsgNbr; /* at any given time. */ 416 | CPU_INT16U InactivityTimeout_s; /* Inactivity timeout of sock, in seconds. */ 417 | CPU_INT32U TaskDly; /* Optional internal task dly. */ 418 | } MQTTc_CFG; 419 | 420 | 421 | /* 422 | ********************************************************************************************************* 423 | ********************************************************************************************************* 424 | * PUBLIC FUNCTION PROTOTYPES 425 | ********************************************************************************************************* 426 | ********************************************************************************************************* 427 | */ 428 | 429 | void MQTTc_Init (const MQTTc_CFG *p_cfg, 430 | const NET_TASK_CFG *p_task_cfg, 431 | MEM_SEG *p_mem_seg, 432 | MQTTc_ERR *p_err); 433 | 434 | void MQTTc_ConnClr ( MQTTc_CONN *p_conn, 435 | MQTTc_ERR *p_err); 436 | 437 | void MQTTc_ConnSetParam ( MQTTc_CONN *p_conn, 438 | MQTTc_PARAM_TYPE type, 439 | void *p_param, 440 | MQTTc_ERR *p_err); 441 | 442 | void MQTTc_ConnOpen ( MQTTc_CONN *p_conn, 443 | MQTTc_FLAGS flags, 444 | MQTTc_ERR *p_err); 445 | 446 | void MQTTc_ConnClose ( MQTTc_CONN *p_conn, 447 | MQTTc_FLAGS flags, 448 | MQTTc_ERR *p_err); 449 | 450 | void MQTTc_MsgClr ( MQTTc_MSG *p_msg, 451 | MQTTc_ERR *p_err); 452 | 453 | void MQTTc_MsgSetParam ( MQTTc_MSG *p_msg, 454 | MQTTc_PARAM_TYPE type, 455 | void *p_param, 456 | MQTTc_ERR *p_err); 457 | 458 | void MQTTc_Connect ( MQTTc_CONN *p_conn, 459 | MQTTc_MSG *p_msg, 460 | MQTTc_ERR *p_err); 461 | 462 | void MQTTc_Publish ( MQTTc_CONN *p_conn, 463 | MQTTc_MSG *p_msg, 464 | const CPU_CHAR *topic_str, 465 | CPU_INT08U qos_lvl, 466 | CPU_BOOLEAN retain_flag, 467 | const CPU_CHAR *p_payload, 468 | CPU_INT32U payload_len, 469 | MQTTc_ERR *p_err); 470 | 471 | void MQTTc_Subscribe ( MQTTc_CONN *p_conn, 472 | MQTTc_MSG *p_msg, 473 | const CPU_CHAR *topic_str, 474 | CPU_INT08U topic_req_qos, 475 | MQTTc_ERR *p_err); 476 | 477 | void MQTTc_SubscribeMult ( MQTTc_CONN *p_conn, 478 | MQTTc_MSG *p_msg, 479 | const CPU_CHAR **topic_str_tbl, 480 | CPU_INT08U *topic_req_qos_tbl, 481 | CPU_INT08U topic_nbr, 482 | MQTTc_ERR *p_err); 483 | 484 | void MQTTc_Unsubscribe ( MQTTc_CONN *p_conn, 485 | MQTTc_MSG *p_msg, 486 | const CPU_CHAR *topic_str, 487 | MQTTc_ERR *p_err); 488 | 489 | void MQTTc_UnsubscribeMult ( MQTTc_CONN *p_conn, 490 | MQTTc_MSG *p_msg, 491 | const CPU_CHAR **topic_str_tbl, 492 | CPU_INT08U topic_nbr, 493 | MQTTc_ERR *p_err); 494 | 495 | void MQTTc_PingReq ( MQTTc_CONN *p_conn, 496 | MQTTc_MSG *p_msg, 497 | MQTTc_ERR *p_err); 498 | 499 | void MQTTc_Disconnect ( MQTTc_CONN *p_conn, 500 | MQTTc_MSG *p_msg, 501 | MQTTc_ERR *p_err); 502 | 503 | 504 | /* 505 | ********************************************************************************************************* 506 | ********************************************************************************************************* 507 | * CONFIGURATION ERRORS 508 | ********************************************************************************************************* 509 | ********************************************************************************************************* 510 | */ 511 | 512 | #ifndef MQTTc_CFG_ARG_CHK_EXT_EN 513 | #error "MQTTc_CFG_ARG_CHK_EXT_EN not #define'd in 'mqtt-c_cfg.h'. Must be [DEF_DISABLED] or [DEF_ENABLED]." 514 | #elif ((MQTTc_CFG_ARG_CHK_EXT_EN != DEF_DISABLED) && \ 515 | (MQTTc_CFG_ARG_CHK_EXT_EN != DEF_ENABLED )) 516 | #error "MQTTc_CFG_ARG_CHK_EXT_EN illegally #define'd in 'mqtt-c_cfg.h'. MUST be [DEF_DISABLED] or [DEF_ENABLED]." 517 | #endif 518 | 519 | #ifndef MQTTc_CFG_DBG_GLOBAL_BUF_EN 520 | #error "MQTTc_CFG_DBG_GLOBAL_BUF_EN not #define'd in 'mqtt-c_cfg.h'. Must be [DEF_DISABLED] or [DEF_ENABLED]." 521 | #elif ((MQTTc_CFG_DBG_GLOBAL_BUF_EN != DEF_DISABLED) && \ 522 | (MQTTc_CFG_DBG_GLOBAL_BUF_EN != DEF_ENABLED )) 523 | #error "MQTTc_CFG_DBG_GLOBAL_BUF_EN illegally #define'd in 'mqtt-c_cfg.h'. MUST be [DEF_DISABLED] or [DEF_ENABLED]." 524 | #elif (MQTTc_CFG_DBG_GLOBAL_BUF_EN == DEF_ENABLED) 525 | #ifndef MQTTc_CFG_DBG_GLOBAL_BUF_LEN 526 | #error "MQTTc_CFG_DBG_GLOBAL_BUF_LEN not #define'd in 'mqtt-c_cfg.h'. Must be >= 0u." 527 | #elif (MQTTc_CFG_DBG_GLOBAL_BUF_LEN == 0u) 528 | #error "MQTTc_CFG_DBG_GLOBAL_BUF_LEN illegally #define'd in 'mqtt-c_cfg.h'. Must be >= 0u." 529 | #endif 530 | #endif 531 | 532 | 533 | /* 534 | ********************************************************************************************************* 535 | ********************************************************************************************************* 536 | * MODULE END 537 | ********************************************************************************************************* 538 | ********************************************************************************************************* 539 | */ 540 | 541 | #endif 542 | -------------------------------------------------------------------------------- /Client/Source/mqtt-c_sock.c: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/MQTTc 4 | * Message Queue Telemetry Transport Client 5 | * 6 | * Copyright 2014-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * MQTT CLIENT 21 | * 22 | * Filename : mqtt-c_sock.c 23 | * Version : V1.02.00 24 | ********************************************************************************************************* 25 | */ 26 | 27 | /* 28 | ********************************************************************************************************* 29 | ********************************************************************************************************* 30 | * INCLUDE FILES 31 | ********************************************************************************************************* 32 | ********************************************************************************************************* 33 | */ 34 | 35 | #define MICRIUM_SOURCE 36 | #include 37 | #include 38 | #include 39 | #include "mqtt-c_sock.h" 40 | 41 | 42 | /* 43 | ********************************************************************************************************* 44 | ********************************************************************************************************* 45 | * LOCAL DEFINES 46 | ********************************************************************************************************* 47 | ********************************************************************************************************* 48 | */ 49 | 50 | #define MQTTc_SOCK_SEL_FLAG_DESC_MSK (DEF_BIT_00 | DEF_BIT_01 | DEF_BIT_02) 51 | #define MQTTc_SOCK_SEL_FLAG_DESC_RD DEF_BIT_00 52 | #define MQTTc_SOCK_SEL_FLAG_DESC_WR DEF_BIT_01 53 | #define MQTTc_SOCK_SEL_FLAG_DESC_ERR DEF_BIT_02 54 | 55 | #define MQTTc_NET_SOCK_SEL_TIMEOUT_us 1000u 56 | 57 | 58 | /* 59 | ********************************************************************************************************* 60 | ********************************************************************************************************* 61 | * LOCAL GLOBAL VARIABLES 62 | ********************************************************************************************************* 63 | ********************************************************************************************************* 64 | */ 65 | 66 | static NET_SOCK_DESC MQTTc_NetSockDescRd; 67 | static NET_SOCK_DESC MQTTc_NetSockDescWr; 68 | static NET_SOCK_DESC MQTTc_NetSockDescErr; 69 | 70 | static NET_SOCK_TIMEOUT MQTTc_NetSockSelTimeout = { 71 | 0u, 72 | MQTTc_NET_SOCK_SEL_TIMEOUT_us 73 | }; 74 | 75 | 76 | /* 77 | ********************************************************************************************************* 78 | ********************************************************************************************************* 79 | * GLOBAL FUNCTIONS 80 | ********************************************************************************************************* 81 | ********************************************************************************************************* 82 | */ 83 | 84 | /* 85 | ********************************************************************************************************* 86 | * MQTTc_SockConnOpen() 87 | * 88 | * Description : Open socket for MQTTc module. 89 | * 90 | * Argument(s) : p_conn Pointer to MQTTc_CONN to open. 91 | * 92 | * p_err Pointer to variable that will receive error code from this function: 93 | * MQTTc_ERR_NONE Socket operation completed successfully. 94 | * MQTTc_ERR_NULL_PTR Parameter was null. 95 | * MQTTc_ERR_SOCK_FAIL Socket operation failed. 96 | * 97 | * Return(s) : none. 98 | * 99 | * Caller(s) : MQTTc_SockConnOpen(). 100 | * 101 | * Note(s) : none. 102 | ********************************************************************************************************* 103 | */ 104 | 105 | void MQTTc_SockConnOpen (MQTTc_CONN *p_conn, 106 | MQTTc_ERR *p_err) 107 | { 108 | NET_ERR err_net; 109 | CPU_BOOLEAN flag = DEF_TRUE; 110 | 111 | 112 | #if (MQTTc_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) 113 | if (p_err == DEF_NULL) { 114 | CPU_SW_EXCEPTION(;); 115 | } 116 | 117 | if (p_conn == DEF_NULL) { 118 | *p_err = MQTTc_ERR_NULL_PTR; 119 | return; 120 | } 121 | 122 | if (p_conn->BrokerNamePtr == DEF_NULL) { 123 | *p_err = MQTTc_ERR_NULL_PTR; 124 | return; 125 | } 126 | #endif 127 | 128 | NetApp_ClientStreamOpenByHostname(&p_conn->SockId, 129 | p_conn->BrokerNamePtr, 130 | p_conn->BrokerPortNbr, 131 | DEF_NULL, 132 | p_conn->SecureCfgPtr, 133 | p_conn->TimeoutMs, 134 | &err_net); 135 | switch (err_net) { 136 | case NET_APP_ERR_NONE: 137 | break; 138 | 139 | default: 140 | p_conn->SockId = NET_SOCK_ID_NONE; 141 | *p_err = MQTTc_ERR_SOCK_FAIL; 142 | return; 143 | } 144 | 145 | NetSock_CfgBlock(p_conn->SockId, NET_SOCK_BLOCK_SEL_NO_BLOCK, &err_net); 146 | if (err_net != NET_SOCK_ERR_NONE) { 147 | goto end_err; 148 | } 149 | 150 | (void)NetSock_OptSet( p_conn->SockId, /* Set NO DELAY option. */ 151 | NET_SOCK_PROTOCOL_TCP, 152 | NET_SOCK_OPT_TCP_NO_DELAY, 153 | (void *)&flag, 154 | sizeof(flag), 155 | &err_net); 156 | if (err_net != NET_SOCK_ERR_NONE) { 157 | goto end_err; 158 | } 159 | 160 | /* Set sock conn inactivity timeout. */ 161 | (void)NetSock_OptSet( p_conn->SockId, 162 | NET_SOCK_PROTOCOL_TCP, 163 | NET_SOCK_OPT_TCP_KEEP_IDLE, 164 | (void *)&p_conn->InactivityTimeout_s, 165 | sizeof(p_conn->InactivityTimeout_s), 166 | &err_net); 167 | if (err_net != NET_SOCK_ERR_NONE) { 168 | goto end_err; 169 | } 170 | 171 | *p_err = MQTTc_ERR_NONE; 172 | 173 | return; 174 | 175 | end_err: 176 | *p_err = MQTTc_ERR_SOCK_FAIL; 177 | 178 | NetSock_Close(p_conn->SockId, &err_net); 179 | (void)&err_net; 180 | 181 | p_conn->SockId = NET_SOCK_ID_NONE; 182 | 183 | return; 184 | } 185 | 186 | 187 | /* 188 | ********************************************************************************************************* 189 | * MQTTc_SockConnClose() 190 | * 191 | * Description : Close socket for MQTTc module. 192 | * 193 | * Argument(s) : p_conn Pointer to MQTTc_CONN to close. 194 | * 195 | * p_err Pointer to variable that will receive error code from this function: 196 | * MQTTc_ERR_NONE Socket operation completed successfully. 197 | * MQTTc_ERR_NULL_PTR Parameter was null. 198 | * MQTTc_ERR_SOCK_FAIL Socket operation failed. 199 | * 200 | * Return(s) : none. 201 | * 202 | * Caller(s) : MQTTc_SockConnClose(). 203 | * 204 | * Note(s) : none. 205 | ********************************************************************************************************* 206 | */ 207 | 208 | void MQTTc_SockConnClose (MQTTc_CONN *p_conn, 209 | MQTTc_ERR *p_err) 210 | { 211 | NET_ERR err_net; 212 | 213 | 214 | #if (MQTTc_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) 215 | if (p_err == DEF_NULL) { 216 | CPU_SW_EXCEPTION(;); 217 | } 218 | 219 | if (p_conn == DEF_NULL) { 220 | *p_err = MQTTc_ERR_NULL_PTR; 221 | return; 222 | } 223 | #endif 224 | 225 | NetSock_Close(p_conn->SockId, &err_net); 226 | if (err_net == NET_SOCK_ERR_NONE) { 227 | *p_err = MQTTc_ERR_NONE; 228 | } else { 229 | *p_err = MQTTc_ERR_FAIL; 230 | } 231 | } 232 | 233 | 234 | /* 235 | ********************************************************************************************************* 236 | * MQTTc_SockTx() 237 | * 238 | * Description : Transmit data on given connection's socket. 239 | * 240 | * Argument(s) : p_conn Pointer to MQTTc_CONN that needs to transmit. 241 | * 242 | * p_buf Pointer to start of buffer to transmit. 243 | * 244 | * buf_len Length, in bytes, to transmit. 245 | * 246 | * p_err Pointer to variable that will receive error code from this function: 247 | * MQTTc_ERR_NONE Socket operation completed successfully. 248 | * MQTTc_ERR_NULL_PTR Parameter was null. 249 | * MQTTc_ERR_TX Transmit operation failed. 250 | * 251 | * Return(s) : none. 252 | * 253 | * Caller(s) : MQTTc_WrSockProcess(). 254 | * 255 | * Note(s) : none. 256 | ********************************************************************************************************* 257 | */ 258 | 259 | CPU_INT32U MQTTc_SockTx (MQTTc_CONN *p_conn, 260 | CPU_INT08U *p_buf, 261 | CPU_INT32U buf_len, 262 | MQTTc_ERR *p_err) 263 | { 264 | NET_SOCK_RTN_CODE ret_val; 265 | NET_ERR err_net; 266 | 267 | 268 | #if (MQTTc_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) 269 | if (p_err == DEF_NULL) { 270 | CPU_SW_EXCEPTION(;); 271 | } 272 | 273 | if (p_conn == DEF_NULL) { 274 | *p_err = MQTTc_ERR_NULL_PTR; 275 | return (0u); 276 | } 277 | 278 | if (p_buf == DEF_NULL) { 279 | *p_err = MQTTc_ERR_NULL_PTR; 280 | return (0u); 281 | } 282 | #endif 283 | 284 | ret_val = NetSock_TxData( p_conn->SockId, 285 | (void *)p_buf, 286 | buf_len, 287 | NET_SOCK_FLAG_TX_NO_BLOCK, 288 | &err_net); 289 | if (err_net == NET_SOCK_ERR_NONE) { 290 | *p_err = MQTTc_ERR_NONE; 291 | } else { 292 | *p_err = MQTTc_ERR_TX; 293 | ret_val = 0u; 294 | } 295 | 296 | return (ret_val); 297 | } 298 | 299 | 300 | /* 301 | ********************************************************************************************************* 302 | * MQTTc_SockRx() 303 | * 304 | * Description : Receive data on given connection's socket. 305 | * 306 | * Argument(s) : p_conn Pointer to MQTTc_CONN that needs to receive. 307 | * 308 | * p_buf Pointer to start of buffer in which received data will be put. 309 | * 310 | * buf_len Length, in bytes, of receive buffer. 311 | * 312 | * p_err Pointer to variable that will receive error code from this function: 313 | * MQTTc_ERR_NONE Socket operation completed successfully. 314 | * MQTTc_ERR_NULL_PTR Parameter was null. 315 | * MQTTc_ERR_RX_BUF_EMPTY No more bytes available to receive at the moment. 316 | * MQTTc_ERR_RX Receive operation failed. 317 | * MQTTc_ERR_FATAL Fatal err reported. 318 | * 319 | * Return(s) : Number of bytes received, if NO error(s), 320 | * 0, otherwise. 321 | * 322 | * Caller(s) : MQTTc_RdSockProcess(). 323 | * 324 | * Note(s) : none. 325 | ********************************************************************************************************* 326 | */ 327 | 328 | CPU_INT32U MQTTc_SockRx (MQTTc_CONN *p_conn, 329 | CPU_INT08U *p_buf, 330 | CPU_INT32U buf_len, 331 | MQTTc_ERR *p_err) 332 | { 333 | NET_SOCK_RTN_CODE ret_val; 334 | NET_ERR err_net; 335 | 336 | 337 | #if (MQTTc_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) 338 | if (p_err == DEF_NULL) { 339 | CPU_SW_EXCEPTION(;); 340 | } 341 | 342 | if (p_conn == DEF_NULL) { 343 | *p_err = MQTTc_ERR_NULL_PTR; 344 | return (0u); 345 | } 346 | 347 | if (p_buf == DEF_NULL) { 348 | *p_err = MQTTc_ERR_NULL_PTR; 349 | return (0u); 350 | } 351 | #endif 352 | 353 | ret_val = NetSock_RxData(p_conn->SockId, 354 | p_buf, 355 | buf_len, 356 | NET_SOCK_FLAG_NONE, 357 | &err_net); 358 | switch (err_net) { 359 | case NET_SOCK_ERR_NONE: 360 | *p_err = MQTTc_ERR_NONE; 361 | break; 362 | 363 | case NET_SOCK_ERR_RX_Q_EMPTY: 364 | *p_err = MQTTc_ERR_RX_BUF_EMPTY; 365 | break; 366 | 367 | case NET_ERR_RX: 368 | *p_err = MQTTc_ERR_RX; 369 | ret_val = 0u; 370 | break; 371 | 372 | default: 373 | *p_err = MQTTc_ERR_FATAL; 374 | ret_val = 0u; 375 | break; 376 | } 377 | 378 | return (ret_val); 379 | } 380 | 381 | 382 | /* 383 | ********************************************************************************************************* 384 | * MQTTc_SockSelDescSet() 385 | * 386 | * Description : Set select descriptor type for given connection. 387 | * 388 | * Argument(s) : p_conn Pointer to MQTTc_CONN for which to set its descriptor. 389 | * 390 | * sel_desc_type Select descriptor type to set. 391 | * 392 | * Return(s) : none. 393 | * 394 | * Caller(s) : MQTTc_MsgProcess(), 395 | * MQTTc_WrSockProcess(). 396 | * 397 | * Note(s) : none. 398 | ********************************************************************************************************* 399 | */ 400 | 401 | void MQTTc_SockSelDescSet (MQTTc_CONN *p_conn, 402 | MQTTc_SEL_DESC_TYPE sel_desc_type) 403 | { 404 | NET_ERR err_net; 405 | 406 | 407 | switch (sel_desc_type) { 408 | case MQTTc_SEL_DESC_TYPE_RD: 409 | DEF_BIT_SET(p_conn->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_RD); 410 | break; 411 | 412 | case MQTTc_SEL_DESC_TYPE_WR: 413 | DEF_BIT_SET(p_conn->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_WR); 414 | break; 415 | 416 | case MQTTc_SEL_DESC_TYPE_ERR: 417 | default: 418 | DEF_BIT_SET(p_conn->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_ERR); 419 | break; 420 | } 421 | 422 | NetSock_SelAbort(p_conn->SockId, &err_net); 423 | (void)&err_net; 424 | 425 | return; 426 | } 427 | 428 | 429 | /* 430 | ********************************************************************************************************* 431 | * MQTTc_SockSelDescClr() 432 | * 433 | * Description : Clear select descriptor type for given connection. 434 | * 435 | * Argument(s) : p_conn Pointer to MQTTc_CONN for which to clear its descriptor. 436 | * 437 | * sel_desc_type Select descriptor type to clear. 438 | * 439 | * Return(s) : none. 440 | * 441 | * Caller(s) : MQTTc_WrSockProcess(). 442 | * 443 | * Note(s) : none. 444 | ********************************************************************************************************* 445 | */ 446 | 447 | void MQTTc_SockSelDescClr (MQTTc_CONN *p_conn, 448 | MQTTc_SEL_DESC_TYPE sel_desc_type) 449 | { 450 | NET_ERR err_net; 451 | 452 | 453 | switch (sel_desc_type) { 454 | case MQTTc_SEL_DESC_TYPE_RD: 455 | DEF_BIT_CLR(p_conn->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_RD); 456 | break; 457 | 458 | case MQTTc_SEL_DESC_TYPE_WR: 459 | DEF_BIT_CLR(p_conn->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_WR); 460 | break; 461 | 462 | case MQTTc_SEL_DESC_TYPE_ERR: 463 | default: 464 | DEF_BIT_CLR(p_conn->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_ERR); 465 | break; 466 | } 467 | 468 | NetSock_SelAbort(p_conn->SockId, &err_net); 469 | (void)&err_net; 470 | 471 | return; 472 | } 473 | 474 | 475 | /* 476 | ********************************************************************************************************* 477 | * MQTTc_SockSelDescProc() 478 | * 479 | * Description : Process select descriptor type for given connection. 480 | * 481 | * Argument(s) : p_conn Pointer to MQTTc_CONN on which to process select descriptor. 482 | * 483 | * sel_desc_type Select descriptor type to process. 484 | * 485 | * Return(s) : DEF_YES, if given descriptor is set, 486 | * DEF_NO, otherwise. 487 | * 488 | * Caller(s) : MQTTc_Task(). 489 | * 490 | * Note(s) : none. 491 | ********************************************************************************************************* 492 | */ 493 | 494 | CPU_BOOLEAN MQTTc_SockSelDescProc (MQTTc_CONN *p_conn, 495 | MQTTc_SEL_DESC_TYPE sel_desc_type) 496 | { 497 | NET_SOCK_DESC *p_net_sock_desc; 498 | CPU_BOOLEAN ret_val = DEF_NO; 499 | 500 | 501 | switch (sel_desc_type) { 502 | case MQTTc_SEL_DESC_TYPE_RD: 503 | p_net_sock_desc = &MQTTc_NetSockDescRd; 504 | break; 505 | 506 | case MQTTc_SEL_DESC_TYPE_WR: 507 | p_net_sock_desc = &MQTTc_NetSockDescWr; 508 | break; 509 | 510 | case MQTTc_SEL_DESC_TYPE_ERR: 511 | default: 512 | p_net_sock_desc = &MQTTc_NetSockDescErr; 513 | break; 514 | } 515 | 516 | ret_val = NET_SOCK_DESC_IS_SET(p_conn->SockId, p_net_sock_desc); 517 | 518 | return (ret_val); 519 | } 520 | 521 | 522 | /* 523 | ********************************************************************************************************* 524 | * MQTTc_SockSel() 525 | * 526 | * Description : Execute select operation for selected connections. 527 | * 528 | * Argument(s) : p_head_conn Pointer to head of MQTTc Connection object list. 529 | * 530 | * p_err Pointer to variable that will receive error code from this function: 531 | * MQTTc_ERR_NONE Socket operation completed successfully. 532 | * MQTTc_ERR_TIMEOUT Operation timed-out. 533 | * MQTTc_ERR_SOCK_FAIL Socket operation failed. 534 | * 535 | * Return(s) : none. 536 | * 537 | * Caller(s) : MQTTc_Task(). 538 | * 539 | * Note(s) : none. 540 | ********************************************************************************************************* 541 | */ 542 | 543 | void MQTTc_SockSel (MQTTc_CONN *p_head_conn, 544 | MQTTc_ERR *p_err) 545 | { 546 | MQTTc_CONN *p_conn_iter; 547 | CPU_BOOLEAN must_call_sel = DEF_NO; 548 | NET_ERR err_net; 549 | 550 | 551 | #if (MQTTc_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) 552 | if (p_err == DEF_NULL) { 553 | CPU_SW_EXCEPTION(;); 554 | } 555 | #endif 556 | 557 | NET_SOCK_DESC_INIT(&MQTTc_NetSockDescRd); 558 | NET_SOCK_DESC_INIT(&MQTTc_NetSockDescWr); 559 | NET_SOCK_DESC_INIT(&MQTTc_NetSockDescErr); 560 | 561 | p_conn_iter = p_head_conn; 562 | while (p_conn_iter != DEF_NULL) { 563 | if ((p_conn_iter->SockId != NET_SOCK_ID_NONE) && 564 | (DEF_BIT_IS_SET_ANY(p_conn_iter->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_MSK) == DEF_YES)) { 565 | must_call_sel = DEF_YES; 566 | if (DEF_BIT_IS_SET(p_conn_iter->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_RD) == DEF_YES) { 567 | NET_SOCK_DESC_SET(p_conn_iter->SockId, &MQTTc_NetSockDescRd); 568 | } 569 | if (DEF_BIT_IS_SET(p_conn_iter->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_WR) == DEF_YES) { 570 | NET_SOCK_DESC_SET(p_conn_iter->SockId, &MQTTc_NetSockDescWr); 571 | } 572 | if (DEF_BIT_IS_SET(p_conn_iter->SockSelFlags, MQTTc_SOCK_SEL_FLAG_DESC_ERR) == DEF_YES) { 573 | NET_SOCK_DESC_SET(p_conn_iter->SockId, &MQTTc_NetSockDescErr); 574 | } 575 | } 576 | p_conn_iter = p_conn_iter->NextPtr; 577 | } 578 | 579 | if (must_call_sel == DEF_YES) { 580 | (void)NetSock_Sel(NET_SOCK_NBR_SOCK, 581 | &MQTTc_NetSockDescRd, 582 | &MQTTc_NetSockDescWr, 583 | &MQTTc_NetSockDescErr, 584 | &MQTTc_NetSockSelTimeout, 585 | &err_net); 586 | switch (err_net) { 587 | case NET_SOCK_ERR_NONE: 588 | *p_err = MQTTc_ERR_NONE; 589 | break; 590 | 591 | case NET_SOCK_ERR_TIMEOUT: 592 | *p_err = MQTTc_ERR_TIMEOUT; 593 | break; 594 | 595 | default: 596 | *p_err = MQTTc_ERR_SOCK_FAIL; 597 | break; 598 | } 599 | } else { 600 | *p_err = MQTTc_ERR_NONE; 601 | } 602 | 603 | return; 604 | } 605 | -------------------------------------------------------------------------------- /Client/Source/mqtt-c_sock.h: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/MQTTc 4 | * Message Queue Telemetry Transport Client 5 | * 6 | * Copyright 2014-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * MQTT CLIENT 21 | * 22 | * Filename : mqtt-c_sock.h 23 | * Version : V1.02.00 24 | ********************************************************************************************************* 25 | */ 26 | 27 | /* 28 | ********************************************************************************************************* 29 | ********************************************************************************************************* 30 | * MODULE 31 | * 32 | * Note(s) : (1) This main network protocol suite header file is protected from multiple pre-processor 33 | * inclusion through use of the MQTTc module present pre-processor macro definition. 34 | ********************************************************************************************************* 35 | ********************************************************************************************************* 36 | */ 37 | 38 | #ifndef MQTTc_CONN_MODULE_PRESENT 39 | #define MQTTc_CONN_MODULE_PRESENT 40 | 41 | 42 | /* 43 | ********************************************************************************************************* 44 | ********************************************************************************************************* 45 | * INCLUDE FILES 46 | ********************************************************************************************************* 47 | ********************************************************************************************************* 48 | */ 49 | 50 | #include "mqtt-c.h" 51 | 52 | 53 | /* 54 | ********************************************************************************************************* 55 | ********************************************************************************************************* 56 | * DATA TYPES 57 | ********************************************************************************************************* 58 | ********************************************************************************************************* 59 | */ 60 | 61 | typedef enum mqttc_sel_desc_type { 62 | MQTTc_SEL_DESC_TYPE_RD, 63 | MQTTc_SEL_DESC_TYPE_WR, 64 | MQTTc_SEL_DESC_TYPE_ERR 65 | } MQTTc_SEL_DESC_TYPE; 66 | 67 | 68 | /* 69 | ********************************************************************************************************* 70 | ********************************************************************************************************* 71 | * FUNCTION PROTOTYPES 72 | ********************************************************************************************************* 73 | ********************************************************************************************************* 74 | */ 75 | 76 | void MQTTc_SockConnOpen (MQTTc_CONN *p_conn, 77 | MQTTc_ERR *p_err); 78 | 79 | void MQTTc_SockConnClose (MQTTc_CONN *p_conn, 80 | MQTTc_ERR *p_err); 81 | 82 | CPU_INT32U MQTTc_SockTx (MQTTc_CONN *p_conn, 83 | CPU_INT08U *p_buf, 84 | CPU_INT32U buf_len, 85 | MQTTc_ERR *p_err); 86 | 87 | CPU_INT32U MQTTc_SockRx (MQTTc_CONN *p_conn, 88 | CPU_INT08U *p_buf, 89 | CPU_INT32U buf_len, 90 | MQTTc_ERR *p_err); 91 | 92 | void MQTTc_SockSelDescSet (MQTTc_CONN *p_conn, 93 | MQTTc_SEL_DESC_TYPE sel_desc_type); 94 | 95 | void MQTTc_SockSelDescClr (MQTTc_CONN *p_conn, 96 | MQTTc_SEL_DESC_TYPE sel_desc_type); 97 | 98 | CPU_BOOLEAN MQTTc_SockSelDescProc(MQTTc_CONN *p_conn, 99 | MQTTc_SEL_DESC_TYPE sel_desc_type); 100 | 101 | void MQTTc_SockSel (MQTTc_CONN *p_head_conn, 102 | MQTTc_ERR *p_err); 103 | 104 | 105 | /* 106 | ********************************************************************************************************* 107 | ********************************************************************************************************* 108 | * MODULE END 109 | ********************************************************************************************************* 110 | ********************************************************************************************************* 111 | */ 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /Common/mqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/MQTTc 4 | * Message Queue Telemetry Transport Client 5 | * 6 | * Copyright 2014-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * MQTT DEF 21 | * 22 | * Filename : mqtt.h 23 | * Version : V1.02.00 24 | ********************************************************************************************************* 25 | */ 26 | 27 | /* 28 | ********************************************************************************************************* 29 | ********************************************************************************************************* 30 | * MODULE 31 | * 32 | * Note(s) : (1) This main network protocol suite header file is protected from multiple pre-processor 33 | * inclusion through use of the MQTTc module present pre-processor macro definition. 34 | ********************************************************************************************************* 35 | ********************************************************************************************************* 36 | */ 37 | 38 | #ifndef MQTT_DEF_MODULE_PRESENT 39 | #define MQTT_DEF_MODULE_PRESENT 40 | 41 | /* 42 | ********************************************************************************************************* 43 | ********************************************************************************************************* 44 | * INCLUDE FILES 45 | ********************************************************************************************************* 46 | ********************************************************************************************************* 47 | */ 48 | 49 | #define MICRIUM_SOURCE 50 | #define MQTT_DEF_MODULE 51 | #include 52 | #include 53 | 54 | 55 | /* 56 | ********************************************************************************************************* 57 | ********************************************************************************************************* 58 | * DEFINES 59 | ********************************************************************************************************* 60 | ********************************************************************************************************* 61 | */ 62 | 63 | /* 64 | ********************************************************************************************************* 65 | * MSG LEN DEFINES 66 | ********************************************************************************************************* 67 | */ 68 | 69 | #define MQTT_MSG_BASE_LEN 4u 70 | #define MQTT_MSG_PING_DISCONN_LEN 2u 71 | 72 | 73 | /* 74 | ********************************************************************************************************* 75 | * MSG TYPE DEFINES 76 | ********************************************************************************************************* 77 | */ 78 | 79 | #define MQTT_MSG_TYPE_BIT_SHIFT 4u 80 | #define MQTT_MSG_TYPE_MSK DEF_BIT_FIELD( 4u, MQTT_MSG_TYPE_BIT_SHIFT) 81 | #define MQTT_MSG_TYPE_CONNECT ( 1u << MQTT_MSG_TYPE_BIT_SHIFT) /* Tx only. */ 82 | #define MQTT_MSG_TYPE_CONNACK ( 2u << MQTT_MSG_TYPE_BIT_SHIFT) /* Rx only. */ 83 | #define MQTT_MSG_TYPE_PUBLISH ( 3u << MQTT_MSG_TYPE_BIT_SHIFT) /* Both. */ 84 | #define MQTT_MSG_TYPE_PUBACK ( 4u << MQTT_MSG_TYPE_BIT_SHIFT) /* Both. */ 85 | #define MQTT_MSG_TYPE_PUBREC ( 5u << MQTT_MSG_TYPE_BIT_SHIFT) /* Both. */ 86 | #define MQTT_MSG_TYPE_PUBREL ( 6u << MQTT_MSG_TYPE_BIT_SHIFT) /* Both. */ 87 | #define MQTT_MSG_TYPE_PUBCOMP ( 7u << MQTT_MSG_TYPE_BIT_SHIFT) /* Both. */ 88 | #define MQTT_MSG_TYPE_SUBSCRIBE ( 8u << MQTT_MSG_TYPE_BIT_SHIFT) /* Tx only. */ 89 | #define MQTT_MSG_TYPE_SUBACK ( 9u << MQTT_MSG_TYPE_BIT_SHIFT) /* Rx only. */ 90 | #define MQTT_MSG_TYPE_UNSUBSCRIBE (10u << MQTT_MSG_TYPE_BIT_SHIFT) /* Tx only. */ 91 | #define MQTT_MSG_TYPE_UNSUBACK (11u << MQTT_MSG_TYPE_BIT_SHIFT) /* Rx only. */ 92 | #define MQTT_MSG_TYPE_PINGREQ (12u << MQTT_MSG_TYPE_BIT_SHIFT) /* Tx only. */ 93 | #define MQTT_MSG_TYPE_PINGRESP (13u << MQTT_MSG_TYPE_BIT_SHIFT) /* Rx only. */ 94 | #define MQTT_MSG_TYPE_DISCONNECT (14u << MQTT_MSG_TYPE_BIT_SHIFT) /* Tx only. */ 95 | 96 | 97 | /* 98 | ********************************************************************************************************* 99 | * MSG FIXED HDR DEFINES 100 | ********************************************************************************************************* 101 | */ 102 | 103 | /* ----------------------- DUP ------------------------ */ 104 | #define MQTT_MSG_FIXED_HDR_FLAGS_DUP_BIT_SHIFT 3u 105 | #define MQTT_MSG_FIXED_HDR_FLAGS_DUP_MSK DEF_BIT(MQTT_MSG_FIXED_HDR_FLAGS_DUP_BIT_SHIFT) 106 | 107 | /* ----------------------- QoS ------------------------ */ 108 | #define MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_BIT_SHIFT 1u 109 | #define MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_MSK DEF_BIT_FIELD( 2u, MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_BIT_SHIFT) 110 | #define MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_0 (0x00u << MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_BIT_SHIFT) 111 | #define MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_1 (0x01u << MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_BIT_SHIFT) 112 | #define MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_2 (0x02u << MQTT_MSG_FIXED_HDR_FLAGS_QOS_LVL_BIT_SHIFT) 113 | #define MQTT_MSG_QOS_LVL_MAX 2u 114 | 115 | /* ---------------------- RETAIN ---------------------- */ 116 | #define MQTT_MSG_FIXED_HDR_FLAGS_RETAIN_BIT_SHIFT 0u 117 | #define MQTT_MSG_FIXED_HDR_FLAGS_RETAIN_MSK DEF_BIT(MQTT_MSG_FIXED_HDR_FLAGS_RETAIN_BIT_SHIFT) 118 | 119 | /* --------------------- REM LEN ---------------------- */ 120 | #define MQTT_MSG_FIXED_HDR_REM_LEN_MSK DEF_BIT_FIELD( 7u, 0u) 121 | #define MQTT_MSG_FIXED_HDR_REM_LEN_MAX_LEN 128u 122 | #define MQTT_MSG_FIXED_HDR_REM_LEN_CONTINUATION_BIT DEF_BIT_07 123 | #define MQTT_MSG_FIXED_HDR_REM_LEN_NBR_BYTES_MAX 4u 124 | #define MQTT_MSG_FIXED_HDR_REM_LEN_MAX 268435455u 125 | 126 | /* --------------- FIXED HDR TOTAL LEN ---------------- */ 127 | #define MQTT_MSG_FIXED_HDR_MAX_LEN_BYTES 5u 128 | 129 | 130 | /* 131 | ********************************************************************************************************* 132 | * MSG VAR HDR DEFINES 133 | ********************************************************************************************************* 134 | */ 135 | 136 | #define MQTT_MSG_VAR_HDR_PROTOCOL_NAME_STR "MQTT" 137 | #define MQTT_MSG_VAR_HDR_PROTOCOL_NAME_LEN 4u 138 | #define MQTT_MSG_VAR_HDR_PROTOCOL_VERSION 4u 139 | 140 | 141 | /* 142 | ********************************************************************************************************* 143 | * VAR HDR FOR CONNECT MSG DEFINES 144 | ********************************************************************************************************* 145 | */ 146 | 147 | /* ------------------- VAR HDR LEN -------------------- */ 148 | #define MQTT_MSG_VAR_HDR_CONNECT_LEN MQTT_MSG_VAR_HDR_PROTOCOL_NAME_LEN + 6u 149 | 150 | /* -------------- VAR HDR CONNECT FLAGS --------------- */ 151 | #define MQTT_MSG_VAR_HDR_CONNECT_FLAG_CLEAN_SESSION DEF_BIT_01 152 | #define MQTT_MSG_VAR_HDR_CONNECT_FLAG_WILL_FLAG DEF_BIT_02 153 | #define MQTT_MSG_VAR_HDR_CONNECT_FLAG_WILL_QOS_BIT_SHIFT 3u 154 | #define MQTT_MSG_VAR_HDR_CONNECT_FLAG_WILL_QOS (DEF_BIT_04 | DEF_BIT_03) 155 | #define MQTT_MSG_VAR_HDR_CONNECT_FLAG_WILL_RETAIN DEF_BIT_05 156 | #define MQTT_MSG_VAR_HDR_CONNECT_FLAG_PSWD_FLAG DEF_BIT_06 157 | #define MQTT_MSG_VAR_HDR_CONNECT_FLAG_USER_NAME_FLAG DEF_BIT_07 158 | 159 | /* -------------- VAR HDR CLIENT ID LEN --------------- */ 160 | #define MQTT_MSG_VAR_HDR_CONNECT_CLIENT_ID_MAX_STR_LEN 23u 161 | 162 | 163 | /* 164 | ********************************************************************************************************* 165 | * VAR HDR FOR CONNACK MSG DEFINES 166 | ********************************************************************************************************* 167 | */ 168 | 169 | /* ------------ VAR HDR CONNACK RET CODES ------------- */ 170 | #define MQTT_MSG_VAR_HDR_CONNACK_RET_CODE_ACCEPTED 0u 171 | #define MQTT_MSG_VAR_HDR_CONNACK_RET_CODE_UNACCEPTABLE_VERSION 1u 172 | #define MQTT_MSG_VAR_HDR_CONNACK_RET_CODE_ID_REJECTED 2u 173 | #define MQTT_MSG_VAR_HDR_CONNACK_RET_CODE_SERVER_UNAVAIL 3u 174 | #define MQTT_MSG_VAR_HDR_CONNACK_RET_CODE_BAD_USER_NAME_PSWD 4u 175 | #define MQTT_MSG_VAR_HDR_CONNACK_RET_CODE_NOT_AUTHORIZED 5u 176 | #define MQTT_MSG_VAR_HDR_CONNACK_RET_CODE_MAX 5u 177 | 178 | 179 | /* 180 | ********************************************************************************************************* 181 | * MSG ID DEFINES 182 | ********************************************************************************************************* 183 | */ 184 | 185 | #define MQTT_MSG_ID_SIZE 2u 186 | #define MQTT_MSG_ID_INVALID 0u 187 | #define MQTT_MSG_ID_NONE 0u 188 | 189 | 190 | /* 191 | ********************************************************************************************************* 192 | * UTF-8 UTILITY DEFINES 193 | ********************************************************************************************************* 194 | */ 195 | 196 | #define MQTT_MSG_UTF8_LEN_RD(p_buf) (((p_buf)[0u] << 8u) | (p_buf)[1u]) 197 | #define MQTT_MSG_UTF8_LEN_SIZE 2u 198 | 199 | 200 | /* 201 | ********************************************************************************************************* 202 | ********************************************************************************************************* 203 | * MODULE END 204 | ********************************************************************************************************* 205 | ********************************************************************************************************* 206 | */ 207 | 208 | #endif 209 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | ATTENTION ALL USERS OF THIS REPOSITORY: 2 | 3 | The original work found in this repository is provided by Silicon Labs under the 4 | Apache License, Version 2.0. 5 | 6 | Any third party may contribute derivative works to the original work in which 7 | modifications are clearly identified as being licensed under: 8 | 9 | (1) the Apache License, Version 2.0 or a compatible open source license; or 10 | (2) under a proprietary license with a copy of such license deposited. 11 | 12 | All posted derivative works must clearly identify which license choice has been 13 | elected. 14 | 15 | No such posted derivative works will be considered to be a “Contribution” under 16 | the Apache License, Version 2.0. 17 | 18 | SILICON LABS MAKES NO WARRANTY WITH RESPECT TO ALL POSTED THIRD PARTY CONTENT 19 | AND DISCLAIMS ALL OTHER WARRANTIES OR LIABILITIES, INCLUDING ALL WARRANTIES OF 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, OWNERSHIP, 21 | NON-INFRINGEMENT, AND NON-MISAPPROPRIATION. 22 | 23 | In the event a derivative work is desired to be submitted to Silicon Labs as a 24 | “Contribution” under the Apache License, Version 2.0, a “Contributor” must give 25 | written email notice to micrium@weston-embedded.com. Unless an email response in 26 | the affirmative to accept the derivative work as a “Contribution”, such email 27 | submission should be considered to have not been incorporated into the original 28 | work. 29 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # uC/MQTTc 2 | 3 | µC/MQTTc is the Micriµm Message Queue Telemetry Transport client. It is part of the Micriµm network applications products suite. 4 | 5 | ## For the complete documentation, visit https://doc.micrium.com/display/ucos/ --------------------------------------------------------------------------------