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
#![cfg_attr(all(not(feature="std"), not(test)), no_std)]

extern crate alloc;

pub mod grapple;
#[cfg(feature = "ni")]
pub mod ni;
pub mod macros;
pub mod bridge;

pub use binmarshal;

use binmarshal::Demarshal;
use binmarshal::Marshal;
use binmarshal::MarshalUpdate;
use grapple::MANUFACTURER_GRAPPLE;
use grapple::MaybeFragment;
use grapple::errors::GrappleResult;

pub const DEVICE_TYPE_BROADCAST: u8 = 0x00;
pub const DEVICE_TYPE_FIRMWARE_UPGRADE: u8 = 31;
pub const DEVICE_ID_BROADCAST: u8 = 0x3F;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct MessageId {
  pub device_type: u8,
  pub manufacturer: u8,
  pub api_class: u8,
  pub api_index: u8,
  pub device_id: u8,
}

impl From<u32> for MessageId {
  fn from(value: u32) -> Self {
    Self {
      device_type: ((value >> 6+4+6+8) & 0b11111) as u8,
      manufacturer: ((value >> 6+4+6) & 0b11111111) as u8,
      api_class: ((value >> 6+4) & 0b111111) as u8,
      api_index: ((value >> 6) & 0b1111) as u8,
      device_id: (value & 0b111111) as u8
    }
  }
}

impl From<MessageId> for u32 {
  fn from(v: MessageId) -> Self {
    (v.device_id as u32 & 0b111111)
    | ((v.api_index as u32 & 0b1111) << 6)
    | ((v.api_class as u32 & 0b111111) << (6+4))
    | ((v.manufacturer as u32 & 0b11111111) << (6+4+6))
    | ((v.device_type as u32 & 0b11111) << (6+4+6+8))
  }
}

impl Marshal for MessageId {
  fn write<W: binmarshal::BitWriter>(&self, writer: &mut W, ctx: ()) -> Result<(), binmarshal::MarshalError> {
    Into::<u32>::into(*self).write(writer, ctx)
  }
}

impl<'dm> Demarshal<'dm, ()> for MessageId {
  fn read(view: &mut binmarshal::BitView<'dm>, ctx: ()) -> Result<Self, binmarshal::MarshalError> {
    Ok(Into::<Self>::into(u32::read(view, ctx)?))
  }
}

#[derive(Debug, Clone, PartialEq, Eq, Marshal, Demarshal, MarshalUpdate)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Message<'a> {
  pub id: MessageId,

  #[marshal(
    ctx = "construct",
    ctx_type = MessageId,
    ctx_member(field = "device_type", member = "id.device_type"),
    ctx_member(field = "manufacturer", member = "id.manufacturer"),
    ctx_member(field = "api_class", member = "id.api_class"),
    ctx_member(field = "api_index", member = "id.api_index"),
    ctx_member(field = "device_id", member = "id.device_id"),
  )]
  #[cfg_attr(feature = "serde", serde(borrow))]
  pub msg: ManufacturerMessage<'a>
}

impl<'a> Message<'a> {
  pub fn new(device_id: u8, msg: ManufacturerMessage<'a>) -> Self {
    let mut newmsg = Self {
      id: MessageId {
        device_type: 0,
        manufacturer: 0,
        api_class: 0,
        api_index: 0,
        device_id,
      },
      msg,
    };

    newmsg.update(&mut ());

    newmsg
  }
}

impl<'a> Validate for Message<'a> {
  fn validate(&self) -> GrappleResult<()> {
    self.msg.validate()
  }
}

#[derive(Debug, Clone, PartialEq, Eq, Marshal, Demarshal, MarshalUpdate)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[marshal(ctx = MessageId, tag = "ctx.manufacturer")]
pub enum ManufacturerMessage<'a> {
  #[cfg(feature = "ni")]
  #[marshal(tag = "ni::MANUFACTURER_NI")]
  Ni(
    #[marshal(ctx = "forward")]
    ni::NiDeviceMessage
  ),
  #[marshal(tag = "MANUFACTURER_GRAPPLE")]
  Grapple(
    #[marshal(ctx = "coerce", ctx_type = crate::grapple::GrappleMessageId)]
    #[cfg_attr(feature = "serde", serde(borrow))]
    MaybeFragment<'a>
  )
}

impl<'a> Validate for ManufacturerMessage<'a> {
  fn validate(&self) -> GrappleResult<()> {
    match self {
      #[cfg(feature = "ni")]
      ManufacturerMessage::Ni(_) => Ok(()),
      ManufacturerMessage::Grapple(grpl) => grpl.validate(),
    }
  }
}

pub trait Validate {
  fn validate(&self) -> GrappleResult<()>;
}