esp_modem_compat.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include "esp_netif.h"
  16. #include "esp_netif_ppp.h"
  17. #include "esp_modem.h"
  18. #include "esp_modem_netif.h"
  19. #include "esp_log.h"
  20. static const char *TAG = "esp-modem-compat";
  21. static void on_modem_compat_handler(void *arg, esp_event_base_t event_base,
  22. int32_t event_id, void *event_data)
  23. {
  24. int32_t compat_event_id = MODEM_EVENT_UNKNOWN;
  25. switch (event_id) {
  26. case ESP_MODEM_EVENT_PPP_START:
  27. compat_event_id = MODEM_EVENT_PPP_START;
  28. break;
  29. case ESP_MODEM_EVENT_PPP_STOP:
  30. compat_event_id = MODEM_EVENT_PPP_STOP;
  31. break;
  32. default:
  33. break;
  34. }
  35. esp_event_post(ESP_MODEM_EVENT, compat_event_id, NULL, 0, 0);
  36. }
  37. static void on_ip_event(void *arg, esp_event_base_t event_base,
  38. int32_t event_id, void *event_data)
  39. {
  40. ESP_LOGI(TAG, "IP event! %d", event_id);
  41. if (event_id == IP_EVENT_PPP_GOT_IP) {
  42. esp_netif_dns_info_t dns_info;
  43. ppp_client_ip_info_t ipinfo = {0};
  44. ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  45. esp_netif_t *netif = event->esp_netif;
  46. ipinfo.ip.addr = event->ip_info.ip.addr;
  47. ipinfo.gw.addr = event->ip_info.gw.addr;
  48. ipinfo.netmask.addr = event->ip_info.netmask.addr;
  49. esp_netif_get_dns_info(netif, 0, &dns_info);
  50. ipinfo.ns1.addr = dns_info.ip.u_addr.ip4.addr;
  51. ipinfo.ns2.addr = dns_info.ip.u_addr.ip4.addr;
  52. esp_event_post(ESP_MODEM_EVENT, MODEM_EVENT_PPP_CONNECT, &ipinfo, sizeof(ipinfo), 0);
  53. } else if (event_id == IP_EVENT_PPP_LOST_IP) {
  54. ESP_LOGI(TAG, "Modem Disconnect from PPP Server");
  55. esp_event_post(ESP_MODEM_EVENT, MODEM_EVENT_PPP_DISCONNECT, NULL, 0, 0);
  56. }
  57. }
  58. esp_err_t esp_modem_add_event_handler(modem_dte_t *dte, esp_event_handler_t handler, void *handler_args)
  59. {
  60. // event loop has to be created when using this API -- create and ignore failure if already created
  61. esp_event_loop_create_default();
  62. ESP_ERROR_CHECK(esp_event_handler_register(ESP_MODEM_EVENT, MODEM_EVENT_PPP_START, handler, handler_args));
  63. ESP_ERROR_CHECK(esp_event_handler_register(ESP_MODEM_EVENT, MODEM_EVENT_PPP_CONNECT, handler, handler_args));
  64. ESP_ERROR_CHECK(esp_event_handler_register(ESP_MODEM_EVENT, MODEM_EVENT_PPP_DISCONNECT, handler, handler_args));
  65. ESP_ERROR_CHECK(esp_event_handler_register(ESP_MODEM_EVENT, MODEM_EVENT_PPP_STOP, handler, handler_args));
  66. return esp_modem_set_event_handler(dte, on_modem_compat_handler, ESP_EVENT_ANY_ID, handler_args);
  67. }
  68. esp_err_t esp_modem_setup_ppp(modem_dte_t *dte)
  69. {
  70. #if CONFIG_LWIP_PPP_PAP_SUPPORT
  71. esp_netif_auth_type_t auth_type = NETIF_PPP_AUTHTYPE_PAP;
  72. #elif CONFIG_LWIP_PPP_CHAP_SUPPORT
  73. esp_netif_auth_type_t auth_type = NETIF_PPP_AUTHTYPE_CHAP;
  74. #else
  75. #error "Unsupported AUTH Negotiation"
  76. #endif
  77. // Init netif object
  78. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_PPP();
  79. esp_netif_t *esp_netif = esp_netif_new(&cfg);
  80. assert(esp_netif);
  81. // event loop has to be created when using this API -- create and ignore failure if already created
  82. esp_event_loop_create_default();
  83. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event, NULL));
  84. esp_netif_ppp_set_auth(esp_netif, auth_type, CONFIG_EXAMPLE_MODEM_PPP_AUTH_USERNAME, CONFIG_EXAMPLE_MODEM_PPP_AUTH_PASSWORD);
  85. void *modem_netif_adapter = esp_modem_netif_setup(dte);
  86. esp_modem_netif_set_default_handlers(modem_netif_adapter, esp_netif);
  87. /* attach the modem to the network interface */
  88. return esp_netif_attach(esp_netif, modem_netif_adapter);
  89. }
  90. esp_err_t esp_modem_exit_ppp(modem_dte_t *dte)
  91. {
  92. // Note: A minor memory leak is expected when using esp-modem-compat
  93. return esp_modem_stop_ppp(dte);
  94. }