source: asp3_tinet_ecnl_arm/trunk/app1_usb_watt_meter/src/main.c@ 364

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

TINETとSocket APIなどを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 9.8 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 <stdlib.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 <tinet_config.h>
55#include <netinet/in.h>
56#include <netinet/in_itron.h>
57#include <tinet_nic_defs.h>
58#include <tinet_cfg.h>
59#include <netinet/in_var.h>
60#include <net/ethernet.h>
61#include <net/if6_var.h>
62#include <net/net.h>
63#include <net/if_var.h>
64#include <netinet/udp_var.h>
65#include "netapp/dhcp4_cli.h"
66#include "ffarch.h"
67#include "ff.h"
68#include "core/ntshell.h"
69#include "core/ntlibc.h"
70#include "util/ntstdio.h"
71#include "usrcmd.h"
72#include "util/ntopt.h"
73#include "socket_stub.h"
74#include "gpio_api.h"
75#include "usb_hbth.h"
76#include "ntshell_main.h"
77#include <btstack/utils.h>
78#include "client.h"
79#include "ntp_cli.h"
80#include "netcmd.h"
81
82ID ws_api_mailboxid = MAIN_DATAQUEUE;
83#ifndef NOUSE_MPF_NET_BUF
84ID ws_mempoolid = MPF_NET_BUF_256;
85#endif
86
87#if defined(IF_ETHER_BTUSB) || defined(IF_ETHER_ENBT)
88extern int bt_bnep_mode;
89/* PANU向けリモートアドレス */
90bd_addr_t remote_addr = {0x00,0x1B,0xDC,0x09,0x27,0x26};
91bool_t dhcp_enable = false;
92#else
93bool_t dhcp_enable = true;
94#endif
95
96uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0x01};
97PRI main_task_priority = MAIN_PRIORITY + 1;
98
99static void netif_link_callback(T_IFNET *ether);
100extern int execute_command(int wait);
101
102enum main_state_t {
103 main_state_start,
104 main_state_idle,
105};
106
107struct main_t {
108 int timer;
109 enum main_state_t state;
110 SYSTIM prev, now;
111};
112struct main_t main_obj;
113
114static void main_initialize();
115static int main_get_timer();
116static void main_progress(int interval);
117static void main_timeout();
118
119extern int ntshell_exit;
120
121int uart_read(char *buf, int cnt, void *extobj)
122{
123 struct main_t *obj = (struct main_t *)extobj;
124 int result;
125 ER ret;
126 int timer;
127
128 obj->prev = obj->now;
129
130 /* タイマー取得 */
131 timer = main_get_timer();
132
133 /* 待ち */
134 ret = serial_trea_dat(SIO_PORTID, buf, cnt, timer);
135 if ((ret < 0) && (ret != E_OK) && (ret != E_TMOUT)) {
136 syslog(LOG_NOTICE, "tslp_tsk ret: %s %d", itron_strerror(ret), timer);
137 ntshell_exit = 1;
138 return -1;
139 }
140 result = (int)ret;
141
142 ret = get_tim(&obj->now);
143 if (ret != E_OK) {
144 syslog(LOG_NOTICE, "get_tim ret: %s", itron_strerror(ret));
145 ntshell_exit = 1;
146 return -1;
147 }
148
149 /* 時間経過 */
150 int elapse = obj->now - obj->prev;
151 main_progress(elapse);
152
153 /* タイムアウト処理 */
154 main_timeout();
155
156 return result;
157}
158
159int uart_write(const char *buf, int cnt, void *extobj)
160{
161 return serial_wri_dat(SIO_PORTID, buf, cnt);
162}
163
164ntshell_t ntshell;
165
166/*
167 * メインタスク
168 */
169void main_task(intptr_t exinf)
170{
171#if defined(IF_ETHER_BTUSB)
172 // PANU mode
173 bt_bnep_mode = 0;
174#elif defined(IF_ETHER_ENBT)
175 // NAP mode
176 bt_bnep_mode = 1;
177#endif
178
179 main_initialize();
180
181 ntshell_init(&ntshell, uart_read, uart_write, cmd_execute, &main_obj);
182 ntshell_set_prompt(&ntshell, "NTShell>");
183 ntshell_execute(&ntshell);
184}
185
186int wolfSSL_Debugging_ON(void);
187
188/*
189 * 初期化
190 */
191static void main_initialize()
192{
193 FILINFO fno;
194#if _USE_LFN
195 char lfn[_MAX_LFN + 1];
196 fno.lfname = lfn;
197 fno.lfsize = _MAX_LFN + 1;
198#endif
199 ER ret;
200
201#ifdef TOPPERS_OMIT_TECS
202 serial_opn_por(SIO_PORTID);
203#endif
204 serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
205
206 //wolfSSL_Debugging_ON();
207
208 ntshell_task_init(uart_read, uart_write, &main_obj);
209
210 main_obj.timer = TMO_FEVR;
211 main_obj.state = main_state_start;
212
213 gpio_t led_blue, led_green, led_red, sw;
214 gpio_init_out(&led_blue, LED_BLUE);
215 gpio_init_out(&led_green, LED_GREEN);
216 gpio_init_out(&led_red, LED_RED);
217 gpio_init_in(&sw, USER_BUTTON0);
218
219 bool_t exec = gpio_read(&sw) == 1;
220
221 gpio_write(&led_blue, 1);
222 gpio_write(&led_green, exec ? 1 : 0);
223 gpio_write(&led_red, 0);
224
225 if (!dhcp_enable) {
226 dhcp4c_rel_info();
227
228 in4_add_ifaddr(IPV4_ADDR_STAIC_LOCAL, IPV4_ADDR_STAIC_LOCAL_MASK);
229 uint8_t data[32], c;
230 int i, j;
231
232 for (i = 0, j = 0; i < sizeof(mac_addr); i++) {
233 c = mac_addr[i] >> 4;
234 data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
235 c = mac_addr[i] & 0xF;
236 data[j++] = (c < 10) ? ('0' + c) : ('A' - 10 + c);
237 data[j++] = ':';
238 }
239 data[--j] = '\0';
240
241 syslog(LOG_NOTICE, "mac_addr %s %s", data, dhcp_enable ? "dhcp" : "static");
242 }
243
244 /* libcurlとWolfSSLの初期化 */
245 client_init();
246
247 ether_set_link_callback(netif_link_callback);
248
249 /* 初期化 */
250 ffarch_init();
251
252 gpio_write(&led_green, 0);
253
254#if defined(IF_ETHER_BTUSB) || defined(IF_ETHER_ENBT)
255 usbhost_init(USBHOST_TASK);
256#endif
257
258 ret = get_tim(&main_obj.now);
259 if (ret != E_OK) {
260 syslog(LOG_ERROR, "get_tim");
261 ext_tsk();
262 return;
263 }
264}
265
266/*
267 * タイマー取得
268 */
269static int main_get_timer()
270{
271 int timer = main_obj.timer;
272
273 return timer;
274}
275
276/*
277 * 時間経過
278 */
279static void main_progress(int interval)
280{
281 if (main_obj.timer != TMO_FEVR) {
282 main_obj.timer -= interval;
283 if (main_obj.timer < 0) {
284 main_obj.timer = 0;
285 }
286 }
287}
288
289/*
290 * タイムアウト処理
291 */
292static void main_timeout()
293{
294 //if (main_obj.timer == 0) {
295 //}
296}
297
298/* MACアドレスの設定時に呼ばれる */
299void mbed_mac_address(char *mac)
300{
301 memcpy(mac, mac_addr, 6);
302}
303
304void echonet_change_netif_link(uint8_t link_up, uint8_t up);
305
306static void netif_link_callback(T_IFNET *ether)
307{
308 uint8_t link_up = (ether->flags & IF_FLAG_LINK_UP) != 0;
309 uint8_t up = (ether->flags & IF_FLAG_UP) != 0;
310
311 if (dhcp_enable) {
312 if (!link_up)
313 dhcp4c_rel_info();
314 else if (!up)
315 dhcp4c_renew_info();
316 }
317 else {
318 up = link_up;
319 }
320
321 if (link_up && up)
322 ntp_cli_execute();
323
324 ntshell_change_netif_link(link_up, up);
325
326 echonet_change_netif_link(link_up, up);
327}
328
329int custom_rand_generate_seed(uint8_t* output, int32_t sz)
330{
331 SYSTIM now;
332 int32_t i;
333
334 get_tim(&now);
335 srand(now);
336
337 for (i = 0; i < sz; i++)
338 output[i] = rand();
339
340 return 0;
341}
342
343int cmd_creid(int argc, char **argv)
344{
345 if (argc != 2) {
346 printf("creid {client_id}\n");
347 return 0;
348 }
349
350 client_set_client_id(argv[1]);
351
352 return 0;
353}
354
355int cmd_secret(int argc, char **argv)
356{
357 if (argc != 2) {
358 printf("secret {client_secret}\n");
359 return 0;
360 }
361
362 client_set_client_secret(argv[1]);
363
364 return 0;
365}
366
367int cmd_logupload(int argc, char **argv)
368{
369 return client_upload_file(argc, argv);
370}
371
372extern int curl_main(int argc, char **argv);
373
374static const cmd_table_t cmdlist[] = {
375 {"cd", "change directory", usrcmd_cd },
376 {"ls", "list files", usrcmd_ls },
377 {"cp", "copy file", usrcmd_cp },
378 {"rm", "remove file", usrcmd_rm },
379 {"mv", "move file", usrcmd_mv },
380 {"mkdir", "Make directory", usrcmd_mkdir},
381 {"hexdump", "Hex dump", usrcmd_hexdump},
382 {"date", "print date and time", usrcmd_date},
383 {"curl", "Command lines or scripts to transfer data", curl_main},
384 {"ping", "ping", usrcmd_ping},
385 {"dhcpc", "DHCP Client rel/renew/info", usrcmd_dhcp4c},
386 {"dnsc", "DNS client", usrcmd_dnsc },
387 {"ntpc", "NTP client", usrcmd_ntpc },
388 {"creid", "Regster device", cmd_creid},
389 {"secret", "Regster device", cmd_secret},
390 {"gdi", "get_device_id", client_get_device_id},
391 {"gat", "get_access_token", client_get_access_token},
392 {"uat", "update_access_token", client_update_access_token},
393 {"rd", "revoke device", client_revoke},
394 {"uf", "upload_file", client_upload_file},
395 {"logupload", "Upload log to server", cmd_logupload},
396 {"info", "This is a description text string for info command.", usrcmd_info},
397 {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
398};
399cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
Note: See TracBrowser for help on using the repository browser.