[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 "main.h"
|
---|
| 53 | #include <tinet_config.h>
|
---|
| 54 | #include <netinet/in.h>
|
---|
| 55 | #include <netinet/in_itron.h>
|
---|
| 56 | #include <tinet_nic_defs.h>
|
---|
| 57 | #include <tinet_cfg.h>
|
---|
| 58 | #include <netinet/in_var.h>
|
---|
| 59 | #include <net/ethernet.h>
|
---|
| 60 | #include <net/if6_var.h>
|
---|
| 61 | #include <net/net.h>
|
---|
| 62 | #include <net/if_var.h>
|
---|
| 63 | #include <netinet/udp_var.h>
|
---|
| 64 | #include "netapp/dhcp4_cli.h"
|
---|
| 65 | #include "ffarch.h"
|
---|
| 66 | #include "ff.h"
|
---|
| 67 | #include "core/ntshell.h"
|
---|
| 68 | #include "core/ntlibc.h"
|
---|
[374] | 69 | #include <stdio.h>
|
---|
[337] | 70 | #include "usrcmd.h"
|
---|
| 71 | #include "util/ntopt.h"
|
---|
| 72 | #include "socket_stub.h"
|
---|
[374] | 73 | #include "gpio_api.h"
|
---|
[337] | 74 | #include "usb_hbth.h"
|
---|
| 75 | #include "ntshell_main.h"
|
---|
| 76 | #include <btstack/utils.h>
|
---|
[340] | 77 | #include "ntp_cli.h"
|
---|
[342] | 78 | #include "netcmd.h"
|
---|
[337] | 79 |
|
---|
| 80 | ID ws_api_mailboxid = MAIN_DATAQUEUE;
|
---|
[364] | 81 | #ifndef NOUSE_MPF_NET_BUF
|
---|
[337] | 82 | ID ws_mempoolid = MPF_NET_BUF_256;
|
---|
[364] | 83 | #endif
|
---|
[337] | 84 |
|
---|
| 85 | #ifdef IF_ETHER_BTUSB
|
---|
| 86 | extern int bt_bnep_mode;
|
---|
[342] | 87 | /* PANU向けリモートアドレス */
|
---|
[337] | 88 | bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x09,0x27,0x26};
|
---|
| 89 | #endif
|
---|
| 90 |
|
---|
| 91 | uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0x07};
|
---|
| 92 | bool_t dhcp_enable = false;
|
---|
| 93 | PRI main_task_priority = MAIN_PRIORITY + 1;
|
---|
| 94 |
|
---|
| 95 | static void netif_link_callback(T_IFNET *ether);
|
---|
| 96 | extern int execute_command(int wait);
|
---|
| 97 |
|
---|
| 98 | enum main_state_t {
|
---|
| 99 | main_state_start,
|
---|
| 100 | main_state_idle,
|
---|
| 101 | };
|
---|
| 102 |
|
---|
| 103 | struct main_t {
|
---|
| 104 | int timer;
|
---|
| 105 | enum main_state_t state;
|
---|
| 106 | SYSTIM prev, now;
|
---|
| 107 | };
|
---|
| 108 | struct main_t main_obj;
|
---|
| 109 |
|
---|
| 110 | static void main_initialize();
|
---|
| 111 | static int main_get_timer();
|
---|
| 112 | static void main_progress(int interval);
|
---|
| 113 | static void main_timeout();
|
---|
| 114 |
|
---|
[364] | 115 | extern int ntshell_exit;
|
---|
| 116 |
|
---|
[337] | 117 | int uart_read(char *buf, int cnt, void *extobj)
|
---|
| 118 | {
|
---|
[364] | 119 | struct main_t *obj = (struct main_t *)extobj;
|
---|
| 120 | int result;
|
---|
| 121 | ER ret;
|
---|
| 122 | int timer;
|
---|
| 123 |
|
---|
| 124 | obj->prev = obj->now;
|
---|
| 125 |
|
---|
| 126 | /* タイマー取得 */
|
---|
| 127 | timer = main_get_timer();
|
---|
| 128 |
|
---|
| 129 | /* 待ち */
|
---|
| 130 | ret = serial_trea_dat(SIO_PORTID, buf, cnt, timer);
|
---|
| 131 | if ((ret < 0) && (ret != E_OK) && (ret != E_TMOUT)) {
|
---|
| 132 | syslog(LOG_NOTICE, "tslp_tsk ret: %s %d", itron_strerror(ret), timer);
|
---|
| 133 | ntshell_exit = 1;
|
---|
| 134 | return -1;
|
---|
| 135 | }
|
---|
| 136 | result = (int)ret;
|
---|
| 137 |
|
---|
| 138 | ret = get_tim(&obj->now);
|
---|
| 139 | if (ret != E_OK) {
|
---|
| 140 | syslog(LOG_NOTICE, "get_tim ret: %s", itron_strerror(ret));
|
---|
| 141 | ntshell_exit = 1;
|
---|
| 142 | return -1;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /* 時間経過 */
|
---|
| 146 | int elapse = obj->now - obj->prev;
|
---|
| 147 | main_progress(elapse);
|
---|
| 148 |
|
---|
| 149 | /* タイムアウト処理 */
|
---|
| 150 | main_timeout();
|
---|
| 151 |
|
---|
| 152 | return result;
|
---|
[337] | 153 | }
|
---|
| 154 |
|
---|
| 155 | int uart_write(const char *buf, int cnt, void *extobj)
|
---|
| 156 | {
|
---|
| 157 | return serial_wri_dat(SIO_PORTID, buf, cnt);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | ntshell_t ntshell;
|
---|
| 161 |
|
---|
| 162 | /*
|
---|
| 163 | * メインタスク
|
---|
| 164 | */
|
---|
| 165 | void main_task(intptr_t exinf)
|
---|
| 166 | {
|
---|
| 167 | #ifdef IF_ETHER_BTUSB
|
---|
| 168 | // PANU mode
|
---|
| 169 | bt_bnep_mode = 0;
|
---|
| 170 | #endif
|
---|
| 171 |
|
---|
| 172 | main_initialize();
|
---|
| 173 |
|
---|
[364] | 174 | ntshell_init(&ntshell, uart_read, uart_write, cmd_execute, &main_obj);
|
---|
[337] | 175 | ntshell_set_prompt(&ntshell, "NTShell>");
|
---|
| 176 | ntshell_execute(&ntshell);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /*
|
---|
| 180 | * 初期化
|
---|
| 181 | */
|
---|
| 182 | static void main_initialize()
|
---|
| 183 | {
|
---|
| 184 | FILINFO fno;
|
---|
[374] | 185 | #if FF_USE_LFN
|
---|
| 186 | char lfn[FF_MAX_LFN + 1];
|
---|
[337] | 187 | fno.lfname = lfn;
|
---|
[374] | 188 | fno.lfsize = FF_MAX_LFN + 1;
|
---|
[337] | 189 | #endif
|
---|
[342] | 190 | ER ret;
|
---|
[337] | 191 |
|
---|
[374] | 192 | ntshell_task_init(SIO_PORTID);
|
---|
[337] | 193 |
|
---|
| 194 | main_obj.timer = TMO_FEVR;
|
---|
| 195 | main_obj.state = main_state_start;
|
---|
| 196 |
|
---|
| 197 | gpio_t led_blue, led_green, led_red, sw;
|
---|
| 198 | gpio_init_out(&led_blue, LED_BLUE);
|
---|
| 199 | gpio_init_out(&led_green, LED_GREEN);
|
---|
| 200 | gpio_init_out(&led_red, LED_RED);
|
---|
| 201 | gpio_init_in(&sw, USER_BUTTON0);
|
---|
| 202 |
|
---|
| 203 | bool_t exec = gpio_read(&sw) == 1;
|
---|
| 204 |
|
---|
| 205 | gpio_write(&led_blue, 1);
|
---|
| 206 | gpio_write(&led_green, exec ? 1 : 0);
|
---|
| 207 | gpio_write(&led_red, 0);
|
---|
| 208 |
|
---|
| 209 | if (!dhcp_enable) {
|
---|
| 210 | dhcp4c_rel_info();
|
---|
| 211 |
|
---|
| 212 | in4_add_ifaddr(IPV4_ADDR_STAIC_LOCAL, IPV4_ADDR_STAIC_LOCAL_MASK);
|
---|
| 213 | uint8_t data[32], c;
|
---|
| 214 | int i, j;
|
---|
| 215 |
|
---|
| 216 | for (i = 0, j = 0; i < sizeof(mac_addr); i++) {
|
---|
| 217 | c = mac_addr[i] >> 4;
|
---|
| 218 | data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
|
---|
| 219 | c = mac_addr[i] & 0xF;
|
---|
| 220 | data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
|
---|
| 221 | data[j++] = ':';
|
---|
| 222 | }
|
---|
| 223 | data[--j] = '\0';
|
---|
| 224 |
|
---|
| 225 | syslog(LOG_NOTICE, "mac_addr %s %s", data, dhcp_enable ? "dhcp" : "static");
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | ether_set_link_callback(netif_link_callback);
|
---|
| 229 |
|
---|
| 230 | /* 初期化 */
|
---|
[359] | 231 | ffarch_init();
|
---|
[337] | 232 |
|
---|
| 233 | gpio_write(&led_green, 0);
|
---|
| 234 |
|
---|
| 235 | #ifdef IF_ETHER_BTUSB
|
---|
| 236 | usbhost_init(USBHOST_TASK);
|
---|
| 237 | #endif
|
---|
| 238 |
|
---|
[342] | 239 | ret = get_tim(&main_obj.now);
|
---|
| 240 | if (ret != E_OK) {
|
---|
[337] | 241 | syslog(LOG_ERROR, "get_tim");
|
---|
| 242 | ext_tsk();
|
---|
| 243 | return;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /*
|
---|
| 248 | * タイマー取得
|
---|
| 249 | */
|
---|
| 250 | static int main_get_timer()
|
---|
| 251 | {
|
---|
| 252 | int timer = main_obj.timer;
|
---|
| 253 |
|
---|
| 254 | return timer;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /*
|
---|
| 258 | * 時間経過
|
---|
| 259 | */
|
---|
| 260 | static void main_progress(int interval)
|
---|
| 261 | {
|
---|
| 262 | if (main_obj.timer != TMO_FEVR) {
|
---|
| 263 | main_obj.timer -= interval;
|
---|
| 264 | if (main_obj.timer < 0) {
|
---|
| 265 | main_obj.timer = 0;
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /*
|
---|
| 271 | * タイムアウト処理
|
---|
| 272 | */
|
---|
| 273 | static void main_timeout()
|
---|
| 274 | {
|
---|
| 275 | //if (main_obj.timer == 0) {
|
---|
| 276 | //}
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /* MACアドレスの設定時に呼ばれる */
|
---|
| 280 | void mbed_mac_address(char *mac)
|
---|
| 281 | {
|
---|
| 282 | memcpy(mac, mac_addr, 6);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | void echonet_change_netif_link(uint8_t link_up, uint8_t up);
|
---|
| 286 |
|
---|
| 287 | static void netif_link_callback(T_IFNET *ether)
|
---|
| 288 | {
|
---|
| 289 | uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
|
---|
| 290 | uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
|
---|
| 291 |
|
---|
| 292 | if (dhcp_enable) {
|
---|
| 293 | if (!link_up)
|
---|
| 294 | dhcp4c_rel_info();
|
---|
| 295 | else if (!up)
|
---|
| 296 | dhcp4c_renew_info();
|
---|
| 297 | }
|
---|
| 298 | else {
|
---|
| 299 | up = link_up;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[340] | 302 | if (link_up && up)
|
---|
| 303 | ntp_cli_execute();
|
---|
| 304 |
|
---|
[337] | 305 | ntshell_change_netif_link(link_up, up);
|
---|
| 306 |
|
---|
| 307 | echonet_change_netif_link(link_up, up);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | static const cmd_table_t cmdlist[] = {
|
---|
| 311 | {"cd", "change directory", usrcmd_cd },
|
---|
| 312 | {"ls", "list files", usrcmd_ls },
|
---|
| 313 | {"cp", "copy file", usrcmd_cp },
|
---|
| 314 | {"rm", "remove file", usrcmd_rm },
|
---|
| 315 | {"mv", "move file", usrcmd_mv },
|
---|
| 316 | {"mkdir", "Make directory", usrcmd_mkdir},
|
---|
| 317 | {"hexdump", "Hex dump", usrcmd_hexdump},
|
---|
[340] | 318 | {"date", "print date and time", usrcmd_date},
|
---|
[337] | 319 | {"ping", "ping", usrcmd_ping},
|
---|
| 320 | {"dhcpc", "DHCP Client rel/renew/info", usrcmd_dhcp4c},
|
---|
[340] | 321 | {"dnsc", "DNS client", usrcmd_dnsc },
|
---|
[341] | 322 | {"ntpc", "NTP client", usrcmd_ntpc },
|
---|
[337] | 323 | {"info", "This is a description text string for info command.", usrcmd_info},
|
---|
| 324 | {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
|
---|
| 325 | };
|
---|
| 326 | cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
|
---|