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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use bytes::{Buf, BufMut};
use message::{Message, MessageClass, MessageHeader, MessagePayload, MessageType};
use std::io::{Cursor, Read};

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct get_bt_address {}

impl get_bt_address {
    pub fn new() -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: 0x00,
            message_class: MessageClass::system,
            message_id: 0x03,
        };
        let payload = get_bt_address {};
        let payload = MessagePayload::cmd_system_get_bt_address(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for get_bt_address {
    fn from(_: &[u8]) -> get_bt_address {
        get_bt_address {}
    }
}

impl Into<Vec<u8>> for get_bt_address {
    fn into(self) -> Vec<u8> {
        Vec::new()
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct get_counters {
    pub reset: u8,
}

impl get_counters {
    pub fn new(reset: u8) -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: 0x01,
            message_class: MessageClass::system,
            message_id: 0x0f,
        };
        let payload = get_counters { reset };
        let payload = MessagePayload::cmd_system_get_counters(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for get_counters {
    fn from(data: &[u8]) -> get_counters {
        let mut cursor = Cursor::new(data);
        get_counters {
            reset: cursor.get_u8(),
        }
    }
}

impl Into<Vec<u8>> for get_counters {
    fn into(self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.put_u8(self.reset);
        bytes
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct get_random_data {
    pub length: u8,
}

impl get_random_data {
    pub fn new(length: u8) -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: 0x01,
            message_class: MessageClass::system,
            message_id: 0x0b,
        };
        let payload = get_random_data { length };
        let payload = MessagePayload::cmd_system_get_random_data(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for get_random_data {
    fn from(data: &[u8]) -> get_random_data {
        let mut cursor = Cursor::new(data);
        get_random_data {
            length: cursor.get_u8(),
        }
    }
}

impl Into<Vec<u8>> for get_random_data {
    fn into(self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.put_u8(self.length);
        bytes
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct halt {
    pub halt: u8,
}

impl halt {
    pub fn new(halt: u8) -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: 0x01,
            message_class: MessageClass::system,
            message_id: 0x0c,
        };
        let payload = halt { halt };
        let payload = MessagePayload::cmd_system_halt(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for halt {
    fn from(data: &[u8]) -> halt {
        let mut cursor = Cursor::new(data);
        halt {
            halt: cursor.get_u8(),
        }
    }
}

impl Into<Vec<u8>> for halt {
    fn into(self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.put_u8(self.halt);
        bytes
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct hello {}

impl hello {
    pub fn new() -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: 0x00,
            message_class: MessageClass::system,
            message_id: 0x00,
        };
        let payload = hello {};
        let payload = MessagePayload::cmd_system_hello(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for hello {
    fn from(_: &[u8]) -> hello {
        hello {}
    }
}

impl Into<Vec<u8>> for hello {
    fn into(self) -> Vec<u8> {
        Vec::new()
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct reset {
    pub dfu: u8,
}

impl reset {
    pub fn new(dfu: u8) -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: 0x01,
            message_class: MessageClass::system,
            message_id: 0x01,
        };
        let payload = reset { dfu };
        let payload = MessagePayload::cmd_system_reset(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for reset {
    fn from(data: &[u8]) -> reset {
        let mut cursor = Cursor::new(data);
        reset {
            dfu: cursor.get_u8(),
        }
    }
}

impl Into<Vec<u8>> for reset {
    fn into(self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.put_u8(self.dfu);
        bytes
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct set_bt_address {
    pub address: [u8; 6],
}

impl set_bt_address {
    pub fn new(address: [u8; 6]) -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: address.len() as u8,
            message_class: MessageClass::system,
            message_id: 0x04,
        };
        let payload = set_bt_address { address };
        let payload = MessagePayload::cmd_system_set_bt_address(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for set_bt_address {
    fn from(data: &[u8]) -> set_bt_address {
        let mut cursor = Cursor::new(data);
        let mut address: [u8; 6] = Default::default();
        cursor
            .read_exact(&mut address)
            .expect("Failed to read bytes.");
        address.reverse();
        set_bt_address { address }
    }
}

impl Into<Vec<u8>> for set_bt_address {
    fn into(self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.extend(self.address.iter().rev());
        bytes
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct set_device_name {
    pub dtype: u8,
    pub name: Vec<u8>,
}

impl set_device_name {
    pub fn new(dtype: u8, name: Vec<u8>) -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: 0x01 + (1 + name.len() as u8),
            message_class: MessageClass::system,
            message_id: 0x0d,
        };
        let payload = set_device_name { dtype, name };
        let payload = MessagePayload::cmd_system_set_device_name(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for set_device_name {
    fn from(data: &[u8]) -> set_device_name {
        let mut cursor = Cursor::new(data);
        let dtype = cursor.get_u8();
        let mut name = Vec::new();
        cursor.get_u8();
        cursor
            .read_to_end(&mut name)
            .expect("Failed to read bytes.");
        set_device_name { dtype, name }
    }
}

impl Into<Vec<u8>> for set_device_name {
    fn into(self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.put_u8(self.dtype);
        bytes.put_u8(self.name.len() as u8);
        bytes.extend(self.name.iter());
        bytes
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct set_tx_power {
    pub power: i16,
}

impl set_tx_power {
    pub fn new(power: i16) -> Message {
        let header = MessageHeader {
            message_type: MessageType::command_response,
            payload_length: 0x02,
            message_class: MessageClass::system,
            message_id: 0x0a,
        };
        let payload = set_tx_power { power };
        let payload = MessagePayload::cmd_system_set_tx_power(payload);
        Message { header, payload }
    }
}

impl From<&[u8]> for set_tx_power {
    fn from(data: &[u8]) -> set_tx_power {
        let mut cursor = Cursor::new(data);
        set_tx_power {
            power: cursor.get_i16_le(),
        }
    }
}

impl Into<Vec<u8>> for set_tx_power {
    fn into(self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.put_i16_le(self.power);
        bytes
    }
}