100#include "mbedtls/build_info.h"
101#include "mbedtls/platform.h"
102#include "mbedtls/net.h"
103#include "mbedtls/ssl.h"
104#include "mbedtls/entropy.h"
105#include "mbedtls/ctr_drbg.h"
106#include "mbedtls/debug.h"
107#include "core_mqtt.h"
115#define CLIENT_ID_PREFIX "MM_Client_%s"
122#define MQTT_BROKER_ENDPOINT "test.mosquitto.org"
127#define MQTT_BROKER_PORT 1883
130#define KEEP_ALIVE_TIMEOUT_SECONDS 60
132#define MQTT_CONNACK_RECV_TIMEOUT_MS 10000
139#define DELAY_BETWEEN_PUBLISHES 1000
145#define TOPIC_FORMAT "/MorseMicro/%s/topic"
148#define EXAMPLE_MESSAGE "G'day World!"
150#define MAC_ADDR_STR_LEN (18)
153static unsigned char buf[1024];
163 static char tmptopic[80];
164 static char tmppayload[128];
165 size_t topic_name_length;
166 size_t payload_length;
168 topic_name_length = pxPublishInfo->topicNameLength;
169 if (topic_name_length >=
sizeof(tmptopic))
171 topic_name_length =
sizeof(tmptopic) - 1;
173 strncpy(tmptopic, pxPublishInfo->pTopicName, topic_name_length);
174 tmptopic[topic_name_length] =
'\0';
176 payload_length = pxPublishInfo->payloadLength;
177 if (payload_length >=
sizeof(tmppayload))
179 payload_length =
sizeof(tmppayload) - 1;
181 strncpy(tmppayload, (
char *)pxPublishInfo->pPayload, payload_length);
182 tmppayload[payload_length] =
'\0';
184 printf(
"Incoming Topic: %s\n"
185 "Incoming Message : %s\n",
197 MQTTStatus_t xResult = MQTTSuccess;
198 uint8_t *pucPayload = NULL;
203 switch (pxIncomingPacket->type)
205 case MQTT_PACKET_TYPE_SUBACK:
212 xResult = MQTT_GetSubAckStatusCodes(pxIncomingPacket, &pucPayload, &ulSize);
219 case MQTT_PACKET_TYPE_UNSUBACK:
221 printf(
"Unsubscribed from requested topic\n");
224 case MQTT_PACKET_TYPE_PINGRESP:
227 printf(
"WARNING: PINGRESP should not be handled by the application "
228 "callback when using MQTT_ProcessLoop.\n");
233 printf(
"MQTTProcessResponse() called with unknown packet type:(%02X).\n",
234 pxIncomingPacket->type);
245 MQTTPacketInfo_t *pxPacketInfo,
246 MQTTDeserializedInfo_t *pxDeserializedInfo)
251 if ((pxPacketInfo->type & 0xF0U) == MQTT_PACKET_TYPE_PUBLISH)
269 NetworkContext_t *pxNetworkContext,
272 MQTTStatus_t xResult;
273 MQTTConnectInfo_t xConnectInfo;
274 bool xSessionPresent;
275 TransportInterface_t xTransport;
276 MQTTFixedBuffer_t xBuffer;
278 xBuffer.pBuffer =
buf;
279 xBuffer.size =
sizeof(
buf);
282 memset(&xTransport, 0,
sizeof(xTransport));
283 xTransport.pNetworkContext = pxNetworkContext;
284 xTransport.send = transport_send;
285 xTransport.recv = transport_recv;
289 if (xResult != MQTTSuccess)
295 (void)memset((
void *)&xConnectInfo, 0x00,
sizeof(xConnectInfo));
301 xConnectInfo.cleanSession =
true;
306 xConnectInfo.pClientIdentifier = clientID;
307 xConnectInfo.clientIdentifierLength = (uint16_t)strlen(clientID);
317 xResult = MQTT_Connect(pxMQTTContext,
333 MQTTStatus_t xResult = MQTTSuccess;
334 MQTTSubscribeInfo_t xMQTTSubscription[
TOPIC_COUNT];
335 uint16_t usSubscribePacketIdentifier;
338 (void)memset((
void *)&xMQTTSubscription, 0x00,
sizeof(xMQTTSubscription));
341 usSubscribePacketIdentifier = MQTT_GetPacketId(pxMQTTContext);
345 xMQTTSubscription[0].qos = MQTTQoS0;
346 xMQTTSubscription[0].pTopicFilter = topic;
347 xMQTTSubscription[0].topicFilterLength = strlen(topic);
352 xResult = MQTT_Subscribe(pxMQTTContext,
355 usSubscribePacketIdentifier);
356 if (xResult != MQTTSuccess)
371 xResult = MQTT_ProcessLoop(pxMQTTContext);
383 MQTTStatus_t xResult;
384 MQTTSubscribeInfo_t xMQTTSubscription[
TOPIC_COUNT];
385 uint16_t usUnsubscribePacketIdentifier;
388 (void)memset((
void *)&xMQTTSubscription, 0x00,
sizeof(xMQTTSubscription));
392 xMQTTSubscription[0].qos = MQTTQoS0;
393 xMQTTSubscription[0].pTopicFilter = topic;
394 xMQTTSubscription[0].topicFilterLength = (uint16_t)strlen(topic);
397 usUnsubscribePacketIdentifier = MQTT_GetPacketId(pxMQTTContext);
400 xResult = MQTT_Unsubscribe(pxMQTTContext,
402 sizeof(xMQTTSubscription) /
sizeof(MQTTSubscribeInfo_t),
403 usUnsubscribePacketIdentifier);
419 size_t payloadLength)
421 MQTTStatus_t xResult;
422 MQTTPublishInfo_t xMQTTPublishInfo;
425 (void)memset((
void *)&xMQTTPublishInfo, 0x00,
sizeof(xMQTTPublishInfo));
428 xMQTTPublishInfo.qos = MQTTQoS0;
429 xMQTTPublishInfo.retain =
false;
430 xMQTTPublishInfo.pTopicName = topic;
431 xMQTTPublishInfo.topicNameLength = (uint16_t)strlen(topic);
432 xMQTTPublishInfo.pPayload = payload;
433 xMQTTPublishInfo.payloadLength = payloadLength;
436 xResult = MQTT_Publish(pxMQTTContext, &xMQTTPublishInfo, 0U);
446 printf(
"\n\nMorse MQTT Demo (Built " __DATE__
" " __TIME__
")\n\n");
452 uint32_t ulPublishCount = 0U;
453 const uint32_t ulMaxPublishCount = 5UL;
454 NetworkContext_t xNetworkContext = { 0 };
455 MQTTContext_t xMQTTContext;
456 MQTTStatus_t xMQTTStatus;
457 TransportStatus_t xNetworkStatus;
460 static char client_id[48];
461 static char topic[80];
462 static char server[80];
463 static char message[80];
473 printf(
"Failed to read MAC address (status code %d)\n", status);
476 snprintf(mac_address_str,
477 sizeof(mac_address_str),
478 "%02x:%02x:%02x:%02x:%02x:%02x",
486 snprintf(topic,
sizeof(topic),
TOPIC_FORMAT, client_id);
505 printf(
"Connecting to server socket on %s:%ld...", server, port);
506 xNetworkStatus = transport_connect(&xNetworkContext, server, (uint16_t)port, NULL);
507 if (xNetworkStatus != TRANSPORT_SUCCESS)
509 printf(
"failed with code %d\n", xNetworkStatus);
515 printf(
"Client %s Creating MQTT connection with broker....", client_id);
517 if (xMQTTStatus != MQTTSuccess)
519 printf(
"failed with code %d\n", xMQTTStatus);
520 transport_disconnect(&xNetworkContext);
528 printf(
"Subscribing to topic %s...", topic);
530 if (xMQTTStatus != MQTTSuccess)
532 printf(
"failed with code %d\n", xMQTTStatus);
540 for (ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++)
542 printf(
"Publishing to topic %s...", topic);
544 if (xMQTTStatus != MQTTSuccess)
546 printf(
"failed with code %d\n", xMQTTStatus);
557 xMQTTStatus = MQTT_ProcessLoop(&xMQTTContext);
558 if (xMQTTStatus != MQTTSuccess)
560 printf(
"MQTT_ProcessLoop() failed with code %d\n", xMQTTStatus);
571 if (xMQTTStatus != MQTTSuccess)
573 printf(
"MQTTUnsubscribeFromTopic() failed with code %d\n", xMQTTStatus);
580 xMQTTStatus = MQTT_ProcessLoop(&xMQTTContext);
581 if (xMQTTStatus != MQTTSuccess)
583 printf(
"MQTT_ProcessLoop() failed with code %d\n", xMQTTStatus);
591 printf(
"Disconnecting from server and closing socket.\n");
592 xMQTTStatus = MQTT_Disconnect(&xMQTTContext);
593 if (xMQTTStatus != MQTTSuccess)
595 printf(
"MQTT_Disconnect() failed with code %d\n", xMQTTStatus);
599 transport_disconnect(&xNetworkContext);
int mmconfig_read_string(const char *key, char *buffer, int bufsize)
Returns the persistent store string value identified by the key.
int mmconfig_read_uint32(const char *key, uint32_t *value)
Returns the unsigned integer stored in persistent store identified by the key.
#define MMOSAL_ASSERT(expr)
Assert that the given expression evaluates to true and abort execution if not.
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
static enum mmwlan_status mmwlan_get_mac_addr(uint8_t *mac_addr)
Gets the MAC address of the STA interface.
mmwlan_status
Enumeration of status return codes.
#define MMWLAN_MAC_ADDR_LEN
Length of a WLAN MAC address.
@ MMWLAN_SUCCESS
The operation was successful.
Morse Micro application helper routines for initializing/de-initializing the Wireless LAN interface a...
void app_wlan_init(void)
Initializes the WLAN interface (and dependencies) using settings specified in the config store.
void app_wlan_start(void)
Starts the WLAN interface and connects to Wi-Fi using settings specified in the config store.
#define MAC_ADDR_STR_LEN
Length of MAC address string (i.e., "XX:XX:XX:XX:XX:XX") including terminator.
#define EXAMPLE_MESSAGE
Message to publish/subscribe.
MQTTStatus_t MQTTSubscribe(MQTTContext_t *pxMQTTContext, const char *topic)
Subscribes to the specified topic.
MQTTStatus_t MQTTUnsubscribeFromTopic(MQTTContext_t *pxMQTTContext, const char *topic)
Unsubscribes from the specified topic.
#define MQTT_CONNACK_RECV_TIMEOUT_MS
Receive timeout.
#define DELAY_BETWEEN_PUBLISHES
Delay in ms between publishes.
static void MQTTProcessIncomingPublish(MQTTPublishInfo_t *pxPublishInfo)
This callback gets called when a published message matches one of our subscribed topics.
MQTTStatus_t MQTTPublishToTopic(MQTTContext_t *pxMQTTContext, const char *topic, void *payload, size_t payloadLength)
Publish a message to the specified MQTT topic.
static void EventCallback(MQTTContext_t *pxMQTTContext, MQTTPacketInfo_t *pxPacketInfo, MQTTDeserializedInfo_t *pxDeserializedInfo)
This is a callback from MQTT_Process whenever a packet is received from the server.
#define TOPIC_COUNT
Number of topics we subscribe to.
#define KEEP_ALIVE_TIMEOUT_SECONDS
Keep alive Delay.
MQTTStatus_t CreateMQTTConnectionToBroker(MQTTContext_t *pxMQTTContext, NetworkContext_t *pxNetworkContext, char *clientID)
Initializes an MQTT connection with the server.
static unsigned char buf[1024]
Statically allocated buffer for MQTT.
#define MQTT_BROKER_PORT
Broker port.
#define CLIENT_ID_PREFIX
The MQTT client identifier used in this example.
#define TOPIC_FORMAT
Topic to publish/subscribe, we include the client ID to keep it unique.
#define MQTT_BROKER_ENDPOINT
Broker address to connect to.
void app_init(void)
Main entry point to the application.
static void MQTTProcessResponse(MQTTPacketInfo_t *pxIncomingPacket, uint16_t usPacketId)
This callback gets called whenever we receive an ACK from the server.