1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include "aos/init.h"
6 #include "board.h"
7 #include <aos/errno.h>
8 #include <aos/kernel.h>
9 #include <k_api.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include "netmgr.h"
14 #include "linkkit/wifi_provision_api.h"
15 #include "linkkit/infra/infra_types.h"
16 #include "linkkit/infra/infra_defs.h"
17 #include "linkkit/infra/infra_compat.h"
18 #include "linkkit/dev_model_api.h"
19 #include "linkkit/infra/infra_config.h"
20 #include "linkkit/wrappers/wrappers.h"
21
22 #include "cJSON.h"
23
24 // for demo only
25 #define PRODUCT_KEY "a1FxISeKbq9"
26 #define PRODUCT_SECRET "ThNbP5iNUQ1lQe2Q"
27 #define DEVICE_NAME "alen-activate-test"
28 #define DEVICE_SECRET "jcumDL5AJRgU7zRNcCcnHRiQmtii0vDn"
29
30 #define EXAMPLE_TRACE(...) \
31 do { \
32 HAL_Printf("\033[1;32;40m%s.%d: ", __func__, __LINE__); \
33 HAL_Printf(__VA_ARGS__); \
34 HAL_Printf("\033[0m\r\n"); \
35 } while (0)
36
37 #define EXAMPLE_MASTER_DEVID (0)
38 #define EXAMPLE_YIELD_TIMEOUT_MS (200)
39
40 typedef struct {
41 int master_devid;
42 int cloud_connected;
43 int master_initialized;
44 } user_example_ctx_t;
45
46 /**
47 * These PRODUCT_KEY|PRODUCT_SECRET|DEVICE_NAME|DEVICE_SECRET are listed for demo only
48 *
49 * When you created your own devices on iot.console.com, you SHOULD replace them with what you got from console
50 *
51 */
52
53 static user_example_ctx_t g_user_example_ctx;
54
55
56 /** cloud connected event callback */
user_connected_event_handler(void)57 static int user_connected_event_handler(void)
58 {
59 EXAMPLE_TRACE("Cloud Connected");
60 g_user_example_ctx.cloud_connected = 1;
61
62 return 0;
63 }
64
65 /** cloud disconnected event callback */
user_disconnected_event_handler(void)66 static int user_disconnected_event_handler(void)
67 {
68 EXAMPLE_TRACE("Cloud Disconnected");
69 g_user_example_ctx.cloud_connected = 0;
70
71 return 0;
72 }
73
74 /* device initialized event callback */
user_initialized(const int devid)75 static int user_initialized(const int devid)
76 {
77 EXAMPLE_TRACE("Device Initialized");
78 g_user_example_ctx.master_initialized = 1;
79
80 return 0;
81 }
82
83 /** recv property post response message from cloud **/
user_report_reply_event_handler(const int devid,const int msgid,const int code,const char * reply,const int reply_len)84 static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
85 const int reply_len)
86 {
87 EXAMPLE_TRACE("Message Post Reply Received, Message ID: %d, Code: %d, Reply: %.*s", msgid, code,
88 reply_len,
89 (reply == NULL) ? ("NULL") : (reply));
90 return 0;
91 }
92
93 /** recv event post response message from cloud **/
user_trigger_event_reply_event_handler(const int devid,const int msgid,const int code,const char * eventid,const int eventid_len,const char * message,const int message_len)94 static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
95 const int eventid_len, const char *message, const int message_len)
96 {
97 EXAMPLE_TRACE("Trigger Event Reply Received, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s",
98 msgid, code,
99 eventid_len,
100 eventid, message_len, message);
101
102 return 0;
103 }
104
105 /** recv event post response message from cloud **/
user_property_set_event_handler(const int devid,const char * request,const int request_len)106 static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
107 {
108 int res = 0;
109 EXAMPLE_TRACE("Property Set Received, Request: %s", request);
110
111 res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
112 (unsigned char *)request, request_len);
113 EXAMPLE_TRACE("Post Property Message ID: %d", res);
114
115 return 0;
116 }
117
118
user_service_request_event_handler(const int devid,const char * serviceid,const int serviceid_len,const char * request,const int request_len,char ** response,int * response_len)119 static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
120 const char *request, const int request_len,
121 char **response, int *response_len)
122 {
123 int add_result = 0;
124 int ret = -1;
125 cJSON *root = NULL, *item_number_a = NULL, *item_number_b = NULL;
126 const char *response_fmt = "{\"Result\": %d}";
127 EXAMPLE_TRACE("Service Request Received, Service ID: %.*s, Payload: %s", serviceid_len, serviceid, request);
128
129 /* Parse Root */
130 root = cJSON_Parse(request);
131 if (root == NULL || !cJSON_IsObject(root)) {
132 EXAMPLE_TRACE("JSON Parse Error");
133 return -1;
134 }
135 do {
136 if (strlen("Operation_Service") == serviceid_len && memcmp("Operation_Service", serviceid, serviceid_len) == 0) {
137 /* Parse NumberA */
138 item_number_a = cJSON_GetObjectItem(root, "NumberA");
139 if (item_number_a == NULL || !cJSON_IsNumber(item_number_a)) {
140 break;
141 }
142 EXAMPLE_TRACE("NumberA = %d", item_number_a->valueint);
143
144 /* Parse NumberB */
145 item_number_b = cJSON_GetObjectItem(root, "NumberB");
146 if (item_number_b == NULL || !cJSON_IsNumber(item_number_b)) {
147 break;
148 }
149 EXAMPLE_TRACE("NumberB = %d", item_number_b->valueint);
150 add_result = item_number_a->valueint + item_number_b->valueint;
151 ret = 0;
152 /* Send Service Response To Cloud */
153 }
154 } while (0);
155
156 *response_len = strlen(response_fmt) + 10 + 1;
157 *response = (char *)HAL_Malloc(*response_len);
158 if (*response != NULL) {
159 memset(*response, 0, *response_len);
160 HAL_Snprintf(*response, *response_len, response_fmt, add_result);
161 *response_len = strlen(*response);
162 }
163
164 cJSON_Delete(root);
165 return ret;
166 }
167
user_timestamp_reply_event_handler(const char * timestamp)168 static int user_timestamp_reply_event_handler(const char *timestamp)
169 {
170 EXAMPLE_TRACE("Current Timestamp: %s", timestamp);
171
172 return 0;
173 }
174
175 /** fota event handler **/
user_fota_event_handler(int type,const char * version)176 static int user_fota_event_handler(int type, const char *version)
177 {
178 char buffer[128] = {0};
179 int buffer_length = 128;
180
181 /* 0 - new firmware exist, query the new firmware */
182 if (type == 0) {
183 EXAMPLE_TRACE("New Firmware Version: %s", version);
184
185 IOT_Linkkit_Query(EXAMPLE_MASTER_DEVID, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)buffer, buffer_length);
186 }
187
188 return 0;
189 }
190
191 /* cota event handler */
user_cota_event_handler(int type,const char * config_id,int config_size,const char * get_type,const char * sign,const char * sign_method,const char * url)192 static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
193 const char *sign, const char *sign_method, const char *url)
194 {
195 char buffer[128] = {0};
196 int buffer_length = 128;
197
198 /* type = 0, new config exist, query the new config */
199 if (type == 0) {
200 EXAMPLE_TRACE("New Config ID: %s", config_id);
201 EXAMPLE_TRACE("New Config Size: %d", config_size);
202 EXAMPLE_TRACE("New Config Type: %s", get_type);
203 EXAMPLE_TRACE("New Config Sign: %s", sign);
204 EXAMPLE_TRACE("New Config Sign Method: %s", sign_method);
205 EXAMPLE_TRACE("New Config URL: %s", url);
206
207 IOT_Linkkit_Query(EXAMPLE_MASTER_DEVID, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)buffer, buffer_length);
208 }
209
210 return 0;
211 }
212
user_post_property(void)213 void user_post_property(void)
214 {
215 static int cnt = 0;
216 int res = 0;
217
218 char property_payload[30] = {0};
219 HAL_Snprintf(property_payload, sizeof(property_payload), "{\"Counter\": %d}", cnt++);
220
221 res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
222 (unsigned char *)property_payload, strlen(property_payload));
223
224 EXAMPLE_TRACE("Post Property Message ID: %d", res);
225 }
226
user_post_event(void)227 void user_post_event(void)
228 {
229 int res = 0;
230 char *event_id = "HardwareError";
231 char *event_payload = "{\"ErrorCode\": 0}";
232
233 res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
234 event_payload, strlen(event_payload));
235 EXAMPLE_TRACE("Post Event Message ID: %d", res);
236 }
237
user_deviceinfo_update(void)238 void user_deviceinfo_update(void)
239 {
240 int res = 0;
241 char *device_info_update = "[{\"attrKey\":\"abc\",\"attrValue\":\"hello,world\"}]";
242
243 res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_DEVICEINFO_UPDATE,
244 (unsigned char *)device_info_update, strlen(device_info_update));
245 EXAMPLE_TRACE("Device Info Update Message ID: %d", res);
246 }
247
user_deviceinfo_delete(void)248 void user_deviceinfo_delete(void)
249 {
250 int res = 0;
251 char *device_info_delete = "[{\"attrKey\":\"abc\"}]";
252
253 res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_DEVICEINFO_DELETE,
254 (unsigned char *)device_info_delete, strlen(device_info_delete));
255 EXAMPLE_TRACE("Device Info Delete Message ID: %d", res);
256 }
257
user_cloud_error_handler(const int code,const char * data,const char * detail)258 static int user_cloud_error_handler(const int code, const char *data, const char *detail)
259 {
260 EXAMPLE_TRACE("code =%d ,data=%s, detail=%s", code, data, detail);
261 return 0;
262 }
263
set_iotx_info()264 void set_iotx_info()
265 {
266 char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = {0};
267 char _device_name[IOTX_DEVICE_NAME_LEN + 1] = {0};
268
269 HAL_GetProductKey(_product_key);
270 if (strlen(_product_key) == 0) {
271 HAL_SetProductKey(PRODUCT_KEY);
272 HAL_SetProductSecret(PRODUCT_SECRET);
273 }
274
275 HAL_GetDeviceName(_device_name);
276 if (strlen(_device_name) == 0) {
277 HAL_SetDeviceName(DEVICE_NAME);
278 HAL_SetDeviceSecret(DEVICE_SECRET);
279 }
280 }
281
linkkit_main(void)282 int linkkit_main(void)
283 {
284 int res = 0;
285 int cnt = 0;
286 int auto_quit = 0;
287 iotx_linkkit_dev_meta_info_t master_meta_info;
288 int domain_type = 0, dynamic_register = 0, post_reply_need = 0, fota_timeout = 30;
289
290 memset(&g_user_example_ctx, 0, sizeof(user_example_ctx_t));
291
292 memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
293 HAL_GetProductKey(master_meta_info.product_key);
294 HAL_GetDeviceName(master_meta_info.device_name);
295 HAL_GetProductSecret(master_meta_info.product_secret);
296 HAL_GetDeviceSecret(master_meta_info.device_secret);
297
298 IOT_SetLogLevel(IOT_LOG_DEBUG);
299
300 /* Register Callback */
301 IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
302 IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
303 IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
304 IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
305 IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
306 IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
307 IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
308 IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
309 IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
310 IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
311 IOT_RegisterCallback(ITE_CLOUD_ERROR, user_cloud_error_handler);
312
313
314 domain_type = IOTX_CLOUD_REGION_SHANGHAI;
315 IOT_Ioctl(IOTX_IOCTL_SET_DOMAIN, (void *)&domain_type);
316
317 /* Choose Login Method */
318 dynamic_register = 0;
319 IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);
320
321 /* post reply doesn't need */
322 post_reply_need = 1;
323 IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_reply_need);
324
325 IOT_Ioctl(IOTX_IOCTL_FOTA_TIMEOUT_MS, (void *)&fota_timeout);
326 #if defined(USE_ITLS)
327 {
328 char url[128] = {0};
329 int port = 1883;
330 snprintf(url, 128, "%s.itls.cn-shanghai.aliyuncs.com", master_meta_info.product_key);
331 IOT_Ioctl(IOTX_IOCTL_SET_MQTT_DOMAIN, (void *)url);
332 IOT_Ioctl(IOTX_IOCTL_SET_CUSTOMIZE_INFO, (void *)"authtype=id2");
333 IOT_Ioctl(IOTX_IOCTL_SET_MQTT_PORT, &port);
334 }
335 #endif
336 /* Create Master Device Resources */
337 do {
338 g_user_example_ctx.master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
339 if (g_user_example_ctx.master_devid >= 0) {
340 break;
341 }
342 EXAMPLE_TRACE("IOT_Linkkit_Open failed! retry after %d ms\n", 2000);
343 HAL_SleepMs(2000);
344 } while (1);
345 /* Start Connect Aliyun Server */
346 do {
347 res = IOT_Linkkit_Connect(g_user_example_ctx.master_devid);
348 if (res >= 0) {
349 break;
350 }
351 EXAMPLE_TRACE("IOT_Linkkit_Connect failed! retry after %d ms\n", 5000);
352 HAL_SleepMs(5000);
353 } while (1);
354
355 while (1) {
356 IOT_Linkkit_Yield(EXAMPLE_YIELD_TIMEOUT_MS);
357
358 /* Post Proprety Example */
359
360 if ((cnt % 20) == 0) {
361 user_post_property();
362 }
363
364 /* Post Event Example */
365 if ((cnt % 50) == 0) {
366 user_post_event();
367 }
368
369 cnt++;
370
371 if (auto_quit == 1 && cnt > 3600) {
372 break;
373 }
374 }
375
376 IOT_Linkkit_Close(g_user_example_ctx.master_devid);
377
378 IOT_DumpMemoryStats(IOT_LOG_DEBUG);
379
380 return 0;
381 }
382
application_start(int argc,char * argv[])383 int application_start(int argc, char *argv[])
384 {
385 int count = 0;
386
387 printf("nano entry here!\r\n");
388 awss_config_press();
389 awss_start();
390 linkkit_main();
391 while (1) {
392 printf("hello world! count %d \r\n", count++);
393 aos_msleep(10000);
394 };
395 }
396
397