source: asp3_tinet_ecnl_arm/trunk/bnep_bridge/src/main.c@ 387

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

ファイルディスクリプタ処理を更新

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