source: asp3_tinet_ecnl_arm/trunk/app1_usb_watt_meter/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: 8.6 KB
RevLine 
[352]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 "gpio_api.h"
74#include "usb_hbth.h"
75#include "ntshell_main.h"
76#include <btstack/utils.h>
77#include "client.h"
78#include "ntp_cli.h"
79#include "netcmd.h"
80
81ID ws_api_mailboxid = MAIN_DATAQUEUE;
82ID ws_mempoolid = MPF_NET_BUF_256;
83
84#if defined(IF_ETHER_BTUSB) || defined(IF_ETHER_ENBT)
85extern int bt_bnep_mode;
86/* PANU向けリモートアドレス */
87bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x09,0x27,0x26};
88bool_t dhcp_enable = false;
89#else
90bool_t dhcp_enable = true;
91#endif
92
93uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0x01};
94PRI main_task_priority = MAIN_PRIORITY + 1;
95
96static void netif_link_callback(T_IFNET *ether);
97extern int execute_command(int wait);
98
99enum main_state_t {
100 main_state_start,
101 main_state_idle,
102};
103
104struct main_t {
105 int timer;
106 enum main_state_t state;
107 SYSTIM prev, now;
108};
109struct main_t main_obj;
110
111static void main_initialize();
112static int main_get_timer();
113static void main_progress(int interval);
114static void main_timeout();
115
116int uart_read(char *buf, int cnt, void *extobj)
117{
118 return serial_rea_dat(SIO_PORTID, (char *)buf, cnt);
119}
120
121int uart_write(const char *buf, int cnt, void *extobj)
122{
123 return serial_wri_dat(SIO_PORTID, buf, cnt);
124}
125
126unsigned char ntstdio_xi(struct ntstdio_t *handle)
127{
128 char buf[1];
129 if(serial_rea_dat(SIO_PORTID, buf, 1) != 1)
130 return -EIO;
131 return buf[0];
132}
133
134void ntstdio_xo(struct ntstdio_t *handle, unsigned char c)
135{
136 char buf[1];
137 buf[0] = c;
138 serial_wri_dat(SIO_PORTID, buf, 1);
139}
140
141ntshell_t ntshell;
142
143/*
144 * メインタスク
145 */
146void main_task(intptr_t exinf)
147{
148#if defined(IF_ETHER_BTUSB)
149 // PANU mode
150 bt_bnep_mode = 0;
151#elif defined(IF_ETHER_ENBT)
152 // NAP mode
153 bt_bnep_mode = 1;
154#endif
155
156 main_initialize();
157
158 ntshell_init(&ntshell, uart_read, uart_write, cmd_execute, NULL);
159 ntshell_set_prompt(&ntshell, "NTShell>");
160 ntshell_execute(&ntshell);
161}
162
163int wolfSSL_Debugging_ON(void);
164
165/*
166 * 初期化
167 */
168static void main_initialize()
169{
170 FILINFO fno;
171#if _USE_LFN
172 char lfn[_MAX_LFN + 1];
173 fno.lfname = lfn;
174 fno.lfsize = _MAX_LFN + 1;
175#endif
176 ER ret;
177
178 //wolfSSL_Debugging_ON();
179
180 ntshell_task_init();
181
182 main_obj.timer = TMO_FEVR;
183 main_obj.state = main_state_start;
184
185 gpio_t led_blue, led_green, led_red, sw;
186 gpio_init_out(&led_blue, LED_BLUE);
187 gpio_init_out(&led_green, LED_GREEN);
188 gpio_init_out(&led_red, LED_RED);
189 gpio_init_in(&sw, USER_BUTTON0);
190
191 bool_t exec = gpio_read(&sw) == 1;
192
193 gpio_write(&led_blue, 1);
194 gpio_write(&led_green, exec ? 1 : 0);
195 gpio_write(&led_red, 0);
196
197 if (!dhcp_enable) {
198 dhcp4c_rel_info();
199
200 in4_add_ifaddr(IPV4_ADDR_STAIC_LOCAL, IPV4_ADDR_STAIC_LOCAL_MASK);
201 uint8_t data[32], c;
202 int i, j;
203
204 for (i = 0, j = 0; i < sizeof(mac_addr); i++) {
205 c = mac_addr[i] >> 4;
206 data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
207 c = mac_addr[i] & 0xF;
208 data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
209 data[j++] = ':';
210 }
211 data[--j] = '\0';
212
213 syslog(LOG_NOTICE, "mac_addr %s %s", data, dhcp_enable ? "dhcp" : "static");
214 }
215
216 /* libcurlとWolfSSLの初期化 */
217 client_init();
218
219 ether_set_link_callback(netif_link_callback);
220
221 /* 初期化 */
222 if (ffarch_init() == 0) {
223 gpio_write(&led_blue, 0);
224 }
225 else {
226 gpio_write(&led_blue, 0);
227 gpio_write(&led_red, 1);
228 }
229
230 gpio_write(&led_green, 0);
231
232#if defined(IF_ETHER_BTUSB) || defined(IF_ETHER_ENBT)
233 usbhost_init(USBHOST_TASK);
234#endif
235
236 ret = get_tim(&main_obj.now);
237 if (ret != E_OK) {
238 syslog(LOG_ERROR, "get_tim");
239 ext_tsk();
240 return;
241 }
242}
243
244/*
245 * タイマー取得
246 */
247static int main_get_timer()
248{
249 int timer = main_obj.timer;
250
251 return timer;
252}
253
254/*
255 * 時間経過
256 */
257static void main_progress(int interval)
258{
259 if (main_obj.timer != TMO_FEVR) {
260 main_obj.timer -= interval;
261 if (main_obj.timer < 0) {
262 main_obj.timer = 0;
263 }
264 }
265}
266
267/*
268 * タイムアウト処理
269 */
270static void main_timeout()
271{
272 //if (main_obj.timer == 0) {
273 //}
274}
275
276/* MACアドレスの設定時に呼ばれる */
277void mbed_mac_address(char *mac)
278{
279 memcpy(mac, mac_addr, 6);
280}
281
282void echonet_change_netif_link(uint8_t link_up, uint8_t up);
283
284static void netif_link_callback(T_IFNET *ether)
285{
286 uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
287 uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
288
289 if (dhcp_enable) {
290 if (!link_up)
291 dhcp4c_rel_info();
292 else if (!up)
293 dhcp4c_renew_info();
294 }
295 else {
296 up = link_up;
297 }
298
299 if (link_up && up)
300 ntp_cli_execute();
301
302 ntshell_change_netif_link(link_up, up);
303
304 echonet_change_netif_link(link_up, up);
305}
306
307int custom_rand_generate_seed(uint8_t* output, int32_t sz)
308{
309 SYSTIM now;
310 int32_t i;
311
312 get_tim(&now);
313 srand(now);
314
315 for (i = 0; i < sz; i++)
316 output[i] = rand();
317
318 return 0;
319}
320
321int cmd_logupload(int argc, char **argv)
322{
323 client_task(0);
324 return 0;
325}
326
327extern int curl_main(int argc, char **argv);
328
329static const cmd_table_t cmdlist[] = {
330 {"cd", "change directory", usrcmd_cd },
331 {"ls", "list files", usrcmd_ls },
332 {"cp", "copy file", usrcmd_cp },
333 {"rm", "remove file", usrcmd_rm },
334 {"mv", "move file", usrcmd_mv },
335 {"mkdir", "Make directory", usrcmd_mkdir},
336 {"hexdump", "Hex dump", usrcmd_hexdump},
337 {"date", "print date and time", usrcmd_date},
338 {"curl", "Command lines or scripts to transfer data", curl_main},
339 {"ping", "ping", usrcmd_ping},
340 {"dhcpc", "DHCP Client rel/renew/info", usrcmd_dhcp4c},
341 {"dnsc", "DNS client", usrcmd_dnsc },
342 {"ntpc", "NTP client", usrcmd_ntpc },
343 {"logupload", "Upload log to server", cmd_logupload},
344 {"info", "This is a description text string for info command.", usrcmd_info},
345 {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
346};
347cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
Note: See TracBrowser for help on using the repository browser.