1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4 #include <fcntl.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #if(CONFIG_AOS_LWIP > 0)
11 #include <sys/socket.h>
12 #endif
13 #include "aos/init.h"
14 #if(AOS_COMP_CLI > 0)
15 #include "aos/cli.h"
16 #endif
17 #include "ulog/ulog.h"
18 // #include "uagent.h"
19 #include "aos/kernel.h"
20 #if(AOS_COMP_WIFI > 0)
21 #include "aos/hal/wifi.h"
22 #endif
23
24 #ifdef AOS_COMP_PWRMGMT
25 #include <pwrmgmt_api.h>
26 #endif
27
28 #ifdef AOS_COMP_LITTLEFS
29 #include "littlefs.h"
30 #endif
31
32 #ifdef AOS_COMP_FATFS
33 #include "fatfs.h"
34 #endif
35
36 #if AOS_COMP_DEBUG
37 #include "aos/debug.h"
38 #endif
39
40 #ifdef AOS_COMP_UND
41 #include "und/und.h"
42 #endif
43
44 #ifdef IPERF_ENABLED
45 extern int iperf_cli_register(void);
46 #endif
47
48 #ifdef PING_ENABLED
49 extern int ping_cli_register(void);
50 #endif
51
52 extern int vfs_init(void);
53 #ifdef AOS_LOOP
54 #include "aos/yloop.h"
55 extern aos_loop_t aos_loop_init(void);
56 #endif
57 extern int32_t kv_init(void);
58 extern void ota_service_init(void);
59 extern void dumpsys_cli_init(void);
60
61 #ifdef WITH_SAL
62 extern int sal_device_init(void);
63 #endif
64
65 #ifdef WITH_MAL
66 extern int mal_device_init(void);
67 #endif
68
69 #if defined(CONFIG_DRV_VFS) && (CONFIG_DRV_VFS == 1)
70 int u_driver_entry(char* string);
71 int u_post_driver_entry(char* string);
72 #endif
73
74 #ifdef AOS_BINS
75
76 extern void *kmbins_tbl[];
77 extern char app_info_addr;
78 extern k_mm_head *g_kmm_head;
79 struct m_app_info_t *app_info = (struct m_app_info_t *) &app_info_addr;
80
app_pre_init(void)81 static void app_pre_init(void)
82 {
83 memcpy((void *)(app_info->data_ram_start), (void *)(app_info->data_flash_begin),
84 app_info->data_ram_end - app_info->data_ram_start);
85
86 memset((void *)(app_info->bss_start), 0, app_info->bss_end - app_info->bss_start);
87
88 krhino_add_mm_region(g_kmm_head, (void *)(app_info->heap_start),
89 app_info->heap_end - app_info->heap_start);
90 }
91 #endif
92
93 #if (AOS_COMP_CLI > 0)
94 #ifndef CONFIG_NO_LWIP
95 #if (CONFIG_AOS_LWIP > 0)
udp_cmd(char * buf,int len,int argc,char ** argv)96 static void udp_cmd(char *buf, int len, int argc, char **argv)
97 {
98 struct sockaddr_in saddr;
99
100 if (argc < 4) {
101 return;
102 }
103
104 memset(&saddr, 0, sizeof(saddr));
105 saddr.sin_family = AF_INET;
106 saddr.sin_port = htons(atoi(argv[2]));
107 saddr.sin_addr.s_addr = inet_addr(argv[1]);
108
109 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
110 if (sockfd < 0) {
111 aos_cli_printf("error creating socket!\n");
112 return;
113 }
114
115 int ret = sendto(sockfd, argv[3], strlen(argv[3]), 0,
116 (struct sockaddr *)&saddr, sizeof(saddr));
117 if (ret < 0) {
118 aos_cli_printf("error send data %d!\n", ret);
119 }
120
121 close(sockfd);
122 }
123
124 struct cli_command tcpip_cli_cmd[] = {
125 /* net */
126 {"udp", "[ip] [port] [string data] send udp data", udp_cmd},
127 };
128
tcpip_cli_init(void)129 static void tcpip_cli_init(void)
130 {
131 aos_cli_register_commands(&tcpip_cli_cmd[0],sizeof(tcpip_cli_cmd) / sizeof(struct cli_command));
132 }
133
134 #ifdef AOS_NET_WITH_WIFI
hex(char c)135 static uint8_t hex(char c)
136 {
137 if (c >= '0' && c <= '9')
138 return c - '0';
139 if (c >= 'a' && c <= 'z')
140 return c - 'a' + 10;
141 if (c >= 'A' && c <= 'Z')
142 return c - 'A' + 10;
143 return 0;
144 }
145
hexstr2bin(const char * macstr,uint8_t * mac,int len)146 static void hexstr2bin(const char *macstr, uint8_t *mac, int len)
147 {
148 int i;
149 for (i=0;i < len && macstr[2 * i];i++) {
150 mac[i] = hex(macstr[2 * i]) << 4;
151 mac[i] |= hex(macstr[2 * i + 1]);
152 }
153 }
154
wifi_debug_cmd(char * buf,int len,int argc,char ** argv)155 static void wifi_debug_cmd(char *buf, int len, int argc, char **argv)
156 {
157 hal_wifi_start_debug_mode(NULL);
158 }
159
mac_cmd(char * buf,int len,int argc,char ** argv)160 static void mac_cmd(char *buf, int len, int argc, char **argv)
161 {
162 uint8_t mac[6];
163
164 if (argc == 1)
165 {
166 hal_wifi_get_mac_addr(NULL, mac);
167 aos_cli_printf("MAC address: %02x-%02x-%02x-%02x-%02x-%02x\r\n",
168 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
169 }
170 else if(argc == 2)
171 {
172 hexstr2bin(argv[1], mac, 6);
173 hal_wifi_set_mac_addr(NULL, mac);
174 aos_cli_printf("Set MAC address: %02x-%02x-%02x-%02x-%02x-%02x\r\n",
175 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
176 }
177 else
178 {
179 aos_cli_printf("invalid cmd\r\n");
180 }
181 }
182
183
184 static struct cli_command wifi_cli_cmd[] = {
185 { "wifi_debug", "wifi debug mode", wifi_debug_cmd },
186 { "mac", "get/set mac", mac_cmd },
187 };
188
hal_wifi_cli_init(void)189 static void hal_wifi_cli_init(void)
190 {
191 aos_cli_register_commands(&wifi_cli_cmd[0],sizeof(wifi_cli_cmd) / sizeof(struct cli_command));
192 }
193 #endif /* AOS_NET_WITH_WIFI */
194 #endif /* CONFIG_AOS_LWIP */
195 #endif /*!defined CONFIG_NO_LWIP */
196
cli_service_init(kinit_t * kinit)197 void cli_service_init(kinit_t *kinit)
198 {
199 if (kinit->cli_enable)
200 {
201 aos_cli_init();
202 /*kernel basic cmds reg*/
203 #ifndef CONFIG_NO_LWIP
204 #if(CONFIG_AOS_LWIP > 0)
205 tcpip_cli_init();
206 #ifdef AOS_NET_WITH_WIFI
207 hal_wifi_cli_init();
208 #endif
209 #ifdef IPERF_ENABLED
210 iperf_cli_register();
211 #endif
212
213 #ifdef PING_ENABLED
214 ping_cli_register();
215 #endif
216
217 #endif
218 #endif
219
220 }
221 return;
222 }
223
224 #endif
225
aos_show_welcome(void)226 void aos_show_welcome(void)
227 {
228 puts(" Welcome to AliOS Things ");
229 #ifdef CONFIG_AOS_INIT_WELCOME
230 puts(" �����������[ �����[ �����[ �������������[ ���������������[ ");
231 puts(" �����X�T�T�����[�����U �����U�����X�T�T�T�����[�����X�T�T�T�T�a ");
232 puts(" ���������������U�����U �����U�����U �����U���������������[ ");
233 puts(" �����X�T�T�����U�����U �����U�����U �����U�^�T�T�T�T�����U ");
234 puts(" �����U �����U���������������[�����U�^�������������X�a���������������U ");
235 puts(" �^�T�a �^�T�a�^�T�T�T�T�T�T�a�^�T�a �^�T�T�T�T�T�a �^�T�T�T�T�T�T�a ");
236 puts("�����������������[�����[ �����[�����[�������[ �����[ �������������[ ���������������[");
237 puts("�^�T�T�����X�T�T�a�����U �����U�����U���������[ �����U�����X�T�T�T�T�a �����X�T�T�T�T�a");
238 puts(" �����U ���������������U�����U�����X�����[ �����U�����U �������[���������������[");
239 puts(" �����U �����X�T�T�����U�����U�����U�^�����[�����U�����U �����U�^�T�T�T�T�����U");
240 puts(" �����U �����U �����U�����U�����U �^���������U�^�������������X�a���������������U");
241 puts(" �^�T�a �^�T�a �^�T�a�^�T�a�^�T�a �^�T�T�T�a �^�T�T�T�T�T�a �^�T�T�T�T�T�T�a");
242 #endif
243 }
244
aos_components_init(kinit_t * kinit)245 int aos_components_init(kinit_t *kinit)
246 {
247 #ifdef AOS_COMP_VFS
248 vfs_init();
249 #endif
250
251 #if defined(CONFIG_DRV_VFS) && (CONFIG_DRV_VFS == 1)
252 u_driver_entry("aos_components_init");
253 #endif
254
255 #ifdef AOS_COMP_UAGENT
256 uagent_init();
257 #endif
258
259 #if AOS_COMP_CLI
260 cli_service_init(kinit);
261 #endif
262
263 #if defined(CONFIG_DRV_VFS) && (CONFIG_DRV_VFS == 1)
264 u_post_driver_entry("aos_components_init");
265 #endif
266
267 #ifdef AOS_COMP_TRACE
268 TRACE_INIT();
269 #endif
270
271 #ifdef AOS_COMP_NFTL
272 nftl_init();
273 #endif
274
275 #ifdef AOS_COMP_FATFS
276 fatfs_register();
277 #endif
278
279 #ifdef AOS_COMP_LITTLEFS
280 littlefs_register();
281 #endif
282
283 #ifdef AOS_COMP_ULOG
284 ulog_init();
285 #endif
286
287 #ifdef AOS_COMP_KV
288 kv_init();
289 #endif
290
291 #ifdef WITH_SAL
292 sal_device_init();
293 #endif
294
295 #ifdef WITH_MAL
296 mal_device_init();
297 #endif
298
299 #ifdef AOS_LOOP
300 aos_loop_init();
301 #endif
302
303 #ifdef AOS_UOTA
304 ota_service_init();
305 #endif
306
307 #ifdef AOS_COMP_SENSOR
308 sensor_init();
309 #endif
310
311 #ifdef AOS_COMP_PWRMGMT
312 pwrmgmt_init();
313 #endif
314
315 #if AOS_COMP_DEBUG
316 aos_debug_init();
317 #endif
318
319 #ifdef AOS_COMP_UND
320 und_init();
321 #endif
322
323 aos_show_welcome();
324
325 #ifdef AOS_BINS
326 app_pre_init();
327
328 if (app_info->app_entry) {
329 app_info->app_entry((void *)kmbins_tbl, 0, NULL);
330 }
331 #endif
332
333 return 0;
334 }
335
336