IoTHub raw messaging client sample using AMQP

Dependencies:   iothub_client EthernetInterface NTPClient iothub_amqp_transport azure_c_shared_utility mbed-rtos mbed azure_uamqp_c wolfSSL

This sample showcases the usage of Azure IoT client libraries with the AMQP transport for sending/receiving raw messages from an IoT Hub.

Revision:
31:7a8aed41e109
Parent:
28:a1105bba4bb1
Child:
34:b7f31511a8ad
--- a/iothub_client_sample_amqp.c	Fri Jan 29 23:42:22 2016 +0000
+++ b/iothub_client_sample_amqp.c	Thu Feb 04 16:01:03 2016 -0800
@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "platform.h"
 #include "iothub_client.h"
 #include "iothub_message.h"
 #include "threadapi.h"
@@ -14,8 +15,6 @@
 #include "certs.h"
 #endif // MBED_BUILD_TIMESTAMP
 
-/*String containing Hostname, Device Id & Device Key in the format:             */
-/*  "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"    */
 static const char* connectionString = "[device connection string]";
 static int callbackCounter;
 
@@ -30,12 +29,36 @@
 static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
 {
     int* counter = (int*)userContextCallback;
-    const char* buffer;
-    size_t size;
+    const unsigned char* buffer = NULL;
+    size_t size = 0;
+    
+    IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message);
 
-    if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) == IOTHUB_MESSAGE_OK)
+    if (contentType == IOTHUBMESSAGE_BYTEARRAY)
+    {
+        if (IoTHubMessage_GetByteArray(message, &buffer, &size) == IOTHUB_MESSAGE_OK)
+        {
+            (void)printf("Received Message [%d] with BINARY Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
+        }
+        else
+        {
+            (void)printf("Failed getting the BINARY body of the message received.\r\n");
+        }
+    }
+    else if (contentType == IOTHUBMESSAGE_STRING)
     {
-        (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
+        if ((buffer = IoTHubMessage_GetString(message)) != NULL && (size = strlen(buffer)) > 0)
+        {
+            (void)printf("Received Message [%d] with STRING Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
+        }
+        else
+        {
+            (void)printf("Failed getting the STRING body of the message received.\r\n");
+        }
+    }
+    else
+    {
+        (void)printf("Failed getting the body of the message received (type %i).\r\n", contentType);
     }
 
     // Retrieve properties from the message
@@ -49,8 +72,10 @@
         {
             if (propertyCount > 0)
             {
+                size_t index;
+
                 printf("Message Properties:\r\n");
-                for (size_t index = 0; index < propertyCount; index++)
+                for (index = 0; index < propertyCount; index++)
                 {
                     printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
                 }
@@ -78,7 +103,7 @@
 #define MESSAGE_COUNT 5
 
 void iothub_client_sample_amqp_run(void)
-{    
+{
     IOTHUB_CLIENT_HANDLE iotHubClientHandle;
 
     EVENT_INSTANCE messages[MESSAGE_COUNT];
@@ -90,7 +115,7 @@
     int receiveContext = 0;
 
     (void)printf("Starting the IoTHub client sample AMQP...\r\n");
-
+    
     if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL)
     {
         (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");