int main(void) { IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol; IOTHUB_MESSAGE_HANDLE message_handle; const char* telemetry_msg = "test_message"; char *cert_string = NULL; printf("This sample will send %d messages and wait for any C2D messages.\r\nPress the enter key to end the sample\r\n\r\n", MESSAGE_COUNT); // Select the Protocol to use with the connection #ifdef SAMPLE_MQTT protocol = MQTT_Protocol; #endif // SAMPLE_MQTT #ifdef SAMPLE_MQTT_OVER_WEBSOCKETS protocol = MQTT_WebSocket_Protocol; #endif // SAMPLE_MQTT_OVER_WEBSOCKETS #ifdef SAMPLE_AMQP protocol = AMQP_Protocol; #endif // SAMPLE_AMQP #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS protocol = AMQP_Protocol_over_WebSocketsTls; #endif // SAMPLE_AMQP_OVER_WEBSOCKETS #ifdef SAMPLE_HTTP protocol = HTTP_Protocol; #endif // SAMPLE_HTTP IOTHUB_DEVICE_CLIENT_HANDLE device_handle; // Used to initialize IoTHub SDK subsystem (void)IoTHub_Init(); (void)printf("Creating IoTHub handle\r\n"); // Create the iothub handle here device_handle = IoTHubDeviceClient_CreateFromConnectionString(connectionString, protocol); if (device_handle == NULL) { (void)printf("Failure creating device client handle. Hint: Check you connection string.\r\n"); } else { // Setting message callback to get C2D messages (void)IoTHubDeviceClient_SetMessageCallback(device_handle, receive_msg_callback, NULL); // Setting connection status callback to get indication of connection to iothub (void)IoTHubDeviceClient_SetConnectionStatusCallback(device_handle, connection_status_callback, NULL); // Set any option that are necessary. // For available options please see the iothub_sdk_options.md documentation // Turn tracing on/off // bool traceOn = true; // (void)IoTHubDeviceClient_SetOption(device_handle, OPTION_LOG_TRACE, &traceOn); // Provide the Azure IoT device client with the Edge root // X509 CA certificate that was used to setup the Edge runtime cert_string = obtain_edge_ca_certificate(); (void)IoTHubDeviceClient_SetOption(device_handle, OPTION_TRUSTED_CERT, cert_string); #if defined SAMPLE_MQTT || defined SAMPLE_MQTT_OVER_WEBSOCKETS //Setting the auto URL Encoder (recommended for MQTT). Please use this option unless //you are URL Encoding inputs yourself. //ONLY valid for use with MQTT //bool urlEncodeOn = true; //(void)IoTHubDeviceClient_SetOption(device_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn); #endif for (size_t index = 0; index < MESSAGE_COUNT; index++) { // Construct the iothub message from a string message_handle = IoTHubMessage_CreateFromString(telemetry_msg); // Set Message property (void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID"); (void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID"); (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson"); (void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8"); // Add custom properties to message (void)IoTHubMessage_SetProperty(message_handle, "property_key", "property_value"); (void)printf("Sending message %d to Edge Hub\r\n", (int)(index + 1)); IoTHubDeviceClient_SendEventAsync(device_handle, message_handle, send_confirm_callback, NULL); // The message is copied to the sdk so the we can destroy it IoTHubMessage_Destroy(message_handle); } printf("\r\nPress any key to continue\r\n"); (void)getchar(); // Clean up the iothub sdk handle IoTHubDeviceClient_Destroy(device_handle); if (cert_string != NULL) { free(cert_string); cert_string = NULL; } } // Free all the sdk subsystem IoTHub_Deinit(); return 0; }
static void RecvMessage(IOTHUB_PROVISIONED_DEVICE* deviceToUse) { // arrange IOTHUB_CLIENT_HANDLE iotHubClientHandle; IOTHUB_SERVICE_CLIENT_AUTH_HANDLE iotHubServiceClientHandle; IOTHUB_MESSAGING_CLIENT_HANDLE iotHubMessagingHandle; IOTHUB_MESSAGING_RESULT iotHubMessagingResult; IOTHUB_MESSAGE_RESULT iotHubMessageResult; EXPECTED_RECEIVE_DATA* receiveUserContext; IOTHUB_MESSAGE_HANDLE messageHandle; // act IoTHub_Init(); // Create Service Client iotHubServiceClientHandle = IoTHubServiceClientAuth_CreateFromConnectionString(IoTHubAccount_GetIoTHubConnString(g_iothubAcctInfo1)); ASSERT_IS_NOT_NULL(iotHubServiceClientHandle, "Could not initialize IoTHubServiceClient to send C2D messages to the device"); iotHubMessagingHandle = IoTHubMessaging_Create(iotHubServiceClientHandle); ASSERT_IS_NOT_NULL(iotHubMessagingHandle, "Could not initialize IoTHubMessaging to send C2D messages to the device"); iotHubMessagingResult = IoTHubMessaging_Open(iotHubMessagingHandle, openCompleteCallback, (void*)"Context string for open"); ASSERT_ARE_EQUAL(int, IOTHUB_MESSAGING_OK, iotHubMessagingResult); // Create user context and message receiveUserContext = ReceiveUserContext_Create(); ASSERT_IS_NOT_NULL(receiveUserContext, "Could not create receive user context"); messageHandle = IoTHubMessage_CreateFromString(MSG_CONTENT1); ASSERT_IS_NOT_NULL(messageHandle, "Could not create IoTHubMessage to send C2D messages to the device"); iotHubMessageResult = IoTHubMessage_SetMessageId(messageHandle, MSG_ID1); ASSERT_ARE_EQUAL(int, IOTHUB_MESSAGING_OK, iotHubMessageResult); iotHubMessageResult = IoTHubMessage_SetCorrelationId(messageHandle, MSG_CORRELATION_ID1); ASSERT_ARE_EQUAL(int, IOTHUB_MESSAGING_OK, iotHubMessageResult); MAP_HANDLE mapHandle = IoTHubMessage_Properties(messageHandle); for (size_t i = 0; i < MSG_PROP_COUNT; i++) { if (Map_AddOrUpdate(mapHandle, MSG_PROP_KEYS[i], MSG_PROP_VALS[i]) != MAP_OK) { (void)printf("ERROR: Map_AddOrUpdate failed for property %zu!\r\n", i); } } iotHubMessagingResult = IoTHubMessaging_SendAsync(iotHubMessagingHandle, deviceToUse->deviceId, messageHandle, sendCompleteCallback, receiveUserContext); ASSERT_ARE_EQUAL(int, IOTHUB_MESSAGING_OK, iotHubMessagingResult, "IoTHubMessaging_SendAsync failed, could not send C2D message to the device"); iotHubClientHandle = IoTHubClient_CreateFromConnectionString(deviceToUse->connectionString, HTTP_Protocol); ASSERT_IS_NOT_NULL(iotHubClientHandle, "Failure creating Iothub Client"); if (deviceToUse->howToCreate == IOTHUB_ACCOUNT_AUTH_X509) { IOTHUB_CLIENT_RESULT result; result = IoTHubClient_SetOption(iotHubClientHandle, OPTION_X509_CERT, deviceToUse->certificate); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Could not set the device x509 certificate"); result = IoTHubClient_SetOption(iotHubClientHandle, OPTION_X509_PRIVATE_KEY, deviceToUse->primaryAuthentication); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Could not set the device x509 privateKey"); } IOTHUB_CLIENT_RESULT result = IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, receiveUserContext); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Failure setting message callback"); unsigned int minimumPollingTime = 1; /*because it should not wait*/ if (IoTHubClient_SetOption(iotHubClientHandle, OPTION_MIN_POLLING_TIME, &minimumPollingTime) != IOTHUB_CLIENT_OK) { printf("failure to set option \"MinimumPollingTime\"\r\n"); } time_t beginOperation, nowTime; beginOperation = time(NULL); while ( ( (nowTime = time(NULL)), (difftime(nowTime, beginOperation) < MAX_CLOUD_TRAVEL_TIME) //time box ) ) { if (Lock(receiveUserContext->lock) != LOCK_OK) { ASSERT_FAIL("unable ot lock"); } else { if (receiveUserContext->wasFound) { (void)Unlock(receiveUserContext->lock); break; } (void)Unlock(receiveUserContext->lock); } ThreadAPI_Sleep(100); } // assert ASSERT_IS_TRUE(receiveUserContext->wasFound, "Failure retrieving message that was sent to IotHub."); // was found is written by the callback... // cleanup IoTHubMessage_Destroy(messageHandle); IoTHubMessaging_Close(iotHubMessagingHandle); IoTHubMessaging_Destroy(iotHubMessagingHandle); IoTHubServiceClientAuth_Destroy(iotHubServiceClientHandle); IoTHubClient_Destroy(iotHubClientHandle); ReceiveUserContext_Destroy(receiveUserContext); }
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(); } }