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