1 | /*
|
---|
2 | * TOPPERS PROJECT Home Network Working Group Software
|
---|
3 | *
|
---|
4 | * Copyright (C) 2014-2019 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 "target_serial.h"
|
---|
52 | #include "kernel_cfg.h"
|
---|
53 | #include "ffarch.h"
|
---|
54 | #include "ff.h"
|
---|
55 | #include "core/ntshell.h"
|
---|
56 | #include "core/ntlibc.h"
|
---|
57 | #include "util/ntstdio.h"
|
---|
58 | #include "usrcmd.h"
|
---|
59 | #include "util/ntopt.h"
|
---|
60 | #include "ntshell_main.h"
|
---|
61 | #include "fdtable.h"
|
---|
62 |
|
---|
63 | char command[NTOPT_TEXT_MAXLEN];
|
---|
64 |
|
---|
65 | extern uint8_t mac_addr[6];
|
---|
66 | const struct utsname host_name = {
|
---|
67 | "TOPPERS/ASP3",
|
---|
68 | TARGET_NAME,
|
---|
69 | "3.2.0",
|
---|
70 | "3.2.0",
|
---|
71 | TARGET_NAME,
|
---|
72 | "toppers.jp"
|
---|
73 | };
|
---|
74 |
|
---|
75 | int shell_uname(struct utsname *uts)
|
---|
76 | {
|
---|
77 | memcpy(uts, &host_name, sizeof(host_name));
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 | void stdio_open(ID portid);
|
---|
82 | static int usrcmd_ntopt_callback(long *args, void *extobj);
|
---|
83 |
|
---|
84 | int ntshell_exit_code;
|
---|
85 | volatile int ntshell_state;
|
---|
86 | jmp_buf process_exit;
|
---|
87 |
|
---|
88 | void ntshell_task_init(ID portid)
|
---|
89 | {
|
---|
90 | stdio_open(portid);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * ntshellタスク
|
---|
95 | */
|
---|
96 | void ntshell_task(intptr_t exinf)
|
---|
97 | {
|
---|
98 | ntshell_state = 1;
|
---|
99 |
|
---|
100 | if (setjmp(process_exit) == 0) {
|
---|
101 | ntshell_exit_code = ntopt_parse(command, usrcmd_ntopt_callback, NULL);
|
---|
102 | }
|
---|
103 |
|
---|
104 | fflush(stdout);
|
---|
105 | clean_fd();
|
---|
106 |
|
---|
107 | ntshell_state = 2;
|
---|
108 | }
|
---|
109 |
|
---|
110 | void ntshell_change_netif_link(uint8_t link_up, uint8_t up)
|
---|
111 | {
|
---|
112 | FLGPTN flgptn;
|
---|
113 | T_RTSK rtsk;
|
---|
114 | ER ret;
|
---|
115 |
|
---|
116 | ret = ref_tsk(NTSHELL_TASK, &rtsk);
|
---|
117 | if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
|
---|
118 | return;
|
---|
119 |
|
---|
120 | FD_SET(0, (fd_set *)&flgptn);
|
---|
121 |
|
---|
122 | set_flg(FLG_SELECT_WAIT, flgptn);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static int usrcmd_ntopt_callback(long *args, void *extobj)
|
---|
126 | {
|
---|
127 | const cmd_table_t *p = cmd_table_info.table;
|
---|
128 | int result = 0;
|
---|
129 | int found = 0;
|
---|
130 |
|
---|
131 | if (*args == 0)
|
---|
132 | return result;
|
---|
133 |
|
---|
134 | if (strcmp((const char *)args[1], "help") == 0) {
|
---|
135 | found = 1;
|
---|
136 | result = usrcmd_help(args[0], (char **)&args[1]);
|
---|
137 | }
|
---|
138 | else for (int i = 0; i < cmd_table_info.count; i++) {
|
---|
139 | if (strcmp((const char *)args[1], p->cmd) == 0) {
|
---|
140 | found = 1;
|
---|
141 | result = p->func(args[0], (char **)&args[1]);
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | p++;
|
---|
145 | }
|
---|
146 |
|
---|
147 | if ((found == 0) && (((const char *)args[1])[0] != '\0'))
|
---|
148 | printf("Unknown command found.\n");
|
---|
149 |
|
---|
150 | return result;
|
---|
151 | }
|
---|
152 |
|
---|
153 | int usrcmd_help(int argc, char **argv)
|
---|
154 | {
|
---|
155 | const cmd_table_t *p = cmd_table_info.table;
|
---|
156 | for (int i = 0; i < cmd_table_info.count; i++) {
|
---|
157 | fwrite(p->cmd, strlen(p->cmd), 1, stdout);
|
---|
158 | fwrite("\t:", strlen("\t:"), 1, stdout);
|
---|
159 | puts(p->desc);
|
---|
160 | p++;
|
---|
161 | }
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | void shell_abort()
|
---|
166 | {
|
---|
167 | ntshell_exit_code = -1;
|
---|
168 | longjmp(process_exit, 1);
|
---|
169 | }
|
---|
170 |
|
---|
171 | void shell_exit(int exitcd)
|
---|
172 | {
|
---|
173 | ntshell_exit_code = exitcd;
|
---|
174 | longjmp(process_exit, 1);
|
---|
175 | }
|
---|
176 |
|
---|
177 | void shell_exit_group(int exitcd)
|
---|
178 | {
|
---|
179 | ntshell_exit_code = exitcd;
|
---|
180 | longjmp(process_exit, 1);
|
---|
181 | }
|
---|
182 |
|
---|
183 | int execute_command(int wait)
|
---|
184 | {
|
---|
185 | T_RTSK rtsk;
|
---|
186 | ER ret;
|
---|
187 |
|
---|
188 | ret = ter_tsk(NTSHELL_TASK);
|
---|
189 | if ((ret != E_OK) && (ret != E_OBJ)) {
|
---|
190 | syslog(LOG_ERROR, "ter_tsk => %d", ret);
|
---|
191 | }
|
---|
192 |
|
---|
193 | tslp_tsk(100000);
|
---|
194 |
|
---|
195 | clean_fd();
|
---|
196 |
|
---|
197 | ntshell_state = 0;
|
---|
198 | ret = act_tsk(NTSHELL_TASK);
|
---|
199 | if (ret != E_OK) {
|
---|
200 | syslog(LOG_ERROR, "act_tsk => %d", ret);
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (wait == 0)
|
---|
204 | return 0;
|
---|
205 |
|
---|
206 | do {
|
---|
207 | tslp_tsk(100000);
|
---|
208 |
|
---|
209 | ret = ref_tsk(NTSHELL_TASK, &rtsk);
|
---|
210 | if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
|
---|
211 | ntshell_state = 3;
|
---|
212 | } while(ntshell_state == 1);
|
---|
213 |
|
---|
214 | return ntshell_exit_code;
|
---|
215 | }
|
---|
216 |
|
---|
217 | int cmd_execute(const char *text, void *extobj)
|
---|
218 | {
|
---|
219 | ntlibc_strlcpy(command, text, sizeof(command));
|
---|
220 | return execute_command(1);
|
---|
221 | }
|
---|
222 |
|
---|
223 | int shell_clock_getres(clockid_t clk_id, struct timespec *res)
|
---|
224 | {
|
---|
225 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
226 | return -EINVAL;
|
---|
227 |
|
---|
228 | memset(&res->tv_sec, 0xFF, sizeof(res->tv_sec));
|
---|
229 | res->tv_nsec = 0;
|
---|
230 |
|
---|
231 | return 0;
|
---|
232 | }
|
---|
233 |
|
---|
234 | int shell_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
---|
235 | {
|
---|
236 | SYSTIM now = 0;
|
---|
237 |
|
---|
238 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
239 | return -EINVAL;
|
---|
240 |
|
---|
241 | get_tim(&now);
|
---|
242 | tp->tv_sec = now / 1000000;
|
---|
243 | tp->tv_nsec = (now % 1000000) * 1000;
|
---|
244 |
|
---|
245 | return 0;
|
---|
246 | }
|
---|
247 |
|
---|
248 | int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
|
---|
249 | {
|
---|
250 | if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
|
---|
251 | return -EINVAL;
|
---|
252 |
|
---|
253 | SYSTIM time;
|
---|
254 | ER ret;
|
---|
255 |
|
---|
256 | time = (tp->tv_sec * 1000000ll) + (tp->tv_nsec / 1000ll);
|
---|
257 |
|
---|
258 | ret = set_tim(time);
|
---|
259 | if (ret != E_OK) {
|
---|
260 | return -EPERM;
|
---|
261 | }
|
---|
262 |
|
---|
263 | return 0;
|
---|
264 | }
|
---|
265 |
|
---|
266 | sigset_t g_sigmask;
|
---|
267 |
|
---|
268 | int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
|
---|
269 | {
|
---|
270 | if (old != NULL)
|
---|
271 | memcpy(old, &g_sigmask, sizeof(sigset_t));
|
---|
272 |
|
---|
273 | switch (how) {
|
---|
274 | case SIG_BLOCK:
|
---|
275 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
276 | g_sigmask.__bits[i] |= set->__bits[i];
|
---|
277 | }
|
---|
278 | break;
|
---|
279 | case SIG_UNBLOCK:
|
---|
280 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
281 | g_sigmask.__bits[i] &= ~set->__bits[i];
|
---|
282 | }
|
---|
283 | break;
|
---|
284 | case SIG_SETMASK:
|
---|
285 | memcpy(&g_sigmask, set, sizeof(sigset_t));
|
---|
286 | break;
|
---|
287 | default:
|
---|
288 | return -EINVAL;
|
---|
289 | }
|
---|
290 |
|
---|
291 | return 0;
|
---|
292 | }
|
---|
293 |
|
---|
294 | // musl-1.1.18\src\internal\ksigaction.h
|
---|
295 | struct k_sigaction {
|
---|
296 | void(*handler)(int);
|
---|
297 | unsigned long flags;
|
---|
298 | void(*restorer)(void);
|
---|
299 | unsigned mask[2];
|
---|
300 | };
|
---|
301 |
|
---|
302 | struct k_sigaction sigtable[7];
|
---|
303 |
|
---|
304 | int shell_sigaction(int sig, const struct k_sigaction *__restrict sa,
|
---|
305 | struct k_sigaction *__restrict old, size_t size)
|
---|
306 | {
|
---|
307 | struct k_sigaction *sat;
|
---|
308 |
|
---|
309 | switch(sig){
|
---|
310 | case SIGALRM:
|
---|
311 | sat = &sigtable[0];
|
---|
312 | break;
|
---|
313 | case SIGFPE:
|
---|
314 | sat = &sigtable[1];
|
---|
315 | break;
|
---|
316 | case SIGILL:
|
---|
317 | sat = &sigtable[2];
|
---|
318 | break;
|
---|
319 | case SIGSEGV:
|
---|
320 | sat = &sigtable[3];
|
---|
321 | break;
|
---|
322 | case SIGBUS:
|
---|
323 | sat = &sigtable[4];
|
---|
324 | break;
|
---|
325 | case SIGABRT:
|
---|
326 | sat = &sigtable[5];
|
---|
327 | break;
|
---|
328 | case SIGPIPE:
|
---|
329 | sat = &sigtable[6];
|
---|
330 | break;
|
---|
331 | default:
|
---|
332 | return -EINVAL;
|
---|
333 | }
|
---|
334 |
|
---|
335 | if (old != NULL)
|
---|
336 | memcpy(old, sat, offsetof(struct k_sigaction, mask) + size);
|
---|
337 |
|
---|
338 | memcpy(sat, sa, offsetof(struct k_sigaction, mask) + size);
|
---|
339 |
|
---|
340 | return 0;
|
---|
341 | }
|
---|
342 |
|
---|
343 | int shell_madvise(void *a, size_t b, int c)
|
---|
344 | {
|
---|
345 | return 0;
|
---|
346 | }
|
---|
347 |
|
---|
348 | int shell_gettid()
|
---|
349 | {
|
---|
350 | ID tskid;
|
---|
351 | ER ret;
|
---|
352 |
|
---|
353 | ret = get_tid(&tskid);
|
---|
354 | if (ret != E_OK)
|
---|
355 | return -1;
|
---|
356 |
|
---|
357 | return tskid;
|
---|
358 | }
|
---|
359 |
|
---|
360 | int shell_tkill(int tid, int sig)
|
---|
361 | {
|
---|
362 | if ((tid == NTSHELL_TASK) && (sig == SIGABRT)) {
|
---|
363 | shell_abort();
|
---|
364 | }
|
---|
365 |
|
---|
366 | no_implement("tkill");
|
---|
367 | return -1;
|
---|
368 | }
|
---|
369 |
|
---|
370 | int shell_kill(int pid, int sig)
|
---|
371 | {
|
---|
372 | DebugBreak();
|
---|
373 | return -1;
|
---|
374 | }
|
---|
375 |
|
---|
376 | int shell_gettimeofday(struct timeval *tv, void *tzvp)
|
---|
377 | {
|
---|
378 | SYSTIM time;
|
---|
379 | if (!tv) return 0;
|
---|
380 | get_tim(&time);
|
---|
381 | tv->tv_sec = time / 1000000;
|
---|
382 | tv->tv_usec = time - (tv->tv_sec * 1000000);
|
---|
383 | return 0;
|
---|
384 | }
|
---|
385 |
|
---|
386 | int shell_nanosleep(const struct timespec *req, struct timespec *rem)
|
---|
387 | {
|
---|
388 | ER ret;
|
---|
389 | TMO tmo;
|
---|
390 | SYSTIM prev, now, diff;
|
---|
391 |
|
---|
392 | if ((req == NULL) || (req->tv_nsec < 0) || (req->tv_nsec >= 1000000000))
|
---|
393 | return -EINVAL;
|
---|
394 |
|
---|
395 | get_tim(&prev);
|
---|
396 |
|
---|
397 | tmo = req->tv_sec * 1000000 + req->tv_nsec / 1000;
|
---|
398 | ret = tslp_tsk(tmo);
|
---|
399 | if (ret == E_OK) {
|
---|
400 | if (rem != NULL) {
|
---|
401 | get_tim(&now);
|
---|
402 | diff = now - prev;
|
---|
403 | rem->tv_sec = diff / 1000000ll;
|
---|
404 | rem->tv_nsec = (diff - (rem->tv_sec * 1000000ll)) * 1000ll;
|
---|
405 | }
|
---|
406 | return 0;
|
---|
407 | }
|
---|
408 | else if (ret == E_TMOUT) {
|
---|
409 | if (rem != NULL) {
|
---|
410 | rem->tv_sec = 0;
|
---|
411 | rem->tv_nsec = 0;
|
---|
412 | }
|
---|
413 | return 0;
|
---|
414 | }
|
---|
415 |
|
---|
416 | return -EFAULT;
|
---|
417 | }
|
---|