ssp/set_encryption_key/
command.rs

1use crate::{
2    impl_command_display, impl_command_ops, impl_default, impl_message_from_buf, impl_message_ops,
3    len, CommandOps, FixedKey, MessageOps, MessageType,
4};
5
6mod index {
7    pub const FIXED_KEY: usize = 4;
8    pub const FIXED_KEY_END: usize = 12;
9}
10
11/// SetEncryptionKey - Command (0x4B)
12///
13/// Eight data bytes are sent. This is a 64 bit number representing the EncryptionKey, and must be a
14/// prime number.
15///
16/// The device will reply with OK or PARAMETER_OUT_OF_RANGE if the number is not prime.
17#[repr(C)]
18#[derive(Clone, Copy, Debug, PartialEq)]
19pub struct SetEncryptionKeyCommand {
20    buf: [u8; len::SET_ENCRYPTION_KEY_COMMAND],
21}
22
23impl SetEncryptionKeyCommand {
24    /// Creates a new [SetEncryptionKeyCommand] message.
25    pub fn new() -> Self {
26        let mut msg = Self {
27            buf: [0u8; len::SET_ENCRYPTION_KEY_COMMAND],
28        };
29
30        msg.init();
31        msg.set_command(MessageType::SetEncryptionKey);
32
33        msg
34    }
35
36    /// Gets the [FixedKey].
37    pub fn fixed_key(&self) -> FixedKey {
38        let key_bytes: [u8; 8] = self.buf[index::FIXED_KEY..index::FIXED_KEY_END]
39            .try_into()
40            .unwrap();
41        u64::from_le_bytes(key_bytes).into()
42    }
43
44    /// Sets the [FixedKey].
45    pub fn set_fixed_key(&mut self, key: &FixedKey) {
46        let key_bytes = key.as_inner().to_le_bytes();
47        self.buf[index::FIXED_KEY..index::FIXED_KEY_END].copy_from_slice(key_bytes.as_ref());
48    }
49}
50
51impl_default!(SetEncryptionKeyCommand);
52impl_command_display!(SetEncryptionKeyCommand);
53impl_message_from_buf!(SetEncryptionKeyCommand);
54impl_message_ops!(SetEncryptionKeyCommand);
55impl_command_ops!(SetEncryptionKeyCommand);