pppos_example.c 6.8 KB

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