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