1
1
/*
2
- * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2
+ * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
6
6
#include < netdb.h>
7
7
#include < memory>
8
- #include < cerrno>
9
8
#include < sys/socket.h>
10
9
#include " esp_log.h"
11
10
#include " esp_check.h"
17
16
#include " lwip/apps/snmp.h"
18
17
#include " esp_vfs.h"
19
18
#include " esp_vfs_eventfd.h"
19
+ #ifdef CONFIG_WIFI_RMT_OVER_EPPP_HOST_SIDE_NETIF
20
+ #include " esp_private/wifi.h"
21
+ #endif
20
22
21
23
extern " C" esp_netif_t *wifi_remote_eppp_init (eppp_type_t role);
22
24
@@ -30,6 +32,10 @@ const unsigned char crt[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_WIFI_RMT_OVER
30
32
const unsigned char key[] = " -----BEGIN PRIVATE KEY-----\n " CONFIG_WIFI_RMT_OVER_EPPP_SERVER_KEY " \n -----END PRIVATE KEY-----" ;
31
33
// TODO: Add option to supply keys and certs via a global symbol (file)
32
34
35
+ #ifdef CONFIG_WIFI_RMT_OVER_EPPP_HOST_SIDE_NETIF
36
+ RpcInstance* get_instance ();
37
+ #endif
38
+
33
39
}
34
40
35
41
using namespace server ;
@@ -109,9 +115,16 @@ class RpcInstance {
109
115
esp_err_t init ()
110
116
{
111
117
ESP_RETURN_ON_FALSE (netif = wifi_remote_eppp_init (EPPP_SERVER), ESP_FAIL, TAG, " Failed to init EPPP connection" );
118
+ #ifdef CONFIG_WIFI_RMT_OVER_EPPP_HOST_SIDE_NETIF
119
+ ESP_RETURN_ON_ERROR (eppp_add_channels (netif, &channel_tx, channel_rx, this ), TAG, " Failed to add EPPP channel" );
120
+ #endif
121
+
112
122
ESP_RETURN_ON_ERROR (start_server (), TAG, " Failed to start RPC server" );
113
123
ESP_RETURN_ON_ERROR (rpc.init (), TAG, " Failed to init RPC engine" );
124
+ #ifndef CONFIG_WIFI_RMT_OVER_EPPP_HOST_SIDE_NETIF
125
+ // No need to NAPT for client (host) side networking
114
126
ESP_RETURN_ON_ERROR (esp_netif_napt_enable (netif), TAG, " Failed to enable NAPT" );
127
+ #endif
115
128
ESP_RETURN_ON_ERROR (sync.init (), TAG, " Failed to init event queue" );
116
129
ESP_RETURN_ON_ERROR (esp_event_handler_register (WIFI_EVENT, ESP_EVENT_ANY_ID, handler, this ), TAG, " Failed to register event" );
117
130
ESP_RETURN_ON_ERROR (esp_event_handler_register (IP_EVENT, ESP_EVENT_ANY_ID, handler, this ), TAG, " Failed to register event" );
@@ -120,6 +133,26 @@ class RpcInstance {
120
133
Sync sync;
121
134
private:
122
135
esp_netif_t *netif{nullptr };
136
+ #ifdef CONFIG_WIFI_RMT_OVER_EPPP_HOST_SIDE_NETIF
137
+ bool started{false };
138
+ eppp_channel_fn_t channel_tx{nullptr };
139
+ static esp_err_t channel_rx (esp_netif_t *netif, int nr, void *buffer, size_t len)
140
+ {
141
+ if (get_instance ()->started ) {
142
+ return esp_wifi_internal_tx (WIFI_IF_STA, buffer, len);
143
+ }
144
+ return ESP_OK;
145
+ }
146
+ static esp_err_t wifi_receive (void *buffer, uint16_t len, void *eb)
147
+ {
148
+ if (get_instance ()->channel_tx ) {
149
+ auto ret = get_instance ()->channel_tx (get_instance ()->netif , 1 , buffer, len);
150
+ esp_wifi_internal_free_rx_buffer (eb);
151
+ return ret;
152
+ }
153
+ return ESP_OK;
154
+ }
155
+ #endif
123
156
static void task (void *ctx)
124
157
{
125
158
auto instance = static_cast <RpcInstance *>(ctx);
@@ -289,7 +322,11 @@ class RpcInstance {
289
322
if (header.size != 0 ) {
290
323
return ESP_FAIL;
291
324
}
292
-
325
+ #ifdef CONFIG_WIFI_RMT_OVER_EPPP_HOST_SIDE_NETIF
326
+ esp_wifi_internal_reg_rxcb (WIFI_IF_STA, wifi_receive);
327
+ esp_wifi_internal_reg_netstack_buf_cb (esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free);
328
+ started = true ;
329
+ #endif
293
330
auto ret = esp_wifi_start ();
294
331
if (rpc.send (api_id::START, &ret) != ESP_OK) {
295
332
return ESP_FAIL;
@@ -356,6 +393,14 @@ class RpcInstance {
356
393
357
394
namespace server {
358
395
constinit RpcInstance instance;
396
+
397
+ #ifdef CONFIG_WIFI_RMT_OVER_EPPP_HOST_SIDE_NETIF
398
+ // This is needed in wifi-callbacks which lack context param
399
+ RpcInstance* get_instance ()
400
+ {
401
+ return &server::instance;
402
+ }
403
+ #endif
359
404
}
360
405
361
406
RpcInstance *RpcEngine::init_server ()
0 commit comments