linkkit_example_solo.c 12 KB

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