source: asp3_tinet_ecnl_rx/trunk/bnep_bridge/src/main.c@ 364

Last change on this file since 364 was 364, checked in by coas-nagasima, 5 years ago

TINETとSocket APIなどを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 9.7 KB
Line 
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
68ID ws_api_mailboxid = MAIN_DATAQUEUE;
69extern 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
78bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x09,0x27,0x26};
79
80/* ネットワークインタフェースに依存しないソフトウェア情報 */
81
82T_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
97PRI main_task_priority = MAIN_PRIORITY + 1;
98
99static void netif_link_callback(T_IFNET *ether);
100extern int execute_command(int wait);
101
102enum main_state_t {
103 main_state_start,
104 main_state_idle,
105};
106
107struct main_t {
108 TMO timer;
109 enum main_state_t state;
110 SYSTIM prev, now;
111};
112struct main_t main_obj;
113
114static void main_initialize();
115static TMO main_get_timer();
116static void main_progress(TMO interval);
117static void main_timeout();
118
119extern int ntshell_exit;
120
121int uart_read(char *buf, int cnt, void *extobj)
122{
123 struct main_t *obj = (struct main_t *)extobj;
124 int result;
125 ER ret;
126 int timer;
127
128 obj->prev = obj->now;
129
130 /* タイマー取得 */
131 timer = main_get_timer();
132
133 /* 待ち */
134 ret = serial_trea_dat(SIO_PORTID, buf, cnt, timer);
135 if ((ret < 0) && (ret != E_OK) && (ret != E_TMOUT)) {
136 syslog(LOG_NOTICE, "tslp_tsk ret: %s %d", itron_strerror(ret), timer);
137 ntshell_exit = 1;
138 return -1;
139 }
140 result = (int)ret;
141
142 ret = get_tim(&obj->now);
143 if (ret != E_OK) {
144 syslog(LOG_NOTICE, "get_tim ret: %s", itron_strerror(ret));
145 ntshell_exit = 1;
146 return -1;
147 }
148
149 /* 時間経過 */
150 int elapse = obj->now - obj->prev;
151 main_progress(elapse);
152
153 /* タイムアウト処理 */
154 main_timeout();
155
156 return result;
157}
158
159int uart_write(const char *buf, int cnt, void *extobj)
160{
161 return serial_wri_dat(SIO_PORTID, buf, cnt);
162}
163
164ntshell_t ntshell;
165
166/*
167 * メインタスク
168 */
169void main_task(intptr_t exinf)
170{
171 // NAP mode
172 bt_bnep_mode = 1;
173
174 main_initialize();
175
176 ntshell_init(&ntshell, uart_read, uart_write, cmd_execute, &main_obj);
177 ntshell_set_prompt(&ntshell, "NTShell>");
178 ntshell_execute(&ntshell);
179}
180
181/*
182 * 初期化
183 */
184static void main_initialize()
185{
186 FILINFO fno;
187#if _USE_LFN
188 char lfn[_MAX_LFN + 1];
189 fno.lfname = lfn;
190 fno.lfsize = _MAX_LFN + 1;
191#endif
192 ER ret;
193
194#ifdef TOPPERS_OMIT_TECS
195 serial_opn_por(SIO_PORTID);
196#endif
197 serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
198
199 ntshell_task_init(uart_read, uart_write, &main_obj);
200
201 main_obj.timer = TMO_FEVR;
202 main_obj.state = main_state_start;
203
204 gpio_t led_blue, led_green, led_red, sw;
205 gpio_init_out(&led_blue, LED_BLUE);
206 gpio_init_out(&led_green, LED_GREEN);
207 gpio_init_out(&led_red, LED_RED);
208 gpio_init_in(&sw, USER_BUTTON0);
209
210 bool_t exec = gpio_read(&sw) == 1;
211
212 gpio_write(&led_blue, 1);
213 gpio_write(&led_green, exec ? 1 : 0);
214 gpio_write(&led_red, 0);
215
216 ether_set_link_callback(netif_link_callback);
217
218 /* 初期化 */
219 ffarch_init();
220
221 gpio_write(&led_green, 0);
222
223 usbhost_init(USBHOST_TASK);
224
225 ret = get_tim(&main_obj.now);
226 if (ret != E_OK) {
227 syslog(LOG_ERROR, "get_tim");
228 ext_tsk();
229 return;
230 }
231}
232
233/*
234 * タイマー取得
235 */
236static TMO main_get_timer()
237{
238 TMO timer = main_obj.timer;
239
240 return timer;
241}
242
243/*
244 * 時間経過
245 */
246static void main_progress(TMO interval)
247{
248 if (main_obj.timer != TMO_FEVR) {
249 main_obj.timer -= interval;
250 if (main_obj.timer < 0) {
251 main_obj.timer = 0;
252 }
253 }
254}
255
256/*
257 * タイムアウト処理
258 */
259static void main_timeout()
260{
261 //if (main_obj.timer == 0) {
262 //}
263}
264
265void ntshell_change_netif_link(uint8_t link_up, uint8_t up);
266
267static void netif_link_callback(T_IFNET *ether)
268{
269 uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
270 uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
271
272 ntshell_change_netif_link(link_up, up);
273}
274
275static const cmd_table_t cmdlist[] = {
276 {"cd", "change directory", usrcmd_cd },
277 {"ls", "list files", usrcmd_ls },
278 {"cp", "copy file", usrcmd_cp },
279 {"rm", "remove file", usrcmd_rm },
280 {"mv", "move file", usrcmd_mv },
281 {"mkdir", "Make directory", usrcmd_mkdir},
282 {"hexdump", "Hex dump", usrcmd_hexdump},
283 {"date", "print date and time", usrcmd_date},
284 {"info", "This is a description text string for info command.", usrcmd_info},
285 {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
286};
287cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
288
289static T_IFNET ether_ifnet;
290
291/**
292 * Called by a driver when its link goes up
293 */
294void ether_set_link_up(T_IF_SOFTC *ic)
295{
296 if (!(ether_ifnet.flags & IF_FLAG_LINK_UP)) {
297 ether_ifnet.flags |= IF_FLAG_LINK_UP;
298
299 if (ether_ifnet.flags & IF_FLAG_UP) {
300#if LWIP_ARP
301 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
302 if (ether_ifnet.flags & IF_FLAG_ETHARP) {
303 etharp_gratuitous(&ether_ifnet);
304 }
305#endif /* LWIP_ARP */
306
307#if LWIP_IGMP
308 /* resend IGMP memberships */
309 if (ether_ifnet._flags & IF_FLAG_IGMP) {
310 igmp_report_groups(&ether_ifnet);
311 }
312#endif /* LWIP_IGMP */
313 }
314 if (ether_ifnet.link_callback) {
315 (ether_ifnet.link_callback)(&ether_ifnet);
316 }
317 }
318}
319
320/**
321 * Called by a driver when its link goes down
322 */
323void ether_set_link_down(T_IF_SOFTC *ic)
324{
325 if (ether_ifnet.flags & IF_FLAG_LINK_UP) {
326 ether_ifnet.flags &= ~IF_FLAG_LINK_UP;
327 if (ether_ifnet.link_callback) {
328 (ether_ifnet.link_callback)(&ether_ifnet);
329 }
330 }
331}
332
333/**
334 * Set callback to be called when link is brought up/down
335 */
336void ether_set_link_callback(ether_status_callback_fn link_callback)
337{
338 ether_ifnet.link_callback = link_callback;
339}
340
341/*
342 * Ethernet 入力タスク
343 */
344
345void
346ether_input_task(intptr_t exinf)
347{
348 T_IF_SOFTC *ic = &if_softc;
349 T_NET_BUF *input;
350 ER ret;
351
352 btusb_probe(ic);
353 btusb_init(ic);
354
355 rx62n_probe(ic);
356 rx62n_init(ic);
357
358 while (true) {
359 ret = wai_sem(ic->semid_rxb_ready);
360 if (ret != E_OK) {
361 syslog(LOG_DEBUG, "wai_sem(ic->semid_rxb_ready) = %d", ret);
362 return;
363 }
364
365 if ((input = rx62n_read(ic)) != NULL) {
366 int rel = btusb_start(ic, input);
367 if (rel) {
368 rel_net_buf(input);
369 }
370 }
371 }
372}
373
374ER tget_net_buf (T_NET_BUF **blk, uint_t len, TMO tmout)
375{
376 T_NET_BUF *buf;
377
378 buf = malloc(sizeof(T_NET_BUF) + len);
379 *blk = buf;
380 if (buf == NULL)
381 return E_TMOUT;
382
383 buf->len = len;
384 buf->idix = 0;
385 buf->flags = 0;
386
387 return E_OK;
388}
389
390ER rel_net_buf (T_NET_BUF *blk)
391{
392 free(blk);
393
394 return E_OK;
395}
Note: See TracBrowser for help on using the repository browser.