1 // Copyright 2015-2018 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 
15 #pragma once
16 
17 #include "esp_netif.h"
18 #include "esp_netif_ppp.h"
19 #include "esp_netif_slip.h"
20 #include "lwip/netif.h"
21 
22 #ifdef CONFIG_ESP_NETIF_TCPIP_LWIP
23 
24 struct esp_netif_netstack_lwip_vanilla_config {
25     err_t (*init_fn)(struct netif*);
26     void (*input_fn)(void *netif, void *buffer, size_t len, void *eb);
27 };
28 
29 struct esp_netif_netstack_lwip_ppp_config {
30     void (*input_fn)(void *netif, void *buffer, size_t len, void *eb);
31     esp_netif_ppp_config_t ppp_events;
32 };
33 
34 struct esp_netif_netstack_lwip_slip_config {
35     err_t (*init_fn)(struct netif*);
36     void (*input_fn)(void *netif, void *buffer, size_t len, void *eb);
37     esp_netif_slip_config_t slip_config;
38 };
39 
40 // LWIP netif specific network stack configuration
41 struct esp_netif_netstack_config {
42     union {
43         struct esp_netif_netstack_lwip_vanilla_config lwip;
44         struct esp_netif_netstack_lwip_ppp_config lwip_ppp;
45     };
46 };
47 
48 struct esp_netif_api_msg_s;
49 
50 typedef int (*esp_netif_api_fn)(struct esp_netif_api_msg_s *msg);
51 
52 typedef struct esp_netif_api_msg_s {
53     int type;  /**< The first field MUST be int */
54     int ret;
55     esp_netif_api_fn api_fn;
56     esp_netif_t *esp_netif;
57     void    *data;
58 } esp_netif_api_msg_t;
59 
60 
61 typedef struct esp_netif_dns_param_s {
62     esp_netif_dns_type_t dns_type;
63     esp_netif_dns_info_t *dns_info;
64 } esp_netif_dns_param_t;
65 
66 typedef struct esp_netif_ip_lost_timer_s {
67     bool timer_running;
68 } esp_netif_ip_lost_timer_t;
69 
70 /**
71  * @brief Check the netif if of a specific P2P type
72  */
73 #if CONFIG_PPP_SUPPORT || CONFIG_LWIP_SLIP_SUPPORT
74 #define _IS_NETIF_POINT2POINT_TYPE(netif, type) (netif->related_data && netif->related_data->is_point2point && netif->related_data->netif_type == type)
75 #else
76 #define _IS_NETIF_POINT2POINT_TYPE(netif, type) false
77 #endif
78 
79 /**
80  * @brief Additional netif types when related data are needed
81  */
82 enum netif_types {
83     COMMON_LWIP_NETIF,
84     PPP_LWIP_NETIF,
85     SLIP_LWIP_NETIF
86 };
87 
88 /**
89  * @brief Related data to esp-netif (additional data for some special types of netif
90  * (typically for point-point network types, such as PPP or SLIP)
91  */
92 typedef struct netif_related_data {
93     bool is_point2point;
94     enum netif_types netif_type;
95 } netif_related_data_t;
96 
97 /**
98  * @brief Main esp-netif container with interface related information
99  */
100 struct esp_netif_obj {
101     // default interface addresses
102     uint8_t mac[NETIF_MAX_HWADDR_LEN];
103     esp_netif_ip_info_t* ip_info;
104     esp_netif_ip_info_t* ip_info_old;
105 
106     // lwip netif related
107     struct netif *lwip_netif;
108     err_t (*lwip_init_fn)(struct netif*);
109     void (*lwip_input_fn)(void *input_netif_handle, void *buffer, size_t len, void *eb);
110     void * netif_handle;    // netif impl context (either vanilla lwip-netif or ppp_pcb)
111     netif_related_data_t *related_data; // holds additional data for specific netifs
112 
113     // io driver related
114     void* driver_handle;
115     esp_err_t (*driver_transmit)(void *h, void *buffer, size_t len);
116     esp_err_t (*driver_transmit_wrap)(void *h, void *buffer, size_t len, void *pbuf);
117     void (*driver_free_rx_buffer)(void *h, void* buffer);
118 
119     // dhcp related
120     esp_netif_dhcp_status_t dhcpc_status;
121     esp_netif_dhcp_status_t dhcps_status;
122     bool timer_running;
123 
124     // event translation
125     ip_event_t get_ip_event;
126     ip_event_t lost_ip_event;
127 
128     // misc flags, types, keys, priority
129     esp_netif_flags_t flags;
130     char * hostname;
131     char * if_key;
132     char * if_desc;
133     int route_prio;
134 };
135 
136 #endif /* CONFIG_ESP_NETIF_TCPIP_LWIP */
137