Skip to main content

ember_plus/s101/
mod.rs

1//! S101 framing protocol implementation.
2//!
3//! S101 is the transport layer protocol used by Ember+ for framing messages
4//! over TCP connections. It provides:
5//! - Message framing with start/end markers
6//! - Byte escaping (byte stuffing)
7//! - CRC-16 error detection
8//! - Message type identification
9//! - Keep-alive mechanism
10
11mod frame;
12mod codec;
13mod crc;
14mod constants;
15
16pub use frame::{S101Frame, S101Message, MessageType, Command};
17pub use codec::{S101Codec, S101Decoder, S101Encoder};
18pub use constants::*;
19
20use crate::error::{S101Error, Result};
21
22/// S101 slot identifiers.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24#[repr(u8)]
25pub enum Slot {
26    /// Default slot (used for Ember+ messages)
27    Default = 0x00,
28}
29
30impl TryFrom<u8> for Slot {
31    type Error = S101Error;
32
33    fn try_from(value: u8) -> std::result::Result<Self, Self::Error> {
34        match value {
35            0x00 => Ok(Slot::Default),
36            _ => Err(S101Error::InvalidSlot(value)),
37        }
38    }
39}
40
41/// S101 protocol version.
42pub const S101_VERSION: u8 = 0x01;
43
44/// DTD type for Glow (Ember+ payload type).
45pub const DTD_GLOW: u8 = 0x01;
46
47/// Application bytes for Glow.
48pub const APP_BYTES_GLOW: [u8; 2] = [0x01, 0x31]; // Version 1.49
49
50/// Encode a payload into an S101 frame.
51pub fn encode_frame(payload: &[u8]) -> Vec<u8> {
52    S101Encoder::encode_ember_packet(payload)
53}
54
55/// Decode an S101 frame and extract the payload.
56pub fn decode_frame(data: &[u8]) -> Result<Vec<u8>> {
57    let frame = S101Frame::decode(data)?;
58    Ok(frame.payload)
59}