[337] | 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;
|
---|
[340] | 127 | int result = 0;
|
---|
| 128 | int found = 0;
|
---|
| 129 |
|
---|
[337] | 130 | if (strcmp((const char *)args[1], "help") == 0) {
|
---|
[340] | 131 | found = 1;
|
---|
| 132 | result = usrcmd_help(args[0], (char **)&args[1]);
|
---|
[337] | 133 | }
|
---|
| 134 | else for (int i = 0; i < cmd_table_info.count; i++) {
|
---|
| 135 | if (strcmp((const char *)args[1], p->cmd) == 0) {
|
---|
[340] | 136 | found = 1;
|
---|
| 137 | result = p->func(args[0], (char **)&args[1]);
|
---|
| 138 | break;
|
---|
[337] | 139 | }
|
---|
| 140 | p++;
|
---|
| 141 | }
|
---|
[340] | 142 |
|
---|
| 143 | if ((found == 0) && (((const char *)args[1])[0] != '\0'))
|
---|
| 144 | printf("Unknown command found.\n");
|
---|
| 145 |
|
---|
[337] | 146 | clean_fd();
|
---|
[340] | 147 |
|
---|
| 148 | return result;
|
---|
[337] | 149 | }
|
---|
| 150 |
|
---|
| 151 | int usrcmd_help(int argc, char **argv)
|
---|
| 152 | {
|
---|
| 153 | const cmd_table_t *p = cmd_table_info.table;
|
---|
| 154 | for (int i = 0; i < cmd_table_info.count; i++) {
|
---|
| 155 | ntstdio_puts(&ntstdio, p->cmd);
|
---|
| 156 | ntstdio_puts(&ntstdio, "\t:");
|
---|
| 157 | ntstdio_puts(&ntstdio, p->desc);
|
---|
| 158 | ntstdio_puts(&ntstdio, "\n");
|
---|
| 159 | p++;
|
---|
| 160 | }
|
---|
| 161 | return 0;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | void shellif_into()
|
---|
| 165 | {
|
---|
| 166 | /* メインタスクの優先度より高くする */
|
---|
| 167 | chg_pri(NTSHELL_PRIORITY, main_task_priority);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | void shellif_outof()
|
---|
| 171 | {
|
---|
| 172 | /* ntshellタスクの優先度に戻す */
|
---|
| 173 | chg_pri(NTSHELL_PRIORITY, NTSHELL_PRIORITY);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void shell_abort()
|
---|
| 177 | {
|
---|
| 178 | ntshell_exit_code = -1;
|
---|
| 179 | longjmp(process_exit, 1);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void shell_exit(int exitcd)
|
---|
| 183 | {
|
---|
| 184 | ntshell_exit_code = exitcd;
|
---|
| 185 | longjmp(process_exit, 1);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | void shell_exit_group(int exitcd)
|
---|
| 189 | {
|
---|
| 190 | ntshell_exit_code = exitcd;
|
---|
| 191 | longjmp(process_exit, 1);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | int execute_command(int wait)
|
---|
| 195 | {
|
---|
| 196 | ER ret;
|
---|
| 197 |
|
---|
| 198 | ret = ter_tsk(NTSHELL_TASK);
|
---|
| 199 | if ((ret != E_OK) && (ret != E_OBJ)) {
|
---|
| 200 | syslog(LOG_ERROR, "ter_tsk => %d", ret);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | tslp_tsk(100000);
|
---|
| 204 |
|
---|
| 205 | clean_fd();
|
---|
| 206 |
|
---|
| 207 | ntshell_state = 0;
|
---|
| 208 | ret = act_tsk(NTSHELL_TASK);
|
---|
| 209 | if (ret != E_OK) {
|
---|
| 210 | syslog(LOG_ERROR, "act_tsk => %d", ret);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | if (wait == 0)
|
---|
| 214 | return 0;
|
---|
| 215 |
|
---|
| 216 | do {
|
---|
| 217 | tslp_tsk(100000);
|
---|
| 218 | } while(ntshell_state == 1);
|
---|
| 219 |
|
---|
| 220 | return ntshell_exit_code;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | int cmd_execute(const char *text, void *extobj)
|
---|
| 224 | {
|
---|
| 225 | ntlibc_strlcpy(command, text, sizeof(command));
|
---|
| 226 | return execute_command(1);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | int stdio_close(struct _IO_FILE *fp)
|
---|
| 230 | {
|
---|
| 231 | return -EPERM;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | size_t stdio_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
| 235 | {
|
---|
| 236 | return -EPERM;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | size_t stdio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
| 240 | {
|
---|
| 241 | return -EPERM;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | size_t stdin_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
| 245 | {
|
---|
| 246 | int i = 0;
|
---|
| 247 | while (i < len) {
|
---|
| 248 | int c = ntstdio_getc(&ntstdio);
|
---|
| 249 | data[i++] = c;
|
---|
| 250 | if ((c == EOF) || (c == '\n'))
|
---|
| 251 | break;
|
---|
| 252 | }
|
---|
| 253 | return i;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | size_t stdout_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 | size_t stderr_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
| 265 | {
|
---|
| 266 | for (int i = 0; i < len; i++) {
|
---|
| 267 | ntstdio_putc(&ntstdio, data[i]);
|
---|
| 268 | }
|
---|
| 269 | return len;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | int sio_close(struct _IO_FILE *fp)
|
---|
| 273 | {
|
---|
| 274 | return -EPERM;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | size_t sio_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
|
---|
| 278 | {
|
---|
| 279 | return -EPERM;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | size_t sio_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
|
---|
| 283 | {
|
---|
| 284 | return -EPERM;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | off_t sio_seek(struct _IO_FILE *fp, off_t ofs, int org)
|
---|
| 288 | {
|
---|
| 289 | return -EPERM;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | int sio_ioctl(struct _IO_FILE *fp, int request, void *arg)
|
---|
| 293 | {
|
---|
| 294 | switch (request) {
|
---|
| 295 | case TIOCGWINSZ:
|
---|
| 296 | return 0;
|
---|
| 297 | case TCGETS:
|
---|
| 298 | return sio_tcgetattr(fp->fd, (struct termios *)arg);
|
---|
| 299 | case TCSETS + TCSANOW:
|
---|
| 300 | case TCSETS + TCSADRAIN:
|
---|
| 301 | case TCSETS + TCSAFLUSH:
|
---|
| 302 | return sio_tcsetattr(fp->fd, request - TCSETS, (const struct termios *)arg);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | return -EINVAL;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | int shell_clock_getres(clockid_t clk_id, struct timespec *res)
|
---|
| 309 | {
|
---|
| 310 | if (clk_id != CLOCK_REALTIME)
|
---|
| 311 | return -EINVAL;
|
---|
| 312 |
|
---|
| 313 | memset(&res->tv_sec, 0xFF, sizeof(res->tv_sec));
|
---|
| 314 | res->tv_nsec = 0;
|
---|
| 315 |
|
---|
| 316 | return 0;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | int shell_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
---|
| 320 | {
|
---|
| 321 | SYSTIM now = 0;
|
---|
| 322 |
|
---|
| 323 | if (clk_id != CLOCK_REALTIME)
|
---|
| 324 | return -EINVAL;
|
---|
| 325 |
|
---|
| 326 | get_tim(&now);
|
---|
| 327 | tp->tv_sec = now / 1000000;
|
---|
| 328 | tp->tv_nsec = (now % 1000000) * 1000;
|
---|
| 329 |
|
---|
| 330 | return 0;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
|
---|
| 334 | {
|
---|
| 335 | if (clk_id != CLOCK_REALTIME)
|
---|
| 336 | return -EINVAL;
|
---|
| 337 |
|
---|
| 338 | rtc_write(tp->tv_sec);
|
---|
| 339 |
|
---|
| 340 | return 0;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | sigset_t g_sigmask;
|
---|
| 344 |
|
---|
| 345 | int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
|
---|
| 346 | {
|
---|
| 347 | if (old != NULL)
|
---|
| 348 | memcpy(old, &g_sigmask, sizeof(sigset_t));
|
---|
| 349 |
|
---|
| 350 | switch (how) {
|
---|
| 351 | case SIG_BLOCK:
|
---|
| 352 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
| 353 | g_sigmask.__bits[i] |= set->__bits[i];
|
---|
| 354 | }
|
---|
| 355 | break;
|
---|
| 356 | case SIG_UNBLOCK:
|
---|
| 357 | for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
|
---|
| 358 | g_sigmask.__bits[i] &= ~set->__bits[i];
|
---|
| 359 | }
|
---|
| 360 | break;
|
---|
| 361 | case SIG_SETMASK:
|
---|
| 362 | memcpy(&g_sigmask, set, sizeof(sigset_t));
|
---|
| 363 | break;
|
---|
| 364 | default:
|
---|
| 365 | return -EINVAL;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | return 0;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | struct sigaction sigtable[6];
|
---|
| 372 |
|
---|
| 373 | int shell_sigaction(int sig, const struct sigaction *restrict sa, struct sigaction *restrict old)
|
---|
| 374 | {
|
---|
| 375 | struct sigaction *sat;
|
---|
| 376 |
|
---|
| 377 | switch(sig){
|
---|
| 378 | case SIGALRM:
|
---|
| 379 | sat = &sigtable[0];
|
---|
| 380 | break;
|
---|
| 381 | case SIGFPE:
|
---|
| 382 | sat = &sigtable[1];
|
---|
| 383 | break;
|
---|
| 384 | case SIGILL:
|
---|
| 385 | sat = &sigtable[2];
|
---|
| 386 | break;
|
---|
| 387 | case SIGSEGV:
|
---|
| 388 | sat = &sigtable[3];
|
---|
| 389 | break;
|
---|
| 390 | case SIGBUS:
|
---|
| 391 | sat = &sigtable[4];
|
---|
| 392 | break;
|
---|
| 393 | case SIGABRT:
|
---|
| 394 | sat = &sigtable[5];
|
---|
| 395 | break;
|
---|
| 396 | default:
|
---|
| 397 | return -EINVAL;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | if (old != NULL)
|
---|
| 401 | memcpy(old, sat, sizeof(struct sigaction));
|
---|
| 402 |
|
---|
| 403 | memcpy(sat, sa, sizeof(struct sigaction));
|
---|
| 404 |
|
---|
| 405 | return 0;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | int shell_madvise(void *a, size_t b, int c)
|
---|
| 409 | {
|
---|
| 410 | return 0;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | int shell_gettid()
|
---|
| 414 | {
|
---|
| 415 | ID tskid;
|
---|
| 416 | ER ret;
|
---|
| 417 |
|
---|
| 418 | ret = get_tid(&tskid);
|
---|
| 419 | if (ret != E_OK)
|
---|
| 420 | return -1;
|
---|
| 421 |
|
---|
| 422 | return tskid;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | int shell_tkill(int tid, int sig)
|
---|
| 426 | {
|
---|
| 427 | if ((tid == NTSHELL_TASK) && (sig == SIGABRT)) {
|
---|
| 428 | shell_abort();
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | no_implement("tkill");
|
---|
| 432 | return -1;
|
---|
| 433 | }
|
---|
| 434 |
|
---|