/*produces a string in *destination that is equal to name: value*/ HTTP_HEADERS_RESULT HTTPHeaders_GetHeader(HTTP_HEADERS_HANDLE handle, size_t index, char** destination) { HTTP_HEADERS_RESULT result = HTTP_HEADERS_OK; /*Codes_SRS_HTTP_HEADERS_99_028:[ The function shall return NULL if the handle is invalid.]*/ /*Codes_SRS_HTTP_HEADERS_99_032:[ The function shall return HTTP_HEADERS_INVALID_ARG if the destination is NULL]*/ if ( (handle == NULL) || (destination == NULL) ) { result = HTTP_HEADERS_INVALID_ARG; LogError("invalid arg (NULL), result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } /*Codes_SRS_HTTP_HEADERS_99_029:[ The function shall return HTTP_HEADERS_INVALID_ARG if index is not valid (for example, out of range) for the currently stored headers.]*/ else { HTTP_HEADERS_HANDLE_DATA* handleData = (HTTP_HEADERS_HANDLE_DATA*)handle; const char*const* keys; const char*const* values; size_t headerCount; if (Map_GetInternals(handleData->headers, &keys, &values, &headerCount) != MAP_OK) { /*Codes_SRS_HTTP_HEADERS_99_034:[ The function shall return HTTP_HEADERS_ERROR when an internal error occurs]*/ result = HTTP_HEADERS_ERROR; LogError("Map_GetInternals failed, result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { /*Codes_SRS_HTTP_HEADERS_99_029:[ The function shall return HTTP_HEADERS_INVALID_ARG if index is not valid (for example, out of range) for the currently stored headers.]*/ if (index >= headerCount) { result = HTTP_HEADERS_INVALID_ARG; LogError("index out of bounds, result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { *destination = (char*)malloc(strlen(keys[index]) + COLON_AND_SPACE_LENGTH + strlen(values[index]) + 1); if (*destination == NULL) { /*Codes_SRS_HTTP_HEADERS_99_034:[ The function shall return HTTP_HEADERS_ERROR when an internal error occurs]*/ result = HTTP_HEADERS_ERROR; LogError("unable to malloc, result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { /*Codes_SRS_HTTP_HEADERS_99_016:[ The function shall store the name:value pair in such a way that when later retrieved by a call to GetHeader it will return a string that shall strcmp equal to the name+": "+value.]*/ /*Codes_SRS_HTTP_HEADERS_99_027:[ Calling this API shall produce the string value+": "+pair) for the index header in the *destination parameter.]*/ strcpy(*destination, keys[index]); strcat(*destination, COLON_AND_SPACE); strcat(*destination, values[index]); /*Codes_SRS_HTTP_HEADERS_99_035:[ The function shall return HTTP_HEADERS_OK when the function executed without error.]*/ result = HTTP_HEADERS_OK; } } } } return result; }
HTTP_HEADERS_RESULT HTTPHeaders_GetHeaderCount(HTTP_HEADERS_HANDLE handle, size_t* headerCount) { HTTP_HEADERS_RESULT result; /*Codes_SRS_HTTP_HEADERS_99_024:[ The function shall return HTTP_HEADERS_INVALID_ARG when an invalid handle is passed.]*/ /*Codes_SRS_HTTP_HEADERS_99_025:[ The function shall return HTTP_HEADERS_INVALID_ARG when headersCount is NULL.]*/ if ((handle == NULL) || (headerCount == NULL)) { result = HTTP_HEADERS_INVALID_ARG; LogError("(result = %s)", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { HTTP_HEADERS_HANDLE_DATA *handleData = (HTTP_HEADERS_HANDLE_DATA *)handle; const char*const* keys; const char*const* values; /*Codes_SRS_HTTP_HEADERS_99_023:[ Calling this API shall provide the number of stored headers.]*/ if (Map_GetInternals(handleData->headers, &keys, &values, headerCount) != MAP_OK) { /*Codes_SRS_HTTP_HEADERS_99_037:[ The function shall return HTTP_HEADERS_ERROR when an internal error occurs.]*/ result = HTTP_HEADERS_ERROR; LogError("Map_GetInternals failed, result= %s", ENUM_TO_STRING(HTTP_HEADERS_RESULT, result)); } else { /*Codes_SRS_HTTP_HEADERS_99_026:[ The function shall write in *headersCount the number of currently stored headers and shall return HTTP_HEADERS_OK]*/ result = HTTP_HEADERS_OK; } } return result; }
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 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; }
CONSTMAP_RESULT ConstMap_GetInternals(CONSTMAP_HANDLE handle, const char*const** keys, const char*const** values, size_t* count) { CONSTMAP_RESULT result; if (handle == NULL) { /*Codes_SRS_CONSTMAP_17_046: [If parameter handle, keys, values or count is NULL then ConstMap_GetInternals shall return CONSTMAP_INVALIDARG.]*/ result = CONSTMAP_INVALIDARG; LOG_CONSTMAP_ERROR(result); } else { /*Codes_SRS_CONSTMAP_17_043: [ConstMap_GetInternals shall produce in *keys a pointer to an array of const char* having all the keys stored so far by the map.] *Codes_SRS_CONSTMAP_17_044: [ConstMap_GetInternals shall produce in *values a pointer to an array of const char* having all the values stored so far by the map.] *Codes_SRS_CONSTMAP_17_045: [ ConstMap_GetInternals shall produce in *count the number of stored keys and values.] */ MAP_RESULT mapResult = Map_GetInternals(((CONSTMAP_HANDLE_DATA *)handle)->map, keys, values, count); result = ConstMap_ErrorConvert(mapResult); } return result; }
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; }