source: azure_iot_hub_f767zi/trunk/asp_baseplatform/lwip/contrib-2.1.0/examples/ppp/pppos_example.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: 6.8 KB
Line 
1/*
2 * Redistribution and use in source and binary forms, with or without modification,
3 * are permitted provided that the following conditions are met:
4 *
5 * 1. Redistributions of source code must retain the above copyright notice,
6 * this list of conditions and the following disclaimer.
7 * 2. Redistributions in binary form must reproduce the above copyright notice,
8 * this list of conditions and the following disclaimer in the documentation
9 * and/or other materials provided with the distribution.
10 * 3. The name of the author may not be used to endorse or promote products
11 * derived from this software without specific prior written permission.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
16 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
18 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
21 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
22 * OF SUCH DAMAGE.
23 *
24 * This file is part of the lwIP TCP/IP stack.
25 *
26 * Author: Dirk Ziegelmeier <dziegel@gmx.de>
27 *
28 */
29
30#include "lwip/dns.h"
31
32#ifndef PPPOS_SUPPORT
33#define PPPOS_SUPPORT 0
34#endif /* PPPOS_SUPPORT */
35
36#if PPPOS_SUPPORT
37#include "netif/ppp/pppos.h"
38#include "lwip/sio.h"
39#define PPP_PTY_TEST 1
40#endif /* PPPOS_SUPPORT */
41
42#include "pppos_example.h"
43
44#if PPPOS_SUPPORT
45static sio_fd_t ppp_sio;
46static ppp_pcb *ppp;
47static struct netif pppos_netif;
48
49static void
50pppos_rx_thread(void *arg)
51{
52 u32_t len;
53 u8_t buffer[128];
54 LWIP_UNUSED_ARG(arg);
55
56 /* Please read the "PPPoS input path" chapter in the PPP documentation. */
57 while (1) {
58 len = sio_read(ppp_sio, buffer, sizeof(buffer));
59 if (len > 0) {
60 /* Pass received raw characters from PPPoS to be decoded through lwIP
61 * TCPIP thread using the TCPIP API. This is thread safe in all cases
62 * but you should avoid passing data byte after byte. */
63 pppos_input_tcpip(ppp, buffer, len);
64 }
65 }
66}
67
68static void
69ppp_link_status_cb(ppp_pcb *pcb, int err_code, void *ctx)
70{
71 struct netif *pppif = ppp_netif(pcb);
72 LWIP_UNUSED_ARG(ctx);
73
74 switch(err_code) {
75 case PPPERR_NONE: /* No error. */
76 {
77#if LWIP_DNS
78 const ip_addr_t *ns;
79#endif /* LWIP_DNS */
80 fprintf(stderr, "ppp_link_status_cb: PPPERR_NONE\n\r");
81#if LWIP_IPV4
82 fprintf(stderr, " our_ip4addr = %s\n\r", ip4addr_ntoa(netif_ip4_addr(pppif)));
83 fprintf(stderr, " his_ipaddr = %s\n\r", ip4addr_ntoa(netif_ip4_gw(pppif)));
84 fprintf(stderr, " netmask = %s\n\r", ip4addr_ntoa(netif_ip4_netmask(pppif)));
85#endif /* LWIP_IPV4 */
86#if LWIP_IPV6
87 fprintf(stderr, " our_ip6addr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
88#endif /* LWIP_IPV6 */
89
90#if LWIP_DNS
91 ns = dns_getserver(0);
92 fprintf(stderr, " dns1 = %s\n\r", ipaddr_ntoa(ns));
93 ns = dns_getserver(1);
94 fprintf(stderr, " dns2 = %s\n\r", ipaddr_ntoa(ns));
95#endif /* LWIP_DNS */
96#if PPP_IPV6_SUPPORT
97 fprintf(stderr, " our6_ipaddr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
98#endif /* PPP_IPV6_SUPPORT */
99 }
100 break;
101
102 case PPPERR_PARAM: /* Invalid parameter. */
103 printf("ppp_link_status_cb: PPPERR_PARAM\n");
104 break;
105
106 case PPPERR_OPEN: /* Unable to open PPP session. */
107 printf("ppp_link_status_cb: PPPERR_OPEN\n");
108 break;
109
110 case PPPERR_DEVICE: /* Invalid I/O device for PPP. */
111 printf("ppp_link_status_cb: PPPERR_DEVICE\n");
112 break;
113
114 case PPPERR_ALLOC: /* Unable to allocate resources. */
115 printf("ppp_link_status_cb: PPPERR_ALLOC\n");
116 break;
117
118 case PPPERR_USER: /* User interrupt. */
119 printf("ppp_link_status_cb: PPPERR_USER\n");
120 break;
121
122 case PPPERR_CONNECT: /* Connection lost. */
123 printf("ppp_link_status_cb: PPPERR_CONNECT\n");
124 break;
125
126 case PPPERR_AUTHFAIL: /* Failed authentication challenge. */
127 printf("ppp_link_status_cb: PPPERR_AUTHFAIL\n");
128 break;
129
130 case PPPERR_PROTOCOL: /* Failed to meet protocol. */
131 printf("ppp_link_status_cb: PPPERR_PROTOCOL\n");
132 break;
133
134 case PPPERR_PEERDEAD: /* Connection timeout. */
135 printf("ppp_link_status_cb: PPPERR_PEERDEAD\n");
136 break;
137
138 case PPPERR_IDLETIMEOUT: /* Idle Timeout. */
139 printf("ppp_link_status_cb: PPPERR_IDLETIMEOUT\n");
140 break;
141
142 case PPPERR_CONNECTTIME: /* PPPERR_CONNECTTIME. */
143 printf("ppp_link_status_cb: PPPERR_CONNECTTIME\n");
144 break;
145
146 case PPPERR_LOOPBACK: /* Connection timeout. */
147 printf("ppp_link_status_cb: PPPERR_LOOPBACK\n");
148 break;
149
150 default:
151 printf("ppp_link_status_cb: unknown errCode %d\n", err_code);
152 break;
153 }
154}
155
156static u32_t
157ppp_output_cb(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx)
158{
159 LWIP_UNUSED_ARG(pcb);
160 LWIP_UNUSED_ARG(ctx);
161 return sio_write(ppp_sio, data, len);
162}
163
164#if LWIP_NETIF_STATUS_CALLBACK
165static void
166netif_status_callback(struct netif *nif)
167{
168 printf("PPPNETIF: %c%c%d is %s\n", nif->name[0], nif->name[1], nif->num,
169 netif_is_up(nif) ? "UP" : "DOWN");
170#if LWIP_IPV4
171 printf("IPV4: Host at %s ", ip4addr_ntoa(netif_ip4_addr(nif)));
172 printf("mask %s ", ip4addr_ntoa(netif_ip4_netmask(nif)));
173 printf("gateway %s\n", ip4addr_ntoa(netif_ip4_gw(nif)));
174#endif /* LWIP_IPV4 */
175#if LWIP_IPV6
176 printf("IPV6: Host at %s\n", ip6addr_ntoa(netif_ip6_addr(nif, 0)));
177#endif /* LWIP_IPV6 */
178#if LWIP_NETIF_HOSTNAME
179 printf("FQDN: %s\n", netif_get_hostname(nif));
180#endif /* LWIP_NETIF_HOSTNAME */
181}
182#endif /* LWIP_NETIF_STATUS_CALLBACK */
183#endif
184
185void
186pppos_example_init(void)
187{
188#if PPPOS_SUPPORT
189#if PPP_PTY_TEST
190 ppp_sio = sio_open(2);
191#else
192 ppp_sio = sio_open(0);
193#endif
194 if(!ppp_sio)
195 {
196 perror("PPPOS example: Error opening device");
197 return;
198 }
199
200 ppp = pppos_create(&pppos_netif, ppp_output_cb, ppp_link_status_cb, NULL);
201 if (!ppp)
202 {
203 printf("PPPOS example: Could not create PPP control interface");
204 return;
205 }
206
207#ifdef LWIP_PPP_CHAP_TEST
208 ppp_set_auth(ppp, PPPAUTHTYPE_CHAP, "lwip", "mysecret");
209#endif
210
211 ppp_connect(ppp, 0);
212
213#if LWIP_NETIF_STATUS_CALLBACK
214 netif_set_status_callback(&pppos_netif, netif_status_callback);
215#endif /* LWIP_NETIF_STATUS_CALLBACK */
216
217 sys_thread_new("pppos_rx_thread", pppos_rx_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
218#endif /* PPPOS_SUPPORT */
219}
Note: See TracBrowser for help on using the repository browser.