solo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8. #include "infra_compat.h"
  9. #include "infra_defs.h"
  10. #include "linkkit_export.h"
  11. /*
  12. * please modify this string follow as product's TSL.
  13. */
  14. #include "data/solo_tsl.c"
  15. #define EVENT_ERROR_IDENTIFIER "Error"
  16. #define EVENT_ERROR_OUTPUT_INFO_IDENTIFIER "ErrorCode"
  17. /* for demo only */
  18. #define PRODUCT_KEY "a1X2bEnP82z"
  19. #define PRODUCT_SECRET "7jluWm1zql7bt8qK"
  20. #define DEVICE_NAME "test_06"
  21. #define DEVICE_SECRET "wQ1xOzFH3kLdjCTLfi8Xbw4otRz0lHoq"
  22. void HAL_Printf(const char *fmt, ...);
  23. #define EXAMPLE_TRACE(fmt, ...) \
  24. do { \
  25. HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
  26. HAL_Printf(fmt, ##__VA_ARGS__); \
  27. HAL_Printf("%s", "\r\n"); \
  28. } while (0)
  29. typedef struct _sample_context {
  30. const void *thing;
  31. int cloud_connected;
  32. int local_connected;
  33. int thing_enabled;
  34. } sample_context_t;
  35. void HAL_Free(void *ptr);
  36. uint64_t HAL_UptimeMs(void);
  37. int HAL_SetProductKey(char *product_key);
  38. int HAL_SetDeviceName(char *device_name);
  39. int HAL_SetProductSecret(char *product_secret);
  40. int HAL_SetDeviceSecret(char *device_secret);
  41. /*
  42. * the callback of linkkit_post_property.
  43. * response_id is compare with the result of linkkit_post_property.
  44. *
  45. */
  46. void post_property_cb(const void *thing_id, int response_id, int code,
  47. const char *response_message, void *ctx)
  48. {
  49. EXAMPLE_TRACE("thing@%p: response arrived:\nid:%d\tcode:%d\tmessage:%s\n",
  50. thing_id, response_id, code,
  51. response_message == NULL ? "NULL" : response_message);
  52. /* do user's post property callback process logical here. */
  53. /* ............................... */
  54. /* user's post property callback process logical complete */
  55. }
  56. /* connect handle
  57. * cloud and local
  58. */
  59. #ifdef LOCAL_CONN_ENABLE
  60. static int on_connect(void *ctx, int cloud)
  61. #else
  62. static int on_connect(void *ctx)
  63. #endif
  64. {
  65. sample_context_t *sample_ctx = ctx;
  66. #ifdef LOCAL_CONN_ENABLE
  67. if (cloud) {
  68. sample_ctx->cloud_connected = 1;
  69. } else {
  70. sample_ctx->local_connected = 1;
  71. }
  72. EXAMPLE_TRACE("%s is connected\n", cloud ? "cloud" : "local");
  73. #else
  74. sample_ctx->cloud_connected = 1;
  75. EXAMPLE_TRACE("%s is connected\n", "cloud");
  76. #endif
  77. /* do user's connect process logical here. */
  78. /* ............................... */
  79. /* user's connect process logical complete */
  80. return 0;
  81. }
  82. /* disconnect handle
  83. * cloud and local
  84. */
  85. #ifdef LOCAL_CONN_ENABLE
  86. static int on_disconnect(void *ctx, int cloud)
  87. #else
  88. static int on_disconnect(void *ctx)
  89. #endif
  90. {
  91. sample_context_t *sample_ctx = ctx;
  92. #ifdef LOCAL_CONN_ENABLE
  93. if (cloud) {
  94. sample_ctx->cloud_connected = 0;
  95. } else {
  96. sample_ctx->local_connected = 0;
  97. }
  98. EXAMPLE_TRACE("%s is disconnect\n", cloud ? "cloud" : "local");
  99. #else
  100. sample_ctx->cloud_connected = 0;
  101. EXAMPLE_TRACE("%s is disconnect\n", "cloud");
  102. #endif
  103. /* do user's disconnect process logical here. */
  104. /* ............................... */
  105. /* user's disconnect process logical complete */
  106. return 0;
  107. }
  108. /* TODO: */
  109. /*
  110. * receive raw data handler
  111. */
  112. static int raw_data_arrived(const void *thing_id, const void *data, int len,
  113. void *ctx)
  114. {
  115. char raw_data[128] = { 0 };
  116. EXAMPLE_TRACE("raw data arrived,len:%d\n", len);
  117. /* do user's raw data process logical here. */
  118. /* ............................... */
  119. /* user's raw data process logical complete */
  120. /* send result to cloud
  121. * please send your data via raw_data
  122. * example rule: just reply a string to check
  123. */
  124. snprintf(raw_data, sizeof(raw_data), "test down raw reply data %lld",
  125. (long long)HAL_UptimeMs());
  126. /* answer raw data handle result */
  127. linkkit_invoke_raw_service(thing_id, 0, raw_data, strlen(raw_data));
  128. return 0;
  129. }
  130. /* thing create succuss */
  131. static int thing_create(const void *thing_id, void *ctx)
  132. {
  133. sample_context_t *sample_ctx = ctx;
  134. EXAMPLE_TRACE("new thing@%p created.\n", thing_id);
  135. sample_ctx->thing = thing_id;
  136. /* do user's thing create process logical here. */
  137. /* ............................... */
  138. /* user's thing create process logical complete */
  139. return 0;
  140. }
  141. /* thing enable
  142. * thing is enabled, than it can be communicated
  143. */
  144. static int thing_enable(const void *thing_id, void *ctx)
  145. {
  146. sample_context_t *sample_ctx = ctx;
  147. sample_ctx->thing_enabled = 1;
  148. /* do user's thing enable process logical here. */
  149. /* ............................... */
  150. /* user's thing enable process logical complete */
  151. return 0;
  152. }
  153. /* thing disable
  154. * thing is disable, than it can not be communicated
  155. */
  156. static int thing_disable(const void *thing_id, void *ctx)
  157. {
  158. sample_context_t *sample_ctx = ctx;
  159. sample_ctx->thing_enabled = 0;
  160. /* do user's thing disable process logical here. */
  161. /* ............................... */
  162. /* user's thing disable process logical complete */
  163. return 0;
  164. }
  165. /*
  166. * this is the "custom" service handler
  167. * alink method: thing.service.Custom
  168. * please follow TSL modify the idendifier
  169. */
  170. #ifdef RRPC_ENABLED
  171. static int handle_service_custom(sample_context_t *_sample_ctx,
  172. const void *thing,
  173. const char *service_identifier, int request_id,
  174. int rrpc)
  175. #else
  176. static int handle_service_custom(sample_context_t *_sample_ctx,
  177. const void *thing,
  178. const char *service_identifier, int request_id)
  179. #endif /* RRPC_ENABLED */
  180. {
  181. char identifier[128] = { 0 };
  182. /*
  183. * please follow TSL modify the value type
  184. */
  185. int transparency_value;
  186. int contrastratio_value;
  187. /*
  188. * get iutput value.
  189. * compare the service identifier
  190. * please follow user's TSL modify the "transparency".
  191. */
  192. snprintf(identifier, sizeof(identifier), "%s.%s", service_identifier,
  193. "transparency");
  194. linkkit_get_value(linkkit_method_get_service_input_value, thing, identifier,
  195. &transparency_value, NULL);
  196. EXAMPLE_TRACE("identifier: %s value is %d.\n", identifier,
  197. transparency_value);
  198. /*
  199. * set output value according to user's process result.
  200. * example rule: Contrastratio will changed by transparency.
  201. */
  202. /* do user's service process logical here. */
  203. /* ............................... */
  204. /* user's service process logical complete */
  205. /*
  206. * please follow user's TSL modify the "transparency".
  207. */
  208. snprintf(identifier, sizeof(identifier), "%s.%s", service_identifier,
  209. "Contrastratio");
  210. contrastratio_value = transparency_value + 1;
  211. linkkit_set_value(linkkit_method_set_service_output_value, thing,
  212. identifier, &contrastratio_value, NULL);
  213. #ifdef RRPC_ENABLED
  214. linkkit_answer_service(thing, service_identifier, request_id, 200, rrpc);
  215. #else
  216. linkkit_answer_service(thing, service_identifier, request_id, 200);
  217. #endif /* RRPC_ENABLED */
  218. return 0;
  219. }
  220. /*
  221. * the handler of service which is defined by identifier, not property
  222. * alink method: thing.service.{tsl.service.identifier}
  223. */
  224. #ifdef RRPC_ENABLED
  225. static int thing_call_service(const void *thing_id, const char *service,
  226. int request_id, int rrpc, void *ctx)
  227. #else
  228. static int thing_call_service(const void *thing_id, const char *service,
  229. int request_id, void *ctx)
  230. #endif /* RRPC_ENABLED */
  231. {
  232. sample_context_t *sample_ctx = ctx;
  233. EXAMPLE_TRACE("service(%s) requested, id: thing@%p, request id:%d\n",
  234. service, thing_id, request_id);
  235. /* please follow TSL modify the idendifier --- Custom */
  236. if (strcmp(service, "Custom") == 0) {
  237. #ifdef RRPC_ENABLED
  238. handle_service_custom(sample_ctx, thing_id, service, request_id, rrpc);
  239. #else
  240. handle_service_custom(sample_ctx, thing_id, service, request_id);
  241. #endif /* RRPC_ENABLED */
  242. }
  243. return 0;
  244. }
  245. /*
  246. * the handler of property changed
  247. * alink method: thing.service.property.set
  248. */
  249. static int thing_prop_changed(const void *thing_id, const char *property,
  250. void *ctx)
  251. {
  252. char *value_str = NULL;
  253. char property_buf[64] = { 0 };
  254. int response_id = -1;
  255. /* do user's property changed process logical here. */
  256. /* ............................... */
  257. /* user's property changed process logical complete */
  258. /*
  259. * example:
  260. * property identifier:
  261. * IndoorTemperature
  262. * TemperatureModelStatus
  263. * CurrentTemperature
  264. *
  265. * please follow TSL modify this property identifier
  266. */
  267. /* if the proprety id is %s.%s, please follow this code */
  268. /* get new property value */
  269. if (strstr(property, "HSVColor") != 0) {
  270. double hue, saturation, value;
  271. /* generate property identifier HSVColor.Hue */
  272. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Hue");
  273. /* get value by linkkit_get_value */
  274. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  275. property_buf, &hue, &value_str);
  276. if (value_str) {
  277. HAL_Free(value_str);
  278. value_str = NULL;
  279. }
  280. /* generate property identifier HSVColor.Saturation */
  281. snprintf(property_buf, sizeof(property_buf), "%s.%s", property,
  282. "Saturation");
  283. /* get value by linkkit_get_value */
  284. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  285. property_buf, &saturation, &value_str);
  286. if (value_str) {
  287. HAL_Free(value_str);
  288. value_str = NULL;
  289. }
  290. /* generate property identifier HSVColor.Value */
  291. snprintf(property_buf, sizeof(property_buf), "%s.%s", property,
  292. "Value");
  293. /* get value by linkkit_get_value */
  294. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  295. property_buf, &value, &value_str);
  296. if (value_str) {
  297. HAL_Free(value_str);
  298. value_str = NULL;
  299. }
  300. EXAMPLE_TRACE("property(%s), Hue:%f, Saturation:%f, Value:%f\n",
  301. property, hue, saturation, value);
  302. } else if (strstr(property, "HSLColor") != 0) {
  303. double hue, saturation, lightness;
  304. /* generate property identifier HSLColor.Hue */
  305. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Hue");
  306. /* get value by linkkit_get_value */
  307. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  308. property_buf, &hue, &value_str);
  309. if (value_str) {
  310. HAL_Free(value_str);
  311. value_str = NULL;
  312. }
  313. /* generate property identifier HSLColor.Saturation */
  314. snprintf(property_buf, sizeof(property_buf), "%s.%s", property,
  315. "Saturation");
  316. /* get value by linkkit_get_value */
  317. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  318. property_buf, &saturation, &value_str);
  319. if (value_str) {
  320. HAL_Free(value_str);
  321. value_str = NULL;
  322. }
  323. /* generate property identifier HSLColor.Lightness */
  324. snprintf(property_buf, sizeof(property_buf), "%s.%s", property,
  325. "Lightness");
  326. /* get value by linkkit_get_value */
  327. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  328. property_buf, &lightness, &value_str);
  329. if (value_str) {
  330. HAL_Free(value_str);
  331. value_str = NULL;
  332. }
  333. EXAMPLE_TRACE("property(%s), Hue:%f, Saturation:%f, Lightness:%f\n",
  334. property, hue, saturation, lightness);
  335. } else if (strstr(property, "RGBColor") != 0) {
  336. int red, green, blue;
  337. /* generate property identifier RGBColor.Red */
  338. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Red");
  339. /* get value by linkkit_get_value */
  340. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  341. property_buf, &red, &value_str);
  342. if (value_str) {
  343. HAL_Free(value_str);
  344. value_str = NULL;
  345. }
  346. /* generate property identifier RGBColor.Green */
  347. snprintf(property_buf, sizeof(property_buf), "%s.%s", property,
  348. "Green");
  349. /* get value by linkkit_get_value */
  350. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  351. property_buf, &green, &value_str);
  352. if (value_str) {
  353. HAL_Free(value_str);
  354. value_str = NULL;
  355. }
  356. /* generate property identifier RGBColor.Blue */
  357. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Blue");
  358. /* get value by linkkit_get_value */
  359. linkkit_get_value(linkkit_method_get_property_value, thing_id,
  360. property_buf, &blue, &value_str);
  361. if (value_str) {
  362. HAL_Free(value_str);
  363. value_str = NULL;
  364. }
  365. EXAMPLE_TRACE("property(%s), Red:%d, Green:%d, Blue:%d\n", property,
  366. red, green, blue);
  367. }
  368. /* post property
  369. * result is response_id; if response_id = -1, it is fail, else it is
  370. * success. response_id by be compare in post_property_cb.
  371. */
  372. response_id = linkkit_post_property(thing_id, property, post_property_cb);
  373. EXAMPLE_TRACE("post property(%s) response id: %d\n", property, response_id);
  374. return 0;
  375. }
  376. /* there is some data transparent transmission by linkkit */
  377. static int linkit_data_arrived(const void *thing_id, const void *params,
  378. int len, void *ctx)
  379. {
  380. EXAMPLE_TRACE("thing@%p: masterdev_linkkit_data(%d byte): %s\n", thing_id,
  381. len, (const char *)params);
  382. /* do user's data arrived process logical here. */
  383. /* ............................... */
  384. /* user's data arrived process logical complete */
  385. return 0;
  386. }
  387. static int is_active(sample_context_t *sample_ctx)
  388. {
  389. #ifdef LOCAL_CONN_ENABLE
  390. return (sample_ctx->cloud_connected /* && sample_ctx->thing_enabled*/) ||
  391. (sample_ctx->local_connected /* && sample_ctx->thing_enabled*/);
  392. #else
  393. return sample_ctx->cloud_connected /* && sample_ctx->thing_enabled*/;
  394. #endif
  395. }
  396. #ifdef POST_WIFI_STATUS
  397. typedef struct {
  398. char *band;
  399. int channel;
  400. int rssi;
  401. int snr;
  402. char mac[6];
  403. int tx_rate;
  404. int rx_rate;
  405. } user_wireless_info_t;
  406. static user_wireless_info_t example_wireless_info = {
  407. .band = 0,
  408. .channel = 1,
  409. .rssi = -30,
  410. .snr = 30,
  411. .mac = {0x18, 0xFE, 0x34, 0x12, 0x34, 0x56},
  412. .tx_rate = 1,
  413. .rx_rate = 1,
  414. };
  415. static int get_wireless_info(user_wireless_info_t *wireless_info)
  416. {
  417. if (wireless_info) {
  418. memcpy(wireless_info, &example_wireless_info, sizeof(user_wireless_info_t));
  419. }
  420. return 0;
  421. }
  422. static int post_property_wifi_status_once(sample_context_t *sample_ctx)
  423. {
  424. int ret = -1;
  425. int i = 0;
  426. static int is_post = 0;
  427. char val_buf[32];
  428. uint8_t bssid[ETH_ALEN];
  429. user_wireless_info_t wireless_info;
  430. char *band = NULL;
  431. int channel = 0;
  432. int rssi = 0;
  433. int snr = 0;
  434. int tx_rate = 0;
  435. int rx_rate = 0;
  436. if (is_active(sample_ctx) && 0 == is_post) {
  437. get_wireless_info(&wireless_info);
  438. #ifdef WIFI_PROVISION_ENABLED
  439. HAL_Wifi_Get_Ap_Info(NULL, NULL, bssid);
  440. #endif
  441. band = wireless_info.band == 0 ? "2.4G" : "5G";
  442. channel = wireless_info.channel;
  443. rssi = wireless_info.rssi;
  444. snr = wireless_info.snr;
  445. tx_rate = wireless_info.tx_rate;
  446. rx_rate = wireless_info.rx_rate;
  447. linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
  448. "WIFI_Band", band, NULL);
  449. linkkit_post_property(sample_ctx->thing, "WIFI_Band", post_property_cb);
  450. linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
  451. "WIFI_Channel", &channel, NULL);
  452. linkkit_post_property(sample_ctx->thing, "WIFI_Channel",
  453. post_property_cb);
  454. linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
  455. "WiFI_RSSI", &rssi, NULL);
  456. linkkit_post_property(sample_ctx->thing, "WiFI_RSSI", post_property_cb);
  457. linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
  458. "WiFI_SNR", &snr, NULL);
  459. linkkit_post_property(sample_ctx->thing, "WiFI_SNR", post_property_cb);
  460. memset(val_buf, 0, sizeof(val_buf));
  461. for (i = 0; i < ETH_ALEN; i++) {
  462. snprintf(val_buf + strlen(val_buf),
  463. sizeof(val_buf) - strlen(val_buf), "%c:", bssid[i]);
  464. }
  465. if (strlen(val_buf) > 0 && val_buf[strlen(val_buf) - 1] == ':') {
  466. val_buf[strlen(val_buf) - 1] = '\0';
  467. }
  468. linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
  469. "WIFI_AP_BSSID", val_buf, NULL);
  470. linkkit_post_property(sample_ctx->thing, "WIFI_AP_BSSID",
  471. post_property_cb);
  472. linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
  473. "WIFI_Tx_Rate", &tx_rate, NULL);
  474. linkkit_post_property(sample_ctx->thing, "WIFI_Tx_Rate",
  475. post_property_cb);
  476. linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
  477. "WIFI_Rx_Rate", &rx_rate, NULL);
  478. linkkit_post_property(sample_ctx->thing, "WIFI_Rx_Rate",
  479. post_property_cb);
  480. is_post = 1;
  481. ret = 0;
  482. }
  483. return ret;
  484. }
  485. #endif
  486. static unsigned long long uptime_sec(void)
  487. {
  488. static unsigned long long start_time = 0;
  489. if (start_time == 0) {
  490. start_time = HAL_UptimeMs();
  491. }
  492. return (HAL_UptimeMs() - start_time) / 1000;
  493. }
  494. int post_all_prop(sample_context_t *sample)
  495. {
  496. /* demo for post all property */
  497. return linkkit_post_property(sample->thing, NULL, post_property_cb);
  498. }
  499. int trigger_event(sample_context_t *sample)
  500. {
  501. char event_output_identifier[64];
  502. int errorCode = 0;
  503. snprintf(event_output_identifier, sizeof(event_output_identifier), "%s.%s",
  504. EVENT_ERROR_IDENTIFIER, EVENT_ERROR_OUTPUT_INFO_IDENTIFIER);
  505. linkkit_set_value(linkkit_method_set_event_output_value, sample->thing,
  506. event_output_identifier, &errorCode, NULL);
  507. return linkkit_trigger_event(sample->thing, EVENT_ERROR_IDENTIFIER,
  508. post_property_cb);
  509. }
  510. #ifdef EXTENDED_INFO_ENABLED
  511. int trigger_deviceinfo(sample_context_t *sample)
  512. {
  513. /* please modify the parameter */
  514. return linkkit_trigger_extended_info_operate(
  515. sample->thing, "[{device_info : 21}]",
  516. linkkit_extended_info_operate_update);
  517. }
  518. #endif
  519. void ntp_time_reply(const char *offset_time)
  520. {
  521. EXAMPLE_TRACE("ntp time: %s\n", offset_time);
  522. }
  523. int linkkit_example()
  524. {
  525. sample_context_t sample_ctx = { 0 };
  526. int exit = 0;
  527. int cnt = 0;
  528. unsigned long long now = 0;
  529. unsigned long long prev_sec = 0;
  530. int get_tsl_from_cloud = 0; /* the param of whether it is get tsl from cloud */
  531. linkkit_ops_t linkkit_ops = {
  532. .on_connect = on_connect, /* connect handler */
  533. .on_disconnect = on_disconnect, /* disconnect handler */
  534. .raw_data_arrived = raw_data_arrived, /* receive raw data handler */
  535. .thing_create = thing_create, /* thing created handler */
  536. .thing_enable = thing_enable, /* thing enabled handler */
  537. .thing_disable = thing_disable, /* thing disabled handler */
  538. .thing_call_service = thing_call_service, /* self-defined service handler */
  539. .thing_prop_changed = thing_prop_changed, /* property set handler */
  540. .linkit_data_arrived = linkit_data_arrived, /* transparent transmission data handler */
  541. };
  542. EXAMPLE_TRACE("linkkit start");
  543. /*
  544. * linkkit start
  545. * max_buffered_msg = 16, set the handle msg max numbers.
  546. * if it is enough memory, this number can be set bigger.
  547. * if get_tsl_from_cloud = 0, it will use the default tsl [TSL_STRING]; if
  548. * get_tsl_from_cloud =1, it will get tsl from cloud.
  549. */
  550. if (-1 == linkkit_start(16, get_tsl_from_cloud, linkkit_loglevel_debug,
  551. &linkkit_ops, linkkit_cloud_domain_shanghai,
  552. &sample_ctx)) {
  553. EXAMPLE_TRACE("linkkit start fail");
  554. return -1;
  555. }
  556. if (!get_tsl_from_cloud) {
  557. /*
  558. * if get_tsl_from_cloud = 0, set default tsl [TSL_STRING]
  559. * please modify TSL_STRING by the TSL's defined.
  560. */
  561. linkkit_set_tsl(TSL_STRING, strlen(TSL_STRING));
  562. }
  563. EXAMPLE_TRACE("linkkit enter loop");
  564. while (!linkkit_is_try_leave()) {
  565. /*
  566. * if linkkit is support Multi-thread, the linkkit_dispatch and
  567. * linkkit_yield with callback by linkkit, else it need user to call
  568. * these function to received data.
  569. */
  570. #if (CONFIG_SDK_THREAD_COST == 0)
  571. linkkit_yield(100);
  572. if (++cnt % 10 == 0) {
  573. EXAMPLE_TRACE(".");
  574. cnt = 0;
  575. }
  576. linkkit_dispatch();
  577. #else
  578. HAL_SleepMs(100);
  579. #endif /* CONFIG_SDK_THREAD_COST */
  580. now = uptime_sec();
  581. if (prev_sec == now) {
  582. continue;
  583. }
  584. /*
  585. * do user's process logical here.
  586. * example rule:
  587. * about 10 seconds, assume trigger post wifi property event about
  588. * every 10s. about 30 seconds, assume trigger post property event about
  589. * every 30s.
  590. *
  591. * please follow user's rule to modify these code.
  592. */
  593. /* Manually Trigger Config OTA */
  594. /* if (now % 10 == 0) {
  595. linkkit_invoke_cota_get_config("product","file","",NULL);
  596. } */
  597. #ifdef POST_WIFI_STATUS
  598. if (now % 10 == 0) {
  599. linkkit_ntp_time_request(ntp_time_reply);
  600. post_property_wifi_status_once(&sample_ctx);
  601. }
  602. #endif
  603. if (now % 30 == 0 && is_active(&sample_ctx)) {
  604. linkkit_ntp_time_request(ntp_time_reply);
  605. post_all_prop(&sample_ctx);
  606. }
  607. if (now % 45 == 0 && is_active(&sample_ctx)) {
  608. linkkit_ntp_time_request(ntp_time_reply);
  609. trigger_event(&sample_ctx);
  610. }
  611. #ifdef EXTENDED_INFO_ENABLED
  612. if (now % 50 == 0 && is_active(&sample_ctx)) {
  613. linkkit_ntp_time_request(ntp_time_reply);
  614. trigger_deviceinfo(&sample_ctx);
  615. }
  616. #endif
  617. if (exit) {
  618. break;
  619. }
  620. /* after all, this is an sample, give a chance to return... */
  621. /* modify this value for this sample executaion time period */
  622. #if 0
  623. if (now > 60 * execution_time) {
  624. exit = 1;
  625. }
  626. #endif
  627. prev_sec = now;
  628. }
  629. /* linkkit end */
  630. linkkit_end();
  631. return 0;
  632. }
  633. void set_iotx_info()
  634. {
  635. HAL_SetProductKey(PRODUCT_KEY);
  636. HAL_SetProductSecret(PRODUCT_SECRET);
  637. HAL_SetDeviceName(DEVICE_NAME);
  638. HAL_SetDeviceSecret(DEVICE_SECRET);
  639. }
  640. int main(int argc, char *argv[])
  641. {
  642. #if !defined(WIFI_PROVISION_ENABLED) || !defined(BUILD_AOS)
  643. set_iotx_info();
  644. #endif
  645. EXAMPLE_TRACE("start!\n");
  646. /*
  647. * linkkit demo
  648. * please check document: https://help.aliyun.com/document_detail/73708.html
  649. * API introduce: https://help.aliyun.com/document_detail/68687.html
  650. */
  651. linkkit_example();
  652. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  653. EXAMPLE_TRACE("out of sample!\n");
  654. return 0;
  655. }