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

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

ファイルヘッダーの更新

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