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