-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathespnow_server.cpp
More file actions
329 lines (281 loc) · 10.2 KB
/
Copy pathespnow_server.cpp
File metadata and controls
329 lines (281 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "espnow_server.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/ringbuf.h"
#include "esp_app_desc.h"
#include "nvs_flash.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_now.h"
#include "tasks.h"
#include "msptypes.h"
#include "msp.h"
#include "peer_manager.h"
#define NO_BINDING_TIMEOUT 120000 / portTICK_PERIOD_MS
#define STORAGE_NAMESPACE "netpack"
#define STORAGE_MAC_KEY "bp_mac_addr"
static const char *TAG = "espnow_server";
const esp_app_desc_t *description = esp_app_get_description();
const TickType_t espnowDelay = CONFIG_ESPNOW_SEND_DELAY / portTICK_PERIOD_MS;
static nvs_handle_t bp_mac_handle;
static TaskHandle_t espnowTaskHandle = NULL;
static TaskHandle_t bindTaskHandle = NULL;
static RingbufHandle_t xRingReceivedEspnow = NULL;
static bool isBinding = false;
static void sendInProgressResponse()
{
mspPacket_t out;
const uint8_t *response = (const uint8_t *)"P";
out.reset();
out.makeResponse();
out.function = MSP_ELRS_BACKPACK_SET_MODE;
for (uint32_t i = 0; i < 1; i++)
{
out.addByte(response[i]);
}
if (xRingbufferSend(xRingReceivedEspnow, &out, sizeof(mspPacket_t), pdMS_TO_TICKS(1000)) == pdTRUE)
ESP_LOGI(TAG, "Added progress response to ring buffer");
else
ESP_LOGE(TAG, "Failed to add item progress response to ring buffer");
}
static void runBindTask(void *pvParameters)
{
vTaskDelay(NO_BINDING_TIMEOUT);
isBinding = false;
}
static void espnowRecvCB(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len)
{
MSP msp;
for (int i = 0; i < len; i++)
{
if (msp.processReceivedByte(data[i]))
{
mspPacket_t *packet = msp.getReceivedPacket();
switch (packet->function)
{
case MSP_ELRS_BIND:
{
if (!isBinding)
break;
if (bindTaskHandle != NULL)
{
vTaskDelete(bindTaskHandle);
bindTaskHandle = NULL;
}
isBinding = false;
uint8_t recievedAddress[6];
for (int i = 0; i < 6; i++)
{
recievedAddress[i] = packet->payload[i];
}
if (recievedAddress[0] == 0 && recievedAddress[1] == 0 && recievedAddress[2] == 0 &&
recievedAddress[3] == 0 && recievedAddress[4] == 0 && recievedAddress[5] == 0)
{
ESP_LOGW(TAG, "Preventing UID from being saved to default value");
break;
}
if (nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &bp_mac_handle) == ESP_OK)
{
if (nvs_set_blob(bp_mac_handle, STORAGE_MAC_KEY, &recievedAddress, sizeof(recievedAddress)) == ESP_OK)
{
if (nvs_commit(bp_mac_handle) == ESP_OK)
ESP_LOGI(TAG, "UID saved to nvs");
else
ESP_LOGE(TAG, "Failed to commit nvs data");
}
else
ESP_LOGE(TAG, "Failed to write MAC address to nvs");
}
else
ESP_LOGE(TAG, "Error opening NVS handle!");
nvs_close(bp_mac_handle);
recievedAddress[0] = recievedAddress[0] & ~0x01;
recievedAddress[5] = 3;
ESP_ERROR_CHECK(esp_wifi_set_mac(WIFI_IF_STA, recievedAddress));
ESP_LOGI(TAG, "Backpack UID set to: [%d,%d,%d,%d,%d,%d]", recievedAddress[0], recievedAddress[1],
recievedAddress[2], recievedAddress[3], recievedAddress[4], recievedAddress[5]);
break;
}
case MSP_ELRS_BACKPACK_SET_RECORDING_STATE:
{
if (xRingbufferSend(xRingReceivedEspnow, msp.getReceivedPacket(), sizeof(mspPacket_t), pdMS_TO_TICKS(1000)) == pdTRUE)
ESP_LOGI(TAG, "Recording state change added to buffer");
else
ESP_LOGE(TAG, "Failed to add recieved ESPNOW data to ring buffer");
}
}
msp.markPacketReceived();
}
}
}
void processPacketFromHost(mspPacket_t *packet)
{
switch (packet->function)
{
case MSP_ELRS_GET_BACKPACK_VERSION:
{
ESP_LOGI(TAG, "Processing MSP_ELRS_GET_BACKPACK_VERSION...");
mspPacket_t out;
out.reset();
out.makeResponse();
out.function = MSP_ELRS_GET_BACKPACK_VERSION;
for (size_t i = 0; i < sizeof(description->version); i++)
{
out.addByte(description->version[i]);
}
if (xRingbufferSend(xRingReceivedEspnow, &out, sizeof(mspPacket_t), pdMS_TO_TICKS(1000)) == pdTRUE)
ESP_LOGI(TAG, "Added device version to ring buffer");
else
ESP_LOGE(TAG, "Failed to add item to ring buffer");
break;
}
case MSP_ELRS_BACKPACK_SET_MODE:
{
if (packet->payloadSize == 1)
{
if (packet->payload[0] == 'B')
{
ESP_LOGI(TAG, "Enter binding mode...");
isBinding = true;
}
if (bindTaskHandle != NULL)
{
vTaskDelete(bindTaskHandle);
bindTaskHandle = NULL;
}
xTaskCreate(runBindTask, "BindTask", 4096, NULL, 10, &bindTaskHandle);
sendInProgressResponse();
}
break;
}
case MSP_ELRS_REGISTER_ESPNOW_PEER:
{
ESP_LOGI(TAG, "Processing MSP_ELRS_REGISTER_ESPNOW_PEER...");
uint8_t mode = packet->readByte();
// Set target send address
if (mode == 0x01)
{
uint8_t receivedAddress[6];
receivedAddress[0] = packet->readByte();
receivedAddress[1] = packet->readByte();
receivedAddress[2] = packet->readByte();
receivedAddress[3] = packet->readByte();
receivedAddress[4] = packet->readByte();
receivedAddress[5] = packet->readByte();
peerManager.addPeer(receivedAddress);
}
else if (mode == 0x02)
{
uint8_t receivedAddress[6];
receivedAddress[0] = packet->readByte();
receivedAddress[1] = packet->readByte();
receivedAddress[2] = packet->readByte();
receivedAddress[3] = packet->readByte();
receivedAddress[4] = packet->readByte();
receivedAddress[5] = packet->readByte();
peerManager.removePeer(receivedAddress);
}
else if (mode == 0x03)
{
peerManager.clearPeers();
}
break;
}
case MSP_ELRS_SEND_RACE_OSD:
{
uint8_t receivedAddress[6];
receivedAddress[0] = packet->readByte();
receivedAddress[1] = packet->readByte();
receivedAddress[2] = packet->readByte();
receivedAddress[3] = packet->readByte();
receivedAddress[4] = packet->readByte();
receivedAddress[5] = packet->readByte();
if (receivedAddress[0] == 0 &&
receivedAddress[1] == 0 &&
receivedAddress[2] == 0 &&
receivedAddress[3] == 0 &&
receivedAddress[4] == 0 &&
receivedAddress[5] == 0)
peerManager.sendToPeers(packet);
else
peerManager.sendToPeer(receivedAddress, packet);
break;
}
default:
{
peerManager.sendToPeers(packet);
break;
}
}
}
void runESPNOWServer(void *pvParameters)
{
TaskBufferParams *buffers = (TaskBufferParams *)pvParameters;
xRingReceivedEspnow = buffers->write;
espnowTaskHandle = xTaskGetCurrentTaskHandle();
MSP msp;
uint8_t macAddress[6];
esp_err_t err;
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// Get Backpack MAC address from NVS
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &bp_mac_handle);
if (err == ESP_OK)
{
uint8_t mac_addr[6];
size_t size = sizeof(mac_addr);
err = nvs_get_blob(bp_mac_handle, STORAGE_MAC_KEY, macAddress, &size);
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND)
ESP_LOGW(TAG, "Unable to retreive mac address from nvs");
else
{
macAddress[0] = macAddress[0] & ~0x01;
macAddress[5] = 3;
ESP_LOGI(TAG, "Backpack UID: [%d,%d,%d,%d,%d,%d]", macAddress[0], macAddress[1],
macAddress[2], macAddress[3], macAddress[4], macAddress[5]);
}
}
else
ESP_LOGE(TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err));
nvs_close(bp_mac_handle);
// Start WiFi for ESPNOW
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_set_channel(CONFIG_ESPNOW_CHANNEL, WIFI_SECOND_CHAN_NONE));
ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR));
// Start ESPNOW
ESP_ERROR_CHECK(esp_now_init());
ESP_ERROR_CHECK(esp_now_register_send_cb(espnowSendCB));
ESP_ERROR_CHECK(esp_now_register_recv_cb(espnowRecvCB));
ESP_ERROR_CHECK(esp_wifi_set_mac(WIFI_IF_STA, macAddress));
while (1)
{
// Send data from incoming buffer over ESPNOW
size_t item_size;
mspPacket_t *packet = (mspPacket_t *)xRingbufferReceive(buffers->read, &item_size, portMAX_DELAY);
if (packet != NULL)
{
uint8_t packetSize = msp.getTotalPacketSize(packet);
uint8_t nowDataOutput[packetSize];
uint8_t result = msp.convertToByteArray(packet, nowDataOutput);
if (result)
{
processPacketFromHost(packet);
}
}
vRingbufferReturnItem(buffers->read, (void *)packet);
}
}