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