1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use prelude::v1::*;

/// Packet parsing error
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum MspPacketParseError {
	OutputBufferSizeMismatch,
	CrcMismatch { expected: u8, calculated: u8 },
	InvalidData,
	InvalidHeader1,
	InvalidHeader2,
	InvalidDirection,
	InvalidDataLength
}

/// Packet's desired destination
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum MspPacketDirection {
	/// Network byte '<'
	ToFlightController,
	/// Network byte '>'
	FromFlightController,
	/// Network byte '!'
	Unsupported
}

impl MspPacketDirection {
	/// To network byte
	pub fn to_byte(&self) -> u8 {
		let b = match *self {
			MspPacketDirection::ToFlightController => '<',
			MspPacketDirection::FromFlightController => '>',
			MspPacketDirection::Unsupported => '!'
		};
		b as u8
	}
}

#[derive(Debug, Clone, PartialEq)]
/// A decoded MSP packet, with a command code, direction and payload
pub struct MspPacket<'a> {
	pub cmd: u8,
	pub direction: MspPacketDirection,
	pub data: Cow<'a, [u8]>
}

#[derive(Copy, Clone, PartialEq, Debug)]
enum MspParserState {
	Header1,
	Header2,
	Direction,
	DataLength,
	Command,
	Data,
	Crc
}

#[derive(Debug)]
/// Parser that can find packets from a raw byte stream
pub struct MspParser {
	state: MspParserState,
	packet_direction: MspPacketDirection,
	packet_cmd: u8,
	packet_data_length_remaining: usize,
	packet_data: Vec<u8>,
	packet_crc: u8
}

impl MspParser {
	/// Create a new parser
	pub fn new() -> MspParser {
		MspParser {
			state: MspParserState::Header1,
			packet_direction: MspPacketDirection::ToFlightController,
			packet_data_length_remaining: 0,
			packet_cmd: 0,
			packet_data: Vec::new(),
			packet_crc: 0
		}
	}

	/// Are we waiting for the header of a brand new packet?
	pub fn state_is_between_packets(&self) -> bool {
		self.state == MspParserState::Header1
	}

	/// Parse the next input byte. Returns a valid packet whenever a full packet is received, otherwise
	/// restarts the state of the parser.
	pub fn parse<'b>(&mut self, input: u8) -> Result<Option<MspPacket<'b>>, MspPacketParseError> {
		match self.state {
			MspParserState::Header1 => {
				if input == ('$' as u8) {
					self.state = MspParserState::Header2;
				} else {
					self.reset();
				}
			},

			MspParserState::Header2 => {
				if input == ('M' as u8) {
					self.state = MspParserState::Direction;
				} else {
					self.reset();
				}
			},

			MspParserState::Direction => {
				match input {
					60 => self.packet_direction = MspPacketDirection::ToFlightController,
					62 => self.packet_direction = MspPacketDirection::FromFlightController,
					33 => self.packet_direction = MspPacketDirection::Unsupported,
					_ => {
						self.reset();
						return Ok(None);
					}
				}

				self.state = MspParserState::DataLength;
			},

			MspParserState::DataLength => {

				self.packet_data_length_remaining = input as usize;
				self.state = MspParserState::Command;
				self.packet_crc ^= input;
				self.packet_data = Vec::with_capacity(input as usize);
			},

			MspParserState::Command => {

				self.packet_cmd = input;

				if self.packet_data_length_remaining == 0 {
					self.state = MspParserState::Crc;
				} else {
					self.state = MspParserState::Data;
				}

				self.packet_crc ^= input;

			},

			MspParserState::Data => {

				self.packet_data.push(input);
				self.packet_data_length_remaining -= 1;

				self.packet_crc ^= input;

				if self.packet_data_length_remaining == 0 {
					self.state = MspParserState::Crc;
				}

			},

			MspParserState::Crc => {

				let packet_crc = self.packet_crc;
				if input != packet_crc {
					self.reset();
					return Err(MspPacketParseError::CrcMismatch { expected: input, calculated: packet_crc });
				}

				let mut n = Vec::new();
				mem::swap(&mut self.packet_data, &mut n);

				let packet = MspPacket {
					cmd: self.packet_cmd,
					direction: self.packet_direction,
					data: Cow::Owned(n)
				};

				self.reset();

				return Ok(Some(packet));
			}

		}

		Ok(None)
	}

	fn reset(&mut self) {
		self.state = MspParserState::Header1;
		self.packet_direction = MspPacketDirection::ToFlightController;
		self.packet_data_length_remaining = 0;
		self.packet_cmd = 0;
		self.packet_data.clear();
		self.packet_crc = 0;
	}
}

impl<'a> MspPacket<'a> {
	/// Number of bytes that this packet requires to be packed
	pub fn packet_size_bytes(&self) -> usize {
		6 + self.data.len()
	}

	/// Serialize to network bytes
	pub fn serialize(&self, output: &mut [u8]) -> Result<(), MspPacketParseError> {
		let l = output.len();

		if l != self.packet_size_bytes() {
			return Err(MspPacketParseError::OutputBufferSizeMismatch);
		}

		output[0] = '$' as u8;
		output[1] = 'M' as u8;
		output[2] = self.direction.to_byte();
		output[3] = self.data.len() as u8;
		output[4] = self.cmd;


		output[5..l - 1].copy_from_slice(&self.data);

		let mut crc = output[3] ^ output[4];
		for b in &*self.data {
			crc ^= *b;
		}
		output[l - 1] = crc;

		Ok(())
	}
}


#[test]
fn test_serialize() {
	let packet = MspPacket {
		cmd: 2,
		direction: MspPacketDirection::ToFlightController,
		data: Cow::Owned(vec![0xbe, 0xef])
	};

	let size = packet.packet_size_bytes();
	assert_eq!(8, size);

	let mut output = vec![0; size];
	packet.serialize(&mut output).unwrap();
	let expected = ['$' as u8, 'M' as u8, '<' as u8, 2, 2, 0xbe, 0xef, 81];
	assert_eq!(&expected, output.as_slice());

	let crc = 2 ^ 2 ^ 0xBE ^ 0xEF;
	assert_eq!(81, crc);

	
	let mut packet_parsed = None;
	let mut parser = MspParser::new();
	for b in output {
		let s = parser.parse(b);
		if let Ok(Some(p)) = s {
			packet_parsed = Some(p);
		}
	}

	assert_eq!(packet, packet_parsed.unwrap());
}


#[test]
fn test_roundtrip() {
	fn roundtrip(packet: &MspPacket) {
		let size = packet.packet_size_bytes();
		let mut output = vec![0; size];

		packet.serialize(&mut output).unwrap();

		let mut parser = MspParser::new();
		let mut packet_parsed = None;
		for b in output {
			let s = parser.parse(b);
			if let Ok(Some(p)) = s {
				packet_parsed = Some(p);
			}
		}		
		assert_eq!(packet, &packet_parsed.unwrap());
	}

	{
		let packet = MspPacket {
			cmd: 1,
			direction: MspPacketDirection::ToFlightController,
			data: Cow::Owned(vec![0x00, 0x00, 0x00])
		};
		roundtrip(&packet);
	}

	{
		let packet = MspPacket {
			cmd: 200,
			direction: MspPacketDirection::FromFlightController,
			data: Cow::Owned(vec![])
		};
		roundtrip(&packet);
	}

	{
		let packet = MspPacket {
			cmd: 100,
			direction: MspPacketDirection::Unsupported,
			data: Cow::Owned(vec![0x44, 0x20, 0x00, 0x80])
		};
		roundtrip(&packet);
	}
}