source: asp3_tinet_ecnl_arm/trunk/ntshell/src/ntshell_main.c@ 374

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

mbed関連を更新
シリアルドライバをmbedのHALを使うよう変更
ファイルディスクリプタの処理を更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 8.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
62char command[NTOPT_TEXT_MAXLEN];
63
64extern uint8_t mac_addr[6];
65const struct utsname host_name = {
66 "TOPPERS/ASP3",
67 TARGET_NAME,
68 "3.2.0",
69 "3.2.0",
70 TARGET_NAME,
71 "toppers.jp"
72};
73
74int shell_uname(struct utsname *uts)
75{
76 memcpy(uts, &host_name, sizeof(host_name));
77 return 0;
78}
79
80static int usrcmd_ntopt_callback(long *args, void *extobj);
81
82int ntshell_exit_code;
83volatile int ntshell_state;
84jmp_buf process_exit;
85
86void ntshell_task_init(ID portid)
87{
88 serial_ctl_por(portid, IOCTL_CRLF | IOCTL_FCSND | IOCTL_FCRCV);
89}
90
91/*
92 * ntshellタスク
93 */
94void ntshell_task(intptr_t exinf)
95{
96 ntshell_state = 1;
97 ntshell_exit_code = ntopt_parse(command, usrcmd_ntopt_callback, NULL);
98 ntshell_state = 2;
99}
100
101void ntshell_change_netif_link(uint8_t link_up, uint8_t up)
102{
103 FLGPTN flgptn;
104 T_RTSK rtsk;
105 ER ret;
106
107 ret = ref_tsk(NTSHELL_TASK, &rtsk);
108 if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
109 return;
110
111 FD_SET(0, (fd_set *)&flgptn);
112
113 set_flg(FLG_SELECT_WAIT, flgptn);
114}
115
116static int usrcmd_ntopt_callback(long *args, void *extobj)
117{
118 const cmd_table_t *p = cmd_table_info.table;
119 int result = 0;
120 int found = 0;
121
122 if (*args == 0)
123 return result;
124
125 if (strcmp((const char *)args[1], "help") == 0) {
126 found = 1;
127 result = usrcmd_help(args[0], (char **)&args[1]);
128 }
129 else for (int i = 0; i < cmd_table_info.count; i++) {
130 if (strcmp((const char *)args[1], p->cmd) == 0) {
131 found = 1;
132 result = p->func(args[0], (char **)&args[1]);
133 break;
134 }
135 p++;
136 }
137
138 if ((found == 0) && (((const char *)args[1])[0] != '\0'))
139 printf("Unknown command found.\n");
140
141 clean_fd();
142
143 return result;
144}
145
146int usrcmd_help(int argc, char **argv)
147{
148 const cmd_table_t *p = cmd_table_info.table;
149 for (int i = 0; i < cmd_table_info.count; i++) {
150 fwrite(p->cmd, strlen(p->cmd), 1, stdout);
151 fwrite("\t:", strlen("\t:"), 1, stdout);
152 puts(p->desc);
153 p++;
154 }
155 return 0;
156}
157
158void shell_abort()
159{
160 ntshell_exit_code = -1;
161 longjmp(process_exit, 1);
162}
163
164void shell_exit(int exitcd)
165{
166 ntshell_exit_code = exitcd;
167 longjmp(process_exit, 1);
168}
169
170void shell_exit_group(int exitcd)
171{
172 ntshell_exit_code = exitcd;
173 longjmp(process_exit, 1);
174}
175
176int execute_command(int wait)
177{
178 T_RTSK rtsk;
179 ER ret;
180
181 ret = ter_tsk(NTSHELL_TASK);
182 if ((ret != E_OK) && (ret != E_OBJ)) {
183 syslog(LOG_ERROR, "ter_tsk => %d", ret);
184 }
185
186 tslp_tsk(100000);
187
188 clean_fd();
189
190 ntshell_state = 0;
191 ret = act_tsk(NTSHELL_TASK);
192 if (ret != E_OK) {
193 syslog(LOG_ERROR, "act_tsk => %d", ret);
194 }
195
196 if (wait == 0)
197 return 0;
198
199 do {
200 tslp_tsk(100000);
201
202 ret = ref_tsk(NTSHELL_TASK, &rtsk);
203 if ((ret != E_OK) || (rtsk.tskstat == TTS_DMT))
204 ntshell_state = 3;
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 shell_clock_getres(clockid_t clk_id, struct timespec *res)
217{
218 if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
219 return -EINVAL;
220
221 memset(&res->tv_sec, 0xFF, sizeof(res->tv_sec));
222 res->tv_nsec = 0;
223
224 return 0;
225}
226
227int shell_clock_gettime(clockid_t clk_id, struct timespec *tp)
228{
229 SYSTIM now = 0;
230
231 if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
232 return -EINVAL;
233
234 get_tim(&now);
235 tp->tv_sec = now / 1000000;
236 tp->tv_nsec = (now % 1000000) * 1000;
237
238 return 0;
239}
240
241int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
242{
243 if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
244 return -EINVAL;
245
246 SYSTIM time;
247 ER ret;
248
249 time = (tp->tv_sec * 1000000ll) + (tp->tv_nsec / 1000ll);
250
251 ret = set_tim(time);
252 if (ret != E_OK) {
253 return -EPERM;
254 }
255
256 return 0;
257}
258
259sigset_t g_sigmask;
260
261int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
262{
263 if (old != NULL)
264 memcpy(old, &g_sigmask, sizeof(sigset_t));
265
266 switch (how) {
267 case SIG_BLOCK:
268 for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
269 g_sigmask.__bits[i] |= set->__bits[i];
270 }
271 break;
272 case SIG_UNBLOCK:
273 for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
274 g_sigmask.__bits[i] &= ~set->__bits[i];
275 }
276 break;
277 case SIG_SETMASK:
278 memcpy(&g_sigmask, set, sizeof(sigset_t));
279 break;
280 default:
281 return -EINVAL;
282 }
283
284 return 0;
285}
286
287struct sigaction sigtable[7];
288
289int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
290{
291 struct sigaction *sat;
292
293 switch(sig){
294 case SIGALRM:
295 sat = &sigtable[0];
296 break;
297 case SIGFPE:
298 sat = &sigtable[1];
299 break;
300 case SIGILL:
301 sat = &sigtable[2];
302 break;
303 case SIGSEGV:
304 sat = &sigtable[3];
305 break;
306 case SIGBUS:
307 sat = &sigtable[4];
308 break;
309 case SIGABRT:
310 sat = &sigtable[5];
311 break;
312 case SIGPIPE:
313 sat = &sigtable[6];
314 break;
315 default:
316 return -EINVAL;
317 }
318
319 if (old != NULL)
320 memcpy(old, sat, sizeof(struct sigaction));
321
322 memcpy(sat, sa, sizeof(struct sigaction));
323
324 return 0;
325}
326
327int shell_madvise(void *a, size_t b, int c)
328{
329 return 0;
330}
331
332int shell_gettid()
333{
334 ID tskid;
335 ER ret;
336
337 ret = get_tid(&tskid);
338 if (ret != E_OK)
339 return -1;
340
341 return tskid;
342}
343
344int shell_tkill(int tid, int sig)
345{
346 if ((tid == NTSHELL_TASK) && (sig == SIGABRT)) {
347 shell_abort();
348 }
349
350 no_implement("tkill");
351 return -1;
352}
353
354int shell_kill(int pid, int sig)
355{
356 DebugBreak();
357 return -1;
358}
359
360int shell_gettimeofday(struct timeval *tv, void *tzvp)
361{
362 SYSTIM time;
363 if (!tv) return 0;
364 get_tim(&time);
365 tv->tv_sec = time / 1000000;
366 tv->tv_usec = time - (tv->tv_sec * 1000000);
367 return 0;
368}
369
Note: See TracBrowser for help on using the repository browser.