Skip to content

Commit 7c9790f

Browse files
committed
dramatic reliability improvements, fix MSP off by one
1 parent a07e7e6 commit 7c9790f

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

libshims/libduml_hal.so

-5.68 KB
Binary file not shown.

msp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,5 @@ msp_error_e msp_process_data(msp_state_t *msp_state, uint8_t dat)
127127
}
128128
break;
129129
}
130-
return 0;
130+
return MSP_ERR_NONE;
131131
}

msp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MSP_CMD_DISPLAYPORT 182
1515

1616
typedef enum {
17+
MSP_ERR_NONE,
1718
MSP_ERR_HDR,
1819
MSP_ERR_LEN,
1920
MSP_ERR_CKS

msp_displayport_mux.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ static uint8_t cache_msp_message(msp_msg_t *msp_message) {
5151
uint8_t retval = 0;
5252
msp_cache_entry_t *cache_message = msp_message_cache[msp_message->cmd];
5353
if (cache_message == NULL) {
54-
DEBUG_PRINT("no entry for msg %d, allocating\n", msp_message->cmd);
54+
DEBUG_PRINT("FC -> AU CACHE: no entry for msg %d, allocating\n", msp_message->cmd);
5555
cache_message = calloc(1, sizeof(msp_cache_entry_t));
5656
msp_message_cache[msp_message->cmd] = cache_message;
5757
retval = 1;
5858
}
59+
DEBUG_PRINT ("FC -> AU CACHE: refreshing %d\n", msp_message->cmd);
5960
memcpy(&cache_message->message, msp_message, sizeof(msp_msg_t));
6061
clock_gettime(CLOCK_MONOTONIC, &cache_message->time);
6162
return retval;
@@ -97,25 +98,36 @@ static void rx_msp_callback(msp_msg_t *msp_message)
9798
if(fb_cursor > sizeof(frame_buffer)) {
9899
printf("Exhausted frame buffer!\n");
99100
}
100-
memcpy(&frame_buffer[fb_cursor], rx_message_buffer, rx_cursor);
101-
fb_cursor += rx_cursor;
101+
uint16_t size = msp_data_from_msg(rx_message_buffer, msp_message);
102+
memcpy(&frame_buffer[fb_cursor], rx_message_buffer, size);
103+
fb_cursor += size;
102104
if(msp_message->payload[0] == 4) {
103105
// Once we have a whole frame of data, send it to the goggles.
104106
write(socket_fd, frame_buffer, fb_cursor);
105107
DEBUG_PRINT("DRAW! wrote %d bytes\n", fb_cursor);
106108
fb_cursor = 0;
107109
}
108110
} else {
111+
uint16_t size = msp_data_from_msg(rx_message_buffer, msp_message);
109112
// This isn't an MSP DisplayPort message, so send it to either DJI directly or to the cache.
110113
if(serial_passthrough) {
111114
// Serial passthrough is on, so send it straight to DJI.
112-
write(pty_fd, rx_message_buffer, rx_cursor);
115+
for (int i = 0; i < rx_cursor; i++) {
116+
DEBUG_PRINT("%02X ", rx_message_buffer[i]);
117+
}
118+
DEBUG_PRINT("\n");
119+
write(pty_fd, rx_message_buffer, size);
113120
} else {
114121
// Serial passthrough is off, so cache the response we got.
115122
if(cache_msp_message(msp_message)) {
116123
// 1 -> cache miss, so this message expired or hasn't been seen.
117124
// this means DJI is waiting for it, so send it over
118-
write(pty_fd, rx_message_buffer, rx_cursor);
125+
DEBUG_PRINT("DJI was waiting, got msg %d\n", msp_message->cmd);
126+
for (int i = 0; i < rx_cursor; i++) {
127+
DEBUG_PRINT("%02X ", rx_message_buffer[i]);
128+
}
129+
DEBUG_PRINT("\n");
130+
write(pty_fd, rx_message_buffer, size);
119131
}
120132
}
121133
}
@@ -132,6 +144,10 @@ static void tx_msp_callback(msp_msg_t *msp_message)
132144
if(0 < (size = msp_msg_from_cache(send_buffer, msp_message->cmd))) {
133145
// cache hit, so write the cached message straight back to DJI
134146
DEBUG_PRINT("DJI->FC MSP CACHE HIT msg %d with response len %d \n", msp_message->cmd, size);
147+
for(int i = 0; i < size; i++) {
148+
DEBUG_PRINT("%02X ", send_buffer[i]);
149+
}
150+
DEBUG_PRINT("\n");
135151
write(pty_fd, send_buffer, size);
136152
} else {
137153
// cache miss, so write the DJI request to serial and wait for the FC to come back.
@@ -202,7 +218,7 @@ int main(int argc, char *argv[]) {
202218
if (0 < (serial_data_size = read(serial_fd, serial_data, sizeof(serial_data)))) {
203219
DEBUG_PRINT("RECEIVED data! length %d\n", serial_data_size);
204220
for (ssize_t i = 0; i < serial_data_size; i++) {
205-
if(msp_process_data(rx_msp_state, serial_data[i]) == 0) {
221+
if(msp_process_data(rx_msp_state, serial_data[i]) == MSP_ERR_NONE) {
206222
// 0 -> MSP data was valid, so buffer it to forward on to either goggles or DJI later
207223
rx_message_buffer[rx_cursor] = serial_data[i];
208224
rx_cursor++;
@@ -225,7 +241,7 @@ int main(int argc, char *argv[]) {
225241
// Otherwise, queue it up for processing by the MSP layer.
226242
DEBUG_PRINT("SEND data to MSP buffer! length %d\n", serial_data_size);
227243
for (ssize_t i = 0; i < serial_data_size; i++) {
228-
if(msp_process_data(tx_msp_state, serial_data[i]) == 0) {
244+
if(msp_process_data(tx_msp_state, serial_data[i]) == MSP_ERR_NONE) {
229245
// 0 -> MSP data was valid, so buffer it to forward on later
230246
tx_message_buffer[tx_cursor] = serial_data[i];
231247
tx_cursor++;

0 commit comments

Comments
 (0)