source: asp3_tinet_ecnl_rx/trunk/ntshell/src/ntshell_main.c@ 342

Last change on this file since 342 was 342, checked in by coas-nagasima, 6 years ago

syscallが関数呼びになるよう変更
他更新

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