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

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