source: asp3_tinet_ecnl_rx/trunk/app1_usb_watt_meter/src/main.c@ 349

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

不要なコードを削除

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