iothub_ll_c2d_sample.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. // CAVEAT: This sample is to demonstrate azure IoT client concepts only and is not a guide design principles or style
  4. // Checking of return codes and error values shall be omitted for brevity. Please practice sound engineering practices
  5. // when writing production code.
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "iothub.h"
  9. #include "iothub_device_client_ll.h"
  10. #include "iothub_client_options.h"
  11. #include "iothub_message.h"
  12. #include "azure_c_shared_utility/threadapi.h"
  13. #include "azure_c_shared_utility/shared_util_options.h"
  14. #include <rtthread.h>
  15. #ifdef FINSH_USING_MSH
  16. #include <finsh.h>
  17. #endif
  18. // The protocol you wish to use should be uncommented
  19. //
  20. #ifdef PKG_USING_AZURE_MQTT_PROTOCOL
  21. #define SAMPLE_MQTT
  22. #endif
  23. #ifdef PKG_USING_AZURE_HTTP_PROTOCOL
  24. #define SAMPLE_HTTP
  25. #endif
  26. //#define SAMPLE_MQTT_OVER_WEBSOCKETS
  27. //#define SAMPLE_AMQP
  28. //#define SAMPLE_AMQP_OVER_WEBSOCKETS
  29. #ifdef SAMPLE_MQTT
  30. #include "iothubtransportmqtt.h"
  31. #endif // SAMPLE_MQTT
  32. #ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
  33. #include "iothubtransportmqtt_websockets.h"
  34. #endif // SAMPLE_MQTT_OVER_WEBSOCKETS
  35. #ifdef SAMPLE_AMQP
  36. #include "iothubtransportamqp.h"
  37. #endif // SAMPLE_AMQP
  38. #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
  39. #include "iothubtransportamqp_websockets.h"
  40. #endif // SAMPLE_AMQP_OVER_WEBSOCKETS
  41. #ifdef SAMPLE_HTTP
  42. #include "iothubtransporthttp.h"
  43. #endif // SAMPLE_HTTP
  44. #ifdef SET_TRUSTED_CERT_IN_SAMPLES
  45. #include "certs/certs.h"
  46. #endif // SET_TRUSTED_CERT_IN_SAMPLES
  47. /* Paste in the your iothub connection string */
  48. static const char* connectionString = "Your device connection string copied from the device explorer";
  49. #define MESSAGE_COUNT 3
  50. static bool g_continueRunning = true;
  51. static size_t g_message_recv_count = 0;
  52. static IOTHUBMESSAGE_DISPOSITION_RESULT receive_msg_callback(IOTHUB_MESSAGE_HANDLE message, void* user_context)
  53. {
  54. (void)user_context;
  55. const char* messageId;
  56. const char* correlationId;
  57. // Message properties
  58. if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
  59. {
  60. messageId = "<unavailable>";
  61. }
  62. if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
  63. {
  64. correlationId = "<unavailable>";
  65. }
  66. IOTHUBMESSAGE_CONTENT_TYPE content_type = IoTHubMessage_GetContentType(message);
  67. if (content_type == IOTHUBMESSAGE_BYTEARRAY)
  68. {
  69. const unsigned char* buff_msg;
  70. size_t buff_len;
  71. if (IoTHubMessage_GetByteArray(message, &buff_msg, &buff_len) != IOTHUB_MESSAGE_OK)
  72. {
  73. (void)printf("Failure retrieving byte array message\r\n");
  74. }
  75. else
  76. {
  77. (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);
  78. }
  79. }
  80. else
  81. {
  82. const char* string_msg = IoTHubMessage_GetString(message);
  83. if (string_msg == NULL)
  84. {
  85. (void)printf("Failure retrieving byte array message\r\n");
  86. }
  87. else
  88. {
  89. (void)printf("Received String Message\r\nMessage ID: %s\r\n Correlation ID: %s\r\n Data: <<<%s>>>\r\n", messageId, correlationId, string_msg);
  90. }
  91. }
  92. const char* property_value = "property_value";
  93. const char* property_key = IoTHubMessage_GetProperty(message, property_value);
  94. if (property_key != NULL)
  95. {
  96. printf("\r\nMessage Properties:\r\n");
  97. printf("\tKey: %s Value: %s\r\n", property_value, property_key);
  98. }
  99. g_message_recv_count++;
  100. return IOTHUBMESSAGE_ACCEPTED;
  101. }
  102. void azure_iothub_ll_c2d_sample(void *parameter)
  103. {
  104. IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol;
  105. size_t messages_count = 0;
  106. // Select the Protocol to use with the connection
  107. #ifdef SAMPLE_MQTT
  108. protocol = MQTT_Protocol;
  109. #endif // SAMPLE_MQTT
  110. #ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
  111. protocol = MQTT_WebSocket_Protocol;
  112. #endif // SAMPLE_MQTT_OVER_WEBSOCKETS
  113. #ifdef SAMPLE_AMQP
  114. protocol = AMQP_Protocol;
  115. #endif // SAMPLE_AMQP
  116. #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
  117. protocol = AMQP_Protocol_over_WebSocketsTls;
  118. #endif // SAMPLE_AMQP_OVER_WEBSOCKETS
  119. #ifdef SAMPLE_HTTP
  120. protocol = HTTP_Protocol;
  121. #endif // SAMPLE_HTTP
  122. // Used to initialize IoTHub SDK subsystem
  123. (void)IoTHub_Init();
  124. IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle;
  125. (void)printf("Creating IoTHub Device handle\r\n");
  126. // Create the iothub handle here
  127. device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol);
  128. if (device_ll_handle == NULL)
  129. {
  130. (void)printf("Failure createing Iothub device. Hint: Check you connection string.\r\n");
  131. }
  132. else
  133. {
  134. // Set any option that are neccessary.
  135. // For available options please see the iothub_sdk_options.md documentation
  136. //bool traceOn = true;
  137. //IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_LOG_TRACE, &traceOn);
  138. #ifdef SET_TRUSTED_CERT_IN_SAMPLES
  139. // Setting the Trusted Certificate. This is only necessary on system with without
  140. // built in certificate stores.
  141. IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_TRUSTED_CERT, certificates);
  142. #endif // SET_TRUSTED_CERT_IN_SAMPLES
  143. #if defined SAMPLE_MQTT || defined SAMPLE_MQTT_WS
  144. //Setting the auto URL Decoder (recommended for MQTT). Please use this option unless
  145. //you are URL Decoding responses yourself.
  146. //ONLY valid for use with MQTT
  147. //bool urlDecodeOn = true;
  148. //IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlDecodeOn);
  149. #endif
  150. #ifdef SAMPLE_HTTP
  151. unsigned int timeout = 241000;
  152. // Because it can poll "after 9 seconds" polls will happen effectively // at ~10 seconds.
  153. // Note that for scalabilty, the default value of minimumPollingTime
  154. // is 25 minutes. For more information, see:
  155. // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
  156. unsigned int minimumPollingTime = 9;
  157. IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_MIN_POLLING_TIME, &minimumPollingTime);
  158. IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_HTTP_TIMEOUT, &timeout);
  159. #endif // SAMPLE_HTTP
  160. if (IoTHubDeviceClient_LL_SetMessageCallback(device_ll_handle, receive_msg_callback, &messages_count) != IOTHUB_CLIENT_OK)
  161. {
  162. (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
  163. }
  164. else
  165. {
  166. (void)printf("Waiting for message to be sent to device (will quit after %d messages)\r\n", MESSAGE_COUNT);
  167. do
  168. {
  169. if (g_message_recv_count >= MESSAGE_COUNT)
  170. {
  171. // After all messages are all received stop running
  172. g_continueRunning = false;
  173. }
  174. IoTHubDeviceClient_LL_DoWork(device_ll_handle);
  175. ThreadAPI_Sleep(10);
  176. } while (g_continueRunning);
  177. }
  178. // Clean up the iothub sdk handle
  179. IoTHubDeviceClient_LL_Destroy(device_ll_handle);
  180. }
  181. // Free all the sdk subsystem
  182. IoTHub_Deinit();
  183. rt_kprintf("Azure Sample Exit");
  184. //getchar();
  185. }
  186. #define THREAD_PRIORITY 10
  187. #define THREAD_STACK_SIZE 8192
  188. #define THREAD_TIMESLICE 5
  189. int azure_c2d_sample()
  190. {
  191. rt_thread_t tid1 = RT_NULL;
  192. tid1 = rt_thread_create("azure_c2d_sample",
  193. azure_iothub_ll_c2d_sample, (void*)1,
  194. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  195. if (tid1 != RT_NULL)
  196. rt_thread_startup(tid1);
  197. else
  198. return -1;
  199. return 0;
  200. }
  201. MSH_CMD_EXPORT(azure_c2d_sample, iothub_ll_c2d_sample);