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
use std::{
fmt,
hash::{Hash, Hasher},
};
use crate::{header::Header, id_hash::IdHash};
/// The `Telegram` type stores information according to the VBus protocol version 3.x.
///
/// Telegrams are used to transmit small amount of information (up to 21 bytes of payload).
///
/// ## The "identity" of `Telegram` values
///
/// As described in [the corresponding section of the `Header` struct][1] VBus data types use
/// some of their fields as part of their "identity". In addition to the fields used by the
/// `Header` type the `Telegram` type also respects the `command` field. That means that two
/// `Telegram` with differing `timestamp` and `frame_data` fields are still considered
/// "identical", if the other fields match.
///
/// [1]: struct.Header.html#the-identity-of-header-values
pub struct Telegram {
/// The shared `Header` of all VBus protocol types.
pub header: Header,
/// The command of this `Telegram`.
pub command: u8,
/// The actual data from the frames attached to this `Telegram`.
pub frame_data: [u8; 21],
}
impl Telegram {
/// Get number of frames from a VBus protocol version 3.x command.
///
/// # Examples
///
/// ```rust
/// use resol_vbus::Telegram;
///
/// assert_eq!(0, Telegram::frame_count_from_command(0x1F));
/// assert_eq!(1, Telegram::frame_count_from_command(0x3F));
/// assert_eq!(2, Telegram::frame_count_from_command(0x5F));
/// assert_eq!(3, Telegram::frame_count_from_command(0x7F));
/// ```
pub fn frame_count_from_command(command: u8) -> u8 {
command >> 5
}
/// Get number of 7-byte frames attached to this `Telegram`.
///
/// # Examples
///
/// ```rust
/// use resol_vbus::{Telegram, Header};
/// use resol_vbus::utils::utc_timestamp;
///
/// let tgram = Telegram {
/// header: Header {
/// timestamp: utc_timestamp(1485688933),
/// channel: 0x11,
/// destination_address: 0x1213,
/// source_address: 0x1415,
/// protocol_version: 0x36,
/// },
/// command: 0x37,
/// frame_data: [0u8; 21],
/// };
///
/// assert_eq!(1, tgram.frame_count());
/// ```
pub fn frame_count(&self) -> u8 {
Telegram::frame_count_from_command(self.command)
}
/// Return the length of the valid area of the `frame_data`.
///
/// # Examples
///
/// ```rust
/// use resol_vbus::{Telegram, Header};
/// use resol_vbus::utils::utc_timestamp;
///
/// let tgram = Telegram {
/// header: Header {
/// timestamp: utc_timestamp(1485688933),
/// channel: 0x11,
/// destination_address: 0x1213,
/// source_address: 0x1415,
/// protocol_version: 0x36,
/// },
/// command: 0x37,
/// frame_data: [0u8; 21],
/// };
///
/// assert_eq!(7, tgram.valid_frame_data_len());
/// ```
pub fn valid_frame_data_len(&self) -> usize {
self.frame_count() as usize * 7
}
/// Return the valid area of the `frame_data` immutably.
///
/// # Examples
///
/// ```rust
/// use resol_vbus::{Telegram, Header};
/// use resol_vbus::utils::utc_timestamp;
///
/// let tgram = Telegram {
/// header: Header {
/// timestamp: utc_timestamp(1485688933),
/// channel: 0x11,
/// destination_address: 0x1213,
/// source_address: 0x1415,
/// protocol_version: 0x36,
/// },
/// command: 0x37,
/// frame_data: [0u8; 21],
/// };
///
/// assert_eq!(7, tgram.valid_frame_data().len());
/// ```
pub fn valid_frame_data(&self) -> &[u8] {
let end = self.valid_frame_data_len();
&self.frame_data[0..end]
}
/// Return the valid area of the `frame_data` mutably.
///
/// # Examples
///
/// ```rust
/// use resol_vbus::{Telegram, Header};
/// use resol_vbus::utils::utc_timestamp;
///
/// let mut tgram = Telegram {
/// header: Header {
/// timestamp: utc_timestamp(1485688933),
/// channel: 0x11,
/// destination_address: 0x1213,
/// source_address: 0x1415,
/// protocol_version: 0x36,
/// },
/// command: 0x37,
/// frame_data: [0u8; 21],
/// };
///
/// assert_eq!(7, tgram.valid_frame_data_mut().len());
/// ```
pub fn valid_frame_data_mut(&mut self) -> &mut [u8] {
let end = self.valid_frame_data_len();
&mut self.frame_data[0..end]
}
/// Creates an identification string for this `Telegram`.
///
/// The string contains all fields that count towards the "identity" of the `Telegram`:
///
/// - `channel`
/// - `destination_address`
/// - `source_address`
/// - `protocol_version`
/// - `command`
///
/// # Examples
///
/// ```rust
/// use resol_vbus::{Telegram, Header};
/// use resol_vbus::utils::utc_timestamp;
///
/// let tgram = Telegram {
/// header: Header {
/// timestamp: utc_timestamp(1485688933),
/// channel: 0x11,
/// destination_address: 0x1213,
/// source_address: 0x1415,
/// protocol_version: 0x36,
/// },
/// command: 0x17,
/// frame_data: [0u8; 21],
/// };
///
/// assert_eq!("11_1213_1415_36_17", tgram.id_string());
/// ```
pub fn id_string(&self) -> String {
format!("{}_{:02X}", self.header.id_string(), self.command)
}
}
impl IdHash for Telegram {
/// Returns an identification hash for this `Telegram`.
///
/// The hash contains all fields that count towards the "identity" of the `Telegram`:
///
/// - `channel`
/// - `destination_address`
/// - `source_address`
/// - `protocol_version`
/// - `command`
///
/// # Examples
///
/// ```rust
/// use resol_vbus::{Header, Telegram, id_hash};
/// use resol_vbus::utils::utc_timestamp;
///
/// let tgram = Telegram {
/// header: Header {
/// timestamp: utc_timestamp(1485688933),
/// channel: 0x11,
/// destination_address: 0x1213,
/// source_address: 0x1415,
/// protocol_version: 0x36,
/// },
/// command: 0x17,
/// frame_data: [0u8; 21],
/// };
///
/// assert_eq!(7671625633196679790, id_hash(&tgram));
/// ```
fn id_hash<H: Hasher>(&self, h: &mut H) {
self.header.id_hash(h);
self.command.hash(h);
}
}
impl fmt::Debug for Telegram {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Telegram")
.field("header", &self.header)
.field("command", &format_args!("0x{:02X}", self.command))
.field("frame_data", &format_args!("..."))
.finish()
}
}
impl Clone for Telegram {
fn clone(&self) -> Self {
let mut frame_data = [0u8; 21];
frame_data.copy_from_slice(&self.frame_data);
Telegram {
header: self.header.clone(),
command: self.command,
frame_data,
}
}
}
impl AsRef<Header> for Telegram {
fn as_ref(&self) -> &Header {
&self.header
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::utc_timestamp;
#[test]
fn test_frame_count_from_command() {
assert_eq!(0, Telegram::frame_count_from_command(0x1F));
assert_eq!(1, Telegram::frame_count_from_command(0x3F));
assert_eq!(2, Telegram::frame_count_from_command(0x5F));
assert_eq!(3, Telegram::frame_count_from_command(0x7F));
}
#[test]
fn test_frame_count() {
let tgram = Telegram {
header: Header {
timestamp: utc_timestamp(1485688933),
channel: 0x11,
destination_address: 0x1213,
source_address: 0x1415,
protocol_version: 0x36,
},
command: 0x37,
frame_data: [0u8; 21],
};
assert_eq!(1, tgram.frame_count());
}
#[test]
fn test_valid_frame_data_len() {
let tgram = Telegram {
header: Header {
timestamp: utc_timestamp(1485688933),
channel: 0x11,
destination_address: 0x1213,
source_address: 0x1415,
protocol_version: 0x36,
},
command: 0x37,
frame_data: [0u8; 21],
};
assert_eq!(7, tgram.valid_frame_data_len());
}
#[test]
fn test_valid_frame_data() {
let tgram = Telegram {
header: Header {
timestamp: utc_timestamp(1485688933),
channel: 0x11,
destination_address: 0x1213,
source_address: 0x1415,
protocol_version: 0x36,
},
command: 0x37,
frame_data: [0u8; 21],
};
assert_eq!(7, tgram.valid_frame_data().len());
}
#[test]
fn test_valid_frame_data_mut() {
let mut tgram = Telegram {
header: Header {
timestamp: utc_timestamp(1485688933),
channel: 0x11,
destination_address: 0x1213,
source_address: 0x1415,
protocol_version: 0x36,
},
command: 0x37,
frame_data: [0u8; 21],
};
assert_eq!(7, tgram.valid_frame_data_mut().len());
}
#[test]
fn test_debug_fmt() {
let tgram = Telegram {
header: Header {
timestamp: utc_timestamp(1485688933),
channel: 0x11,
destination_address: 0x1213,
source_address: 0x1415,
protocol_version: 0x36,
},
command: 0x17,
frame_data: [0u8; 21],
};
let result = format!("{:?}", tgram);
assert_eq!("Telegram { header: Header { timestamp: 2017-01-29T11:22:13Z, channel: 0x11, destination_address: 0x1213, source_address: 0x1415, protocol_version: 0x36 }, command: 0x17, frame_data: ... }", result);
}
}