int main(void) { IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol; IOTHUB_MESSAGE_HANDLE message_handle; size_t messages_sent = 0; const char* telemetry_msg = "test_message"; // 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 // Used to initialize IoTHub SDK subsystem (void)IoTHub_Init(); IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle; (void)printf("Creating IoTHub Device handle\r\n"); // Create the iothub handle here device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol); if (device_ll_handle == NULL) { (void)printf("Failure createing Iothub device. Hint: Check you connection string.\r\n"); } else { // Set any option that are neccessary. // For available options please see the iothub_sdk_options.md documentation bool traceOn = true; IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_LOG_TRACE, &traceOn); #ifdef SET_TRUSTED_CERT_IN_SAMPLES // Setting the Trusted Certificate. This is only necessary on system with without // built in certificate stores. IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_TRUSTED_CERT, certificates); #endif // SET_TRUSTED_CERT_IN_SAMPLES #if defined SAMPLE_MQTT || defined SAMPLE_MQTT_WS //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; //IoTHubDeviceClient_LL_SetOption(iothub_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn); #endif // Setting connection status callback to get indication of connection to iothub (void)IoTHubDeviceClient_LL_SetConnectionStatusCallback(device_ll_handle, connection_status_callback, NULL); do { if (messages_sent < MESSAGE_COUNT) { // Construct the iothub message from a string or a byte array message_handle = IoTHubMessage_CreateFromString(telemetry_msg); //message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))); // 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 IoTHub\r\n", (int)(messages_sent + 1)); IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL); // The message is copied to the sdk so the we can destroy it IoTHubMessage_Destroy(message_handle); messages_sent++; } else if (g_message_count_send_confirmations >= MESSAGE_COUNT) { // After all messages are all received stop running g_continueRunning = false; } IoTHubDeviceClient_LL_DoWork(device_ll_handle); ThreadAPI_Sleep(1); } while (g_continueRunning); // Clean up the iothub sdk handle IoTHubDeviceClient_LL_Destroy(device_ll_handle); } // Free all the sdk subsystem IoTHub_Deinit(); printf("Press any key to continue"); getchar(); return 0; }
int main(void) { TRANSPORT_HANDLE transport_handle; IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol; IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle1; IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle2; #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 g_continueRunning = true; //callbackCounter = 0; int receiveContext1 = 0; int receiveContext2 = 0; (void)printf("Starting the IoTHub client shared sample. Send `quit` message to either device to close...\r\n"); // Used to initialize IoTHub SDK subsystem (void)IoTHub_Init(); if ((transport_handle = IoTHubTransport_Create(protocol, hubName, hubSuffix)) == NULL) { printf("Failed to creating the protocol handle.\r\n"); } else { EVENT_INSTANCE device1_event; EVENT_INSTANCE device2_event; device1_event.deviceId = deviceId1; IOTHUB_CLIENT_DEVICE_CONFIG config1 = { 0 }; config1.deviceId = deviceId1; config1.deviceKey = deviceKey1; config1.deviceSasToken = NULL; config1.protocol = protocol; config1.transportHandle = IoTHubTransport_GetLLTransport(transport_handle); device2_event.deviceId = deviceId2; IOTHUB_CLIENT_DEVICE_CONFIG config2 = { 0 }; config2.deviceId = deviceId2; config2.deviceKey = deviceKey2; config2.deviceSasToken = NULL; config2.protocol = protocol; config2.transportHandle = IoTHubTransport_GetLLTransport(transport_handle); if ((device_ll_handle1 = IoTHubDeviceClient_LL_CreateWithTransport(&config1)) == NULL) { (void)printf("ERROR: iotHubClientHandle1 is NULL!\r\n"); } else if ((device_ll_handle2 = IoTHubDeviceClient_LL_CreateWithTransport(&config2)) == NULL) { (void)printf("ERROR: iotHubClientHandle1 is NULL!\r\n"); } else { // Set any option that are neccessary. // For available options please see the iothub_sdk_options.md documentation //bool traceOn = true; //IoTHubDeviceClient_LL_SetOption(device_ll_handle1, OPTION_LOG_TRACE, &traceOn); //IoTHubDeviceClient_LL_SetOption(device_ll_handle2, OPTION_LOG_TRACE, &traceOn); #ifdef SET_TRUSTED_CERT_IN_SAMPLES // Setting the Trusted Certificate. This is only necessary on system without // built in certificate stores. IoTHubDeviceClient_LL_SetOption(device_ll_handle1, OPTION_TRUSTED_CERT, certificates); IoTHubDeviceClient_LL_SetOption(device_ll_handle2, OPTION_TRUSTED_CERT, certificates); #endif // SET_TRUSTED_CERT_IN_SAMPLES #ifdef SAMPLE_HTTP unsigned int timeout = 241000; // Because it can poll "after 9 seconds" polls will happen effectively // at ~10 seconds. // Note that for scalabilty, the default value of minimumPollingTime // is 25 minutes. For more information, see: // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging unsigned int minimumPollingTime = 9; IoTHubDeviceClient_LL_SetOption(device_ll_handle1, OPTION_MIN_POLLING_TIME, &minimumPollingTime); IoTHubDeviceClient_LL_SetOption(device_ll_handle1, OPTION_HTTP_TIMEOUT, &timeout); IoTHubDeviceClient_LL_SetOption(device_ll_handle2, OPTION_MIN_POLLING_TIME, &minimumPollingTime); IoTHubDeviceClient_LL_SetOption(device_ll_handle2, OPTION_HTTP_TIMEOUT, &timeout); #endif // SAMPLE_HTTP /* Setting Message call back, so we can receive Commands. */ (void)IoTHubDeviceClient_LL_SetMessageCallback(device_ll_handle1, ReceiveMessageCallback, &receiveContext1); (void)IoTHubDeviceClient_LL_SetMessageCallback(device_ll_handle2, ReceiveMessageCallback, &receiveContext2); /* Now that we are ready to receive commands, let's send some messages */ size_t messages_sent = 0; IOTHUB_MESSAGE_HANDLE message_handle; do { if (messages_sent < MESSAGE_COUNT) { // Create the event hub message message_handle = create_events(&device1_event); (void)IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle1, message_handle, SendConfirmationCallback, &device1_event); // The message is copied to the sdk so the we can destroy it IoTHubMessage_Destroy(message_handle); message_handle = create_events(&device2_event); (void)IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle2, message_handle, SendConfirmationCallback, &device2_event); // The message is copied to the sdk so the we can destroy it IoTHubMessage_Destroy(message_handle); messages_sent++; } IoTHubDeviceClient_LL_DoWork(device_ll_handle1); IoTHubDeviceClient_LL_DoWork(device_ll_handle2); ThreadAPI_Sleep(1); } while (g_continueRunning); (void)printf("client_amqp_shared_sample has gotten quit message, call DoWork %d more time to complete final sending...\r\n", DOWORK_LOOP_NUM); for (size_t index = 0; index < DOWORK_LOOP_NUM; index++) { IoTHubDeviceClient_LL_DoWork(device_ll_handle1); IoTHubDeviceClient_LL_DoWork(device_ll_handle2); ThreadAPI_Sleep(1); } // Clean up the iothub sdk handle IoTHubDeviceClient_LL_Destroy(device_ll_handle1); IoTHubDeviceClient_LL_Destroy(device_ll_handle2); } IoTHubTransport_Destroy(transport_handle); // Free all the sdk subsystem IoTHub_Deinit(); } return 0; }