IOTHUB_MESSAGING_RESULT IoTHubMessaging_SendAsync(IOTHUB_MESSAGING_CLIENT_HANDLE messagingClientHandle, const char* deviceId, IOTHUB_MESSAGE_HANDLE message, IOTHUB_SEND_COMPLETE_CALLBACK sendCompleteCallback, void* userContextCallback) { IOTHUB_MESSAGING_RESULT result; if (messagingClientHandle == NULL) { /*Codes_SRS_IOTHUBMESSAGING_12_033: [ If messagingClientHandle is NULL, IoTHubMessaging_SendAsync shall return IOTHUB_MESSAGING_INVALID_ARG. ]*/ LogError("NULL iothubClientHandle"); result = IOTHUB_MESSAGING_INVALID_ARG; } else { IOTHUB_MESSAGING_CLIENT_INSTANCE* iotHubMessagingClientInstance = (IOTHUB_MESSAGING_CLIENT_INSTANCE*)messagingClientHandle; /*Codes_SRS_IOTHUBMESSAGING_12_034: [ IoTHubMessaging_SendAsync shall be made thread-safe by using the lock created in IoTHubMessaging_Create. ]*/ if (Lock(iotHubMessagingClientInstance->LockHandle) != LOCK_OK) { /*Codes_SRS_IOTHUBMESSAGING_12_035: [ If acquiring the lock fails, IoTHubMessaging_SendAsync shall return IOTHUB_MESSAGING_ERROR. ]*/ LogError("Could not acquire lock"); result = IOTHUB_MESSAGING_INVALID_ARG; } else { /*Codes_SRS_IOTHUBMESSAGING_12_036: [ IoTHubClient_SendEventAsync shall start the worker thread if it was not previously started. ]*/ if ((result = StartWorkerThreadIfNeeded(iotHubMessagingClientInstance)) != IOTHUB_MESSAGING_OK) { /*Codes_SRS_IOTHUBMESSAGING_12_037: [ If starting the thread fails, IoTHubClient_SendEventAsync shall return IOTHUB_CLIENT_ERROR. ]*/ LogError("Could not start worker thread"); result = IOTHUB_MESSAGING_ERROR; } else { /*Codes_SRS_IOTHUBMESSAGING_12_038: [ IoTHubMessaging_SendAsync shall call IoTHubMessaging_LL_Send, while passing the IOTHUB_MESSAGING_HANDLE handle created by IoTHubClient_Create and the parameters deviceId, message, sendCompleteCallback and userContextCallback.*/ /*Codes_SRS_IOTHUBMESSAGING_12_039: [ When IoTHubMessaging_LL_Send is called, IoTHubMessaging_SendAsync shall return the result of IoTHubMessaging_LL_Send. ]*/ result = IoTHubMessaging_LL_Send(iotHubMessagingClientInstance->IoTHubMessagingHandle, deviceId, message, sendCompleteCallback, userContextCallback); } /*Codes_SRS_IOTHUBMESSAGING_12_040: [ IoTHubClient_SendEventAsync shall be made thread-safe by using the lock created in IoTHubClient_Create. ]*/ (void)Unlock(iotHubMessagingClientInstance->LockHandle); } } return result; }
void iothub_messaging_ll_sample_run(void) { xlogging_set_log_function(consolelogger_log); if (platform_init() != 0) { (void)printf("Failed to initialize the platform.\r\n"); } else { (void)printf("Calling IoTHubServiceClientAuth_CreateFromConnectionString with connection string\n"); iotHubServiceClientHandle = IoTHubServiceClientAuth_CreateFromConnectionString(connectionString); if (iotHubServiceClientHandle == NULL) { (void)printf("IoTHubServiceClientAuth_CreateFromConnectionString failed\n"); } else { (void)printf("Service Client Authentication handle has been created successfully\n"); (void)printf("Creating Messaging...\n"); iotHubMessagingHandle = IoTHubMessaging_LL_Create(iotHubServiceClientHandle); if (iotHubMessagingHandle == NULL) { (void)printf("IoTHubMessaging_LL_Create failed\n"); } else { (void)printf("Messaging has been created successfully\n"); (void)printf("Opening Messaging...\n"); iotHubMessagingResult = IoTHubMessaging_LL_SetFeedbackMessageCallback(iotHubMessagingHandle, feedbackReceivedCallback, "Context string for feedback"); if (iotHubMessagingResult != IOTHUB_MESSAGING_OK) { (void)printf("IoTHubMessaging_LL_SetFeedbackMessageCallback failed\n"); } else { iotHubMessagingResult = IoTHubMessaging_LL_Open(iotHubMessagingHandle, openCompleteCallback, "Context string for open"); if (iotHubMessagingResult != IOTHUB_MESSAGING_OK) { (void)printf("IoTHubMessaging_LL_Open failed\n"); } else { for (int i = 0; i < MESSAGE_COUNT; i++) { double avgWindSpeed = 10.0; static char msgText[1024]; sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":%s,\"windSpeed\":%.2f, \"i\":%d}", deviceId, avgWindSpeed + (rand() % 4 + 2), i); IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)); if (messageHandle == NULL) { (void)printf("IoTHubMessage_CreateFromByteArray failed\n"); break; } else { iotHubMessagingResult = IoTHubMessaging_LL_Send(iotHubMessagingHandle, deviceId, messageHandle, sendCompleteCallback, NULL); if (iotHubMessagingResult != IOTHUB_MESSAGING_OK) { (void)printf("IoTHubMessaging_LL_Send failed\n"); } else { (void)printf("IoTHubMessaging_LL_Send accepted data for transmission to IoT Hub.\r\n"); } } IoTHubMessage_Destroy(messageHandle); } feedbackCount = 0; while (feedbackCount < MESSAGE_COUNT) { IoTHubMessaging_LL_DoWork(iotHubMessagingHandle); ThreadAPI_Sleep(1); } /* Wait for user to press a key. */ (void)printf("Press any key to exit the application. \r\n"); (void)getchar(); IoTHubMessaging_LL_Close(iotHubMessagingHandle); } } (void)printf("Calling IoTHubMessaging_LL_Destroy...\n"); IoTHubMessaging_LL_Destroy(iotHubMessagingHandle); } (void)printf("Calling IoTHubServiceClientAuth_Destroy...\n"); IoTHubServiceClientAuth_Destroy(iotHubServiceClientHandle); } platform_deinit(); } }
void iothub_messaging_ll_sample_run(void) { xlogging_set_log_function(consolelogger_log); if (platform_init() != 0) { (void)printf("Failed to initialize the platform.\r\n"); } else { (void)printf("Calling IoTHubServiceClientAuth_CreateFromConnectionString with connection string\n"); iotHubServiceClientHandle = IoTHubServiceClientAuth_CreateFromConnectionString(connectionString); if (iotHubServiceClientHandle == NULL) { (void)printf("IoTHubServiceClientAuth_CreateFromConnectionString failed\n"); } else { (void)printf("Service Client Authentication handle has been created successfully\n"); (void)printf("Creating Messaging...\n"); iotHubMessagingHandle = IoTHubMessaging_LL_Create(iotHubServiceClientHandle); if (iotHubMessagingHandle == NULL) { (void)printf("IoTHubMessaging_LL_Create failed\n"); } else { (void)printf("Messaging has been created successfully\n"); (void)printf("Opening Messaging...\n"); iotHubMessagingResult = IoTHubMessaging_LL_SetFeedbackMessageCallback(iotHubMessagingHandle, feedbackReceivedCallback, "Context string for feedback"); if (iotHubMessagingResult != IOTHUB_MESSAGING_OK) { (void)printf("IoTHubMessaging_LL_SetFeedbackMessageCallback failed\n"); } else { iotHubMessagingResult = IoTHubMessaging_LL_Open(iotHubMessagingHandle, openCompleteCallback, "Context string for open"); if (iotHubMessagingResult != IOTHUB_MESSAGING_OK) { (void)printf("IoTHubMessaging_LL_Open failed\n"); } else { for (int i = 0; i < MESSAGE_COUNT; i++) { double avgWindSpeed = 10.0; static char msgText[1024]; sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":%s,\"windSpeed\":%.2f, \"i\":%d}", deviceId, avgWindSpeed + (rand() % 4 + 2), i); IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)); if (messageHandle == NULL) { (void)printf("IoTHubMessage_CreateFromByteArray failed\n"); break; } else { IOTHUB_MESSAGE_RESULT iotHubMessageResult; const char* MSG_ID = "Sample_MessageId"; const char* MSG_CORRELATION_ID = "Sample_MessageCorrelationId"; const char* MSG_PROP_KEYS[3] = { "Sample_Key1", "Sample_Key2", "Sample_Key3" }; const char* MSG_PROP_VALS[3] = { "Sample_Val1", "Sample_Val2", "Sample_Val3" }; iotHubMessageResult = IoTHubMessage_SetMessageId(messageHandle, MSG_ID); if (iotHubMessageResult != IOTHUB_MESSAGE_OK) { (void)printf("IoTHubMessage_SetMessageId failed. Exiting...\n"); IoTHubMessage_Destroy(messageHandle); break; } else { iotHubMessageResult = IoTHubMessage_SetCorrelationId(messageHandle, MSG_CORRELATION_ID); if (iotHubMessageResult != IOTHUB_MESSAGE_OK) { (void)printf("IoTHubMessage_SetCorrelationId failed. Exiting...\n"); IoTHubMessage_Destroy(messageHandle); break; } else { MAP_HANDLE mapHandle = IoTHubMessage_Properties(messageHandle); for (size_t j = 0; j < 3; j++) { if (Map_AddOrUpdate(mapHandle, MSG_PROP_KEYS[j], MSG_PROP_VALS[j]) != MAP_OK) { (void)printf("ERROR: Map_AddOrUpdate failed for property %zu!\r\n", j); } } iotHubMessagingResult = IoTHubMessaging_LL_Send(iotHubMessagingHandle, deviceId, messageHandle, sendCompleteCallback, NULL); if (iotHubMessagingResult != IOTHUB_MESSAGING_OK) { (void)printf("IoTHubMessaging_LL_Send failed\n"); } else { (void)printf("IoTHubMessaging_LL_Send accepted data for transmission to IoT Hub.\r\n"); } } } } IoTHubMessage_Destroy(messageHandle); } feedbackCount = 0; while (feedbackCount < MESSAGE_COUNT) { IoTHubMessaging_LL_DoWork(iotHubMessagingHandle); ThreadAPI_Sleep(1); } /* Wait for user to press a key. */ (void)printf("Press any key to exit the application. \r\n"); (void)getchar(); IoTHubMessaging_LL_Close(iotHubMessagingHandle); } } (void)printf("Calling IoTHubMessaging_LL_Destroy...\n"); IoTHubMessaging_LL_Destroy(iotHubMessagingHandle); } (void)printf("Calling IoTHubServiceClientAuth_Destroy...\n"); IoTHubServiceClientAuth_Destroy(iotHubServiceClientHandle); } platform_deinit(); } }