source: azure_iot_hub_f767zi/trunk/app_iothub_client/src/main.c@ 457

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

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 26.3 KB
Line 
1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 * Copyright (C) 2004-2012 by Embedded and Real-Time Systems Laboratory
9 * Graduate School of Information Science, Nagoya Univ., JAPAN
10 *
11 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
12 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
13 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
14 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
16 * スコード中に含まれていること.
17 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
19 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
20 * の無保証規定を掲載すること.
21 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
22 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
23 * と.
24 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
25 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
26 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
27 * 報告すること.
28 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
29 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
30 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
31 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
32 * 免責すること.
33 *
34 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
35 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
36 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
37 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
38 * の責任を負わない.
39 *
40 * $Id$
41 */
42
43/*
44 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
45 * All rights reserved.
46 *
47 * Redistribution and use in source and binary forms, with or without modification,
48 * are permitted provided that the following conditions are met:
49 *
50 * 1. Redistributions of source code must retain the above copyright notice,
51 * this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright notice,
53 * this list of conditions and the following disclaimer in the documentation
54 * and/or other materials provided with the distribution.
55 * 3. The name of the author may not be used to endorse or promote products
56 * derived from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
59 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
60 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
61 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
62 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
63 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
66 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
67 * OF SUCH DAMAGE.
68 *
69 * This file is part of the lwIP TCP/IP stack.
70 *
71 * Author: Adam Dunkels <adam@sics.se>
72 *
73 */
74
75/*
76 * サンプルプログラムの本体
77 */
78
79#include <kernel.h>
80#include <t_syslog.h>
81#include <t_stdlib.h>
82#include <sil.h>
83#include "syssvc/serial.h"
84#include "syssvc/syslog.h"
85#ifdef USE_STORAGEDEVICE
86#include "storagedevice.h"
87#endif /* USE_STORAGEDEVICE */
88#include "kernel_cfg.h"
89#include "device.h"
90#include "sdmmc.h"
91#include "client.h"
92#include "monitor.h"
93#include "main.h"
94
95#include "lwip/opt.h"
96
97#include "lwip/init.h"
98
99#include "lwip/mem.h"
100#include "lwip/memp.h"
101#include "lwip/sys.h"
102#include "lwip/timeouts.h"
103
104#include "lwip/ip_addr.h"
105
106#include "lwip/dns.h"
107#include "lwip/dhcp.h"
108
109#include "lwip/stats.h"
110
111#include "lwip/tcp.h"
112#include "lwip/inet_chksum.h"
113
114#include "lwip/tcpip.h"
115#include "lwip/sockets.h"
116
117
118#if 0
119#include "netif/pcapif.h"
120#endif
121
122#ifndef LWIP_HAVE_SLIPIF
123#define LWIP_HAVE_SLIPIF 0
124#endif
125#if LWIP_HAVE_SLIPIF
126#include "netif/slipif.h"
127#define SLIP_PTY_TEST 1
128#endif
129
130#if PPP_SUPPORT
131#include "netif/ppp/pppos.h"
132#include "lwip/sio.h"
133#define PPP_PTY_TEST 1
134#include <termios.h>
135#endif
136
137#include "lwip/ip_addr.h"
138
139#include "lwip/apps/httpd.h"
140#include "apps/udpecho/udpecho.h"
141#include "apps/tcpecho/tcpecho.h"
142#include "apps/shell/shell.h"
143#include "apps/chargen/chargen.h"
144#include "apps/netio/netio.h"
145#include "apps/ping/ping.h"
146#include "lwip/apps/netbiosns.h"
147#include "lwip/apps/mdns.h"
148#include "lwip/apps/sntp.h"
149#include "lwip/apps/snmp.h"
150#include "lwip/apps/snmp_mib2.h"
151#include "examples/snmp/snmp_private_mib/private_mib.h"
152#include "lwip/apps/tftp_server.h"
153
154#if LWIP_RAW
155#include "lwip/icmp.h"
156#include "lwip/raw.h"
157#endif
158#include "lwip/inet.h"
159#include "esp_at_socket.h"
160
161err_t ethernetif_init(struct netif *netif);
162
163/*
164 * サービスコールのエラーのログ出力
165 */
166Inline void
167svc_perror(const char *file, int_t line, const char *expr, ER ercd)
168{
169 if (ercd < 0) {
170 t_perror(LOG_ERROR, file, line, expr, ercd);
171 }
172}
173
174#define SVC_PERROR(expr) svc_perror(__FILE__, __LINE__, #expr, (expr))
175
176/*static*/ uint32_t heap_area[16*1024];
177
178intptr_t heap_param[2] = {
179 (intptr_t )heap_area,
180 sizeof(heap_area)
181};
182
183#ifdef _STORAGEDEVICE_H_
184static void storagedev_notice(void *psdev, bool_t sw)
185{
186 syslog_2(LOG_NOTICE, "SD-CARD[%08x] on/off(%d) !", psdev, sw);
187}
188
189static uint32_t get_fat_time(void)
190{
191 int reqtime = 0;
192
193 reqtime = 0x00210000;
194 return reqtime;
195}
196#endif /* _STORAGEDEVICE_H_ */
197
198uint32_t LWIP_RAND(void)
199{
200 return (uint32_t)rand();
201}
202
203#if LWIP_SNMP
204static const struct snmp_mib *mibs[] = {
205 &mib2,
206 &mib_private
207};
208#endif /* LWIP_SNMP */
209
210#if LWIP_IPV4
211/* (manual) host IP configuration */
212static ip_addr_t ipaddr, netmask, gw;
213#endif /* LWIP_IPV4 */
214
215struct netif netif;
216
217/* ping out destination cmd option */
218static ip_addr_t ping_addr;
219
220static bool_t tcpip_init_flag = false;
221
222/* nonstatic debug cmd option, exported in lwipopts.h */
223unsigned char debug_flags;
224
225static void init_netifs(void);
226
227#if 0
228static void
229tcp_debug_timeout(void *data)
230{
231 LWIP_UNUSED_ARG(data);
232#if TCP_DEBUG
233 tcp_debug_print_pcbs();
234#endif /* TCP_DEBUG */
235 sys_timeout(5000, tcp_debug_timeout, NULL);
236}
237#endif
238
239/*
240 * gmtime_r関数
241 */
242struct tm *gmtime_r(const time_t *pt, struct tm *ptm);
243
244void
245sntp_set_system_time(u32_t sec)
246{
247 struct tm current_time_val;
248 time_t current_time = (time_t)sec;
249
250 gmtime_r(&current_time, &current_time_val);
251 rtc_set_time((struct tm2 *)&current_time_val);
252
253 current_time = 0;
254 memset(&current_time_val, 0, sizeof(current_time_val));
255
256 current_time = time(NULL);
257 //gmtime_r(&current_time, &current_time_val);
258 //printf("%04d/%02d/%02d %02d:%02d:%02d\n",
259 // current_time_val.tm_year+1900, current_time_val.tm_mon+1, current_time_val.tm_mday,
260 // current_time_val.tm_hour, current_time_val.tm_min, current_time_val.tm_sec);
261 printf("%s\n", ctime(&current_time));
262}
263
264#if LWIP_MDNS_RESPONDER
265static void
266srv_txt(struct mdns_service *service, void *txt_userdata)
267{
268 err_t res;
269 LWIP_UNUSED_ARG(txt_userdata);
270
271 res = mdns_resp_add_service_txtitem(service, "path=/", 6);
272 LWIP_ERROR("mdns add service txt failed\n", (res == ERR_OK), return);
273}
274#endif
275
276#if LWIP_UDP
277
278static void*
279tftp_open(const char* fname, const char* mode, u8_t is_write)
280{
281#ifdef _STORAGEDEVICE_H_
282 LWIP_UNUSED_ARG(mode);
283
284 if(is_write)
285 return (void*)fopen(fname, "wb");
286 else
287 return (void*)fopen(fname, "rb");
288#else /* _STORAGEDEVICE_H_ */
289 return NULL;
290#endif /* _STORAGEDEVICE_H_ */
291}
292
293static void
294tftp_close(void* handle)
295{
296#ifdef _STORAGEDEVICE_H_
297 fclose((FILE*)handle);
298#endif /* _STORAGEDEVICE_H_ */
299}
300
301static int
302tftp_read(void* handle, void* buf, int bytes)
303{
304#ifdef _STORAGEDEVICE_H_
305 int rlen = fread(buf, 1, bytes, (FILE*)handle);
306 if(rlen <= 0)
307 return -1;
308 else
309 return rlen;
310#else /* _STORAGEDEVICE_H_ */
311 return -1;
312#endif /* _STORAGEDEVICE_H_ */
313}
314
315static int
316tftp_write(void* handle, struct pbuf* p)
317{
318#ifdef _STORAGEDEVICE_H_
319 while(p != NULL){
320 if(fwrite(p->payload, 1, p->len, (FILE*)handle) != (size_t)p->len)
321 return -1;
322 p = p->next;
323 }
324 return 0;
325#else /* _STORAGEDEVICE_H_ */
326 return -1;
327#endif /* _STORAGEDEVICE_H_ */
328}
329
330static const struct tftp_context tftp = {
331 tftp_open,
332 tftp_close,
333 tftp_read,
334 tftp_write
335};
336
337#endif /* LWIP_UDP */
338
339static void
340tcpip_init_done(void *arg)
341{
342 sys_sem_t *sem;
343 sem = (sys_sem_t *)arg;
344
345 init_netifs();
346
347#if LWIP_IPV4
348 netbiosns_set_name("toppershost");
349 netbiosns_init();
350#endif /* LWIP_IPV4 */
351
352 sntp_setoperatingmode(SNTP_OPMODE_POLL);
353#if LWIP_DHCP
354 sntp_servermode_dhcp(1); /* get SNTP server via DHCP */
355#else /* LWIP_DHCP */
356#if LWIP_IPV4
357 //sntp_setserver(0, netif_ip_gw4(&netif));
358#endif /* LWIP_IPV4 */
359#endif /* LWIP_DHCP */
360 sntp_setservername(0, "ntp.nict.jp");
361 sntp_init();
362
363#if LWIP_SNMP
364 lwip_privmib_init();
365#if SNMP_LWIP_MIB2
366#if SNMP_USE_NETCONN
367 snmp_threadsync_init(&snmp_mib2_lwip_locks, snmp_mib2_lwip_synchronizer);
368#endif /* SNMP_USE_NETCONN */
369 snmp_mib2_set_syscontact_readonly((const u8_t*)"root", NULL);
370 snmp_mib2_set_syslocation_readonly((const u8_t*)"lwIP development PC", NULL);
371 snmp_mib2_set_sysdescr((const u8_t*)"toppershost", NULL);
372#endif /* SNMP_LWIP_MIB2 */
373
374 snmp_set_mibs(mibs, LWIP_ARRAYSIZE(mibs));
375 snmp_init();
376#endif /* LWIP_SNMP */
377
378#if LWIP_MDNS_RESPONDER
379 mdns_resp_init();
380 mdns_resp_add_netif(&netif, "toppershost", 3600);
381 mdns_resp_add_service(&netif, "myweb", "_http", DNSSD_PROTO_TCP, 80, 3600, srv_txt, NULL);
382#endif
383
384#if LWIP_UDP
385 tftp_init(&tftp);
386#endif /* LWIP_UDP */
387
388 sys_sem_signal(sem);
389}
390
391/*-----------------------------------------------------------------------------------*/
392
393#if LWIP_HAVE_SLIPIF
394/* (manual) host IP configuration */
395#if LWIP_IPV4
396static ip_addr_t ipaddr_slip, netmask_slip, gw_slip;
397#endif /* LWIP_IPV4 */
398struct netif slipif;
399#endif /* LWIP_HAVE_SLIPIF */
400
401#if PPP_SUPPORT
402sio_fd_t ppp_sio;
403ppp_pcb *ppp;
404struct netif pppos_netif;
405
406static void
407pppos_rx_thread(void *arg)
408{
409 u32_t len;
410 u8_t buffer[128];
411 LWIP_UNUSED_ARG(arg);
412
413 /* Please read the "PPPoS input path" chapter in the PPP documentation. */
414 while(1){
415 len = sio_read(ppp_sio, buffer, sizeof(buffer));
416 if (len > 0) {
417 /* Pass received raw characters from PPPoS to be decoded through lwIP
418 * TCPIP thread using the TCPIP API. This is thread safe in all cases
419 * but you should avoid passing data byte after byte. */
420 pppos_input_tcpip(ppp, buffer, len);
421 }
422 }
423}
424
425static void
426ppp_link_status_cb(ppp_pcb *pcb, int err_code, void *ctx)
427{
428 struct netif *pppif = ppp_netif(pcb);
429 LWIP_UNUSED_ARG(ctx);
430
431 switch(err_code) {
432 case PPPERR_NONE: /* No error. */
433 {
434#if LWIP_DNS
435 const ip_addr_t *ns;
436#endif /* LWIP_DNS */
437 fprintf(stderr, "ppp_link_status_cb: PPPERR_NONE\n\r");
438#if LWIP_IPV4
439 fprintf(stderr, " our_ip4addr = %s\n\r", ip4addr_ntoa(netif_ip4_addr(pppif)));
440 fprintf(stderr, " his_ipaddr = %s\n\r", ip4addr_ntoa(netif_ip4_gw(pppif)));
441 fprintf(stderr, " netmask = %s\n\r", ip4addr_ntoa(netif_ip4_netmask(pppif)));
442#endif /* LWIP_IPV4 */
443#if LWIP_IPV6
444 fprintf(stderr, " our_ip6addr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
445#endif /* LWIP_IPV6 */
446
447#if LWIP_DNS
448 ns = dns_getserver(0);
449 fprintf(stderr, " dns1 = %s\n\r", ipaddr_ntoa(ns));
450 ns = dns_getserver(1);
451 fprintf(stderr, " dns2 = %s\n\r", ipaddr_ntoa(ns));
452#endif /* LWIP_DNS */
453#if PPP_IPV6_SUPPORT
454 fprintf(stderr, " our6_ipaddr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
455#endif /* PPP_IPV6_SUPPORT */
456 }
457 break;
458
459 case PPPERR_PARAM: /* Invalid parameter. */
460 printf("ppp_link_status_cb: PPPERR_PARAM\n");
461 break;
462
463 case PPPERR_OPEN: /* Unable to open PPP session. */
464 printf("ppp_link_status_cb: PPPERR_OPEN\n");
465 break;
466
467 case PPPERR_DEVICE: /* Invalid I/O device for PPP. */
468 printf("ppp_link_status_cb: PPPERR_DEVICE\n");
469 break;
470
471 case PPPERR_ALLOC: /* Unable to allocate resources. */
472 printf("ppp_link_status_cb: PPPERR_ALLOC\n");
473 break;
474
475 case PPPERR_USER: /* User interrupt. */
476 printf("ppp_link_status_cb: PPPERR_USER\n");
477 break;
478
479 case PPPERR_CONNECT: /* Connection lost. */
480 printf("ppp_link_status_cb: PPPERR_CONNECT\n");
481 break;
482
483 case PPPERR_AUTHFAIL: /* Failed authentication challenge. */
484 printf("ppp_link_status_cb: PPPERR_AUTHFAIL\n");
485 break;
486
487 case PPPERR_PROTOCOL: /* Failed to meet protocol. */
488 printf("ppp_link_status_cb: PPPERR_PROTOCOL\n");
489 break;
490
491 case PPPERR_PEERDEAD: /* Connection timeout. */
492 printf("ppp_link_status_cb: PPPERR_PEERDEAD\n");
493 break;
494
495 case PPPERR_IDLETIMEOUT: /* Idle Timeout. */
496 printf("ppp_link_status_cb: PPPERR_IDLETIMEOUT\n");
497 break;
498
499 case PPPERR_CONNECTTIME: /* PPPERR_CONNECTTIME. */
500 printf("ppp_link_status_cb: PPPERR_CONNECTTIME\n");
501 break;
502
503 case PPPERR_LOOPBACK: /* Connection timeout. */
504 printf("ppp_link_status_cb: PPPERR_LOOPBACK\n");
505 break;
506
507 default:
508 printf("ppp_link_status_cb: unknown errCode %d\n", err_code);
509 break;
510 }
511}
512
513static u32_t
514ppp_output_cb(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx)
515{
516 LWIP_UNUSED_ARG(pcb);
517 LWIP_UNUSED_ARG(ctx);
518 return sio_write(ppp_sio, data, len);
519}
520#endif
521
522#if LWIP_NETIF_STATUS_CALLBACK
523static void
524netif_status_callback(struct netif *nif)
525{
526 printf("NETIF: %c%c%d is %s\n", nif->name[0], nif->name[1], nif->num,
527 netif_is_up(nif) ? "UP" : "DOWN");
528#if LWIP_IPV4
529 printf("IPV4: Host at %s ", ip4addr_ntoa(netif_ip4_addr(nif)));
530 printf("mask %s ", ip4addr_ntoa(netif_ip4_netmask(nif)));
531 printf("gateway %s\n", ip4addr_ntoa(netif_ip4_gw(nif)));
532#endif /* LWIP_IPV4 */
533#if LWIP_IPV6
534 printf("IPV6: Host at %s\n", ip6addr_ntoa(netif_ip6_addr(nif, 0)));
535#endif /* LWIP_IPV6 */
536#if LWIP_NETIF_HOSTNAME
537 printf("FQDN: %s\n", netif_get_hostname(nif));
538#endif /* LWIP_NETIF_HOSTNAME */
539
540#if LWIP_MDNS_RESPONDER
541 if(tcpip_init_flag)
542 mdns_resp_netif_settings_changed(nif);
543#endif
544}
545#endif /* LWIP_NETIF_STATUS_CALLBACK */
546
547static void
548init_netifs(void)
549{
550#if LWIP_HAVE_SLIPIF
551#if SLIP_PTY_TEST
552 u8_t siodev_slip = 3;
553#else
554 u8_t siodev_slip = 0;
555#endif
556
557#if LWIP_IPV4
558 netif_add(&slipif, ip_2_ip4(&ipaddr_slip), ip_2_ip4(&netmask_slip), ip_2_ip4(&gw_slip),
559 (void*)&siodev_slip, slipif_init, tcpip_input);
560#else /* LWIP_IPV4 */
561 netif_add(&slipif, (void*)&siodev_slip, slipif_init, tcpip_input);
562#endif /* LWIP_IPV4 */
563#if LWIP_IPV6
564 netif_create_ip6_linklocal_address(&slipif, 1);
565#endif
566#if LWIP_NETIF_STATUS_CALLBACK
567 netif_set_status_callback(&slipif, netif_status_callback);
568#endif /* LWIP_NETIF_STATUS_CALLBACK */
569 netif_set_link_up(&slipif);
570 netif_set_up(&slipif);
571#endif /* LWIP_HAVE_SLIPIF */
572
573#if PPP_SUPPORT
574#if PPP_PTY_TEST
575 ppp_sio = sio_open(2);
576#else
577 ppp_sio = sio_open(0);
578#endif
579 if(!ppp_sio){
580 syslog_0(LOG_ERROR, "Error opening device: ");
581 slp_tsk();
582 }
583
584 ppp = pppos_create(&pppos_netif, ppp_output_cb, ppp_link_status_cb, NULL);
585 if(!ppp){
586 syslog_0(LOG_ERROR, "Could not create PPP control interface");
587 slp_tsk();
588 }
589
590#ifdef LWIP_PPP_CHAP_TEST
591 ppp_set_auth(ppp, PPPAUTHTYPE_CHAP, "lwip", "mysecret");
592#endif
593
594 ppp_connect(ppp, 0);
595
596#if LWIP_NETIF_STATUS_CALLBACK
597 netif_set_status_callback(&pppos_netif, netif_status_callback);
598#endif /* LWIP_NETIF_STATUS_CALLBACK */
599#endif /* PPP_SUPPORT */
600
601#if LWIP_IPV4
602#if LWIP_DHCP
603 IP_ADDR4(&gw, 0,0,0,0);
604 IP_ADDR4(&ipaddr, 0,0,0,0);
605 IP_ADDR4(&netmask, 0,0,0,0);
606#endif /* LWIP_DHCP */
607 netif_add(&netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw), NULL, ethernetif_init, tcpip_input);
608#else /* LWIP_IPV4 */
609 netif_add(&netif, NULL, ethernetif_init, tcpip_input);
610#endif /* LWIP_IPV4 */
611#if LWIP_IPV6
612 netif_create_ip6_linklocal_address(&netif, 1);
613 netif.ip6_autoconfig_enabled = 1;
614#endif
615#if LWIP_NETIF_STATUS_CALLBACK
616 netif_set_status_callback(&netif, netif_status_callback);
617#endif /* LWIP_NETIF_STATUS_CALLBACK */
618 netif_set_default(&netif);
619 netif_set_up(&netif);
620
621#if LWIP_DHCP
622 dhcp_start(&netif);
623#endif /* LWIP_DHCP */
624
625#if 0
626 /* Only used for testing purposes: */
627 netif_add(&ipaddr, &netmask, &gw, NULL, pcapif_init, tcpip_input);
628#endif
629
630#if LWIP_TCP
631 netio_init();
632#endif
633#if LWIP_TCP && LWIP_NETCONN
634 tcpecho_init();
635 shell_init();
636 httpd_init();
637#endif
638#if LWIP_UDP && LWIP_NETCONN
639 udpecho_init();
640#endif
641#if LWIP_SOCKET
642 chargen_init();
643#endif
644 /* sys_timeout(5000, tcp_debug_timeout, NULL);*/
645}
646
647/*-----------------------------------------------------------------------------------*/
648static void
649main_thread(void *arg)
650{
651 sys_sem_t sem;
652 LWIP_UNUSED_ARG(arg);
653
654 if(sys_sem_new(&sem, 0) != ERR_OK) {
655 LWIP_ASSERT("Failed to create semaphore", 0);
656 }
657 tcpip_init(tcpip_init_done, &sem);
658 sys_sem_wait(&sem);
659 syslog_0(LOG_NOTICE, "TCP/IP initialized.");
660
661#if LWIP_SOCKET && defined(USE_PINGSEND)
662 ping_init(&ping_addr);
663#endif
664
665 syslog_0(LOG_NOTICE, "Applications started.");
666
667#ifdef MEM_PERF
668 mem_perf_init("/tmp/memstats.client");
669#endif /* MEM_PERF */
670#if 0
671 stats_display();
672#endif
673#if PPP_SUPPORT
674 /* Block forever. */
675 sys_thread_new("pppos_rx_thread", pppos_rx_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
676 sys_sem_wait(&sem);
677#endif
678 tcpip_init_flag = true;
679}
680
681int wolfSSL_Debugging_ON(void);
682void esp_at_task(intptr_t exinf);
683
684/*
685 * メインタスク
686 */
687void
688main_task(intptr_t exinf)
689{
690#ifdef _STORAGEDEVICE_H_
691 StorageDevice_t *psdev;
692#endif /* _STORAGEDEVICE_H_ */
693 ER_UINT ercd;
694
695 SVC_PERROR(syslog_msk_log(LOG_UPTO(LOG_NOTICE), LOG_UPTO(LOG_EMERG)));
696 syslog(LOG_NOTICE, "Sample program starts (exinf = %d).", (int_t) exinf);
697
698 /*
699 * シリアルポートの初期化
700 *
701 * システムログタスクと同じシリアルポートを使う場合など,シリアル
702 * ポートがオープン済みの場合にはここでE_OBJエラーになるが,支障は
703 * ない.
704 */
705 ercd = serial_opn_por(TASK_PORTID);
706 if (ercd < 0 && MERCD(ercd) != E_OBJ) {
707 syslog(LOG_ERROR, "%s (%d) reported by `serial_opn_por'.",
708 itron_strerror(ercd), SERCD(ercd));
709 }
710 SVC_PERROR(serial_ctl_por(TASK_PORTID,
711 (IOCTL_CRLF | IOCTL_FCSND | IOCTL_FCRCV)));
712
713#ifdef _STORAGEDEVICE_H_
714 /*
715 * SD-CARDの設定
716 */
717 psdev = SDMGetStorageDevice(0);
718 psdev->_sdev_notice = storagedev_notice;
719 SDeviceHead._get_datetime = get_fat_time;
720#endif /* _STORAGEDEVICE_H_ */
721
722 /* startup defaults (may be overridden by one or more opts) */
723#if LWIP_IPV4
724 IP_ADDR4(&gw, 192,168, 0,1);
725 IP_ADDR4(&netmask, 255,255,255,0);
726 IP_ADDR4(&ipaddr, 192,168, 0,2);
727#if LWIP_HAVE_SLIPIF
728 IP_ADDR4(&gw_slip, 192,168, 2, 1);
729 IP_ADDR4(&netmask_slip, 255,255,255,255);
730 IP_ADDR4(&ipaddr_slip, 192,168, 2, 2);
731#endif
732#endif /* LWIP_IPV4 */
733
734 IP_SET_TYPE_VAL(ping_addr, IPADDR_TYPE_V4);
735 /* use debug flags defined by debug.h */
736 debug_flags = LWIP_DBG_OFF;
737
738 sys_init();
739 sys_thread_new("main_thread", main_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
740
741 //wolfSSL_Debugging_ON();
742 iothub_client_init();
743
744 esp_at_task(0);
745
746 syslog(LOG_NOTICE, "Sample program ends.");
747 slp_tsk();
748 SVC_PERROR(ext_ker());
749 assert(0);
750}
751
752/*
753 * ESP-WROOM-02テストの本体
754 * シールド上のプッシュスイッチがESP-WROOM-02の
755 * リセットスイッチです.リセット後、ATコマンドを使って、
756 * ESP-WROOM-02と通信ができます。
757 * 例えば、AT+GMRは、AT GMR(return)でESP-WROOM-02に送信され
758 * ESP-WROOM-02からの受信データはコンソールに表示されます.
759 */
760
761static char aTxBuffer[TX_BUF_SIZE];
762static uint8_t aRxBuffer[RX_BUF_SIZE];
763
764int rx_mode = MODE_DEFAULT;
765queue_t rx_queue;
766
767/*
768 * デバイスコマンド番号
769 */
770#define BASE_CMD_LEN 4
771
772static int_t mode_func(int argc, char **argv);
773static int_t count_func(int argc, char **argv);
774static int_t at_func(int argc, char **argv);
775
776/*
777 * デバイスコマンドテーブル
778 */
779static const COMMAND_INFO at_command_info[] = {
780 {"MODE", mode_func},
781 {"TCNT", count_func},
782 {"", at_func}
783};
784
785#define NUM_DEVICE_CMD (sizeof(at_command_info)/sizeof(COMMAND_INFO))
786
787static const char at_name[] = "AT";
788static const char at_help[] =
789" AT MODE (no) echo mode\n"
790" TCNT recived count\n"
791" any command\n";
792
793static COMMAND_LINK at_command_link = {
794 NULL,
795 NUM_DEVICE_CMD,
796 at_name,
797 NULL,
798 at_help,
799 &at_command_info[0]
800};
801
802static int a2i(char *str)
803{
804 int num = 0;
805
806 while(*str >= '0' && *str <= '9'){
807 num = num * 10 + *str++ - '0';
808 }
809 return num;
810}
811
812/*
813 * ATコマンド設定関数
814 */
815void at_info_init(intptr_t exinf)
816{
817 setup_command(&at_command_link);
818}
819
820/*
821 * ECHOモード設定関数
822 */
823static int_t mode_func(int argc, char **argv)
824{
825 int mode = rx_mode;
826
827 if(argc >= 2){
828 mode = a2i(argv[1]);
829 if(mode < 0 || mode > MODE_ECHO_HEX)
830 mode = MODE_DEFAULT;
831 rx_mode = mode;
832 }
833 printf("AT ECHO MODE(%d)\n", mode);
834 return mode;
835}
836
837/*
838 * 受信カウントコマンド取得関数
839 */
840static int_t count_func(int argc, char **argv)
841{
842 printf("AT RECIVED COUNT(%d)(%d)\n", rx_queue.size, rx_queue.clen);
843 return rx_queue.clen;
844}
845
846
847/*
848 * AT設定コマンド関数
849 */
850static int_t at_func(int argc, char **argv)
851{
852 int i, arg_count = BASE_CMD_LEN;
853 char *p = aTxBuffer;
854 char *s, c;
855 ER_UINT result;
856 int caps = 1;
857
858 for(i = 0 ; i < argc ; i++){
859 arg_count++;
860 arg_count += strlen(argv[i]);
861 }
862 *p++ = 'A';
863 *p++ = 'T';
864 if(arg_count > BASE_CMD_LEN){
865 for(i = 0 ; i < argc ; i++){
866 s = argv[i];
867 *p++ = '+';
868 while(*s != 0){
869 c = *s++;
870 if(c == '"')
871 caps ^= 1;
872 if(caps && c >= 'a' && c <= 'z')
873 c -= 0x20;
874 *p++ = c;
875 }
876 }
877 }
878 *p++ = '\r';
879 *p++ = '\n';
880 *p++ = 0;
881 result = serial_wri_dat(AT_PORTID, (const char *)aTxBuffer, arg_count);
882 if(result < 0){
883 syslog_1(LOG_ERROR, "AT command send error(%d) !", result);
884 }
885 printf("%d:%s", arg_count, aTxBuffer);
886 return arg_count;
887}
888
889/*
890 * SW1割込み
891 */
892void sw_int(void)
893{
894 syslog_0(LOG_NOTICE, "## sw_int() ##");
895}
896
897int esp_at_rx_handler(void *data, int len);
898
899/*
900 * ATコマンドシリアルタスク
901 */
902void esp_at_task(intptr_t exinf)
903{
904 T_SERIAL_RPOR k_rpor;
905 queue_t *rxque;
906 ER_UINT ercd;
907 int i, j, len;
908 int dlen = 0;
909 uint8_t ch;
910
911 init_esp_at();
912
913 SVC_PERROR(syslog_msk_log(LOG_UPTO(LOG_INFO), LOG_UPTO(LOG_EMERG)));
914 syslog(LOG_NOTICE, "Sample program starts (exinf = %d).", (int_t) exinf);
915
916 /*
917 * シリアルポートの初期化
918 *
919 * システムログタスクと同じシリアルポートを使う場合など,シリアル
920 * ポートがオープン済みの場合にはここでE_OBJエラーになるが,支障は
921 * ない.
922 */
923 ercd = serial_opn_por(TASK_PORTID);
924 if (ercd < 0 && MERCD(ercd) != E_OBJ) {
925 syslog(LOG_ERROR, "%s (%d) reported by `serial_opn_por'.",
926 itron_strerror(ercd), SERCD(ercd));
927 }
928 SVC_PERROR(serial_ctl_por(TASK_PORTID,
929 (IOCTL_CRLF | IOCTL_FCSND | IOCTL_FCRCV)));
930
931 /*
932 * キューバッファの初期化
933 */
934 rxque = &rx_queue;
935 rxque->size = RX_BUF_SIZE;
936 rxque->clen = 0;
937 rxque->head = 0;
938 rxque->tail = 0;
939 rxque->pbuffer = aRxBuffer;
940
941 /*
942 * ESP-WROOM02用シリアルポートの初期化
943 */
944 ercd = serial_opn_por(AT_PORTID);
945 if (ercd < 0 && MERCD(ercd) != E_OBJ) {
946 syslog(LOG_ERROR, "%s (%d) reported by `serial_opn_por'.(AT)",
947 itron_strerror(ercd), SERCD(ercd));
948 slp_tsk();
949 }
950 SVC_PERROR(serial_ctl_por(AT_PORTID, 0));
951 while(dlen < 500000){
952 serial_ref_por(AT_PORTID, &k_rpor);
953 len = k_rpor.reacnt;
954 if((rxque->size - rxque->clen) < len)
955 len = rxque->size - rxque->clen;
956 if(len > 0){
957 if(rxque->head >= rxque->tail){
958 if((rxque->size - rxque->head) < len)
959 i = rxque->size - rxque->head;
960 else
961 i = len;
962 j = serial_rea_dat(AT_PORTID, (char *)&rxque->pbuffer[rxque->head], i);
963 rxque->head += j;
964 if(rxque->head >= rxque->size)
965 rxque->head -= rxque->size;
966 rxque->clen += j;
967 len -= j;
968 }
969 if(len > 0){
970 j = serial_rea_dat(AT_PORTID, (char *)&rxque->pbuffer[rxque->head], len);
971 rxque->head += j;
972 if(rxque->head >= rxque->size)
973 rxque->head -= rxque->size;
974 rxque->clen += j;
975 len -= j;
976 }
977 }
978 while(rxque->clen > 0 && rx_mode != MODE_NOECHO){
979 if(rx_mode == MODE_ECHO_CHAR){
980 ch = rxque->pbuffer[rxque->tail];
981 if(ch >= 0x7f)
982 ch = '.';
983 else if(ch < 0x20 && ch != '\r' && ch != '\n')
984 ch = '.';
985 putchar(ch);
986 }
987 else
988 printf("%02x ", rxque->pbuffer[rxque->tail]);
989 rxque->clen--;
990 rxque->tail++;
991 if(rxque->tail >= rxque->size)
992 rxque->tail -= rxque->size;
993 dlen++;
994 if((dlen % 32) == 0 && rx_mode == MODE_ECHO_HEX){
995 printf("\n");
996 dly_tsk(50);
997 }
998 }
999 if (rxque->clen > 0) {
1000 len = rxque->clen;
1001 if (rxque->tail >= rxque->head) {
1002 if ((rxque->size - rxque->tail) < len)
1003 i = rxque->size - rxque->tail;
1004 else
1005 i = len;
1006 j = esp_at_rx_handler((char *)&rxque->pbuffer[rxque->tail], i);
1007 rxque->tail += j;
1008 if(rxque->tail >= rxque->size)
1009 rxque->tail -= rxque->size;
1010 rxque->clen -= j;
1011 len -= j;
1012 }
1013 if (len > 0) {
1014 j = esp_at_rx_handler((char *)&rxque->pbuffer[rxque->tail], len);
1015 rxque->tail += j;
1016 if(rxque->tail >= rxque->size)
1017 rxque->tail -= rxque->size;
1018 rxque->clen -= j;
1019 len -= j;
1020 }
1021 }
1022 else {
1023 dly_tsk(20);
1024 }
1025 }
1026 syslog_0(LOG_NOTICE, "## STOP ##");
1027 slp_tsk();
1028
1029 syslog(LOG_NOTICE, "Sample program ends.");
1030// SVC_PERROR(ext_ker());
1031}
Note: See TracBrowser for help on using the repository browser.