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 "ntp_cli.h"
|
---|
78 |
|
---|
79 | ID ws_api_mailboxid = MAIN_DATAQUEUE;
|
---|
80 | ID ws_mempoolid = MPF_NET_BUF_256;
|
---|
81 |
|
---|
82 | #ifdef IF_ETHER_BTUSB
|
---|
83 | extern int bt_bnep_mode;
|
---|
84 |
|
---|
85 | // BUFFALLO
|
---|
86 | //bd_addr_t remote_addr = { 0x00,0x1B,0xDC,0x04,0x5C,0x2D };
|
---|
87 | // Raspberry Pi 3
|
---|
88 | //bd_addr_t remote_addr = {0xB8,0x27,0xEB,0x10,0x6D,0x60};
|
---|
89 | // PRINCETON
|
---|
90 | //bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x06,0x6E,0x98};
|
---|
91 | // ELECOM
|
---|
92 | bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x09,0x27,0x26};
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0x02};
|
---|
96 | bool_t dhcp_enable = false;
|
---|
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 | int 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 int main_get_timer();
|
---|
116 | static void main_progress(int 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 | void _start_c(long *p);
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * メインタスク
|
---|
149 | */
|
---|
150 | void main_task(intptr_t exinf)
|
---|
151 | {
|
---|
152 | long args[] = {
|
---|
153 | 1,
|
---|
154 | (long)"ntshell",
|
---|
155 | };
|
---|
156 | _start_c(args);
|
---|
157 | }
|
---|
158 |
|
---|
159 | int main(int argc, char **argv)
|
---|
160 | {
|
---|
161 | #ifdef IF_ETHER_BTUSB
|
---|
162 | // PANU mode
|
---|
163 | bt_bnep_mode = 0;
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | main_initialize();
|
---|
167 |
|
---|
168 | ntshell_init(&ntshell, uart_read, uart_write, cmd_execute, NULL);
|
---|
169 | ntshell_set_prompt(&ntshell, "NTShell>");
|
---|
170 | ntshell_execute(&ntshell);
|
---|
171 |
|
---|
172 | return 0;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * 初期化
|
---|
177 | */
|
---|
178 | static void main_initialize()
|
---|
179 | {
|
---|
180 | FILINFO fno;
|
---|
181 | #if _USE_LFN
|
---|
182 | char lfn[_MAX_LFN + 1];
|
---|
183 | fno.lfname = lfn;
|
---|
184 | fno.lfsize = _MAX_LFN + 1;
|
---|
185 | #endif
|
---|
186 | ER ret2;
|
---|
187 |
|
---|
188 | ntshell_task_init();
|
---|
189 |
|
---|
190 | main_obj.timer = TMO_FEVR;
|
---|
191 | main_obj.state = main_state_start;
|
---|
192 |
|
---|
193 | gpio_t led_blue, led_green, led_red, sw;
|
---|
194 | gpio_init_out(&led_blue, LED_BLUE);
|
---|
195 | gpio_init_out(&led_green, LED_GREEN);
|
---|
196 | gpio_init_out(&led_red, LED_RED);
|
---|
197 | gpio_init_in(&sw, USER_BUTTON0);
|
---|
198 |
|
---|
199 | bool_t exec = gpio_read(&sw) == 1;
|
---|
200 |
|
---|
201 | gpio_write(&led_blue, 1);
|
---|
202 | gpio_write(&led_green, exec ? 1 : 0);
|
---|
203 | gpio_write(&led_red, 0);
|
---|
204 |
|
---|
205 | if (!dhcp_enable) {
|
---|
206 | dhcp4c_rel_info();
|
---|
207 |
|
---|
208 | in4_add_ifaddr(IPV4_ADDR_STAIC_LOCAL, IPV4_ADDR_STAIC_LOCAL_MASK);
|
---|
209 | uint8_t data[32], c;
|
---|
210 | int i, j;
|
---|
211 |
|
---|
212 | for (i = 0, j = 0; i < sizeof(mac_addr); i++) {
|
---|
213 | c = mac_addr[i] >> 4;
|
---|
214 | data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
|
---|
215 | c = mac_addr[i] & 0xF;
|
---|
216 | data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
|
---|
217 | data[j++] = ':';
|
---|
218 | }
|
---|
219 | data[--j] = '\0';
|
---|
220 |
|
---|
221 | syslog(LOG_NOTICE, "mac_addr %s %s", data, dhcp_enable ? "dhcp" : "static");
|
---|
222 | }
|
---|
223 |
|
---|
224 | ether_set_link_callback(netif_link_callback);
|
---|
225 |
|
---|
226 | /* 初期化 */
|
---|
227 | if (ffarch_init() == 0) {
|
---|
228 | gpio_write(&led_blue, 0);
|
---|
229 | }
|
---|
230 | else {
|
---|
231 | gpio_write(&led_blue, 0);
|
---|
232 | gpio_write(&led_red, 1);
|
---|
233 | }
|
---|
234 |
|
---|
235 | gpio_write(&led_green, 0);
|
---|
236 |
|
---|
237 | #ifdef IF_ETHER_BTUSB
|
---|
238 | usbhost_init(USBHOST_TASK);
|
---|
239 | #endif
|
---|
240 |
|
---|
241 | ret2 = get_tim(&main_obj.now);
|
---|
242 | if (ret2 != E_OK) {
|
---|
243 | syslog(LOG_ERROR, "get_tim");
|
---|
244 | ext_tsk();
|
---|
245 | return;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | /*
|
---|
250 | * タイマー取得
|
---|
251 | */
|
---|
252 | static int main_get_timer()
|
---|
253 | {
|
---|
254 | int timer = main_obj.timer;
|
---|
255 |
|
---|
256 | return timer;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * 時間経過
|
---|
261 | */
|
---|
262 | static void main_progress(int interval)
|
---|
263 | {
|
---|
264 | if (main_obj.timer != TMO_FEVR) {
|
---|
265 | main_obj.timer -= interval;
|
---|
266 | if (main_obj.timer < 0) {
|
---|
267 | main_obj.timer = 0;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * タイムアウト処理
|
---|
274 | */
|
---|
275 | static void main_timeout()
|
---|
276 | {
|
---|
277 | //if (main_obj.timer == 0) {
|
---|
278 | //}
|
---|
279 | }
|
---|
280 |
|
---|
281 | /* MACアドレスの設定時に呼ばれる */
|
---|
282 | void mbed_mac_address(char *mac)
|
---|
283 | {
|
---|
284 | memcpy(mac, mac_addr, 6);
|
---|
285 | }
|
---|
286 |
|
---|
287 | void echonet_change_netif_link(uint8_t link_up, uint8_t up);
|
---|
288 |
|
---|
289 | static void netif_link_callback(T_IFNET *ether)
|
---|
290 | {
|
---|
291 | uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
|
---|
292 | uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
|
---|
293 |
|
---|
294 | if (dhcp_enable) {
|
---|
295 | if (!link_up)
|
---|
296 | dhcp4c_rel_info();
|
---|
297 | else if (!up)
|
---|
298 | dhcp4c_renew_info();
|
---|
299 | }
|
---|
300 | else {
|
---|
301 | up = link_up;
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (link_up && up)
|
---|
305 | ntp_cli_execute();
|
---|
306 |
|
---|
307 | ntshell_change_netif_link(link_up, up);
|
---|
308 |
|
---|
309 | echonet_change_netif_link(link_up, up);
|
---|
310 | }
|
---|
311 |
|
---|
312 | extern int usrcmd_ping(int argc, char **argv);
|
---|
313 | extern int usrcmd_dhcp4c(int argc, char **argv);
|
---|
314 | extern int usrcmd_dnsc(int argc, char **argv);
|
---|
315 | extern int usrcmd_ntpc(int argc, char **argv);
|
---|
316 |
|
---|
317 | static const cmd_table_t cmdlist[] = {
|
---|
318 | {"cd", "change directory", usrcmd_cd },
|
---|
319 | {"ls", "list files", usrcmd_ls },
|
---|
320 | {"cp", "copy file", usrcmd_cp },
|
---|
321 | {"rm", "remove file", usrcmd_rm },
|
---|
322 | {"mv", "move file", usrcmd_mv },
|
---|
323 | {"mkdir", "Make directory", usrcmd_mkdir},
|
---|
324 | {"hexdump", "Hex dump", usrcmd_hexdump},
|
---|
325 | {"date", "print date and time", usrcmd_date},
|
---|
326 | {"ping", "ping", usrcmd_ping},
|
---|
327 | {"dhcpc", "DHCP Client rel/renew/info", usrcmd_dhcp4c},
|
---|
328 | {"dnsc", "DNS client", usrcmd_dnsc },
|
---|
329 | {"ntpc", "NTP client", usrcmd_ntpc },
|
---|
330 | {"info", "This is a description text string for info command.", usrcmd_info},
|
---|
331 | {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
|
---|
332 | };
|
---|
333 | cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
|
---|