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