linkkit_example_gateway.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #include "infra_config.h"
  5. #ifdef DEPRECATED_LINKKIT
  6. #include "gateway.c"
  7. #else
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14. #include "cJSON.h"
  15. #include "infra_types.h"
  16. #include "infra_defs.h"
  17. #include "infra_compat.h"
  18. #include "infra_log.h"
  19. #include "infra_compat.h"
  20. #include "infra_log.h"
  21. #include "dev_model_api.h"
  22. #include "dm_wrapper.h"
  23. #ifdef LINKKIT_GATEWAY_TEST_CMD
  24. #include "simulate_subdev/testcmd.h"
  25. #endif
  26. #if defined(OTA_ENABLED) && defined(BUILD_AOS)
  27. #include "ota_service.h"
  28. #endif
  29. char PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
  30. char PRODUCT_SECRET[IOTX_PRODUCT_SECRET_LEN + 1] = {0};
  31. char DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
  32. char DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
  33. #define USER_EXAMPLE_YIELD_TIMEOUT_MS (200)
  34. void HAL_Printf(const char *fmt, ...);
  35. #define EXAMPLE_TRACE(...) \
  36. do { \
  37. HAL_Printf("\033[1;32;40m%s.%d: ", __func__, __LINE__); \
  38. HAL_Printf(__VA_ARGS__); \
  39. HAL_Printf("\033[0m\r\n"); \
  40. } while (0)
  41. #define EXAMPLE_SUBDEV_ADD_NUM 3
  42. #define EXAMPLE_SUBDEV_MAX_NUM 20
  43. const iotx_linkkit_dev_meta_info_t subdevArr[EXAMPLE_SUBDEV_MAX_NUM] = {
  44. {
  45. "a13Npv1vjZ4",
  46. "PKbZL7baK8pBso94",
  47. "example_sub1",
  48. "eglNFNJiRuR0yncB9RP05sSTY4FrUIoe"
  49. },
  50. {
  51. "a1YRfb9bepk",
  52. "PKbZL7baK8pBso94",
  53. "test_02",
  54. "jFsErM3uA7UfbS6J0hm0QaEXsQbmO6Pa"
  55. },
  56. {
  57. "a1YRfb9bepk",
  58. "PKbZL7baK8pBso94",
  59. "test_03",
  60. "MjWMvCLBcuZyqUswryBbgypN8uOgJGVD"
  61. },
  62. {
  63. "a1YRfb9bepk",
  64. "PKbZL7baK8pBso94",
  65. "test_04",
  66. "DXbcbpxepIyYm3BiS0ICdBou4uWPfP6L"
  67. },
  68. {
  69. "a1YRfb9bepk",
  70. "PKbZL7baK8pBso94",
  71. "test_05",
  72. "VKuVZfcz3umcoR3WhOp4cu1p2dyTQGq1"
  73. },
  74. {
  75. "a1YRfb9bepk",
  76. "PKbZL7baK8pBso94",
  77. "test_06",
  78. "QTobiz1BdGW5XNgLGIgNSylH0btVvvGS"
  79. },
  80. {
  81. "a1YRfb9bepk",
  82. "PKbZL7baK8pBso94",
  83. "test_07",
  84. "IX7ol50rRS2uP8V74jt0DKfmYn8iC6h1"
  85. },
  86. {
  87. "a1YRfb9bepk",
  88. "PKbZL7baK8pBso94",
  89. "test_08",
  90. "sbFxx62evXVoVgJ5gL2oCLcz1pX9d6K2"
  91. },
  92. {
  93. "a1YRfb9bepk",
  94. "PKbZL7baK8pBso94",
  95. "test_09",
  96. "S0CpOl54GZxEO7Gz5DWQa5YxgUMfT4xA"
  97. },
  98. {
  99. "a1YRfb9bepk",
  100. "PKbZL7baK8pBso94",
  101. "test_10",
  102. "MowJJjiNIkTdUcX5fCNUDu39Yz02KADL"
  103. },
  104. {
  105. "a1YRfb9bepk",
  106. "PKbZL7baK8pBso94",
  107. "test_11",
  108. "BrJNdAihVznMWTpdRfe8HIiI95ubSYdN"
  109. },
  110. {
  111. "a1YRfb9bepk",
  112. "PKbZL7baK8pBso94",
  113. "test_12",
  114. "Yd3ZHK8D6cAKKRQb9rUevCfwPf7atoQ4"
  115. },
  116. {
  117. "a1YRfb9bepk",
  118. "PKbZL7baK8pBso94",
  119. "test_13",
  120. "d3HKvu2eBR5ytcgDaBEt0gpvJZlu9W0g"
  121. },
  122. {
  123. "a1YRfb9bepk",
  124. "PKbZL7baK8pBso94",
  125. "test_14",
  126. "qAJYUpQ1tGmAINQBzMiZwwbyjY6YXDGc"
  127. },
  128. {
  129. "a1YRfb9bepk",
  130. "PKbZL7baK8pBso94",
  131. "test_15",
  132. "GxgVknnAmUmwjjdHJf9dbEBDoqyDaUfp"
  133. },
  134. {
  135. "a1YRfb9bepk",
  136. "PKbZL7baK8pBso94",
  137. "test_16",
  138. "9d17Sv05j1XeTYOs80UBpBU1OYTTJ58X"
  139. },
  140. {
  141. "a1YRfb9bepk",
  142. "PKbZL7baK8pBso94",
  143. "test_17",
  144. "FTUm4HAfhZ5wH2u0pPn7PWcCLGDrgEfn"
  145. },
  146. {
  147. "a1YRfb9bepk",
  148. "PKbZL7baK8pBso94",
  149. "test_18",
  150. "mF7a2ptc3PRi7jWLE92t0GElhGdPnAe3"
  151. },
  152. {
  153. "a1YRfb9bepk",
  154. "PKbZL7baK8pBso94",
  155. "test_19",
  156. "Vrh8HuNvu3jtTEwSzulAjTqgOseAsVgz"
  157. },
  158. {
  159. "a1YRfb9bepk",
  160. "PKbZL7baK8pBso94",
  161. "test_20",
  162. "8Wxrxnjch6SW0s2HR5JkIBtgjt3BOUo7"
  163. }
  164. };
  165. typedef struct {
  166. int auto_add_subdev;
  167. int master_devid;
  168. int cloud_connected;
  169. int master_initialized;
  170. int subdev_index;
  171. int permit_join;
  172. void *g_user_dispatch_thread;
  173. int g_user_dispatch_thread_running;
  174. } user_example_ctx_t;
  175. static user_example_ctx_t g_user_example_ctx;
  176. void *example_malloc(size_t size)
  177. {
  178. return HAL_Malloc(size);
  179. }
  180. void example_free(void *ptr)
  181. {
  182. HAL_Free(ptr);
  183. }
  184. static user_example_ctx_t *user_example_get_ctx(void)
  185. {
  186. return &g_user_example_ctx;
  187. }
  188. static int user_connected_event_handler(void)
  189. {
  190. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  191. EXAMPLE_TRACE("Cloud Connected");
  192. user_example_ctx->cloud_connected = 1;
  193. return 0;
  194. }
  195. static int user_disconnected_event_handler(void)
  196. {
  197. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  198. EXAMPLE_TRACE("Cloud Disconnected");
  199. user_example_ctx->cloud_connected = 0;
  200. return 0;
  201. }
  202. static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
  203. {
  204. int res = 0;
  205. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  206. EXAMPLE_TRACE("Property Set Received, Devid: %d, Request: %s", devid, request);
  207. res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
  208. (unsigned char *)request, request_len);
  209. EXAMPLE_TRACE("Post Property Message ID: %d", res);
  210. return 0;
  211. }
  212. static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
  213. const int reply_len)
  214. {
  215. const char *reply_value = (reply == NULL) ? ("NULL") : (reply);
  216. const int reply_value_len = (reply_len == 0) ? (strlen("NULL")) : (reply_len);
  217. EXAMPLE_TRACE("Message Post Reply Received, Devid: %d, Message ID: %d, Code: %d, Reply: %.*s", devid, msgid, code,
  218. reply_value_len,
  219. reply_value);
  220. return 0;
  221. }
  222. static int user_timestamp_reply_event_handler(const char *timestamp)
  223. {
  224. EXAMPLE_TRACE("Current Timestamp: %s", timestamp);
  225. return 0;
  226. }
  227. static int user_initialized(const int devid)
  228. {
  229. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  230. EXAMPLE_TRACE("Device Initialized, Devid: %d", devid);
  231. if (user_example_ctx->master_devid == devid) {
  232. user_example_ctx->master_initialized = 1;
  233. user_example_ctx->subdev_index++;
  234. }
  235. return 0;
  236. }
  237. static uint64_t user_update_sec(void)
  238. {
  239. static uint64_t time_start_ms = 0;
  240. if (time_start_ms == 0) {
  241. time_start_ms = HAL_UptimeMs();
  242. }
  243. return (HAL_UptimeMs() - time_start_ms) / 1000;
  244. }
  245. void user_post_property(void)
  246. {
  247. int res = 0;
  248. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  249. char *property_payload = "{\"Counter\":1}";
  250. res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
  251. (unsigned char *)property_payload, strlen(property_payload));
  252. EXAMPLE_TRACE("Post Property Message ID: %d", res);
  253. }
  254. void user_deviceinfo_update(void)
  255. {
  256. int res = 0;
  257. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  258. char *device_info_update = "[{\"attrKey\":\"abc\",\"attrValue\":\"hello,world\"}]";
  259. res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_DEVICEINFO_UPDATE,
  260. (unsigned char *)device_info_update, strlen(device_info_update));
  261. EXAMPLE_TRACE("Device Info Update Message ID: %d", res);
  262. }
  263. void user_deviceinfo_delete(void)
  264. {
  265. int res = 0;
  266. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  267. char *device_info_delete = "[{\"attrKey\":\"abc\"}]";
  268. res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_DEVICEINFO_DELETE,
  269. (unsigned char *)device_info_delete, strlen(device_info_delete));
  270. EXAMPLE_TRACE("Device Info Delete Message ID: %d", res);
  271. }
  272. static int user_master_dev_available(void)
  273. {
  274. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  275. if (user_example_ctx->cloud_connected && user_example_ctx->master_initialized) {
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. static int example_add_subdev(iotx_linkkit_dev_meta_info_t *meta_info)
  281. {
  282. int res = 0, devid = -1;
  283. devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_SLAVE, meta_info);
  284. if (devid == FAIL_RETURN) {
  285. EXAMPLE_TRACE("subdev open Failed\n");
  286. return FAIL_RETURN;
  287. }
  288. EXAMPLE_TRACE("subdev open susseed, devid = %d\n", devid);
  289. res = IOT_Linkkit_Connect(devid);
  290. if (res == FAIL_RETURN) {
  291. EXAMPLE_TRACE("subdev connect Failed\n");
  292. return res;
  293. }
  294. EXAMPLE_TRACE("subdev connect success: devid = %d\n", devid);
  295. res = IOT_Linkkit_Report(devid, ITM_MSG_LOGIN, NULL, 0);
  296. if (res == FAIL_RETURN) {
  297. EXAMPLE_TRACE("subdev login Failed\n");
  298. return res;
  299. }
  300. EXAMPLE_TRACE("subdev login success: devid = %d\n", devid);
  301. return res;
  302. }
  303. int user_permit_join_event_handler(const char *product_key, const int time)
  304. {
  305. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  306. EXAMPLE_TRACE("Product Key: %s, Time: %d", product_key, time);
  307. user_example_ctx->permit_join = 1;
  308. return 0;
  309. }
  310. void *user_dispatch_yield(void *args)
  311. {
  312. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  313. while (user_example_ctx->g_user_dispatch_thread_running) {
  314. IOT_Linkkit_Yield(USER_EXAMPLE_YIELD_TIMEOUT_MS);
  315. }
  316. return NULL;
  317. }
  318. static int max_running_seconds = 0;
  319. int main(int argc, char **argv)
  320. {
  321. int res = 0;
  322. uint64_t time_prev_sec = 0, time_now_sec = 0, time_begin_sec = 0;
  323. user_example_ctx_t *user_example_ctx = user_example_get_ctx();
  324. iotx_linkkit_dev_meta_info_t master_meta_info;
  325. int domain_type = 0;
  326. int dynamic_register = 0;
  327. int post_event_reply = 0;
  328. memset(user_example_ctx, 0, sizeof(user_example_ctx_t));
  329. #if defined(__UBUNTU_SDK_DEMO__)
  330. if (argc > 1) {
  331. int tmp = atoi(argv[1]);
  332. if (tmp >= 60) {
  333. max_running_seconds = tmp;
  334. EXAMPLE_TRACE("set [max_running_seconds] = %d seconds\n", max_running_seconds);
  335. }
  336. }
  337. if (argc > 2) {
  338. if (strlen("auto") == strlen(argv[2]) &&
  339. memcmp("auto", argv[2], strlen(argv[2])) == 0) {
  340. user_example_ctx->auto_add_subdev = 1;
  341. }
  342. }
  343. #endif
  344. HAL_GetProductKey(PRODUCT_KEY);
  345. HAL_GetProductSecret(PRODUCT_SECRET);
  346. HAL_GetDeviceName(DEVICE_NAME);
  347. HAL_GetDeviceSecret(DEVICE_SECRET);
  348. user_example_ctx->subdev_index = -1;
  349. IOT_SetLogLevel(IOT_LOG_DEBUG);
  350. /* Register Callback */
  351. IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
  352. IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
  353. IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
  354. IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
  355. IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
  356. IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
  357. IOT_RegisterCallback(ITE_PERMIT_JOIN, user_permit_join_event_handler);
  358. memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
  359. memcpy(master_meta_info.product_key, PRODUCT_KEY, strlen(PRODUCT_KEY));
  360. memcpy(master_meta_info.product_secret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));
  361. memcpy(master_meta_info.device_name, DEVICE_NAME, strlen(DEVICE_NAME));
  362. memcpy(master_meta_info.device_secret, DEVICE_SECRET, strlen(DEVICE_SECRET));
  363. /* Create Master Device Resources */
  364. user_example_ctx->master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
  365. if (user_example_ctx->master_devid < 0) {
  366. EXAMPLE_TRACE("IOT_Linkkit_Open Failed\n");
  367. return -1;
  368. }
  369. /* Choose Login Server */
  370. domain_type = IOTX_CLOUD_REGION_SHANGHAI;
  371. IOT_Ioctl(IOTX_IOCTL_SET_DOMAIN, (void *)&domain_type);
  372. /* Choose Login Method */
  373. dynamic_register = 0;
  374. IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);
  375. /* Choose Whether You Need Post Property/Event Reply */
  376. post_event_reply = 0;
  377. IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_event_reply);
  378. /* Start Connect Aliyun Server */
  379. res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
  380. if (res < 0) {
  381. EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
  382. return -1;
  383. }
  384. user_example_ctx->g_user_dispatch_thread_running = 1;
  385. res = HAL_ThreadCreate(&user_example_ctx->g_user_dispatch_thread, user_dispatch_yield, NULL, NULL, NULL);
  386. if (res < 0) {
  387. EXAMPLE_TRACE("HAL_ThreadCreate Failed\n");
  388. IOT_Linkkit_Close(user_example_ctx->master_devid);
  389. return -1;
  390. }
  391. time_begin_sec = user_update_sec();
  392. while (1) {
  393. HAL_SleepMs(200);
  394. time_now_sec = user_update_sec();
  395. if (time_prev_sec == time_now_sec) {
  396. continue;
  397. }
  398. if (max_running_seconds && (time_now_sec - time_begin_sec > max_running_seconds)) {
  399. EXAMPLE_TRACE("Example Run for Over %d Seconds, Break Loop!\n", max_running_seconds);
  400. break;
  401. }
  402. /* Add subdev */
  403. if (user_example_ctx->master_initialized && user_example_ctx->subdev_index >= 0 &&
  404. (user_example_ctx->auto_add_subdev == 1 || user_example_ctx->permit_join != 0)) {
  405. if (user_example_ctx->subdev_index < EXAMPLE_SUBDEV_ADD_NUM) {
  406. /* Add next subdev */
  407. if (example_add_subdev((iotx_linkkit_dev_meta_info_t *)&subdevArr[user_example_ctx->subdev_index]) == SUCCESS_RETURN) {
  408. EXAMPLE_TRACE("subdev %s add succeed", subdevArr[user_example_ctx->subdev_index].device_name);
  409. } else {
  410. EXAMPLE_TRACE("subdev %s add failed", subdevArr[user_example_ctx->subdev_index].device_name);
  411. }
  412. user_example_ctx->subdev_index++;
  413. user_example_ctx->permit_join = 0;
  414. }
  415. }
  416. /* Post Proprety Example */
  417. if (time_now_sec % 11 == 0 && user_master_dev_available()) {
  418. /* user_post_property(); */
  419. }
  420. /* Device Info Update Example */
  421. if (time_now_sec % 23 == 0 && user_master_dev_available()) {
  422. /* user_deviceinfo_update(); */
  423. }
  424. /* Device Info Delete Example */
  425. if (time_now_sec % 29 == 0 && user_master_dev_available()) {
  426. /* user_deviceinfo_delete(); */
  427. }
  428. time_prev_sec = time_now_sec;
  429. }
  430. user_example_ctx->g_user_dispatch_thread_running = 0;
  431. IOT_Linkkit_Close(user_example_ctx->master_devid);
  432. HAL_ThreadDelete(user_example_ctx->g_user_dispatch_thread);
  433. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  434. IOT_SetLogLevel(IOT_LOG_NONE);
  435. return 0;
  436. }
  437. #endif