esp_modem: add simple compatibility layer for new esp_modem connecting over esp-netif

This commit is contained in:
David Cermak 2019-11-21 13:20:13 +01:00 committed by bot
parent 25913af2cc
commit 04a25394aa
7 changed files with 180 additions and 14 deletions

View File

@ -1,6 +1,7 @@
set(srcs "src/esp_modem.c"
"src/esp_modem_dce_service"
"src/esp_modem_netif.c"
"src/esp_modem_compat.c"
"src/sim800.c"
"src/bg96.c")

View File

@ -21,6 +21,7 @@ extern "C" {
#include "esp_modem_dte.h"
#include "esp_event.h"
#include "driver/uart.h"
#include "esp_modem_compat.h"
/**
* @brief Declare Event Base for ESP Modem
@ -33,9 +34,9 @@ ESP_EVENT_DECLARE_BASE(ESP_MODEM_EVENT);
*
*/
typedef enum {
MODEM_EVENT_PPP_START, /*!< ESP Modem Start PPP Session */
MODEM_EVENT_PPP_STOP, /*!< ESP Modem Stop PPP Session*/
MODEM_EVENT_UNKNOWN /*!< ESP Modem Unknown Response */
ESP_MODEM_EVENT_PPP_START = 0, /*!< ESP Modem Start PPP Session */
ESP_MODEM_EVENT_PPP_STOP = 3, /*!< ESP Modem Stop PPP Session*/
ESP_MODEM_EVENT_UNKNOWN = 4 /*!< ESP Modem Unknown Response */
} esp_modem_event_t;
/**
@ -91,7 +92,7 @@ modem_dte_t *esp_modem_dte_init(const esp_modem_dte_config_t *config);
* - ESP_ERR_NO_MEM on allocating memory for the handler failed
* - ESP_ERR_INVALID_ARG on invalid combination of event base and event id
*/
esp_err_t esp_modem_add_event_handler(modem_dte_t *dte, esp_event_handler_t handler, int32_t event_id, void *handler_args);
esp_err_t esp_modem_set_event_handler(modem_dte_t *dte, esp_event_handler_t handler, int32_t event_id, void *handler_args);
/**
* @brief Unregister event handler for ESP Modem event loop

View File

@ -0,0 +1,62 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "lwip/ip.h"
/**
* @brief ESP Modem Event backward compatible version
*/
typedef enum {
MODEM_EVENT_PPP_START = 0x100,
MODEM_EVENT_PPP_CONNECT = 0x101,
MODEM_EVENT_PPP_DISCONNECT = 0x102,
MODEM_EVENT_PPP_STOP = 0x103,
MODEM_EVENT_UNKNOWN = 0x104,
} esp_modem_compat_event_t;
/**
* @brief PPPoS Client IP Information backward compatible version
*
*/
typedef struct {
ip4_addr_t ip; /*!< IP Address */
ip4_addr_t netmask; /*!< Net Mask */
ip4_addr_t gw; /*!< Gateway */
ip4_addr_t ns1; /*!< Name Server1 */
ip4_addr_t ns2; /*!< Name Server2 */
} ppp_client_ip_info_t;
/**
* @brief Backward compatible version of esp_modem_set_event_handler()
*/
esp_err_t esp_modem_add_event_handler(modem_dte_t *dte, esp_event_handler_t handler, void *handler_args) __attribute__ ((deprecated));
/**
* @brief Backward compatible version of creating esp-netif(PPP) and attaching to esp_modem_start_ppp()
*/
esp_err_t esp_modem_setup_ppp(modem_dte_t *dte) __attribute__ ((deprecated));
/**
* @brief Backward compatible version of deleting esp-netif and esp_modem_stop_ppp()
*/
esp_err_t esp_modem_exit_ppp(modem_dte_t *dte) __attribute__ ((deprecated));
#ifdef __cplusplus
}
#endif

View File

@ -91,8 +91,8 @@ static esp_err_t esp_dte_handle_line(esp_modem_dte_t *esp_dte)
}
return ESP_OK;
err_handle:
/* Send MODEM_EVENT_UNKNOWN signal to event loop */
esp_event_post_to(esp_dte->event_loop_hdl, ESP_MODEM_EVENT, MODEM_EVENT_UNKNOWN,
/* Send ESP_MODEM_EVENT_UNKNOWN signal to event loop */
esp_event_post_to(esp_dte->event_loop_hdl, ESP_MODEM_EVENT, ESP_MODEM_EVENT_UNKNOWN,
(void *)line, strlen(line) + 1, pdMS_TO_TICKS(100));
err:
return ESP_FAIL;
@ -445,7 +445,7 @@ err_dte_mem:
return NULL;
}
esp_err_t esp_modem_add_event_handler(modem_dte_t *dte, esp_event_handler_t handler, int32_t event_id, void *handler_args)
esp_err_t esp_modem_set_event_handler(modem_dte_t *dte, esp_event_handler_t handler, int32_t event_id, void *handler_args)
{
esp_modem_dte_t *esp_dte = __containerof(dte, esp_modem_dte_t, parent);
return esp_event_handler_register_with(esp_dte->event_loop_hdl, ESP_MODEM_EVENT, event_id, handler, handler_args);
@ -468,7 +468,7 @@ esp_err_t esp_modem_start_ppp(modem_dte_t *dte)
MODEM_CHECK(dte->change_mode(dte, MODEM_PPP_MODE) == ESP_OK, "enter ppp mode failed", err);
/* post PPP mode started event */
esp_event_post_to(esp_dte->event_loop_hdl, ESP_MODEM_EVENT, MODEM_EVENT_PPP_START, NULL, 0, 0);
esp_event_post_to(esp_dte->event_loop_hdl, ESP_MODEM_EVENT, ESP_MODEM_EVENT_PPP_START, NULL, 0, 0);
return ESP_OK;
err:
return ESP_FAIL;

View File

@ -0,0 +1,102 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include "esp_netif.h"
#include "esp_netif_ppp.h"
#include "esp_modem.h"
#include "esp_modem_netif.h"
#include "esp_log.h"
static const char *TAG = "esp-modem-compat";
static void on_modem_compat_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
int32_t compat_event_id = MODEM_EVENT_UNKNOWN;
switch (event_id) {
case ESP_MODEM_EVENT_PPP_START:
compat_event_id = MODEM_EVENT_PPP_START;
break;
case ESP_MODEM_EVENT_PPP_STOP:
compat_event_id = MODEM_EVENT_PPP_STOP;
break;
default:
break;
}
esp_event_post(ESP_MODEM_EVENT, compat_event_id, NULL, 0, 0);
}
static void on_ip_event(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ESP_LOGI(TAG, "IP event! %d", event_id);
if (event_id == IP_EVENT_PPP_GOT_IP) {
esp_netif_dns_info_t dns_info;
ppp_client_ip_info_t ipinfo = {0};
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
esp_netif_t *netif = event->esp_netif;
ipinfo.ip.addr = event->ip_info.ip.addr;
ipinfo.gw.addr = event->ip_info.gw.addr;
ipinfo.netmask.addr = event->ip_info.netmask.addr;
esp_netif_get_dns_info(netif, 0, &dns_info);
ipinfo.ns1.addr = dns_info.ip.u_addr.ip4.addr;
ipinfo.ns2.addr = dns_info.ip.u_addr.ip4.addr;
esp_event_post(ESP_MODEM_EVENT, MODEM_EVENT_PPP_CONNECT, &ipinfo, sizeof(ipinfo), 0);
} else if (event_id == IP_EVENT_PPP_LOST_IP) {
ESP_LOGI(TAG, "Modem Disconnect from PPP Server");
esp_event_post(ESP_MODEM_EVENT, MODEM_EVENT_PPP_DISCONNECT, NULL, 0, 0);
}
}
esp_err_t esp_modem_add_event_handler(modem_dte_t *dte, esp_event_handler_t handler, void *handler_args)
{
// event loop has to be created when using this API -- create and ignore failure if already created
esp_event_loop_create_default();
ESP_ERROR_CHECK(esp_event_handler_register(ESP_MODEM_EVENT, MODEM_EVENT_PPP_START, handler, handler_args));
ESP_ERROR_CHECK(esp_event_handler_register(ESP_MODEM_EVENT, MODEM_EVENT_PPP_CONNECT, handler, handler_args));
ESP_ERROR_CHECK(esp_event_handler_register(ESP_MODEM_EVENT, MODEM_EVENT_PPP_DISCONNECT, handler, handler_args));
ESP_ERROR_CHECK(esp_event_handler_register(ESP_MODEM_EVENT, MODEM_EVENT_PPP_STOP, handler, handler_args));
return esp_modem_set_event_handler(dte, on_modem_compat_handler, ESP_EVENT_ANY_ID, handler_args);
}
esp_err_t esp_modem_setup_ppp(modem_dte_t *dte)
{
#if CONFIG_LWIP_PPP_PAP_SUPPORT
esp_netif_auth_type_t auth_type = NETIF_PPP_AUTHTYPE_PAP;
#elif CONFIG_LWIP_PPP_CHAP_SUPPORT
esp_netif_auth_type_t auth_type = NETIF_PPP_AUTHTYPE_CHAP;
#else
#error "Unsupported AUTH Negotiation"
#endif
// Init netif object
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_PPP();
esp_netif_t *esp_netif = esp_netif_new(&cfg);
assert(esp_netif);
// event loop has to be created when using this API -- create and ignore failure if already created
esp_event_loop_create_default();
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event, NULL));
esp_netif_ppp_set_auth(esp_netif, auth_type, CONFIG_EXAMPLE_MODEM_PPP_AUTH_USERNAME, CONFIG_EXAMPLE_MODEM_PPP_AUTH_PASSWORD);
void *modem_netif_adapter = esp_modem_netif_setup(dte);
esp_modem_netif_set_default_handlers(modem_netif_adapter, esp_netif);
/* attach the modem to the network interface */
return esp_netif_attach(esp_netif, modem_netif_adapter);
}
esp_err_t esp_modem_exit_ppp(modem_dte_t *dte)
{
// Note: A minor memory leak is expected when using esp-modem-compat
return esp_modem_stop_ppp(dte);
}

View File

@ -138,11 +138,11 @@ esp_err_t esp_modem_netif_set_default_handlers(void *h, esp_netif_t * esp_netif)
{
esp_modem_netif_driver_t *driver = h;
esp_err_t ret;
ret = esp_modem_add_event_handler(driver->dte, esp_netif_action_start, MODEM_EVENT_PPP_START, esp_netif);
ret = esp_modem_set_event_handler(driver->dte, esp_netif_action_start, ESP_MODEM_EVENT_PPP_START, esp_netif);
if (ret != ESP_OK) {
goto set_event_failed;
}
ret = esp_modem_add_event_handler(driver->dte, esp_netif_action_stop, MODEM_EVENT_PPP_STOP, esp_netif);
ret = esp_modem_set_event_handler(driver->dte, esp_netif_action_stop, ESP_MODEM_EVENT_PPP_STOP, esp_netif);
if (ret != ESP_OK) {
goto set_event_failed;
}

View File

@ -111,14 +111,14 @@ err:
static void modem_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
switch (event_id) {
case MODEM_EVENT_PPP_START:
case ESP_MODEM_EVENT_PPP_START:
ESP_LOGI(TAG, "Modem PPP Started");
break;
case MODEM_EVENT_PPP_STOP:
case ESP_MODEM_EVENT_PPP_STOP:
ESP_LOGI(TAG, "Modem PPP Stopped");
xEventGroupSetBits(event_group, STOP_BIT);
break;
case MODEM_EVENT_UNKNOWN:
case ESP_MODEM_EVENT_UNKNOWN:
ESP_LOGW(TAG, "Unknow line received: %s", (char *)event_data);
break;
default:
@ -231,7 +231,7 @@ void app_main(void)
esp_modem_dte_config_t config = ESP_MODEM_DTE_DEFAULT_CONFIG();
modem_dte_t *dte = esp_modem_dte_init(&config);
/* Register event handler */
ESP_ERROR_CHECK(esp_modem_add_event_handler(dte, modem_event_handler, ESP_EVENT_ANY_ID, NULL));
ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, modem_event_handler, ESP_EVENT_ANY_ID, NULL));
/* create dce object */
#if CONFIG_EXAMPLE_MODEM_DEVICE_SIM800
modem_dce_t *dce = sim800_init(dte);