101#include "mbedtls/build_info.h"
102#include "mbedtls/platform.h"
103#include "mbedtls/net.h"
104#include "mbedtls/ssl.h"
105#include "mbedtls/entropy.h"
106#include "mbedtls/ctr_drbg.h"
107#include "mbedtls/debug.h"
108#include "core_mqtt.h"
116#define CLIENT_ID_PREFIX "MM_Client_%s"
123#define MQTT_BROKER_ENDPOINT "test.mosquitto.org"
128#define MQTT_BROKER_PORT 1883
132#define KEEP_ALIVE_TIMEOUT_SECONDS 60
134#define CONNACK_RECV_TIMEOUT_MS 10000
139#ifdef ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME
140 #define ipconfigSOCK_DEFAULT_RECEIVE_BLOCK_TIME (10000)
148#define DELAY_BETWEEN_PUBLISHES 1000
154#define TOPIC_FORMAT "/MorseMicro/%s/topic"
157#define EXAMPLE_MESSAGE "G'day World!"
159#define MAC_ADDR_STR_LEN (18)
162static unsigned char buf[1024];
173 static char tmptopic[80];
174 static char tmppayload[128];
175 size_t topic_name_length;
176 size_t payload_length;
178 topic_name_length = pxPublishInfo->topicNameLength;
179 if (topic_name_length >=
sizeof(tmptopic))
181 topic_name_length =
sizeof(tmptopic) - 1;
183 strncpy(tmptopic, pxPublishInfo->pTopicName, topic_name_length);
184 tmptopic[topic_name_length] =
'\0';
186 payload_length = pxPublishInfo->payloadLength;
187 if (payload_length >=
sizeof(tmppayload))
189 payload_length =
sizeof(tmppayload) - 1;
191 strncpy(tmppayload, (
char*)pxPublishInfo->pPayload, payload_length);
192 tmppayload[payload_length] =
'\0';
194 printf(
"Incoming Topic: %s\n"
195 "Incoming Message : %s\n",
208 MQTTStatus_t xResult = MQTTSuccess;
209 uint8_t * pucPayload = NULL;
214 switch (pxIncomingPacket->type)
216 case MQTT_PACKET_TYPE_SUBACK:
223 xResult = MQTT_GetSubAckStatusCodes(pxIncomingPacket,
232 case MQTT_PACKET_TYPE_UNSUBACK:
234 printf(
"Unsubscribed from requested topic\n");
237 case MQTT_PACKET_TYPE_PINGRESP:
240 printf(
"WARNING: PINGRESP should not be handled by the application "
241 "callback when using MQTT_ProcessLoop.\n");
246 printf(
"MQTTProcessResponse() called with unknown packet type:(%02X).\n",
247 pxIncomingPacket->type);
258 MQTTPacketInfo_t * pxPacketInfo,
259 MQTTDeserializedInfo_t * pxDeserializedInfo)
262 (void) pxMQTTContext;
264 if ((pxPacketInfo->type & 0xF0U) == MQTT_PACKET_TYPE_PUBLISH)
282 NetworkContext_t * pxNetworkContext,
285 MQTTStatus_t xResult;
286 MQTTConnectInfo_t xConnectInfo;
287 bool xSessionPresent;
288 TransportInterface_t xTransport;
289 MQTTFixedBuffer_t xBuffer;
291 xBuffer.pBuffer =
buf;
292 xBuffer.size =
sizeof(
buf);
295 memset(&xTransport, 0,
sizeof(xTransport));
296 xTransport.pNetworkContext = pxNetworkContext;
297 xTransport.send = transport_send;
298 xTransport.recv = transport_recv;
301 xResult = MQTT_Init(pxMQTTContext,
306 if (xResult != MQTTSuccess)
312 (void) memset((
void *) &xConnectInfo, 0x00,
sizeof(xConnectInfo));
318 xConnectInfo.cleanSession =
true;
323 xConnectInfo.pClientIdentifier = clientID;
324 xConnectInfo.clientIdentifierLength = (uint16_t) strlen(clientID);
334 xResult = MQTT_Connect(pxMQTTContext,
351 MQTTStatus_t xResult = MQTTSuccess;
352 MQTTSubscribeInfo_t xMQTTSubscription[
TOPIC_COUNT ];
353 uint16_t usSubscribePacketIdentifier;
356 (void) memset((
void *) &xMQTTSubscription, 0x00,
sizeof(xMQTTSubscription));
359 usSubscribePacketIdentifier = MQTT_GetPacketId(pxMQTTContext);
363 xMQTTSubscription[ 0 ].qos = MQTTQoS0;
364 xMQTTSubscription[ 0 ].pTopicFilter = topic;
365 xMQTTSubscription[ 0 ].topicFilterLength = strlen(topic);
370 xResult = MQTT_Subscribe(pxMQTTContext,
373 usSubscribePacketIdentifier);
374 if (xResult != MQTTSuccess)
389 xResult = MQTT_ProcessLoop(pxMQTTContext);
402 MQTTStatus_t xResult;
403 MQTTSubscribeInfo_t xMQTTSubscription[
TOPIC_COUNT ];
404 uint16_t usUnsubscribePacketIdentifier;
407 (void) memset((
void *) &xMQTTSubscription, 0x00,
sizeof(xMQTTSubscription));
411 xMQTTSubscription[ 0 ].qos = MQTTQoS0;
412 xMQTTSubscription[ 0 ].pTopicFilter = topic;
413 xMQTTSubscription[ 0 ].topicFilterLength = (uint16_t) strlen(
417 usUnsubscribePacketIdentifier = MQTT_GetPacketId(pxMQTTContext);
420 xResult = MQTT_Unsubscribe(pxMQTTContext,
422 sizeof(xMQTTSubscription) /
sizeof(
423 MQTTSubscribeInfo_t),
424 usUnsubscribePacketIdentifier);
439 void * payload,
size_t payloadLength)
441 MQTTStatus_t xResult;
442 MQTTPublishInfo_t xMQTTPublishInfo;
445 (void) memset((
void *) &xMQTTPublishInfo, 0x00,
sizeof(xMQTTPublishInfo));
448 xMQTTPublishInfo.qos = MQTTQoS0;
449 xMQTTPublishInfo.retain =
false;
450 xMQTTPublishInfo.pTopicName = topic;
451 xMQTTPublishInfo.topicNameLength = (uint16_t) strlen(topic);
452 xMQTTPublishInfo.pPayload = payload;
453 xMQTTPublishInfo.payloadLength = payloadLength;
456 xResult = MQTT_Publish(pxMQTTContext, &xMQTTPublishInfo, 0U);
466 printf(
"\n\nMorse MQTT Demo (Built " __DATE__
" " __TIME__
")\n\n");
472 uint32_t ulPublishCount = 0U;
473 const uint32_t ulMaxPublishCount = 5UL;
474 NetworkContext_t xNetworkContext = { 0 };
475 MQTTContext_t xMQTTContext;
476 MQTTStatus_t xMQTTStatus;
477 TransportStatus_t xNetworkStatus;
480 static char client_id[48];
481 static char topic[80];
482 static char server[80];
483 static char message[80];
493 printf(
"Failed to read MAC address (status code %d)\n", status);
496 snprintf(mac_address_str,
sizeof(mac_address_str),
"%02x:%02x:%02x:%02x:%02x:%02x",
497 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
499 snprintf(topic,
sizeof(topic),
TOPIC_FORMAT, client_id);
518 printf(
"Connecting to server socket on %s:%ld...", server, port);
519 xNetworkStatus = transport_connect(&xNetworkContext, server, (uint16_t) port, NULL);
520 if (xNetworkStatus != TRANSPORT_SUCCESS)
522 printf(
"failed with code %d\n", xNetworkStatus);
528 printf(
"Client %s Creating MQTT connection with broker....", client_id);
530 if (xMQTTStatus != MQTTSuccess)
532 printf(
"failed with code %d\n", xMQTTStatus);
533 transport_disconnect(&xNetworkContext);
541 printf(
"Subscribing to topic %s...", topic);
543 if (xMQTTStatus != MQTTSuccess)
545 printf(
"failed with code %d\n", xMQTTStatus);
553 for (ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++)
555 printf(
"Publishing to topic %s...", topic);
557 message, strlen(message));
558 if (xMQTTStatus != MQTTSuccess)
560 printf(
"failed with code %d\n", xMQTTStatus);
571 xMQTTStatus = MQTT_ProcessLoop(&xMQTTContext);
572 if (xMQTTStatus != MQTTSuccess)
574 printf(
"MQTT_ProcessLoop() failed with code %d\n", xMQTTStatus);
585 if (xMQTTStatus != MQTTSuccess)
587 printf(
"MQTTUnsubscribeFromTopic() failed with code %d\n", xMQTTStatus);
594 xMQTTStatus = MQTT_ProcessLoop(&xMQTTContext);
595 if (xMQTTStatus != MQTTSuccess)
597 printf(
"MQTT_ProcessLoop() failed with code %d\n", xMQTTStatus);
605 printf(
"Disconnecting from server and closing socket.\n");
606 xMQTTStatus = MQTT_Disconnect(&xMQTTContext);
607 if (xMQTTStatus != MQTTSuccess)
609 printf(
"MQTT_Disconnect() failed with code %d\n", xMQTTStatus);
613 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.
enum mmwlan_status mmwlan_get_mac_addr(uint8_t *mac_addr)
Gets the MAC address of this device.
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 DELAY_BETWEEN_PUBLISHES
Override the default FreeRTOS + TCP receive socket timeouts, as the test server can be slow to respon...
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.
#define CONNACK_RECV_TIMEOUT_MS
Receive timeout.
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.