urap 0.2.1

Specification and library for the URAP protocol
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]


#[cfg(feature = "usockets")]
#[cfg_attr(docsrs, doc(cfg(feature = "usockets")))]
pub mod usockets;

use core::fmt::Display;

use bytemuck::{bytes_of, cast_slice_mut, checked::{cast_slice, from_bytes}};
use embedded_io::{Read, ReadExactError, Write};
#[cfg(feature = "std")]
use embedded_io::ErrorType;

/// Number of bytes in a register
pub const URAP_DATA_WIDTH: usize = 4;
/// Number of bytes in a CRC
pub const URAP_CRC_WIDTH: usize = 1;
/// Number of bytes when addressing a register via URAP
pub const URAP_REG_WIDTH: usize = 2;
/// Number of bytes in a count
pub const URAP_COUNT_WIDTH: usize = 1;
/// Number of bytes in a head byte
pub const URAP_HEAD_WIDTH: usize = URAP_COUNT_WIDTH;
/// Number of bytes in an ACK
pub const URAP_ACK_WIDTH: usize = 1;
/// Most significant bit signifying a write in URAP
pub const URAP_WRITE_OR: u8 = 0x80;
/// Maximum register that can be accessed in a single packet
pub const URAP_COUNT_MAX: usize = 128;
/// Maximum amount of data in a packet
pub const URAP_MAX_DATA_SIZE: usize = URAP_DATA_WIDTH * URAP_COUNT_MAX;
/// Maximum size of a single packet
pub const URAP_MAX_PACKET_SIZE: usize = URAP_HEAD_WIDTH + URAP_REG_WIDTH + URAP_DATA_WIDTH * URAP_COUNT_MAX + URAP_CRC_WIDTH;

/// CRC Table for polynomial 0x1D
pub static CRC_TABLE: [u8; 256] = [
 0x00, 0x1D, 0x3A, 0x27, 0x74, 0x69, 0x4E, 0x53, 0xE8, 0xF5, 0xD2, 0xCF, 0x9C, 0x81, 0xA6, 0xBB, 
 0xCD, 0xD0, 0xF7, 0xEA, 0xB9, 0xA4, 0x83, 0x9E, 0x25, 0x38, 0x1F, 0x02, 0x51, 0x4C, 0x6B, 0x76, 
 0x87, 0x9A, 0xBD, 0xA0, 0xF3, 0xEE, 0xC9, 0xD4, 0x6F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, 0x64, 0x79, 0x5E, 0x43, 
 0xB2, 0xAF, 0x88, 0x95, 0xC6, 0xDB, 0xFC, 0xE1, 0x5A, 0x47, 0x60, 0x7D, 0x2E, 0x33, 0x14, 0x09, 
 0x7F, 0x62, 0x45, 0x58, 0x0B, 0x16, 0x31, 0x2C, 0x97, 0x8A, 0xAD, 0xB0, 0xE3, 0xFE, 0xD9, 0xC4, 
];

/// Calculate the CRC of given data using the table CRC_TABLE
pub fn crc(start_crc: u8, data: &[u8]) -> u8 {
    let mut crc = start_crc;

    for byte in data {
        crc = CRC_TABLE[(*byte ^ crc) as usize];
    }

    crc
}

/// ACK byte, set to 0xAA due to it's resiliance to most natural interference
pub const ACK: u8 = 0xAA;

#[repr(u8)]
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
/// Possible NAK codes, see README for more info
pub enum NakCode {
    Unknown = 0,
    SecondaryFailure = 1,
    BadCrc = 2,
    OutOfBounds = 3,
    IncompletePacket = 4,
    IndexWriteProtected = 5,
    CountExceedsBounds = 6,
}

impl From<u8> for NakCode {
    fn from(value: u8) -> Self {
        match value {
            1 => Self::SecondaryFailure,
            2 => Self::BadCrc,
            3 => Self::OutOfBounds,
            4 => Self::IncompletePacket,
            5 => Self::IndexWriteProtected,
            6 => Self::CountExceedsBounds,
            _ => Self::Unknown,
        }
    }
}

/// Errors a Primary client or Secondary server can return
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub enum Error<E> {
    Io(E),
    Nak(NakCode),
    SecondaryFailure,
    BadCrc,
    OutOfBounds(u16),
    IncompletePacket,
    IndexWriteProtected(u8, u16),
    CountExceedsBounds(u8, u16),
}

impl<E> Display for Error<E>
where
    E: Display,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Error::Io(e) => write!(f, "{}", e),
            Error::Nak(code) => write!(f, "NAK Recieved, code {:?}", code),
            Error::SecondaryFailure => write!(f, "Secondary Failure"),
            Error::BadCrc => write!(f, "Bad Crc"),
            Error::OutOfBounds(index) => write!(
                f,
                "Attempted to access index {}, which is out of bounds",
                index
            ),
            Error::IncompletePacket => write!(f, "Incomplete Packet"),
            Error::IndexWriteProtected(count, index) => write!(
                f,
                "Attempted to write to a write protected index between {} and {}",
                index,
                index + *count as u16
            ),
            Error::CountExceedsBounds(count, index) => write!(
                f,
                "Attempted to access {} registers at index {}, which exceeds bounds",
                count,
                index,
            ),
        }
    }
}

impl<E> From<E> for Error<E> {
    fn from(value: E) -> Self {
        Error::Io(value)
    }
}

impl<E> From<ReadExactError<E>> for Error<E> {
    fn from(value: ReadExactError<E>) -> Self {
        match value {
            ReadExactError::UnexpectedEof => Error::IncompletePacket,
            ReadExactError::Other(e) => Error::Io(e),
        }
    }
}


// A bunch of conversions that make this compatible between both embedded_io
// and std::io. Why there still isn't a core::io, you tell me.
#[cfg(feature = "std")]
pub struct StdIo<IO>
where
    IO: std::io::Read + std::io::Write,
{
    io: IO,
}

#[cfg(feature = "std")]
impl<IO> StdIo<IO>
where
    IO: std::io::Read + std::io::Write,
{
    #[inline]
    pub fn get_inner(&mut self) -> &IO {
        &self.io
    }

    #[inline]
    pub fn get_inner_mut(&mut self) -> &mut IO {
        &mut self.io
    }
}

#[cfg(feature = "std")]
impl<IO> From<IO> for StdIo<IO>
where
    IO: std::io::Read + std::io::Write,
{
    #[inline]
    fn from(value: IO) -> Self {
        Self { io: value }
    }
}

#[cfg(feature = "std")]
impl<IO> ErrorType for StdIo<IO>
where
    IO: std::io::Read + std::io::Write,
{
    type Error = std::io::Error;
}

#[cfg(feature = "std")]
impl<IO> Read for StdIo<IO>
where
    IO: std::io::Read + std::io::Write,
{
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
        self.io.read(buf)
    }
}

#[cfg(feature = "std")]
impl<IO> Write for StdIo<IO>
where
    IO: std::io::Read + std::io::Write,
{
    #[inline]
    fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
        self.io.write(buf)
    }

    #[inline]
    fn flush(&mut self) -> Result<(), Self::Error> {
        self.io.flush()
    }
}

/// Secondary server struct, allows you to poll and process incoming packets.
pub struct UrapSecondary<'a, 'c, IO, const REGCNT: usize>
where
    IO: Read + Write,
{
    io: &'a mut IO,
    writeprotect: &'c [bool; REGCNT],
}

impl<'a, 'c, IO, const REGCNT: usize> UrapSecondary<'a, 'c, IO, REGCNT>
where
    IO: Read + Write,
{
    /// Create a new secondary server with IO and a slice with boolean values
    /// corresponding to the write protect status of individual registers.
    pub fn new(
        io: &'a mut IO,
        writeprotect: &'c [bool; REGCNT],
    ) -> Self {
        Self {
            io,
            writeprotect,
        }
    }

    /// Poll the IO for data, and if there is data return the recieved packet
    /// to be further processed.
    pub fn poll(&mut self) -> Result<Option<UrapRecievedPacket>, Error<IO::Error>> {
        let mut buffer: [u8; URAP_HEAD_WIDTH + URAP_REG_WIDTH] = [0; URAP_HEAD_WIDTH + URAP_REG_WIDTH];

        let i = self.io.read(&mut buffer)?;

        if i > 0 {
            if i < buffer.len() {
                let buffer_len = buffer.len();
                self.io.read_exact(&mut buffer[i..buffer_len])?;
            }
        
            let head = buffer[0];
            let write = head & URAP_WRITE_OR > 0;
            let count = (head & !URAP_WRITE_OR) + 1;
            let calcd_crc = crc(0, &buffer);

            let start_register = u16::from_le_bytes([buffer[1], buffer[2]]);

            if write {
                let mut buffer: [u8; URAP_MAX_DATA_SIZE + URAP_CRC_WIDTH] = [0; URAP_MAX_DATA_SIZE + URAP_CRC_WIDTH];

                let count_bytes = count as usize * URAP_DATA_WIDTH;

                self.io.read_exact(&mut buffer[..count_bytes + URAP_CRC_WIDTH])?;

                let calcd_crc = crc(calcd_crc, &buffer[..count_bytes + URAP_CRC_WIDTH]);

                let nak_code = if calcd_crc != 0 {
                    Some(NakCode::BadCrc)
                } else if start_register as usize >= REGCNT {
                    Some(NakCode::OutOfBounds)
                } else if start_register as usize + count as usize > REGCNT {
                    Some(NakCode::CountExceedsBounds)
                } else {
                    let mut write_protected = false;
                    
                    for reg in &self.writeprotect[start_register as usize..start_register as usize + count as usize] {
                        write_protected = write_protected || *reg;
                    }

                    if write_protected {
                        Some(NakCode::IndexWriteProtected)
                    } else {
                        None
                    }
                };

                let write_buffer: &[[u8; URAP_DATA_WIDTH]; URAP_COUNT_MAX] = from_bytes(&buffer[..URAP_MAX_DATA_SIZE]);

                Ok(Some(UrapRecievedPacket {
                    count,
                    start_register,
                    write_buffer: Some(*write_buffer),
                    nak_code,
                }))
            } else {
                let mut buffer: [u8; URAP_CRC_WIDTH] = [0; URAP_CRC_WIDTH];

                self.io.read_exact(&mut buffer)?;

                let calcd_crc = crc(calcd_crc, &buffer[..URAP_CRC_WIDTH]);

                let nak_code = if calcd_crc != 0 {
                    Some(NakCode::BadCrc)
                } else if start_register as usize >= REGCNT {
                    Some(NakCode::OutOfBounds)
                } else if start_register as usize + count as usize > REGCNT {
                    Some(NakCode::CountExceedsBounds)
                } else {
                    None
                };

                Ok(Some(UrapRecievedPacket{
                    count,
                    start_register,
                    write_buffer: None,
                    nak_code,
                }))
            }
        } else {
            Ok(None)
        }
    }

    /// Process a packet read by polling.
    pub fn process(&mut self, recieved_packet: UrapRecievedPacket, registers: &mut [[u8; URAP_DATA_WIDTH]; REGCNT]) -> Result<(), Error<IO::Error>> {
        if let Some(nak_code) = recieved_packet.nak_code {
            self.io.write_all(&[nak_code as u8])?;

            return Ok(());
        }

        let start_register = recieved_packet.start_register as usize;
        let end_register = recieved_packet.start_register as usize + recieved_packet.count as usize;

        if let Some(write_buffer) = recieved_packet.write_buffer {
            registers[start_register..end_register].copy_from_slice(&write_buffer[..recieved_packet.count as usize]);

            self.io.write_all(&[ACK])?;
        } else {
            let mut buffer: [u8; URAP_ACK_WIDTH + URAP_MAX_DATA_SIZE + URAP_CRC_WIDTH] = [ACK; URAP_ACK_WIDTH + URAP_MAX_DATA_SIZE + URAP_CRC_WIDTH];

            let reg_start_offset = URAP_ACK_WIDTH;
            let reg_end_offset = reg_start_offset + URAP_DATA_WIDTH * recieved_packet.count as usize;
            let crc_index = reg_end_offset;
            let buffer_len = reg_end_offset + URAP_CRC_WIDTH;

            buffer[reg_start_offset..reg_end_offset].copy_from_slice(cast_slice(&registers[start_register..end_register]));

            let calcd_crc = crc(0, &buffer[reg_start_offset..reg_end_offset]);

            buffer[crc_index] = calcd_crc;

            self.io.write_all(&buffer[..buffer_len])?;
        }

        Ok(())
    }
}

/// A packet recieved during polling.
pub struct UrapRecievedPacket {
    /// Number of registers being accessed during operation.
    pub count: u8,
    /// The first register to be accessed in operation
    pub start_register: u16,
    /// If None, this is a read operation. If Some, the Some contains the entire write buffer.
    pub write_buffer: Option<[[u8; URAP_DATA_WIDTH]; URAP_COUNT_MAX]>,
    /// If there was an error the Nak code is here; needs to be written to the Primary first.
    pub nak_code: Option<NakCode>,
}

/// Primary client, used for interacting with a server via IO.
pub struct UrapPrimary<'a, IO>
where
    IO: Read + Write,
{
    io: &'a mut IO,
}

impl<'a, IO> UrapPrimary<'a, IO>
where
    IO: Read + Write,
{
    /// Create a client with IO.
    pub fn new(io: &'a mut IO) -> Self {
        Self { io }
    }

    /// Read `n` registers into an array of `[[u8; 4]; n]`
    pub fn read_4u8(&mut self, start_register: u16, data: &mut [[u8; 4]]) -> Result<(), Error<IO::Error>> {
        assert!(data.len() <= URAP_COUNT_MAX);

        if data.len() == 0 {
            return Ok(());
        }

        let start_register = start_register.to_le_bytes();

        let count = (data.len() - 1) as u8;

        let calcd_crc = crc(0, &[count]);
        let calcd_crc = crc(calcd_crc, &start_register);

        let packet_data: [u8; URAP_COUNT_WIDTH + URAP_REG_WIDTH + URAP_CRC_WIDTH] = [
            count,
            start_register[0],
            start_register[1],
            calcd_crc
        ];

        self.io.write_all(&packet_data)?;

        let mut ack_or_nak: [u8; 1] = [0; 1];

        self.io.read_exact(&mut ack_or_nak)?;


        if ack_or_nak[0] != ACK {
            return Err(Error::Nak(ack_or_nak[0].into()));
        }

        let data_bytes: &mut [u8] = cast_slice_mut(data);

        self.io.read_exact(data_bytes)?;

        let calcd_crc = crc(0, &data_bytes);

        let mut crc_data: [u8; URAP_CRC_WIDTH] = [0; URAP_CRC_WIDTH];

        self.io.read_exact(&mut crc_data)?;

        if crc(calcd_crc, &crc_data) != 0 {
            return Err(Error::BadCrc);
        }

        Ok(())
    }
 
    /// Write `n` registers from an array of `[[u8; 4]; n]`
    pub fn write_4u8(&mut self, start_register: u16, data: &[[u8; URAP_DATA_WIDTH]]) -> Result<(), Error<IO::Error>> {
        assert!(data.len() <= URAP_COUNT_MAX);

        if data.len() == 0 {
            return Ok(());
        }

        let start_register = start_register.to_le_bytes();

        let count = (data.len() - 1) as u8;
        let head = count | URAP_WRITE_OR;
        let data_bytes: &[u8] = cast_slice(data);

        let mut packet_data: [u8; URAP_MAX_PACKET_SIZE] = [0; URAP_MAX_PACKET_SIZE];

        let data_start_index = URAP_HEAD_WIDTH + URAP_REG_WIDTH;
        let data_end_index = data_start_index + data_bytes.len();
        let crc_index = data_end_index;
        let packet_end_index = crc_index + 1;

        packet_data[0] = head;
        packet_data[1] = start_register[0];
        packet_data[2] = start_register[1];
        packet_data[data_start_index..data_end_index].copy_from_slice(data_bytes);

        let calcd_crc = crc(0, &packet_data[..crc_index]);
        packet_data[crc_index] = calcd_crc;

        self.io.write_all(&packet_data[..packet_end_index])?;

        let mut ack_or_nak: [u8; 1] = [0];

        self.io.read_exact(&mut ack_or_nak)?;

        if ack_or_nak[0] != ACK {
            return Err(Error::Nak(ack_or_nak[0].into()));
        }

        Ok(())
    }

    /// Check if the connection is healthy
    #[inline]
    pub fn is_healthy(&mut self) -> bool {
        let mut buffer: [[u8; URAP_DATA_WIDTH]; 1] = [[0; URAP_DATA_WIDTH]; 1];
        match self.read_4u8(0, &mut buffer) {
            Ok(_) => true,
            Err(_) => false,
        }
    }
}