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
use bytes::{Buf, BufMut}; use error::Error; use num_traits::FromPrimitive; use std::io::{Cursor, Read}; #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct find_attribute { pub result: Error, pub attribute: u16, } impl From<&[u8]> for find_attribute { fn from(data: &[u8]) -> find_attribute { let mut cursor = Cursor::new(data); find_attribute { result: FromPrimitive::from_u16(cursor.get_u16_le()).unwrap(), attribute: cursor.get_u16_le(), } } } impl Into<Vec<u8>> for find_attribute { fn into(self) -> Vec<u8> { let mut bytes = Vec::new(); bytes.put_u16_le(self.result.clone() as u16); bytes.put_u16_le(self.attribute); bytes } } #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct read_attribute_type { pub result: Error, pub atype: Vec<u8>, } impl From<&[u8]> for read_attribute_type { fn from(data: &[u8]) -> read_attribute_type { let mut cursor = Cursor::new(data); let result = FromPrimitive::from_u16(cursor.get_u16_le()).unwrap(); let mut atype = Vec::new(); cursor.get_u8(); cursor .read_to_end(&mut atype) .expect("Failed to read bytes."); read_attribute_type { result, atype } } } impl Into<Vec<u8>> for read_attribute_type { fn into(self) -> Vec<u8> { let mut bytes = Vec::new(); bytes.put_u16_le(self.result.clone() as u16); bytes.put_u8(self.atype.len() as u8); bytes.extend(self.atype.iter()); bytes } } #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct read_attribute_value { pub result: Error, pub value: Vec<u8>, } impl From<&[u8]> for read_attribute_value { fn from(data: &[u8]) -> read_attribute_value { let mut cursor = Cursor::new(data); let result = FromPrimitive::from_u16(cursor.get_u16_le()).unwrap(); let mut value = Vec::new(); cursor.get_u8(); cursor .read_to_end(&mut value) .expect("Failed to read bytes."); read_attribute_value { result, value } } } impl Into<Vec<u8>> for read_attribute_value { fn into(self) -> Vec<u8> { let mut bytes = Vec::new(); bytes.put_u16_le(self.result.clone() as u16); bytes.put_u8(self.value.len() as u8); bytes.extend(self.value.iter()); bytes } } #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct send_characteristic_notification { pub result: Error, pub sent_len: u16, } impl From<&[u8]> for send_characteristic_notification { fn from(data: &[u8]) -> send_characteristic_notification { let mut cursor = Cursor::new(data); send_characteristic_notification { result: FromPrimitive::from_u16(cursor.get_u16_le()).unwrap(), sent_len: cursor.get_u16_le(), } } } impl Into<Vec<u8>> for send_characteristic_notification { fn into(self) -> Vec<u8> { let mut bytes = Vec::new(); bytes.put_u16_le(self.result.clone() as u16); bytes.put_u16_le(self.sent_len); bytes } } #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct send_user_read_response { pub result: Error, pub sent_len: u16, } impl From<&[u8]> for send_user_read_response { fn from(data: &[u8]) -> send_user_read_response { let mut cursor = Cursor::new(data); send_user_read_response { result: FromPrimitive::from_u16(cursor.get_u16_le()).unwrap(), sent_len: cursor.get_u16_le(), } } } impl Into<Vec<u8>> for send_user_read_response { fn into(self) -> Vec<u8> { let mut bytes = Vec::new(); bytes.put_u16_le(self.result.clone() as u16); bytes.put_u16_le(self.sent_len); bytes } } #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct send_user_write_response { pub result: Error, } impl From<&[u8]> for send_user_write_response { fn from(data: &[u8]) -> send_user_write_response { let mut cursor = Cursor::new(data); send_user_write_response { result: FromPrimitive::from_u16(cursor.get_u16_le()).unwrap(), } } } impl Into<Vec<u8>> for send_user_write_response { fn into(self) -> Vec<u8> { let mut bytes = Vec::new(); bytes.put_u16_le(self.result.clone() as u16); bytes } } #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct set_capabilities { pub result: Error, } impl From<&[u8]> for set_capabilities { fn from(data: &[u8]) -> set_capabilities { let mut cursor = Cursor::new(data); set_capabilities { result: FromPrimitive::from_u16(cursor.get_u16_le()).unwrap(), } } } impl Into<Vec<u8>> for set_capabilities { fn into(self) -> Vec<u8> { let mut bytes = Vec::new(); bytes.put_u16_le(self.result.clone() as u16); bytes } } #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct write_attribute_value { pub result: Error, } impl From<&[u8]> for write_attribute_value { fn from(data: &[u8]) -> write_attribute_value { let mut cursor = Cursor::new(data); write_attribute_value { result: FromPrimitive::from_u16(cursor.get_u16_le()).unwrap(), } } } impl Into<Vec<u8>> for write_attribute_value { fn into(self) -> Vec<u8> { let mut bytes = Vec::new(); bytes.put_u16_le(self.result.clone() as u16); bytes } }