source: asp3_tinet_ecnl_rx/trunk/app5_temp_sensor/src/main.c@ 337

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

ASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 8.1 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 <setjmp.h>
47#include <string.h>
48#include "syssvc/serial.h"
49#include "syssvc/syslog.h"
50#include "target_syssvc.h"
51#include "kernel_cfg.h"
52#include "main.h"
53#include <tinet_config.h>
54#include <netinet/in.h>
55#include <netinet/in_itron.h>
56#include <tinet_nic_defs.h>
57#include <tinet_cfg.h>
58#include <netinet/in_var.h>
59#include <net/ethernet.h>
60#include <net/if6_var.h>
61#include <net/net.h>
62#include <net/if_var.h>
63#include <netinet/udp_var.h>
64#include "netapp/dhcp4_cli.h"
65#include "ffarch.h"
66#include "ff.h"
67#include "core/ntshell.h"
68#include "core/ntlibc.h"
69#include "util/ntstdio.h"
70#include "usrcmd.h"
71#include "util/ntopt.h"
72#include "socket_stub.h"
73#include "mbed_api.h"
74#include "usb_hbth.h"
75#include "ntshell_main.h"
76#include <btstack/utils.h>
77
78ID ws_api_mailboxid = MAIN_DATAQUEUE;
79ID ws_mempoolid = MPF_NET_BUF_256;
80
81#ifdef IF_ETHER_BTUSB
82extern int bt_bnep_mode;
83
84// BUFFALLO
85//bd_addr_t remote_addr = { 0x00,0x1B,0xDC,0x04,0x5C,0x2D };
86// Raspberry Pi 3
87//bd_addr_t remote_addr = {0xB8,0x27,0xEB,0x10,0x6D,0x60};
88// PRINCETON
89//bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x06,0x6E,0x98};
90// ELECOM
91bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x09,0x27,0x26};
92#endif
93
94uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0x05};
95bool_t dhcp_enable = false;
96PRI main_task_priority = MAIN_PRIORITY + 1;
97
98static void netif_link_callback(T_IFNET *ether);
99extern int execute_command(int wait);
100
101enum main_state_t {
102 main_state_start,
103 main_state_idle,
104};
105
106struct main_t {
107 int timer;
108 enum main_state_t state;
109 SYSTIM prev, now;
110};
111struct main_t main_obj;
112
113static void main_initialize();
114static int main_get_timer();
115static void main_progress(int interval);
116static void main_timeout();
117
118int uart_read(char *buf, int cnt, void *extobj)
119{
120 return serial_rea_dat(SIO_PORTID, (char *)buf, cnt);
121}
122
123int uart_write(const char *buf, int cnt, void *extobj)
124{
125 return serial_wri_dat(SIO_PORTID, buf, cnt);
126}
127
128unsigned char ntstdio_xi(struct ntstdio_t *handle)
129{
130 char buf[1];
131 if(serial_rea_dat(SIO_PORTID, buf, 1) != 1)
132 return -EIO;
133 return buf[0];
134}
135
136void ntstdio_xo(struct ntstdio_t *handle, unsigned char c)
137{
138 char buf[1];
139 buf[0] = c;
140 serial_wri_dat(SIO_PORTID, buf, 1);
141}
142
143ntshell_t ntshell;
144void _start_c(long *p);
145
146/*
147 * メインタスク
148 */
149void main_task(intptr_t exinf)
150{
151 long args[] = {
152 1,
153 (long)"ntshell",
154 };
155 _start_c(args);
156}
157
158int main(int argc, char **argv)
159{
160#ifdef IF_ETHER_BTUSB
161 // PANU mode
162 bt_bnep_mode = 0;
163#endif
164
165 main_initialize();
166
167 ntshell_init(&ntshell, uart_read, uart_write, cmd_execute, NULL);
168 ntshell_set_prompt(&ntshell, "NTShell>");
169 ntshell_execute(&ntshell);
170
171 return 0;
172}
173
174/*
175 * 初期化
176 */
177static void main_initialize()
178{
179 FILINFO fno;
180#if _USE_LFN
181 char lfn[_MAX_LFN + 1];
182 fno.lfname = lfn;
183 fno.lfsize = _MAX_LFN + 1;
184#endif
185 ER ret2;
186
187 ntshell_task_init();
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 if (!dhcp_enable) {
205 dhcp4c_rel_info();
206
207 in4_add_ifaddr(IPV4_ADDR_STAIC_LOCAL, IPV4_ADDR_STAIC_LOCAL_MASK);
208 uint8_t data[32], c;
209 int i, j;
210
211 for (i = 0, j = 0; i < sizeof(mac_addr); i++) {
212 c = mac_addr[i] >> 4;
213 data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
214 c = mac_addr[i] & 0xF;
215 data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
216 data[j++] = ':';
217 }
218 data[--j] = '\0';
219
220 syslog(LOG_NOTICE, "mac_addr %s %s", data, dhcp_enable ? "dhcp" : "static");
221 }
222
223 ether_set_link_callback(netif_link_callback);
224
225 /* 初期化 */
226 if (ffarch_init() == 0) {
227 gpio_write(&led_blue, 0);
228 }
229 else {
230 gpio_write(&led_blue, 0);
231 gpio_write(&led_red, 1);
232 }
233
234 gpio_write(&led_green, 0);
235
236#ifdef IF_ETHER_BTUSB
237 usbhost_init(USBHOST_TASK);
238#endif
239
240 ret2 = get_tim(&main_obj.now);
241 if (ret2 != E_OK) {
242 syslog(LOG_ERROR, "get_tim");
243 ext_tsk();
244 return;
245 }
246}
247
248/*
249 * タイマー取得
250 */
251static int main_get_timer()
252{
253 int timer = main_obj.timer;
254
255 return timer;
256}
257
258/*
259 * 時間経過
260 */
261static void main_progress(int interval)
262{
263 if (main_obj.timer != TMO_FEVR) {
264 main_obj.timer -= interval;
265 if (main_obj.timer < 0) {
266 main_obj.timer = 0;
267 }
268 }
269}
270
271/*
272 * タイムアウト処理
273 */
274static void main_timeout()
275{
276 //if (main_obj.timer == 0) {
277 //}
278}
279
280/* MACアドレスの設定時に呼ばれる */
281void mbed_mac_address(char *mac)
282{
283 memcpy(mac, mac_addr, 6);
284}
285
286void echonet_change_netif_link(uint8_t link_up, uint8_t up);
287
288static void netif_link_callback(T_IFNET *ether)
289{
290 uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
291 uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
292
293 if (dhcp_enable) {
294 if (!link_up)
295 dhcp4c_rel_info();
296 else if (!up)
297 dhcp4c_renew_info();
298 }
299 else {
300 up = link_up;
301 }
302
303 ntshell_change_netif_link(link_up, up);
304
305 echonet_change_netif_link(link_up, up);
306}
307
308extern int usrcmd_ping(int argc, char **argv);
309extern int usrcmd_dhcp4c(int argc, char **argv);
310
311static const cmd_table_t cmdlist[] = {
312 {"cd", "change directory", usrcmd_cd },
313 {"ls", "list files", usrcmd_ls },
314 {"cp", "copy file", usrcmd_cp },
315 {"rm", "remove file", usrcmd_rm },
316 {"mv", "move file", usrcmd_mv },
317 {"mkdir", "Make directory", usrcmd_mkdir},
318 {"hexdump", "Hex dump", usrcmd_hexdump},
319 {"ping", "ping", usrcmd_ping},
320 {"dhcpc", "DHCP Client rel/renew/info", usrcmd_dhcp4c},
321 {"info", "This is a description text string for info command.", usrcmd_info},
322 {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
323};
324cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
Note: See TracBrowser for help on using the repository browser.