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

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

arm向けASP3版ECNLを追加

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