Skip to content

Commit 5a5058a

Browse files
committed
filter DJI serial messages and poll with only our own
1 parent 564b076 commit 5a5058a

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

libshims/libduml_hal.so

-96 Bytes
Binary file not shown.

msp.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
#include <stdio.h>
44
#include "msp.h"
55

6+
msp_error_e construct_msp_command(uint8_t message_buffer[], uint8_t command, uint8_t payload[], uint8_t size) {
7+
uint8_t checksum;
8+
message_buffer[0] = '$'; // Header
9+
message_buffer[1] = 'M'; // MSP V1
10+
message_buffer[2] = '<'; // Command Direction
11+
message_buffer[3] = size; // Payload Size
12+
checksum = size;
13+
message_buffer[4] = command; // Command
14+
checksum ^= command;
15+
for(uint8_t i = 0; i < size; i++) {
16+
message_buffer[5 + i] = payload[i];
17+
checksum ^= message_buffer[5 + i];
18+
}
19+
message_buffer[5 + size] = checksum;
20+
return 0;
21+
}
22+
623
msp_error_e msp_process_data(msp_state_t *msp_state, uint8_t dat)
724
{
825
switch (msp_state->state)

msp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <stdint.h>
22

3+
#define MSP_CMD_STATUS 101
4+
#define MSP_CMD_BATTERY_STATE 130
35
#define MSP_CMD_DISPLAYPORT 182
46

57
typedef enum {
@@ -40,4 +42,5 @@ typedef struct msp_state_s {
4042
msp_msg_t message;
4143
} msp_state_t;
4244

45+
msp_error_e construct_msp_command(uint8_t message_buffer[], uint8_t command, uint8_t payload[], uint8_t size);
4346
msp_error_e msp_process_data(msp_state_t *msp_state, uint8_t dat);

msp_displayport_mux.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <unistd.h>
44
#include <string.h>
55
#include <sys/poll.h>
6+
#include <time.h>
67

78
#include "network.h"
89
#include "serial.h"
@@ -34,6 +35,7 @@ static void sig_handler(int _)
3435

3536
static void msp_callback(msp_msg_t *msp_message)
3637
{
38+
// Process a received MSP message and decide whether to send it to the PTY (DJI) or UDP port (MSP-OSD on Goggles)
3739
if(msp_message->cmd == MSP_CMD_DISPLAYPORT) {
3840
if(fb_cursor > sizeof(frame_buffer)) {
3941
printf("Exhausted frame buffer!\n");
@@ -55,20 +57,31 @@ static void msp_callback(msp_msg_t *msp_message)
5557
int main(int argc, char *argv[]) {
5658
int opt;
5759
uint8_t fast_serial = 0;
58-
while((opt = getopt(argc, argv, "f")) != -1){
60+
uint8_t serial_passthrough = 1;
61+
uint8_t poll_manually = 0;
62+
uint8_t msg_flip = 0;
63+
while((opt = getopt(argc, argv, "fsp")) != -1){
5964
switch(opt){
6065
case 'f':
6166
fast_serial = 1;
6267
printf("Configuring serial to 230400 baud\n");
6368
break;
69+
case 's':
70+
serial_passthrough = 0;
71+
printf("Disabling serial passthrough, enabling filtering\n");
72+
break;
73+
case 'p':
74+
poll_manually = 1;
75+
printf("Enabling manual MSP polling every 500ms\n");
76+
break;
6477
case '?':
6578
printf("unknown option: %c\n", optopt);
6679
break;
6780
}
6881
}
6982

7083
if((argc - optind) < 2) {
71-
printf("usage: msp_displayport_mux [-f] ipaddr serial_port [pty_target]");
84+
printf("usage: msp_displayport_mux [-f] [-s] [-p] ipaddr serial_port [pty_target]\n-s : enable serial filtering\n-f : 230400 baud serial\n-p: enable manual polling\n");
7285
return 0;
7386
}
7487

@@ -94,26 +107,52 @@ int main(int argc, char *argv[]) {
94107
socket_fd = connect_to_server(ip_address, PORT);
95108
uint8_t serial_data[256];
96109
ssize_t serial_data_size;
110+
struct timespec now, last;
97111
while (!quit) {
98112
poll_fds[0].fd = serial_fd;
99113
poll_fds[1].fd = pty_fd;
100114
poll_fds[0].events = POLLIN;
101115
poll_fds[1].events = POLLIN;
102-
poll(poll_fds, 2, -1);
116+
poll(poll_fds, 2, 250);
117+
// We got serial data, process it as MSP data.
103118
if (0 < (serial_data_size = read(serial_fd, serial_data, sizeof(serial_data)))) {
119+
DEBUG_PRINT("RECEIVED data! length %d\n", serial_data_size);
104120
for (ssize_t i = 0; i < serial_data_size; i++) {
105121
if(msp_process_data(msp_state, serial_data[i]) == 0) {
106122
// 0 -> MSP data was valid, so buffer it to forward on later
107123
message_buffer[cursor] = serial_data[i];
124+
DEBUG_PRINT("%02X ", serial_data[i]);
108125
cursor++;
109126
} else {
110127
cursor = 0;
111128
}
112129
}
130+
DEBUG_PRINT("\n");
113131
}
114-
if(0 < (serial_data_size = read(pty_fd, serial_data, sizeof(serial_data)))) {
132+
133+
// If serial passthrough is enabled, send the message through verbatim.
134+
if(0 < (serial_data_size = read(pty_fd, serial_data, sizeof(serial_data))) && serial_passthrough) {
135+
DEBUG_PRINT("SEND data! length %d\n", serial_data_size);
136+
for (ssize_t i= 0; i < serial_data_size; i++) {
137+
DEBUG_PRINT("%02X ", serial_data[i]);
138+
}
139+
DEBUG_PRINT("\n");
115140
write(serial_fd, &serial_data, serial_data_size);
116141
}
142+
143+
// If manual MSP polling is enabled, poll only for a few messages: arming status and battery voltage
144+
clock_gettime(CLOCK_MONOTONIC, &now);
145+
if(poll_manually && (now.tv_sec > last.tv_sec || now.tv_nsec > (last.tv_nsec + 50000000000))) {
146+
construct_msp_command(&serial_data, msg_flip ? MSP_CMD_STATUS : MSP_CMD_BATTERY_STATE, 0, 0);
147+
msg_flip = !msg_flip;
148+
DEBUG_PRINT("Polling: ");
149+
for(ssize_t i = 0; i < 6; i++) {
150+
DEBUG_PRINT("%02X ", serial_data[i]);
151+
}
152+
DEBUG_PRINT("\n");
153+
write(serial_fd, &serial_data, 6);
154+
}
155+
last = now;
117156
}
118157
close(serial_fd);
119158
close(pty_fd);

0 commit comments

Comments
 (0)