Skip to content

Commit 40778a7

Browse files
committed
feat: Add elaphureLink support
1 parent 0f9f0eb commit 40778a7

File tree

7 files changed

+203
-16
lines changed

7 files changed

+203
-16
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(COMPONENT_ADD_INCLUDEDIRS ".")
2+
set(COMPONENT_SRCS "./elaphureLink_protocol.c")
3+
4+
register_component()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "components/elaphureLink/elaphureLink_protocol.h"
2+
3+
4+
#include "lwip/err.h"
5+
#include "lwip/sockets.h"
6+
#include "lwip/sys.h"
7+
#include <lwip/netdb.h>
8+
9+
extern int kSock;
10+
extern int usbip_network_send(int s, const void *dataptr, size_t size, int flags);
11+
12+
extern void malloc_dap_ringbuf();
13+
extern void free_dap_ringbuf();
14+
15+
extern uint32_t DAP_ExecuteCommand(const uint8_t *request, uint8_t *response);
16+
17+
uint8_t* el_process_buffer = NULL;
18+
19+
void el_process_buffer_malloc() {
20+
if (el_process_buffer != NULL)
21+
return;
22+
23+
free_dap_ringbuf();
24+
25+
el_process_buffer = malloc(1500);
26+
}
27+
28+
29+
void el_process_buffer_free() {
30+
if (el_process_buffer != NULL) {
31+
free(el_process_buffer);
32+
el_process_buffer = NULL;
33+
}
34+
}
35+
36+
37+
int el_handshake_process(int fd, void *buffer, size_t len) {
38+
if (len != sizeof(el_request_handshake)) {
39+
return -1;
40+
}
41+
42+
el_request_handshake* req = (el_request_handshake*)buffer;
43+
44+
if (ntohl(req->el_link_identifier) != EL_LINK_IDENTIFIER) {
45+
return -1;
46+
}
47+
48+
if (ntohl(req->command) != EL_COMMAND_HANDSHAKE) {
49+
return -1;
50+
}
51+
52+
el_response_handshake res;
53+
res.el_link_identifier = htonl(EL_LINK_IDENTIFIER);
54+
res.command = htonl(EL_COMMAND_HANDSHAKE);
55+
res.el_dap_version = htonl(EL_DAP_VERSION);
56+
57+
usbip_network_send(fd, &res, sizeof(el_response_handshake), 0);
58+
59+
return 0;
60+
}
61+
62+
63+
void el_dap_data_process(void* buffer, size_t len) {
64+
int res = DAP_ExecuteCommand(buffer, (uint8_t *)el_process_buffer);
65+
res &= 0xFFFF;
66+
67+
usbip_network_send(kSock, el_process_buffer, res, 0);
68+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#ifndef __ELAPHURELINK_PROTOCOL_H__
2+
#define __ELAPHURELINK_PROTOCOL_H__
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
7+
#define EL_LINK_IDENTIFIER 0x8a656c70
8+
9+
#define EL_DAP_VERSION 0x00000001
10+
11+
#define EL_COMMAND_HANDSHAKE 0x00000000
12+
13+
14+
typedef struct
15+
{
16+
uint32_t el_link_identifier;
17+
uint32_t command;
18+
uint32_t el_proxy_version
19+
} __attribute__((packed)) el_request_handshake;
20+
21+
22+
typedef struct
23+
{
24+
uint32_t el_link_identifier;
25+
uint32_t command;
26+
uint32_t el_dap_version
27+
} __attribute__((packed)) el_response_handshake;
28+
29+
30+
/**
31+
* @brief elahpureLink Proxy handshake phase process
32+
*
33+
* @param fd socket fd
34+
* @param buffer packet buffer
35+
* @param len packet length
36+
* @return 0 on Success, other on failed.
37+
*/
38+
int el_handshake_process(int fd, void* buffer, size_t len);
39+
40+
41+
/**
42+
* @brief Process dap data and send to socket
43+
*
44+
* @param buffer dap data buffer
45+
* @param len dap data length
46+
*/
47+
void el_dap_data_process(void* buffer, size_t len);
48+
49+
50+
void el_process_buffer_malloc();
51+
void el_process_buffer_free();
52+
53+
#endif

main/DAP_handle.c

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct
5858
extern int kSock;
5959
extern TaskHandle_t kDAPTaskHandle;
6060

61-
int kRestartDAPHandle = 0;
61+
int kRestartDAPHandle = NO_SIGNAL;
6262

6363

6464
static DapPacket_t DAPDataProcessed;
@@ -74,6 +74,36 @@ static RingbufHandle_t dap_dataOUT_handle = NULL;
7474
static SemaphoreHandle_t data_response_mux = NULL;
7575

7676

77+
void malloc_dap_ringbuf() {
78+
if (data_response_mux && xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE)
79+
{
80+
if (dap_dataIN_handle == NULL) {
81+
dap_dataIN_handle = xRingbufferCreate(DAP_HANDLE_SIZE * DAP_BUFFER_NUM, RINGBUF_TYPE_BYTEBUF);
82+
}
83+
if (dap_dataOUT_handle == NULL) {
84+
dap_dataOUT_handle = xRingbufferCreate(DAP_HANDLE_SIZE * DAP_BUFFER_NUM, RINGBUF_TYPE_BYTEBUF);
85+
}
86+
87+
xSemaphoreGive(data_response_mux);
88+
}
89+
}
90+
91+
void free_dap_ringbuf() {
92+
if (data_response_mux && xSemaphoreTake(data_response_mux, portMAX_DELAY) == pdTRUE) {
93+
if (dap_dataIN_handle) {
94+
vRingbufferDelete(dap_dataIN_handle);
95+
}
96+
if (dap_dataOUT_handle) {
97+
vRingbufferDelete(dap_dataOUT_handle);
98+
}
99+
100+
dap_dataIN_handle = dap_dataOUT_handle = NULL;
101+
xSemaphoreGive(data_response_mux);
102+
}
103+
104+
}
105+
106+
77107
void handle_dap_data_request(usbip_stage2_header *header, uint32_t length)
78108
{
79109
uint8_t *data_in = (uint8_t *)header;
@@ -168,21 +198,28 @@ void DAP_Thread(void *argument)
168198
{
169199
if (kRestartDAPHandle)
170200
{
171-
vRingbufferDelete(dap_dataIN_handle);
172-
vRingbufferDelete(dap_dataOUT_handle);
173-
dap_dataIN_handle = dap_dataOUT_handle = NULL;
174-
175-
dap_dataIN_handle = xRingbufferCreate(DAP_HANDLE_SIZE * DAP_BUFFER_NUM, RINGBUF_TYPE_BYTEBUF);
176-
dap_dataOUT_handle = xRingbufferCreate(DAP_HANDLE_SIZE * DAP_BUFFER_NUM, RINGBUF_TYPE_BYTEBUF);
177-
if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL)
178-
{
179-
os_printf("Can not create DAP ringbuf/mux!\r\n");
180-
vTaskDelete(NULL);
201+
free_dap_ringbuf();
202+
203+
if (kRestartDAPHandle == RESET_HANDLE) {
204+
malloc_dap_ringbuf();
205+
if (dap_dataIN_handle == NULL || dap_dataIN_handle == NULL)
206+
{
207+
os_printf("Can not create DAP ringbuf/mux!\r\n");
208+
vTaskDelete(NULL);
209+
}
181210
}
182-
kRestartDAPHandle = 0;
211+
212+
kRestartDAPHandle = NO_SIGNAL;
183213
}
184214

185-
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
215+
ulTaskNotifyTake(pdFALSE, portMAX_DELAY); // wait event
216+
217+
218+
if (dap_dataIN_handle == NULL || dap_dataOUT_handle == NULL) {
219+
continue; // may be use elaphureLink, wait...
220+
}
221+
222+
186223
packetSize = 0;
187224
item = (DapPacket_t *)xRingbufferReceiveUpTo(dap_dataIN_handle, &packetSize,
188225
pdMS_TO_TICKS(1), DAP_HANDLE_SIZE);

main/DAP_handle.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
#include "components/USBIP/usbip_defs.h"
55

6+
enum reset_handle_t
7+
{
8+
NO_SIGNAL = 0,
9+
RESET_HANDLE = 1,
10+
DELETE_HANDLE = 2,
11+
};
12+
613
void handle_dap_data_request(usbip_stage2_header *header, uint32_t length);
714
void handle_dap_data_response(usbip_stage2_header *header);
815
void handle_swo_trace_response(usbip_stage2_header *header);

main/tcp_server.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
#include "main/wifi_configuration.h"
1515
#include "main/usbip_server.h"
16+
#include "main/DAP_handle.h"
17+
18+
#include "components/elaphureLink/elaphureLink_protocol.h"
1619

1720
#include "freertos/FreeRTOS.h"
1821
#include "freertos/task.h"
@@ -146,12 +149,24 @@ void tcp_server_task(void *pvParameters)
146149
kState = ATTACHING;
147150

148151
case ATTACHING:
152+
// elaphureLink handshake
153+
if (el_handshake_process(kSock, tcp_rx_buffer, len) == 0) {
154+
// handshake successed
155+
kState = EL_DATA_PHASE;
156+
kRestartDAPHandle = DELETE_HANDLE;
157+
el_process_buffer_malloc();
158+
break;
159+
}
160+
149161
attach(tcp_rx_buffer, len);
150162
break;
151163

152164
case EMULATING:
153165
emulate(tcp_rx_buffer, len);
154166
break;
167+
case EL_DATA_PHASE:
168+
el_dap_data_process(tcp_rx_buffer, len);
169+
break;
155170
default:
156171
os_printf("unkonw kstate!\r\n");
157172
}
@@ -163,11 +178,13 @@ void tcp_server_task(void *pvParameters)
163178
os_printf("Shutting down socket and restarting...\r\n");
164179
//shutdown(kSock, 0);
165180
close(kSock);
166-
if (kState == EMULATING)
181+
if (kState == EMULATING || kState == EL_DATA_PHASE)
167182
kState = ACCEPTING;
168183

169184
// Restart DAP Handle
170-
kRestartDAPHandle = 1;
185+
el_process_buffer_free();
186+
187+
kRestartDAPHandle = RESET_HANDLE;
171188
if (kDAPTaskHandle)
172189
xTaskNotifyGive(kDAPTaskHandle);
173190

main/usbip_server.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ enum state_t
99
{
1010
ACCEPTING,
1111
ATTACHING,
12-
EMULATING
12+
EMULATING,
13+
EL_DATA_PHASE
1314
};
1415
extern uint8_t kState;
1516
extern int kSock;

0 commit comments

Comments
 (0)