forked from alibaba/AliOS-Things
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_entry.c
310 lines (267 loc) · 8.68 KB
/
app_entry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <aos/aos.h>
#include <aos/yloop.h>
#include "netmgr.h"
#include "iot_export.h"
#include "app_entry.h"
#ifdef AOS_ATCMD
#include <atparser.h>
#endif
#ifdef CSP_LINUXHOST
#include <signal.h>
#endif
#include <k_api.h>
static char linkkit_started = 0;
static char awss_running = 0;
void set_iotx_info();
void do_awss_active();
#ifdef CONFIG_PRINT_HEAP
void print_heap()
{
extern k_mm_head *g_kmm_head;
int free = g_kmm_head->free_size;
LOG("============free heap size =%d==========", free);
}
#endif
static void wifi_service_event(input_event_t *event, void *priv_data)
{
if (event->type != EV_WIFI) {
return;
}
if (event->code != CODE_WIFI_ON_GOT_IP) {
return;
}
netmgr_ap_config_t config;
memset(&config, 0, sizeof(netmgr_ap_config_t));
netmgr_get_ap_config(&config);
LOG("wifi_service_event config.ssid %s", config.ssid);
if (strcmp(config.ssid, "adha") == 0 || strcmp(config.ssid, "aha") == 0) {
// clear_wifi_ssid();
return;
}
if (!linkkit_started) {
#ifdef CONFIG_PRINT_HEAP
print_heap();
#endif
#ifdef MQTT_DIRECT
aos_task_new("linkkit", (void (*)(void *))linkkit_main, NULL, 1024 * 6);
#else
aos_task_new("linkkit", (void (*)(void *))linkkit_main, NULL, 1024 * 8);
#endif
linkkit_started = 1;
}
}
static void cloud_service_event(input_event_t *event, void *priv_data)
{
if (event->type != EV_YUNIO) {
return;
}
LOG("cloud_service_event %d", event->code);
if (event->code == CODE_YUNIO_ON_CONNECTED) {
LOG("user sub and pub here");
return;
}
if (event->code == CODE_YUNIO_ON_DISCONNECTED) {
}
}
/*
* Note:
* the linkkit_event_monitor must not block and should run to complete fast
* if user wants to do complex operation with much time,
* user should post one task to do this, not implement complex operation in
* linkkit_event_monitor
*/
static void linkkit_event_monitor(int event)
{
switch (event) {
case IOTX_AWSS_START: // AWSS start without enbale, just supports device
// discover
// operate led to indicate user
LOG("IOTX_AWSS_START");
break;
case IOTX_AWSS_ENABLE: // AWSS enable
LOG("IOTX_AWSS_ENABLE");
// operate led to indicate user
break;
case IOTX_AWSS_LOCK_CHAN: // AWSS lock channel(Got AWSS sync packet)
LOG("IOTX_AWSS_LOCK_CHAN");
// operate led to indicate user
break;
case IOTX_AWSS_PASSWD_ERR: // AWSS decrypt passwd error
LOG("IOTX_AWSS_PASSWD_ERR");
// operate led to indicate user
break;
case IOTX_AWSS_GOT_SSID_PASSWD:
LOG("IOTX_AWSS_GOT_SSID_PASSWD");
// operate led to indicate user
break;
case IOTX_AWSS_CONNECT_ADHA: // AWSS try to connnect adha (device
// discover, router solution)
LOG("IOTX_AWSS_CONNECT_ADHA");
// operate led to indicate user
break;
case IOTX_AWSS_CONNECT_ADHA_FAIL: // AWSS fails to connect adha
LOG("IOTX_AWSS_CONNECT_ADHA_FAIL");
// operate led to indicate user
break;
case IOTX_AWSS_CONNECT_AHA: // AWSS try to connect aha (AP solution)
LOG("IOTX_AWSS_CONNECT_AHA");
// operate led to indicate user
break;
case IOTX_AWSS_CONNECT_AHA_FAIL: // AWSS fails to connect aha
LOG("IOTX_AWSS_CONNECT_AHA_FAIL");
// operate led to indicate user
break;
case IOTX_AWSS_SETUP_NOTIFY: // AWSS sends out device setup information
// (AP and router solution)
LOG("IOTX_AWSS_SETUP_NOTIFY");
// operate led to indicate user
break;
case IOTX_AWSS_CONNECT_ROUTER: // AWSS try to connect destination router
LOG("IOTX_AWSS_CONNECT_ROUTER");
// operate led to indicate user
break;
case IOTX_AWSS_CONNECT_ROUTER_FAIL: // AWSS fails to connect destination
// router.
LOG("IOTX_AWSS_CONNECT_ROUTER_FAIL");
// operate led to indicate user
break;
case IOTX_AWSS_GOT_IP: // AWSS connects destination successfully and got
// ip address
LOG("IOTX_AWSS_GOT_IP");
// operate led to indicate user
break;
case IOTX_AWSS_SUC_NOTIFY: // AWSS sends out success notify (AWSS
// sucess)
LOG("IOTX_AWSS_SUC_NOTIFY");
// operate led to indicate user
break;
case IOTX_AWSS_BIND_NOTIFY: // AWSS sends out bind notify information to
// support bind between user and device
LOG("IOTX_AWSS_BIND_NOTIFY");
// operate led to indicate user
break;
case IOTX_CONN_CLOUD: // Device try to connect cloud
LOG("IOTX_CONN_CLOUD");
// operate led to indicate user
break;
case IOTX_CONN_CLOUD_FAIL: // Device fails to connect cloud, refer to
// net_sockets.h for error code
LOG("IOTX_CONN_CLOUD_FAIL");
// operate led to indicate user
break;
case IOTX_CONN_CLOUD_SUC: // Device connects cloud successfully
LOG("IOTX_CONN_CLOUD_SUC");
// operate led to indicate user
break;
case IOTX_RESET: // Linkkit reset success (just got reset response from
// cloud without any other operation)
LOG("IOTX_RESET");
// operate led to indicate user
break;
default:
break;
}
}
static void start_netmgr(void *p)
{
iotx_event_regist_cb(linkkit_event_monitor);
netmgr_start(true);
aos_task_exit(0);
}
void do_awss_active()
{
LOG("do_awss_active %d\n", awss_running);
awss_running = 1;
#ifdef WIFI_AWSS_ENABLED
extern int awss_config_press();
awss_config_press();
#endif
}
static void linkkit_reset(void *p)
{
netmgr_clear_ap_config();
HAL_Sys_reboot();
}
extern int awss_report_reset();
static void do_awss_reset()
{
#ifdef WIFI_AWSS_ENABLED
aos_task_new("reset", (void (*)(void *))awss_report_reset, NULL, 2048);
#endif
aos_post_delayed_action(2000, linkkit_reset, NULL);
}
void linkkit_key_process(input_event_t *eventinfo, void *priv_data)
{
if (eventinfo->type != EV_KEY) {
return;
}
LOG("awss config press %d\n", eventinfo->value);
if (eventinfo->code == CODE_BOOT) {
if (eventinfo->value == VALUE_KEY_CLICK) {
do_awss_active();
} else if (eventinfo->value == VALUE_KEY_LTCLICK) {
do_awss_reset();
}
}
}
#ifdef CONFIG_AOS_CLI
static void handle_reset_cmd(char *pwbuf, int blen, int argc, char **argv)
{
aos_schedule_call(do_awss_reset, NULL);
}
static void handle_active_cmd(char *pwbuf, int blen, int argc, char **argv)
{
aos_schedule_call(do_awss_active, NULL);
}
static struct cli_command resetcmd = { .name = "reset",
.help = "factory reset",
.function = handle_reset_cmd };
static struct cli_command ncmd = { .name = "active_awss",
.help = "active_awss [start]",
.function = handle_active_cmd };
#endif
#ifdef CONFIG_PRINT_HEAP
static void duration_work(void *p)
{
print_heap();
aos_post_delayed_action(5000, duration_work, NULL);
}
#endif
int application_start(int argc, char **argv)
{
#ifdef CONFIG_PRINT_HEAP
print_heap();
aos_post_delayed_action(5000, duration_work, NULL);
#endif
#ifdef CSP_LINUXHOST
signal(SIGPIPE, SIG_IGN);
#endif
#if AOS_ATCMD
at.set_mode(ASYN);
at.init(AT_RECV_PREFIX, AT_RECV_SUCCESS_POSTFIX, AT_RECV_FAIL_POSTFIX,
AT_SEND_DELIMITER, 1000);
#endif
#ifdef WITH_SAL
sal_init();
#endif
aos_set_log_level(AOS_LL_DEBUG);
netmgr_init();
aos_register_event_filter(EV_KEY, linkkit_key_process, NULL);
aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
aos_register_event_filter(EV_YUNIO, cloud_service_event, NULL);
#ifdef CONFIG_AOS_CLI
aos_cli_register_command(&resetcmd);
aos_cli_register_command(&ncmd);
#endif
set_iotx_info();
aos_task_new("netmgr", start_netmgr, NULL, 4096);
aos_loop_run();
return 0;
}