/////////////////////////////////////////////////////////////////////////////////////////////////// // This function "links" IoTHub to the serialization library. static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { IOTHUBMESSAGE_DISPOSITION_RESULT result; const unsigned char* buffer; size_t size; if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) { printf("unable to IoTHubMessage_GetByteArray\r\n"); result = EXECUTE_COMMAND_ERROR; } else { // Buffer is not zero terminated char* temp = malloc(size + 1); if (temp == NULL) { printf("failed to malloc\r\n"); result = EXECUTE_COMMAND_ERROR; } else { memcpy(temp, buffer, size); temp[size] = '\0'; EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp); result = (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED : (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED : IOTHUBMESSAGE_REJECTED; free(temp); } } return result; }
/*this function "links" IoTHub to the serialization library*/ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { const unsigned char* buffer; size_t size; if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) { ASSERT_FAIL("unable to IoTHubMessage_GetByteArray"); } else { /*buffer is not zero terminated*/ char* buffer_string = (char*)malloc(size+1); ASSERT_IS_NOT_NULL(buffer_string); if (memcpy(buffer_string, buffer, size) == 0) { ASSERT_FAIL("memcpy failed for buffer"); } else { buffer_string[size] = '\0'; EXECUTE_COMMAND(userContextCallback, buffer_string); } free(buffer_string); } return IOTHUBMESSAGE_ACCEPTED; }
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_callback(IOTHUB_MESSAGE_HANDLE message, void* context) { auto completion = (std::promise<void>*)context; const unsigned char* buffer; size_t size; if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) { printf("unable to IoTHubMessage_GetByteArray\r\n"); } else { /*buffer is not zero terminated*/ std::string str_msg; str_msg.resize(size + 1); memcpy((void*)str_msg.data(), buffer, size); str_msg[size] = '\0'; printf("Received message '%s' from IoT Hub\n", str_msg.c_str()); } completion->set_value(); return IOTHUBMESSAGE_ACCEPTED; }
/*this function "links" IoTHub to the serialization library*/ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { const unsigned char* buffer; size_t size; if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) { (void)printf("unable to IoTHubMessage_GetByteArray\r\n"); } else { /*buffer is not zero terminated*/ char* temp = malloc(size + 1); if (temp == NULL) { (void)printf("failed to malloc\r\n"); } else { (void)memcpy(temp, buffer, size); temp[size] = '\0'; EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp); if (executeCommandResult != EXECUTE_COMMAND_SUCCESS) { (void)printf("Execute command failed\r\n"); } free(temp); } } // MQTT can only accept messages return IOTHUBMESSAGE_ACCEPTED; }
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { int* counter = (int*)userContextCallback; const char* buffer; size_t size; MAP_HANDLE mapProperties; 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 if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK) { printf("unable to retrieve the message data\r\n"); } else { (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size); } // Retrieve properties from the message 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"); } } } /* Some device specific action code goes here... */ (*counter)++; return IOTHUBMESSAGE_ACCEPTED; }
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE msg, void* userContextCallback) { EXPECTED_RECEIVE_DATA* notifyData = (EXPECTED_RECEIVE_DATA*)userContextCallback; if (Lock(notifyData->lock) != LOCK_OK) { ASSERT_FAIL("unable to lock"); /*because the test must absolutely be terminated*/ } else { if (notifyData != NULL) { const char* buffer; size_t size; IoTHubMessage_GetByteArray(msg, (const unsigned char**)&buffer, &size); if (notifyData->data == NULL) { if (size == 0) { notifyData->wasFound = true; } else { notifyData->wasFound = false; } } else { if (buffer == NULL) { notifyData->wasFound = false; } else { if (memcmp(notifyData->data, buffer, size) == 0) { notifyData->wasFound = true; } else { notifyData->wasFound = false; } } } } Unlock(notifyData->lock); } return IOTHUBMESSAGE_ACCEPTED; }
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { int* counter = (int*)userContextCallback; const char* buffer; size_t size; MAP_HANDLE mapProperties; if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK) { printf("unable to retrieve the message data\r\n"); } else { (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%zu\r\n", *counter, (int)size, buffer, size); if (size == 4 && memcmp(buffer, "quit", size) == 0) { g_continueRunning = false; } } // Retrieve properties from the message 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"); } } } /* Some device specific action code goes here... */ (*counter)++; return IOTHUBMESSAGE_ACCEPTED; }
/*this function "links" IoTHub to the serialization library*/ static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { IOTHUBMESSAGE_DISPOSITION_RESULT result; const unsigned char* buffer; size_t size; if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) { LogInfo("Unable to IoTHubMessage_GetByteArray.\r\n"); result = IOTHUBMESSAGE_ABANDONED; } else { receivedMessageBack = true; /*buffer is not zero terminated*/ char* temp = (char*)malloc(size + 1); if (temp == NULL) { LogInfo("Failed to malloc\r\n"); result = IOTHUBMESSAGE_ABANDONED; } else { memcpy(temp, buffer, size); temp[size] = '\0'; LogInfo("Received following message back from IoT Hub %s\r\n",temp); // Parse the json message received StaticJsonBuffer<200> jsonBuffer; JsonObject& root = jsonBuffer.parseObject(temp); if (strcmp(root["message"], pingMessage)==0) { LogInfo("Ping successful!\r\n"); // TODO: turn green led on result = IOTHUBMESSAGE_ACCEPTED; } else { LogInfo("Received a message back... but not the one expected.\r\n"); // TODO: turn red led on result = IOTHUBMESSAGE_REJECTED; } free(temp); } } 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; }
/*this function "links" IoTHub to the serialization library*/ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { const unsigned char* buffer; size_t size; if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) { ASSERT_FAIL("unable to IoTHubMessage_GetByteArray"); } else { /*buffer is not zero terminated*/ STRING_HANDLE temp = STRING_construct_n((char*)buffer, size); ASSERT_IS_NOT_NULL(temp); EXECUTE_COMMAND(userContextCallback, STRING_c_str(temp)); STRING_delete(temp); } return IOTHUBMESSAGE_ACCEPTED; }
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE msg, void* userContextCallback) { EXPECTED_RECEIVE_DATA* notifyData = (EXPECTED_RECEIVE_DATA*)userContextCallback; if (notifyData != NULL) { const char* buffer; size_t size; IoTHubMessage_GetByteArray(msg, (const unsigned char**)&buffer, &size); if (notifyData->data == NULL) { if (size == 0) { notifyData->wasFound = true; } else { notifyData->wasFound = false; } } else { if (buffer == NULL) { notifyData->wasFound = false; } else { if (memcmp(notifyData->data, buffer, size) == 0) { notifyData->wasFound = true; } else { notifyData->wasFound = false; } } } } 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 messageHandle, void* userContextCallback) { EXPECTED_RECEIVE_DATA* receiveUserContext = (EXPECTED_RECEIVE_DATA*)userContextCallback; if (receiveUserContext == NULL) { ASSERT_FAIL("User context is NULL"); } else { if (Lock(receiveUserContext->lock) != LOCK_OK) { ASSERT_FAIL("Unable to lock"); /*because the test must absolutely be terminated*/ } else { const char* messageId; const char* correlationId; const unsigned char* content; size_t contentSize; if ((messageId = IoTHubMessage_GetMessageId(messageHandle)) == NULL) { ASSERT_FAIL("Message ID is NULL."); } if ((strcmp(messageId, MSG_ID1) != 0) && (strcmp(messageId, MSG_ID2) != 0)) { ASSERT_FAIL("Message ID mismatch."); } if ((correlationId = IoTHubMessage_GetCorrelationId(messageHandle)) == NULL) { ASSERT_FAIL("Message correlation ID is NULL."); } if ((strcmp(correlationId, MSG_CORRELATION_ID1) != 0) && (strcmp(correlationId, MSG_CORRELATION_ID2) != 0)) { ASSERT_FAIL("Message correlation ID mismatch."); } IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(messageHandle); if (contentType != IOTHUBMESSAGE_BYTEARRAY) { ASSERT_FAIL("Message content type mismatch."); } else { if (IoTHubMessage_GetByteArray(messageHandle, &content, &contentSize) != IOTHUB_MESSAGE_OK) { ASSERT_FAIL("Unable to get the content of the message."); } else { (void)printf("Received new message from IoT Hub :\r\nMessage-id: %s\r\nCorrelation-id: %s\r\n", messageId, correlationId); (void)printf("\r\n"); } receiveUserContext->wasFound = true; MAP_HANDLE mapHandle = IoTHubMessage_Properties(messageHandle); if (mapHandle != NULL) { const char*const* keys; const char*const* values; size_t propertyCount = 0; if (Map_GetInternals(mapHandle, &keys, &values, &propertyCount) == MAP_OK) { receiveUserContext->wasFound = true; if (propertyCount == MSG_PROP_COUNT) { (void)printf("Message Properties:\r\n"); for (size_t index = 0; index < propertyCount; index++) { (void)printf("\tKey: %s Value: %s\r\n", keys[index], values[index]); if (strcmp(keys[index], MSG_PROP_KEYS[index]) != 0) { receiveUserContext->wasFound = false; } if (strcmp(values[index], MSG_PROP_VALS[index]) != 0) { receiveUserContext->wasFound = false; } } (void)printf("\r\n"); } else { receiveUserContext->wasFound = false; } } } } Unlock(receiveUserContext->lock); } } return IOTHUBMESSAGE_ACCEPTED; }
static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE msg, void* userContextCallback) { if (userContextCallback != NULL) { EXPECTED_RECEIVE_DATA* notifyData = (EXPECTED_RECEIVE_DATA*)userContextCallback; if (Lock(notifyData->lock) != LOCK_OK) { LogError("Unable to lock on ReceiveMessageCallback"); /*because the test must absolutely be terminated*/ } else { const char* messageId; const char* correlationId; const char* buffer; size_t size; if ((messageId = IoTHubMessage_GetMessageId(msg)) == NULL) { messageId = "<null>"; } if ((correlationId = IoTHubMessage_GetCorrelationId(msg)) == NULL) { correlationId = "<null>"; } LogInfo("Received new message from IoT Hub (message-id: %s, correlation-id: %s)", messageId, correlationId); if (IoTHubMessage_GetByteArray(msg, (const unsigned char**)&buffer, &size) != IOTHUB_CLIENT_OK) { LogInfo("Failed calling IoTHubMessage_GetByteArray() for new message received."); } else { if (notifyData->data == NULL) { if (size == 0) { notifyData->receivedByClient = true; if ((notifyData->timeReceived = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting notifyData->timeReceived (time(NULL) failed)"); } } else { notifyData->receivedByClient = false; } } else { if (buffer == NULL) { notifyData->receivedByClient = false; } else { if ((size == notifyData->dataSize) && (memcmp(notifyData->data, buffer, size) == 0)) { notifyData->receivedByClient = true; if ((notifyData->timeReceived = time(NULL)) == INDEFINITE_TIME) { LogError("Failed setting notifyData->timeReceived (time(NULL) failed)"); } } else { notifyData->receivedByClient = false; } } } } Unlock(notifyData->lock); } } 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; }