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 "mbed_api.h"
|
---|
62 | #include "usb_hbth.h"
|
---|
63 |
|
---|
64 | extern int bt_bnep_mode;
|
---|
65 |
|
---|
66 | char command[NTOPT_TEXT_MAXLEN];
|
---|
67 | ntstdio_t ntstdio;
|
---|
68 |
|
---|
69 | extern uint8_t mac_addr[6];
|
---|
70 | const struct utsname host_name = {
|
---|
71 | "TOPPERS/ASP3",
|
---|
72 | TARGET_NAME,
|
---|
73 | "3.2.0",
|
---|
74 | "3.2.0",
|
---|
75 | TARGET_NAME,
|
---|
76 | "toppers.jp"
|
---|
77 | };
|
---|
78 |
|
---|
79 | int shell_uname(struct utsname *uts)
|
---|
80 | {
|
---|
81 | memcpy(uts, &host_name, sizeof(host_name));
|
---|
82 | return 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static int usrcmd_ntopt_callback(long *args, void *extobj);
|
---|
86 |
|
---|
87 | int ntshell_exit_code;
|
---|
88 | volatile int ntshell_state;
|
---|
89 | jmp_buf process_exit;
|
---|
90 | void sys_init(void);
|
---|
91 |
|
---|
92 | void ntshell_task_init()
|
---|
93 | {
|
---|
94 | sys_init();
|
---|
95 |
|
---|
96 | ntstdio_init(&ntstdio, NTSTDIO_OPTION_LINE_ECHO | NTSTDIO_OPTION_CANON | NTSTDIO_OPTION_LF_CRLF | NTSTDIO_OPTION_LF_CR, ntstdio_xi, ntstdio_xo);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * ntshellタスク
|
---|
101 | */
|
---|
102 | void ntshell_task(intptr_t exinf)
|
---|
103 | {
|
---|
104 | ntshell_state = 1;
|
---|
105 | ntshell_exit_code = ntopt_parse(command, usrcmd_ntopt_callback, NULL);
|
---|
106 | ntshell_state = 2;
|
---|
107 | }
|
---|
108 |
|
---|
109 | void ntshell_change_netif_link(uint8_t link_up, uint8_t up)
|
---|
110 | {
|
---|
111 | FLGPTN flgptn;
|
---|
112 | T_RTSK rtsk;
|
---|
113 | ER ret;
|
---|
114 |
|
---|
115 | ret = ref_tsk(NTSHELL_TASK, &rtsk);
|
---|
116 | if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
|
---|
117 | return;
|
---|
118 |
|
---|
119 | FD_SET(0, (fd_set *)&flgptn);
|
---|
120 |
|
---|
121 | set_flg(FLG_SELECT_WAIT, flgptn);
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int usrcmd_ntopt_callback(long *args, void *extobj)
|
---|
125 | {
|
---|
126 | const cmd_table_t *p = cmd_table_info.table;
|
---|
127 | if (strcmp((const char *)args[1], "help") == 0) {
|
---|
128 | usrcmd_help(args[0], (char **)&args[1]);
|
---|
129 | }
|
---|
130 | else for (int i = 0; i < cmd_table_info.count; i++) {
|
---|
131 | if (strcmp((const char *)args[1], p->cmd) == 0) {
|
---|
132 | return p->func(args[0], (char **)&args[1]);
|
---|
133 | }
|
---|
134 | p++;
|
---|
135 | }
|
---|
136 | /*if (setjmp(process_exit) == 0) {
|
---|
137 | main(args);
|
---|
138 | }*/
|
---|
139 | clean_fd();
|
---|
140 | return 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int usrcmd_help(int argc, char **argv)
|
---|
144 | {
|
---|
145 | const cmd_table_t *p = cmd_table_info.table;
|
---|
146 | for (int i = 0; i < cmd_table_info.count; i++) {
|
---|
147 | ntstdio_puts(&ntstdio, p->cmd);
|
---|
148 | ntstdio_puts(&ntstdio, "\t:");
|
---|
149 | ntstdio_puts(&ntstdio, p->desc);
|
---|
150 | ntstdio_puts(&ntstdio, "\n");
|
---|
151 | p++;
|
---|
152 | }
|
---|
153 | return 0;
|
---|
154 | }
|
---|
155 |
|
---|
156 | void shellif_into()
|
---|
157 | {
|
---|
158 | /* メインタスクの優先度より高くする */
|
---|
159 | chg_pri(NTSHELL_PRIORITY, main_task_priority);
|
---|
160 | }
|
---|
161 |
|
---|
162 | void shellif_outof()
|
---|
163 | {
|
---|
164 | /* ntshellタスクの優先度に戻す */
|
---|
165 | chg_pri(NTSHELL_PRIORITY, NTSHELL_PRIORITY);
|
---|
166 | }
|
---|
167 |
|
---|
168 | void shell_abort()
|
---|
169 | {
|
---|
170 | ntshell_exit_code = -1;
|
---|
171 | longjmp(process_exit, 1);
|
---|
172 | }
|
---|
173 |
|
---|
174 | void shell_exit(int exitcd)
|
---|
175 | {
|
---|
176 | ntshell_exit_code = exitcd;
|
---|
177 | longjmp(process_exit, 1);
|
---|
178 | }
|
---|
179 |
|
---|
180 | void shell_exit_group(int exitcd)
|
---|
181 | {
|
---|
182 | ntshell_exit_code = exitcd;
|
---|
183 | longjmp(process_exit, 1);
|
---|
184 | }
|
---|
185 |
|
---|
186 | int execute_command(int wait)
|
---|
187 | {
|
---|
188 | ER ret;
|
---|
189 |
|
---|
190 | ret = ter_tsk(NTSHELL_TASK);
|
---|
191 | if ((ret != E_OK) && (ret != E_OBJ)) {
|
---|
192 | syslog(LOG_ERROR, "ter_tsk => %d", ret);
|
---|
193 | }
|
---|
194 |
|
---|
195 | tslp_tsk(100000);
|
---|
196 |
|
---|
197 | clean_fd();
|
---|
198 |
|
---|
199 | ntshell_state = 0;
|
---|
200 | ret = act_tsk(NTSHELL_TASK);
|
---|
201 | if (ret != E_OK) {
|
---|
202 | syslog(LOG_ERROR, "act_tsk => %d", ret);
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (wait == 0)
|
---|
206 | return 0;
|
---|
207 |
|
---|
208 | do {
|
---|
209 | tslp_tsk(100000);
|
---|
210 | } while(ntshell_state == 1);
|
---|
211 |
|
---|
212 | return ntshell_exit_code;
|
---|
213 | }
|
---|
214 |
|
---|
215 | int cmd_execute(const char *text, void *extobj)
|
---|
216 | {
|
---|
217 | ntlibc_strlcpy(command, text, sizeof(command));
|
---|
218 | return execute_command(1);
|
---|
219 | }
|
---|
220 |
|
---|
221 | int stdio_close(struct _IO_FILE *fp)
|
---|
222 | {
|
---|
223 | return -EPERM;
|
---|
224 | }
|
---|
225 |
|
---|
226 | size_t stdio_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
227 | {
|
---|
228 | return -EPERM;
|
---|
229 | }
|
---|
230 |
|
---|
231 | size_t stdio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
232 | {
|
---|
233 | return -EPERM;
|
---|
234 | }
|
---|
235 |
|
---|
236 | size_t stdin_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
237 | {
|
---|
238 | int i = 0;
|
---|
239 | while (i < len) {
|
---|
240 | int c = ntstdio_getc(&ntstdio);
|
---|
241 | data[i++] = c;
|
---|
242 | if ((c == EOF) || (c == '\n'))
|
---|
243 | break;
|
---|
244 | }
|
---|
245 | return i;
|
---|
246 | }
|
---|
247 |
|
---|
248 | size_t stdout_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
249 | {
|
---|
250 | for (int i = 0; i < len; i++) {
|
---|
251 | ntstdio_putc(&ntstdio, data[i]);
|
---|
252 | }
|
---|
253 | return len;
|
---|
254 | }
|
---|
255 |
|
---|
256 | size_t stderr_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
257 | {
|
---|
258 | for (int i = 0; i < len; i++) {
|
---|
259 | ntstdio_putc(&ntstdio, data[i]);
|
---|
260 | }
|
---|
261 | return len;
|
---|
262 | }
|
---|
263 |
|
---|
264 | int sio_close(struct _IO_FILE *fp)
|
---|
265 | {
|
---|
266 | return -EPERM;
|
---|
267 | }
|
---|
268 |
|
---|
269 | size_t sio_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
270 | {
|
---|
271 | return -EPERM;
|
---|
272 | }
|
---|
273 |
|
---|
274 | size_t sio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
275 | {
|
---|
276 | return -EPERM;
|
---|
277 | }
|
---|
278 |
|
---|
279 | off_t sio_seek(struct _IO_FILE *fp, off_t ofs, int org)
|
---|
280 | {
|
---|
281 | return -EPERM;
|
---|
282 | }
|
---|
283 |
|
---|
284 | int sio_ioctl(struct _IO_FILE *fp, int request, void *arg)
|
---|
285 | {
|
---|
286 | switch (request) {
|
---|
287 | case TIOCGWINSZ:
|
---|
288 | return 0;
|
---|
289 | case TCGETS:
|
---|
290 | return sio_tcgetattr(fp->fd, (struct termios *)arg);
|
---|
291 | case TCSETS + TCSANOW:
|
---|
292 | case TCSETS + TCSADRAIN:
|
---|
293 | case TCSETS + TCSAFLUSH:
|
---|
294 | return sio_tcsetattr(fp->fd, request - TCSETS, (const struct termios *)arg);
|
---|
295 | }
|
---|
296 |
|
---|
297 | return -EINVAL;
|
---|
298 | }
|
---|
299 |
|
---|
300 | int shell_clock_getres(clockid_t clk_id, struct timespec *res)
|
---|
301 | {
|
---|
302 | if (clk_id != CLOCK_REALTIME)
|
---|
303 | return -EINVAL;
|
---|
304 |
|
---|
305 | memset(&res->tv_sec, 0xFF, sizeof(res->tv_sec));
|
---|
306 | res->tv_nsec = 0;
|
---|
307 |
|
---|
308 | return 0;
|
---|
309 | }
|
---|
310 |
|
---|
311 | int shell_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
---|
312 | {
|
---|
313 | SYSTIM now = 0;
|
---|
314 |
|
---|
315 | if (clk_id != CLOCK_REALTIME)
|
---|
316 | return -EINVAL;
|
---|
317 |
|
---|
318 | get_tim(&now);
|
---|
319 | tp->tv_sec = now / 1000000;
|
---|
320 | tp->tv_nsec = (now % 1000000) * 1000;
|
---|
321 |
|
---|
322 | return 0;
|
---|
323 | }
|
---|
324 |
|
---|
325 | int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
|
---|
326 | {
|
---|
327 | if (clk_id != CLOCK_REALTIME)
|
---|
328 | return -EINVAL;
|
---|
329 |
|
---|
330 | rtc_write(tp->tv_sec);
|
---|
331 |
|
---|
332 | return 0;
|
---|
333 | }
|
---|
334 |
|
---|
335 | sigset_t g_sigmask;
|
---|
336 |
|
---|
337 | int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
|
---|
338 | {
|
---|
339 | if (old != NULL)
|
---|
340 | memcpy(old, &g_sigmask, sizeof(sigset_t));
|
---|
341 |
|
---|
342 | switch (how) {
|
---|
343 | case SIG_BLOCK:
|
---|
344 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
345 | g_sigmask.__bits[i] |= set->__bits[i];
|
---|
346 | }
|
---|
347 | break;
|
---|
348 | case SIG_UNBLOCK:
|
---|
349 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
350 | g_sigmask.__bits[i] &= ~set->__bits[i];
|
---|
351 | }
|
---|
352 | break;
|
---|
353 | case SIG_SETMASK:
|
---|
354 | memcpy(&g_sigmask, set, sizeof(sigset_t));
|
---|
355 | break;
|
---|
356 | default:
|
---|
357 | return -EINVAL;
|
---|
358 | }
|
---|
359 |
|
---|
360 | return 0;
|
---|
361 | }
|
---|
362 |
|
---|
363 | struct sigaction sigtable[6];
|
---|
364 |
|
---|
365 | int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
|
---|
366 | {
|
---|
367 | struct sigaction *sat;
|
---|
368 |
|
---|
369 | switch(sig){
|
---|
370 | case SIGALRM:
|
---|
371 | sat = &sigtable[0];
|
---|
372 | break;
|
---|
373 | case SIGFPE:
|
---|
374 | sat = &sigtable[1];
|
---|
375 | break;
|
---|
376 | case SIGILL:
|
---|
377 | sat = &sigtable[2];
|
---|
378 | break;
|
---|
379 | case SIGSEGV:
|
---|
380 | sat = &sigtable[3];
|
---|
381 | break;
|
---|
382 | case SIGBUS:
|
---|
383 | sat = &sigtable[4];
|
---|
384 | break;
|
---|
385 | case SIGABRT:
|
---|
386 | sat = &sigtable[5];
|
---|
387 | break;
|
---|
388 | default:
|
---|
389 | return -EINVAL;
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (old != NULL)
|
---|
393 | memcpy(old, sat, sizeof(struct sigaction));
|
---|
394 |
|
---|
395 | memcpy(sat, sa, sizeof(struct sigaction));
|
---|
396 |
|
---|
397 | return 0;
|
---|
398 | }
|
---|
399 |
|
---|
400 | int shell_madvise(void *a, size_t b, int c)
|
---|
401 | {
|
---|
402 | return 0;
|
---|
403 | }
|
---|
404 |
|
---|
405 | int shell_gettid()
|
---|
406 | {
|
---|
407 | ID tskid;
|
---|
408 | ER ret;
|
---|
409 |
|
---|
410 | ret = get_tid(&tskid);
|
---|
411 | if (ret != E_OK)
|
---|
412 | return -1;
|
---|
413 |
|
---|
414 | return tskid;
|
---|
415 | }
|
---|
416 |
|
---|
417 | int shell_tkill(int tid, int sig)
|
---|
418 | {
|
---|
419 | if ((tid == NTSHELL_TASK) && (sig == SIGABRT)) {
|
---|
420 | shell_abort();
|
---|
421 | }
|
---|
422 |
|
---|
423 | no_implement("tkill");
|
---|
424 | return -1;
|
---|
425 | }
|
---|
426 |
|
---|