1 | /*
|
---|
2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
3 | *
|
---|
4 | * Copyright (C) 2014-2017 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
7 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
8 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
9 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
10 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
11 | * スコード中に含まれていること.
|
---|
12 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
13 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
14 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
15 | * の無保証規定を掲載すること.
|
---|
16 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
17 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
18 | * と.
|
---|
19 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
20 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
21 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
22 | * 報告すること.
|
---|
23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
25 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
26 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
27 | * 免責すること.
|
---|
28 | *
|
---|
29 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
30 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
31 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
32 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
33 | * の責任を負わない.
|
---|
34 | *
|
---|
35 | * @(#) $Id$
|
---|
36 | */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * サンプルプログラム(1)の本体
|
---|
40 | */
|
---|
41 |
|
---|
42 | #include "shellif.h"
|
---|
43 | #include <kernel.h>
|
---|
44 | #include <t_stdlib.h>
|
---|
45 | #include <sil.h>
|
---|
46 | #include <setjmp.h>
|
---|
47 | #include <string.h>
|
---|
48 | #include "syssvc/serial.h"
|
---|
49 | #include "syssvc/syslog.h"
|
---|
50 | #include "target_syssvc.h"
|
---|
51 | #include "kernel_cfg.h"
|
---|
52 | #include "ffarch.h"
|
---|
53 | #include "ff.h"
|
---|
54 | #include "core/ntshell.h"
|
---|
55 | #include "core/ntlibc.h"
|
---|
56 | #include "util/ntstdio.h"
|
---|
57 | #include "usrcmd.h"
|
---|
58 | #include "util/ntopt.h"
|
---|
59 | #include "ntshell_main.h"
|
---|
60 | #include "socket_stub.h"
|
---|
61 | #include "usb_hbth.h"
|
---|
62 |
|
---|
63 | extern int bt_bnep_mode;
|
---|
64 |
|
---|
65 | char command[NTOPT_TEXT_MAXLEN];
|
---|
66 | ntstdio_t ntstdio;
|
---|
67 |
|
---|
68 | extern uint8_t mac_addr[6];
|
---|
69 | const struct utsname host_name = {
|
---|
70 | "TOPPERS/ASP3",
|
---|
71 | TARGET_NAME,
|
---|
72 | "3.2.0",
|
---|
73 | "3.2.0",
|
---|
74 | TARGET_NAME,
|
---|
75 | "toppers.jp"
|
---|
76 | };
|
---|
77 |
|
---|
78 | int shell_uname(struct utsname *uts)
|
---|
79 | {
|
---|
80 | memcpy(uts, &host_name, sizeof(host_name));
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static int usrcmd_ntopt_callback(long *args, void *extobj);
|
---|
85 |
|
---|
86 | int ntshell_exit_code;
|
---|
87 | volatile int ntshell_state;
|
---|
88 | jmp_buf process_exit;
|
---|
89 | void sys_init(void);
|
---|
90 | NTSHELL_SERIAL_READ ntshell_serial_read = 0;
|
---|
91 | NTSHELL_SERIAL_WRITE ntshell_serial_write = 0;
|
---|
92 | void *ntshell_serial_extobj;
|
---|
93 |
|
---|
94 | unsigned char ntstdio_xi(struct ntstdio_t *handle)
|
---|
95 | {
|
---|
96 | unsigned char buf[1];
|
---|
97 | ntshell_serial_read((char *)buf, 1, ntshell_serial_extobj);
|
---|
98 | return buf[0];
|
---|
99 | }
|
---|
100 |
|
---|
101 | void ntstdio_xo(struct ntstdio_t *handle, unsigned char c)
|
---|
102 | {
|
---|
103 | char buf[1];
|
---|
104 | buf[0] = c;
|
---|
105 | ntshell_serial_write(buf, 1, ntshell_serial_extobj);
|
---|
106 | }
|
---|
107 |
|
---|
108 | void ntshell_task_init(NTSHELL_SERIAL_READ func_read,
|
---|
109 | NTSHELL_SERIAL_WRITE func_write, void *extobj)
|
---|
110 | {
|
---|
111 | ntshell_serial_read = func_read;
|
---|
112 | ntshell_serial_write = func_write;
|
---|
113 | ntshell_serial_extobj = extobj;
|
---|
114 |
|
---|
115 | sys_init();
|
---|
116 |
|
---|
117 | ntstdio_init(&ntstdio, NTSTDIO_OPTION_LINE_ECHO | NTSTDIO_OPTION_CANON | NTSTDIO_OPTION_LF_CRLF | NTSTDIO_OPTION_LF_CR, ntstdio_xi, ntstdio_xo);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * ntshellタスク
|
---|
122 | */
|
---|
123 | void ntshell_task(intptr_t exinf)
|
---|
124 | {
|
---|
125 | ntshell_state = 1;
|
---|
126 | ntshell_exit_code = ntopt_parse(command, usrcmd_ntopt_callback, NULL);
|
---|
127 | ntshell_state = 2;
|
---|
128 | }
|
---|
129 |
|
---|
130 | void ntshell_change_netif_link(uint8_t link_up, uint8_t up)
|
---|
131 | {
|
---|
132 | FLGPTN flgptn;
|
---|
133 | T_RTSK rtsk;
|
---|
134 | ER ret;
|
---|
135 |
|
---|
136 | ret = ref_tsk(NTSHELL_TASK, &rtsk);
|
---|
137 | if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
|
---|
138 | return;
|
---|
139 |
|
---|
140 | FD_SET(0, (fd_set *)&flgptn);
|
---|
141 |
|
---|
142 | set_flg(FLG_SELECT_WAIT, flgptn);
|
---|
143 | }
|
---|
144 |
|
---|
145 | static int usrcmd_ntopt_callback(long *args, void *extobj)
|
---|
146 | {
|
---|
147 | const cmd_table_t *p = cmd_table_info.table;
|
---|
148 | int result = 0;
|
---|
149 | int found = 0;
|
---|
150 |
|
---|
151 | if (*args == 0)
|
---|
152 | return result;
|
---|
153 |
|
---|
154 | if (strcmp((const char *)args[1], "help") == 0) {
|
---|
155 | found = 1;
|
---|
156 | result = usrcmd_help(args[0], (char **)&args[1]);
|
---|
157 | }
|
---|
158 | else for (int i = 0; i < cmd_table_info.count; i++) {
|
---|
159 | if (strcmp((const char *)args[1], p->cmd) == 0) {
|
---|
160 | found = 1;
|
---|
161 | result = p->func(args[0], (char **)&args[1]);
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | p++;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if ((found == 0) && (((const char *)args[1])[0] != '\0'))
|
---|
168 | printf("Unknown command found.\n");
|
---|
169 |
|
---|
170 | clean_fd();
|
---|
171 |
|
---|
172 | return result;
|
---|
173 | }
|
---|
174 |
|
---|
175 | int usrcmd_help(int argc, char **argv)
|
---|
176 | {
|
---|
177 | const cmd_table_t *p = cmd_table_info.table;
|
---|
178 | for (int i = 0; i < cmd_table_info.count; i++) {
|
---|
179 | ntstdio_puts(&ntstdio, p->cmd);
|
---|
180 | ntstdio_puts(&ntstdio, "\t:");
|
---|
181 | ntstdio_puts(&ntstdio, p->desc);
|
---|
182 | ntstdio_puts(&ntstdio, "\n");
|
---|
183 | p++;
|
---|
184 | }
|
---|
185 | return 0;
|
---|
186 | }
|
---|
187 |
|
---|
188 | void shell_abort()
|
---|
189 | {
|
---|
190 | ntshell_exit_code = -1;
|
---|
191 | longjmp(process_exit, 1);
|
---|
192 | }
|
---|
193 |
|
---|
194 | void shell_exit(int exitcd)
|
---|
195 | {
|
---|
196 | ntshell_exit_code = exitcd;
|
---|
197 | longjmp(process_exit, 1);
|
---|
198 | }
|
---|
199 |
|
---|
200 | void shell_exit_group(int exitcd)
|
---|
201 | {
|
---|
202 | ntshell_exit_code = exitcd;
|
---|
203 | longjmp(process_exit, 1);
|
---|
204 | }
|
---|
205 |
|
---|
206 | int execute_command(int wait)
|
---|
207 | {
|
---|
208 | ER ret;
|
---|
209 |
|
---|
210 | ret = ter_tsk(NTSHELL_TASK);
|
---|
211 | if ((ret != E_OK) && (ret != E_OBJ)) {
|
---|
212 | syslog(LOG_ERROR, "ter_tsk => %d", ret);
|
---|
213 | }
|
---|
214 |
|
---|
215 | tslp_tsk(100000);
|
---|
216 |
|
---|
217 | clean_fd();
|
---|
218 |
|
---|
219 | ntshell_state = 0;
|
---|
220 | ret = act_tsk(NTSHELL_TASK);
|
---|
221 | if (ret != E_OK) {
|
---|
222 | syslog(LOG_ERROR, "act_tsk => %d", ret);
|
---|
223 | }
|
---|
224 |
|
---|
225 | if (wait == 0)
|
---|
226 | return 0;
|
---|
227 |
|
---|
228 | do {
|
---|
229 | tslp_tsk(100000);
|
---|
230 | } while(ntshell_state == 1);
|
---|
231 |
|
---|
232 | return ntshell_exit_code;
|
---|
233 | }
|
---|
234 |
|
---|
235 | int cmd_execute(const char *text, void *extobj)
|
---|
236 | {
|
---|
237 | ntlibc_strlcpy(command, text, sizeof(command));
|
---|
238 | return execute_command(1);
|
---|
239 | }
|
---|
240 |
|
---|
241 | int stdio_close(struct _IO_FILE *fp)
|
---|
242 | {
|
---|
243 | return -EPERM;
|
---|
244 | }
|
---|
245 |
|
---|
246 | size_t stdio_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
247 | {
|
---|
248 | return -EPERM;
|
---|
249 | }
|
---|
250 |
|
---|
251 | size_t stdio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
252 | {
|
---|
253 | return -EPERM;
|
---|
254 | }
|
---|
255 |
|
---|
256 | size_t stdin_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
257 | {
|
---|
258 | int i = 0;
|
---|
259 | while (i < len) {
|
---|
260 | int c = ntstdio_getc(&ntstdio);
|
---|
261 | data[i++] = c;
|
---|
262 | if ((c == EOF) || (c == '\n'))
|
---|
263 | break;
|
---|
264 | }
|
---|
265 | return i;
|
---|
266 | }
|
---|
267 |
|
---|
268 | size_t stdout_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
269 | {
|
---|
270 | for (int i = 0; i < len; i++) {
|
---|
271 | ntstdio_putc(&ntstdio, data[i]);
|
---|
272 | }
|
---|
273 | return len;
|
---|
274 | }
|
---|
275 |
|
---|
276 | size_t stderr_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
277 | {
|
---|
278 | for (int i = 0; i < len; i++) {
|
---|
279 | ntstdio_putc(&ntstdio, data[i]);
|
---|
280 | }
|
---|
281 | return len;
|
---|
282 | }
|
---|
283 |
|
---|
284 | int sio_close(struct _IO_FILE *fp)
|
---|
285 | {
|
---|
286 | return -EPERM;
|
---|
287 | }
|
---|
288 |
|
---|
289 | size_t sio_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
290 | {
|
---|
291 | return -EPERM;
|
---|
292 | }
|
---|
293 |
|
---|
294 | size_t sio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
295 | {
|
---|
296 | return -EPERM;
|
---|
297 | }
|
---|
298 |
|
---|
299 | off_t sio_seek(struct _IO_FILE *fp, off_t ofs, int org)
|
---|
300 | {
|
---|
301 | return -EPERM;
|
---|
302 | }
|
---|
303 |
|
---|
304 | int sio_ioctl(struct _IO_FILE *fp, int request, void *arg)
|
---|
305 | {
|
---|
306 | switch (request) {
|
---|
307 | case TIOCGWINSZ:
|
---|
308 | return 0;
|
---|
309 | case TCGETS:
|
---|
310 | return sio_tcgetattr(fp->fd, (struct termios *)arg);
|
---|
311 | case TCSETS + TCSANOW:
|
---|
312 | case TCSETS + TCSADRAIN:
|
---|
313 | case TCSETS + TCSAFLUSH:
|
---|
314 | return sio_tcsetattr(fp->fd, request - TCSETS, (const struct termios *)arg);
|
---|
315 | }
|
---|
316 |
|
---|
317 | return -EINVAL;
|
---|
318 | }
|
---|
319 |
|
---|
320 | int shell_clock_getres(clockid_t clk_id, struct timespec *res)
|
---|
321 | {
|
---|
322 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
323 | return -EINVAL;
|
---|
324 |
|
---|
325 | memset(&res->tv_sec, 0xFF, sizeof(res->tv_sec));
|
---|
326 | res->tv_nsec = 0;
|
---|
327 |
|
---|
328 | return 0;
|
---|
329 | }
|
---|
330 |
|
---|
331 | int shell_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
---|
332 | {
|
---|
333 | SYSTIM now = 0;
|
---|
334 |
|
---|
335 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
336 | return -EINVAL;
|
---|
337 |
|
---|
338 | get_tim(&now);
|
---|
339 | tp->tv_sec = now / 1000000;
|
---|
340 | tp->tv_nsec = (now % 1000000) * 1000;
|
---|
341 |
|
---|
342 | return 0;
|
---|
343 | }
|
---|
344 |
|
---|
345 | int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
|
---|
346 | {
|
---|
347 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
348 | return -EINVAL;
|
---|
349 |
|
---|
350 | SYSTIM time;
|
---|
351 | ER ret;
|
---|
352 |
|
---|
353 | time = (tp->tv_sec * 1000000ll) + (tp->tv_nsec / 1000ll);
|
---|
354 |
|
---|
355 | ret = set_tim(time);
|
---|
356 | if (ret != E_OK) {
|
---|
357 | return -EPERM;
|
---|
358 | }
|
---|
359 |
|
---|
360 | return 0;
|
---|
361 | }
|
---|
362 |
|
---|
363 | sigset_t g_sigmask;
|
---|
364 |
|
---|
365 | int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
|
---|
366 | {
|
---|
367 | if (old != NULL)
|
---|
368 | memcpy(old, &g_sigmask, sizeof(sigset_t));
|
---|
369 |
|
---|
370 | switch (how) {
|
---|
371 | case SIG_BLOCK:
|
---|
372 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
373 | g_sigmask.__bits[i] |= set->__bits[i];
|
---|
374 | }
|
---|
375 | break;
|
---|
376 | case SIG_UNBLOCK:
|
---|
377 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
378 | g_sigmask.__bits[i] &= ~set->__bits[i];
|
---|
379 | }
|
---|
380 | break;
|
---|
381 | case SIG_SETMASK:
|
---|
382 | memcpy(&g_sigmask, set, sizeof(sigset_t));
|
---|
383 | break;
|
---|
384 | default:
|
---|
385 | return -EINVAL;
|
---|
386 | }
|
---|
387 |
|
---|
388 | return 0;
|
---|
389 | }
|
---|
390 |
|
---|
391 | struct sigaction sigtable[7];
|
---|
392 |
|
---|
393 | int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
|
---|
394 | {
|
---|
395 | struct sigaction *sat;
|
---|
396 |
|
---|
397 | switch(sig){
|
---|
398 | case SIGALRM:
|
---|
399 | sat = &sigtable[0];
|
---|
400 | break;
|
---|
401 | case SIGFPE:
|
---|
402 | sat = &sigtable[1];
|
---|
403 | break;
|
---|
404 | case SIGILL:
|
---|
405 | sat = &sigtable[2];
|
---|
406 | break;
|
---|
407 | case SIGSEGV:
|
---|
408 | sat = &sigtable[3];
|
---|
409 | break;
|
---|
410 | case SIGBUS:
|
---|
411 | sat = &sigtable[4];
|
---|
412 | break;
|
---|
413 | case SIGABRT:
|
---|
414 | sat = &sigtable[5];
|
---|
415 | break;
|
---|
416 | case SIGPIPE:
|
---|
417 | sat = &sigtable[6];
|
---|
418 | break;
|
---|
419 | default:
|
---|
420 | return -EINVAL;
|
---|
421 | }
|
---|
422 |
|
---|
423 | if (old != NULL)
|
---|
424 | memcpy(old, sat, sizeof(struct sigaction));
|
---|
425 |
|
---|
426 | memcpy(sat, sa, sizeof(struct sigaction));
|
---|
427 |
|
---|
428 | return 0;
|
---|
429 | }
|
---|
430 |
|
---|
431 | int shell_madvise(void *a, size_t b, int c)
|
---|
432 | {
|
---|
433 | return 0;
|
---|
434 | }
|
---|
435 |
|
---|
436 | int shell_gettid()
|
---|
437 | {
|
---|
438 | ID tskid;
|
---|
439 | ER ret;
|
---|
440 |
|
---|
441 | ret = get_tid(&tskid);
|
---|
442 | if (ret != E_OK)
|
---|
443 | return -1;
|
---|
444 |
|
---|
445 | return tskid;
|
---|
446 | }
|
---|
447 |
|
---|
448 | int shell_tkill(int tid, int sig)
|
---|
449 | {
|
---|
450 | if ((tid == NTSHELL_TASK) && (sig == SIGABRT)) {
|
---|
451 | shell_abort();
|
---|
452 | }
|
---|
453 |
|
---|
454 | no_implement("tkill");
|
---|
455 | return -1;
|
---|
456 | }
|
---|
457 |
|
---|