Skip to content

Commit a07e7e6

Browse files
committed
better debugging and a few little fixes
1 parent e9d54b0 commit a07e7e6

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

msp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#include <stdio.h>
44
#include "msp.h"
55

6-
void msp_data_from_msg(uint8_t message_buffer[], msp_msg_t *msg) {
6+
uint16_t msp_data_from_msg(uint8_t message_buffer[], msp_msg_t *msg) {
7+
// return size
78
construct_msp_command(message_buffer, msg->cmd, msg->payload, msg->size, msg->direction);
9+
return msg->size + 6;
810
}
911

1012
msp_error_e construct_msp_command(uint8_t message_buffer[], uint8_t command, uint8_t payload[], uint8_t size, msp_direction_e direction) {

msp_displayport_mux.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static uint8_t cache_msp_message(msp_msg_t *msp_message) {
5656
msp_message_cache[msp_message->cmd] = cache_message;
5757
retval = 1;
5858
}
59-
memcpy(&cache_message->message, msp_message, sizeof(msp_message));
59+
memcpy(&cache_message->message, msp_message, sizeof(msp_msg_t));
6060
clock_gettime(CLOCK_MONOTONIC, &cache_message->time);
6161
return retval;
6262
}
@@ -77,11 +77,13 @@ static int16_t msp_msg_from_cache(uint8_t msg_buffer[], uint8_t cmd_id) {
7777
clock_gettime(CLOCK_MONOTONIC, &now);
7878
if(now.tv_sec > cache_message->time.tv_sec) {
7979
// message is too old, invalidate cache and force a resend
80+
DEBUG_PRINT("MSP cache EXPIRED %d\n", cmd_id);
8081
free(cache_message);
8182
msp_message_cache[cmd_id] = 0;
8283
return -1;
8384
}
8485
}
86+
// message existed and was not stale, send it back
8587
return msp_data_from_msg(msg_buffer, &cache_message->message);
8688
}
8789
}
@@ -97,7 +99,6 @@ static void rx_msp_callback(msp_msg_t *msp_message)
9799
}
98100
memcpy(&frame_buffer[fb_cursor], rx_message_buffer, rx_cursor);
99101
fb_cursor += rx_cursor;
100-
rx_cursor = 0;
101102
if(msp_message->payload[0] == 4) {
102103
// Once we have a whole frame of data, send it to the goggles.
103104
write(socket_fd, frame_buffer, fb_cursor);
@@ -112,28 +113,29 @@ static void rx_msp_callback(msp_msg_t *msp_message)
112113
} else {
113114
// Serial passthrough is off, so cache the response we got.
114115
if(cache_msp_message(msp_message)) {
115-
// 1 -> cache miss, so this message expired or hasn't been seen. this means DJI is waiting for it, so send it over
116+
// 1 -> cache miss, so this message expired or hasn't been seen.
117+
// this means DJI is waiting for it, so send it over
116118
write(pty_fd, rx_message_buffer, rx_cursor);
117119
}
118120
}
119-
rx_cursor = 0;
120121
}
122+
rx_cursor = 0;
121123
}
122124

123125
static void tx_msp_callback(msp_msg_t *msp_message)
124126
{
125127
// We got a valid message from DJI asking for something. See if there's a response in the cache or not.
126128
// We can only get here if serial passthrough is off and caching is on, so no need to check again.
127-
DEBUG_PRINT("DJI->FC MSP msg %d with data len %d \n", msp_message->cmd, msp_message->size);
129+
DEBUG_PRINT("DJI->FC MSP msg %d with request len %d \n", msp_message->cmd, msp_message->size);
128130
uint8_t send_buffer[256];
129-
uint16_t size;
131+
int16_t size;
130132
if(0 < (size = msp_msg_from_cache(send_buffer, msp_message->cmd))) {
131133
// cache hit, so write the cached message straight back to DJI
132-
DEBUG_PRINT("DJI->FC MSP CACHE HIT %d with data len %d \n", msp_message->cmd, size);
134+
DEBUG_PRINT("DJI->FC MSP CACHE HIT msg %d with response len %d \n", msp_message->cmd, size);
133135
write(pty_fd, send_buffer, size);
134136
} else {
135137
// cache miss, so write the DJI request to serial and wait for the FC to come back.
136-
DEBUG_PRINT("DJI->FC MSP CACHE MISS %d\n",msp_message->cmd);
138+
DEBUG_PRINT("DJI->FC MSP CACHE MISS msg %d\n",msp_message->cmd);
137139
write(serial_fd, tx_message_buffer, tx_cursor);
138140
}
139141
tx_cursor = 0;

0 commit comments

Comments
 (0)