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