linkkit_example_auto.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "infra_types.h"
  7. #include "infra_defs.h"
  8. #include "infra_compat.h"
  9. #include "infra_compat.h"
  10. #include "dev_model_api.h"
  11. #include "dm_wrapper.h"
  12. #include "cJSON.h"
  13. #ifdef ATM_ENABLED
  14. #include "at_api.h"
  15. #endif
  16. void HAL_Printf(const char *fmt, ...);
  17. int HAL_SetProductKey(char *product_key);
  18. int HAL_SetDeviceName(char *device_name);
  19. int HAL_SetProductSecret(char *product_secret);
  20. int HAL_SetDeviceSecret(char *device_secret);
  21. /* Fill in your device info here */
  22. #define PRODUCT_KEY " ###DEVICE_PK### "
  23. #define PRODUCT_SECRET "0HxtPRZjwr14bi7K"
  24. #define DEVICE_NAME "auto_example1"
  25. #define DEVICE_SECRET "L1xQk7AntumkNNc8wvHwMJsh9PBoexN0"
  26. /* This time interval will be given to SDK for receiving packets from network */
  27. #define USER_EXAMPLE_YIELD_TIMEOUT_MS (200)
  28. #define EXAMPLE_TRACE(...) \
  29. do { \
  30. HAL_Printf("\033[1;32;40m%s.%d: ", __func__, __LINE__); \
  31. HAL_Printf(__VA_ARGS__); \
  32. HAL_Printf("\033[0m\r\n"); \
  33. } while (0)
  34. typedef struct {
  35. int cloud_connected;
  36. int master_devid;
  37. int master_initialized;
  38. } user_example_ctx_t;
  39. static user_example_ctx_t g_user_example_ctx;
  40. static user_example_ctx_t *user_example_get_ctx(void)
  41. {
  42. return &g_user_example_ctx;
  43. }
  44. static int user_connected_event_handler(void)
  45. {
  46. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  47. EXAMPLE_TRACE("Alibaba Cloud Connected Successfully!");
  48. user_example_ctx->cloud_connected = 1;
  49. return 0;
  50. }
  51. static int user_master_dev_available(void)
  52. {
  53. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  54. if (user_example_ctx->cloud_connected && user_example_ctx->master_initialized) {
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. static int user_disconnected_event_handler(void)
  60. {
  61. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  62. EXAMPLE_TRACE("Cloud Disconnected Event Arrived!");
  63. user_example_ctx->cloud_connected = 0;
  64. return 0;
  65. }
  66. static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
  67. {
  68. int res = 0;
  69. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  70. EXAMPLE_TRACE("Property Set Received, Devid: %d, Request: %s", devid, request);
  71. res = IOT_Linkkit_Report(user_example_ctx->master_devid,
  72. ITM_MSG_POST_PROPERTY,
  73. (unsigned char *)request,
  74. request_len);
  75. EXAMPLE_TRACE("Post Property Message ID: %d", res);
  76. return 0;
  77. }
  78. static int user_initialized(const int devid)
  79. {
  80. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  81. EXAMPLE_TRACE("Device Initialized, Devid: %d", devid);
  82. if (user_example_ctx->master_devid == devid) {
  83. user_example_ctx->master_initialized = 1;
  84. }
  85. return 0;
  86. }
  87. void user_post_property(void)
  88. {
  89. int res = 0;
  90. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  91. char *property_payload = "NULL";
  92. /* Normal Example */
  93. property_payload = "{\" ###DM_PROP_ID### \":0}";
  94. res = IOT_Linkkit_Report(user_example_ctx->master_devid,
  95. ITM_MSG_POST_PROPERTY,
  96. (unsigned char *)property_payload,
  97. strlen(property_payload));
  98. EXAMPLE_TRACE("Post Property Message ID: %d", res);
  99. }
  100. void user_post_event(void)
  101. {
  102. int res = 0;
  103. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  104. char *event_id = " ###DM_EVT_ID### ";
  105. char *event_payload = "NULL";
  106. /* Normal Example */
  107. event_payload = "{\" ###DM_EVT_OU_KEY### \":0}";
  108. res = IOT_Linkkit_TriggerEvent(user_example_ctx->master_devid,
  109. event_id,
  110. strlen(event_id),
  111. event_payload,
  112. strlen(event_payload));
  113. EXAMPLE_TRACE("Post Event Message ID: %d", res);
  114. }
  115. void set_iotx_info()
  116. {
  117. HAL_SetProductKey(PRODUCT_KEY);
  118. HAL_SetProductSecret(PRODUCT_SECRET);
  119. HAL_SetDeviceName(DEVICE_NAME);
  120. HAL_SetDeviceSecret(DEVICE_SECRET);
  121. }
  122. int main(void)
  123. {
  124. unsigned int loop_cnt = 0;
  125. int res = 0;
  126. iotx_linkkit_dev_meta_info_t master_meta_info;
  127. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  128. #ifdef ATM_ENABLED
  129. if (IOT_ATM_Init() < 0) {
  130. EXAMPLE_TRACE("IOT ATM init failed!\n");
  131. return -1;
  132. }
  133. #endif
  134. IOT_SetLogLevel(IOT_LOG_DEBUG);
  135. set_iotx_info();
  136. /* Register Callback to Listen What You Interested inside SDK */
  137. IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
  138. IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
  139. IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
  140. IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
  141. /* Create Meta Information Struct for IOT_Linkkit_Open() */
  142. memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
  143. memcpy(master_meta_info.product_key, PRODUCT_KEY, strlen(PRODUCT_KEY));
  144. memcpy(master_meta_info.product_secret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));
  145. memcpy(master_meta_info.device_name, DEVICE_NAME, strlen(DEVICE_NAME));
  146. memcpy(master_meta_info.device_secret, DEVICE_SECRET, strlen(DEVICE_SECRET));
  147. /* Create Master Device Resources by IOT_Linkkit_Open() */
  148. memset(user_example_ctx, 0, sizeof(user_example_ctx_t));
  149. user_example_ctx->master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
  150. if (user_example_ctx->master_devid < 0) {
  151. EXAMPLE_TRACE("IOT_Linkkit_Open() Failed\n");
  152. return -1;
  153. }
  154. EXAMPLE_TRACE("IOT_Linkkit_Open() Success!\n");
  155. /* Start Connectting Alibaba Cloud */
  156. res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
  157. if (res < 0) {
  158. EXAMPLE_TRACE("IOT_Linkkit_Connect() Failed\n");
  159. return -1;
  160. }
  161. EXAMPLE_TRACE("IOT_Linkkit_Connect() Success! res = IOT_Linkkit_Connect() = %d\n", res);
  162. /* Infinite Loop of Processing Command Received from Cloud */
  163. while (1) {
  164. IOT_Linkkit_Yield(USER_EXAMPLE_YIELD_TIMEOUT_MS);
  165. /* Post Proprety Example */
  166. if (loop_cnt % 100 == 0 && user_master_dev_available()) {
  167. EXAMPLE_TRACE("loop_cnt = %d, going to post property\n", loop_cnt);
  168. user_post_property();
  169. }
  170. /* Post Event Example */
  171. if (loop_cnt % 180 == 0 && user_master_dev_available()) {
  172. EXAMPLE_TRACE("loop_cnt = %d, going to post event\n", loop_cnt);
  173. user_post_event();
  174. }
  175. ++ loop_cnt;
  176. }
  177. IOT_Linkkit_Close(user_example_ctx->master_devid);
  178. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  179. IOT_SetLogLevel(IOT_LOG_NONE);
  180. return 0;
  181. }