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

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

mbed関連を更新
シリアルドライバをmbedのHALを使うよう変更
ファイルディスクリプタの処理を更新

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