static const unsigned char* RetrieveMessagePayload(IOTHUB_MESSAGE_HANDLE messageHandle, size_t* length) { const unsigned char* result; IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(messageHandle); if (contentType == IOTHUBMESSAGE_BYTEARRAY) { if (IoTHubMessage_GetByteArray(messageHandle, &result, length) != IOTHUB_MESSAGE_OK) { LogError("Failure result from IoTHubMessage_GetByteArray\r\n"); result = NULL; *length = 0; } } else if (contentType == IOTHUBMESSAGE_STRING) { result = (const unsigned char*)IoTHubMessage_GetString(messageHandle); if (result == NULL) { LogError("Failure result from IoTHubMessage_GetString\r\n"); result = NULL; *length = 0; } else { *length = strlen((const char*)result); } } else { result = NULL; *length = 0; } return result; }
static IOTHUBMESSAGE_DISPOSITION_RESULT receive_msg_callback(IOTHUB_MESSAGE_HANDLE message, void* user_context) { (void)user_context; const char* messageId; const char* correlationId; // Message properties if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL) { messageId = "<unavailable>"; } if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL) { correlationId = "<unavailable>"; } IOTHUBMESSAGE_CONTENT_TYPE content_type = IoTHubMessage_GetContentType(message); if (content_type == IOTHUBMESSAGE_BYTEARRAY) { const unsigned char* buff_msg; size_t buff_len; if (IoTHubMessage_GetByteArray(message, &buff_msg, &buff_len) != IOTHUB_MESSAGE_OK) { (void)printf("Failure retrieving byte array message\r\n"); } else { (void)printf("Received Binary message\r\nMessage ID: %s\r\n Correlation ID: %s\r\n Data: <<<%.*s>>> & Size=%d\r\n", messageId, correlationId, (int)buff_len, buff_msg, (int)buff_len); } } else { const char* string_msg = IoTHubMessage_GetString(message); if (string_msg == NULL) { (void)printf("Failure retrieving byte array message\r\n"); } else { (void)printf("Received String Message\r\nMessage ID: %s\r\n Correlation ID: %s\r\n Data: <<<%s>>>\r\n", messageId, correlationId, string_msg); } } return IOTHUBMESSAGE_ACCEPTED; }
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { int* counter = (int*)userContextCallback; const unsigned char* buffer = NULL; size_t size = 0; const char* messageId; const char* correlationId; // Message properties if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL) { messageId = "<null>"; } if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL) { correlationId = "<null>"; } // Message content IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message); if (contentType == IOTHUBMESSAGE_BYTEARRAY) { if (IoTHubMessage_GetByteArray(message, &buffer, &size) == IOTHUB_MESSAGE_OK) { (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n BINARY Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size); } else { (void)printf("Failed getting the BINARY body of the message received.\r\n"); } } else if (contentType == IOTHUBMESSAGE_STRING) { if ((buffer = (const unsigned char*)IoTHubMessage_GetString(message)) != NULL && (size = strlen((const char*)buffer)) > 0) { (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n STRING Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (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 MAP_HANDLE mapProperties = IoTHubMessage_Properties(message); if (mapProperties != NULL) { const char*const* keys; const char*const* values; size_t propertyCount = 0; if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK) { if (propertyCount > 0) { size_t index; printf(" Message Properties:\r\n"); for (index = 0; index < propertyCount; index++) { printf("\tKey: %s Value: %s\r\n", keys[index], values[index]); } printf("\r\n"); } } } (*counter)++; /* Some device specific action code goes here... */ return IOTHUBMESSAGE_ACCEPTED; }
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { int* counter = (int*)userContextCallback; const unsigned char* buffer = NULL; size_t size = 0; const char* messageId; const char* correlationId; // Message properties if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL) { messageId = "<null>"; } if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL) { correlationId = "<null>"; } // Increment the counter *counter = (*counter) + 1; // Message content IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message); if (contentType == IOTHUBMESSAGE_BYTEARRAY) { if (IoTHubMessage_GetByteArray(message, &buffer, &size) == IOTHUB_MESSAGE_OK) { (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n BINARY Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size); } else { (void)printf("Failed getting the BINARY body of the message received.\r\n"); } } else if (contentType == IOTHUBMESSAGE_STRING) { if ((buffer = (const unsigned char*)IoTHubMessage_GetString(message)) != NULL && (size = strlen((const char*)buffer)) > 0) { (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n STRING Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size); // If we receive the work 'quit' then we stop running } 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 const char* property_key = "property_key"; const char* property_value = IoTHubMessage_GetProperty(message, property_key); if (property_value != NULL) { printf("\r\nMessage Properties:\r\n"); printf("\tKey: %s Value: %s\r\n", property_key, property_value); } if (memcmp(buffer, "quit", size) == 0) { g_continueRunning = false; } /* Some device specific action code goes here... */ return IOTHUBMESSAGE_ACCEPTED; }