gateway.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <time.h>
  10. #include "cJSON.h"
  11. #include "infra_compat.h"
  12. #include "infra_defs.h"
  13. #include "linkkit_gateway_export.h"
  14. #ifdef LINKKIT_GATEWAY_TEST_CMD
  15. #include "simulate_subdev/testcmd.h"
  16. #endif
  17. void HAL_Free(void *ptr);
  18. uint64_t HAL_UptimeMs(void);
  19. void HAL_SleepMs(uint32_t ms);
  20. int HAL_SetProductKey(char *product_key);
  21. int HAL_SetDeviceName(char *device_name);
  22. int HAL_SetProductSecret(char *product_secret);
  23. int HAL_SetDeviceSecret(char *device_secret);
  24. /* for demo only */
  25. #define PRODUCT_KEY "a16UKrlKekO"
  26. #define PRODUCT_SECRET "RDluqbn3LQazrdqM"
  27. #define DEVICE_NAME "gateway_test01"
  28. #define DEVICE_SECRET "AT2XFOPOIbJaKfXsKeaEhabJ8TLhMQYp"
  29. #define EXAMPLE_TRACE(...) \
  30. do { \
  31. printf("\033[1;31;40m%s.%d: ", __func__, __LINE__); \
  32. printf(__VA_ARGS__); \
  33. printf("\033[0m"); \
  34. } while (0)
  35. typedef struct {
  36. int ZB_Band;
  37. int ZB_Channel;
  38. char ZB_CO_MAC[32 + 1];
  39. char ZB_PAN_ID[32 + 1];
  40. char EXT_PAN_ID[32 + 1];
  41. char NETWORK_KEY[32 + 1];
  42. int connected;
  43. int register_completed;
  44. int lk_dev;
  45. } gateway_t;
  46. gateway_t gateway;
  47. static int gateway_register_complete(void *ctx);
  48. static int gateway_get_property(char *in, char *out, int out_len, void *ctx);
  49. static int gateway_set_property(char *in, void *ctx);
  50. static int gateway_call_service(char *identifier, char *in, char *out,
  51. int out_len, void *ctx);
  52. /* callback function */
  53. static linkkit_cbs_t linkkit_cbs = {
  54. .register_complete = gateway_register_complete,
  55. .get_property = gateway_get_property,
  56. .set_property = gateway_set_property,
  57. .call_service = gateway_call_service,
  58. };
  59. #ifdef LINKKIT_GATEWAY_TEST_CMD
  60. static int disable_join = 1; /* not allow to join */
  61. static char permit_productKey[PRODUCT_KEY_MAXLEN];
  62. static uint64_t permit_timeout_ms;
  63. int linkkit_gateway_check_permit(int devtype, const char *productKey)
  64. {
  65. uint64_t uptime_ms = HAL_UptimeMs();
  66. EXAMPLE_TRACE("devtype = %d\n", devtype);
  67. if (1 != devtype) {
  68. return 1;
  69. }
  70. EXAMPLE_TRACE("disable_join = %d\n", disable_join);
  71. if (disable_join) {
  72. return 0;
  73. }
  74. EXAMPLE_TRACE("permit_productKey = %s\n", permit_productKey);
  75. if (permit_productKey[0] && strcmp(permit_productKey, productKey)) {
  76. return 0;
  77. }
  78. EXAMPLE_TRACE("uptime_ms = %llu, permit_timeout_ms = %llu\n", uptime_ms,
  79. permit_timeout_ms);
  80. if (uptime_ms < permit_timeout_ms) {
  81. return 1;
  82. }
  83. disable_join = 1;
  84. return 0;
  85. }
  86. int linkkit_gateway_testcmd_post_properties(const char *properties)
  87. {
  88. if (linkkit_gateway_post_property_json_sync(gateway.lk_dev,
  89. (char *)properties, 10000) < 0) {
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. int linkkit_gateway_testcmd_post_event(const char *identifier,
  95. const char *events)
  96. {
  97. if (linkkit_gateway_trigger_event_json_sync(
  98. gateway.lk_dev, (char *)identifier, (char *)events, 10000) < 0) {
  99. return -1;
  100. }
  101. return 0;
  102. }
  103. #endif
  104. static int gateway_register_complete(void *ctx)
  105. {
  106. gateway_t *gw = (gateway_t *)ctx;
  107. gw->register_completed = 1;
  108. return 0;
  109. }
  110. /*
  111. * the handler property get
  112. * alink method: thing.service.property.get
  113. */
  114. static int gateway_get_property(char *in, char *out, int out_len, void *ctx)
  115. {
  116. gateway_t *gw = ctx;
  117. cJSON *rJson, *pJson;
  118. char *p;
  119. int iSize, i;
  120. EXAMPLE_TRACE("in: %s\n", in);
  121. if (!gw) {
  122. EXAMPLE_TRACE("gateway not found\n");
  123. return -1;
  124. }
  125. /* parse input json */
  126. rJson = cJSON_Parse(in);
  127. if (!rJson) {
  128. return -1;
  129. }
  130. iSize = cJSON_GetArraySize(rJson);
  131. if (iSize <= 0) {
  132. cJSON_Delete(rJson);
  133. return -1;
  134. }
  135. pJson = cJSON_CreateObject();
  136. if (!pJson) {
  137. cJSON_Delete(rJson);
  138. return -1;
  139. }
  140. /*
  141. * follow TSL to parse input json, and generate output json.
  142. * please modify this logic in user's case follow as user's TSL.
  143. */
  144. for (i = 0; i < iSize; i++) {
  145. cJSON *pSub = cJSON_GetArrayItem(rJson, i);
  146. if (strcmp(pSub->valuestring, "ZB_Band") == 0) {
  147. cJSON_AddNumberToObject(pJson, "ZB_Band", gw->ZB_Band);
  148. } else if (strcmp(pSub->valuestring, "ZB_Channel") == 0) {
  149. cJSON_AddNumberToObject(pJson, "ZB_Channel", gw->ZB_Channel);
  150. } else if (strcmp(pSub->valuestring, "ZB_CO_MAC") == 0) {
  151. cJSON_AddStringToObject(pJson, "ZB_CO_MAC", gw->ZB_CO_MAC);
  152. } else if (strcmp(pSub->valuestring, "ZB_PAN_ID") == 0) {
  153. cJSON_AddStringToObject(pJson, "ZB_PAN_ID", gw->ZB_PAN_ID);
  154. } else if (strcmp(pSub->valuestring, "EXT_PAN_ID") == 0) {
  155. cJSON_AddStringToObject(pJson, "EXT_PAN_ID", gw->EXT_PAN_ID);
  156. } else if (strcmp(pSub->valuestring, "NETWORK_KEY") == 0) {
  157. cJSON_AddStringToObject(pJson, "NETWORK_KEY", gw->NETWORK_KEY);
  158. }
  159. }
  160. p = cJSON_PrintUnformatted(pJson);
  161. if (!p) {
  162. cJSON_Delete(rJson);
  163. cJSON_Delete(pJson);
  164. return -1;
  165. }
  166. if (strlen(p) >= out_len) {
  167. cJSON_Delete(rJson);
  168. cJSON_Delete(pJson);
  169. HAL_Free(p);
  170. return -1;
  171. }
  172. strcpy(out, p);
  173. EXAMPLE_TRACE("out: %s\n", out);
  174. cJSON_Delete(rJson);
  175. cJSON_Delete(pJson);
  176. HAL_Free(p);
  177. return 0;
  178. }
  179. /*
  180. * the handler property set
  181. * alink method: thing.service.property.set
  182. */
  183. static int gateway_set_property(char *in, void *ctx)
  184. {
  185. gateway_t *gw = ctx;
  186. cJSON *rJson, *ZB_Band, *ZB_Channel, *ZB_PAN_ID, *EXT_PAN_ID, *ZB_CO_MAC,
  187. *NETWORK_KEY;
  188. EXAMPLE_TRACE("in: %s\n", in);
  189. rJson = cJSON_Parse(in);
  190. if (!rJson) {
  191. return -1;
  192. }
  193. /* parse input json, set the gateway value */
  194. ZB_Band = cJSON_GetObjectItem(rJson, "ZB_Band");
  195. if (ZB_Band) {
  196. gw->ZB_Band = ZB_Band->valueint;
  197. }
  198. ZB_Channel = cJSON_GetObjectItem(rJson, "ZB_Channel");
  199. if (ZB_Channel) {
  200. gw->ZB_Channel = ZB_Channel->valueint;
  201. }
  202. ZB_PAN_ID = cJSON_GetObjectItem(rJson, "ZB_PAN_ID");
  203. if (ZB_PAN_ID)
  204. strncpy(gw->ZB_PAN_ID, ZB_PAN_ID->valuestring,
  205. sizeof(gw->ZB_PAN_ID) - 1);
  206. EXT_PAN_ID = cJSON_GetObjectItem(rJson, "EXT_PAN_ID");
  207. if (EXT_PAN_ID)
  208. strncpy(gw->EXT_PAN_ID, EXT_PAN_ID->valuestring,
  209. sizeof(gw->EXT_PAN_ID) - 1);
  210. ZB_CO_MAC = cJSON_GetObjectItem(rJson, "ZB_CO_MAC");
  211. if (ZB_CO_MAC)
  212. strncpy(gw->ZB_CO_MAC, ZB_CO_MAC->valuestring,
  213. sizeof(gw->ZB_CO_MAC) - 1);
  214. NETWORK_KEY = cJSON_GetObjectItem(rJson, "NETWORK_KEY");
  215. if (NETWORK_KEY)
  216. strncpy(gw->NETWORK_KEY, NETWORK_KEY->valuestring,
  217. sizeof(gw->NETWORK_KEY) - 1);
  218. linkkit_gateway_post_property_json_sync(gw->lk_dev, in, 5000);
  219. cJSON_Delete(rJson);
  220. return 0;
  221. }
  222. /*
  223. * the handler of service which is defined by identifier, not property
  224. * alink method: thing.service.{tsl.service.identifier}
  225. */
  226. static int gateway_call_service(char *identifier, char *in, char *out,
  227. int out_len, void *ctx)
  228. {
  229. /*
  230. * please follow user's TSL to mofidy this id - SetTimerTask and TimeReset.
  231. */
  232. if (strcmp(identifier, "SetTimerTask") == 0) {
  233. snprintf(out, out_len, "{\"SetTimer\": \"hello, gateway!\"}");
  234. } else if (strcmp(identifier, "TimeReset") == 0) {
  235. EXAMPLE_TRACE("TimeReset params: %s\n", in);
  236. }
  237. return 0;
  238. }
  239. /*
  240. * post all properties
  241. */
  242. static int post_all_properties(gateway_t *gw)
  243. {
  244. cJSON *pJson = cJSON_CreateObject();
  245. char *p = NULL;
  246. if (!pJson) {
  247. return -1;
  248. }
  249. cJSON_AddNumberToObject(pJson, "ZB_Band", gw->ZB_Band);
  250. cJSON_AddNumberToObject(pJson, "ZB_Channel", gw->ZB_Channel);
  251. cJSON_AddStringToObject(pJson, "ZB_CO_MAC", gw->ZB_CO_MAC);
  252. cJSON_AddStringToObject(pJson, "ZB_PAN_ID", gw->ZB_PAN_ID);
  253. cJSON_AddStringToObject(pJson, "EXT_PAN_ID", gw->EXT_PAN_ID);
  254. cJSON_AddStringToObject(pJson, "NETWORK_KEY", gw->NETWORK_KEY);
  255. p = cJSON_PrintUnformatted(pJson);
  256. if (!p) {
  257. cJSON_Delete(pJson);
  258. return -1;
  259. }
  260. EXAMPLE_TRACE("property: %s\n", p);
  261. linkkit_gateway_post_property_json_sync(gw->lk_dev, p, 5000);
  262. cJSON_Delete(pJson);
  263. HAL_Free(p);
  264. return 0;
  265. }
  266. /* event handler for gateway */
  267. static int event_handler(linkkit_event_t *ev, void *ctx)
  268. {
  269. gateway_t *gw = ctx;
  270. switch (ev->event_type) {
  271. /* cloud connected */
  272. case LINKKIT_EVENT_CLOUD_CONNECTED: {
  273. EXAMPLE_TRACE("cloud connected\n");
  274. /* modify user's logic in there */
  275. /* example case just post all property */
  276. post_all_properties(gw); /* sync to cloud */
  277. gw->connected = 1;
  278. }
  279. break;
  280. /* cloud disconnected */
  281. case LINKKIT_EVENT_CLOUD_DISCONNECTED: {
  282. gw->connected = 0;
  283. EXAMPLE_TRACE("cloud disconnected\n");
  284. }
  285. break;
  286. /* subdev delete */
  287. case LINKKIT_EVENT_SUBDEV_DELETED: {
  288. char *productKey = ev->event_data.subdev_deleted.productKey;
  289. char *deviceName = ev->event_data.subdev_deleted.deviceName;
  290. EXAMPLE_TRACE("delete subdev %s<%s>\n", productKey, deviceName);
  291. #ifdef LINKKIT_GATEWAY_TEST_CMD
  292. if (testcmd_delete_device(productKey, deviceName)) {
  293. EXAMPLE_TRACE("Delete subdevice %s<%s> failed!\n", productKey,
  294. deviceName);
  295. }
  296. #endif
  297. }
  298. break;
  299. /* between timeoutSec, subdev of productKey can be register */
  300. case LINKKIT_EVENT_SUBDEV_PERMITED: {
  301. char *productKey = ev->event_data.subdev_permited.productKey;
  302. int timeoutSec = ev->event_data.subdev_permited.timeoutSec;
  303. EXAMPLE_TRACE("permit subdev %s in %d seconds\n", productKey,
  304. timeoutSec);
  305. #ifdef LINKKIT_GATEWAY_TEST_CMD
  306. disable_join = 0;
  307. if (NULL == productKey) {
  308. memset(permit_productKey, 0, sizeof(permit_productKey));
  309. } else {
  310. strcpy(permit_productKey, productKey);
  311. }
  312. permit_timeout_ms = HAL_UptimeMs() + timeoutSec * 1000;
  313. #endif
  314. /* please enter user's logic in there */
  315. }
  316. break;
  317. }
  318. return 0;
  319. }
  320. void set_iotx_info()
  321. {
  322. HAL_SetProductKey(PRODUCT_KEY);
  323. HAL_SetProductSecret(PRODUCT_SECRET);
  324. HAL_SetDeviceName(DEVICE_NAME);
  325. HAL_SetDeviceSecret(DEVICE_SECRET);
  326. }
  327. int main(int argc, char **argv)
  328. {
  329. linkkit_params_t *initParams = NULL;
  330. int maxMsgSize, maxMsgQueueSize, prop_post_reply, event_post_reply;
  331. IOT_SetLogLevel(IOT_LOG_DEBUG);
  332. #if !defined(WIFI_PROVISION_ENABLED) || !defined(BUILD_AOS)
  333. set_iotx_info();
  334. #endif
  335. memset(&gateway, 0, sizeof(gateway_t));
  336. /* fill fake zigbee network info */
  337. /*
  338. * this example is a zigbee subdev.
  339. * please modify this logic follow as user's case.
  340. */
  341. gateway.ZB_Band = 25;
  342. gateway.ZB_Channel = 16;
  343. strcpy(gateway.ZB_PAN_ID, "8215");
  344. strcpy(gateway.EXT_PAN_ID, "000D6F000ED34E34");
  345. strcpy(gateway.ZB_CO_MAC, "000D6F000ED34E34");
  346. strcpy(gateway.NETWORK_KEY, "21B9F385F114B1C4AE07D5753B95355D");
  347. /*
  348. * please set init parameter
  349. */
  350. initParams = linkkit_gateway_get_default_params();
  351. if (!initParams) {
  352. return -1;
  353. }
  354. /* LINKKIT_OPT_MAX_MSG_SIZE: max size of message */
  355. maxMsgSize = 4096;
  356. linkkit_gateway_setopt(initParams, LINKKIT_OPT_MAX_MSG_SIZE, &maxMsgSize,
  357. sizeof(int));
  358. /* LINKKIT_OPT_MAX_MSG_QUEUE_SIZE: max size of message queue */
  359. maxMsgQueueSize = 8;
  360. linkkit_gateway_setopt(initParams, LINKKIT_OPT_MAX_MSG_QUEUE_SIZE,
  361. &maxMsgQueueSize, sizeof(int));
  362. prop_post_reply = 0;
  363. linkkit_gateway_setopt(initParams, LINKKIT_OPT_PROPERTY_POST_REPLY,
  364. &prop_post_reply, sizeof(int));
  365. event_post_reply = 0;
  366. linkkit_gateway_setopt(initParams, LINKKIT_OPT_EVENT_POST_REPLY,
  367. &event_post_reply, sizeof(int));
  368. int stacksize = 1024 * 4;
  369. linkkit_gateway_setopt(initParams, LINKKIT_OPT_THREAD_STACK_SIZE,
  370. &stacksize, sizeof(int));
  371. /* set event handler */
  372. linkkit_gateway_set_event_callback(initParams, event_handler, &gateway);
  373. /* set init parameter into gateway */
  374. if (linkkit_gateway_init(initParams) < 0) {
  375. EXAMPLE_TRACE("linkkit_gateway_init failed\n");
  376. return -1;
  377. }
  378. /* start */
  379. gateway.lk_dev = linkkit_gateway_start(&linkkit_cbs, &gateway);
  380. if (gateway.lk_dev < 0) {
  381. EXAMPLE_TRACE("linkkit_gateway_start failed\n");
  382. return -1;
  383. }
  384. while (gateway.register_completed == 0) {
  385. HAL_SleepMs(1000);
  386. }
  387. while (1) {
  388. /*
  389. * gateway trigger event
  390. * please follow user's case, modify this logic
  391. */
  392. linkkit_gateway_trigger_event_json_sync(gateway.lk_dev, "Error",
  393. "{\"ErrorCode\": 0}", 10000);
  394. HAL_SleepMs(1000);
  395. }
  396. /* gateway stop */
  397. linkkit_gateway_stop(gateway.lk_dev);
  398. /* gateway exit */
  399. linkkit_gateway_exit();
  400. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  401. EXAMPLE_TRACE("out of sample!\n");
  402. return 0;
  403. }