[352] | 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 <stdlib.h>
|
---|
| 47 | #include <setjmp.h>
|
---|
| 48 | #include <string.h>
|
---|
| 49 | #include "syssvc/serial.h"
|
---|
| 50 | #include "syssvc/syslog.h"
|
---|
| 51 | #include "target_syssvc.h"
|
---|
| 52 | #include "kernel_cfg.h"
|
---|
| 53 | #include "main.h"
|
---|
| 54 | #include "ffarch.h"
|
---|
| 55 | #include "ff.h"
|
---|
| 56 | #include "core/ntshell.h"
|
---|
| 57 | #include "core/ntlibc.h"
|
---|
| 58 | #include "util/ntstdio.h"
|
---|
| 59 | #include "usrcmd.h"
|
---|
| 60 | #include "util/ntopt.h"
|
---|
| 61 | #include "gpio_api.h"
|
---|
| 62 | #include "usb_hbth.h"
|
---|
| 63 | #include "ntshell_main.h"
|
---|
| 64 | #include <btstack/utils.h>
|
---|
| 65 | #include "hal/pinmap.h"
|
---|
| 66 | #include "if_btusb.h"
|
---|
| 67 | #include "if_mbed.h"
|
---|
| 68 |
|
---|
| 69 | ID ws_api_mailboxid = MAIN_DATAQUEUE;
|
---|
| 70 | extern int bt_bnep_mode;
|
---|
| 71 | /* PANU向けリモートアドレス */
|
---|
| 72 | // BUFFALLO
|
---|
| 73 | //bd_addr_t remote_addr = { 0x00,0x1B,0xDC,0x04,0x5C,0x2D };
|
---|
| 74 | // Raspberry Pi 3
|
---|
| 75 | //bd_addr_t remote_addr = {0xB8,0x27,0xEB,0x10,0x6D,0x60};
|
---|
| 76 | // PRINCETON
|
---|
| 77 | //bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x06,0x6E,0x98};
|
---|
| 78 | // ELECOM
|
---|
| 79 | bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x09,0x27,0x26};
|
---|
| 80 |
|
---|
| 81 | /* ネットワークインタフェースに依存しないソフトウェア情報 */
|
---|
| 82 |
|
---|
| 83 | T_IF_SOFTC if_softc = {
|
---|
| 84 | {0,}, /* ネットワークインタフェースのアドレス */
|
---|
| 85 | 0, /* 送信タイムアウト */
|
---|
| 86 | NULL, /* ディバイス依存のソフトウェア情報 */
|
---|
| 87 | NULL, /* ディバイス依存のソフトウェア情報 */
|
---|
| 88 | SEM_IF_MBED_SBUF_READY, /* 送信セマフォ */
|
---|
| 89 | SEM_IF_MBED_RBUF_READY, /* 受信セマフォ */
|
---|
| 90 |
|
---|
| 91 | #ifdef SUPPORT_INET6
|
---|
| 92 |
|
---|
| 93 | IF_MADDR_INIT, /* マルチキャストアドレスリスト */
|
---|
| 94 |
|
---|
| 95 | #endif /* of #ifdef SUPPORT_INET6 */
|
---|
| 96 | };
|
---|
| 97 |
|
---|
| 98 | PRI main_task_priority = MAIN_PRIORITY + 1;
|
---|
| 99 |
|
---|
| 100 | static void netif_link_callback(T_IFNET *ether);
|
---|
| 101 | extern int execute_command(int wait);
|
---|
| 102 |
|
---|
| 103 | enum main_state_t {
|
---|
| 104 | main_state_start,
|
---|
| 105 | main_state_idle,
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | struct main_t {
|
---|
| 109 | TMO timer;
|
---|
| 110 | enum main_state_t state;
|
---|
| 111 | SYSTIM prev, now;
|
---|
| 112 | };
|
---|
| 113 | struct main_t main_obj;
|
---|
| 114 |
|
---|
| 115 | static void main_initialize();
|
---|
| 116 | static TMO main_get_timer();
|
---|
| 117 | static void main_progress(TMO interval);
|
---|
| 118 | static void main_timeout();
|
---|
| 119 |
|
---|
[364] | 120 | extern int ntshell_exit;
|
---|
| 121 |
|
---|
[352] | 122 | int uart_read(char *buf, int cnt, void *extobj)
|
---|
| 123 | {
|
---|
[364] | 124 | struct main_t *obj = (struct main_t *)extobj;
|
---|
| 125 | int result;
|
---|
| 126 | ER ret;
|
---|
| 127 | int timer;
|
---|
| 128 |
|
---|
| 129 | obj->prev = obj->now;
|
---|
| 130 |
|
---|
| 131 | /* タイマー取得 */
|
---|
| 132 | timer = main_get_timer();
|
---|
| 133 |
|
---|
| 134 | /* 待ち */
|
---|
| 135 | ret = serial_trea_dat(SIO_PORTID, buf, cnt, timer);
|
---|
| 136 | if ((ret < 0) && (ret != E_OK) && (ret != E_TMOUT)) {
|
---|
| 137 | syslog(LOG_NOTICE, "tslp_tsk ret: %s %d", itron_strerror(ret), timer);
|
---|
| 138 | ntshell_exit = 1;
|
---|
| 139 | return -1;
|
---|
| 140 | }
|
---|
| 141 | result = (int)ret;
|
---|
| 142 |
|
---|
| 143 | ret = get_tim(&obj->now);
|
---|
| 144 | if (ret != E_OK) {
|
---|
| 145 | syslog(LOG_NOTICE, "get_tim ret: %s", itron_strerror(ret));
|
---|
| 146 | ntshell_exit = 1;
|
---|
| 147 | return -1;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /* 時間経過 */
|
---|
| 151 | int elapse = obj->now - obj->prev;
|
---|
| 152 | main_progress(elapse);
|
---|
| 153 |
|
---|
| 154 | /* タイムアウト処理 */
|
---|
| 155 | main_timeout();
|
---|
| 156 |
|
---|
| 157 | return result;
|
---|
[352] | 158 | }
|
---|
| 159 |
|
---|
| 160 | int uart_write(const char *buf, int cnt, void *extobj)
|
---|
| 161 | {
|
---|
| 162 | return serial_wri_dat(SIO_PORTID, buf, cnt);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | ntshell_t ntshell;
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | * メインタスク
|
---|
| 169 | */
|
---|
| 170 | void main_task(intptr_t exinf)
|
---|
| 171 | {
|
---|
| 172 | // NAP mode
|
---|
| 173 | bt_bnep_mode = 1;
|
---|
| 174 |
|
---|
| 175 | main_initialize();
|
---|
| 176 |
|
---|
[364] | 177 | ntshell_init(&ntshell, uart_read, uart_write, cmd_execute, &main_obj);
|
---|
[352] | 178 | ntshell_set_prompt(&ntshell, "NTShell>");
|
---|
| 179 | ntshell_execute(&ntshell);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /*
|
---|
| 183 | * 初期化
|
---|
| 184 | */
|
---|
| 185 | static void main_initialize()
|
---|
| 186 | {
|
---|
| 187 | FILINFO fno;
|
---|
| 188 | #if _USE_LFN
|
---|
| 189 | char lfn[_MAX_LFN + 1];
|
---|
| 190 | fno.lfname = lfn;
|
---|
| 191 | fno.lfsize = _MAX_LFN + 1;
|
---|
| 192 | #endif
|
---|
| 193 | ER ret;
|
---|
| 194 |
|
---|
[364] | 195 | #ifdef TOPPERS_OMIT_TECS
|
---|
| 196 | serial_opn_por(SIO_PORTID);
|
---|
| 197 | #endif
|
---|
| 198 | serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
|
---|
[352] | 199 |
|
---|
[364] | 200 | ntshell_task_init(uart_read, uart_write, &main_obj);
|
---|
| 201 |
|
---|
[352] | 202 | main_obj.timer = TMO_FEVR;
|
---|
| 203 | main_obj.state = main_state_start;
|
---|
| 204 |
|
---|
| 205 | gpio_t led_blue, led_green, led_red, sw;
|
---|
| 206 | gpio_init_out(&led_blue, LED_BLUE);
|
---|
| 207 | gpio_init_out(&led_green, LED_GREEN);
|
---|
| 208 | gpio_init_out(&led_red, LED_RED);
|
---|
| 209 | gpio_init_in(&sw, USER_BUTTON0);
|
---|
| 210 |
|
---|
| 211 | bool_t exec = gpio_read(&sw) == 1;
|
---|
| 212 |
|
---|
| 213 | gpio_write(&led_blue, 1);
|
---|
| 214 | gpio_write(&led_green, exec ? 1 : 0);
|
---|
| 215 | gpio_write(&led_red, 0);
|
---|
| 216 |
|
---|
| 217 | ether_set_link_callback(netif_link_callback);
|
---|
| 218 |
|
---|
| 219 | /* 初期化 */
|
---|
[359] | 220 | ffarch_init();
|
---|
[352] | 221 |
|
---|
| 222 | gpio_write(&led_green, 0);
|
---|
| 223 |
|
---|
| 224 | usbhost_init(USBHOST_TASK);
|
---|
| 225 |
|
---|
| 226 | ret = get_tim(&main_obj.now);
|
---|
| 227 | if (ret != E_OK) {
|
---|
| 228 | syslog(LOG_ERROR, "get_tim");
|
---|
| 229 | ext_tsk();
|
---|
| 230 | return;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /*
|
---|
| 235 | * タイマー取得
|
---|
| 236 | */
|
---|
| 237 | static TMO main_get_timer()
|
---|
| 238 | {
|
---|
| 239 | TMO timer = main_obj.timer;
|
---|
| 240 |
|
---|
| 241 | return timer;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /*
|
---|
| 245 | * 時間経過
|
---|
| 246 | */
|
---|
| 247 | static void main_progress(TMO interval)
|
---|
| 248 | {
|
---|
| 249 | if (main_obj.timer != TMO_FEVR) {
|
---|
| 250 | main_obj.timer -= interval;
|
---|
| 251 | if (main_obj.timer < 0) {
|
---|
| 252 | main_obj.timer = 0;
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /*
|
---|
| 258 | * タイムアウト処理
|
---|
| 259 | */
|
---|
| 260 | static void main_timeout()
|
---|
| 261 | {
|
---|
| 262 | //if (main_obj.timer == 0) {
|
---|
| 263 | //}
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | void ntshell_change_netif_link(uint8_t link_up, uint8_t up);
|
---|
| 267 |
|
---|
| 268 | static void netif_link_callback(T_IFNET *ether)
|
---|
| 269 | {
|
---|
| 270 | uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
|
---|
| 271 | uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
|
---|
| 272 |
|
---|
| 273 | ntshell_change_netif_link(link_up, up);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | static const cmd_table_t cmdlist[] = {
|
---|
| 277 | {"cd", "change directory", usrcmd_cd },
|
---|
| 278 | {"ls", "list files", usrcmd_ls },
|
---|
| 279 | {"cp", "copy file", usrcmd_cp },
|
---|
| 280 | {"rm", "remove file", usrcmd_rm },
|
---|
| 281 | {"mv", "move file", usrcmd_mv },
|
---|
| 282 | {"mkdir", "Make directory", usrcmd_mkdir},
|
---|
| 283 | {"hexdump", "Hex dump", usrcmd_hexdump},
|
---|
| 284 | {"date", "print date and time", usrcmd_date},
|
---|
| 285 | {"info", "This is a description text string for info command.", usrcmd_info},
|
---|
| 286 | {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
|
---|
| 287 | };
|
---|
| 288 | cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
|
---|
| 289 |
|
---|
| 290 | static T_IFNET ether_ifnet;
|
---|
| 291 |
|
---|
| 292 | T_IFNET *ether_get_ifnet()
|
---|
| 293 | {
|
---|
| 294 | return ðer_ifnet;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /**
|
---|
| 298 | * Called by a driver when its link goes up
|
---|
| 299 | */
|
---|
| 300 | void ether_set_link_up(T_IF_SOFTC *ic)
|
---|
| 301 | {
|
---|
| 302 | if (!(ether_ifnet.flags & IF_FLAG_LINK_UP)) {
|
---|
| 303 | ether_ifnet.flags |= IF_FLAG_LINK_UP;
|
---|
| 304 |
|
---|
| 305 | if (ether_ifnet.flags & IF_FLAG_UP) {
|
---|
| 306 | #if LWIP_ARP
|
---|
| 307 | /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
|
---|
| 308 | if (ether_ifnet.flags & IF_FLAG_ETHARP) {
|
---|
| 309 | etharp_gratuitous(ðer_ifnet);
|
---|
| 310 | }
|
---|
| 311 | #endif /* LWIP_ARP */
|
---|
| 312 |
|
---|
| 313 | #if LWIP_IGMP
|
---|
| 314 | /* resend IGMP memberships */
|
---|
| 315 | if (ether_ifnet._flags & IF_FLAG_IGMP) {
|
---|
| 316 | igmp_report_groups(ðer_ifnet);
|
---|
| 317 | }
|
---|
| 318 | #endif /* LWIP_IGMP */
|
---|
| 319 | }
|
---|
| 320 | if (ether_ifnet.link_callback) {
|
---|
| 321 | (ether_ifnet.link_callback)(ðer_ifnet);
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | /**
|
---|
| 327 | * Called by a driver when its link goes down
|
---|
| 328 | */
|
---|
| 329 | void ether_set_link_down(T_IF_SOFTC *ic)
|
---|
| 330 | {
|
---|
| 331 | if (ether_ifnet.flags & IF_FLAG_LINK_UP) {
|
---|
| 332 | ether_ifnet.flags &= ~IF_FLAG_LINK_UP;
|
---|
| 333 | if (ether_ifnet.link_callback) {
|
---|
| 334 | (ether_ifnet.link_callback)(ðer_ifnet);
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | /**
|
---|
| 340 | * Set callback to be called when link is brought up/down
|
---|
| 341 | */
|
---|
| 342 | void ether_set_link_callback(ether_status_callback_fn link_callback)
|
---|
| 343 | {
|
---|
| 344 | ether_ifnet.link_callback = link_callback;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | /*
|
---|
| 348 | * Ethernet 入力タスク
|
---|
| 349 | */
|
---|
| 350 |
|
---|
| 351 | void
|
---|
| 352 | ether_input_task(intptr_t exinf)
|
---|
| 353 | {
|
---|
| 354 | T_IF_SOFTC *ic = &if_softc;
|
---|
| 355 | T_NET_BUF *input;
|
---|
| 356 | ER ret;
|
---|
| 357 |
|
---|
| 358 | btusb_probe(ic);
|
---|
| 359 | btusb_init(ic);
|
---|
| 360 |
|
---|
| 361 | if_mbed_probe(ic);
|
---|
| 362 | if_mbed_init(ic);
|
---|
| 363 |
|
---|
| 364 | while (true) {
|
---|
| 365 | ret = wai_sem(ic->semid_rxb_ready);
|
---|
| 366 | if (ret != E_OK) {
|
---|
| 367 | syslog(LOG_DEBUG, "wai_sem(ic->semid_rxb_ready) = %d", ret);
|
---|
| 368 | return;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | if ((input = if_mbed_read(ic)) != NULL) {
|
---|
| 372 | int rel = btusb_start(ic, input);
|
---|
| 373 | if (rel) {
|
---|
| 374 | rel_net_buf(input);
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | ER tget_net_buf (T_NET_BUF **blk, uint_t len, TMO tmout)
|
---|
| 381 | {
|
---|
| 382 | T_NET_BUF *buf;
|
---|
| 383 |
|
---|
| 384 | buf = malloc(sizeof(T_NET_BUF) + len);
|
---|
| 385 | *blk = buf;
|
---|
| 386 | if (buf == NULL)
|
---|
| 387 | return E_TMOUT;
|
---|
| 388 |
|
---|
| 389 | buf->len = len;
|
---|
| 390 | buf->idix = 0;
|
---|
| 391 | buf->flags = 0;
|
---|
| 392 |
|
---|
| 393 | return E_OK;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | ER rel_net_buf (T_NET_BUF *blk)
|
---|
| 397 | {
|
---|
| 398 | free(blk);
|
---|
| 399 |
|
---|
| 400 | return E_OK;
|
---|
| 401 | }
|
---|