3
3
#include <unistd.h>
4
4
#include <string.h>
5
5
#include <sys/poll.h>
6
+ #include <time.h>
6
7
7
8
#include "network.h"
8
9
#include "serial.h"
@@ -34,6 +35,7 @@ static void sig_handler(int _)
34
35
35
36
static void msp_callback (msp_msg_t * msp_message )
36
37
{
38
+ // Process a received MSP message and decide whether to send it to the PTY (DJI) or UDP port (MSP-OSD on Goggles)
37
39
if (msp_message -> cmd == MSP_CMD_DISPLAYPORT ) {
38
40
if (fb_cursor > sizeof (frame_buffer )) {
39
41
printf ("Exhausted frame buffer!\n" );
@@ -55,20 +57,31 @@ static void msp_callback(msp_msg_t *msp_message)
55
57
int main (int argc , char * argv []) {
56
58
int opt ;
57
59
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 ){
59
64
switch (opt ){
60
65
case 'f' :
61
66
fast_serial = 1 ;
62
67
printf ("Configuring serial to 230400 baud\n" );
63
68
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 ;
64
77
case '?' :
65
78
printf ("unknown option: %c\n" , optopt );
66
79
break ;
67
80
}
68
81
}
69
82
70
83
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 " );
72
85
return 0 ;
73
86
}
74
87
@@ -94,26 +107,52 @@ int main(int argc, char *argv[]) {
94
107
socket_fd = connect_to_server (ip_address , PORT );
95
108
uint8_t serial_data [256 ];
96
109
ssize_t serial_data_size ;
110
+ struct timespec now , last ;
97
111
while (!quit ) {
98
112
poll_fds [0 ].fd = serial_fd ;
99
113
poll_fds [1 ].fd = pty_fd ;
100
114
poll_fds [0 ].events = POLLIN ;
101
115
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.
103
118
if (0 < (serial_data_size = read (serial_fd , serial_data , sizeof (serial_data )))) {
119
+ DEBUG_PRINT ("RECEIVED data! length %d\n" , serial_data_size );
104
120
for (ssize_t i = 0 ; i < serial_data_size ; i ++ ) {
105
121
if (msp_process_data (msp_state , serial_data [i ]) == 0 ) {
106
122
// 0 -> MSP data was valid, so buffer it to forward on later
107
123
message_buffer [cursor ] = serial_data [i ];
124
+ DEBUG_PRINT ("%02X " , serial_data [i ]);
108
125
cursor ++ ;
109
126
} else {
110
127
cursor = 0 ;
111
128
}
112
129
}
130
+ DEBUG_PRINT ("\n" );
113
131
}
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" );
115
140
write (serial_fd , & serial_data , serial_data_size );
116
141
}
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 ;
117
156
}
118
157
close (serial_fd );
119
158
close (pty_fd );
0 commit comments