[388] | 1 | /*
|
---|
| 2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 2014-2019 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: 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 <stdlib.h>
|
---|
| 64 | #include <stdbool.h>
|
---|
| 65 | #include "syssvc/serial.h"
|
---|
| 66 | #include "syssvc/syslog.h"
|
---|
| 67 | #include "target_syssvc.h"
|
---|
| 68 | #include "kernel_cfg.h"
|
---|
| 69 | #include "main.h"
|
---|
| 70 | #include <tinet_config.h>
|
---|
| 71 | #include <netinet/in.h>
|
---|
| 72 | #include <netinet/in_itron.h>
|
---|
| 73 | #include <tinet_nic_defs.h>
|
---|
| 74 | #include <tinet_cfg.h>
|
---|
| 75 | #include <netinet/in_var.h>
|
---|
| 76 | #include <net/ethernet.h>
|
---|
| 77 | #include <net/if6_var.h>
|
---|
| 78 | #include <net/net.h>
|
---|
| 79 | #include <net/if_var.h>
|
---|
| 80 | #include <netinet/udp_var.h>
|
---|
| 81 | #include "netapp/dhcp4_cli.h"
|
---|
| 82 | #include "ffarch.h"
|
---|
| 83 | #include "ff.h"
|
---|
| 84 | #include <stdio.h>
|
---|
| 85 | #include "usrcmd.h"
|
---|
| 86 | #include "fdtable.h"
|
---|
| 87 | #include "gpio_api.h"
|
---|
| 88 | #include "usb_hbth.h"
|
---|
| 89 | #include "ntshell_main.h"
|
---|
| 90 | #ifdef IF_ETHER_BTUSB
|
---|
| 91 | #include <btstack/utils.h>
|
---|
| 92 | #endif
|
---|
| 93 | #include "ntp_cli.h"
|
---|
| 94 | #include "netcmd.h"
|
---|
| 95 | #include "adafruit_ssd1306.h"
|
---|
| 96 | #include "draw_font.h"
|
---|
| 97 |
|
---|
| 98 | ID ws_api_mailboxid = MAIN_DATAQUEUE;
|
---|
| 99 | #ifndef NOUSE_MPF_NET_BUF
|
---|
| 100 | ID ws_mempoolid = MPF_NET_BUF_256;
|
---|
| 101 | #endif
|
---|
| 102 |
|
---|
| 103 | #ifdef IF_ETHER_BTUSB
|
---|
| 104 | extern int bt_bnep_mode;
|
---|
| 105 | /* PANUåããªã¢ã¼ãã¢ãã¬ã¹ */
|
---|
| 106 | bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x09,0x27,0x26};
|
---|
| 107 | bool_t dhcp_enable = false;
|
---|
| 108 | #else
|
---|
| 109 | bool_t dhcp_enable = true;
|
---|
| 110 | #endif
|
---|
| 111 |
|
---|
| 112 | uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0x01};
|
---|
| 113 | PRI main_task_priority = MAIN_PRIORITY + 1;
|
---|
| 114 |
|
---|
| 115 | static void netif_link_callback(T_IFNET *ether);
|
---|
| 116 | extern int execute_command(int wait);
|
---|
| 117 |
|
---|
| 118 | enum main_state_t {
|
---|
| 119 | main_state_start,
|
---|
| 120 | main_state_idle,
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | struct main_t {
|
---|
| 124 | int timer;
|
---|
| 125 | enum main_state_t state;
|
---|
| 126 | SYSTIM prev, now;
|
---|
| 127 | };
|
---|
| 128 | struct main_t main_obj;
|
---|
| 129 |
|
---|
| 130 | static void main_initialize();
|
---|
| 131 | static int main_get_timer();
|
---|
| 132 | static void main_progress(int interval);
|
---|
| 133 | static void main_timeout();
|
---|
| 134 |
|
---|
| 135 | extern int ntshell_exit;
|
---|
| 136 |
|
---|
| 137 | int uart_read(char *buf, int cnt, void *extobj)
|
---|
| 138 | {
|
---|
| 139 | struct main_t *obj = (struct main_t *)extobj;
|
---|
| 140 | int result;
|
---|
| 141 | ER ret;
|
---|
| 142 | int timer;
|
---|
| 143 |
|
---|
| 144 | obj->prev = obj->now;
|
---|
| 145 |
|
---|
| 146 | /* ã¿ã¤ãã¼åå¾ */
|
---|
| 147 | timer = main_get_timer();
|
---|
| 148 |
|
---|
| 149 | /* å¾
|
---|
| 150 | ã¡ */
|
---|
| 151 | ret = serial_trea_dat(SIO_PORTID, buf, cnt, timer);
|
---|
| 152 | if ((ret < 0) && (ret != E_OK) && (ret != E_TMOUT)) {
|
---|
| 153 | syslog(LOG_NOTICE, "tslp_tsk ret: %s %d", itron_strerror(ret), timer);
|
---|
| 154 | ntshell_exit = 1;
|
---|
| 155 | return -1;
|
---|
| 156 | }
|
---|
| 157 | result = (int)ret;
|
---|
| 158 |
|
---|
| 159 | ret = get_tim(&obj->now);
|
---|
| 160 | if (ret != E_OK) {
|
---|
| 161 | syslog(LOG_NOTICE, "get_tim ret: %s", itron_strerror(ret));
|
---|
| 162 | ntshell_exit = 1;
|
---|
| 163 | return -1;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /* æéçµé */
|
---|
| 167 | int elapse = obj->now - obj->prev;
|
---|
| 168 | main_progress(elapse);
|
---|
| 169 |
|
---|
| 170 | /* ã¿ã¤ã ã¢ã¦ãå¦ç */
|
---|
| 171 | main_timeout();
|
---|
| 172 |
|
---|
| 173 | return result;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | int uart_write(const char *buf, int cnt, void *extobj)
|
---|
| 177 | {
|
---|
| 178 | return serial_wri_dat(SIO_PORTID, buf, cnt);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | ntshell_t ntshell;
|
---|
| 182 |
|
---|
| 183 | /*
|
---|
| 184 | * ã¡ã¤ã³ã¿ã¹ã¯
|
---|
| 185 | */
|
---|
| 186 | void main_task(intptr_t exinf)
|
---|
| 187 | {
|
---|
| 188 | #ifdef IF_ETHER_BTUSB
|
---|
| 189 | // PANU mode
|
---|
| 190 | bt_bnep_mode = 0;
|
---|
| 191 | #endif
|
---|
| 192 |
|
---|
| 193 | main_initialize();
|
---|
| 194 |
|
---|
| 195 | ntshell_init(&ntshell, uart_read, uart_write, cmd_execute, &main_obj);
|
---|
| 196 | ntshell_set_prompt(&ntshell, "NTShell>");
|
---|
| 197 | ntshell_execute(&ntshell);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | #if !defined(_MSC_VER)
|
---|
| 201 | LCD_Handler_t lcd;
|
---|
| 202 | #endif
|
---|
| 203 |
|
---|
| 204 | int wolfSSL_Debugging_ON(void);
|
---|
| 205 |
|
---|
| 206 | /*
|
---|
| 207 | * åæå
|
---|
| 208 | */
|
---|
| 209 | static void main_initialize()
|
---|
| 210 | {
|
---|
| 211 | FILINFO fno;
|
---|
| 212 | #if FF_USE_LFN
|
---|
| 213 | char lfn[FF_MAX_LFN + 1];
|
---|
| 214 | fno.lfname = lfn;
|
---|
| 215 | fno.lfsize = FF_MAX_LFN + 1;
|
---|
| 216 | #endif
|
---|
| 217 | ER ret;
|
---|
| 218 |
|
---|
| 219 | //wolfSSL_Debugging_ON();
|
---|
| 220 |
|
---|
| 221 | ntshell_task_init(SIO_PORTID);
|
---|
| 222 |
|
---|
| 223 | main_obj.timer = TMO_FEVR;
|
---|
| 224 | main_obj.state = main_state_start;
|
---|
| 225 |
|
---|
| 226 | gpio_t led_blue, led_green, led_red, sw;
|
---|
| 227 | gpio_init_out(&led_blue, LED_BLUE);
|
---|
| 228 | gpio_init_out(&led_green, LED_GREEN);
|
---|
| 229 | gpio_init_out(&led_red, LED_RED);
|
---|
| 230 | gpio_init_in(&sw, USER_BUTTON0);
|
---|
| 231 |
|
---|
| 232 | bool_t exec = gpio_read(&sw) == 1;
|
---|
| 233 |
|
---|
| 234 | gpio_write(&led_blue, 1);
|
---|
| 235 | gpio_write(&led_green, exec ? 1 : 0);
|
---|
| 236 | gpio_write(&led_red, 0);
|
---|
| 237 |
|
---|
| 238 | if (!dhcp_enable) {
|
---|
| 239 | dhcp4c_rel_info();
|
---|
| 240 |
|
---|
| 241 | in4_add_ifaddr(IPV4_ADDR_STAIC_LOCAL, IPV4_ADDR_STAIC_LOCAL_MASK);
|
---|
| 242 | uint8_t data[32], c;
|
---|
| 243 | int i, j;
|
---|
| 244 |
|
---|
| 245 | for (i = 0, j = 0; i < sizeof(mac_addr); i++) {
|
---|
| 246 | c = mac_addr[i] >> 4;
|
---|
| 247 | data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
|
---|
| 248 | c = mac_addr[i] & 0xF;
|
---|
| 249 | data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
|
---|
| 250 | data[j++] = ':';
|
---|
| 251 | }
|
---|
| 252 | data[--j] = '\0';
|
---|
| 253 |
|
---|
| 254 | syslog(LOG_NOTICE, "mac_addr %s %s", data, dhcp_enable ? "dhcp" : "static");
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | ether_set_link_callback(netif_link_callback);
|
---|
| 258 |
|
---|
| 259 | /* åæå */
|
---|
| 260 | ffarch_init();
|
---|
| 261 |
|
---|
| 262 | gpio_write(&led_green, 0);
|
---|
| 263 | #if !defined(_MSC_VER)
|
---|
| 264 | i2c_init(&lcd.i2c, I2C_SDA, I2C_SCL);
|
---|
| 265 | i2c_frequency(&lcd.i2c, 100000);
|
---|
| 266 |
|
---|
| 267 | lcd_init3(&lcd, NC);
|
---|
| 268 |
|
---|
| 269 | lcd_begin(&lcd, SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS, true);
|
---|
| 270 |
|
---|
| 271 | lcd_clearDisplay(&lcd);
|
---|
| 272 |
|
---|
| 273 | lcd_display(&lcd);
|
---|
| 274 |
|
---|
| 275 | lcd_drawString(&lcd, "OLED表示åæåå®äº!", 0, 0, WHITE, BLACK);
|
---|
| 276 |
|
---|
| 277 | lcd_display(&lcd);
|
---|
| 278 | #endif
|
---|
| 279 | #ifdef IF_ETHER_BTUSB
|
---|
| 280 | usbhost_init(USBHOST_TASK);
|
---|
| 281 | #endif
|
---|
| 282 |
|
---|
| 283 | if (f_stat("1:/upload/main.mrb", &fno) != FR_OK) {
|
---|
| 284 | exec = false;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | if (exec) {
|
---|
| 288 | cmd_execute("mruby -b 1:/upload/main.mrb", NULL);
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | ret = get_tim(&main_obj.now);
|
---|
| 292 | if (ret != E_OK) {
|
---|
| 293 | syslog(LOG_ERROR, "get_tim");
|
---|
| 294 | ext_tsk();
|
---|
| 295 | return;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /*
|
---|
| 300 | * ã¿ã¤ãã¼åå¾
|
---|
| 301 | */
|
---|
| 302 | static int main_get_timer()
|
---|
| 303 | {
|
---|
| 304 | int timer = main_obj.timer;
|
---|
| 305 |
|
---|
| 306 | return timer;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /*
|
---|
| 310 | * æéçµé
|
---|
| 311 | */
|
---|
| 312 | static void main_progress(int interval)
|
---|
| 313 | {
|
---|
| 314 | if (main_obj.timer != TMO_FEVR) {
|
---|
| 315 | main_obj.timer -= interval;
|
---|
| 316 | if (main_obj.timer < 0) {
|
---|
| 317 | main_obj.timer = 0;
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | /*
|
---|
| 323 | * ã¿ã¤ã ã¢ã¦ãå¦ç
|
---|
| 324 | */
|
---|
| 325 | static void main_timeout()
|
---|
| 326 | {
|
---|
| 327 | //if (main_obj.timer == 0) {
|
---|
| 328 | //}
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /* MACã¢ãã¬ã¹ã®è¨å®æã«å¼ã°ãã */
|
---|
| 332 | void mbed_mac_address(char *mac)
|
---|
| 333 | {
|
---|
| 334 | memcpy(mac, mac_addr, 6);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | static void netif_link_callback(T_IFNET *ether)
|
---|
| 338 | {
|
---|
| 339 | uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
|
---|
| 340 | uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
|
---|
| 341 |
|
---|
| 342 | if (dhcp_enable) {
|
---|
| 343 | if (!link_up)
|
---|
| 344 | dhcp4c_rel_info();
|
---|
| 345 | else if (!up)
|
---|
| 346 | dhcp4c_renew_info();
|
---|
| 347 | }
|
---|
| 348 | else {
|
---|
| 349 | up = link_up;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | if (link_up && up)
|
---|
| 353 | ntp_cli_execute();
|
---|
| 354 |
|
---|
| 355 | ntshell_change_netif_link(link_up, up);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | int custom_rand_generate_seed(uint8_t* output, int32_t sz)
|
---|
| 359 | {
|
---|
| 360 | SYSTIM now;
|
---|
| 361 | int32_t i;
|
---|
| 362 |
|
---|
| 363 | get_tim(&now);
|
---|
| 364 | srand(now);
|
---|
| 365 |
|
---|
| 366 | for (i = 0; i < sz; i++)
|
---|
| 367 | output[i] = rand();
|
---|
| 368 |
|
---|
| 369 | return 0;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | //extern int curl_main(int argc, char **argv);
|
---|
| 373 | extern int iothub_client_main(int argc, char **argv);
|
---|
| 374 |
|
---|
| 375 | static const cmd_table_t cmdlist[] = {
|
---|
| 376 | {"cd", "change directory", usrcmd_cd },
|
---|
| 377 | {"ls", "list files", usrcmd_ls },
|
---|
| 378 | {"cp", "copy file", usrcmd_cp },
|
---|
| 379 | {"rm", "remove file", usrcmd_rm },
|
---|
| 380 | {"mv", "move file", usrcmd_mv },
|
---|
| 381 | {"mkdir", "Make directory", usrcmd_mkdir},
|
---|
| 382 | {"hexdump", "Hex dump", usrcmd_hexdump},
|
---|
| 383 | {"date", "print date and time", usrcmd_date},
|
---|
| 384 | // {"curl", "Command lines or scripts to transfer data", curl_main},
|
---|
| 385 | {"ping", "ping", usrcmd_ping},
|
---|
| 386 | {"dhcpc", "DHCP Client rel/renew/info", usrcmd_dhcp4c},
|
---|
| 387 | {"dnsc", "DNS client", usrcmd_dnsc },
|
---|
| 388 | {"ntpc", "NTP client", usrcmd_ntpc },
|
---|
| 389 | {"iothub", "Asure IoT Hub Client", iothub_client_main},
|
---|
| 390 | {"info", "This is a description text string for info command.", usrcmd_info},
|
---|
| 391 | {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
|
---|
| 392 | };
|
---|
| 393 | cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
|
---|