ble_data_struct/descriptors/server_characteristic_configuration.rs
1//! Server Characteristic Configuration (Attribute Type: 0x2903) module.
2
3use crate::Uuid16bit;
4
5/// Server Characteristic Configuration.
6#[derive(Debug, PartialEq, Clone)]
7pub struct ServerCharacteristicConfiguration {
8 /// Characteristic Configuration Bits
9 pub configuration: u16,
10}
11
12impl ServerCharacteristicConfiguration {
13 /// Create [`ServerCharacteristicConfiguration`] from `Characteristic Configuration Bit`.
14 ///
15 /// # Examples
16 ///
17 /// ```
18 /// use ble_data_struct::descriptors::server_characteristic_configuration::{
19 /// ServerCharacteristicConfiguration, BROADCAST,
20 /// };
21 ///
22 /// let result = ServerCharacteristicConfiguration::new(BROADCAST);
23 /// assert_eq!(BROADCAST, result.configuration);
24 /// ```
25 pub fn new(configuration: u16) -> Self {
26 Self { configuration }
27 }
28
29 /// check Notification configuration.
30 ///
31 /// # Examples
32 ///
33 /// ```
34 /// use ble_data_struct::descriptors::server_characteristic_configuration::{
35 /// ServerCharacteristicConfiguration, BROADCAST,
36 /// };
37 ///
38 /// let result = ServerCharacteristicConfiguration::new(BROADCAST);
39 /// assert!(result.is_broadcast());
40 /// ```
41 pub fn is_broadcast(&self) -> bool {
42 self.configuration == BROADCAST
43 }
44}
45
46/// Broadcast
47pub const BROADCAST: u16 = 0b00000001;
48
49impl TryFrom<&Vec<u8>> for ServerCharacteristicConfiguration {
50 type Error = String;
51 /// Create [`ServerCharacteristicConfiguration`] from [`Vec<u8>`].
52 ///
53 /// # Examples
54 ///
55 /// ```
56 /// use ble_data_struct::descriptors::server_characteristic_configuration::{
57 /// ServerCharacteristicConfiguration, BROADCAST,
58 /// };
59 ///
60 /// let configuration = BROADCAST.to_le_bytes().to_vec();
61 /// let result = ServerCharacteristicConfiguration::try_from(&configuration);
62 /// assert!(result.is_ok());
63 /// assert_eq!(BROADCAST, result.unwrap().configuration);
64 ///
65 /// let configuration = Vec::new();
66 /// let result = ServerCharacteristicConfiguration::try_from(&configuration);
67 /// assert!(!result.is_ok());
68 /// ```
69 fn try_from(value: &Vec<u8>) -> Result<Self, String> {
70 let len = value.len();
71 if len != 2 {
72 return Err(format!("Invalid data size :{}", len).to_string());
73 }
74 Ok(Self {
75 configuration: u16::from_le_bytes(value[..2].try_into().unwrap()),
76 })
77 }
78}
79
80impl Into<Vec<u8>> for ServerCharacteristicConfiguration {
81 /// Create [`Vec<u8>`] from [`ServerCharacteristicConfiguration`].
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// use ble_data_struct::descriptors::server_characteristic_configuration::{
87 /// ServerCharacteristicConfiguration, BROADCAST,
88 /// };
89 ///
90 /// let configuration = BROADCAST.to_le_bytes().to_vec();
91 /// let result = ServerCharacteristicConfiguration::new(BROADCAST);
92 /// let into_data: Vec<u8> = result.into();
93 /// assert_eq!(configuration, into_data);
94 /// ```
95 fn into(self) -> Vec<u8> {
96 u16::to_le_bytes(self.configuration).to_vec()
97 }
98}
99
100impl Uuid16bit for ServerCharacteristicConfiguration {
101 /// return `0x2903`.
102 ///
103 /// # Examples
104 ///
105 /// ```
106 /// use ble_data_struct::Uuid16bit;
107 /// use ble_data_struct::descriptors::server_characteristic_configuration::ServerCharacteristicConfiguration;
108 ///
109 /// assert_eq!(0x2903, ServerCharacteristicConfiguration::uuid_16bit());
110 /// ```
111 fn uuid_16bit() -> u16 {
112 0x2903
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use crate::{
119 descriptors::server_characteristic_configuration::{
120 ServerCharacteristicConfiguration, BROADCAST,
121 },
122 Uuid16bit,
123 };
124
125 #[test]
126 fn test_new() {
127 let result = ServerCharacteristicConfiguration::new(BROADCAST);
128 assert_eq!(BROADCAST, result.configuration);
129 }
130
131 #[test]
132 fn test_is_broadcast() {
133 let result = ServerCharacteristicConfiguration::new(BROADCAST);
134 assert!(result.is_broadcast());
135 }
136
137 #[test]
138 fn test_try_from() {
139 let configuration = BROADCAST.to_le_bytes().to_vec();
140 let result = ServerCharacteristicConfiguration::try_from(&configuration);
141 assert!(result.is_ok());
142 assert_eq!(BROADCAST, result.unwrap().configuration);
143
144 let configuration = Vec::new();
145 let result = ServerCharacteristicConfiguration::try_from(&configuration);
146 assert!(!result.is_ok());
147 }
148
149 #[test]
150 fn test_into() {
151 let configuration = BROADCAST.to_le_bytes().to_vec();
152 let result = ServerCharacteristicConfiguration::new(BROADCAST);
153 let into_data: Vec<u8> = result.into();
154 assert_eq!(configuration, into_data);
155 }
156
157 #[test]
158 fn test_uuid_16bit() {
159 assert_eq!(0x2903, ServerCharacteristicConfiguration::uuid_16bit());
160 }
161}