linkkit_example_solo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. *
  4. * Again edit by rt-thread group
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2019-07-21 MurphyZhao first edit
  8. */
  9. #include "rtthread.h"
  10. #include "infra_config.h"
  11. extern void HAL_Printf(const char *fmt, ...);
  12. extern int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
  13. #ifdef DEPRECATED_LINKKIT
  14. #include "solo.c"
  15. #else
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "infra_types.h"
  20. #include "infra_defs.h"
  21. #include "infra_compat.h"
  22. #include "infra_compat.h"
  23. #ifdef INFRA_MEM_STATS
  24. #include "infra_mem_stats.h"
  25. #endif
  26. #include "dev_model_api.h"
  27. #include "dm_wrapper.h"
  28. #include "cJSON.h"
  29. #ifdef ATM_ENABLED
  30. #include "at_api.h"
  31. #endif
  32. #define EXAMPLE_TRACE(...) \
  33. do { \
  34. HAL_Printf("\033[1;32;40m%s.%d: ", __func__, __LINE__); \
  35. HAL_Printf(__VA_ARGS__); \
  36. HAL_Printf("\033[0m\r\n"); \
  37. } while (0)
  38. #define EXAMPLE_MASTER_DEVID (0)
  39. #define EXAMPLE_YIELD_TIMEOUT_MS (200)
  40. typedef struct {
  41. int master_devid;
  42. int cloud_connected;
  43. int master_initialized;
  44. } user_example_ctx_t;
  45. /**
  46. * These PRODUCT_KEY|PRODUCT_SECRET|DEVICE_NAME|DEVICE_SECRET are listed for demo only
  47. *
  48. * When you created your own devices on iot.console.com, you SHOULD replace them with what you got from console
  49. *
  50. */
  51. char PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
  52. char PRODUCT_SECRET[IOTX_PRODUCT_SECRET_LEN + 1] = {0};
  53. char DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
  54. char DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
  55. static user_example_ctx_t g_user_example_ctx;
  56. /** cloud connected event callback */
  57. static int user_connected_event_handler(void)
  58. {
  59. EXAMPLE_TRACE("Cloud Connected");
  60. g_user_example_ctx.cloud_connected = 1;
  61. return 0;
  62. }
  63. /** cloud disconnected event callback */
  64. static int user_disconnected_event_handler(void)
  65. {
  66. EXAMPLE_TRACE("Cloud Disconnected");
  67. g_user_example_ctx.cloud_connected = 0;
  68. return 0;
  69. }
  70. /* device initialized event callback */
  71. static int user_initialized(const int devid)
  72. {
  73. EXAMPLE_TRACE("Device Initialized");
  74. g_user_example_ctx.master_initialized = 1;
  75. return 0;
  76. }
  77. /** recv property post response message from cloud **/
  78. static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
  79. const int reply_len)
  80. {
  81. EXAMPLE_TRACE("Message Post Reply Received, Message ID: %d, Code: %d, Reply: %.*s", msgid, code,
  82. reply_len,
  83. (reply == NULL)? ("NULL") : (reply));
  84. return 0;
  85. }
  86. /** recv event post response message from cloud **/
  87. static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
  88. const int eventid_len, const char *message, const int message_len)
  89. {
  90. EXAMPLE_TRACE("Trigger Event Reply Received, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s",
  91. msgid, code,
  92. eventid_len,
  93. eventid, message_len, message);
  94. return 0;
  95. }
  96. /** recv event post response message from cloud **/
  97. static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
  98. {
  99. int res = 0;
  100. EXAMPLE_TRACE("Property Set Received, Request: %s", request);
  101. res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
  102. (unsigned char *)request, request_len);
  103. EXAMPLE_TRACE("Post Property Message ID: %d", res);
  104. return 0;
  105. }
  106. static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
  107. const char *request, const int request_len,
  108. char **response, int *response_len)
  109. {
  110. int add_result = 0;
  111. cJSON *root = NULL, *item_number_a = NULL, *item_number_b = NULL;
  112. const char *response_fmt = "{\"Result\": %d}";
  113. EXAMPLE_TRACE("Service Request Received, Service ID: %.*s, Payload: %s", serviceid_len, serviceid, request);
  114. /* Parse Root */
  115. root = cJSON_Parse(request);
  116. if (root == NULL) {
  117. EXAMPLE_TRACE("JSON Parse Error");
  118. return -1;
  119. }
  120. if (strlen("Operation_Service") == serviceid_len && memcmp("Operation_Service", serviceid, serviceid_len) == 0) {
  121. /* Parse NumberA */
  122. item_number_a = cJSON_GetObjectItem(root, "NumberA");
  123. if (item_number_a == NULL) {
  124. cJSON_Delete(root);
  125. return -1;
  126. }
  127. EXAMPLE_TRACE("NumberA = %d", item_number_a->valueint);
  128. /* Parse NumberB */
  129. item_number_b = cJSON_GetObjectItem(root, "NumberB");
  130. if (item_number_b == NULL) {
  131. cJSON_Delete(root);
  132. return -1;
  133. }
  134. EXAMPLE_TRACE("NumberB = %d", item_number_b->valueint);
  135. add_result = item_number_a->valueint + item_number_b->valueint;
  136. /* Send Service Response To Cloud */
  137. *response_len = strlen(response_fmt) + 10 + 1;
  138. *response = (char *)HAL_Malloc(*response_len);
  139. if (*response == NULL) {
  140. EXAMPLE_TRACE("Memory Not Enough");
  141. return -1;
  142. }
  143. memset(*response, 0, *response_len);
  144. HAL_Snprintf(*response, *response_len, response_fmt, add_result);
  145. *response_len = strlen(*response);
  146. }
  147. cJSON_Delete(root);
  148. return 0;
  149. }
  150. static int user_timestamp_reply_event_handler(const char *timestamp)
  151. {
  152. EXAMPLE_TRACE("Current Timestamp: %s", timestamp);
  153. return 0;
  154. }
  155. /** fota event handler **/
  156. static int user_fota_event_handler(int type, const char *version)
  157. {
  158. char buffer[128] = {0};
  159. int buffer_length = 128;
  160. /* 0 - new firmware exist, query the new firmware */
  161. if (type == 0) {
  162. EXAMPLE_TRACE("New Firmware Version: %s", version);
  163. IOT_Linkkit_Query(EXAMPLE_MASTER_DEVID, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)buffer, buffer_length);
  164. }
  165. return 0;
  166. }
  167. /* cota event handler */
  168. static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
  169. const char *sign, const char *sign_method, const char *url)
  170. {
  171. char buffer[128] = {0};
  172. int buffer_length = 128;
  173. /* type = 0, new config exist, query the new config */
  174. if (type == 0) {
  175. EXAMPLE_TRACE("New Config ID: %s", config_id);
  176. EXAMPLE_TRACE("New Config Size: %d", config_size);
  177. EXAMPLE_TRACE("New Config Type: %s", get_type);
  178. EXAMPLE_TRACE("New Config Sign: %s", sign);
  179. EXAMPLE_TRACE("New Config Sign Method: %s", sign_method);
  180. EXAMPLE_TRACE("New Config URL: %s", url);
  181. IOT_Linkkit_Query(EXAMPLE_MASTER_DEVID, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)buffer, buffer_length);
  182. }
  183. return 0;
  184. }
  185. void user_post_property(void)
  186. {
  187. static int cnt = 0;
  188. int res = 0;
  189. char property_payload[30] = {0};
  190. HAL_Snprintf(property_payload, sizeof(property_payload), "{\"Counter\": %d}", cnt++);
  191. res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
  192. (unsigned char *)property_payload, strlen(property_payload));
  193. EXAMPLE_TRACE("Post Property Message ID: %d", res);
  194. }
  195. void user_post_event(void)
  196. {
  197. int res = 0;
  198. char *event_id = "HardwareError";
  199. char *event_payload = "{\"ErrorCode\": 0}";
  200. res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
  201. event_payload, strlen(event_payload));
  202. EXAMPLE_TRACE("Post Event Message ID: %d", res);
  203. }
  204. void user_deviceinfo_update(void)
  205. {
  206. int res = 0;
  207. char *device_info_update = "[{\"attrKey\":\"abc\",\"attrValue\":\"hello,world\"}]";
  208. res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_DEVICEINFO_UPDATE,
  209. (unsigned char *)device_info_update, strlen(device_info_update));
  210. EXAMPLE_TRACE("Device Info Update Message ID: %d", res);
  211. }
  212. void user_deviceinfo_delete(void)
  213. {
  214. int res = 0;
  215. char *device_info_delete = "[{\"attrKey\":\"abc\"}]";
  216. res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_DEVICEINFO_DELETE,
  217. (unsigned char *)device_info_delete, strlen(device_info_delete));
  218. EXAMPLE_TRACE("Device Info Delete Message ID: %d", res);
  219. }
  220. int linkkit_solo_main(void)
  221. {
  222. int res = 0;
  223. int cnt = 0;
  224. iotx_linkkit_dev_meta_info_t master_meta_info;
  225. int domain_type = 0, dynamic_register = 0, post_reply_need = 0;
  226. #ifdef ATM_ENABLED
  227. if (IOT_ATM_Init() < 0) {
  228. EXAMPLE_TRACE("IOT ATM init failed!\n");
  229. return -1;
  230. }
  231. #endif
  232. memset(&g_user_example_ctx, 0, sizeof(user_example_ctx_t));
  233. HAL_GetProductKey(PRODUCT_KEY);
  234. HAL_GetProductSecret(PRODUCT_SECRET);
  235. HAL_GetDeviceName(DEVICE_NAME);
  236. HAL_GetDeviceSecret(DEVICE_SECRET);
  237. memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
  238. memcpy(master_meta_info.product_key, PRODUCT_KEY, strlen(PRODUCT_KEY));
  239. memcpy(master_meta_info.product_secret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));
  240. memcpy(master_meta_info.device_name, DEVICE_NAME, strlen(DEVICE_NAME));
  241. memcpy(master_meta_info.device_secret, DEVICE_SECRET, strlen(DEVICE_SECRET));
  242. IOT_SetLogLevel(IOT_LOG_DEBUG);
  243. /* Register Callback */
  244. IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
  245. IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
  246. IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
  247. IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
  248. IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
  249. IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
  250. IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
  251. IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
  252. IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
  253. IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
  254. domain_type = IOTX_CLOUD_REGION_SHANGHAI;
  255. IOT_Ioctl(IOTX_IOCTL_SET_DOMAIN, (void *)&domain_type);
  256. /* Choose Login Method */
  257. dynamic_register = 0;
  258. IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);
  259. /* post reply doesn't need */
  260. post_reply_need = 1;
  261. IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_reply_need);
  262. /* Create Master Device Resources */
  263. g_user_example_ctx.master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
  264. if (g_user_example_ctx.master_devid < 0) {
  265. EXAMPLE_TRACE("IOT_Linkkit_Open Failed\n");
  266. return -1;
  267. }
  268. rt_kprintf("== 1 ==\n");
  269. /* Start Connect Aliyun Server */
  270. res = IOT_Linkkit_Connect(g_user_example_ctx.master_devid);
  271. if (res < 0) {
  272. EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
  273. return -1;
  274. }
  275. rt_kprintf("== 2 ==\n");
  276. while (1) {
  277. IOT_Linkkit_Yield(EXAMPLE_YIELD_TIMEOUT_MS);
  278. /* Post Proprety Example */
  279. if ((cnt % 2) == 0) {
  280. user_post_property();
  281. }
  282. /* Post Event Example */
  283. if ((cnt % 10) == 0) {
  284. user_post_event();
  285. }
  286. if (++cnt > 3600) {
  287. break;
  288. }
  289. HAL_SleepMs(1000);
  290. }
  291. IOT_Linkkit_Close(g_user_example_ctx.master_devid);
  292. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  293. IOT_SetLogLevel(IOT_LOG_NONE);
  294. return 0;
  295. }
  296. MSH_CMD_EXPORT_ALIAS(linkkit_solo_main, ali_linkkit_solo_sample, ali linkkit sole sample);
  297. #endif