• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdio.h>
45 
46 #if PPPOS_SUPPORT
47 static sio_fd_t ppp_sio;
48 static ppp_pcb *ppp;
49 static struct netif pppos_netif;
50 
51 static void
pppos_rx_thread(void * arg)52 pppos_rx_thread(void *arg)
53 {
54   u32_t len;
55   u8_t buffer[128];
56   LWIP_UNUSED_ARG(arg);
57 
58   /* Please read the "PPPoS input path" chapter in the PPP documentation. */
59   while (1) {
60     len = sio_read(ppp_sio, buffer, sizeof(buffer));
61     if (len > 0) {
62       /* Pass received raw characters from PPPoS to be decoded through lwIP
63        * TCPIP thread using the TCPIP API. This is thread safe in all cases
64        * but you should avoid passing data byte after byte. */
65       pppos_input_tcpip(ppp, buffer, len);
66     }
67   }
68 }
69 
70 static void
ppp_link_status_cb(ppp_pcb * pcb,int err_code,void * ctx)71 ppp_link_status_cb(ppp_pcb *pcb, int err_code, void *ctx)
72 {
73     struct netif *pppif = ppp_netif(pcb);
74     LWIP_UNUSED_ARG(ctx);
75 
76     switch(err_code) {
77     case PPPERR_NONE:               /* No error. */
78         {
79 #if LWIP_DNS
80         const ip_addr_t *ns;
81 #endif /* LWIP_DNS */
82         fprintf(stderr, "ppp_link_status_cb: PPPERR_NONE\n\r");
83 #if LWIP_IPV4
84         fprintf(stderr, "   our_ip4addr = %s\n\r", ip4addr_ntoa(netif_ip4_addr(pppif)));
85         fprintf(stderr, "   his_ipaddr  = %s\n\r", ip4addr_ntoa(netif_ip4_gw(pppif)));
86         fprintf(stderr, "   netmask     = %s\n\r", ip4addr_ntoa(netif_ip4_netmask(pppif)));
87 #endif /* LWIP_IPV4 */
88 #if LWIP_IPV6
89         fprintf(stderr, "   our_ip6addr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
90 #endif /* LWIP_IPV6 */
91 
92 #if LWIP_DNS
93         ns = dns_getserver(0);
94         fprintf(stderr, "   dns1        = %s\n\r", ipaddr_ntoa(ns));
95         ns = dns_getserver(1);
96         fprintf(stderr, "   dns2        = %s\n\r", ipaddr_ntoa(ns));
97 #endif /* LWIP_DNS */
98 #if PPP_IPV6_SUPPORT
99         fprintf(stderr, "   our6_ipaddr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
100 #endif /* PPP_IPV6_SUPPORT */
101         }
102         break;
103 
104     case PPPERR_PARAM:             /* Invalid parameter. */
105         printf("ppp_link_status_cb: PPPERR_PARAM\n");
106         break;
107 
108     case PPPERR_OPEN:              /* Unable to open PPP session. */
109         printf("ppp_link_status_cb: PPPERR_OPEN\n");
110         break;
111 
112     case PPPERR_DEVICE:            /* Invalid I/O device for PPP. */
113         printf("ppp_link_status_cb: PPPERR_DEVICE\n");
114         break;
115 
116     case PPPERR_ALLOC:             /* Unable to allocate resources. */
117         printf("ppp_link_status_cb: PPPERR_ALLOC\n");
118         break;
119 
120     case PPPERR_USER:              /* User interrupt. */
121         printf("ppp_link_status_cb: PPPERR_USER\n");
122         break;
123 
124     case PPPERR_CONNECT:           /* Connection lost. */
125         printf("ppp_link_status_cb: PPPERR_CONNECT\n");
126         break;
127 
128     case PPPERR_AUTHFAIL:          /* Failed authentication challenge. */
129         printf("ppp_link_status_cb: PPPERR_AUTHFAIL\n");
130         break;
131 
132     case PPPERR_PROTOCOL:          /* Failed to meet protocol. */
133         printf("ppp_link_status_cb: PPPERR_PROTOCOL\n");
134         break;
135 
136     case PPPERR_PEERDEAD:          /* Connection timeout. */
137         printf("ppp_link_status_cb: PPPERR_PEERDEAD\n");
138         break;
139 
140     case PPPERR_IDLETIMEOUT:       /* Idle Timeout. */
141         printf("ppp_link_status_cb: PPPERR_IDLETIMEOUT\n");
142         break;
143 
144     case PPPERR_CONNECTTIME:       /* PPPERR_CONNECTTIME. */
145         printf("ppp_link_status_cb: PPPERR_CONNECTTIME\n");
146         break;
147 
148     case PPPERR_LOOPBACK:          /* Connection timeout. */
149         printf("ppp_link_status_cb: PPPERR_LOOPBACK\n");
150         break;
151 
152     default:
153         printf("ppp_link_status_cb: unknown errCode %d\n", err_code);
154         break;
155     }
156 }
157 
158 static u32_t
ppp_output_cb(ppp_pcb * pcb,const void * data,u32_t len,void * ctx)159 ppp_output_cb(ppp_pcb *pcb, const void *data, u32_t len, void *ctx)
160 {
161   LWIP_UNUSED_ARG(pcb);
162   LWIP_UNUSED_ARG(ctx);
163   return sio_write(ppp_sio, (const u8_t*)data, len);
164 }
165 
166 #if LWIP_NETIF_STATUS_CALLBACK
167 static void
netif_status_callback(struct netif * nif)168 netif_status_callback(struct netif *nif)
169 {
170   printf("PPPNETIF: %c%c%d is %s\n", nif->name[0], nif->name[1], nif->num,
171          netif_is_up(nif) ? "UP" : "DOWN");
172 #if LWIP_IPV4
173   printf("IPV4: Host at %s ", ip4addr_ntoa(netif_ip4_addr(nif)));
174   printf("mask %s ", ip4addr_ntoa(netif_ip4_netmask(nif)));
175   printf("gateway %s\n", ip4addr_ntoa(netif_ip4_gw(nif)));
176 #endif /* LWIP_IPV4 */
177 #if LWIP_IPV6
178   printf("IPV6: Host at %s\n", ip6addr_ntoa(netif_ip6_addr(nif, 0)));
179 #endif /* LWIP_IPV6 */
180 #if LWIP_NETIF_HOSTNAME
181   printf("FQDN: %s\n", netif_get_hostname(nif));
182 #endif /* LWIP_NETIF_HOSTNAME */
183 }
184 #endif /* LWIP_NETIF_STATUS_CALLBACK */
185 #endif
186 
187 void
pppos_example_init(void)188 pppos_example_init(void)
189 {
190 #if PPPOS_SUPPORT
191 #if PPP_PTY_TEST
192   ppp_sio = sio_open(2);
193 #else
194   ppp_sio = sio_open(0);
195 #endif
196   if(!ppp_sio)
197   {
198       perror("PPPOS example: Error opening device");
199       return;
200   }
201 
202   ppp = pppos_create(&pppos_netif, ppp_output_cb, ppp_link_status_cb, NULL);
203   if (!ppp)
204   {
205       printf("PPPOS example: Could not create PPP control interface");
206       return;
207   }
208 
209 #ifdef LWIP_PPP_CHAP_TEST
210   ppp_set_auth(ppp, PPPAUTHTYPE_CHAP, "lwip", "mysecret");
211 #endif
212 
213   ppp_connect(ppp, 0);
214 
215 #if LWIP_NETIF_STATUS_CALLBACK
216   netif_set_status_callback(&pppos_netif, netif_status_callback);
217 #endif /* LWIP_NETIF_STATUS_CALLBACK */
218 
219   sys_thread_new("pppos_rx_thread", pppos_rx_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
220 #endif /* PPPOS_SUPPORT */
221 }
222