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
use super::{instruction_id, packet_id, Instruction};
use crate::endian::read_u16_le;

#[derive(Debug, Clone)]
pub struct Ping {
	pub motor_id: u8,
}

#[derive(Debug, Clone)]
pub struct PingResponse {
	pub motor_id: u8,
	pub model: u16,
	pub firmware: u8,
}

impl Ping {
	pub fn unicast(motor_id: u8) -> Self {
		Self { motor_id }
	}

	pub fn broadcast() -> Self {
		Self {
			motor_id: packet_id::BROADCAST,
		}
	}
}

impl Instruction for Ping {
	type Response = PingResponse;

	fn request_packet_id(&self) -> u8 {
		self.motor_id
	}

	fn request_instruction_id(&self) -> u8 {
		instruction_id::PING
	}

	fn request_parameters_len(&self) -> u16 {
		0
	}

	fn encode_request_parameters(&self, _buffer: &mut [u8]) {
		// Empty parameters.
	}

	fn decode_response_parameters(&mut self, packet_id: u8, parameters: &[u8]) -> Result<Self::Response, crate::InvalidMessage> {
		crate::InvalidPacketId::check_ignore_broadcast(packet_id, self.motor_id)?;
		crate::InvalidParameterCount::check(parameters.len(), 3)?;

		Ok(Self::Response {
			motor_id: packet_id,
			model: read_u16_le(&parameters[0..]),
			firmware: parameters[2],
		})
	}
}