1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // @generated rust packets from uci_packets.pdl 15 16 #![allow(clippy::all)] 17 #![allow(non_upper_case_globals)] 18 #![allow(non_camel_case_types)] 19 #![allow(non_snake_case)] 20 #![allow(unused)] 21 #![allow(missing_docs)] 22 23 use bytes::{BufMut, Bytes, BytesMut}; 24 use num_derive::{FromPrimitive, ToPrimitive}; 25 use num_traits::{FromPrimitive, ToPrimitive}; 26 use std::convert::{TryFrom, TryInto}; 27 use std::fmt; 28 use std::sync::Arc; 29 use thiserror::Error; 30 31 type Result<T> = std::result::Result<T, Error>; 32 33 #[derive(Debug, Error)] 34 pub enum Error { 35 #[error("Packet parsing failed")] 36 InvalidPacketError, 37 #[error("{field} was {value:x}, which is not known")] 38 ConstraintOutOfBounds { field: String, value: u64 }, 39 #[error("when parsing {obj}.{field} needed length of {wanted} but got {got}")] 40 InvalidLengthError { 41 obj: String, 42 field: String, 43 wanted: usize, 44 got: usize, 45 }, 46 #[error("Due to size restrictions a struct could not be parsed.")] 47 ImpossibleStructError, 48 #[error("when parsing field {obj}.{field}, {value} is not a valid {type_} value")] 49 InvalidEnumValueError { 50 obj: String, 51 field: String, 52 value: u64, 53 type_: String, 54 }, 55 } 56 57 #[derive(Debug, Error)] 58 #[error("{0}")] 59 pub struct TryFromError(&'static str); 60 61 pub trait Packet { to_bytes(self) -> Bytes62 fn to_bytes(self) -> Bytes; to_vec(self) -> Vec<u8>63 fn to_vec(self) -> Vec<u8>; 64 } 65 66 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 67 #[repr(u64)] 68 pub enum PacketBoundaryFlag { 69 Complete = 0x0, 70 NotComplete = 0x1, 71 } 72 impl fmt::Display for PacketBoundaryFlag { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 74 match self { 75 PacketBoundaryFlag::Complete => write!(f, "{:#04X} (COMPLETE)", self.to_u8().unwrap()), 76 PacketBoundaryFlag::NotComplete => { 77 write!(f, "{:#04X} (NOT_COMPLETE)", self.to_u8().unwrap()) 78 } 79 } 80 } 81 } 82 83 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 84 #[repr(u64)] 85 pub enum GroupId { 86 Core = 0x0, 87 SessionConfig = 0x1, 88 RangingSessionControl = 0x2, 89 DataControl = 0x3, 90 VendorReservedA = 0xa, 91 VendorReservedB = 0xb, 92 VendorAndroid = 0xc, 93 Test = 0xd, 94 VendorReservedE = 0xe, 95 VendorReservedF = 0xf, 96 } 97 impl fmt::Display for GroupId { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result98 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 99 match self { 100 GroupId::Core => write!(f, "{:#04X} (CORE)", self.to_u8().unwrap()), 101 GroupId::SessionConfig => write!(f, "{:#04X} (SESSION_CONFIG)", self.to_u8().unwrap()), 102 GroupId::RangingSessionControl => write!( 103 f, 104 "{:#04X} (RANGING_SESSION_CONTROL)", 105 self.to_u8().unwrap() 106 ), 107 GroupId::DataControl => write!(f, "{:#04X} (DATA_CONTROL)", self.to_u8().unwrap()), 108 GroupId::VendorReservedA => { 109 write!(f, "{:#04X} (VENDOR_RESERVED_A)", self.to_u8().unwrap()) 110 } 111 GroupId::VendorReservedB => { 112 write!(f, "{:#04X} (VENDOR_RESERVED_B)", self.to_u8().unwrap()) 113 } 114 GroupId::VendorAndroid => write!(f, "{:#04X} (VENDOR_ANDROID)", self.to_u8().unwrap()), 115 GroupId::Test => write!(f, "{:#04X} (TEST)", self.to_u8().unwrap()), 116 GroupId::VendorReservedE => { 117 write!(f, "{:#04X} (VENDOR_RESERVED_E)", self.to_u8().unwrap()) 118 } 119 GroupId::VendorReservedF => { 120 write!(f, "{:#04X} (VENDOR_RESERVED_F)", self.to_u8().unwrap()) 121 } 122 } 123 } 124 } 125 126 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 127 #[repr(u64)] 128 pub enum CoreOpCode { 129 CoreDeviceReset = 0x0, 130 CoreDeviceStatusNtf = 0x1, 131 CoreGetDeviceInfo = 0x2, 132 CoreGetCapsInfo = 0x3, 133 CoreSetConfig = 0x4, 134 CoreGetConfig = 0x5, 135 CoreDeviceSuspend = 0x6, 136 CoreGenericErrorNtf = 0x7, 137 } 138 impl fmt::Display for CoreOpCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result139 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 140 match self { 141 CoreOpCode::CoreDeviceReset => { 142 write!(f, "{:#04X} (CORE_DEVICE_RESET)", self.to_u8().unwrap()) 143 } 144 CoreOpCode::CoreDeviceStatusNtf => { 145 write!(f, "{:#04X} (CORE_DEVICE_STATUS_NTF)", self.to_u8().unwrap()) 146 } 147 CoreOpCode::CoreGetDeviceInfo => { 148 write!(f, "{:#04X} (CORE_GET_DEVICE_INFO)", self.to_u8().unwrap()) 149 } 150 CoreOpCode::CoreGetCapsInfo => { 151 write!(f, "{:#04X} (CORE_GET_CAPS_INFO)", self.to_u8().unwrap()) 152 } 153 CoreOpCode::CoreSetConfig => { 154 write!(f, "{:#04X} (CORE_SET_CONFIG)", self.to_u8().unwrap()) 155 } 156 CoreOpCode::CoreGetConfig => { 157 write!(f, "{:#04X} (CORE_GET_CONFIG)", self.to_u8().unwrap()) 158 } 159 CoreOpCode::CoreDeviceSuspend => { 160 write!(f, "{:#04X} (CORE_DEVICE_SUSPEND)", self.to_u8().unwrap()) 161 } 162 CoreOpCode::CoreGenericErrorNtf => { 163 write!(f, "{:#04X} (CORE_GENERIC_ERROR_NTF)", self.to_u8().unwrap()) 164 } 165 } 166 } 167 } 168 169 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 170 #[repr(u64)] 171 pub enum SessionOpCode { 172 SessionInit = 0x0, 173 SessionDeinit = 0x1, 174 SessionStatusNtf = 0x2, 175 SessionSetAppConfig = 0x3, 176 SessionGetAppConfig = 0x4, 177 SessionGetCount = 0x5, 178 SessionGetState = 0x6, 179 SessionUpdateControllerMulticastList = 0x7, 180 } 181 impl fmt::Display for SessionOpCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result182 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 183 match self { 184 SessionOpCode::SessionInit => { 185 write!(f, "{:#04X} (SESSION_INIT)", self.to_u8().unwrap()) 186 } 187 SessionOpCode::SessionDeinit => { 188 write!(f, "{:#04X} (SESSION_DEINIT)", self.to_u8().unwrap()) 189 } 190 SessionOpCode::SessionStatusNtf => { 191 write!(f, "{:#04X} (SESSION_STATUS_NTF)", self.to_u8().unwrap()) 192 } 193 SessionOpCode::SessionSetAppConfig => { 194 write!(f, "{:#04X} (SESSION_SET_APP_CONFIG)", self.to_u8().unwrap()) 195 } 196 SessionOpCode::SessionGetAppConfig => { 197 write!(f, "{:#04X} (SESSION_GET_APP_CONFIG)", self.to_u8().unwrap()) 198 } 199 SessionOpCode::SessionGetCount => { 200 write!(f, "{:#04X} (SESSION_GET_COUNT)", self.to_u8().unwrap()) 201 } 202 SessionOpCode::SessionGetState => { 203 write!(f, "{:#04X} (SESSION_GET_STATE)", self.to_u8().unwrap()) 204 } 205 SessionOpCode::SessionUpdateControllerMulticastList => write!( 206 f, 207 "{:#04X} (SESSION_UPDATE_CONTROLLER_MULTICAST_LIST)", 208 self.to_u8().unwrap() 209 ), 210 } 211 } 212 } 213 214 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 215 #[repr(u64)] 216 pub enum RangeOpCode { 217 RangeStart = 0x0, 218 RangeStop = 0x1, 219 RangeIntervalUpdateReq = 0x2, 220 RangeGetRangingCount = 0x3, 221 } 222 impl fmt::Display for RangeOpCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result223 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 224 match self { 225 RangeOpCode::RangeStart => write!(f, "{:#04X} (RANGE_START)", self.to_u8().unwrap()), 226 RangeOpCode::RangeStop => write!(f, "{:#04X} (RANGE_STOP)", self.to_u8().unwrap()), 227 RangeOpCode::RangeIntervalUpdateReq => write!( 228 f, 229 "{:#04X} (RANGE_INTERVAL_UPDATE_REQ)", 230 self.to_u8().unwrap() 231 ), 232 RangeOpCode::RangeGetRangingCount => write!( 233 f, 234 "{:#04X} (RANGE_GET_RANGING_COUNT)", 235 self.to_u8().unwrap() 236 ), 237 } 238 } 239 } 240 241 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 242 #[repr(u64)] 243 pub enum AppDataOpCode { 244 AppDataTx = 0x0, 245 AppDataRx = 0x1, 246 } 247 impl fmt::Display for AppDataOpCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result248 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 249 match self { 250 AppDataOpCode::AppDataTx => write!(f, "{:#04X} (APP_DATA_TX)", self.to_u8().unwrap()), 251 AppDataOpCode::AppDataRx => write!(f, "{:#04X} (APP_DATA_RX)", self.to_u8().unwrap()), 252 } 253 } 254 } 255 256 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 257 #[repr(u64)] 258 pub enum PicaOpCode { 259 PicaInitDevice = 0x0, 260 PicaSetDevicePosition = 0x1, 261 PicaCreateBeacon = 0x2, 262 PicaSetBeaconPosition = 0x3, 263 PicaDestroyBeacon = 0x4, 264 } 265 impl fmt::Display for PicaOpCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result266 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 267 match self { 268 PicaOpCode::PicaInitDevice => { 269 write!(f, "{:#04X} (PICA_INIT_DEVICE)", self.to_u8().unwrap()) 270 } 271 PicaOpCode::PicaSetDevicePosition => write!( 272 f, 273 "{:#04X} (PICA_SET_DEVICE_POSITION)", 274 self.to_u8().unwrap() 275 ), 276 PicaOpCode::PicaCreateBeacon => { 277 write!(f, "{:#04X} (PICA_CREATE_BEACON)", self.to_u8().unwrap()) 278 } 279 PicaOpCode::PicaSetBeaconPosition => write!( 280 f, 281 "{:#04X} (PICA_SET_BEACON_POSITION)", 282 self.to_u8().unwrap() 283 ), 284 PicaOpCode::PicaDestroyBeacon => { 285 write!(f, "{:#04X} (PICA_DESTROY_BEACON)", self.to_u8().unwrap()) 286 } 287 } 288 } 289 } 290 291 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 292 #[repr(u64)] 293 pub enum AndroidOpCode { 294 AndroidGetPowerStats = 0x0, 295 AndroidSetCountryCode = 0x1, 296 } 297 impl fmt::Display for AndroidOpCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result298 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 299 match self { 300 AndroidOpCode::AndroidGetPowerStats => write!( 301 f, 302 "{:#04X} (ANDROID_GET_POWER_STATS)", 303 self.to_u8().unwrap() 304 ), 305 AndroidOpCode::AndroidSetCountryCode => write!( 306 f, 307 "{:#04X} (ANDROID_SET_COUNTRY_CODE)", 308 self.to_u8().unwrap() 309 ), 310 } 311 } 312 } 313 314 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 315 #[repr(u64)] 316 pub enum StatusCode { 317 UciStatusOk = 0x0, 318 UciStatusRejected = 0x1, 319 UciStatusFailed = 0x2, 320 UciStatusSyntaxError = 0x3, 321 UciStatusInvalidParam = 0x4, 322 UciStatusInvalidRange = 0x5, 323 UciStatusInvalidMsgSize = 0x6, 324 UciStatusUnknownGid = 0x7, 325 UciStatusUnknownOid = 0x8, 326 UciStatusReadOnly = 0x9, 327 UciStatusCommandRetry = 0xa, 328 UciStatusSessionNotExist = 0x11, 329 UciStatusSessionDuplicate = 0x12, 330 UciStatusSessionActive = 0x13, 331 UciStatusMaxSessionsExceeded = 0x14, 332 UciStatusSessionNotConfigured = 0x15, 333 UciStatusActiveSessionOngoing = 0x16, 334 UciStatusMulticastListFull = 0x17, 335 UciStatusAddressNotFound = 0x18, 336 UciStatusAddressAlreadyPresent = 0x19, 337 UciStatusRangingTxFailed = 0x20, 338 UciStatusRangingRxTimeout = 0x21, 339 UciStatusRangingRxPhyDecFailed = 0x22, 340 UciStatusRangingRxPhyToaFailed = 0x23, 341 UciStatusRangingRxPhyStsFailed = 0x24, 342 UciStatusRangingRxMacDecFailed = 0x25, 343 UciStatusRangingRxMacIeDecFailed = 0x26, 344 UciStatusRangingRxMacIeMissing = 0x27, 345 UciStatusDataMaxTxPsduSizeExceeded = 0x30, 346 UciStatusDataRxCrcError = 0x31, 347 UciStatusErrorCccSeBusy = 0x50, 348 UciStatusErrorCccLifecycle = 0x51, 349 } 350 impl fmt::Display for StatusCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result351 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 352 match self { 353 StatusCode::UciStatusOk => write!(f, "{:#04X} (UCI_STATUS_OK)", self.to_u8().unwrap()), 354 StatusCode::UciStatusRejected => { 355 write!(f, "{:#04X} (UCI_STATUS_REJECTED)", self.to_u8().unwrap()) 356 } 357 StatusCode::UciStatusFailed => { 358 write!(f, "{:#04X} (UCI_STATUS_FAILED)", self.to_u8().unwrap()) 359 } 360 StatusCode::UciStatusSyntaxError => write!( 361 f, 362 "{:#04X} (UCI_STATUS_SYNTAX_ERROR)", 363 self.to_u8().unwrap() 364 ), 365 StatusCode::UciStatusInvalidParam => write!( 366 f, 367 "{:#04X} (UCI_STATUS_INVALID_PARAM)", 368 self.to_u8().unwrap() 369 ), 370 StatusCode::UciStatusInvalidRange => write!( 371 f, 372 "{:#04X} (UCI_STATUS_INVALID_RANGE)", 373 self.to_u8().unwrap() 374 ), 375 StatusCode::UciStatusInvalidMsgSize => write!( 376 f, 377 "{:#04X} (UCI_STATUS_INVALID_MSG_SIZE)", 378 self.to_u8().unwrap() 379 ), 380 StatusCode::UciStatusUnknownGid => { 381 write!(f, "{:#04X} (UCI_STATUS_UNKNOWN_GID)", self.to_u8().unwrap()) 382 } 383 StatusCode::UciStatusUnknownOid => { 384 write!(f, "{:#04X} (UCI_STATUS_UNKNOWN_OID)", self.to_u8().unwrap()) 385 } 386 StatusCode::UciStatusReadOnly => { 387 write!(f, "{:#04X} (UCI_STATUS_READ_ONLY)", self.to_u8().unwrap()) 388 } 389 StatusCode::UciStatusCommandRetry => write!( 390 f, 391 "{:#04X} (UCI_STATUS_COMMAND_RETRY)", 392 self.to_u8().unwrap() 393 ), 394 StatusCode::UciStatusSessionNotExist => write!( 395 f, 396 "{:#04X} (UCI_STATUS_SESSION_NOT_EXIST)", 397 self.to_u8().unwrap() 398 ), 399 StatusCode::UciStatusSessionDuplicate => write!( 400 f, 401 "{:#04X} (UCI_STATUS_SESSION_DUPLICATE)", 402 self.to_u8().unwrap() 403 ), 404 StatusCode::UciStatusSessionActive => write!( 405 f, 406 "{:#04X} (UCI_STATUS_SESSION_ACTIVE)", 407 self.to_u8().unwrap() 408 ), 409 StatusCode::UciStatusMaxSessionsExceeded => write!( 410 f, 411 "{:#04X} (UCI_STATUS_MAX_SESSIONS_EXCEEDED)", 412 self.to_u8().unwrap() 413 ), 414 StatusCode::UciStatusSessionNotConfigured => write!( 415 f, 416 "{:#04X} (UCI_STATUS_SESSION_NOT_CONFIGURED)", 417 self.to_u8().unwrap() 418 ), 419 StatusCode::UciStatusActiveSessionOngoing => write!( 420 f, 421 "{:#04X} (UCI_STATUS_ACTIVE_SESSION_ONGOING)", 422 self.to_u8().unwrap() 423 ), 424 StatusCode::UciStatusMulticastListFull => write!( 425 f, 426 "{:#04X} (UCI_STATUS_MULTICAST_LIST_FULL)", 427 self.to_u8().unwrap() 428 ), 429 StatusCode::UciStatusAddressNotFound => write!( 430 f, 431 "{:#04X} (UCI_STATUS_ADDRESS_NOT_FOUND)", 432 self.to_u8().unwrap() 433 ), 434 StatusCode::UciStatusAddressAlreadyPresent => write!( 435 f, 436 "{:#04X} (UCI_STATUS_ADDRESS_ALREADY_PRESENT)", 437 self.to_u8().unwrap() 438 ), 439 StatusCode::UciStatusRangingTxFailed => write!( 440 f, 441 "{:#04X} (UCI_STATUS_RANGING_TX_FAILED)", 442 self.to_u8().unwrap() 443 ), 444 StatusCode::UciStatusRangingRxTimeout => write!( 445 f, 446 "{:#04X} (UCI_STATUS_RANGING_RX_TIMEOUT)", 447 self.to_u8().unwrap() 448 ), 449 StatusCode::UciStatusRangingRxPhyDecFailed => write!( 450 f, 451 "{:#04X} (UCI_STATUS_RANGING_RX_PHY_DEC_FAILED)", 452 self.to_u8().unwrap() 453 ), 454 StatusCode::UciStatusRangingRxPhyToaFailed => write!( 455 f, 456 "{:#04X} (UCI_STATUS_RANGING_RX_PHY_TOA_FAILED)", 457 self.to_u8().unwrap() 458 ), 459 StatusCode::UciStatusRangingRxPhyStsFailed => write!( 460 f, 461 "{:#04X} (UCI_STATUS_RANGING_RX_PHY_STS_FAILED)", 462 self.to_u8().unwrap() 463 ), 464 StatusCode::UciStatusRangingRxMacDecFailed => write!( 465 f, 466 "{:#04X} (UCI_STATUS_RANGING_RX_MAC_DEC_FAILED)", 467 self.to_u8().unwrap() 468 ), 469 StatusCode::UciStatusRangingRxMacIeDecFailed => write!( 470 f, 471 "{:#04X} (UCI_STATUS_RANGING_RX_MAC_IE_DEC_FAILED)", 472 self.to_u8().unwrap() 473 ), 474 StatusCode::UciStatusRangingRxMacIeMissing => write!( 475 f, 476 "{:#04X} (UCI_STATUS_RANGING_RX_MAC_IE_MISSING)", 477 self.to_u8().unwrap() 478 ), 479 StatusCode::UciStatusDataMaxTxPsduSizeExceeded => write!( 480 f, 481 "{:#04X} (UCI_STATUS_DATA_MAX_TX_PSDU_SIZE_EXCEEDED)", 482 self.to_u8().unwrap() 483 ), 484 StatusCode::UciStatusDataRxCrcError => write!( 485 f, 486 "{:#04X} (UCI_STATUS_DATA_RX_CRC_ERROR)", 487 self.to_u8().unwrap() 488 ), 489 StatusCode::UciStatusErrorCccSeBusy => write!( 490 f, 491 "{:#04X} (UCI_STATUS_ERROR_CCC_SE_BUSY)", 492 self.to_u8().unwrap() 493 ), 494 StatusCode::UciStatusErrorCccLifecycle => write!( 495 f, 496 "{:#04X} (UCI_STATUS_ERROR_CCC_LIFECYCLE)", 497 self.to_u8().unwrap() 498 ), 499 } 500 } 501 } 502 503 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 504 #[repr(u64)] 505 pub enum ResetConfig { 506 UwbsReset = 0x0, 507 } 508 impl fmt::Display for ResetConfig { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result509 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 510 match self { 511 ResetConfig::UwbsReset => write!(f, "{:#04X} (UWBS_RESET)", self.to_u8().unwrap()), 512 } 513 } 514 } 515 516 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 517 #[repr(u64)] 518 pub enum DeviceConfigId { 519 DeviceState = 0x0, 520 LowPowerMode = 0x1, 521 } 522 impl fmt::Display for DeviceConfigId { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result523 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 524 match self { 525 DeviceConfigId::DeviceState => { 526 write!(f, "{:#04X} (DEVICE_STATE)", self.to_u8().unwrap()) 527 } 528 DeviceConfigId::LowPowerMode => { 529 write!(f, "{:#04X} (LOW_POWER_MODE)", self.to_u8().unwrap()) 530 } 531 } 532 } 533 } 534 535 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 536 #[repr(u64)] 537 pub enum AppConfigTlvType { 538 DeviceType = 0x0, 539 RangingRoundUsage = 0x1, 540 StsConfig = 0x2, 541 MultiNodeMode = 0x3, 542 ChannelNumber = 0x4, 543 NoOfControlee = 0x5, 544 DeviceMacAddress = 0x6, 545 DstMacAddress = 0x7, 546 SlotDuration = 0x8, 547 RangingInterval = 0x9, 548 StsIndex = 0xa, 549 MacFcsType = 0xb, 550 RangingRoundControl = 0xc, 551 AoaResultReq = 0xd, 552 RngDataNtf = 0xe, 553 RngDataNtfProximityNear = 0xf, 554 RngDataNtfProximityFar = 0x10, 555 DeviceRole = 0x11, 556 RframeConfig = 0x12, 557 PreambleCodeIndex = 0x14, 558 SfdId = 0x15, 559 PsduDataRate = 0x16, 560 PreambleDuration = 0x17, 561 RangingTimeStruct = 0x1a, 562 SlotsPerRr = 0x1b, 563 TxAdaptivePayloadPower = 0x1c, 564 ResponderSlotIndex = 0x1e, 565 PrfMode = 0x1f, 566 ScheduledMode = 0x22, 567 KeyRotation = 0x23, 568 KeyRotationRate = 0x24, 569 SessionPriority = 0x25, 570 MacAddressMode = 0x26, 571 VendorId = 0x27, 572 StaticStsIv = 0x28, 573 NumberOfStsSegments = 0x29, 574 MaxRrRetry = 0x2a, 575 UwbInitiationTime = 0x2b, 576 HoppingMode = 0x2c, 577 BlockStrideLength = 0x2d, 578 ResultReportConfig = 0x2e, 579 InBandTerminationAttemptCount = 0x2f, 580 SubSessionId = 0x30, 581 BprfPhrDataRate = 0x31, 582 MaxNumberOfMeasurements = 0x32, 583 StsLength = 0x35, 584 CccHopModeKey = 0xa0, 585 CccUwbTime0 = 0xa1, 586 CccRangingProtocolVer = 0xa3, 587 CccUwbConfigId = 0xa4, 588 CccPulseshapeCombo = 0xa5, 589 CccUrskTtl = 0xa6, 590 NbOfRangeMeasurements = 0xe3, 591 NbOfAzimuthMeasurements = 0xe4, 592 NbOfElevationMeasurements = 0xe5, 593 } 594 impl fmt::Display for AppConfigTlvType { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result595 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 596 match self { 597 AppConfigTlvType::DeviceType => { 598 write!(f, "{:#04X} (DEVICE_TYPE)", self.to_u8().unwrap()) 599 } 600 AppConfigTlvType::RangingRoundUsage => { 601 write!(f, "{:#04X} (RANGING_ROUND_USAGE)", self.to_u8().unwrap()) 602 } 603 AppConfigTlvType::StsConfig => write!(f, "{:#04X} (STS_CONFIG)", self.to_u8().unwrap()), 604 AppConfigTlvType::MultiNodeMode => { 605 write!(f, "{:#04X} (MULTI_NODE_MODE)", self.to_u8().unwrap()) 606 } 607 AppConfigTlvType::ChannelNumber => { 608 write!(f, "{:#04X} (CHANNEL_NUMBER)", self.to_u8().unwrap()) 609 } 610 AppConfigTlvType::NoOfControlee => { 611 write!(f, "{:#04X} (NO_OF_CONTROLEE)", self.to_u8().unwrap()) 612 } 613 AppConfigTlvType::DeviceMacAddress => { 614 write!(f, "{:#04X} (DEVICE_MAC_ADDRESS)", self.to_u8().unwrap()) 615 } 616 AppConfigTlvType::DstMacAddress => { 617 write!(f, "{:#04X} (DST_MAC_ADDRESS)", self.to_u8().unwrap()) 618 } 619 AppConfigTlvType::SlotDuration => { 620 write!(f, "{:#04X} (SLOT_DURATION)", self.to_u8().unwrap()) 621 } 622 AppConfigTlvType::RangingInterval => { 623 write!(f, "{:#04X} (RANGING_INTERVAL)", self.to_u8().unwrap()) 624 } 625 AppConfigTlvType::StsIndex => write!(f, "{:#04X} (STS_INDEX)", self.to_u8().unwrap()), 626 AppConfigTlvType::MacFcsType => { 627 write!(f, "{:#04X} (MAC_FCS_TYPE)", self.to_u8().unwrap()) 628 } 629 AppConfigTlvType::RangingRoundControl => { 630 write!(f, "{:#04X} (RANGING_ROUND_CONTROL)", self.to_u8().unwrap()) 631 } 632 AppConfigTlvType::AoaResultReq => { 633 write!(f, "{:#04X} (AOA_RESULT_REQ)", self.to_u8().unwrap()) 634 } 635 AppConfigTlvType::RngDataNtf => { 636 write!(f, "{:#04X} (RNG_DATA_NTF)", self.to_u8().unwrap()) 637 } 638 AppConfigTlvType::RngDataNtfProximityNear => write!( 639 f, 640 "{:#04X} (RNG_DATA_NTF_PROXIMITY_NEAR)", 641 self.to_u8().unwrap() 642 ), 643 AppConfigTlvType::RngDataNtfProximityFar => write!( 644 f, 645 "{:#04X} (RNG_DATA_NTF_PROXIMITY_FAR)", 646 self.to_u8().unwrap() 647 ), 648 AppConfigTlvType::DeviceRole => { 649 write!(f, "{:#04X} (DEVICE_ROLE)", self.to_u8().unwrap()) 650 } 651 AppConfigTlvType::RframeConfig => { 652 write!(f, "{:#04X} (RFRAME_CONFIG)", self.to_u8().unwrap()) 653 } 654 AppConfigTlvType::PreambleCodeIndex => { 655 write!(f, "{:#04X} (PREAMBLE_CODE_INDEX)", self.to_u8().unwrap()) 656 } 657 AppConfigTlvType::SfdId => write!(f, "{:#04X} (SFD_ID)", self.to_u8().unwrap()), 658 AppConfigTlvType::PsduDataRate => { 659 write!(f, "{:#04X} (PSDU_DATA_RATE)", self.to_u8().unwrap()) 660 } 661 AppConfigTlvType::PreambleDuration => { 662 write!(f, "{:#04X} (PREAMBLE_DURATION)", self.to_u8().unwrap()) 663 } 664 AppConfigTlvType::RangingTimeStruct => { 665 write!(f, "{:#04X} (RANGING_TIME_STRUCT)", self.to_u8().unwrap()) 666 } 667 AppConfigTlvType::SlotsPerRr => { 668 write!(f, "{:#04X} (SLOTS_PER_RR)", self.to_u8().unwrap()) 669 } 670 AppConfigTlvType::TxAdaptivePayloadPower => write!( 671 f, 672 "{:#04X} (TX_ADAPTIVE_PAYLOAD_POWER)", 673 self.to_u8().unwrap() 674 ), 675 AppConfigTlvType::ResponderSlotIndex => { 676 write!(f, "{:#04X} (RESPONDER_SLOT_INDEX)", self.to_u8().unwrap()) 677 } 678 AppConfigTlvType::PrfMode => write!(f, "{:#04X} (PRF_MODE)", self.to_u8().unwrap()), 679 AppConfigTlvType::ScheduledMode => { 680 write!(f, "{:#04X} (SCHEDULED_MODE)", self.to_u8().unwrap()) 681 } 682 AppConfigTlvType::KeyRotation => { 683 write!(f, "{:#04X} (KEY_ROTATION)", self.to_u8().unwrap()) 684 } 685 AppConfigTlvType::KeyRotationRate => { 686 write!(f, "{:#04X} (KEY_ROTATION_RATE)", self.to_u8().unwrap()) 687 } 688 AppConfigTlvType::SessionPriority => { 689 write!(f, "{:#04X} (SESSION_PRIORITY)", self.to_u8().unwrap()) 690 } 691 AppConfigTlvType::MacAddressMode => { 692 write!(f, "{:#04X} (MAC_ADDRESS_MODE)", self.to_u8().unwrap()) 693 } 694 AppConfigTlvType::VendorId => write!(f, "{:#04X} (VENDOR_ID)", self.to_u8().unwrap()), 695 AppConfigTlvType::StaticStsIv => { 696 write!(f, "{:#04X} (STATIC_STS_IV)", self.to_u8().unwrap()) 697 } 698 AppConfigTlvType::NumberOfStsSegments => { 699 write!(f, "{:#04X} (NUMBER_OF_STS_SEGMENTS)", self.to_u8().unwrap()) 700 } 701 AppConfigTlvType::MaxRrRetry => { 702 write!(f, "{:#04X} (MAX_RR_RETRY)", self.to_u8().unwrap()) 703 } 704 AppConfigTlvType::UwbInitiationTime => { 705 write!(f, "{:#04X} (UWB_INITIATION_TIME)", self.to_u8().unwrap()) 706 } 707 AppConfigTlvType::HoppingMode => { 708 write!(f, "{:#04X} (HOPPING_MODE)", self.to_u8().unwrap()) 709 } 710 AppConfigTlvType::BlockStrideLength => { 711 write!(f, "{:#04X} (BLOCK_STRIDE_LENGTH)", self.to_u8().unwrap()) 712 } 713 AppConfigTlvType::ResultReportConfig => { 714 write!(f, "{:#04X} (RESULT_REPORT_CONFIG)", self.to_u8().unwrap()) 715 } 716 AppConfigTlvType::InBandTerminationAttemptCount => write!( 717 f, 718 "{:#04X} (IN_BAND_TERMINATION_ATTEMPT_COUNT)", 719 self.to_u8().unwrap() 720 ), 721 AppConfigTlvType::SubSessionId => { 722 write!(f, "{:#04X} (SUB_SESSION_ID)", self.to_u8().unwrap()) 723 } 724 AppConfigTlvType::BprfPhrDataRate => { 725 write!(f, "{:#04X} (BPRF_PHR_DATA_RATE)", self.to_u8().unwrap()) 726 } 727 AppConfigTlvType::MaxNumberOfMeasurements => write!( 728 f, 729 "{:#04X} (MAX_NUMBER_OF_MEASUREMENTS)", 730 self.to_u8().unwrap() 731 ), 732 AppConfigTlvType::StsLength => write!(f, "{:#04X} (STS_LENGTH)", self.to_u8().unwrap()), 733 AppConfigTlvType::CccHopModeKey => { 734 write!(f, "{:#04X} (CCC_HOP_MODE_KEY)", self.to_u8().unwrap()) 735 } 736 AppConfigTlvType::CccUwbTime0 => { 737 write!(f, "{:#04X} (CCC_UWB_TIME0)", self.to_u8().unwrap()) 738 } 739 AppConfigTlvType::CccRangingProtocolVer => write!( 740 f, 741 "{:#04X} (CCC_RANGING_PROTOCOL_VER)", 742 self.to_u8().unwrap() 743 ), 744 AppConfigTlvType::CccUwbConfigId => { 745 write!(f, "{:#04X} (CCC_UWB_CONFIG_ID)", self.to_u8().unwrap()) 746 } 747 AppConfigTlvType::CccPulseshapeCombo => { 748 write!(f, "{:#04X} (CCC_PULSESHAPE_COMBO)", self.to_u8().unwrap()) 749 } 750 AppConfigTlvType::CccUrskTtl => { 751 write!(f, "{:#04X} (CCC_URSK_TTL)", self.to_u8().unwrap()) 752 } 753 AppConfigTlvType::NbOfRangeMeasurements => write!( 754 f, 755 "{:#04X} (NB_OF_RANGE_MEASUREMENTS)", 756 self.to_u8().unwrap() 757 ), 758 AppConfigTlvType::NbOfAzimuthMeasurements => write!( 759 f, 760 "{:#04X} (NB_OF_AZIMUTH_MEASUREMENTS)", 761 self.to_u8().unwrap() 762 ), 763 AppConfigTlvType::NbOfElevationMeasurements => write!( 764 f, 765 "{:#04X} (NB_OF_ELEVATION_MEASUREMENTS)", 766 self.to_u8().unwrap() 767 ), 768 } 769 } 770 } 771 772 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 773 #[repr(u64)] 774 pub enum CapTlvType { 775 SupportedFiraPhyVersionRange = 0x0, 776 SupportedFiraMacVersionRange = 0x1, 777 SupportedDeviceRoles = 0x2, 778 SupportedRangingMethod = 0x3, 779 SupportedStsConfig = 0x4, 780 SupportedMultiNodeModes = 0x5, 781 SupportedRangingTimeStruct = 0x6, 782 SupportedScheduledMode = 0x7, 783 SupportedHoppingMode = 0x8, 784 SupportedBlockStriding = 0x9, 785 SupportedUwbInitiationTime = 0xa, 786 SupportedChannels = 0xb, 787 SupportedRframeConfig = 0xc, 788 SupportedCcConstraintLength = 0xd, 789 SupportedBprfParameterSets = 0xe, 790 SupportedHprfParameterSets = 0xf, 791 SupportedAoa = 0x10, 792 SupportedExtendedMacAddress = 0x11, 793 CccSupportedChapsPerSlot = 0xa0, 794 CccSupportedSyncCodes = 0xa1, 795 CccSupportedHoppingConfigModesAndSequences = 0xa2, 796 CccSupportedChannels = 0xa3, 797 CccSupportedVersions = 0xa4, 798 CccSupportedUwbConfigs = 0xa5, 799 CccSupportedPulseShapeCombos = 0xa6, 800 CccSupportedRanMultiplier = 0xa7, 801 SupportedPowerStats = 0xc0, 802 SupportedAoaResultReqAntennaInterleaving = 0xe3, 803 } 804 impl fmt::Display for CapTlvType { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result805 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 806 match self { 807 CapTlvType::SupportedFiraPhyVersionRange => write!( 808 f, 809 "{:#04X} (SUPPORTED_FIRA_PHY_VERSION_RANGE)", 810 self.to_u8().unwrap() 811 ), 812 CapTlvType::SupportedFiraMacVersionRange => write!( 813 f, 814 "{:#04X} (SUPPORTED_FIRA_MAC_VERSION_RANGE)", 815 self.to_u8().unwrap() 816 ), 817 CapTlvType::SupportedDeviceRoles => { 818 write!(f, "{:#04X} (SUPPORTED_DEVICE_ROLES)", self.to_u8().unwrap()) 819 } 820 CapTlvType::SupportedRangingMethod => write!( 821 f, 822 "{:#04X} (SUPPORTED_RANGING_METHOD)", 823 self.to_u8().unwrap() 824 ), 825 CapTlvType::SupportedStsConfig => { 826 write!(f, "{:#04X} (SUPPORTED_STS_CONFIG)", self.to_u8().unwrap()) 827 } 828 CapTlvType::SupportedMultiNodeModes => write!( 829 f, 830 "{:#04X} (SUPPORTED_MULTI_NODE_MODES)", 831 self.to_u8().unwrap() 832 ), 833 CapTlvType::SupportedRangingTimeStruct => write!( 834 f, 835 "{:#04X} (SUPPORTED_RANGING_TIME_STRUCT)", 836 self.to_u8().unwrap() 837 ), 838 CapTlvType::SupportedScheduledMode => write!( 839 f, 840 "{:#04X} (SUPPORTED_SCHEDULED_MODE)", 841 self.to_u8().unwrap() 842 ), 843 CapTlvType::SupportedHoppingMode => { 844 write!(f, "{:#04X} (SUPPORTED_HOPPING_MODE)", self.to_u8().unwrap()) 845 } 846 CapTlvType::SupportedBlockStriding => write!( 847 f, 848 "{:#04X} (SUPPORTED_BLOCK_STRIDING)", 849 self.to_u8().unwrap() 850 ), 851 CapTlvType::SupportedUwbInitiationTime => write!( 852 f, 853 "{:#04X} (SUPPORTED_UWB_INITIATION_TIME)", 854 self.to_u8().unwrap() 855 ), 856 CapTlvType::SupportedChannels => { 857 write!(f, "{:#04X} (SUPPORTED_CHANNELS)", self.to_u8().unwrap()) 858 } 859 CapTlvType::SupportedRframeConfig => write!( 860 f, 861 "{:#04X} (SUPPORTED_RFRAME_CONFIG)", 862 self.to_u8().unwrap() 863 ), 864 CapTlvType::SupportedCcConstraintLength => write!( 865 f, 866 "{:#04X} (SUPPORTED_CC_CONSTRAINT_LENGTH)", 867 self.to_u8().unwrap() 868 ), 869 CapTlvType::SupportedBprfParameterSets => write!( 870 f, 871 "{:#04X} (SUPPORTED_BPRF_PARAMETER_SETS)", 872 self.to_u8().unwrap() 873 ), 874 CapTlvType::SupportedHprfParameterSets => write!( 875 f, 876 "{:#04X} (SUPPORTED_HPRF_PARAMETER_SETS)", 877 self.to_u8().unwrap() 878 ), 879 CapTlvType::SupportedAoa => write!(f, "{:#04X} (SUPPORTED_AOA)", self.to_u8().unwrap()), 880 CapTlvType::SupportedExtendedMacAddress => write!( 881 f, 882 "{:#04X} (SUPPORTED_EXTENDED_MAC_ADDRESS)", 883 self.to_u8().unwrap() 884 ), 885 CapTlvType::CccSupportedChapsPerSlot => write!( 886 f, 887 "{:#04X} (CCC_SUPPORTED_CHAPS_PER_SLOT)", 888 self.to_u8().unwrap() 889 ), 890 CapTlvType::CccSupportedSyncCodes => write!( 891 f, 892 "{:#04X} (CCC_SUPPORTED_SYNC_CODES)", 893 self.to_u8().unwrap() 894 ), 895 CapTlvType::CccSupportedHoppingConfigModesAndSequences => write!( 896 f, 897 "{:#04X} (CCC_SUPPORTED_HOPPING_CONFIG_MODES_AND_SEQUENCES)", 898 self.to_u8().unwrap() 899 ), 900 CapTlvType::CccSupportedChannels => { 901 write!(f, "{:#04X} (CCC_SUPPORTED_CHANNELS)", self.to_u8().unwrap()) 902 } 903 CapTlvType::CccSupportedVersions => { 904 write!(f, "{:#04X} (CCC_SUPPORTED_VERSIONS)", self.to_u8().unwrap()) 905 } 906 CapTlvType::CccSupportedUwbConfigs => write!( 907 f, 908 "{:#04X} (CCC_SUPPORTED_UWB_CONFIGS)", 909 self.to_u8().unwrap() 910 ), 911 CapTlvType::CccSupportedPulseShapeCombos => write!( 912 f, 913 "{:#04X} (CCC_SUPPORTED_PULSE_SHAPE_COMBOS)", 914 self.to_u8().unwrap() 915 ), 916 CapTlvType::CccSupportedRanMultiplier => write!( 917 f, 918 "{:#04X} (CCC_SUPPORTED_RAN_MULTIPLIER)", 919 self.to_u8().unwrap() 920 ), 921 CapTlvType::SupportedPowerStats => { 922 write!(f, "{:#04X} (SUPPORTED_POWER_STATS)", self.to_u8().unwrap()) 923 } 924 CapTlvType::SupportedAoaResultReqAntennaInterleaving => write!( 925 f, 926 "{:#04X} (SUPPORTED_AOA_RESULT_REQ_ANTENNA_INTERLEAVING)", 927 self.to_u8().unwrap() 928 ), 929 } 930 } 931 } 932 933 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 934 #[repr(u64)] 935 pub enum AoaResultReqType { 936 AoaDisable = 0x0, 937 AoaEnable = 0x1, 938 AoaEnableAzimuth = 0x2, 939 AoaEnableElevation = 0x3, 940 AoaEnableInterleaved = 0xf0, 941 } 942 impl fmt::Display for AoaResultReqType { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result943 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 944 match self { 945 AoaResultReqType::AoaDisable => { 946 write!(f, "{:#04X} (AOA_DISABLE)", self.to_u8().unwrap()) 947 } 948 AoaResultReqType::AoaEnable => write!(f, "{:#04X} (AOA_ENABLE)", self.to_u8().unwrap()), 949 AoaResultReqType::AoaEnableAzimuth => { 950 write!(f, "{:#04X} (AOA_ENABLE_AZIMUTH)", self.to_u8().unwrap()) 951 } 952 AoaResultReqType::AoaEnableElevation => { 953 write!(f, "{:#04X} (AOA_ENABLE_ELEVATION)", self.to_u8().unwrap()) 954 } 955 AoaResultReqType::AoaEnableInterleaved => { 956 write!(f, "{:#04X} (AOA_ENABLE_INTERLEAVED)", self.to_u8().unwrap()) 957 } 958 } 959 } 960 } 961 962 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 963 #[repr(u64)] 964 pub enum DeviceState { 965 DeviceStateReady = 0x1, 966 DeviceStateActive = 0x2, 967 DeviceStateError = 0xff, 968 } 969 impl fmt::Display for DeviceState { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result970 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 971 match self { 972 DeviceState::DeviceStateReady => { 973 write!(f, "{:#04X} (DEVICE_STATE_READY)", self.to_u8().unwrap()) 974 } 975 DeviceState::DeviceStateActive => { 976 write!(f, "{:#04X} (DEVICE_STATE_ACTIVE)", self.to_u8().unwrap()) 977 } 978 DeviceState::DeviceStateError => { 979 write!(f, "{:#04X} (DEVICE_STATE_ERROR)", self.to_u8().unwrap()) 980 } 981 } 982 } 983 } 984 985 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 986 #[repr(u64)] 987 pub enum SessionState { 988 SessionStateInit = 0x0, 989 SessionStateDeinit = 0x1, 990 SessionStateActive = 0x2, 991 SessionStateIdle = 0x3, 992 } 993 impl fmt::Display for SessionState { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result994 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 995 match self { 996 SessionState::SessionStateInit => { 997 write!(f, "{:#04X} (SESSION_STATE_INIT)", self.to_u8().unwrap()) 998 } 999 SessionState::SessionStateDeinit => { 1000 write!(f, "{:#04X} (SESSION_STATE_DEINIT)", self.to_u8().unwrap()) 1001 } 1002 SessionState::SessionStateActive => { 1003 write!(f, "{:#04X} (SESSION_STATE_ACTIVE)", self.to_u8().unwrap()) 1004 } 1005 SessionState::SessionStateIdle => { 1006 write!(f, "{:#04X} (SESSION_STATE_IDLE)", self.to_u8().unwrap()) 1007 } 1008 } 1009 } 1010 } 1011 1012 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 1013 #[repr(u64)] 1014 pub enum ReasonCode { 1015 StateChangeWithSessionManagementCommands = 0x0, 1016 MaxRangingRoundRetryCountReached = 0x1, 1017 MaxNumberOfMeasurementsReached = 0x2, 1018 ErrorSlotLengthNotSupported = 0x20, 1019 ErrorInsufficientSlotsPerRr = 0x21, 1020 ErrorMacAddressModeNotSupported = 0x22, 1021 ErrorInvalidRangingInterval = 0x23, 1022 ErrorInvalidStsConfig = 0x24, 1023 ErrorInvalidRframeConfig = 0x25, 1024 } 1025 impl fmt::Display for ReasonCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1026 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 1027 match self { 1028 ReasonCode::StateChangeWithSessionManagementCommands => write!( 1029 f, 1030 "{:#04X} (STATE_CHANGE_WITH_SESSION_MANAGEMENT_COMMANDS)", 1031 self.to_u8().unwrap() 1032 ), 1033 ReasonCode::MaxRangingRoundRetryCountReached => write!( 1034 f, 1035 "{:#04X} (MAX_RANGING_ROUND_RETRY_COUNT_REACHED)", 1036 self.to_u8().unwrap() 1037 ), 1038 ReasonCode::MaxNumberOfMeasurementsReached => write!( 1039 f, 1040 "{:#04X} (MAX_NUMBER_OF_MEASUREMENTS_REACHED)", 1041 self.to_u8().unwrap() 1042 ), 1043 ReasonCode::ErrorSlotLengthNotSupported => write!( 1044 f, 1045 "{:#04X} (ERROR_SLOT_LENGTH_NOT_SUPPORTED)", 1046 self.to_u8().unwrap() 1047 ), 1048 ReasonCode::ErrorInsufficientSlotsPerRr => write!( 1049 f, 1050 "{:#04X} (ERROR_INSUFFICIENT_SLOTS_PER_RR)", 1051 self.to_u8().unwrap() 1052 ), 1053 ReasonCode::ErrorMacAddressModeNotSupported => write!( 1054 f, 1055 "{:#04X} (ERROR_MAC_ADDRESS_MODE_NOT_SUPPORTED)", 1056 self.to_u8().unwrap() 1057 ), 1058 ReasonCode::ErrorInvalidRangingInterval => write!( 1059 f, 1060 "{:#04X} (ERROR_INVALID_RANGING_INTERVAL)", 1061 self.to_u8().unwrap() 1062 ), 1063 ReasonCode::ErrorInvalidStsConfig => write!( 1064 f, 1065 "{:#04X} (ERROR_INVALID_STS_CONFIG)", 1066 self.to_u8().unwrap() 1067 ), 1068 ReasonCode::ErrorInvalidRframeConfig => write!( 1069 f, 1070 "{:#04X} (ERROR_INVALID_RFRAME_CONFIG)", 1071 self.to_u8().unwrap() 1072 ), 1073 } 1074 } 1075 } 1076 1077 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 1078 #[repr(u64)] 1079 pub enum MulticastUpdateStatusCode { 1080 StatusOkMulticastListUpdate = 0x0, 1081 StatusErrorMulticastListFull = 0x1, 1082 StatusErrorKeyFetchFail = 0x2, 1083 StatusErrorSubSessionIdNotFound = 0x3, 1084 } 1085 impl fmt::Display for MulticastUpdateStatusCode { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1086 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 1087 match self { 1088 MulticastUpdateStatusCode::StatusOkMulticastListUpdate => write!( 1089 f, 1090 "{:#04X} (STATUS_OK_MULTICAST_LIST_UPDATE)", 1091 self.to_u8().unwrap() 1092 ), 1093 MulticastUpdateStatusCode::StatusErrorMulticastListFull => write!( 1094 f, 1095 "{:#04X} (STATUS_ERROR_MULTICAST_LIST_FULL)", 1096 self.to_u8().unwrap() 1097 ), 1098 MulticastUpdateStatusCode::StatusErrorKeyFetchFail => write!( 1099 f, 1100 "{:#04X} (STATUS_ERROR_KEY_FETCH_FAIL)", 1101 self.to_u8().unwrap() 1102 ), 1103 MulticastUpdateStatusCode::StatusErrorSubSessionIdNotFound => write!( 1104 f, 1105 "{:#04X} (STATUS_ERROR_SUB_SESSION_ID_NOT_FOUND)", 1106 self.to_u8().unwrap() 1107 ), 1108 } 1109 } 1110 } 1111 1112 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 1113 #[repr(u64)] 1114 pub enum MacAddressIndicator { 1115 ShortAddress = 0x0, 1116 ExtendedAddress = 0x1, 1117 } 1118 impl fmt::Display for MacAddressIndicator { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 1120 match self { 1121 MacAddressIndicator::ShortAddress => { 1122 write!(f, "{:#04X} (SHORT_ADDRESS)", self.to_u8().unwrap()) 1123 } 1124 MacAddressIndicator::ExtendedAddress => { 1125 write!(f, "{:#04X} (EXTENDED_ADDRESS)", self.to_u8().unwrap()) 1126 } 1127 } 1128 } 1129 } 1130 1131 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 1132 #[repr(u64)] 1133 pub enum SessionType { 1134 FiraRangingSession = 0x0, 1135 FiraDataTransfer = 0x1, 1136 Ccc = 0xa0, 1137 } 1138 impl fmt::Display for SessionType { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1139 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 1140 match self { 1141 SessionType::FiraRangingSession => { 1142 write!(f, "{:#04X} (FIRA_RANGING_SESSION)", self.to_u8().unwrap()) 1143 } 1144 SessionType::FiraDataTransfer => { 1145 write!(f, "{:#04X} (FIRA_DATA_TRANSFER)", self.to_u8().unwrap()) 1146 } 1147 SessionType::Ccc => write!(f, "{:#04X} (CCC)", self.to_u8().unwrap()), 1148 } 1149 } 1150 } 1151 1152 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 1153 #[repr(u64)] 1154 pub enum MessageType { 1155 Command = 0x1, 1156 Response = 0x2, 1157 Notification = 0x3, 1158 } 1159 impl fmt::Display for MessageType { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 1161 match self { 1162 MessageType::Command => write!(f, "{:#04X} (COMMAND)", self.to_u8().unwrap()), 1163 MessageType::Response => write!(f, "{:#04X} (RESPONSE)", self.to_u8().unwrap()), 1164 MessageType::Notification => write!(f, "{:#04X} (NOTIFICATION)", self.to_u8().unwrap()), 1165 } 1166 } 1167 } 1168 1169 #[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)] 1170 #[repr(u64)] 1171 pub enum RangingMeasurementType { 1172 OneWay = 0x0, 1173 TwoWay = 0x1, 1174 } 1175 impl fmt::Display for RangingMeasurementType { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 1177 match self { 1178 RangingMeasurementType::OneWay => write!(f, "{:#04X} (ONE_WAY)", self.to_u8().unwrap()), 1179 RangingMeasurementType::TwoWay => write!(f, "{:#04X} (TWO_WAY)", self.to_u8().unwrap()), 1180 } 1181 } 1182 } 1183 1184 #[derive(Debug, Clone, PartialEq)] 1185 pub struct CapTlv { 1186 pub t: CapTlvType, 1187 pub v: Vec<u8>, 1188 } 1189 impl CapTlv { conforms(bytes: &[u8]) -> bool1190 fn conforms(bytes: &[u8]) -> bool { 1191 if bytes.len() < 2 { 1192 return false; 1193 } 1194 true 1195 } parse(bytes: &[u8]) -> Result<Self>1196 pub fn parse(bytes: &[u8]) -> Result<Self> { 1197 if bytes.len() < 1 { 1198 return Err(Error::InvalidLengthError { 1199 obj: "CapTlv".to_string(), 1200 field: "t".to_string(), 1201 wanted: 1, 1202 got: bytes.len(), 1203 }); 1204 } 1205 let t = u8::from_le_bytes([bytes[0]]); 1206 let t = CapTlvType::from_u8(t).ok_or_else(|| Error::InvalidEnumValueError { 1207 obj: "CapTlv".to_string(), 1208 field: "t".to_string(), 1209 value: t as u64, 1210 type_: "CapTlvType".to_string(), 1211 })?; 1212 if bytes.len() < 2 { 1213 return Err(Error::InvalidLengthError { 1214 obj: "CapTlv".to_string(), 1215 field: "v_count".to_string(), 1216 wanted: 2, 1217 got: bytes.len(), 1218 }); 1219 } 1220 let v_count = u8::from_le_bytes([bytes[1]]); 1221 let want_ = 2 + ((v_count as usize) * 1); 1222 if bytes.len() < want_ { 1223 return Err(Error::InvalidLengthError { 1224 obj: "CapTlv".to_string(), 1225 field: "v".to_string(), 1226 wanted: want_, 1227 got: bytes.len(), 1228 }); 1229 } 1230 let v: Vec<u8> = bytes[2..2 + ((v_count as usize) * 1)] 1231 .to_vec() 1232 .chunks_exact(1) 1233 .into_iter() 1234 .map(|i| u8::from_le_bytes([i[0]])) 1235 .collect(); 1236 Ok(Self { t, v }) 1237 } write_to(&self, buffer: &mut [u8])1238 fn write_to(&self, buffer: &mut [u8]) { 1239 let t = self.t.to_u8().unwrap(); 1240 buffer[0..1].copy_from_slice(&t.to_le_bytes()[0..1]); 1241 buffer[1..2].copy_from_slice(&(self.v.len() as u8).to_le_bytes()); 1242 for (i, e) in self.v.iter().enumerate() { 1243 buffer[2 + i..2 + i + 1].copy_from_slice(&e.to_le_bytes()) 1244 } 1245 } get_total_size(&self) -> usize1246 fn get_total_size(&self) -> usize { 1247 let ret = 0; 1248 let ret = ret + 2; 1249 let ret = ret + (self.v.len() * ((/* Bits: */8 + /* Dynamic: */ 0) / 8)); 1250 ret 1251 } 1252 } 1253 1254 #[derive(Debug, Clone, PartialEq)] 1255 pub struct DeviceParameter { 1256 pub id: u8, 1257 pub value: Vec<u8>, 1258 } 1259 impl DeviceParameter { conforms(bytes: &[u8]) -> bool1260 fn conforms(bytes: &[u8]) -> bool { 1261 if bytes.len() < 2 { 1262 return false; 1263 } 1264 true 1265 } parse(bytes: &[u8]) -> Result<Self>1266 pub fn parse(bytes: &[u8]) -> Result<Self> { 1267 if bytes.len() < 1 { 1268 return Err(Error::InvalidLengthError { 1269 obj: "DeviceParameter".to_string(), 1270 field: "id".to_string(), 1271 wanted: 1, 1272 got: bytes.len(), 1273 }); 1274 } 1275 let id = u8::from_le_bytes([bytes[0]]); 1276 if bytes.len() < 2 { 1277 return Err(Error::InvalidLengthError { 1278 obj: "DeviceParameter".to_string(), 1279 field: "value_count".to_string(), 1280 wanted: 2, 1281 got: bytes.len(), 1282 }); 1283 } 1284 let value_count = u8::from_le_bytes([bytes[1]]); 1285 let want_ = 2 + ((value_count as usize) * 1); 1286 if bytes.len() < want_ { 1287 return Err(Error::InvalidLengthError { 1288 obj: "DeviceParameter".to_string(), 1289 field: "value".to_string(), 1290 wanted: want_, 1291 got: bytes.len(), 1292 }); 1293 } 1294 let value: Vec<u8> = bytes[2..2 + ((value_count as usize) * 1)] 1295 .to_vec() 1296 .chunks_exact(1) 1297 .into_iter() 1298 .map(|i| u8::from_le_bytes([i[0]])) 1299 .collect(); 1300 Ok(Self { id, value }) 1301 } write_to(&self, buffer: &mut [u8])1302 fn write_to(&self, buffer: &mut [u8]) { 1303 let id = self.id; 1304 buffer[0..1].copy_from_slice(&id.to_le_bytes()[0..1]); 1305 buffer[1..2].copy_from_slice(&(self.value.len() as u8).to_le_bytes()); 1306 for (i, e) in self.value.iter().enumerate() { 1307 buffer[2 + i..2 + i + 1].copy_from_slice(&e.to_le_bytes()) 1308 } 1309 } get_total_size(&self) -> usize1310 fn get_total_size(&self) -> usize { 1311 let ret = 0; 1312 let ret = ret + 2; 1313 let ret = ret + (self.value.len() * ((/* Bits: */8 + /* Dynamic: */ 0) / 8)); 1314 ret 1315 } 1316 } 1317 1318 #[derive(Debug, Clone, PartialEq)] 1319 pub struct DeviceConfigStatus { 1320 pub parameter_id: u8, 1321 pub status: StatusCode, 1322 } 1323 impl DeviceConfigStatus { conforms(bytes: &[u8]) -> bool1324 fn conforms(bytes: &[u8]) -> bool { 1325 if bytes.len() < 2 { 1326 return false; 1327 } 1328 true 1329 } parse(bytes: &[u8]) -> Result<Self>1330 pub fn parse(bytes: &[u8]) -> Result<Self> { 1331 if bytes.len() < 1 { 1332 return Err(Error::InvalidLengthError { 1333 obj: "DeviceConfigStatus".to_string(), 1334 field: "parameter_id".to_string(), 1335 wanted: 1, 1336 got: bytes.len(), 1337 }); 1338 } 1339 let parameter_id = u8::from_le_bytes([bytes[0]]); 1340 if bytes.len() < 2 { 1341 return Err(Error::InvalidLengthError { 1342 obj: "DeviceConfigStatus".to_string(), 1343 field: "status".to_string(), 1344 wanted: 2, 1345 got: bytes.len(), 1346 }); 1347 } 1348 let status = u8::from_le_bytes([bytes[1]]); 1349 let status = StatusCode::from_u8(status).ok_or_else(|| Error::InvalidEnumValueError { 1350 obj: "DeviceConfigStatus".to_string(), 1351 field: "status".to_string(), 1352 value: status as u64, 1353 type_: "StatusCode".to_string(), 1354 })?; 1355 Ok(Self { 1356 parameter_id, 1357 status, 1358 }) 1359 } write_to(&self, buffer: &mut [u8])1360 fn write_to(&self, buffer: &mut [u8]) { 1361 let parameter_id = self.parameter_id; 1362 buffer[0..1].copy_from_slice(¶meter_id.to_le_bytes()[0..1]); 1363 let status = self.status.to_u8().unwrap(); 1364 buffer[1..2].copy_from_slice(&status.to_le_bytes()[0..1]); 1365 } get_total_size(&self) -> usize1366 fn get_total_size(&self) -> usize { 1367 let ret = 0; 1368 let ret = ret + 2; 1369 ret 1370 } 1371 } 1372 1373 #[derive(Debug, Clone, PartialEq)] 1374 pub struct AppConfigParameter { 1375 pub id: u8, 1376 pub value: Vec<u8>, 1377 } 1378 impl AppConfigParameter { conforms(bytes: &[u8]) -> bool1379 fn conforms(bytes: &[u8]) -> bool { 1380 if bytes.len() < 2 { 1381 return false; 1382 } 1383 true 1384 } parse(bytes: &[u8]) -> Result<Self>1385 pub fn parse(bytes: &[u8]) -> Result<Self> { 1386 if bytes.len() < 1 { 1387 return Err(Error::InvalidLengthError { 1388 obj: "AppConfigParameter".to_string(), 1389 field: "id".to_string(), 1390 wanted: 1, 1391 got: bytes.len(), 1392 }); 1393 } 1394 let id = u8::from_le_bytes([bytes[0]]); 1395 if bytes.len() < 2 { 1396 return Err(Error::InvalidLengthError { 1397 obj: "AppConfigParameter".to_string(), 1398 field: "value_count".to_string(), 1399 wanted: 2, 1400 got: bytes.len(), 1401 }); 1402 } 1403 let value_count = u8::from_le_bytes([bytes[1]]); 1404 let want_ = 2 + ((value_count as usize) * 1); 1405 if bytes.len() < want_ { 1406 return Err(Error::InvalidLengthError { 1407 obj: "AppConfigParameter".to_string(), 1408 field: "value".to_string(), 1409 wanted: want_, 1410 got: bytes.len(), 1411 }); 1412 } 1413 let value: Vec<u8> = bytes[2..2 + ((value_count as usize) * 1)] 1414 .to_vec() 1415 .chunks_exact(1) 1416 .into_iter() 1417 .map(|i| u8::from_le_bytes([i[0]])) 1418 .collect(); 1419 Ok(Self { id, value }) 1420 } write_to(&self, buffer: &mut [u8])1421 fn write_to(&self, buffer: &mut [u8]) { 1422 let id = self.id; 1423 buffer[0..1].copy_from_slice(&id.to_le_bytes()[0..1]); 1424 buffer[1..2].copy_from_slice(&(self.value.len() as u8).to_le_bytes()); 1425 for (i, e) in self.value.iter().enumerate() { 1426 buffer[2 + i..2 + i + 1].copy_from_slice(&e.to_le_bytes()) 1427 } 1428 } get_total_size(&self) -> usize1429 fn get_total_size(&self) -> usize { 1430 let ret = 0; 1431 let ret = ret + 2; 1432 let ret = ret + (self.value.len() * ((/* Bits: */8 + /* Dynamic: */ 0) / 8)); 1433 ret 1434 } 1435 } 1436 1437 #[derive(Debug, Clone, PartialEq)] 1438 pub struct AppConfigStatus { 1439 pub config_id: u8, 1440 pub status: StatusCode, 1441 } 1442 impl AppConfigStatus { conforms(bytes: &[u8]) -> bool1443 fn conforms(bytes: &[u8]) -> bool { 1444 if bytes.len() < 2 { 1445 return false; 1446 } 1447 true 1448 } parse(bytes: &[u8]) -> Result<Self>1449 pub fn parse(bytes: &[u8]) -> Result<Self> { 1450 if bytes.len() < 1 { 1451 return Err(Error::InvalidLengthError { 1452 obj: "AppConfigStatus".to_string(), 1453 field: "config_id".to_string(), 1454 wanted: 1, 1455 got: bytes.len(), 1456 }); 1457 } 1458 let config_id = u8::from_le_bytes([bytes[0]]); 1459 if bytes.len() < 2 { 1460 return Err(Error::InvalidLengthError { 1461 obj: "AppConfigStatus".to_string(), 1462 field: "status".to_string(), 1463 wanted: 2, 1464 got: bytes.len(), 1465 }); 1466 } 1467 let status = u8::from_le_bytes([bytes[1]]); 1468 let status = StatusCode::from_u8(status).ok_or_else(|| Error::InvalidEnumValueError { 1469 obj: "AppConfigStatus".to_string(), 1470 field: "status".to_string(), 1471 value: status as u64, 1472 type_: "StatusCode".to_string(), 1473 })?; 1474 Ok(Self { config_id, status }) 1475 } write_to(&self, buffer: &mut [u8])1476 fn write_to(&self, buffer: &mut [u8]) { 1477 let config_id = self.config_id; 1478 buffer[0..1].copy_from_slice(&config_id.to_le_bytes()[0..1]); 1479 let status = self.status.to_u8().unwrap(); 1480 buffer[1..2].copy_from_slice(&status.to_le_bytes()[0..1]); 1481 } get_total_size(&self) -> usize1482 fn get_total_size(&self) -> usize { 1483 let ret = 0; 1484 let ret = ret + 2; 1485 ret 1486 } 1487 } 1488 1489 #[derive(Debug, Clone, PartialEq)] 1490 pub struct Controlee { 1491 pub short_address: u16, 1492 pub subsession_id: u32, 1493 } 1494 impl Controlee { conforms(bytes: &[u8]) -> bool1495 fn conforms(bytes: &[u8]) -> bool { 1496 if bytes.len() < 6 { 1497 return false; 1498 } 1499 true 1500 } parse(bytes: &[u8]) -> Result<Self>1501 pub fn parse(bytes: &[u8]) -> Result<Self> { 1502 if bytes.len() < 2 { 1503 return Err(Error::InvalidLengthError { 1504 obj: "Controlee".to_string(), 1505 field: "short_address".to_string(), 1506 wanted: 2, 1507 got: bytes.len(), 1508 }); 1509 } 1510 let short_address = u16::from_le_bytes([bytes[0], bytes[1]]); 1511 if bytes.len() < 6 { 1512 return Err(Error::InvalidLengthError { 1513 obj: "Controlee".to_string(), 1514 field: "subsession_id".to_string(), 1515 wanted: 6, 1516 got: bytes.len(), 1517 }); 1518 } 1519 let subsession_id = u32::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5]]); 1520 Ok(Self { 1521 short_address, 1522 subsession_id, 1523 }) 1524 } write_to(&self, buffer: &mut [u8])1525 fn write_to(&self, buffer: &mut [u8]) { 1526 let short_address = self.short_address; 1527 buffer[0..2].copy_from_slice(&short_address.to_le_bytes()[0..2]); 1528 let subsession_id = self.subsession_id; 1529 buffer[2..6].copy_from_slice(&subsession_id.to_le_bytes()[0..4]); 1530 } get_total_size(&self) -> usize1531 fn get_total_size(&self) -> usize { 1532 let ret = 0; 1533 let ret = ret + 6; 1534 ret 1535 } 1536 } 1537 1538 #[derive(Debug, Clone, PartialEq)] 1539 pub struct ControleeStatus { 1540 pub mac_address: u16, 1541 pub subsession_id: u32, 1542 pub status: u8, 1543 } 1544 impl ControleeStatus { conforms(bytes: &[u8]) -> bool1545 fn conforms(bytes: &[u8]) -> bool { 1546 if bytes.len() < 7 { 1547 return false; 1548 } 1549 true 1550 } parse(bytes: &[u8]) -> Result<Self>1551 pub fn parse(bytes: &[u8]) -> Result<Self> { 1552 if bytes.len() < 2 { 1553 return Err(Error::InvalidLengthError { 1554 obj: "ControleeStatus".to_string(), 1555 field: "mac_address".to_string(), 1556 wanted: 2, 1557 got: bytes.len(), 1558 }); 1559 } 1560 let mac_address = u16::from_le_bytes([bytes[0], bytes[1]]); 1561 if bytes.len() < 6 { 1562 return Err(Error::InvalidLengthError { 1563 obj: "ControleeStatus".to_string(), 1564 field: "subsession_id".to_string(), 1565 wanted: 6, 1566 got: bytes.len(), 1567 }); 1568 } 1569 let subsession_id = u32::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5]]); 1570 if bytes.len() < 7 { 1571 return Err(Error::InvalidLengthError { 1572 obj: "ControleeStatus".to_string(), 1573 field: "status".to_string(), 1574 wanted: 7, 1575 got: bytes.len(), 1576 }); 1577 } 1578 let status = u8::from_le_bytes([bytes[6]]); 1579 Ok(Self { 1580 mac_address, 1581 subsession_id, 1582 status, 1583 }) 1584 } write_to(&self, buffer: &mut [u8])1585 fn write_to(&self, buffer: &mut [u8]) { 1586 let mac_address = self.mac_address; 1587 buffer[0..2].copy_from_slice(&mac_address.to_le_bytes()[0..2]); 1588 let subsession_id = self.subsession_id; 1589 buffer[2..6].copy_from_slice(&subsession_id.to_le_bytes()[0..4]); 1590 let status = self.status; 1591 buffer[6..7].copy_from_slice(&status.to_le_bytes()[0..1]); 1592 } get_total_size(&self) -> usize1593 fn get_total_size(&self) -> usize { 1594 let ret = 0; 1595 let ret = ret + 7; 1596 ret 1597 } 1598 } 1599 1600 #[derive(Debug, Clone, PartialEq)] 1601 pub struct ShortAddressTwoWayRangingMeasurement { 1602 pub mac_address: u16, 1603 pub status: StatusCode, 1604 pub nlos: u8, 1605 pub distance: u16, 1606 pub aoa_azimuth: u16, 1607 pub aoa_azimuth_fom: u8, 1608 pub aoa_elevation: u16, 1609 pub aoa_elevation_fom: u8, 1610 pub aoa_destination_azimuth: u16, 1611 pub aoa_destination_azimuth_fom: u8, 1612 pub aoa_destination_elevation: u16, 1613 pub aoa_destination_elevation_fom: u8, 1614 pub slot_index: u8, 1615 } 1616 impl ShortAddressTwoWayRangingMeasurement { conforms(bytes: &[u8]) -> bool1617 fn conforms(bytes: &[u8]) -> bool { 1618 if bytes.len() < 31 { 1619 return false; 1620 } 1621 true 1622 } parse(bytes: &[u8]) -> Result<Self>1623 pub fn parse(bytes: &[u8]) -> Result<Self> { 1624 if bytes.len() < 2 { 1625 return Err(Error::InvalidLengthError { 1626 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1627 field: "mac_address".to_string(), 1628 wanted: 2, 1629 got: bytes.len(), 1630 }); 1631 } 1632 let mac_address = u16::from_le_bytes([bytes[0], bytes[1]]); 1633 if bytes.len() < 3 { 1634 return Err(Error::InvalidLengthError { 1635 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1636 field: "status".to_string(), 1637 wanted: 3, 1638 got: bytes.len(), 1639 }); 1640 } 1641 let status = u8::from_le_bytes([bytes[2]]); 1642 let status = StatusCode::from_u8(status).ok_or_else(|| Error::InvalidEnumValueError { 1643 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1644 field: "status".to_string(), 1645 value: status as u64, 1646 type_: "StatusCode".to_string(), 1647 })?; 1648 if bytes.len() < 4 { 1649 return Err(Error::InvalidLengthError { 1650 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1651 field: "nlos".to_string(), 1652 wanted: 4, 1653 got: bytes.len(), 1654 }); 1655 } 1656 let nlos = u8::from_le_bytes([bytes[3]]); 1657 if bytes.len() < 6 { 1658 return Err(Error::InvalidLengthError { 1659 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1660 field: "distance".to_string(), 1661 wanted: 6, 1662 got: bytes.len(), 1663 }); 1664 } 1665 let distance = u16::from_le_bytes([bytes[4], bytes[5]]); 1666 if bytes.len() < 8 { 1667 return Err(Error::InvalidLengthError { 1668 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1669 field: "aoa_azimuth".to_string(), 1670 wanted: 8, 1671 got: bytes.len(), 1672 }); 1673 } 1674 let aoa_azimuth = u16::from_le_bytes([bytes[6], bytes[7]]); 1675 if bytes.len() < 9 { 1676 return Err(Error::InvalidLengthError { 1677 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1678 field: "aoa_azimuth_fom".to_string(), 1679 wanted: 9, 1680 got: bytes.len(), 1681 }); 1682 } 1683 let aoa_azimuth_fom = u8::from_le_bytes([bytes[8]]); 1684 if bytes.len() < 11 { 1685 return Err(Error::InvalidLengthError { 1686 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1687 field: "aoa_elevation".to_string(), 1688 wanted: 11, 1689 got: bytes.len(), 1690 }); 1691 } 1692 let aoa_elevation = u16::from_le_bytes([bytes[9], bytes[10]]); 1693 if bytes.len() < 12 { 1694 return Err(Error::InvalidLengthError { 1695 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1696 field: "aoa_elevation_fom".to_string(), 1697 wanted: 12, 1698 got: bytes.len(), 1699 }); 1700 } 1701 let aoa_elevation_fom = u8::from_le_bytes([bytes[11]]); 1702 if bytes.len() < 14 { 1703 return Err(Error::InvalidLengthError { 1704 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1705 field: "aoa_destination_azimuth".to_string(), 1706 wanted: 14, 1707 got: bytes.len(), 1708 }); 1709 } 1710 let aoa_destination_azimuth = u16::from_le_bytes([bytes[12], bytes[13]]); 1711 if bytes.len() < 15 { 1712 return Err(Error::InvalidLengthError { 1713 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1714 field: "aoa_destination_azimuth_fom".to_string(), 1715 wanted: 15, 1716 got: bytes.len(), 1717 }); 1718 } 1719 let aoa_destination_azimuth_fom = u8::from_le_bytes([bytes[14]]); 1720 if bytes.len() < 17 { 1721 return Err(Error::InvalidLengthError { 1722 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1723 field: "aoa_destination_elevation".to_string(), 1724 wanted: 17, 1725 got: bytes.len(), 1726 }); 1727 } 1728 let aoa_destination_elevation = u16::from_le_bytes([bytes[15], bytes[16]]); 1729 if bytes.len() < 18 { 1730 return Err(Error::InvalidLengthError { 1731 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1732 field: "aoa_destination_elevation_fom".to_string(), 1733 wanted: 18, 1734 got: bytes.len(), 1735 }); 1736 } 1737 let aoa_destination_elevation_fom = u8::from_le_bytes([bytes[17]]); 1738 if bytes.len() < 19 { 1739 return Err(Error::InvalidLengthError { 1740 obj: "ShortAddressTwoWayRangingMeasurement".to_string(), 1741 field: "slot_index".to_string(), 1742 wanted: 19, 1743 got: bytes.len(), 1744 }); 1745 } 1746 let slot_index = u8::from_le_bytes([bytes[18]]); 1747 Ok(Self { 1748 mac_address, 1749 status, 1750 nlos, 1751 distance, 1752 aoa_azimuth, 1753 aoa_azimuth_fom, 1754 aoa_elevation, 1755 aoa_elevation_fom, 1756 aoa_destination_azimuth, 1757 aoa_destination_azimuth_fom, 1758 aoa_destination_elevation, 1759 aoa_destination_elevation_fom, 1760 slot_index, 1761 }) 1762 } write_to(&self, buffer: &mut [u8])1763 fn write_to(&self, buffer: &mut [u8]) { 1764 let mac_address = self.mac_address; 1765 buffer[0..2].copy_from_slice(&mac_address.to_le_bytes()[0..2]); 1766 let status = self.status.to_u8().unwrap(); 1767 buffer[2..3].copy_from_slice(&status.to_le_bytes()[0..1]); 1768 let nlos = self.nlos; 1769 buffer[3..4].copy_from_slice(&nlos.to_le_bytes()[0..1]); 1770 let distance = self.distance; 1771 buffer[4..6].copy_from_slice(&distance.to_le_bytes()[0..2]); 1772 let aoa_azimuth = self.aoa_azimuth; 1773 buffer[6..8].copy_from_slice(&aoa_azimuth.to_le_bytes()[0..2]); 1774 let aoa_azimuth_fom = self.aoa_azimuth_fom; 1775 buffer[8..9].copy_from_slice(&aoa_azimuth_fom.to_le_bytes()[0..1]); 1776 let aoa_elevation = self.aoa_elevation; 1777 buffer[9..11].copy_from_slice(&aoa_elevation.to_le_bytes()[0..2]); 1778 let aoa_elevation_fom = self.aoa_elevation_fom; 1779 buffer[11..12].copy_from_slice(&aoa_elevation_fom.to_le_bytes()[0..1]); 1780 let aoa_destination_azimuth = self.aoa_destination_azimuth; 1781 buffer[12..14].copy_from_slice(&aoa_destination_azimuth.to_le_bytes()[0..2]); 1782 let aoa_destination_azimuth_fom = self.aoa_destination_azimuth_fom; 1783 buffer[14..15].copy_from_slice(&aoa_destination_azimuth_fom.to_le_bytes()[0..1]); 1784 let aoa_destination_elevation = self.aoa_destination_elevation; 1785 buffer[15..17].copy_from_slice(&aoa_destination_elevation.to_le_bytes()[0..2]); 1786 let aoa_destination_elevation_fom = self.aoa_destination_elevation_fom; 1787 buffer[17..18].copy_from_slice(&aoa_destination_elevation_fom.to_le_bytes()[0..1]); 1788 let slot_index = self.slot_index; 1789 buffer[18..19].copy_from_slice(&slot_index.to_le_bytes()[0..1]); 1790 } get_total_size(&self) -> usize1791 fn get_total_size(&self) -> usize { 1792 let ret = 0; 1793 let ret = ret + 31; 1794 ret 1795 } 1796 } 1797 1798 #[derive(Debug, Clone, PartialEq)] 1799 pub struct ExtendedAddressTwoWayRangingMeasurement { 1800 pub mac_address: u64, 1801 pub status: StatusCode, 1802 pub nlos: u8, 1803 pub distance: u16, 1804 pub aoa_azimuth: u16, 1805 pub aoa_azimuth_fom: u8, 1806 pub aoa_elevation: u16, 1807 pub aoa_elevation_fom: u8, 1808 pub aoa_destination_azimuth: u16, 1809 pub aoa_destination_azimuth_fom: u8, 1810 pub aoa_destination_elevation: u16, 1811 pub aoa_destination_elevation_fom: u8, 1812 pub slot_index: u8, 1813 } 1814 impl ExtendedAddressTwoWayRangingMeasurement { conforms(bytes: &[u8]) -> bool1815 fn conforms(bytes: &[u8]) -> bool { 1816 if bytes.len() < 31 { 1817 return false; 1818 } 1819 true 1820 } parse(bytes: &[u8]) -> Result<Self>1821 pub fn parse(bytes: &[u8]) -> Result<Self> { 1822 if bytes.len() < 8 { 1823 return Err(Error::InvalidLengthError { 1824 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1825 field: "mac_address".to_string(), 1826 wanted: 8, 1827 got: bytes.len(), 1828 }); 1829 } 1830 let mac_address = u64::from_le_bytes([ 1831 bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], 1832 ]); 1833 if bytes.len() < 9 { 1834 return Err(Error::InvalidLengthError { 1835 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1836 field: "status".to_string(), 1837 wanted: 9, 1838 got: bytes.len(), 1839 }); 1840 } 1841 let status = u8::from_le_bytes([bytes[8]]); 1842 let status = StatusCode::from_u8(status).ok_or_else(|| Error::InvalidEnumValueError { 1843 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1844 field: "status".to_string(), 1845 value: status as u64, 1846 type_: "StatusCode".to_string(), 1847 })?; 1848 if bytes.len() < 10 { 1849 return Err(Error::InvalidLengthError { 1850 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1851 field: "nlos".to_string(), 1852 wanted: 10, 1853 got: bytes.len(), 1854 }); 1855 } 1856 let nlos = u8::from_le_bytes([bytes[9]]); 1857 if bytes.len() < 12 { 1858 return Err(Error::InvalidLengthError { 1859 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1860 field: "distance".to_string(), 1861 wanted: 12, 1862 got: bytes.len(), 1863 }); 1864 } 1865 let distance = u16::from_le_bytes([bytes[10], bytes[11]]); 1866 if bytes.len() < 14 { 1867 return Err(Error::InvalidLengthError { 1868 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1869 field: "aoa_azimuth".to_string(), 1870 wanted: 14, 1871 got: bytes.len(), 1872 }); 1873 } 1874 let aoa_azimuth = u16::from_le_bytes([bytes[12], bytes[13]]); 1875 if bytes.len() < 15 { 1876 return Err(Error::InvalidLengthError { 1877 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1878 field: "aoa_azimuth_fom".to_string(), 1879 wanted: 15, 1880 got: bytes.len(), 1881 }); 1882 } 1883 let aoa_azimuth_fom = u8::from_le_bytes([bytes[14]]); 1884 if bytes.len() < 17 { 1885 return Err(Error::InvalidLengthError { 1886 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1887 field: "aoa_elevation".to_string(), 1888 wanted: 17, 1889 got: bytes.len(), 1890 }); 1891 } 1892 let aoa_elevation = u16::from_le_bytes([bytes[15], bytes[16]]); 1893 if bytes.len() < 18 { 1894 return Err(Error::InvalidLengthError { 1895 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1896 field: "aoa_elevation_fom".to_string(), 1897 wanted: 18, 1898 got: bytes.len(), 1899 }); 1900 } 1901 let aoa_elevation_fom = u8::from_le_bytes([bytes[17]]); 1902 if bytes.len() < 20 { 1903 return Err(Error::InvalidLengthError { 1904 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1905 field: "aoa_destination_azimuth".to_string(), 1906 wanted: 20, 1907 got: bytes.len(), 1908 }); 1909 } 1910 let aoa_destination_azimuth = u16::from_le_bytes([bytes[18], bytes[19]]); 1911 if bytes.len() < 21 { 1912 return Err(Error::InvalidLengthError { 1913 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1914 field: "aoa_destination_azimuth_fom".to_string(), 1915 wanted: 21, 1916 got: bytes.len(), 1917 }); 1918 } 1919 let aoa_destination_azimuth_fom = u8::from_le_bytes([bytes[20]]); 1920 if bytes.len() < 23 { 1921 return Err(Error::InvalidLengthError { 1922 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1923 field: "aoa_destination_elevation".to_string(), 1924 wanted: 23, 1925 got: bytes.len(), 1926 }); 1927 } 1928 let aoa_destination_elevation = u16::from_le_bytes([bytes[21], bytes[22]]); 1929 if bytes.len() < 24 { 1930 return Err(Error::InvalidLengthError { 1931 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1932 field: "aoa_destination_elevation_fom".to_string(), 1933 wanted: 24, 1934 got: bytes.len(), 1935 }); 1936 } 1937 let aoa_destination_elevation_fom = u8::from_le_bytes([bytes[23]]); 1938 if bytes.len() < 25 { 1939 return Err(Error::InvalidLengthError { 1940 obj: "ExtendedAddressTwoWayRangingMeasurement".to_string(), 1941 field: "slot_index".to_string(), 1942 wanted: 25, 1943 got: bytes.len(), 1944 }); 1945 } 1946 let slot_index = u8::from_le_bytes([bytes[24]]); 1947 Ok(Self { 1948 mac_address, 1949 status, 1950 nlos, 1951 distance, 1952 aoa_azimuth, 1953 aoa_azimuth_fom, 1954 aoa_elevation, 1955 aoa_elevation_fom, 1956 aoa_destination_azimuth, 1957 aoa_destination_azimuth_fom, 1958 aoa_destination_elevation, 1959 aoa_destination_elevation_fom, 1960 slot_index, 1961 }) 1962 } write_to(&self, buffer: &mut [u8])1963 fn write_to(&self, buffer: &mut [u8]) { 1964 let mac_address = self.mac_address; 1965 buffer[0..8].copy_from_slice(&mac_address.to_le_bytes()[0..8]); 1966 let status = self.status.to_u8().unwrap(); 1967 buffer[8..9].copy_from_slice(&status.to_le_bytes()[0..1]); 1968 let nlos = self.nlos; 1969 buffer[9..10].copy_from_slice(&nlos.to_le_bytes()[0..1]); 1970 let distance = self.distance; 1971 buffer[10..12].copy_from_slice(&distance.to_le_bytes()[0..2]); 1972 let aoa_azimuth = self.aoa_azimuth; 1973 buffer[12..14].copy_from_slice(&aoa_azimuth.to_le_bytes()[0..2]); 1974 let aoa_azimuth_fom = self.aoa_azimuth_fom; 1975 buffer[14..15].copy_from_slice(&aoa_azimuth_fom.to_le_bytes()[0..1]); 1976 let aoa_elevation = self.aoa_elevation; 1977 buffer[15..17].copy_from_slice(&aoa_elevation.to_le_bytes()[0..2]); 1978 let aoa_elevation_fom = self.aoa_elevation_fom; 1979 buffer[17..18].copy_from_slice(&aoa_elevation_fom.to_le_bytes()[0..1]); 1980 let aoa_destination_azimuth = self.aoa_destination_azimuth; 1981 buffer[18..20].copy_from_slice(&aoa_destination_azimuth.to_le_bytes()[0..2]); 1982 let aoa_destination_azimuth_fom = self.aoa_destination_azimuth_fom; 1983 buffer[20..21].copy_from_slice(&aoa_destination_azimuth_fom.to_le_bytes()[0..1]); 1984 let aoa_destination_elevation = self.aoa_destination_elevation; 1985 buffer[21..23].copy_from_slice(&aoa_destination_elevation.to_le_bytes()[0..2]); 1986 let aoa_destination_elevation_fom = self.aoa_destination_elevation_fom; 1987 buffer[23..24].copy_from_slice(&aoa_destination_elevation_fom.to_le_bytes()[0..1]); 1988 let slot_index = self.slot_index; 1989 buffer[24..25].copy_from_slice(&slot_index.to_le_bytes()[0..1]); 1990 } get_total_size(&self) -> usize1991 fn get_total_size(&self) -> usize { 1992 let ret = 0; 1993 let ret = ret + 31; 1994 ret 1995 } 1996 } 1997 1998 #[derive(Debug, Clone, PartialEq)] 1999 pub struct PicaPosition { 2000 pub x: u16, 2001 pub y: u16, 2002 pub z: u16, 2003 pub yaw: u16, 2004 pub pitch: u8, 2005 pub roll: u16, 2006 } 2007 impl PicaPosition { conforms(bytes: &[u8]) -> bool2008 fn conforms(bytes: &[u8]) -> bool { 2009 if bytes.len() < 11 { 2010 return false; 2011 } 2012 true 2013 } parse(bytes: &[u8]) -> Result<Self>2014 pub fn parse(bytes: &[u8]) -> Result<Self> { 2015 if bytes.len() < 2 { 2016 return Err(Error::InvalidLengthError { 2017 obj: "PicaPosition".to_string(), 2018 field: "x".to_string(), 2019 wanted: 2, 2020 got: bytes.len(), 2021 }); 2022 } 2023 let x = u16::from_le_bytes([bytes[0], bytes[1]]); 2024 if bytes.len() < 4 { 2025 return Err(Error::InvalidLengthError { 2026 obj: "PicaPosition".to_string(), 2027 field: "y".to_string(), 2028 wanted: 4, 2029 got: bytes.len(), 2030 }); 2031 } 2032 let y = u16::from_le_bytes([bytes[2], bytes[3]]); 2033 if bytes.len() < 6 { 2034 return Err(Error::InvalidLengthError { 2035 obj: "PicaPosition".to_string(), 2036 field: "z".to_string(), 2037 wanted: 6, 2038 got: bytes.len(), 2039 }); 2040 } 2041 let z = u16::from_le_bytes([bytes[4], bytes[5]]); 2042 if bytes.len() < 8 { 2043 return Err(Error::InvalidLengthError { 2044 obj: "PicaPosition".to_string(), 2045 field: "yaw".to_string(), 2046 wanted: 8, 2047 got: bytes.len(), 2048 }); 2049 } 2050 let yaw = u16::from_le_bytes([bytes[6], bytes[7]]); 2051 if bytes.len() < 9 { 2052 return Err(Error::InvalidLengthError { 2053 obj: "PicaPosition".to_string(), 2054 field: "pitch".to_string(), 2055 wanted: 9, 2056 got: bytes.len(), 2057 }); 2058 } 2059 let pitch = u8::from_le_bytes([bytes[8]]); 2060 if bytes.len() < 11 { 2061 return Err(Error::InvalidLengthError { 2062 obj: "PicaPosition".to_string(), 2063 field: "roll".to_string(), 2064 wanted: 11, 2065 got: bytes.len(), 2066 }); 2067 } 2068 let roll = u16::from_le_bytes([bytes[9], bytes[10]]); 2069 Ok(Self { 2070 x, 2071 y, 2072 z, 2073 yaw, 2074 pitch, 2075 roll, 2076 }) 2077 } write_to(&self, buffer: &mut [u8])2078 fn write_to(&self, buffer: &mut [u8]) { 2079 let x = self.x; 2080 buffer[0..2].copy_from_slice(&x.to_le_bytes()[0..2]); 2081 let y = self.y; 2082 buffer[2..4].copy_from_slice(&y.to_le_bytes()[0..2]); 2083 let z = self.z; 2084 buffer[4..6].copy_from_slice(&z.to_le_bytes()[0..2]); 2085 let yaw = self.yaw; 2086 buffer[6..8].copy_from_slice(&yaw.to_le_bytes()[0..2]); 2087 let pitch = self.pitch; 2088 buffer[8..9].copy_from_slice(&pitch.to_le_bytes()[0..1]); 2089 let roll = self.roll; 2090 buffer[9..11].copy_from_slice(&roll.to_le_bytes()[0..2]); 2091 } get_total_size(&self) -> usize2092 fn get_total_size(&self) -> usize { 2093 let ret = 0; 2094 let ret = ret + 11; 2095 ret 2096 } 2097 } 2098 2099 #[derive(Debug, Clone, PartialEq)] 2100 pub struct PowerStats { 2101 pub status: StatusCode, 2102 pub idle_time_ms: u32, 2103 pub tx_time_ms: u32, 2104 pub rx_time_ms: u32, 2105 pub total_wake_count: u32, 2106 } 2107 impl PowerStats { conforms(bytes: &[u8]) -> bool2108 fn conforms(bytes: &[u8]) -> bool { 2109 if bytes.len() < 17 { 2110 return false; 2111 } 2112 true 2113 } parse(bytes: &[u8]) -> Result<Self>2114 pub fn parse(bytes: &[u8]) -> Result<Self> { 2115 if bytes.len() < 1 { 2116 return Err(Error::InvalidLengthError { 2117 obj: "PowerStats".to_string(), 2118 field: "status".to_string(), 2119 wanted: 1, 2120 got: bytes.len(), 2121 }); 2122 } 2123 let status = u8::from_le_bytes([bytes[0]]); 2124 let status = StatusCode::from_u8(status).ok_or_else(|| Error::InvalidEnumValueError { 2125 obj: "PowerStats".to_string(), 2126 field: "status".to_string(), 2127 value: status as u64, 2128 type_: "StatusCode".to_string(), 2129 })?; 2130 if bytes.len() < 5 { 2131 return Err(Error::InvalidLengthError { 2132 obj: "PowerStats".to_string(), 2133 field: "idle_time_ms".to_string(), 2134 wanted: 5, 2135 got: bytes.len(), 2136 }); 2137 } 2138 let idle_time_ms = u32::from_le_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]); 2139 if bytes.len() < 9 { 2140 return Err(Error::InvalidLengthError { 2141 obj: "PowerStats".to_string(), 2142 field: "tx_time_ms".to_string(), 2143 wanted: 9, 2144 got: bytes.len(), 2145 }); 2146 } 2147 let tx_time_ms = u32::from_le_bytes([bytes[5], bytes[6], bytes[7], bytes[8]]); 2148 if bytes.len() < 13 { 2149 return Err(Error::InvalidLengthError { 2150 obj: "PowerStats".to_string(), 2151 field: "rx_time_ms".to_string(), 2152 wanted: 13, 2153 got: bytes.len(), 2154 }); 2155 } 2156 let rx_time_ms = u32::from_le_bytes([bytes[9], bytes[10], bytes[11], bytes[12]]); 2157 if bytes.len() < 17 { 2158 return Err(Error::InvalidLengthError { 2159 obj: "PowerStats".to_string(), 2160 field: "total_wake_count".to_string(), 2161 wanted: 17, 2162 got: bytes.len(), 2163 }); 2164 } 2165 let total_wake_count = u32::from_le_bytes([bytes[13], bytes[14], bytes[15], bytes[16]]); 2166 Ok(Self { 2167 status, 2168 idle_time_ms, 2169 tx_time_ms, 2170 rx_time_ms, 2171 total_wake_count, 2172 }) 2173 } write_to(&self, buffer: &mut [u8])2174 fn write_to(&self, buffer: &mut [u8]) { 2175 let status = self.status.to_u8().unwrap(); 2176 buffer[0..1].copy_from_slice(&status.to_le_bytes()[0..1]); 2177 let idle_time_ms = self.idle_time_ms; 2178 buffer[1..5].copy_from_slice(&idle_time_ms.to_le_bytes()[0..4]); 2179 let tx_time_ms = self.tx_time_ms; 2180 buffer[5..9].copy_from_slice(&tx_time_ms.to_le_bytes()[0..4]); 2181 let rx_time_ms = self.rx_time_ms; 2182 buffer[9..13].copy_from_slice(&rx_time_ms.to_le_bytes()[0..4]); 2183 let total_wake_count = self.total_wake_count; 2184 buffer[13..17].copy_from_slice(&total_wake_count.to_le_bytes()[0..4]); 2185 } get_total_size(&self) -> usize2186 fn get_total_size(&self) -> usize { 2187 let ret = 0; 2188 let ret = ret + 17; 2189 ret 2190 } 2191 } 2192 2193 #[derive(Debug)] 2194 enum UciPacketDataChild { 2195 UciCommand(Arc<UciCommandData>), 2196 UciResponse(Arc<UciResponseData>), 2197 UciNotification(Arc<UciNotificationData>), 2198 Payload(Bytes), 2199 None, 2200 } 2201 impl UciPacketDataChild { get_total_size(&self) -> usize2202 fn get_total_size(&self) -> usize { 2203 match self { 2204 UciPacketDataChild::UciCommand(value) => value.get_total_size(), 2205 UciPacketDataChild::UciResponse(value) => value.get_total_size(), 2206 UciPacketDataChild::UciNotification(value) => value.get_total_size(), 2207 UciPacketDataChild::Payload(p) => p.len(), 2208 UciPacketDataChild::None => 0, 2209 } 2210 } 2211 } 2212 #[derive(Debug)] 2213 pub enum UciPacketChild { 2214 UciCommand(UciCommandPacket), 2215 UciResponse(UciResponsePacket), 2216 UciNotification(UciNotificationPacket), 2217 Payload(Bytes), 2218 None, 2219 } 2220 #[derive(Debug)] 2221 struct UciPacketData { 2222 group_id: GroupId, 2223 packet_boundary_flag: PacketBoundaryFlag, 2224 message_type: MessageType, 2225 opcode: u8, 2226 child: UciPacketDataChild, 2227 } 2228 #[derive(Debug, Clone)] 2229 pub struct UciPacketPacket { 2230 uci_packet: Arc<UciPacketData>, 2231 } 2232 #[derive(Debug)] 2233 pub struct UciPacketBuilder { 2234 pub group_id: GroupId, 2235 pub packet_boundary_flag: PacketBoundaryFlag, 2236 pub message_type: MessageType, 2237 pub opcode: u8, 2238 pub payload: Option<Bytes>, 2239 } 2240 impl UciPacketData { conforms(bytes: &[u8]) -> bool2241 fn conforms(bytes: &[u8]) -> bool { 2242 if bytes.len() < 4 { 2243 return false; 2244 } 2245 true 2246 } parse(bytes: &[u8]) -> Result<Self>2247 fn parse(bytes: &[u8]) -> Result<Self> { 2248 let group_id = u8::from_le_bytes([bytes[0]]); 2249 let group_id = group_id & 0xf; 2250 let group_id = GroupId::from_u8(group_id).ok_or_else(|| Error::InvalidEnumValueError { 2251 obj: "UciPacket".to_string(), 2252 field: "group_id".to_string(), 2253 value: group_id as u64, 2254 type_: "GroupId".to_string(), 2255 })?; 2256 let packet_boundary_flag = u8::from_le_bytes([bytes[0]]); 2257 let packet_boundary_flag = packet_boundary_flag >> 4; 2258 let packet_boundary_flag = packet_boundary_flag & 0x1; 2259 let packet_boundary_flag = 2260 PacketBoundaryFlag::from_u8(packet_boundary_flag).ok_or_else(|| { 2261 Error::InvalidEnumValueError { 2262 obj: "UciPacket".to_string(), 2263 field: "packet_boundary_flag".to_string(), 2264 value: packet_boundary_flag as u64, 2265 type_: "PacketBoundaryFlag".to_string(), 2266 } 2267 })?; 2268 let message_type = u8::from_le_bytes([bytes[0]]); 2269 let message_type = message_type >> 5; 2270 let message_type = message_type & 0x7; 2271 let message_type = 2272 MessageType::from_u8(message_type).ok_or_else(|| Error::InvalidEnumValueError { 2273 obj: "UciPacket".to_string(), 2274 field: "message_type".to_string(), 2275 value: message_type as u64, 2276 type_: "MessageType".to_string(), 2277 })?; 2278 let opcode = u8::from_le_bytes([bytes[1]]); 2279 let opcode = opcode & 0x3f; 2280 if bytes.len() < 4 { 2281 return Err(Error::InvalidLengthError { 2282 obj: "UciPacket".to_string(), 2283 field: "payload_size".to_string(), 2284 wanted: 4, 2285 got: bytes.len(), 2286 }); 2287 } 2288 let payload_size = u8::from_le_bytes([bytes[3]]); 2289 let want_ = 4 + (payload_size as usize); 2290 if bytes.len() < want_ { 2291 return Err(Error::InvalidLengthError { 2292 obj: "UciPacket".to_string(), 2293 field: "payload".to_string(), 2294 wanted: want_, 2295 got: bytes.len(), 2296 }); 2297 } 2298 let payload: Vec<u8> = bytes[4..(4 + payload_size as usize)].into(); 2299 let child = match (packet_boundary_flag, message_type) { 2300 (PacketBoundaryFlag::Complete, MessageType::Command) 2301 if UciCommandData::conforms(&bytes[..]) => 2302 { 2303 UciPacketDataChild::UciCommand(Arc::new(UciCommandData::parse( 2304 &bytes[..], 2305 group_id, 2306 opcode, 2307 )?)) 2308 } 2309 (PacketBoundaryFlag::Complete, MessageType::Response) 2310 if UciResponseData::conforms(&bytes[..]) => 2311 { 2312 UciPacketDataChild::UciResponse(Arc::new(UciResponseData::parse( 2313 &bytes[..], 2314 group_id, 2315 opcode, 2316 )?)) 2317 } 2318 (PacketBoundaryFlag::Complete, MessageType::Notification) 2319 if UciNotificationData::conforms(&bytes[..]) => 2320 { 2321 UciPacketDataChild::UciNotification(Arc::new(UciNotificationData::parse( 2322 &bytes[..], 2323 group_id, 2324 opcode, 2325 )?)) 2326 } 2327 (_, _) => return Err(Error::InvalidPacketError), 2328 }; 2329 Ok(Self { 2330 group_id, 2331 packet_boundary_flag, 2332 message_type, 2333 opcode, 2334 child, 2335 }) 2336 } write_to(&self, buffer: &mut BytesMut)2337 fn write_to(&self, buffer: &mut BytesMut) { 2338 let group_id = self.group_id.to_u8().unwrap(); 2339 let group_id = group_id & 0xf; 2340 buffer[0..1].copy_from_slice(&group_id.to_le_bytes()[0..1]); 2341 let packet_boundary_flag = self.packet_boundary_flag.to_u8().unwrap(); 2342 let packet_boundary_flag = packet_boundary_flag & 0x1; 2343 let packet_boundary_flag = (packet_boundary_flag << 4) | ((buffer[0] as u8) & 0xf); 2344 buffer[0..1].copy_from_slice(&packet_boundary_flag.to_le_bytes()[0..1]); 2345 let message_type = self.message_type.to_u8().unwrap(); 2346 let message_type = message_type & 0x7; 2347 let message_type = (message_type << 5) | ((buffer[0] as u8) & 0x1f); 2348 buffer[0..1].copy_from_slice(&message_type.to_le_bytes()[0..1]); 2349 let opcode = self.opcode; 2350 let opcode = opcode & 0x3f; 2351 buffer[1..2].copy_from_slice(&opcode.to_le_bytes()[0..1]); 2352 let payload_size = 2353 u8::try_from(self.child.get_total_size()).expect("payload size did not fit"); 2354 buffer[3..4].copy_from_slice(&payload_size.to_le_bytes()[0..1]); 2355 match &self.child { 2356 UciPacketDataChild::UciCommand(value) => value.write_to(buffer), 2357 UciPacketDataChild::UciResponse(value) => value.write_to(buffer), 2358 UciPacketDataChild::UciNotification(value) => value.write_to(buffer), 2359 UciPacketDataChild::Payload(p) => buffer[4..].copy_from_slice(&p[..]), 2360 UciPacketDataChild::None => {} 2361 } 2362 } get_total_size(&self) -> usize2363 fn get_total_size(&self) -> usize { 2364 self.get_size() + self.child.get_total_size() 2365 } get_size(&self) -> usize2366 fn get_size(&self) -> usize { 2367 let ret = 0; 2368 let ret = ret + 4; 2369 ret 2370 } 2371 } 2372 impl Packet for UciPacketPacket { to_bytes(self) -> Bytes2373 fn to_bytes(self) -> Bytes { 2374 let mut buffer = BytesMut::new(); 2375 buffer.resize(self.uci_packet.get_total_size(), 0); 2376 self.uci_packet.write_to(&mut buffer); 2377 buffer.freeze() 2378 } to_vec(self) -> Vec<u8>2379 fn to_vec(self) -> Vec<u8> { 2380 self.to_bytes().to_vec() 2381 } 2382 } 2383 impl From<UciPacketPacket> for Bytes { from(packet: UciPacketPacket) -> Self2384 fn from(packet: UciPacketPacket) -> Self { 2385 packet.to_bytes() 2386 } 2387 } 2388 impl From<UciPacketPacket> for Vec<u8> { from(packet: UciPacketPacket) -> Self2389 fn from(packet: UciPacketPacket) -> Self { 2390 packet.to_vec() 2391 } 2392 } 2393 impl UciPacketPacket { parse(bytes: &[u8]) -> Result<Self>2394 pub fn parse(bytes: &[u8]) -> Result<Self> { 2395 Ok(Self::new(Arc::new(UciPacketData::parse(bytes)?)).unwrap()) 2396 } specialize(&self) -> UciPacketChild2397 pub fn specialize(&self) -> UciPacketChild { 2398 match &self.uci_packet.child { 2399 UciPacketDataChild::UciCommand(_) => { 2400 UciPacketChild::UciCommand(UciCommandPacket::new(self.uci_packet.clone()).unwrap()) 2401 } 2402 UciPacketDataChild::UciResponse(_) => UciPacketChild::UciResponse( 2403 UciResponsePacket::new(self.uci_packet.clone()).unwrap(), 2404 ), 2405 UciPacketDataChild::UciNotification(_) => UciPacketChild::UciNotification( 2406 UciNotificationPacket::new(self.uci_packet.clone()).unwrap(), 2407 ), 2408 UciPacketDataChild::Payload(p) => UciPacketChild::Payload(p.clone()), 2409 UciPacketDataChild::None => UciPacketChild::None, 2410 } 2411 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>2412 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 2413 let uci_packet = root; 2414 Ok(Self { uci_packet }) 2415 } get_group_id(&self) -> GroupId2416 pub fn get_group_id(&self) -> GroupId { 2417 self.uci_packet.as_ref().group_id 2418 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag2419 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 2420 self.uci_packet.as_ref().packet_boundary_flag 2421 } get_message_type(&self) -> MessageType2422 pub fn get_message_type(&self) -> MessageType { 2423 self.uci_packet.as_ref().message_type 2424 } get_opcode(&self) -> u82425 pub fn get_opcode(&self) -> u8 { 2426 self.uci_packet.as_ref().opcode 2427 } 2428 } 2429 impl UciPacketBuilder { build(self) -> UciPacketPacket2430 pub fn build(self) -> UciPacketPacket { 2431 let uci_packet = Arc::new(UciPacketData { 2432 group_id: self.group_id, 2433 packet_boundary_flag: self.packet_boundary_flag, 2434 message_type: self.message_type, 2435 opcode: self.opcode, 2436 child: match self.payload { 2437 None => UciPacketDataChild::None, 2438 Some(bytes) => UciPacketDataChild::Payload(bytes), 2439 }, 2440 }); 2441 UciPacketPacket::new(uci_packet).unwrap() 2442 } 2443 } 2444 2445 #[derive(Debug)] 2446 enum UciCommandDataChild { 2447 CoreCommand(Arc<CoreCommandData>), 2448 SessionCommand(Arc<SessionCommandData>), 2449 RangingCommand(Arc<RangingCommandData>), 2450 AndroidCommand(Arc<AndroidCommandData>), 2451 UciVendor_A_Command(Arc<UciVendor_A_CommandData>), 2452 UciVendor_B_Command(Arc<UciVendor_B_CommandData>), 2453 UciVendor_E_Command(Arc<UciVendor_E_CommandData>), 2454 UciVendor_F_Command(Arc<UciVendor_F_CommandData>), 2455 Payload(Bytes), 2456 None, 2457 } 2458 impl UciCommandDataChild { get_total_size(&self) -> usize2459 fn get_total_size(&self) -> usize { 2460 match self { 2461 UciCommandDataChild::CoreCommand(value) => value.get_total_size(), 2462 UciCommandDataChild::SessionCommand(value) => value.get_total_size(), 2463 UciCommandDataChild::RangingCommand(value) => value.get_total_size(), 2464 UciCommandDataChild::AndroidCommand(value) => value.get_total_size(), 2465 UciCommandDataChild::UciVendor_A_Command(value) => value.get_total_size(), 2466 UciCommandDataChild::UciVendor_B_Command(value) => value.get_total_size(), 2467 UciCommandDataChild::UciVendor_E_Command(value) => value.get_total_size(), 2468 UciCommandDataChild::UciVendor_F_Command(value) => value.get_total_size(), 2469 UciCommandDataChild::Payload(p) => p.len(), 2470 UciCommandDataChild::None => 0, 2471 } 2472 } 2473 } 2474 #[derive(Debug)] 2475 pub enum UciCommandChild { 2476 CoreCommand(CoreCommandPacket), 2477 SessionCommand(SessionCommandPacket), 2478 RangingCommand(RangingCommandPacket), 2479 AndroidCommand(AndroidCommandPacket), 2480 UciVendor_A_Command(UciVendor_A_CommandPacket), 2481 UciVendor_B_Command(UciVendor_B_CommandPacket), 2482 UciVendor_E_Command(UciVendor_E_CommandPacket), 2483 UciVendor_F_Command(UciVendor_F_CommandPacket), 2484 Payload(Bytes), 2485 None, 2486 } 2487 #[derive(Debug)] 2488 struct UciCommandData { 2489 child: UciCommandDataChild, 2490 } 2491 #[derive(Debug, Clone)] 2492 pub struct UciCommandPacket { 2493 uci_packet: Arc<UciPacketData>, 2494 uci_command: Arc<UciCommandData>, 2495 } 2496 #[derive(Debug)] 2497 pub struct UciCommandBuilder { 2498 pub group_id: GroupId, 2499 pub opcode: u8, 2500 pub payload: Option<Bytes>, 2501 } 2502 impl UciCommandData { conforms(bytes: &[u8]) -> bool2503 fn conforms(bytes: &[u8]) -> bool { 2504 if bytes.len() < 4 { 2505 return false; 2506 } 2507 true 2508 } parse(bytes: &[u8], group_id: GroupId, opcode: u8) -> Result<Self>2509 fn parse(bytes: &[u8], group_id: GroupId, opcode: u8) -> Result<Self> { 2510 let payload: Vec<u8> = bytes[4..].into(); 2511 let child = match (group_id) { 2512 (GroupId::Core) if CoreCommandData::conforms(&bytes[..]) => { 2513 UciCommandDataChild::CoreCommand(Arc::new(CoreCommandData::parse( 2514 &bytes[..], 2515 opcode, 2516 )?)) 2517 } 2518 (GroupId::SessionConfig) if SessionCommandData::conforms(&bytes[..]) => { 2519 UciCommandDataChild::SessionCommand(Arc::new(SessionCommandData::parse( 2520 &bytes[..], 2521 opcode, 2522 )?)) 2523 } 2524 (GroupId::RangingSessionControl) if RangingCommandData::conforms(&bytes[..]) => { 2525 UciCommandDataChild::RangingCommand(Arc::new(RangingCommandData::parse( 2526 &bytes[..], 2527 opcode, 2528 )?)) 2529 } 2530 (GroupId::VendorAndroid) if AndroidCommandData::conforms(&bytes[..]) => { 2531 UciCommandDataChild::AndroidCommand(Arc::new(AndroidCommandData::parse( 2532 &bytes[..], 2533 opcode, 2534 )?)) 2535 } 2536 (GroupId::VendorReservedA) if UciVendor_A_CommandData::conforms(&bytes[..]) => { 2537 UciCommandDataChild::UciVendor_A_Command(Arc::new(UciVendor_A_CommandData::parse( 2538 &bytes[..], 2539 )?)) 2540 } 2541 (GroupId::VendorReservedB) if UciVendor_B_CommandData::conforms(&bytes[..]) => { 2542 UciCommandDataChild::UciVendor_B_Command(Arc::new(UciVendor_B_CommandData::parse( 2543 &bytes[..], 2544 )?)) 2545 } 2546 (GroupId::VendorReservedE) if UciVendor_E_CommandData::conforms(&bytes[..]) => { 2547 UciCommandDataChild::UciVendor_E_Command(Arc::new(UciVendor_E_CommandData::parse( 2548 &bytes[..], 2549 )?)) 2550 } 2551 (GroupId::VendorReservedF) if UciVendor_F_CommandData::conforms(&bytes[..]) => { 2552 UciCommandDataChild::UciVendor_F_Command(Arc::new(UciVendor_F_CommandData::parse( 2553 &bytes[..], 2554 )?)) 2555 } 2556 (_) => return Err(Error::InvalidPacketError), 2557 }; 2558 Ok(Self { child }) 2559 } write_to(&self, buffer: &mut BytesMut)2560 fn write_to(&self, buffer: &mut BytesMut) { 2561 match &self.child { 2562 UciCommandDataChild::CoreCommand(value) => value.write_to(buffer), 2563 UciCommandDataChild::SessionCommand(value) => value.write_to(buffer), 2564 UciCommandDataChild::RangingCommand(value) => value.write_to(buffer), 2565 UciCommandDataChild::AndroidCommand(value) => value.write_to(buffer), 2566 UciCommandDataChild::UciVendor_A_Command(value) => value.write_to(buffer), 2567 UciCommandDataChild::UciVendor_B_Command(value) => value.write_to(buffer), 2568 UciCommandDataChild::UciVendor_E_Command(value) => value.write_to(buffer), 2569 UciCommandDataChild::UciVendor_F_Command(value) => value.write_to(buffer), 2570 UciCommandDataChild::Payload(p) => buffer[4..].copy_from_slice(&p[..]), 2571 UciCommandDataChild::None => {} 2572 } 2573 } get_total_size(&self) -> usize2574 fn get_total_size(&self) -> usize { 2575 self.get_size() + self.child.get_total_size() 2576 } get_size(&self) -> usize2577 fn get_size(&self) -> usize { 2578 let ret = 0; 2579 ret 2580 } 2581 } 2582 impl Packet for UciCommandPacket { to_bytes(self) -> Bytes2583 fn to_bytes(self) -> Bytes { 2584 let mut buffer = BytesMut::new(); 2585 buffer.resize(self.uci_packet.get_total_size(), 0); 2586 self.uci_packet.write_to(&mut buffer); 2587 buffer.freeze() 2588 } to_vec(self) -> Vec<u8>2589 fn to_vec(self) -> Vec<u8> { 2590 self.to_bytes().to_vec() 2591 } 2592 } 2593 impl From<UciCommandPacket> for Bytes { from(packet: UciCommandPacket) -> Self2594 fn from(packet: UciCommandPacket) -> Self { 2595 packet.to_bytes() 2596 } 2597 } 2598 impl From<UciCommandPacket> for Vec<u8> { from(packet: UciCommandPacket) -> Self2599 fn from(packet: UciCommandPacket) -> Self { 2600 packet.to_vec() 2601 } 2602 } 2603 impl TryFrom<UciPacketPacket> for UciCommandPacket { 2604 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>2605 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 2606 Self::new(value.uci_packet).map_err(TryFromError) 2607 } 2608 } 2609 impl UciCommandPacket { specialize(&self) -> UciCommandChild2610 pub fn specialize(&self) -> UciCommandChild { 2611 match &self.uci_command.child { 2612 UciCommandDataChild::CoreCommand(_) => UciCommandChild::CoreCommand( 2613 CoreCommandPacket::new(self.uci_packet.clone()).unwrap(), 2614 ), 2615 UciCommandDataChild::SessionCommand(_) => UciCommandChild::SessionCommand( 2616 SessionCommandPacket::new(self.uci_packet.clone()).unwrap(), 2617 ), 2618 UciCommandDataChild::RangingCommand(_) => UciCommandChild::RangingCommand( 2619 RangingCommandPacket::new(self.uci_packet.clone()).unwrap(), 2620 ), 2621 UciCommandDataChild::AndroidCommand(_) => UciCommandChild::AndroidCommand( 2622 AndroidCommandPacket::new(self.uci_packet.clone()).unwrap(), 2623 ), 2624 UciCommandDataChild::UciVendor_A_Command(_) => UciCommandChild::UciVendor_A_Command( 2625 UciVendor_A_CommandPacket::new(self.uci_packet.clone()).unwrap(), 2626 ), 2627 UciCommandDataChild::UciVendor_B_Command(_) => UciCommandChild::UciVendor_B_Command( 2628 UciVendor_B_CommandPacket::new(self.uci_packet.clone()).unwrap(), 2629 ), 2630 UciCommandDataChild::UciVendor_E_Command(_) => UciCommandChild::UciVendor_E_Command( 2631 UciVendor_E_CommandPacket::new(self.uci_packet.clone()).unwrap(), 2632 ), 2633 UciCommandDataChild::UciVendor_F_Command(_) => UciCommandChild::UciVendor_F_Command( 2634 UciVendor_F_CommandPacket::new(self.uci_packet.clone()).unwrap(), 2635 ), 2636 UciCommandDataChild::Payload(p) => UciCommandChild::Payload(p.clone()), 2637 UciCommandDataChild::None => UciCommandChild::None, 2638 } 2639 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>2640 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 2641 let uci_packet = root; 2642 let uci_command = match &uci_packet.child { 2643 UciPacketDataChild::UciCommand(value) => (*value).clone(), 2644 _ => return Err("inconsistent state - child was not UciCommand"), 2645 }; 2646 Ok(Self { 2647 uci_packet, 2648 uci_command, 2649 }) 2650 } get_group_id(&self) -> GroupId2651 pub fn get_group_id(&self) -> GroupId { 2652 self.uci_packet.as_ref().group_id 2653 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag2654 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 2655 self.uci_packet.as_ref().packet_boundary_flag 2656 } get_message_type(&self) -> MessageType2657 pub fn get_message_type(&self) -> MessageType { 2658 self.uci_packet.as_ref().message_type 2659 } get_opcode(&self) -> u82660 pub fn get_opcode(&self) -> u8 { 2661 self.uci_packet.as_ref().opcode 2662 } 2663 } 2664 impl Into<UciPacketPacket> for UciCommandPacket { into(self) -> UciPacketPacket2665 fn into(self) -> UciPacketPacket { 2666 UciPacketPacket::new(self.uci_packet).unwrap() 2667 } 2668 } 2669 impl UciCommandBuilder { build(self) -> UciCommandPacket2670 pub fn build(self) -> UciCommandPacket { 2671 let uci_command = Arc::new(UciCommandData { 2672 child: match self.payload { 2673 None => UciCommandDataChild::None, 2674 Some(bytes) => UciCommandDataChild::Payload(bytes), 2675 }, 2676 }); 2677 let uci_packet = Arc::new(UciPacketData { 2678 group_id: self.group_id, 2679 packet_boundary_flag: PacketBoundaryFlag::Complete, 2680 message_type: MessageType::Command, 2681 opcode: self.opcode, 2682 child: UciPacketDataChild::UciCommand(uci_command), 2683 }); 2684 UciCommandPacket::new(uci_packet).unwrap() 2685 } 2686 } 2687 impl Into<UciPacketPacket> for UciCommandBuilder { into(self) -> UciPacketPacket2688 fn into(self) -> UciPacketPacket { 2689 self.build().into() 2690 } 2691 } 2692 2693 #[derive(Debug)] 2694 enum UciResponseDataChild { 2695 CoreResponse(Arc<CoreResponseData>), 2696 SessionResponse(Arc<SessionResponseData>), 2697 RangingResponse(Arc<RangingResponseData>), 2698 AndroidResponse(Arc<AndroidResponseData>), 2699 UciVendor_A_Response(Arc<UciVendor_A_ResponseData>), 2700 UciVendor_B_Response(Arc<UciVendor_B_ResponseData>), 2701 UciVendor_E_Response(Arc<UciVendor_E_ResponseData>), 2702 UciVendor_F_Response(Arc<UciVendor_F_ResponseData>), 2703 Payload(Bytes), 2704 None, 2705 } 2706 impl UciResponseDataChild { get_total_size(&self) -> usize2707 fn get_total_size(&self) -> usize { 2708 match self { 2709 UciResponseDataChild::CoreResponse(value) => value.get_total_size(), 2710 UciResponseDataChild::SessionResponse(value) => value.get_total_size(), 2711 UciResponseDataChild::RangingResponse(value) => value.get_total_size(), 2712 UciResponseDataChild::AndroidResponse(value) => value.get_total_size(), 2713 UciResponseDataChild::UciVendor_A_Response(value) => value.get_total_size(), 2714 UciResponseDataChild::UciVendor_B_Response(value) => value.get_total_size(), 2715 UciResponseDataChild::UciVendor_E_Response(value) => value.get_total_size(), 2716 UciResponseDataChild::UciVendor_F_Response(value) => value.get_total_size(), 2717 UciResponseDataChild::Payload(p) => p.len(), 2718 UciResponseDataChild::None => 0, 2719 } 2720 } 2721 } 2722 #[derive(Debug)] 2723 pub enum UciResponseChild { 2724 CoreResponse(CoreResponsePacket), 2725 SessionResponse(SessionResponsePacket), 2726 RangingResponse(RangingResponsePacket), 2727 AndroidResponse(AndroidResponsePacket), 2728 UciVendor_A_Response(UciVendor_A_ResponsePacket), 2729 UciVendor_B_Response(UciVendor_B_ResponsePacket), 2730 UciVendor_E_Response(UciVendor_E_ResponsePacket), 2731 UciVendor_F_Response(UciVendor_F_ResponsePacket), 2732 Payload(Bytes), 2733 None, 2734 } 2735 #[derive(Debug)] 2736 struct UciResponseData { 2737 child: UciResponseDataChild, 2738 } 2739 #[derive(Debug, Clone)] 2740 pub struct UciResponsePacket { 2741 uci_packet: Arc<UciPacketData>, 2742 uci_response: Arc<UciResponseData>, 2743 } 2744 #[derive(Debug)] 2745 pub struct UciResponseBuilder { 2746 pub group_id: GroupId, 2747 pub opcode: u8, 2748 pub payload: Option<Bytes>, 2749 } 2750 impl UciResponseData { conforms(bytes: &[u8]) -> bool2751 fn conforms(bytes: &[u8]) -> bool { 2752 if bytes.len() < 4 { 2753 return false; 2754 } 2755 true 2756 } parse(bytes: &[u8], group_id: GroupId, opcode: u8) -> Result<Self>2757 fn parse(bytes: &[u8], group_id: GroupId, opcode: u8) -> Result<Self> { 2758 let payload: Vec<u8> = bytes[4..].into(); 2759 let child = match (group_id) { 2760 (GroupId::Core) if CoreResponseData::conforms(&bytes[..]) => { 2761 UciResponseDataChild::CoreResponse(Arc::new(CoreResponseData::parse( 2762 &bytes[..], 2763 opcode, 2764 )?)) 2765 } 2766 (GroupId::SessionConfig) if SessionResponseData::conforms(&bytes[..]) => { 2767 UciResponseDataChild::SessionResponse(Arc::new(SessionResponseData::parse( 2768 &bytes[..], 2769 opcode, 2770 )?)) 2771 } 2772 (GroupId::RangingSessionControl) if RangingResponseData::conforms(&bytes[..]) => { 2773 UciResponseDataChild::RangingResponse(Arc::new(RangingResponseData::parse( 2774 &bytes[..], 2775 opcode, 2776 )?)) 2777 } 2778 (GroupId::VendorAndroid) if AndroidResponseData::conforms(&bytes[..]) => { 2779 UciResponseDataChild::AndroidResponse(Arc::new(AndroidResponseData::parse( 2780 &bytes[..], 2781 opcode, 2782 )?)) 2783 } 2784 (GroupId::VendorReservedA) if UciVendor_A_ResponseData::conforms(&bytes[..]) => { 2785 UciResponseDataChild::UciVendor_A_Response(Arc::new( 2786 UciVendor_A_ResponseData::parse(&bytes[..])?, 2787 )) 2788 } 2789 (GroupId::VendorReservedB) if UciVendor_B_ResponseData::conforms(&bytes[..]) => { 2790 UciResponseDataChild::UciVendor_B_Response(Arc::new( 2791 UciVendor_B_ResponseData::parse(&bytes[..])?, 2792 )) 2793 } 2794 (GroupId::VendorReservedE) if UciVendor_E_ResponseData::conforms(&bytes[..]) => { 2795 UciResponseDataChild::UciVendor_E_Response(Arc::new( 2796 UciVendor_E_ResponseData::parse(&bytes[..])?, 2797 )) 2798 } 2799 (GroupId::VendorReservedF) if UciVendor_F_ResponseData::conforms(&bytes[..]) => { 2800 UciResponseDataChild::UciVendor_F_Response(Arc::new( 2801 UciVendor_F_ResponseData::parse(&bytes[..])?, 2802 )) 2803 } 2804 (_) => return Err(Error::InvalidPacketError), 2805 }; 2806 Ok(Self { child }) 2807 } write_to(&self, buffer: &mut BytesMut)2808 fn write_to(&self, buffer: &mut BytesMut) { 2809 match &self.child { 2810 UciResponseDataChild::CoreResponse(value) => value.write_to(buffer), 2811 UciResponseDataChild::SessionResponse(value) => value.write_to(buffer), 2812 UciResponseDataChild::RangingResponse(value) => value.write_to(buffer), 2813 UciResponseDataChild::AndroidResponse(value) => value.write_to(buffer), 2814 UciResponseDataChild::UciVendor_A_Response(value) => value.write_to(buffer), 2815 UciResponseDataChild::UciVendor_B_Response(value) => value.write_to(buffer), 2816 UciResponseDataChild::UciVendor_E_Response(value) => value.write_to(buffer), 2817 UciResponseDataChild::UciVendor_F_Response(value) => value.write_to(buffer), 2818 UciResponseDataChild::Payload(p) => buffer[4..].copy_from_slice(&p[..]), 2819 UciResponseDataChild::None => {} 2820 } 2821 } get_total_size(&self) -> usize2822 fn get_total_size(&self) -> usize { 2823 self.get_size() + self.child.get_total_size() 2824 } get_size(&self) -> usize2825 fn get_size(&self) -> usize { 2826 let ret = 0; 2827 ret 2828 } 2829 } 2830 impl Packet for UciResponsePacket { to_bytes(self) -> Bytes2831 fn to_bytes(self) -> Bytes { 2832 let mut buffer = BytesMut::new(); 2833 buffer.resize(self.uci_packet.get_total_size(), 0); 2834 self.uci_packet.write_to(&mut buffer); 2835 buffer.freeze() 2836 } to_vec(self) -> Vec<u8>2837 fn to_vec(self) -> Vec<u8> { 2838 self.to_bytes().to_vec() 2839 } 2840 } 2841 impl From<UciResponsePacket> for Bytes { from(packet: UciResponsePacket) -> Self2842 fn from(packet: UciResponsePacket) -> Self { 2843 packet.to_bytes() 2844 } 2845 } 2846 impl From<UciResponsePacket> for Vec<u8> { from(packet: UciResponsePacket) -> Self2847 fn from(packet: UciResponsePacket) -> Self { 2848 packet.to_vec() 2849 } 2850 } 2851 impl TryFrom<UciPacketPacket> for UciResponsePacket { 2852 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>2853 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 2854 Self::new(value.uci_packet).map_err(TryFromError) 2855 } 2856 } 2857 impl UciResponsePacket { specialize(&self) -> UciResponseChild2858 pub fn specialize(&self) -> UciResponseChild { 2859 match &self.uci_response.child { 2860 UciResponseDataChild::CoreResponse(_) => UciResponseChild::CoreResponse( 2861 CoreResponsePacket::new(self.uci_packet.clone()).unwrap(), 2862 ), 2863 UciResponseDataChild::SessionResponse(_) => UciResponseChild::SessionResponse( 2864 SessionResponsePacket::new(self.uci_packet.clone()).unwrap(), 2865 ), 2866 UciResponseDataChild::RangingResponse(_) => UciResponseChild::RangingResponse( 2867 RangingResponsePacket::new(self.uci_packet.clone()).unwrap(), 2868 ), 2869 UciResponseDataChild::AndroidResponse(_) => UciResponseChild::AndroidResponse( 2870 AndroidResponsePacket::new(self.uci_packet.clone()).unwrap(), 2871 ), 2872 UciResponseDataChild::UciVendor_A_Response(_) => { 2873 UciResponseChild::UciVendor_A_Response( 2874 UciVendor_A_ResponsePacket::new(self.uci_packet.clone()).unwrap(), 2875 ) 2876 } 2877 UciResponseDataChild::UciVendor_B_Response(_) => { 2878 UciResponseChild::UciVendor_B_Response( 2879 UciVendor_B_ResponsePacket::new(self.uci_packet.clone()).unwrap(), 2880 ) 2881 } 2882 UciResponseDataChild::UciVendor_E_Response(_) => { 2883 UciResponseChild::UciVendor_E_Response( 2884 UciVendor_E_ResponsePacket::new(self.uci_packet.clone()).unwrap(), 2885 ) 2886 } 2887 UciResponseDataChild::UciVendor_F_Response(_) => { 2888 UciResponseChild::UciVendor_F_Response( 2889 UciVendor_F_ResponsePacket::new(self.uci_packet.clone()).unwrap(), 2890 ) 2891 } 2892 UciResponseDataChild::Payload(p) => UciResponseChild::Payload(p.clone()), 2893 UciResponseDataChild::None => UciResponseChild::None, 2894 } 2895 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>2896 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 2897 let uci_packet = root; 2898 let uci_response = match &uci_packet.child { 2899 UciPacketDataChild::UciResponse(value) => (*value).clone(), 2900 _ => return Err("inconsistent state - child was not UciResponse"), 2901 }; 2902 Ok(Self { 2903 uci_packet, 2904 uci_response, 2905 }) 2906 } get_group_id(&self) -> GroupId2907 pub fn get_group_id(&self) -> GroupId { 2908 self.uci_packet.as_ref().group_id 2909 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag2910 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 2911 self.uci_packet.as_ref().packet_boundary_flag 2912 } get_message_type(&self) -> MessageType2913 pub fn get_message_type(&self) -> MessageType { 2914 self.uci_packet.as_ref().message_type 2915 } get_opcode(&self) -> u82916 pub fn get_opcode(&self) -> u8 { 2917 self.uci_packet.as_ref().opcode 2918 } 2919 } 2920 impl Into<UciPacketPacket> for UciResponsePacket { into(self) -> UciPacketPacket2921 fn into(self) -> UciPacketPacket { 2922 UciPacketPacket::new(self.uci_packet).unwrap() 2923 } 2924 } 2925 impl UciResponseBuilder { build(self) -> UciResponsePacket2926 pub fn build(self) -> UciResponsePacket { 2927 let uci_response = Arc::new(UciResponseData { 2928 child: match self.payload { 2929 None => UciResponseDataChild::None, 2930 Some(bytes) => UciResponseDataChild::Payload(bytes), 2931 }, 2932 }); 2933 let uci_packet = Arc::new(UciPacketData { 2934 group_id: self.group_id, 2935 packet_boundary_flag: PacketBoundaryFlag::Complete, 2936 message_type: MessageType::Response, 2937 opcode: self.opcode, 2938 child: UciPacketDataChild::UciResponse(uci_response), 2939 }); 2940 UciResponsePacket::new(uci_packet).unwrap() 2941 } 2942 } 2943 impl Into<UciPacketPacket> for UciResponseBuilder { into(self) -> UciPacketPacket2944 fn into(self) -> UciPacketPacket { 2945 self.build().into() 2946 } 2947 } 2948 2949 #[derive(Debug)] 2950 enum UciNotificationDataChild { 2951 CoreNotification(Arc<CoreNotificationData>), 2952 SessionNotification(Arc<SessionNotificationData>), 2953 RangingNotification(Arc<RangingNotificationData>), 2954 AndroidNotification(Arc<AndroidNotificationData>), 2955 UciVendor_A_Notification(Arc<UciVendor_A_NotificationData>), 2956 UciVendor_B_Notification(Arc<UciVendor_B_NotificationData>), 2957 UciVendor_E_Notification(Arc<UciVendor_E_NotificationData>), 2958 UciVendor_F_Notification(Arc<UciVendor_F_NotificationData>), 2959 Payload(Bytes), 2960 None, 2961 } 2962 impl UciNotificationDataChild { get_total_size(&self) -> usize2963 fn get_total_size(&self) -> usize { 2964 match self { 2965 UciNotificationDataChild::CoreNotification(value) => value.get_total_size(), 2966 UciNotificationDataChild::SessionNotification(value) => value.get_total_size(), 2967 UciNotificationDataChild::RangingNotification(value) => value.get_total_size(), 2968 UciNotificationDataChild::AndroidNotification(value) => value.get_total_size(), 2969 UciNotificationDataChild::UciVendor_A_Notification(value) => value.get_total_size(), 2970 UciNotificationDataChild::UciVendor_B_Notification(value) => value.get_total_size(), 2971 UciNotificationDataChild::UciVendor_E_Notification(value) => value.get_total_size(), 2972 UciNotificationDataChild::UciVendor_F_Notification(value) => value.get_total_size(), 2973 UciNotificationDataChild::Payload(p) => p.len(), 2974 UciNotificationDataChild::None => 0, 2975 } 2976 } 2977 } 2978 #[derive(Debug)] 2979 pub enum UciNotificationChild { 2980 CoreNotification(CoreNotificationPacket), 2981 SessionNotification(SessionNotificationPacket), 2982 RangingNotification(RangingNotificationPacket), 2983 AndroidNotification(AndroidNotificationPacket), 2984 UciVendor_A_Notification(UciVendor_A_NotificationPacket), 2985 UciVendor_B_Notification(UciVendor_B_NotificationPacket), 2986 UciVendor_E_Notification(UciVendor_E_NotificationPacket), 2987 UciVendor_F_Notification(UciVendor_F_NotificationPacket), 2988 Payload(Bytes), 2989 None, 2990 } 2991 #[derive(Debug)] 2992 struct UciNotificationData { 2993 child: UciNotificationDataChild, 2994 } 2995 #[derive(Debug, Clone)] 2996 pub struct UciNotificationPacket { 2997 uci_packet: Arc<UciPacketData>, 2998 uci_notification: Arc<UciNotificationData>, 2999 } 3000 #[derive(Debug)] 3001 pub struct UciNotificationBuilder { 3002 pub group_id: GroupId, 3003 pub opcode: u8, 3004 pub payload: Option<Bytes>, 3005 } 3006 impl UciNotificationData { conforms(bytes: &[u8]) -> bool3007 fn conforms(bytes: &[u8]) -> bool { 3008 if bytes.len() < 4 { 3009 return false; 3010 } 3011 true 3012 } parse(bytes: &[u8], group_id: GroupId, opcode: u8) -> Result<Self>3013 fn parse(bytes: &[u8], group_id: GroupId, opcode: u8) -> Result<Self> { 3014 let payload: Vec<u8> = bytes[4..].into(); 3015 let child = match (group_id) { 3016 (GroupId::Core) if CoreNotificationData::conforms(&bytes[..]) => { 3017 UciNotificationDataChild::CoreNotification(Arc::new(CoreNotificationData::parse( 3018 &bytes[..], 3019 opcode, 3020 )?)) 3021 } 3022 (GroupId::SessionConfig) if SessionNotificationData::conforms(&bytes[..]) => { 3023 UciNotificationDataChild::SessionNotification(Arc::new( 3024 SessionNotificationData::parse(&bytes[..], opcode)?, 3025 )) 3026 } 3027 (GroupId::RangingSessionControl) if RangingNotificationData::conforms(&bytes[..]) => { 3028 UciNotificationDataChild::RangingNotification(Arc::new( 3029 RangingNotificationData::parse(&bytes[..], opcode)?, 3030 )) 3031 } 3032 (GroupId::VendorAndroid) if AndroidNotificationData::conforms(&bytes[..]) => { 3033 UciNotificationDataChild::AndroidNotification(Arc::new( 3034 AndroidNotificationData::parse(&bytes[..])?, 3035 )) 3036 } 3037 (GroupId::VendorReservedA) if UciVendor_A_NotificationData::conforms(&bytes[..]) => { 3038 UciNotificationDataChild::UciVendor_A_Notification(Arc::new( 3039 UciVendor_A_NotificationData::parse(&bytes[..])?, 3040 )) 3041 } 3042 (GroupId::VendorReservedB) if UciVendor_B_NotificationData::conforms(&bytes[..]) => { 3043 UciNotificationDataChild::UciVendor_B_Notification(Arc::new( 3044 UciVendor_B_NotificationData::parse(&bytes[..])?, 3045 )) 3046 } 3047 (GroupId::VendorReservedE) if UciVendor_E_NotificationData::conforms(&bytes[..]) => { 3048 UciNotificationDataChild::UciVendor_E_Notification(Arc::new( 3049 UciVendor_E_NotificationData::parse(&bytes[..])?, 3050 )) 3051 } 3052 (GroupId::VendorReservedF) if UciVendor_F_NotificationData::conforms(&bytes[..]) => { 3053 UciNotificationDataChild::UciVendor_F_Notification(Arc::new( 3054 UciVendor_F_NotificationData::parse(&bytes[..])?, 3055 )) 3056 } 3057 (_) => return Err(Error::InvalidPacketError), 3058 }; 3059 Ok(Self { child }) 3060 } write_to(&self, buffer: &mut BytesMut)3061 fn write_to(&self, buffer: &mut BytesMut) { 3062 match &self.child { 3063 UciNotificationDataChild::CoreNotification(value) => value.write_to(buffer), 3064 UciNotificationDataChild::SessionNotification(value) => value.write_to(buffer), 3065 UciNotificationDataChild::RangingNotification(value) => value.write_to(buffer), 3066 UciNotificationDataChild::AndroidNotification(value) => value.write_to(buffer), 3067 UciNotificationDataChild::UciVendor_A_Notification(value) => value.write_to(buffer), 3068 UciNotificationDataChild::UciVendor_B_Notification(value) => value.write_to(buffer), 3069 UciNotificationDataChild::UciVendor_E_Notification(value) => value.write_to(buffer), 3070 UciNotificationDataChild::UciVendor_F_Notification(value) => value.write_to(buffer), 3071 UciNotificationDataChild::Payload(p) => buffer[4..].copy_from_slice(&p[..]), 3072 UciNotificationDataChild::None => {} 3073 } 3074 } get_total_size(&self) -> usize3075 fn get_total_size(&self) -> usize { 3076 self.get_size() + self.child.get_total_size() 3077 } get_size(&self) -> usize3078 fn get_size(&self) -> usize { 3079 let ret = 0; 3080 ret 3081 } 3082 } 3083 impl Packet for UciNotificationPacket { to_bytes(self) -> Bytes3084 fn to_bytes(self) -> Bytes { 3085 let mut buffer = BytesMut::new(); 3086 buffer.resize(self.uci_packet.get_total_size(), 0); 3087 self.uci_packet.write_to(&mut buffer); 3088 buffer.freeze() 3089 } to_vec(self) -> Vec<u8>3090 fn to_vec(self) -> Vec<u8> { 3091 self.to_bytes().to_vec() 3092 } 3093 } 3094 impl From<UciNotificationPacket> for Bytes { from(packet: UciNotificationPacket) -> Self3095 fn from(packet: UciNotificationPacket) -> Self { 3096 packet.to_bytes() 3097 } 3098 } 3099 impl From<UciNotificationPacket> for Vec<u8> { from(packet: UciNotificationPacket) -> Self3100 fn from(packet: UciNotificationPacket) -> Self { 3101 packet.to_vec() 3102 } 3103 } 3104 impl TryFrom<UciPacketPacket> for UciNotificationPacket { 3105 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>3106 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 3107 Self::new(value.uci_packet).map_err(TryFromError) 3108 } 3109 } 3110 impl UciNotificationPacket { specialize(&self) -> UciNotificationChild3111 pub fn specialize(&self) -> UciNotificationChild { 3112 match &self.uci_notification.child { 3113 UciNotificationDataChild::CoreNotification(_) => { 3114 UciNotificationChild::CoreNotification( 3115 CoreNotificationPacket::new(self.uci_packet.clone()).unwrap(), 3116 ) 3117 } 3118 UciNotificationDataChild::SessionNotification(_) => { 3119 UciNotificationChild::SessionNotification( 3120 SessionNotificationPacket::new(self.uci_packet.clone()).unwrap(), 3121 ) 3122 } 3123 UciNotificationDataChild::RangingNotification(_) => { 3124 UciNotificationChild::RangingNotification( 3125 RangingNotificationPacket::new(self.uci_packet.clone()).unwrap(), 3126 ) 3127 } 3128 UciNotificationDataChild::AndroidNotification(_) => { 3129 UciNotificationChild::AndroidNotification( 3130 AndroidNotificationPacket::new(self.uci_packet.clone()).unwrap(), 3131 ) 3132 } 3133 UciNotificationDataChild::UciVendor_A_Notification(_) => { 3134 UciNotificationChild::UciVendor_A_Notification( 3135 UciVendor_A_NotificationPacket::new(self.uci_packet.clone()).unwrap(), 3136 ) 3137 } 3138 UciNotificationDataChild::UciVendor_B_Notification(_) => { 3139 UciNotificationChild::UciVendor_B_Notification( 3140 UciVendor_B_NotificationPacket::new(self.uci_packet.clone()).unwrap(), 3141 ) 3142 } 3143 UciNotificationDataChild::UciVendor_E_Notification(_) => { 3144 UciNotificationChild::UciVendor_E_Notification( 3145 UciVendor_E_NotificationPacket::new(self.uci_packet.clone()).unwrap(), 3146 ) 3147 } 3148 UciNotificationDataChild::UciVendor_F_Notification(_) => { 3149 UciNotificationChild::UciVendor_F_Notification( 3150 UciVendor_F_NotificationPacket::new(self.uci_packet.clone()).unwrap(), 3151 ) 3152 } 3153 UciNotificationDataChild::Payload(p) => UciNotificationChild::Payload(p.clone()), 3154 UciNotificationDataChild::None => UciNotificationChild::None, 3155 } 3156 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>3157 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 3158 let uci_packet = root; 3159 let uci_notification = match &uci_packet.child { 3160 UciPacketDataChild::UciNotification(value) => (*value).clone(), 3161 _ => return Err("inconsistent state - child was not UciNotification"), 3162 }; 3163 Ok(Self { 3164 uci_packet, 3165 uci_notification, 3166 }) 3167 } get_group_id(&self) -> GroupId3168 pub fn get_group_id(&self) -> GroupId { 3169 self.uci_packet.as_ref().group_id 3170 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag3171 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 3172 self.uci_packet.as_ref().packet_boundary_flag 3173 } get_message_type(&self) -> MessageType3174 pub fn get_message_type(&self) -> MessageType { 3175 self.uci_packet.as_ref().message_type 3176 } get_opcode(&self) -> u83177 pub fn get_opcode(&self) -> u8 { 3178 self.uci_packet.as_ref().opcode 3179 } 3180 } 3181 impl Into<UciPacketPacket> for UciNotificationPacket { into(self) -> UciPacketPacket3182 fn into(self) -> UciPacketPacket { 3183 UciPacketPacket::new(self.uci_packet).unwrap() 3184 } 3185 } 3186 impl UciNotificationBuilder { build(self) -> UciNotificationPacket3187 pub fn build(self) -> UciNotificationPacket { 3188 let uci_notification = Arc::new(UciNotificationData { 3189 child: match self.payload { 3190 None => UciNotificationDataChild::None, 3191 Some(bytes) => UciNotificationDataChild::Payload(bytes), 3192 }, 3193 }); 3194 let uci_packet = Arc::new(UciPacketData { 3195 group_id: self.group_id, 3196 packet_boundary_flag: PacketBoundaryFlag::Complete, 3197 message_type: MessageType::Notification, 3198 opcode: self.opcode, 3199 child: UciPacketDataChild::UciNotification(uci_notification), 3200 }); 3201 UciNotificationPacket::new(uci_packet).unwrap() 3202 } 3203 } 3204 impl Into<UciPacketPacket> for UciNotificationBuilder { into(self) -> UciPacketPacket3205 fn into(self) -> UciPacketPacket { 3206 self.build().into() 3207 } 3208 } 3209 3210 #[derive(Debug)] 3211 enum CoreCommandDataChild { 3212 DeviceResetCmd(Arc<DeviceResetCmdData>), 3213 GetDeviceInfoCmd(Arc<GetDeviceInfoCmdData>), 3214 GetCapsInfoCmd(Arc<GetCapsInfoCmdData>), 3215 SetConfigCmd(Arc<SetConfigCmdData>), 3216 GetConfigCmd(Arc<GetConfigCmdData>), 3217 None, 3218 } 3219 impl CoreCommandDataChild { get_total_size(&self) -> usize3220 fn get_total_size(&self) -> usize { 3221 match self { 3222 CoreCommandDataChild::DeviceResetCmd(value) => value.get_total_size(), 3223 CoreCommandDataChild::GetDeviceInfoCmd(value) => value.get_total_size(), 3224 CoreCommandDataChild::GetCapsInfoCmd(value) => value.get_total_size(), 3225 CoreCommandDataChild::SetConfigCmd(value) => value.get_total_size(), 3226 CoreCommandDataChild::GetConfigCmd(value) => value.get_total_size(), 3227 CoreCommandDataChild::None => 0, 3228 } 3229 } 3230 } 3231 #[derive(Debug)] 3232 pub enum CoreCommandChild { 3233 DeviceResetCmd(DeviceResetCmdPacket), 3234 GetDeviceInfoCmd(GetDeviceInfoCmdPacket), 3235 GetCapsInfoCmd(GetCapsInfoCmdPacket), 3236 SetConfigCmd(SetConfigCmdPacket), 3237 GetConfigCmd(GetConfigCmdPacket), 3238 None, 3239 } 3240 #[derive(Debug)] 3241 struct CoreCommandData { 3242 child: CoreCommandDataChild, 3243 } 3244 #[derive(Debug, Clone)] 3245 pub struct CoreCommandPacket { 3246 uci_packet: Arc<UciPacketData>, 3247 uci_command: Arc<UciCommandData>, 3248 core_command: Arc<CoreCommandData>, 3249 } 3250 #[derive(Debug)] 3251 pub struct CoreCommandBuilder { 3252 pub opcode: u8, 3253 } 3254 impl CoreCommandData { conforms(bytes: &[u8]) -> bool3255 fn conforms(bytes: &[u8]) -> bool { 3256 if bytes.len() < 4 { 3257 return false; 3258 } 3259 true 3260 } parse(bytes: &[u8], opcode: u8) -> Result<Self>3261 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 3262 let child = match (opcode) { 3263 (0) if DeviceResetCmdData::conforms(&bytes[..]) => { 3264 CoreCommandDataChild::DeviceResetCmd(Arc::new(DeviceResetCmdData::parse( 3265 &bytes[..], 3266 )?)) 3267 } 3268 (2) if GetDeviceInfoCmdData::conforms(&bytes[..]) => { 3269 CoreCommandDataChild::GetDeviceInfoCmd(Arc::new(GetDeviceInfoCmdData::parse( 3270 &bytes[..], 3271 )?)) 3272 } 3273 (3) if GetCapsInfoCmdData::conforms(&bytes[..]) => { 3274 CoreCommandDataChild::GetCapsInfoCmd(Arc::new(GetCapsInfoCmdData::parse( 3275 &bytes[..], 3276 )?)) 3277 } 3278 (4) if SetConfigCmdData::conforms(&bytes[..]) => { 3279 CoreCommandDataChild::SetConfigCmd(Arc::new(SetConfigCmdData::parse(&bytes[..])?)) 3280 } 3281 (5) if GetConfigCmdData::conforms(&bytes[..]) => { 3282 CoreCommandDataChild::GetConfigCmd(Arc::new(GetConfigCmdData::parse(&bytes[..])?)) 3283 } 3284 (_) => return Err(Error::InvalidPacketError), 3285 }; 3286 Ok(Self { child }) 3287 } write_to(&self, buffer: &mut BytesMut)3288 fn write_to(&self, buffer: &mut BytesMut) { 3289 match &self.child { 3290 CoreCommandDataChild::DeviceResetCmd(value) => value.write_to(buffer), 3291 CoreCommandDataChild::GetDeviceInfoCmd(value) => value.write_to(buffer), 3292 CoreCommandDataChild::GetCapsInfoCmd(value) => value.write_to(buffer), 3293 CoreCommandDataChild::SetConfigCmd(value) => value.write_to(buffer), 3294 CoreCommandDataChild::GetConfigCmd(value) => value.write_to(buffer), 3295 CoreCommandDataChild::None => {} 3296 } 3297 } get_total_size(&self) -> usize3298 fn get_total_size(&self) -> usize { 3299 self.get_size() + self.child.get_total_size() 3300 } get_size(&self) -> usize3301 fn get_size(&self) -> usize { 3302 let ret = 0; 3303 ret 3304 } 3305 } 3306 impl Packet for CoreCommandPacket { to_bytes(self) -> Bytes3307 fn to_bytes(self) -> Bytes { 3308 let mut buffer = BytesMut::new(); 3309 buffer.resize(self.uci_packet.get_total_size(), 0); 3310 self.uci_packet.write_to(&mut buffer); 3311 buffer.freeze() 3312 } to_vec(self) -> Vec<u8>3313 fn to_vec(self) -> Vec<u8> { 3314 self.to_bytes().to_vec() 3315 } 3316 } 3317 impl From<CoreCommandPacket> for Bytes { from(packet: CoreCommandPacket) -> Self3318 fn from(packet: CoreCommandPacket) -> Self { 3319 packet.to_bytes() 3320 } 3321 } 3322 impl From<CoreCommandPacket> for Vec<u8> { from(packet: CoreCommandPacket) -> Self3323 fn from(packet: CoreCommandPacket) -> Self { 3324 packet.to_vec() 3325 } 3326 } 3327 impl TryFrom<UciPacketPacket> for CoreCommandPacket { 3328 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>3329 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 3330 Self::new(value.uci_packet).map_err(TryFromError) 3331 } 3332 } 3333 impl CoreCommandPacket { specialize(&self) -> CoreCommandChild3334 pub fn specialize(&self) -> CoreCommandChild { 3335 match &self.core_command.child { 3336 CoreCommandDataChild::DeviceResetCmd(_) => CoreCommandChild::DeviceResetCmd( 3337 DeviceResetCmdPacket::new(self.uci_packet.clone()).unwrap(), 3338 ), 3339 CoreCommandDataChild::GetDeviceInfoCmd(_) => CoreCommandChild::GetDeviceInfoCmd( 3340 GetDeviceInfoCmdPacket::new(self.uci_packet.clone()).unwrap(), 3341 ), 3342 CoreCommandDataChild::GetCapsInfoCmd(_) => CoreCommandChild::GetCapsInfoCmd( 3343 GetCapsInfoCmdPacket::new(self.uci_packet.clone()).unwrap(), 3344 ), 3345 CoreCommandDataChild::SetConfigCmd(_) => CoreCommandChild::SetConfigCmd( 3346 SetConfigCmdPacket::new(self.uci_packet.clone()).unwrap(), 3347 ), 3348 CoreCommandDataChild::GetConfigCmd(_) => CoreCommandChild::GetConfigCmd( 3349 GetConfigCmdPacket::new(self.uci_packet.clone()).unwrap(), 3350 ), 3351 CoreCommandDataChild::None => CoreCommandChild::None, 3352 } 3353 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>3354 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 3355 let uci_packet = root; 3356 let uci_command = match &uci_packet.child { 3357 UciPacketDataChild::UciCommand(value) => (*value).clone(), 3358 _ => return Err("inconsistent state - child was not UciCommand"), 3359 }; 3360 let core_command = match &uci_command.child { 3361 UciCommandDataChild::CoreCommand(value) => (*value).clone(), 3362 _ => return Err("inconsistent state - child was not CoreCommand"), 3363 }; 3364 Ok(Self { 3365 uci_packet, 3366 uci_command, 3367 core_command, 3368 }) 3369 } get_group_id(&self) -> GroupId3370 pub fn get_group_id(&self) -> GroupId { 3371 self.uci_packet.as_ref().group_id 3372 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag3373 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 3374 self.uci_packet.as_ref().packet_boundary_flag 3375 } get_message_type(&self) -> MessageType3376 pub fn get_message_type(&self) -> MessageType { 3377 self.uci_packet.as_ref().message_type 3378 } get_opcode(&self) -> u83379 pub fn get_opcode(&self) -> u8 { 3380 self.uci_packet.as_ref().opcode 3381 } 3382 } 3383 impl Into<UciPacketPacket> for CoreCommandPacket { into(self) -> UciPacketPacket3384 fn into(self) -> UciPacketPacket { 3385 UciPacketPacket::new(self.uci_packet).unwrap() 3386 } 3387 } 3388 impl Into<UciCommandPacket> for CoreCommandPacket { into(self) -> UciCommandPacket3389 fn into(self) -> UciCommandPacket { 3390 UciCommandPacket::new(self.uci_packet).unwrap() 3391 } 3392 } 3393 impl CoreCommandBuilder { build(self) -> CoreCommandPacket3394 pub fn build(self) -> CoreCommandPacket { 3395 let core_command = Arc::new(CoreCommandData { 3396 child: CoreCommandDataChild::None, 3397 }); 3398 let uci_command = Arc::new(UciCommandData { 3399 child: UciCommandDataChild::CoreCommand(core_command), 3400 }); 3401 let uci_packet = Arc::new(UciPacketData { 3402 group_id: GroupId::Core, 3403 packet_boundary_flag: PacketBoundaryFlag::Complete, 3404 message_type: MessageType::Command, 3405 opcode: self.opcode, 3406 child: UciPacketDataChild::UciCommand(uci_command), 3407 }); 3408 CoreCommandPacket::new(uci_packet).unwrap() 3409 } 3410 } 3411 impl Into<UciPacketPacket> for CoreCommandBuilder { into(self) -> UciPacketPacket3412 fn into(self) -> UciPacketPacket { 3413 self.build().into() 3414 } 3415 } 3416 impl Into<UciCommandPacket> for CoreCommandBuilder { into(self) -> UciCommandPacket3417 fn into(self) -> UciCommandPacket { 3418 self.build().into() 3419 } 3420 } 3421 3422 #[derive(Debug)] 3423 enum CoreResponseDataChild { 3424 DeviceResetRsp(Arc<DeviceResetRspData>), 3425 GetDeviceInfoRsp(Arc<GetDeviceInfoRspData>), 3426 GetCapsInfoRsp(Arc<GetCapsInfoRspData>), 3427 SetConfigRsp(Arc<SetConfigRspData>), 3428 GetConfigRsp(Arc<GetConfigRspData>), 3429 None, 3430 } 3431 impl CoreResponseDataChild { get_total_size(&self) -> usize3432 fn get_total_size(&self) -> usize { 3433 match self { 3434 CoreResponseDataChild::DeviceResetRsp(value) => value.get_total_size(), 3435 CoreResponseDataChild::GetDeviceInfoRsp(value) => value.get_total_size(), 3436 CoreResponseDataChild::GetCapsInfoRsp(value) => value.get_total_size(), 3437 CoreResponseDataChild::SetConfigRsp(value) => value.get_total_size(), 3438 CoreResponseDataChild::GetConfigRsp(value) => value.get_total_size(), 3439 CoreResponseDataChild::None => 0, 3440 } 3441 } 3442 } 3443 #[derive(Debug)] 3444 pub enum CoreResponseChild { 3445 DeviceResetRsp(DeviceResetRspPacket), 3446 GetDeviceInfoRsp(GetDeviceInfoRspPacket), 3447 GetCapsInfoRsp(GetCapsInfoRspPacket), 3448 SetConfigRsp(SetConfigRspPacket), 3449 GetConfigRsp(GetConfigRspPacket), 3450 None, 3451 } 3452 #[derive(Debug)] 3453 struct CoreResponseData { 3454 child: CoreResponseDataChild, 3455 } 3456 #[derive(Debug, Clone)] 3457 pub struct CoreResponsePacket { 3458 uci_packet: Arc<UciPacketData>, 3459 uci_response: Arc<UciResponseData>, 3460 core_response: Arc<CoreResponseData>, 3461 } 3462 #[derive(Debug)] 3463 pub struct CoreResponseBuilder { 3464 pub opcode: u8, 3465 } 3466 impl CoreResponseData { conforms(bytes: &[u8]) -> bool3467 fn conforms(bytes: &[u8]) -> bool { 3468 if bytes.len() < 4 { 3469 return false; 3470 } 3471 true 3472 } parse(bytes: &[u8], opcode: u8) -> Result<Self>3473 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 3474 let child = match (opcode) { 3475 (0) if DeviceResetRspData::conforms(&bytes[..]) => { 3476 CoreResponseDataChild::DeviceResetRsp(Arc::new(DeviceResetRspData::parse( 3477 &bytes[..], 3478 )?)) 3479 } 3480 (2) if GetDeviceInfoRspData::conforms(&bytes[..]) => { 3481 CoreResponseDataChild::GetDeviceInfoRsp(Arc::new(GetDeviceInfoRspData::parse( 3482 &bytes[..], 3483 )?)) 3484 } 3485 (3) if GetCapsInfoRspData::conforms(&bytes[..]) => { 3486 CoreResponseDataChild::GetCapsInfoRsp(Arc::new(GetCapsInfoRspData::parse( 3487 &bytes[..], 3488 )?)) 3489 } 3490 (4) if SetConfigRspData::conforms(&bytes[..]) => { 3491 CoreResponseDataChild::SetConfigRsp(Arc::new(SetConfigRspData::parse(&bytes[..])?)) 3492 } 3493 (5) if GetConfigRspData::conforms(&bytes[..]) => { 3494 CoreResponseDataChild::GetConfigRsp(Arc::new(GetConfigRspData::parse(&bytes[..])?)) 3495 } 3496 (_) => return Err(Error::InvalidPacketError), 3497 }; 3498 Ok(Self { child }) 3499 } write_to(&self, buffer: &mut BytesMut)3500 fn write_to(&self, buffer: &mut BytesMut) { 3501 match &self.child { 3502 CoreResponseDataChild::DeviceResetRsp(value) => value.write_to(buffer), 3503 CoreResponseDataChild::GetDeviceInfoRsp(value) => value.write_to(buffer), 3504 CoreResponseDataChild::GetCapsInfoRsp(value) => value.write_to(buffer), 3505 CoreResponseDataChild::SetConfigRsp(value) => value.write_to(buffer), 3506 CoreResponseDataChild::GetConfigRsp(value) => value.write_to(buffer), 3507 CoreResponseDataChild::None => {} 3508 } 3509 } get_total_size(&self) -> usize3510 fn get_total_size(&self) -> usize { 3511 self.get_size() + self.child.get_total_size() 3512 } get_size(&self) -> usize3513 fn get_size(&self) -> usize { 3514 let ret = 0; 3515 ret 3516 } 3517 } 3518 impl Packet for CoreResponsePacket { to_bytes(self) -> Bytes3519 fn to_bytes(self) -> Bytes { 3520 let mut buffer = BytesMut::new(); 3521 buffer.resize(self.uci_packet.get_total_size(), 0); 3522 self.uci_packet.write_to(&mut buffer); 3523 buffer.freeze() 3524 } to_vec(self) -> Vec<u8>3525 fn to_vec(self) -> Vec<u8> { 3526 self.to_bytes().to_vec() 3527 } 3528 } 3529 impl From<CoreResponsePacket> for Bytes { from(packet: CoreResponsePacket) -> Self3530 fn from(packet: CoreResponsePacket) -> Self { 3531 packet.to_bytes() 3532 } 3533 } 3534 impl From<CoreResponsePacket> for Vec<u8> { from(packet: CoreResponsePacket) -> Self3535 fn from(packet: CoreResponsePacket) -> Self { 3536 packet.to_vec() 3537 } 3538 } 3539 impl TryFrom<UciPacketPacket> for CoreResponsePacket { 3540 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>3541 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 3542 Self::new(value.uci_packet).map_err(TryFromError) 3543 } 3544 } 3545 impl CoreResponsePacket { specialize(&self) -> CoreResponseChild3546 pub fn specialize(&self) -> CoreResponseChild { 3547 match &self.core_response.child { 3548 CoreResponseDataChild::DeviceResetRsp(_) => CoreResponseChild::DeviceResetRsp( 3549 DeviceResetRspPacket::new(self.uci_packet.clone()).unwrap(), 3550 ), 3551 CoreResponseDataChild::GetDeviceInfoRsp(_) => CoreResponseChild::GetDeviceInfoRsp( 3552 GetDeviceInfoRspPacket::new(self.uci_packet.clone()).unwrap(), 3553 ), 3554 CoreResponseDataChild::GetCapsInfoRsp(_) => CoreResponseChild::GetCapsInfoRsp( 3555 GetCapsInfoRspPacket::new(self.uci_packet.clone()).unwrap(), 3556 ), 3557 CoreResponseDataChild::SetConfigRsp(_) => CoreResponseChild::SetConfigRsp( 3558 SetConfigRspPacket::new(self.uci_packet.clone()).unwrap(), 3559 ), 3560 CoreResponseDataChild::GetConfigRsp(_) => CoreResponseChild::GetConfigRsp( 3561 GetConfigRspPacket::new(self.uci_packet.clone()).unwrap(), 3562 ), 3563 CoreResponseDataChild::None => CoreResponseChild::None, 3564 } 3565 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>3566 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 3567 let uci_packet = root; 3568 let uci_response = match &uci_packet.child { 3569 UciPacketDataChild::UciResponse(value) => (*value).clone(), 3570 _ => return Err("inconsistent state - child was not UciResponse"), 3571 }; 3572 let core_response = match &uci_response.child { 3573 UciResponseDataChild::CoreResponse(value) => (*value).clone(), 3574 _ => return Err("inconsistent state - child was not CoreResponse"), 3575 }; 3576 Ok(Self { 3577 uci_packet, 3578 uci_response, 3579 core_response, 3580 }) 3581 } get_group_id(&self) -> GroupId3582 pub fn get_group_id(&self) -> GroupId { 3583 self.uci_packet.as_ref().group_id 3584 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag3585 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 3586 self.uci_packet.as_ref().packet_boundary_flag 3587 } get_message_type(&self) -> MessageType3588 pub fn get_message_type(&self) -> MessageType { 3589 self.uci_packet.as_ref().message_type 3590 } get_opcode(&self) -> u83591 pub fn get_opcode(&self) -> u8 { 3592 self.uci_packet.as_ref().opcode 3593 } 3594 } 3595 impl Into<UciPacketPacket> for CoreResponsePacket { into(self) -> UciPacketPacket3596 fn into(self) -> UciPacketPacket { 3597 UciPacketPacket::new(self.uci_packet).unwrap() 3598 } 3599 } 3600 impl Into<UciResponsePacket> for CoreResponsePacket { into(self) -> UciResponsePacket3601 fn into(self) -> UciResponsePacket { 3602 UciResponsePacket::new(self.uci_packet).unwrap() 3603 } 3604 } 3605 impl CoreResponseBuilder { build(self) -> CoreResponsePacket3606 pub fn build(self) -> CoreResponsePacket { 3607 let core_response = Arc::new(CoreResponseData { 3608 child: CoreResponseDataChild::None, 3609 }); 3610 let uci_response = Arc::new(UciResponseData { 3611 child: UciResponseDataChild::CoreResponse(core_response), 3612 }); 3613 let uci_packet = Arc::new(UciPacketData { 3614 group_id: GroupId::Core, 3615 packet_boundary_flag: PacketBoundaryFlag::Complete, 3616 message_type: MessageType::Response, 3617 opcode: self.opcode, 3618 child: UciPacketDataChild::UciResponse(uci_response), 3619 }); 3620 CoreResponsePacket::new(uci_packet).unwrap() 3621 } 3622 } 3623 impl Into<UciPacketPacket> for CoreResponseBuilder { into(self) -> UciPacketPacket3624 fn into(self) -> UciPacketPacket { 3625 self.build().into() 3626 } 3627 } 3628 impl Into<UciResponsePacket> for CoreResponseBuilder { into(self) -> UciResponsePacket3629 fn into(self) -> UciResponsePacket { 3630 self.build().into() 3631 } 3632 } 3633 3634 #[derive(Debug)] 3635 enum CoreNotificationDataChild { 3636 DeviceStatusNtf(Arc<DeviceStatusNtfData>), 3637 GenericError(Arc<GenericErrorData>), 3638 None, 3639 } 3640 impl CoreNotificationDataChild { get_total_size(&self) -> usize3641 fn get_total_size(&self) -> usize { 3642 match self { 3643 CoreNotificationDataChild::DeviceStatusNtf(value) => value.get_total_size(), 3644 CoreNotificationDataChild::GenericError(value) => value.get_total_size(), 3645 CoreNotificationDataChild::None => 0, 3646 } 3647 } 3648 } 3649 #[derive(Debug)] 3650 pub enum CoreNotificationChild { 3651 DeviceStatusNtf(DeviceStatusNtfPacket), 3652 GenericError(GenericErrorPacket), 3653 None, 3654 } 3655 #[derive(Debug)] 3656 struct CoreNotificationData { 3657 child: CoreNotificationDataChild, 3658 } 3659 #[derive(Debug, Clone)] 3660 pub struct CoreNotificationPacket { 3661 uci_packet: Arc<UciPacketData>, 3662 uci_notification: Arc<UciNotificationData>, 3663 core_notification: Arc<CoreNotificationData>, 3664 } 3665 #[derive(Debug)] 3666 pub struct CoreNotificationBuilder { 3667 pub opcode: u8, 3668 } 3669 impl CoreNotificationData { conforms(bytes: &[u8]) -> bool3670 fn conforms(bytes: &[u8]) -> bool { 3671 if bytes.len() < 4 { 3672 return false; 3673 } 3674 true 3675 } parse(bytes: &[u8], opcode: u8) -> Result<Self>3676 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 3677 let child = match (opcode) { 3678 (1) if DeviceStatusNtfData::conforms(&bytes[..]) => { 3679 CoreNotificationDataChild::DeviceStatusNtf(Arc::new(DeviceStatusNtfData::parse( 3680 &bytes[..], 3681 )?)) 3682 } 3683 (7) if GenericErrorData::conforms(&bytes[..]) => { 3684 CoreNotificationDataChild::GenericError(Arc::new(GenericErrorData::parse( 3685 &bytes[..], 3686 )?)) 3687 } 3688 (_) => return Err(Error::InvalidPacketError), 3689 }; 3690 Ok(Self { child }) 3691 } write_to(&self, buffer: &mut BytesMut)3692 fn write_to(&self, buffer: &mut BytesMut) { 3693 match &self.child { 3694 CoreNotificationDataChild::DeviceStatusNtf(value) => value.write_to(buffer), 3695 CoreNotificationDataChild::GenericError(value) => value.write_to(buffer), 3696 CoreNotificationDataChild::None => {} 3697 } 3698 } get_total_size(&self) -> usize3699 fn get_total_size(&self) -> usize { 3700 self.get_size() + self.child.get_total_size() 3701 } get_size(&self) -> usize3702 fn get_size(&self) -> usize { 3703 let ret = 0; 3704 ret 3705 } 3706 } 3707 impl Packet for CoreNotificationPacket { to_bytes(self) -> Bytes3708 fn to_bytes(self) -> Bytes { 3709 let mut buffer = BytesMut::new(); 3710 buffer.resize(self.uci_packet.get_total_size(), 0); 3711 self.uci_packet.write_to(&mut buffer); 3712 buffer.freeze() 3713 } to_vec(self) -> Vec<u8>3714 fn to_vec(self) -> Vec<u8> { 3715 self.to_bytes().to_vec() 3716 } 3717 } 3718 impl From<CoreNotificationPacket> for Bytes { from(packet: CoreNotificationPacket) -> Self3719 fn from(packet: CoreNotificationPacket) -> Self { 3720 packet.to_bytes() 3721 } 3722 } 3723 impl From<CoreNotificationPacket> for Vec<u8> { from(packet: CoreNotificationPacket) -> Self3724 fn from(packet: CoreNotificationPacket) -> Self { 3725 packet.to_vec() 3726 } 3727 } 3728 impl TryFrom<UciPacketPacket> for CoreNotificationPacket { 3729 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>3730 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 3731 Self::new(value.uci_packet).map_err(TryFromError) 3732 } 3733 } 3734 impl CoreNotificationPacket { specialize(&self) -> CoreNotificationChild3735 pub fn specialize(&self) -> CoreNotificationChild { 3736 match &self.core_notification.child { 3737 CoreNotificationDataChild::DeviceStatusNtf(_) => { 3738 CoreNotificationChild::DeviceStatusNtf( 3739 DeviceStatusNtfPacket::new(self.uci_packet.clone()).unwrap(), 3740 ) 3741 } 3742 CoreNotificationDataChild::GenericError(_) => CoreNotificationChild::GenericError( 3743 GenericErrorPacket::new(self.uci_packet.clone()).unwrap(), 3744 ), 3745 CoreNotificationDataChild::None => CoreNotificationChild::None, 3746 } 3747 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>3748 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 3749 let uci_packet = root; 3750 let uci_notification = match &uci_packet.child { 3751 UciPacketDataChild::UciNotification(value) => (*value).clone(), 3752 _ => return Err("inconsistent state - child was not UciNotification"), 3753 }; 3754 let core_notification = match &uci_notification.child { 3755 UciNotificationDataChild::CoreNotification(value) => (*value).clone(), 3756 _ => return Err("inconsistent state - child was not CoreNotification"), 3757 }; 3758 Ok(Self { 3759 uci_packet, 3760 uci_notification, 3761 core_notification, 3762 }) 3763 } get_group_id(&self) -> GroupId3764 pub fn get_group_id(&self) -> GroupId { 3765 self.uci_packet.as_ref().group_id 3766 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag3767 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 3768 self.uci_packet.as_ref().packet_boundary_flag 3769 } get_message_type(&self) -> MessageType3770 pub fn get_message_type(&self) -> MessageType { 3771 self.uci_packet.as_ref().message_type 3772 } get_opcode(&self) -> u83773 pub fn get_opcode(&self) -> u8 { 3774 self.uci_packet.as_ref().opcode 3775 } 3776 } 3777 impl Into<UciPacketPacket> for CoreNotificationPacket { into(self) -> UciPacketPacket3778 fn into(self) -> UciPacketPacket { 3779 UciPacketPacket::new(self.uci_packet).unwrap() 3780 } 3781 } 3782 impl Into<UciNotificationPacket> for CoreNotificationPacket { into(self) -> UciNotificationPacket3783 fn into(self) -> UciNotificationPacket { 3784 UciNotificationPacket::new(self.uci_packet).unwrap() 3785 } 3786 } 3787 impl CoreNotificationBuilder { build(self) -> CoreNotificationPacket3788 pub fn build(self) -> CoreNotificationPacket { 3789 let core_notification = Arc::new(CoreNotificationData { 3790 child: CoreNotificationDataChild::None, 3791 }); 3792 let uci_notification = Arc::new(UciNotificationData { 3793 child: UciNotificationDataChild::CoreNotification(core_notification), 3794 }); 3795 let uci_packet = Arc::new(UciPacketData { 3796 group_id: GroupId::Core, 3797 packet_boundary_flag: PacketBoundaryFlag::Complete, 3798 message_type: MessageType::Notification, 3799 opcode: self.opcode, 3800 child: UciPacketDataChild::UciNotification(uci_notification), 3801 }); 3802 CoreNotificationPacket::new(uci_packet).unwrap() 3803 } 3804 } 3805 impl Into<UciPacketPacket> for CoreNotificationBuilder { into(self) -> UciPacketPacket3806 fn into(self) -> UciPacketPacket { 3807 self.build().into() 3808 } 3809 } 3810 impl Into<UciNotificationPacket> for CoreNotificationBuilder { into(self) -> UciNotificationPacket3811 fn into(self) -> UciNotificationPacket { 3812 self.build().into() 3813 } 3814 } 3815 3816 #[derive(Debug)] 3817 enum SessionCommandDataChild { 3818 SessionInitCmd(Arc<SessionInitCmdData>), 3819 SessionDeinitCmd(Arc<SessionDeinitCmdData>), 3820 SessionSetAppConfigCmd(Arc<SessionSetAppConfigCmdData>), 3821 SessionGetAppConfigCmd(Arc<SessionGetAppConfigCmdData>), 3822 SessionGetCountCmd(Arc<SessionGetCountCmdData>), 3823 SessionGetStateCmd(Arc<SessionGetStateCmdData>), 3824 SessionUpdateControllerMulticastListCmd(Arc<SessionUpdateControllerMulticastListCmdData>), 3825 None, 3826 } 3827 impl SessionCommandDataChild { get_total_size(&self) -> usize3828 fn get_total_size(&self) -> usize { 3829 match self { 3830 SessionCommandDataChild::SessionInitCmd(value) => value.get_total_size(), 3831 SessionCommandDataChild::SessionDeinitCmd(value) => value.get_total_size(), 3832 SessionCommandDataChild::SessionSetAppConfigCmd(value) => value.get_total_size(), 3833 SessionCommandDataChild::SessionGetAppConfigCmd(value) => value.get_total_size(), 3834 SessionCommandDataChild::SessionGetCountCmd(value) => value.get_total_size(), 3835 SessionCommandDataChild::SessionGetStateCmd(value) => value.get_total_size(), 3836 SessionCommandDataChild::SessionUpdateControllerMulticastListCmd(value) => { 3837 value.get_total_size() 3838 } 3839 SessionCommandDataChild::None => 0, 3840 } 3841 } 3842 } 3843 #[derive(Debug)] 3844 pub enum SessionCommandChild { 3845 SessionInitCmd(SessionInitCmdPacket), 3846 SessionDeinitCmd(SessionDeinitCmdPacket), 3847 SessionSetAppConfigCmd(SessionSetAppConfigCmdPacket), 3848 SessionGetAppConfigCmd(SessionGetAppConfigCmdPacket), 3849 SessionGetCountCmd(SessionGetCountCmdPacket), 3850 SessionGetStateCmd(SessionGetStateCmdPacket), 3851 SessionUpdateControllerMulticastListCmd(SessionUpdateControllerMulticastListCmdPacket), 3852 None, 3853 } 3854 #[derive(Debug)] 3855 struct SessionCommandData { 3856 child: SessionCommandDataChild, 3857 } 3858 #[derive(Debug, Clone)] 3859 pub struct SessionCommandPacket { 3860 uci_packet: Arc<UciPacketData>, 3861 uci_command: Arc<UciCommandData>, 3862 session_command: Arc<SessionCommandData>, 3863 } 3864 #[derive(Debug)] 3865 pub struct SessionCommandBuilder { 3866 pub opcode: u8, 3867 } 3868 impl SessionCommandData { conforms(bytes: &[u8]) -> bool3869 fn conforms(bytes: &[u8]) -> bool { 3870 if bytes.len() < 4 { 3871 return false; 3872 } 3873 true 3874 } parse(bytes: &[u8], opcode: u8) -> Result<Self>3875 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 3876 let child = match (opcode) { 3877 (0) if SessionInitCmdData::conforms(&bytes[..]) => { 3878 SessionCommandDataChild::SessionInitCmd(Arc::new(SessionInitCmdData::parse( 3879 &bytes[..], 3880 )?)) 3881 } 3882 (1) if SessionDeinitCmdData::conforms(&bytes[..]) => { 3883 SessionCommandDataChild::SessionDeinitCmd(Arc::new(SessionDeinitCmdData::parse( 3884 &bytes[..], 3885 )?)) 3886 } 3887 (3) if SessionSetAppConfigCmdData::conforms(&bytes[..]) => { 3888 SessionCommandDataChild::SessionSetAppConfigCmd(Arc::new( 3889 SessionSetAppConfigCmdData::parse(&bytes[..])?, 3890 )) 3891 } 3892 (4) if SessionGetAppConfigCmdData::conforms(&bytes[..]) => { 3893 SessionCommandDataChild::SessionGetAppConfigCmd(Arc::new( 3894 SessionGetAppConfigCmdData::parse(&bytes[..])?, 3895 )) 3896 } 3897 (5) if SessionGetCountCmdData::conforms(&bytes[..]) => { 3898 SessionCommandDataChild::SessionGetCountCmd(Arc::new( 3899 SessionGetCountCmdData::parse(&bytes[..])?, 3900 )) 3901 } 3902 (6) if SessionGetStateCmdData::conforms(&bytes[..]) => { 3903 SessionCommandDataChild::SessionGetStateCmd(Arc::new( 3904 SessionGetStateCmdData::parse(&bytes[..])?, 3905 )) 3906 } 3907 (7) if SessionUpdateControllerMulticastListCmdData::conforms(&bytes[..]) => { 3908 SessionCommandDataChild::SessionUpdateControllerMulticastListCmd(Arc::new( 3909 SessionUpdateControllerMulticastListCmdData::parse(&bytes[..])?, 3910 )) 3911 } 3912 (_) => return Err(Error::InvalidPacketError), 3913 }; 3914 Ok(Self { child }) 3915 } write_to(&self, buffer: &mut BytesMut)3916 fn write_to(&self, buffer: &mut BytesMut) { 3917 match &self.child { 3918 SessionCommandDataChild::SessionInitCmd(value) => value.write_to(buffer), 3919 SessionCommandDataChild::SessionDeinitCmd(value) => value.write_to(buffer), 3920 SessionCommandDataChild::SessionSetAppConfigCmd(value) => value.write_to(buffer), 3921 SessionCommandDataChild::SessionGetAppConfigCmd(value) => value.write_to(buffer), 3922 SessionCommandDataChild::SessionGetCountCmd(value) => value.write_to(buffer), 3923 SessionCommandDataChild::SessionGetStateCmd(value) => value.write_to(buffer), 3924 SessionCommandDataChild::SessionUpdateControllerMulticastListCmd(value) => { 3925 value.write_to(buffer) 3926 } 3927 SessionCommandDataChild::None => {} 3928 } 3929 } get_total_size(&self) -> usize3930 fn get_total_size(&self) -> usize { 3931 self.get_size() + self.child.get_total_size() 3932 } get_size(&self) -> usize3933 fn get_size(&self) -> usize { 3934 let ret = 0; 3935 ret 3936 } 3937 } 3938 impl Packet for SessionCommandPacket { to_bytes(self) -> Bytes3939 fn to_bytes(self) -> Bytes { 3940 let mut buffer = BytesMut::new(); 3941 buffer.resize(self.uci_packet.get_total_size(), 0); 3942 self.uci_packet.write_to(&mut buffer); 3943 buffer.freeze() 3944 } to_vec(self) -> Vec<u8>3945 fn to_vec(self) -> Vec<u8> { 3946 self.to_bytes().to_vec() 3947 } 3948 } 3949 impl From<SessionCommandPacket> for Bytes { from(packet: SessionCommandPacket) -> Self3950 fn from(packet: SessionCommandPacket) -> Self { 3951 packet.to_bytes() 3952 } 3953 } 3954 impl From<SessionCommandPacket> for Vec<u8> { from(packet: SessionCommandPacket) -> Self3955 fn from(packet: SessionCommandPacket) -> Self { 3956 packet.to_vec() 3957 } 3958 } 3959 impl TryFrom<UciPacketPacket> for SessionCommandPacket { 3960 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>3961 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 3962 Self::new(value.uci_packet).map_err(TryFromError) 3963 } 3964 } 3965 impl SessionCommandPacket { specialize(&self) -> SessionCommandChild3966 pub fn specialize(&self) -> SessionCommandChild { 3967 match &self.session_command.child { 3968 SessionCommandDataChild::SessionInitCmd(_) => SessionCommandChild::SessionInitCmd( 3969 SessionInitCmdPacket::new(self.uci_packet.clone()).unwrap(), 3970 ), 3971 SessionCommandDataChild::SessionDeinitCmd(_) => SessionCommandChild::SessionDeinitCmd( 3972 SessionDeinitCmdPacket::new(self.uci_packet.clone()).unwrap(), 3973 ), 3974 SessionCommandDataChild::SessionSetAppConfigCmd(_) => { 3975 SessionCommandChild::SessionSetAppConfigCmd( 3976 SessionSetAppConfigCmdPacket::new(self.uci_packet.clone()).unwrap(), 3977 ) 3978 } 3979 SessionCommandDataChild::SessionGetAppConfigCmd(_) => { 3980 SessionCommandChild::SessionGetAppConfigCmd( 3981 SessionGetAppConfigCmdPacket::new(self.uci_packet.clone()).unwrap(), 3982 ) 3983 } 3984 SessionCommandDataChild::SessionGetCountCmd(_) => { 3985 SessionCommandChild::SessionGetCountCmd( 3986 SessionGetCountCmdPacket::new(self.uci_packet.clone()).unwrap(), 3987 ) 3988 } 3989 SessionCommandDataChild::SessionGetStateCmd(_) => { 3990 SessionCommandChild::SessionGetStateCmd( 3991 SessionGetStateCmdPacket::new(self.uci_packet.clone()).unwrap(), 3992 ) 3993 } 3994 SessionCommandDataChild::SessionUpdateControllerMulticastListCmd(_) => { 3995 SessionCommandChild::SessionUpdateControllerMulticastListCmd( 3996 SessionUpdateControllerMulticastListCmdPacket::new(self.uci_packet.clone()) 3997 .unwrap(), 3998 ) 3999 } 4000 SessionCommandDataChild::None => SessionCommandChild::None, 4001 } 4002 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>4003 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 4004 let uci_packet = root; 4005 let uci_command = match &uci_packet.child { 4006 UciPacketDataChild::UciCommand(value) => (*value).clone(), 4007 _ => return Err("inconsistent state - child was not UciCommand"), 4008 }; 4009 let session_command = match &uci_command.child { 4010 UciCommandDataChild::SessionCommand(value) => (*value).clone(), 4011 _ => return Err("inconsistent state - child was not SessionCommand"), 4012 }; 4013 Ok(Self { 4014 uci_packet, 4015 uci_command, 4016 session_command, 4017 }) 4018 } get_group_id(&self) -> GroupId4019 pub fn get_group_id(&self) -> GroupId { 4020 self.uci_packet.as_ref().group_id 4021 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag4022 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 4023 self.uci_packet.as_ref().packet_boundary_flag 4024 } get_message_type(&self) -> MessageType4025 pub fn get_message_type(&self) -> MessageType { 4026 self.uci_packet.as_ref().message_type 4027 } get_opcode(&self) -> u84028 pub fn get_opcode(&self) -> u8 { 4029 self.uci_packet.as_ref().opcode 4030 } 4031 } 4032 impl Into<UciPacketPacket> for SessionCommandPacket { into(self) -> UciPacketPacket4033 fn into(self) -> UciPacketPacket { 4034 UciPacketPacket::new(self.uci_packet).unwrap() 4035 } 4036 } 4037 impl Into<UciCommandPacket> for SessionCommandPacket { into(self) -> UciCommandPacket4038 fn into(self) -> UciCommandPacket { 4039 UciCommandPacket::new(self.uci_packet).unwrap() 4040 } 4041 } 4042 impl SessionCommandBuilder { build(self) -> SessionCommandPacket4043 pub fn build(self) -> SessionCommandPacket { 4044 let session_command = Arc::new(SessionCommandData { 4045 child: SessionCommandDataChild::None, 4046 }); 4047 let uci_command = Arc::new(UciCommandData { 4048 child: UciCommandDataChild::SessionCommand(session_command), 4049 }); 4050 let uci_packet = Arc::new(UciPacketData { 4051 group_id: GroupId::SessionConfig, 4052 packet_boundary_flag: PacketBoundaryFlag::Complete, 4053 message_type: MessageType::Command, 4054 opcode: self.opcode, 4055 child: UciPacketDataChild::UciCommand(uci_command), 4056 }); 4057 SessionCommandPacket::new(uci_packet).unwrap() 4058 } 4059 } 4060 impl Into<UciPacketPacket> for SessionCommandBuilder { into(self) -> UciPacketPacket4061 fn into(self) -> UciPacketPacket { 4062 self.build().into() 4063 } 4064 } 4065 impl Into<UciCommandPacket> for SessionCommandBuilder { into(self) -> UciCommandPacket4066 fn into(self) -> UciCommandPacket { 4067 self.build().into() 4068 } 4069 } 4070 4071 #[derive(Debug)] 4072 enum SessionResponseDataChild { 4073 SessionInitRsp(Arc<SessionInitRspData>), 4074 SessionDeinitRsp(Arc<SessionDeinitRspData>), 4075 SessionSetAppConfigRsp(Arc<SessionSetAppConfigRspData>), 4076 SessionGetAppConfigRsp(Arc<SessionGetAppConfigRspData>), 4077 SessionGetCountRsp(Arc<SessionGetCountRspData>), 4078 SessionGetStateRsp(Arc<SessionGetStateRspData>), 4079 SessionUpdateControllerMulticastListRsp(Arc<SessionUpdateControllerMulticastListRspData>), 4080 None, 4081 } 4082 impl SessionResponseDataChild { get_total_size(&self) -> usize4083 fn get_total_size(&self) -> usize { 4084 match self { 4085 SessionResponseDataChild::SessionInitRsp(value) => value.get_total_size(), 4086 SessionResponseDataChild::SessionDeinitRsp(value) => value.get_total_size(), 4087 SessionResponseDataChild::SessionSetAppConfigRsp(value) => value.get_total_size(), 4088 SessionResponseDataChild::SessionGetAppConfigRsp(value) => value.get_total_size(), 4089 SessionResponseDataChild::SessionGetCountRsp(value) => value.get_total_size(), 4090 SessionResponseDataChild::SessionGetStateRsp(value) => value.get_total_size(), 4091 SessionResponseDataChild::SessionUpdateControllerMulticastListRsp(value) => { 4092 value.get_total_size() 4093 } 4094 SessionResponseDataChild::None => 0, 4095 } 4096 } 4097 } 4098 #[derive(Debug)] 4099 pub enum SessionResponseChild { 4100 SessionInitRsp(SessionInitRspPacket), 4101 SessionDeinitRsp(SessionDeinitRspPacket), 4102 SessionSetAppConfigRsp(SessionSetAppConfigRspPacket), 4103 SessionGetAppConfigRsp(SessionGetAppConfigRspPacket), 4104 SessionGetCountRsp(SessionGetCountRspPacket), 4105 SessionGetStateRsp(SessionGetStateRspPacket), 4106 SessionUpdateControllerMulticastListRsp(SessionUpdateControllerMulticastListRspPacket), 4107 None, 4108 } 4109 #[derive(Debug)] 4110 struct SessionResponseData { 4111 child: SessionResponseDataChild, 4112 } 4113 #[derive(Debug, Clone)] 4114 pub struct SessionResponsePacket { 4115 uci_packet: Arc<UciPacketData>, 4116 uci_response: Arc<UciResponseData>, 4117 session_response: Arc<SessionResponseData>, 4118 } 4119 #[derive(Debug)] 4120 pub struct SessionResponseBuilder { 4121 pub opcode: u8, 4122 } 4123 impl SessionResponseData { conforms(bytes: &[u8]) -> bool4124 fn conforms(bytes: &[u8]) -> bool { 4125 if bytes.len() < 4 { 4126 return false; 4127 } 4128 true 4129 } parse(bytes: &[u8], opcode: u8) -> Result<Self>4130 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 4131 let child = match (opcode) { 4132 (0) if SessionInitRspData::conforms(&bytes[..]) => { 4133 SessionResponseDataChild::SessionInitRsp(Arc::new(SessionInitRspData::parse( 4134 &bytes[..], 4135 )?)) 4136 } 4137 (1) if SessionDeinitRspData::conforms(&bytes[..]) => { 4138 SessionResponseDataChild::SessionDeinitRsp(Arc::new(SessionDeinitRspData::parse( 4139 &bytes[..], 4140 )?)) 4141 } 4142 (3) if SessionSetAppConfigRspData::conforms(&bytes[..]) => { 4143 SessionResponseDataChild::SessionSetAppConfigRsp(Arc::new( 4144 SessionSetAppConfigRspData::parse(&bytes[..])?, 4145 )) 4146 } 4147 (4) if SessionGetAppConfigRspData::conforms(&bytes[..]) => { 4148 SessionResponseDataChild::SessionGetAppConfigRsp(Arc::new( 4149 SessionGetAppConfigRspData::parse(&bytes[..])?, 4150 )) 4151 } 4152 (5) if SessionGetCountRspData::conforms(&bytes[..]) => { 4153 SessionResponseDataChild::SessionGetCountRsp(Arc::new( 4154 SessionGetCountRspData::parse(&bytes[..])?, 4155 )) 4156 } 4157 (6) if SessionGetStateRspData::conforms(&bytes[..]) => { 4158 SessionResponseDataChild::SessionGetStateRsp(Arc::new( 4159 SessionGetStateRspData::parse(&bytes[..])?, 4160 )) 4161 } 4162 (7) if SessionUpdateControllerMulticastListRspData::conforms(&bytes[..]) => { 4163 SessionResponseDataChild::SessionUpdateControllerMulticastListRsp(Arc::new( 4164 SessionUpdateControllerMulticastListRspData::parse(&bytes[..])?, 4165 )) 4166 } 4167 (_) => return Err(Error::InvalidPacketError), 4168 }; 4169 Ok(Self { child }) 4170 } write_to(&self, buffer: &mut BytesMut)4171 fn write_to(&self, buffer: &mut BytesMut) { 4172 match &self.child { 4173 SessionResponseDataChild::SessionInitRsp(value) => value.write_to(buffer), 4174 SessionResponseDataChild::SessionDeinitRsp(value) => value.write_to(buffer), 4175 SessionResponseDataChild::SessionSetAppConfigRsp(value) => value.write_to(buffer), 4176 SessionResponseDataChild::SessionGetAppConfigRsp(value) => value.write_to(buffer), 4177 SessionResponseDataChild::SessionGetCountRsp(value) => value.write_to(buffer), 4178 SessionResponseDataChild::SessionGetStateRsp(value) => value.write_to(buffer), 4179 SessionResponseDataChild::SessionUpdateControllerMulticastListRsp(value) => { 4180 value.write_to(buffer) 4181 } 4182 SessionResponseDataChild::None => {} 4183 } 4184 } get_total_size(&self) -> usize4185 fn get_total_size(&self) -> usize { 4186 self.get_size() + self.child.get_total_size() 4187 } get_size(&self) -> usize4188 fn get_size(&self) -> usize { 4189 let ret = 0; 4190 ret 4191 } 4192 } 4193 impl Packet for SessionResponsePacket { to_bytes(self) -> Bytes4194 fn to_bytes(self) -> Bytes { 4195 let mut buffer = BytesMut::new(); 4196 buffer.resize(self.uci_packet.get_total_size(), 0); 4197 self.uci_packet.write_to(&mut buffer); 4198 buffer.freeze() 4199 } to_vec(self) -> Vec<u8>4200 fn to_vec(self) -> Vec<u8> { 4201 self.to_bytes().to_vec() 4202 } 4203 } 4204 impl From<SessionResponsePacket> for Bytes { from(packet: SessionResponsePacket) -> Self4205 fn from(packet: SessionResponsePacket) -> Self { 4206 packet.to_bytes() 4207 } 4208 } 4209 impl From<SessionResponsePacket> for Vec<u8> { from(packet: SessionResponsePacket) -> Self4210 fn from(packet: SessionResponsePacket) -> Self { 4211 packet.to_vec() 4212 } 4213 } 4214 impl TryFrom<UciPacketPacket> for SessionResponsePacket { 4215 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>4216 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 4217 Self::new(value.uci_packet).map_err(TryFromError) 4218 } 4219 } 4220 impl SessionResponsePacket { specialize(&self) -> SessionResponseChild4221 pub fn specialize(&self) -> SessionResponseChild { 4222 match &self.session_response.child { 4223 SessionResponseDataChild::SessionInitRsp(_) => SessionResponseChild::SessionInitRsp( 4224 SessionInitRspPacket::new(self.uci_packet.clone()).unwrap(), 4225 ), 4226 SessionResponseDataChild::SessionDeinitRsp(_) => { 4227 SessionResponseChild::SessionDeinitRsp( 4228 SessionDeinitRspPacket::new(self.uci_packet.clone()).unwrap(), 4229 ) 4230 } 4231 SessionResponseDataChild::SessionSetAppConfigRsp(_) => { 4232 SessionResponseChild::SessionSetAppConfigRsp( 4233 SessionSetAppConfigRspPacket::new(self.uci_packet.clone()).unwrap(), 4234 ) 4235 } 4236 SessionResponseDataChild::SessionGetAppConfigRsp(_) => { 4237 SessionResponseChild::SessionGetAppConfigRsp( 4238 SessionGetAppConfigRspPacket::new(self.uci_packet.clone()).unwrap(), 4239 ) 4240 } 4241 SessionResponseDataChild::SessionGetCountRsp(_) => { 4242 SessionResponseChild::SessionGetCountRsp( 4243 SessionGetCountRspPacket::new(self.uci_packet.clone()).unwrap(), 4244 ) 4245 } 4246 SessionResponseDataChild::SessionGetStateRsp(_) => { 4247 SessionResponseChild::SessionGetStateRsp( 4248 SessionGetStateRspPacket::new(self.uci_packet.clone()).unwrap(), 4249 ) 4250 } 4251 SessionResponseDataChild::SessionUpdateControllerMulticastListRsp(_) => { 4252 SessionResponseChild::SessionUpdateControllerMulticastListRsp( 4253 SessionUpdateControllerMulticastListRspPacket::new(self.uci_packet.clone()) 4254 .unwrap(), 4255 ) 4256 } 4257 SessionResponseDataChild::None => SessionResponseChild::None, 4258 } 4259 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>4260 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 4261 let uci_packet = root; 4262 let uci_response = match &uci_packet.child { 4263 UciPacketDataChild::UciResponse(value) => (*value).clone(), 4264 _ => return Err("inconsistent state - child was not UciResponse"), 4265 }; 4266 let session_response = match &uci_response.child { 4267 UciResponseDataChild::SessionResponse(value) => (*value).clone(), 4268 _ => return Err("inconsistent state - child was not SessionResponse"), 4269 }; 4270 Ok(Self { 4271 uci_packet, 4272 uci_response, 4273 session_response, 4274 }) 4275 } get_group_id(&self) -> GroupId4276 pub fn get_group_id(&self) -> GroupId { 4277 self.uci_packet.as_ref().group_id 4278 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag4279 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 4280 self.uci_packet.as_ref().packet_boundary_flag 4281 } get_message_type(&self) -> MessageType4282 pub fn get_message_type(&self) -> MessageType { 4283 self.uci_packet.as_ref().message_type 4284 } get_opcode(&self) -> u84285 pub fn get_opcode(&self) -> u8 { 4286 self.uci_packet.as_ref().opcode 4287 } 4288 } 4289 impl Into<UciPacketPacket> for SessionResponsePacket { into(self) -> UciPacketPacket4290 fn into(self) -> UciPacketPacket { 4291 UciPacketPacket::new(self.uci_packet).unwrap() 4292 } 4293 } 4294 impl Into<UciResponsePacket> for SessionResponsePacket { into(self) -> UciResponsePacket4295 fn into(self) -> UciResponsePacket { 4296 UciResponsePacket::new(self.uci_packet).unwrap() 4297 } 4298 } 4299 impl SessionResponseBuilder { build(self) -> SessionResponsePacket4300 pub fn build(self) -> SessionResponsePacket { 4301 let session_response = Arc::new(SessionResponseData { 4302 child: SessionResponseDataChild::None, 4303 }); 4304 let uci_response = Arc::new(UciResponseData { 4305 child: UciResponseDataChild::SessionResponse(session_response), 4306 }); 4307 let uci_packet = Arc::new(UciPacketData { 4308 group_id: GroupId::SessionConfig, 4309 packet_boundary_flag: PacketBoundaryFlag::Complete, 4310 message_type: MessageType::Response, 4311 opcode: self.opcode, 4312 child: UciPacketDataChild::UciResponse(uci_response), 4313 }); 4314 SessionResponsePacket::new(uci_packet).unwrap() 4315 } 4316 } 4317 impl Into<UciPacketPacket> for SessionResponseBuilder { into(self) -> UciPacketPacket4318 fn into(self) -> UciPacketPacket { 4319 self.build().into() 4320 } 4321 } 4322 impl Into<UciResponsePacket> for SessionResponseBuilder { into(self) -> UciResponsePacket4323 fn into(self) -> UciResponsePacket { 4324 self.build().into() 4325 } 4326 } 4327 4328 #[derive(Debug)] 4329 enum SessionNotificationDataChild { 4330 SessionStatusNtf(Arc<SessionStatusNtfData>), 4331 SessionUpdateControllerMulticastListNtf(Arc<SessionUpdateControllerMulticastListNtfData>), 4332 None, 4333 } 4334 impl SessionNotificationDataChild { get_total_size(&self) -> usize4335 fn get_total_size(&self) -> usize { 4336 match self { 4337 SessionNotificationDataChild::SessionStatusNtf(value) => value.get_total_size(), 4338 SessionNotificationDataChild::SessionUpdateControllerMulticastListNtf(value) => { 4339 value.get_total_size() 4340 } 4341 SessionNotificationDataChild::None => 0, 4342 } 4343 } 4344 } 4345 #[derive(Debug)] 4346 pub enum SessionNotificationChild { 4347 SessionStatusNtf(SessionStatusNtfPacket), 4348 SessionUpdateControllerMulticastListNtf(SessionUpdateControllerMulticastListNtfPacket), 4349 None, 4350 } 4351 #[derive(Debug)] 4352 struct SessionNotificationData { 4353 child: SessionNotificationDataChild, 4354 } 4355 #[derive(Debug, Clone)] 4356 pub struct SessionNotificationPacket { 4357 uci_packet: Arc<UciPacketData>, 4358 uci_notification: Arc<UciNotificationData>, 4359 session_notification: Arc<SessionNotificationData>, 4360 } 4361 #[derive(Debug)] 4362 pub struct SessionNotificationBuilder { 4363 pub opcode: u8, 4364 } 4365 impl SessionNotificationData { conforms(bytes: &[u8]) -> bool4366 fn conforms(bytes: &[u8]) -> bool { 4367 if bytes.len() < 4 { 4368 return false; 4369 } 4370 true 4371 } parse(bytes: &[u8], opcode: u8) -> Result<Self>4372 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 4373 let child = match (opcode) { 4374 (2) if SessionStatusNtfData::conforms(&bytes[..]) => { 4375 SessionNotificationDataChild::SessionStatusNtf(Arc::new( 4376 SessionStatusNtfData::parse(&bytes[..])?, 4377 )) 4378 } 4379 (7) if SessionUpdateControllerMulticastListNtfData::conforms(&bytes[..]) => { 4380 SessionNotificationDataChild::SessionUpdateControllerMulticastListNtf(Arc::new( 4381 SessionUpdateControllerMulticastListNtfData::parse(&bytes[..])?, 4382 )) 4383 } 4384 (_) => return Err(Error::InvalidPacketError), 4385 }; 4386 Ok(Self { child }) 4387 } write_to(&self, buffer: &mut BytesMut)4388 fn write_to(&self, buffer: &mut BytesMut) { 4389 match &self.child { 4390 SessionNotificationDataChild::SessionStatusNtf(value) => value.write_to(buffer), 4391 SessionNotificationDataChild::SessionUpdateControllerMulticastListNtf(value) => { 4392 value.write_to(buffer) 4393 } 4394 SessionNotificationDataChild::None => {} 4395 } 4396 } get_total_size(&self) -> usize4397 fn get_total_size(&self) -> usize { 4398 self.get_size() + self.child.get_total_size() 4399 } get_size(&self) -> usize4400 fn get_size(&self) -> usize { 4401 let ret = 0; 4402 ret 4403 } 4404 } 4405 impl Packet for SessionNotificationPacket { to_bytes(self) -> Bytes4406 fn to_bytes(self) -> Bytes { 4407 let mut buffer = BytesMut::new(); 4408 buffer.resize(self.uci_packet.get_total_size(), 0); 4409 self.uci_packet.write_to(&mut buffer); 4410 buffer.freeze() 4411 } to_vec(self) -> Vec<u8>4412 fn to_vec(self) -> Vec<u8> { 4413 self.to_bytes().to_vec() 4414 } 4415 } 4416 impl From<SessionNotificationPacket> for Bytes { from(packet: SessionNotificationPacket) -> Self4417 fn from(packet: SessionNotificationPacket) -> Self { 4418 packet.to_bytes() 4419 } 4420 } 4421 impl From<SessionNotificationPacket> for Vec<u8> { from(packet: SessionNotificationPacket) -> Self4422 fn from(packet: SessionNotificationPacket) -> Self { 4423 packet.to_vec() 4424 } 4425 } 4426 impl TryFrom<UciPacketPacket> for SessionNotificationPacket { 4427 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>4428 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 4429 Self::new(value.uci_packet).map_err(TryFromError) 4430 } 4431 } 4432 impl SessionNotificationPacket { specialize(&self) -> SessionNotificationChild4433 pub fn specialize(&self) -> SessionNotificationChild { 4434 match &self.session_notification.child { 4435 SessionNotificationDataChild::SessionStatusNtf(_) => { 4436 SessionNotificationChild::SessionStatusNtf( 4437 SessionStatusNtfPacket::new(self.uci_packet.clone()).unwrap(), 4438 ) 4439 } 4440 SessionNotificationDataChild::SessionUpdateControllerMulticastListNtf(_) => { 4441 SessionNotificationChild::SessionUpdateControllerMulticastListNtf( 4442 SessionUpdateControllerMulticastListNtfPacket::new(self.uci_packet.clone()) 4443 .unwrap(), 4444 ) 4445 } 4446 SessionNotificationDataChild::None => SessionNotificationChild::None, 4447 } 4448 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>4449 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 4450 let uci_packet = root; 4451 let uci_notification = match &uci_packet.child { 4452 UciPacketDataChild::UciNotification(value) => (*value).clone(), 4453 _ => return Err("inconsistent state - child was not UciNotification"), 4454 }; 4455 let session_notification = match &uci_notification.child { 4456 UciNotificationDataChild::SessionNotification(value) => (*value).clone(), 4457 _ => return Err("inconsistent state - child was not SessionNotification"), 4458 }; 4459 Ok(Self { 4460 uci_packet, 4461 uci_notification, 4462 session_notification, 4463 }) 4464 } get_group_id(&self) -> GroupId4465 pub fn get_group_id(&self) -> GroupId { 4466 self.uci_packet.as_ref().group_id 4467 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag4468 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 4469 self.uci_packet.as_ref().packet_boundary_flag 4470 } get_message_type(&self) -> MessageType4471 pub fn get_message_type(&self) -> MessageType { 4472 self.uci_packet.as_ref().message_type 4473 } get_opcode(&self) -> u84474 pub fn get_opcode(&self) -> u8 { 4475 self.uci_packet.as_ref().opcode 4476 } 4477 } 4478 impl Into<UciPacketPacket> for SessionNotificationPacket { into(self) -> UciPacketPacket4479 fn into(self) -> UciPacketPacket { 4480 UciPacketPacket::new(self.uci_packet).unwrap() 4481 } 4482 } 4483 impl Into<UciNotificationPacket> for SessionNotificationPacket { into(self) -> UciNotificationPacket4484 fn into(self) -> UciNotificationPacket { 4485 UciNotificationPacket::new(self.uci_packet).unwrap() 4486 } 4487 } 4488 impl SessionNotificationBuilder { build(self) -> SessionNotificationPacket4489 pub fn build(self) -> SessionNotificationPacket { 4490 let session_notification = Arc::new(SessionNotificationData { 4491 child: SessionNotificationDataChild::None, 4492 }); 4493 let uci_notification = Arc::new(UciNotificationData { 4494 child: UciNotificationDataChild::SessionNotification(session_notification), 4495 }); 4496 let uci_packet = Arc::new(UciPacketData { 4497 group_id: GroupId::SessionConfig, 4498 packet_boundary_flag: PacketBoundaryFlag::Complete, 4499 message_type: MessageType::Notification, 4500 opcode: self.opcode, 4501 child: UciPacketDataChild::UciNotification(uci_notification), 4502 }); 4503 SessionNotificationPacket::new(uci_packet).unwrap() 4504 } 4505 } 4506 impl Into<UciPacketPacket> for SessionNotificationBuilder { into(self) -> UciPacketPacket4507 fn into(self) -> UciPacketPacket { 4508 self.build().into() 4509 } 4510 } 4511 impl Into<UciNotificationPacket> for SessionNotificationBuilder { into(self) -> UciNotificationPacket4512 fn into(self) -> UciNotificationPacket { 4513 self.build().into() 4514 } 4515 } 4516 4517 #[derive(Debug)] 4518 enum RangingCommandDataChild { 4519 RangeStartCmd(Arc<RangeStartCmdData>), 4520 RangeStopCmd(Arc<RangeStopCmdData>), 4521 RangeGetRangingCountCmd(Arc<RangeGetRangingCountCmdData>), 4522 None, 4523 } 4524 impl RangingCommandDataChild { get_total_size(&self) -> usize4525 fn get_total_size(&self) -> usize { 4526 match self { 4527 RangingCommandDataChild::RangeStartCmd(value) => value.get_total_size(), 4528 RangingCommandDataChild::RangeStopCmd(value) => value.get_total_size(), 4529 RangingCommandDataChild::RangeGetRangingCountCmd(value) => value.get_total_size(), 4530 RangingCommandDataChild::None => 0, 4531 } 4532 } 4533 } 4534 #[derive(Debug)] 4535 pub enum RangingCommandChild { 4536 RangeStartCmd(RangeStartCmdPacket), 4537 RangeStopCmd(RangeStopCmdPacket), 4538 RangeGetRangingCountCmd(RangeGetRangingCountCmdPacket), 4539 None, 4540 } 4541 #[derive(Debug)] 4542 struct RangingCommandData { 4543 session_id: u32, 4544 child: RangingCommandDataChild, 4545 } 4546 #[derive(Debug, Clone)] 4547 pub struct RangingCommandPacket { 4548 uci_packet: Arc<UciPacketData>, 4549 uci_command: Arc<UciCommandData>, 4550 ranging_command: Arc<RangingCommandData>, 4551 } 4552 #[derive(Debug)] 4553 pub struct RangingCommandBuilder { 4554 pub opcode: u8, 4555 pub session_id: u32, 4556 } 4557 impl RangingCommandData { conforms(bytes: &[u8]) -> bool4558 fn conforms(bytes: &[u8]) -> bool { 4559 if bytes.len() < 8 { 4560 return false; 4561 } 4562 true 4563 } parse(bytes: &[u8], opcode: u8) -> Result<Self>4564 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 4565 if bytes.len() < 8 { 4566 return Err(Error::InvalidLengthError { 4567 obj: "RangingCommand".to_string(), 4568 field: "session_id".to_string(), 4569 wanted: 8, 4570 got: bytes.len(), 4571 }); 4572 } 4573 let session_id = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]); 4574 let child = match (opcode) { 4575 (0) if RangeStartCmdData::conforms(&bytes[..]) => { 4576 RangingCommandDataChild::RangeStartCmd(Arc::new(RangeStartCmdData::parse( 4577 &bytes[..], 4578 )?)) 4579 } 4580 (1) if RangeStopCmdData::conforms(&bytes[..]) => RangingCommandDataChild::RangeStopCmd( 4581 Arc::new(RangeStopCmdData::parse(&bytes[..])?), 4582 ), 4583 (3) if RangeGetRangingCountCmdData::conforms(&bytes[..]) => { 4584 RangingCommandDataChild::RangeGetRangingCountCmd(Arc::new( 4585 RangeGetRangingCountCmdData::parse(&bytes[..])?, 4586 )) 4587 } 4588 (_) => return Err(Error::InvalidPacketError), 4589 }; 4590 Ok(Self { session_id, child }) 4591 } write_to(&self, buffer: &mut BytesMut)4592 fn write_to(&self, buffer: &mut BytesMut) { 4593 let session_id = self.session_id; 4594 buffer[4..8].copy_from_slice(&session_id.to_le_bytes()[0..4]); 4595 match &self.child { 4596 RangingCommandDataChild::RangeStartCmd(value) => value.write_to(buffer), 4597 RangingCommandDataChild::RangeStopCmd(value) => value.write_to(buffer), 4598 RangingCommandDataChild::RangeGetRangingCountCmd(value) => value.write_to(buffer), 4599 RangingCommandDataChild::None => {} 4600 } 4601 } get_total_size(&self) -> usize4602 fn get_total_size(&self) -> usize { 4603 self.get_size() + self.child.get_total_size() 4604 } get_size(&self) -> usize4605 fn get_size(&self) -> usize { 4606 let ret = 0; 4607 let ret = ret + 4; 4608 ret 4609 } 4610 } 4611 impl Packet for RangingCommandPacket { to_bytes(self) -> Bytes4612 fn to_bytes(self) -> Bytes { 4613 let mut buffer = BytesMut::new(); 4614 buffer.resize(self.uci_packet.get_total_size(), 0); 4615 self.uci_packet.write_to(&mut buffer); 4616 buffer.freeze() 4617 } to_vec(self) -> Vec<u8>4618 fn to_vec(self) -> Vec<u8> { 4619 self.to_bytes().to_vec() 4620 } 4621 } 4622 impl From<RangingCommandPacket> for Bytes { from(packet: RangingCommandPacket) -> Self4623 fn from(packet: RangingCommandPacket) -> Self { 4624 packet.to_bytes() 4625 } 4626 } 4627 impl From<RangingCommandPacket> for Vec<u8> { from(packet: RangingCommandPacket) -> Self4628 fn from(packet: RangingCommandPacket) -> Self { 4629 packet.to_vec() 4630 } 4631 } 4632 impl TryFrom<UciPacketPacket> for RangingCommandPacket { 4633 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>4634 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 4635 Self::new(value.uci_packet).map_err(TryFromError) 4636 } 4637 } 4638 impl RangingCommandPacket { specialize(&self) -> RangingCommandChild4639 pub fn specialize(&self) -> RangingCommandChild { 4640 match &self.ranging_command.child { 4641 RangingCommandDataChild::RangeStartCmd(_) => RangingCommandChild::RangeStartCmd( 4642 RangeStartCmdPacket::new(self.uci_packet.clone()).unwrap(), 4643 ), 4644 RangingCommandDataChild::RangeStopCmd(_) => RangingCommandChild::RangeStopCmd( 4645 RangeStopCmdPacket::new(self.uci_packet.clone()).unwrap(), 4646 ), 4647 RangingCommandDataChild::RangeGetRangingCountCmd(_) => { 4648 RangingCommandChild::RangeGetRangingCountCmd( 4649 RangeGetRangingCountCmdPacket::new(self.uci_packet.clone()).unwrap(), 4650 ) 4651 } 4652 RangingCommandDataChild::None => RangingCommandChild::None, 4653 } 4654 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>4655 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 4656 let uci_packet = root; 4657 let uci_command = match &uci_packet.child { 4658 UciPacketDataChild::UciCommand(value) => (*value).clone(), 4659 _ => return Err("inconsistent state - child was not UciCommand"), 4660 }; 4661 let ranging_command = match &uci_command.child { 4662 UciCommandDataChild::RangingCommand(value) => (*value).clone(), 4663 _ => return Err("inconsistent state - child was not RangingCommand"), 4664 }; 4665 Ok(Self { 4666 uci_packet, 4667 uci_command, 4668 ranging_command, 4669 }) 4670 } get_group_id(&self) -> GroupId4671 pub fn get_group_id(&self) -> GroupId { 4672 self.uci_packet.as_ref().group_id 4673 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag4674 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 4675 self.uci_packet.as_ref().packet_boundary_flag 4676 } get_message_type(&self) -> MessageType4677 pub fn get_message_type(&self) -> MessageType { 4678 self.uci_packet.as_ref().message_type 4679 } get_opcode(&self) -> u84680 pub fn get_opcode(&self) -> u8 { 4681 self.uci_packet.as_ref().opcode 4682 } get_session_id(&self) -> u324683 pub fn get_session_id(&self) -> u32 { 4684 self.ranging_command.as_ref().session_id 4685 } 4686 } 4687 impl Into<UciPacketPacket> for RangingCommandPacket { into(self) -> UciPacketPacket4688 fn into(self) -> UciPacketPacket { 4689 UciPacketPacket::new(self.uci_packet).unwrap() 4690 } 4691 } 4692 impl Into<UciCommandPacket> for RangingCommandPacket { into(self) -> UciCommandPacket4693 fn into(self) -> UciCommandPacket { 4694 UciCommandPacket::new(self.uci_packet).unwrap() 4695 } 4696 } 4697 impl RangingCommandBuilder { build(self) -> RangingCommandPacket4698 pub fn build(self) -> RangingCommandPacket { 4699 let ranging_command = Arc::new(RangingCommandData { 4700 session_id: self.session_id, 4701 child: RangingCommandDataChild::None, 4702 }); 4703 let uci_command = Arc::new(UciCommandData { 4704 child: UciCommandDataChild::RangingCommand(ranging_command), 4705 }); 4706 let uci_packet = Arc::new(UciPacketData { 4707 group_id: GroupId::RangingSessionControl, 4708 packet_boundary_flag: PacketBoundaryFlag::Complete, 4709 message_type: MessageType::Command, 4710 opcode: self.opcode, 4711 child: UciPacketDataChild::UciCommand(uci_command), 4712 }); 4713 RangingCommandPacket::new(uci_packet).unwrap() 4714 } 4715 } 4716 impl Into<UciPacketPacket> for RangingCommandBuilder { into(self) -> UciPacketPacket4717 fn into(self) -> UciPacketPacket { 4718 self.build().into() 4719 } 4720 } 4721 impl Into<UciCommandPacket> for RangingCommandBuilder { into(self) -> UciCommandPacket4722 fn into(self) -> UciCommandPacket { 4723 self.build().into() 4724 } 4725 } 4726 4727 #[derive(Debug)] 4728 enum RangingResponseDataChild { 4729 RangeStartRsp(Arc<RangeStartRspData>), 4730 RangeStopRsp(Arc<RangeStopRspData>), 4731 RangeGetRangingCountRsp(Arc<RangeGetRangingCountRspData>), 4732 None, 4733 } 4734 impl RangingResponseDataChild { get_total_size(&self) -> usize4735 fn get_total_size(&self) -> usize { 4736 match self { 4737 RangingResponseDataChild::RangeStartRsp(value) => value.get_total_size(), 4738 RangingResponseDataChild::RangeStopRsp(value) => value.get_total_size(), 4739 RangingResponseDataChild::RangeGetRangingCountRsp(value) => value.get_total_size(), 4740 RangingResponseDataChild::None => 0, 4741 } 4742 } 4743 } 4744 #[derive(Debug)] 4745 pub enum RangingResponseChild { 4746 RangeStartRsp(RangeStartRspPacket), 4747 RangeStopRsp(RangeStopRspPacket), 4748 RangeGetRangingCountRsp(RangeGetRangingCountRspPacket), 4749 None, 4750 } 4751 #[derive(Debug)] 4752 struct RangingResponseData { 4753 child: RangingResponseDataChild, 4754 } 4755 #[derive(Debug, Clone)] 4756 pub struct RangingResponsePacket { 4757 uci_packet: Arc<UciPacketData>, 4758 uci_response: Arc<UciResponseData>, 4759 ranging_response: Arc<RangingResponseData>, 4760 } 4761 #[derive(Debug)] 4762 pub struct RangingResponseBuilder { 4763 pub opcode: u8, 4764 } 4765 impl RangingResponseData { conforms(bytes: &[u8]) -> bool4766 fn conforms(bytes: &[u8]) -> bool { 4767 if bytes.len() < 4 { 4768 return false; 4769 } 4770 true 4771 } parse(bytes: &[u8], opcode: u8) -> Result<Self>4772 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 4773 let child = match (opcode) { 4774 (0) if RangeStartRspData::conforms(&bytes[..]) => { 4775 RangingResponseDataChild::RangeStartRsp(Arc::new(RangeStartRspData::parse( 4776 &bytes[..], 4777 )?)) 4778 } 4779 (1) if RangeStopRspData::conforms(&bytes[..]) => { 4780 RangingResponseDataChild::RangeStopRsp(Arc::new(RangeStopRspData::parse( 4781 &bytes[..], 4782 )?)) 4783 } 4784 (3) if RangeGetRangingCountRspData::conforms(&bytes[..]) => { 4785 RangingResponseDataChild::RangeGetRangingCountRsp(Arc::new( 4786 RangeGetRangingCountRspData::parse(&bytes[..])?, 4787 )) 4788 } 4789 (_) => return Err(Error::InvalidPacketError), 4790 }; 4791 Ok(Self { child }) 4792 } write_to(&self, buffer: &mut BytesMut)4793 fn write_to(&self, buffer: &mut BytesMut) { 4794 match &self.child { 4795 RangingResponseDataChild::RangeStartRsp(value) => value.write_to(buffer), 4796 RangingResponseDataChild::RangeStopRsp(value) => value.write_to(buffer), 4797 RangingResponseDataChild::RangeGetRangingCountRsp(value) => value.write_to(buffer), 4798 RangingResponseDataChild::None => {} 4799 } 4800 } get_total_size(&self) -> usize4801 fn get_total_size(&self) -> usize { 4802 self.get_size() + self.child.get_total_size() 4803 } get_size(&self) -> usize4804 fn get_size(&self) -> usize { 4805 let ret = 0; 4806 ret 4807 } 4808 } 4809 impl Packet for RangingResponsePacket { to_bytes(self) -> Bytes4810 fn to_bytes(self) -> Bytes { 4811 let mut buffer = BytesMut::new(); 4812 buffer.resize(self.uci_packet.get_total_size(), 0); 4813 self.uci_packet.write_to(&mut buffer); 4814 buffer.freeze() 4815 } to_vec(self) -> Vec<u8>4816 fn to_vec(self) -> Vec<u8> { 4817 self.to_bytes().to_vec() 4818 } 4819 } 4820 impl From<RangingResponsePacket> for Bytes { from(packet: RangingResponsePacket) -> Self4821 fn from(packet: RangingResponsePacket) -> Self { 4822 packet.to_bytes() 4823 } 4824 } 4825 impl From<RangingResponsePacket> for Vec<u8> { from(packet: RangingResponsePacket) -> Self4826 fn from(packet: RangingResponsePacket) -> Self { 4827 packet.to_vec() 4828 } 4829 } 4830 impl TryFrom<UciPacketPacket> for RangingResponsePacket { 4831 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>4832 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 4833 Self::new(value.uci_packet).map_err(TryFromError) 4834 } 4835 } 4836 impl RangingResponsePacket { specialize(&self) -> RangingResponseChild4837 pub fn specialize(&self) -> RangingResponseChild { 4838 match &self.ranging_response.child { 4839 RangingResponseDataChild::RangeStartRsp(_) => RangingResponseChild::RangeStartRsp( 4840 RangeStartRspPacket::new(self.uci_packet.clone()).unwrap(), 4841 ), 4842 RangingResponseDataChild::RangeStopRsp(_) => RangingResponseChild::RangeStopRsp( 4843 RangeStopRspPacket::new(self.uci_packet.clone()).unwrap(), 4844 ), 4845 RangingResponseDataChild::RangeGetRangingCountRsp(_) => { 4846 RangingResponseChild::RangeGetRangingCountRsp( 4847 RangeGetRangingCountRspPacket::new(self.uci_packet.clone()).unwrap(), 4848 ) 4849 } 4850 RangingResponseDataChild::None => RangingResponseChild::None, 4851 } 4852 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>4853 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 4854 let uci_packet = root; 4855 let uci_response = match &uci_packet.child { 4856 UciPacketDataChild::UciResponse(value) => (*value).clone(), 4857 _ => return Err("inconsistent state - child was not UciResponse"), 4858 }; 4859 let ranging_response = match &uci_response.child { 4860 UciResponseDataChild::RangingResponse(value) => (*value).clone(), 4861 _ => return Err("inconsistent state - child was not RangingResponse"), 4862 }; 4863 Ok(Self { 4864 uci_packet, 4865 uci_response, 4866 ranging_response, 4867 }) 4868 } get_group_id(&self) -> GroupId4869 pub fn get_group_id(&self) -> GroupId { 4870 self.uci_packet.as_ref().group_id 4871 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag4872 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 4873 self.uci_packet.as_ref().packet_boundary_flag 4874 } get_message_type(&self) -> MessageType4875 pub fn get_message_type(&self) -> MessageType { 4876 self.uci_packet.as_ref().message_type 4877 } get_opcode(&self) -> u84878 pub fn get_opcode(&self) -> u8 { 4879 self.uci_packet.as_ref().opcode 4880 } 4881 } 4882 impl Into<UciPacketPacket> for RangingResponsePacket { into(self) -> UciPacketPacket4883 fn into(self) -> UciPacketPacket { 4884 UciPacketPacket::new(self.uci_packet).unwrap() 4885 } 4886 } 4887 impl Into<UciResponsePacket> for RangingResponsePacket { into(self) -> UciResponsePacket4888 fn into(self) -> UciResponsePacket { 4889 UciResponsePacket::new(self.uci_packet).unwrap() 4890 } 4891 } 4892 impl RangingResponseBuilder { build(self) -> RangingResponsePacket4893 pub fn build(self) -> RangingResponsePacket { 4894 let ranging_response = Arc::new(RangingResponseData { 4895 child: RangingResponseDataChild::None, 4896 }); 4897 let uci_response = Arc::new(UciResponseData { 4898 child: UciResponseDataChild::RangingResponse(ranging_response), 4899 }); 4900 let uci_packet = Arc::new(UciPacketData { 4901 group_id: GroupId::RangingSessionControl, 4902 packet_boundary_flag: PacketBoundaryFlag::Complete, 4903 message_type: MessageType::Response, 4904 opcode: self.opcode, 4905 child: UciPacketDataChild::UciResponse(uci_response), 4906 }); 4907 RangingResponsePacket::new(uci_packet).unwrap() 4908 } 4909 } 4910 impl Into<UciPacketPacket> for RangingResponseBuilder { into(self) -> UciPacketPacket4911 fn into(self) -> UciPacketPacket { 4912 self.build().into() 4913 } 4914 } 4915 impl Into<UciResponsePacket> for RangingResponseBuilder { into(self) -> UciResponsePacket4916 fn into(self) -> UciResponsePacket { 4917 self.build().into() 4918 } 4919 } 4920 4921 #[derive(Debug)] 4922 enum RangingNotificationDataChild { 4923 RangeDataNtf(Arc<RangeDataNtfData>), 4924 None, 4925 } 4926 impl RangingNotificationDataChild { get_total_size(&self) -> usize4927 fn get_total_size(&self) -> usize { 4928 match self { 4929 RangingNotificationDataChild::RangeDataNtf(value) => value.get_total_size(), 4930 RangingNotificationDataChild::None => 0, 4931 } 4932 } 4933 } 4934 #[derive(Debug)] 4935 pub enum RangingNotificationChild { 4936 RangeDataNtf(RangeDataNtfPacket), 4937 None, 4938 } 4939 #[derive(Debug)] 4940 struct RangingNotificationData { 4941 child: RangingNotificationDataChild, 4942 } 4943 #[derive(Debug, Clone)] 4944 pub struct RangingNotificationPacket { 4945 uci_packet: Arc<UciPacketData>, 4946 uci_notification: Arc<UciNotificationData>, 4947 ranging_notification: Arc<RangingNotificationData>, 4948 } 4949 #[derive(Debug)] 4950 pub struct RangingNotificationBuilder { 4951 pub opcode: u8, 4952 } 4953 impl RangingNotificationData { conforms(bytes: &[u8]) -> bool4954 fn conforms(bytes: &[u8]) -> bool { 4955 if bytes.len() < 4 { 4956 return false; 4957 } 4958 true 4959 } parse(bytes: &[u8], opcode: u8) -> Result<Self>4960 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 4961 let child = match RangeDataNtfData::parse(&bytes[..]) { 4962 Ok(c) if RangeDataNtfData::conforms(&bytes[..]) => { 4963 RangingNotificationDataChild::RangeDataNtf(Arc::new(c)) 4964 } 4965 Err(Error::InvalidLengthError { .. }) => RangingNotificationDataChild::None, 4966 _ => return Err(Error::InvalidPacketError), 4967 }; 4968 Ok(Self { child }) 4969 } write_to(&self, buffer: &mut BytesMut)4970 fn write_to(&self, buffer: &mut BytesMut) { 4971 match &self.child { 4972 RangingNotificationDataChild::RangeDataNtf(value) => value.write_to(buffer), 4973 RangingNotificationDataChild::None => {} 4974 } 4975 } get_total_size(&self) -> usize4976 fn get_total_size(&self) -> usize { 4977 self.get_size() + self.child.get_total_size() 4978 } get_size(&self) -> usize4979 fn get_size(&self) -> usize { 4980 let ret = 0; 4981 ret 4982 } 4983 } 4984 impl Packet for RangingNotificationPacket { to_bytes(self) -> Bytes4985 fn to_bytes(self) -> Bytes { 4986 let mut buffer = BytesMut::new(); 4987 buffer.resize(self.uci_packet.get_total_size(), 0); 4988 self.uci_packet.write_to(&mut buffer); 4989 buffer.freeze() 4990 } to_vec(self) -> Vec<u8>4991 fn to_vec(self) -> Vec<u8> { 4992 self.to_bytes().to_vec() 4993 } 4994 } 4995 impl From<RangingNotificationPacket> for Bytes { from(packet: RangingNotificationPacket) -> Self4996 fn from(packet: RangingNotificationPacket) -> Self { 4997 packet.to_bytes() 4998 } 4999 } 5000 impl From<RangingNotificationPacket> for Vec<u8> { from(packet: RangingNotificationPacket) -> Self5001 fn from(packet: RangingNotificationPacket) -> Self { 5002 packet.to_vec() 5003 } 5004 } 5005 impl TryFrom<UciPacketPacket> for RangingNotificationPacket { 5006 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>5007 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 5008 Self::new(value.uci_packet).map_err(TryFromError) 5009 } 5010 } 5011 impl RangingNotificationPacket { specialize(&self) -> RangingNotificationChild5012 pub fn specialize(&self) -> RangingNotificationChild { 5013 match &self.ranging_notification.child { 5014 RangingNotificationDataChild::RangeDataNtf(_) => { 5015 RangingNotificationChild::RangeDataNtf( 5016 RangeDataNtfPacket::new(self.uci_packet.clone()).unwrap(), 5017 ) 5018 } 5019 RangingNotificationDataChild::None => RangingNotificationChild::None, 5020 } 5021 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>5022 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 5023 let uci_packet = root; 5024 let uci_notification = match &uci_packet.child { 5025 UciPacketDataChild::UciNotification(value) => (*value).clone(), 5026 _ => return Err("inconsistent state - child was not UciNotification"), 5027 }; 5028 let ranging_notification = match &uci_notification.child { 5029 UciNotificationDataChild::RangingNotification(value) => (*value).clone(), 5030 _ => return Err("inconsistent state - child was not RangingNotification"), 5031 }; 5032 Ok(Self { 5033 uci_packet, 5034 uci_notification, 5035 ranging_notification, 5036 }) 5037 } get_group_id(&self) -> GroupId5038 pub fn get_group_id(&self) -> GroupId { 5039 self.uci_packet.as_ref().group_id 5040 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag5041 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 5042 self.uci_packet.as_ref().packet_boundary_flag 5043 } get_message_type(&self) -> MessageType5044 pub fn get_message_type(&self) -> MessageType { 5045 self.uci_packet.as_ref().message_type 5046 } get_opcode(&self) -> u85047 pub fn get_opcode(&self) -> u8 { 5048 self.uci_packet.as_ref().opcode 5049 } 5050 } 5051 impl Into<UciPacketPacket> for RangingNotificationPacket { into(self) -> UciPacketPacket5052 fn into(self) -> UciPacketPacket { 5053 UciPacketPacket::new(self.uci_packet).unwrap() 5054 } 5055 } 5056 impl Into<UciNotificationPacket> for RangingNotificationPacket { into(self) -> UciNotificationPacket5057 fn into(self) -> UciNotificationPacket { 5058 UciNotificationPacket::new(self.uci_packet).unwrap() 5059 } 5060 } 5061 impl RangingNotificationBuilder { build(self) -> RangingNotificationPacket5062 pub fn build(self) -> RangingNotificationPacket { 5063 let ranging_notification = Arc::new(RangingNotificationData { 5064 child: RangingNotificationDataChild::None, 5065 }); 5066 let uci_notification = Arc::new(UciNotificationData { 5067 child: UciNotificationDataChild::RangingNotification(ranging_notification), 5068 }); 5069 let uci_packet = Arc::new(UciPacketData { 5070 group_id: GroupId::RangingSessionControl, 5071 packet_boundary_flag: PacketBoundaryFlag::Complete, 5072 message_type: MessageType::Notification, 5073 opcode: self.opcode, 5074 child: UciPacketDataChild::UciNotification(uci_notification), 5075 }); 5076 RangingNotificationPacket::new(uci_packet).unwrap() 5077 } 5078 } 5079 impl Into<UciPacketPacket> for RangingNotificationBuilder { into(self) -> UciPacketPacket5080 fn into(self) -> UciPacketPacket { 5081 self.build().into() 5082 } 5083 } 5084 impl Into<UciNotificationPacket> for RangingNotificationBuilder { into(self) -> UciNotificationPacket5085 fn into(self) -> UciNotificationPacket { 5086 self.build().into() 5087 } 5088 } 5089 5090 #[derive(Debug)] 5091 enum AndroidCommandDataChild { 5092 AndroidGetPowerStatsCmd(Arc<AndroidGetPowerStatsCmdData>), 5093 AndroidSetCountryCodeCmd(Arc<AndroidSetCountryCodeCmdData>), 5094 None, 5095 } 5096 impl AndroidCommandDataChild { get_total_size(&self) -> usize5097 fn get_total_size(&self) -> usize { 5098 match self { 5099 AndroidCommandDataChild::AndroidGetPowerStatsCmd(value) => value.get_total_size(), 5100 AndroidCommandDataChild::AndroidSetCountryCodeCmd(value) => value.get_total_size(), 5101 AndroidCommandDataChild::None => 0, 5102 } 5103 } 5104 } 5105 #[derive(Debug)] 5106 pub enum AndroidCommandChild { 5107 AndroidGetPowerStatsCmd(AndroidGetPowerStatsCmdPacket), 5108 AndroidSetCountryCodeCmd(AndroidSetCountryCodeCmdPacket), 5109 None, 5110 } 5111 #[derive(Debug)] 5112 struct AndroidCommandData { 5113 child: AndroidCommandDataChild, 5114 } 5115 #[derive(Debug, Clone)] 5116 pub struct AndroidCommandPacket { 5117 uci_packet: Arc<UciPacketData>, 5118 uci_command: Arc<UciCommandData>, 5119 android_command: Arc<AndroidCommandData>, 5120 } 5121 #[derive(Debug)] 5122 pub struct AndroidCommandBuilder { 5123 pub opcode: u8, 5124 } 5125 impl AndroidCommandData { conforms(bytes: &[u8]) -> bool5126 fn conforms(bytes: &[u8]) -> bool { 5127 if bytes.len() < 4 { 5128 return false; 5129 } 5130 true 5131 } parse(bytes: &[u8], opcode: u8) -> Result<Self>5132 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 5133 let child = match (opcode) { 5134 (0) if AndroidGetPowerStatsCmdData::conforms(&bytes[..]) => { 5135 AndroidCommandDataChild::AndroidGetPowerStatsCmd(Arc::new( 5136 AndroidGetPowerStatsCmdData::parse(&bytes[..])?, 5137 )) 5138 } 5139 (1) if AndroidSetCountryCodeCmdData::conforms(&bytes[..]) => { 5140 AndroidCommandDataChild::AndroidSetCountryCodeCmd(Arc::new( 5141 AndroidSetCountryCodeCmdData::parse(&bytes[..])?, 5142 )) 5143 } 5144 (_) => return Err(Error::InvalidPacketError), 5145 }; 5146 Ok(Self { child }) 5147 } write_to(&self, buffer: &mut BytesMut)5148 fn write_to(&self, buffer: &mut BytesMut) { 5149 match &self.child { 5150 AndroidCommandDataChild::AndroidGetPowerStatsCmd(value) => value.write_to(buffer), 5151 AndroidCommandDataChild::AndroidSetCountryCodeCmd(value) => value.write_to(buffer), 5152 AndroidCommandDataChild::None => {} 5153 } 5154 } get_total_size(&self) -> usize5155 fn get_total_size(&self) -> usize { 5156 self.get_size() + self.child.get_total_size() 5157 } get_size(&self) -> usize5158 fn get_size(&self) -> usize { 5159 let ret = 0; 5160 ret 5161 } 5162 } 5163 impl Packet for AndroidCommandPacket { to_bytes(self) -> Bytes5164 fn to_bytes(self) -> Bytes { 5165 let mut buffer = BytesMut::new(); 5166 buffer.resize(self.uci_packet.get_total_size(), 0); 5167 self.uci_packet.write_to(&mut buffer); 5168 buffer.freeze() 5169 } to_vec(self) -> Vec<u8>5170 fn to_vec(self) -> Vec<u8> { 5171 self.to_bytes().to_vec() 5172 } 5173 } 5174 impl From<AndroidCommandPacket> for Bytes { from(packet: AndroidCommandPacket) -> Self5175 fn from(packet: AndroidCommandPacket) -> Self { 5176 packet.to_bytes() 5177 } 5178 } 5179 impl From<AndroidCommandPacket> for Vec<u8> { from(packet: AndroidCommandPacket) -> Self5180 fn from(packet: AndroidCommandPacket) -> Self { 5181 packet.to_vec() 5182 } 5183 } 5184 impl TryFrom<UciPacketPacket> for AndroidCommandPacket { 5185 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>5186 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 5187 Self::new(value.uci_packet).map_err(TryFromError) 5188 } 5189 } 5190 impl AndroidCommandPacket { specialize(&self) -> AndroidCommandChild5191 pub fn specialize(&self) -> AndroidCommandChild { 5192 match &self.android_command.child { 5193 AndroidCommandDataChild::AndroidGetPowerStatsCmd(_) => { 5194 AndroidCommandChild::AndroidGetPowerStatsCmd( 5195 AndroidGetPowerStatsCmdPacket::new(self.uci_packet.clone()).unwrap(), 5196 ) 5197 } 5198 AndroidCommandDataChild::AndroidSetCountryCodeCmd(_) => { 5199 AndroidCommandChild::AndroidSetCountryCodeCmd( 5200 AndroidSetCountryCodeCmdPacket::new(self.uci_packet.clone()).unwrap(), 5201 ) 5202 } 5203 AndroidCommandDataChild::None => AndroidCommandChild::None, 5204 } 5205 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>5206 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 5207 let uci_packet = root; 5208 let uci_command = match &uci_packet.child { 5209 UciPacketDataChild::UciCommand(value) => (*value).clone(), 5210 _ => return Err("inconsistent state - child was not UciCommand"), 5211 }; 5212 let android_command = match &uci_command.child { 5213 UciCommandDataChild::AndroidCommand(value) => (*value).clone(), 5214 _ => return Err("inconsistent state - child was not AndroidCommand"), 5215 }; 5216 Ok(Self { 5217 uci_packet, 5218 uci_command, 5219 android_command, 5220 }) 5221 } get_group_id(&self) -> GroupId5222 pub fn get_group_id(&self) -> GroupId { 5223 self.uci_packet.as_ref().group_id 5224 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag5225 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 5226 self.uci_packet.as_ref().packet_boundary_flag 5227 } get_message_type(&self) -> MessageType5228 pub fn get_message_type(&self) -> MessageType { 5229 self.uci_packet.as_ref().message_type 5230 } get_opcode(&self) -> u85231 pub fn get_opcode(&self) -> u8 { 5232 self.uci_packet.as_ref().opcode 5233 } 5234 } 5235 impl Into<UciPacketPacket> for AndroidCommandPacket { into(self) -> UciPacketPacket5236 fn into(self) -> UciPacketPacket { 5237 UciPacketPacket::new(self.uci_packet).unwrap() 5238 } 5239 } 5240 impl Into<UciCommandPacket> for AndroidCommandPacket { into(self) -> UciCommandPacket5241 fn into(self) -> UciCommandPacket { 5242 UciCommandPacket::new(self.uci_packet).unwrap() 5243 } 5244 } 5245 impl AndroidCommandBuilder { build(self) -> AndroidCommandPacket5246 pub fn build(self) -> AndroidCommandPacket { 5247 let android_command = Arc::new(AndroidCommandData { 5248 child: AndroidCommandDataChild::None, 5249 }); 5250 let uci_command = Arc::new(UciCommandData { 5251 child: UciCommandDataChild::AndroidCommand(android_command), 5252 }); 5253 let uci_packet = Arc::new(UciPacketData { 5254 group_id: GroupId::VendorAndroid, 5255 packet_boundary_flag: PacketBoundaryFlag::Complete, 5256 message_type: MessageType::Command, 5257 opcode: self.opcode, 5258 child: UciPacketDataChild::UciCommand(uci_command), 5259 }); 5260 AndroidCommandPacket::new(uci_packet).unwrap() 5261 } 5262 } 5263 impl Into<UciPacketPacket> for AndroidCommandBuilder { into(self) -> UciPacketPacket5264 fn into(self) -> UciPacketPacket { 5265 self.build().into() 5266 } 5267 } 5268 impl Into<UciCommandPacket> for AndroidCommandBuilder { into(self) -> UciCommandPacket5269 fn into(self) -> UciCommandPacket { 5270 self.build().into() 5271 } 5272 } 5273 5274 #[derive(Debug)] 5275 enum AndroidResponseDataChild { 5276 AndroidGetPowerStatsRsp(Arc<AndroidGetPowerStatsRspData>), 5277 AndroidSetCountryCodeRsp(Arc<AndroidSetCountryCodeRspData>), 5278 None, 5279 } 5280 impl AndroidResponseDataChild { get_total_size(&self) -> usize5281 fn get_total_size(&self) -> usize { 5282 match self { 5283 AndroidResponseDataChild::AndroidGetPowerStatsRsp(value) => value.get_total_size(), 5284 AndroidResponseDataChild::AndroidSetCountryCodeRsp(value) => value.get_total_size(), 5285 AndroidResponseDataChild::None => 0, 5286 } 5287 } 5288 } 5289 #[derive(Debug)] 5290 pub enum AndroidResponseChild { 5291 AndroidGetPowerStatsRsp(AndroidGetPowerStatsRspPacket), 5292 AndroidSetCountryCodeRsp(AndroidSetCountryCodeRspPacket), 5293 None, 5294 } 5295 #[derive(Debug)] 5296 struct AndroidResponseData { 5297 child: AndroidResponseDataChild, 5298 } 5299 #[derive(Debug, Clone)] 5300 pub struct AndroidResponsePacket { 5301 uci_packet: Arc<UciPacketData>, 5302 uci_response: Arc<UciResponseData>, 5303 android_response: Arc<AndroidResponseData>, 5304 } 5305 #[derive(Debug)] 5306 pub struct AndroidResponseBuilder { 5307 pub opcode: u8, 5308 } 5309 impl AndroidResponseData { conforms(bytes: &[u8]) -> bool5310 fn conforms(bytes: &[u8]) -> bool { 5311 if bytes.len() < 4 { 5312 return false; 5313 } 5314 true 5315 } parse(bytes: &[u8], opcode: u8) -> Result<Self>5316 fn parse(bytes: &[u8], opcode: u8) -> Result<Self> { 5317 let child = match (opcode) { 5318 (0) if AndroidGetPowerStatsRspData::conforms(&bytes[..]) => { 5319 AndroidResponseDataChild::AndroidGetPowerStatsRsp(Arc::new( 5320 AndroidGetPowerStatsRspData::parse(&bytes[..])?, 5321 )) 5322 } 5323 (1) if AndroidSetCountryCodeRspData::conforms(&bytes[..]) => { 5324 AndroidResponseDataChild::AndroidSetCountryCodeRsp(Arc::new( 5325 AndroidSetCountryCodeRspData::parse(&bytes[..])?, 5326 )) 5327 } 5328 (_) => return Err(Error::InvalidPacketError), 5329 }; 5330 Ok(Self { child }) 5331 } write_to(&self, buffer: &mut BytesMut)5332 fn write_to(&self, buffer: &mut BytesMut) { 5333 match &self.child { 5334 AndroidResponseDataChild::AndroidGetPowerStatsRsp(value) => value.write_to(buffer), 5335 AndroidResponseDataChild::AndroidSetCountryCodeRsp(value) => value.write_to(buffer), 5336 AndroidResponseDataChild::None => {} 5337 } 5338 } get_total_size(&self) -> usize5339 fn get_total_size(&self) -> usize { 5340 self.get_size() + self.child.get_total_size() 5341 } get_size(&self) -> usize5342 fn get_size(&self) -> usize { 5343 let ret = 0; 5344 ret 5345 } 5346 } 5347 impl Packet for AndroidResponsePacket { to_bytes(self) -> Bytes5348 fn to_bytes(self) -> Bytes { 5349 let mut buffer = BytesMut::new(); 5350 buffer.resize(self.uci_packet.get_total_size(), 0); 5351 self.uci_packet.write_to(&mut buffer); 5352 buffer.freeze() 5353 } to_vec(self) -> Vec<u8>5354 fn to_vec(self) -> Vec<u8> { 5355 self.to_bytes().to_vec() 5356 } 5357 } 5358 impl From<AndroidResponsePacket> for Bytes { from(packet: AndroidResponsePacket) -> Self5359 fn from(packet: AndroidResponsePacket) -> Self { 5360 packet.to_bytes() 5361 } 5362 } 5363 impl From<AndroidResponsePacket> for Vec<u8> { from(packet: AndroidResponsePacket) -> Self5364 fn from(packet: AndroidResponsePacket) -> Self { 5365 packet.to_vec() 5366 } 5367 } 5368 impl TryFrom<UciPacketPacket> for AndroidResponsePacket { 5369 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>5370 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 5371 Self::new(value.uci_packet).map_err(TryFromError) 5372 } 5373 } 5374 impl AndroidResponsePacket { specialize(&self) -> AndroidResponseChild5375 pub fn specialize(&self) -> AndroidResponseChild { 5376 match &self.android_response.child { 5377 AndroidResponseDataChild::AndroidGetPowerStatsRsp(_) => { 5378 AndroidResponseChild::AndroidGetPowerStatsRsp( 5379 AndroidGetPowerStatsRspPacket::new(self.uci_packet.clone()).unwrap(), 5380 ) 5381 } 5382 AndroidResponseDataChild::AndroidSetCountryCodeRsp(_) => { 5383 AndroidResponseChild::AndroidSetCountryCodeRsp( 5384 AndroidSetCountryCodeRspPacket::new(self.uci_packet.clone()).unwrap(), 5385 ) 5386 } 5387 AndroidResponseDataChild::None => AndroidResponseChild::None, 5388 } 5389 } new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>5390 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 5391 let uci_packet = root; 5392 let uci_response = match &uci_packet.child { 5393 UciPacketDataChild::UciResponse(value) => (*value).clone(), 5394 _ => return Err("inconsistent state - child was not UciResponse"), 5395 }; 5396 let android_response = match &uci_response.child { 5397 UciResponseDataChild::AndroidResponse(value) => (*value).clone(), 5398 _ => return Err("inconsistent state - child was not AndroidResponse"), 5399 }; 5400 Ok(Self { 5401 uci_packet, 5402 uci_response, 5403 android_response, 5404 }) 5405 } get_group_id(&self) -> GroupId5406 pub fn get_group_id(&self) -> GroupId { 5407 self.uci_packet.as_ref().group_id 5408 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag5409 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 5410 self.uci_packet.as_ref().packet_boundary_flag 5411 } get_message_type(&self) -> MessageType5412 pub fn get_message_type(&self) -> MessageType { 5413 self.uci_packet.as_ref().message_type 5414 } get_opcode(&self) -> u85415 pub fn get_opcode(&self) -> u8 { 5416 self.uci_packet.as_ref().opcode 5417 } 5418 } 5419 impl Into<UciPacketPacket> for AndroidResponsePacket { into(self) -> UciPacketPacket5420 fn into(self) -> UciPacketPacket { 5421 UciPacketPacket::new(self.uci_packet).unwrap() 5422 } 5423 } 5424 impl Into<UciResponsePacket> for AndroidResponsePacket { into(self) -> UciResponsePacket5425 fn into(self) -> UciResponsePacket { 5426 UciResponsePacket::new(self.uci_packet).unwrap() 5427 } 5428 } 5429 impl AndroidResponseBuilder { build(self) -> AndroidResponsePacket5430 pub fn build(self) -> AndroidResponsePacket { 5431 let android_response = Arc::new(AndroidResponseData { 5432 child: AndroidResponseDataChild::None, 5433 }); 5434 let uci_response = Arc::new(UciResponseData { 5435 child: UciResponseDataChild::AndroidResponse(android_response), 5436 }); 5437 let uci_packet = Arc::new(UciPacketData { 5438 group_id: GroupId::VendorAndroid, 5439 packet_boundary_flag: PacketBoundaryFlag::Complete, 5440 message_type: MessageType::Response, 5441 opcode: self.opcode, 5442 child: UciPacketDataChild::UciResponse(uci_response), 5443 }); 5444 AndroidResponsePacket::new(uci_packet).unwrap() 5445 } 5446 } 5447 impl Into<UciPacketPacket> for AndroidResponseBuilder { into(self) -> UciPacketPacket5448 fn into(self) -> UciPacketPacket { 5449 self.build().into() 5450 } 5451 } 5452 impl Into<UciResponsePacket> for AndroidResponseBuilder { into(self) -> UciResponsePacket5453 fn into(self) -> UciResponsePacket { 5454 self.build().into() 5455 } 5456 } 5457 5458 #[derive(Debug)] 5459 struct AndroidNotificationData {} 5460 #[derive(Debug, Clone)] 5461 pub struct AndroidNotificationPacket { 5462 uci_packet: Arc<UciPacketData>, 5463 uci_notification: Arc<UciNotificationData>, 5464 android_notification: Arc<AndroidNotificationData>, 5465 } 5466 #[derive(Debug)] 5467 pub struct AndroidNotificationBuilder { 5468 pub opcode: u8, 5469 } 5470 impl AndroidNotificationData { conforms(bytes: &[u8]) -> bool5471 fn conforms(bytes: &[u8]) -> bool { 5472 if bytes.len() < 4 { 5473 return false; 5474 } 5475 true 5476 } parse(bytes: &[u8]) -> Result<Self>5477 fn parse(bytes: &[u8]) -> Result<Self> { 5478 Ok(Self {}) 5479 } write_to(&self, buffer: &mut BytesMut)5480 fn write_to(&self, buffer: &mut BytesMut) {} get_total_size(&self) -> usize5481 fn get_total_size(&self) -> usize { 5482 self.get_size() 5483 } get_size(&self) -> usize5484 fn get_size(&self) -> usize { 5485 let ret = 0; 5486 ret 5487 } 5488 } 5489 impl Packet for AndroidNotificationPacket { to_bytes(self) -> Bytes5490 fn to_bytes(self) -> Bytes { 5491 let mut buffer = BytesMut::new(); 5492 buffer.resize(self.uci_packet.get_total_size(), 0); 5493 self.uci_packet.write_to(&mut buffer); 5494 buffer.freeze() 5495 } to_vec(self) -> Vec<u8>5496 fn to_vec(self) -> Vec<u8> { 5497 self.to_bytes().to_vec() 5498 } 5499 } 5500 impl From<AndroidNotificationPacket> for Bytes { from(packet: AndroidNotificationPacket) -> Self5501 fn from(packet: AndroidNotificationPacket) -> Self { 5502 packet.to_bytes() 5503 } 5504 } 5505 impl From<AndroidNotificationPacket> for Vec<u8> { from(packet: AndroidNotificationPacket) -> Self5506 fn from(packet: AndroidNotificationPacket) -> Self { 5507 packet.to_vec() 5508 } 5509 } 5510 impl TryFrom<UciPacketPacket> for AndroidNotificationPacket { 5511 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>5512 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 5513 Self::new(value.uci_packet).map_err(TryFromError) 5514 } 5515 } 5516 impl AndroidNotificationPacket { new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>5517 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 5518 let uci_packet = root; 5519 let uci_notification = match &uci_packet.child { 5520 UciPacketDataChild::UciNotification(value) => (*value).clone(), 5521 _ => return Err("inconsistent state - child was not UciNotification"), 5522 }; 5523 let android_notification = match &uci_notification.child { 5524 UciNotificationDataChild::AndroidNotification(value) => (*value).clone(), 5525 _ => return Err("inconsistent state - child was not AndroidNotification"), 5526 }; 5527 Ok(Self { 5528 uci_packet, 5529 uci_notification, 5530 android_notification, 5531 }) 5532 } get_group_id(&self) -> GroupId5533 pub fn get_group_id(&self) -> GroupId { 5534 self.uci_packet.as_ref().group_id 5535 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag5536 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 5537 self.uci_packet.as_ref().packet_boundary_flag 5538 } get_message_type(&self) -> MessageType5539 pub fn get_message_type(&self) -> MessageType { 5540 self.uci_packet.as_ref().message_type 5541 } get_opcode(&self) -> u85542 pub fn get_opcode(&self) -> u8 { 5543 self.uci_packet.as_ref().opcode 5544 } 5545 } 5546 impl Into<UciPacketPacket> for AndroidNotificationPacket { into(self) -> UciPacketPacket5547 fn into(self) -> UciPacketPacket { 5548 UciPacketPacket::new(self.uci_packet).unwrap() 5549 } 5550 } 5551 impl Into<UciNotificationPacket> for AndroidNotificationPacket { into(self) -> UciNotificationPacket5552 fn into(self) -> UciNotificationPacket { 5553 UciNotificationPacket::new(self.uci_packet).unwrap() 5554 } 5555 } 5556 impl AndroidNotificationBuilder { build(self) -> AndroidNotificationPacket5557 pub fn build(self) -> AndroidNotificationPacket { 5558 let android_notification = Arc::new(AndroidNotificationData {}); 5559 let uci_notification = Arc::new(UciNotificationData { 5560 child: UciNotificationDataChild::AndroidNotification(android_notification), 5561 }); 5562 let uci_packet = Arc::new(UciPacketData { 5563 group_id: GroupId::VendorAndroid, 5564 packet_boundary_flag: PacketBoundaryFlag::Complete, 5565 message_type: MessageType::Notification, 5566 opcode: self.opcode, 5567 child: UciPacketDataChild::UciNotification(uci_notification), 5568 }); 5569 AndroidNotificationPacket::new(uci_packet).unwrap() 5570 } 5571 } 5572 impl Into<UciPacketPacket> for AndroidNotificationBuilder { into(self) -> UciPacketPacket5573 fn into(self) -> UciPacketPacket { 5574 self.build().into() 5575 } 5576 } 5577 impl Into<UciNotificationPacket> for AndroidNotificationBuilder { into(self) -> UciNotificationPacket5578 fn into(self) -> UciNotificationPacket { 5579 self.build().into() 5580 } 5581 } 5582 5583 #[derive(Debug)] 5584 struct DeviceResetCmdData { 5585 reset_config: ResetConfig, 5586 } 5587 #[derive(Debug, Clone)] 5588 pub struct DeviceResetCmdPacket { 5589 uci_packet: Arc<UciPacketData>, 5590 uci_command: Arc<UciCommandData>, 5591 core_command: Arc<CoreCommandData>, 5592 device_reset_cmd: Arc<DeviceResetCmdData>, 5593 } 5594 #[derive(Debug)] 5595 pub struct DeviceResetCmdBuilder { 5596 pub reset_config: ResetConfig, 5597 } 5598 impl DeviceResetCmdData { conforms(bytes: &[u8]) -> bool5599 fn conforms(bytes: &[u8]) -> bool { 5600 if bytes.len() < 5 { 5601 return false; 5602 } 5603 true 5604 } parse(bytes: &[u8]) -> Result<Self>5605 fn parse(bytes: &[u8]) -> Result<Self> { 5606 if bytes.len() < 5 { 5607 return Err(Error::InvalidLengthError { 5608 obj: "DeviceResetCmd".to_string(), 5609 field: "reset_config".to_string(), 5610 wanted: 5, 5611 got: bytes.len(), 5612 }); 5613 } 5614 let reset_config = u8::from_le_bytes([bytes[4]]); 5615 let reset_config = 5616 ResetConfig::from_u8(reset_config).ok_or_else(|| Error::InvalidEnumValueError { 5617 obj: "DeviceResetCmd".to_string(), 5618 field: "reset_config".to_string(), 5619 value: reset_config as u64, 5620 type_: "ResetConfig".to_string(), 5621 })?; 5622 Ok(Self { reset_config }) 5623 } write_to(&self, buffer: &mut BytesMut)5624 fn write_to(&self, buffer: &mut BytesMut) { 5625 let reset_config = self.reset_config.to_u8().unwrap(); 5626 buffer[4..5].copy_from_slice(&reset_config.to_le_bytes()[0..1]); 5627 } get_total_size(&self) -> usize5628 fn get_total_size(&self) -> usize { 5629 self.get_size() 5630 } get_size(&self) -> usize5631 fn get_size(&self) -> usize { 5632 let ret = 0; 5633 let ret = ret + 1; 5634 ret 5635 } 5636 } 5637 impl Packet for DeviceResetCmdPacket { to_bytes(self) -> Bytes5638 fn to_bytes(self) -> Bytes { 5639 let mut buffer = BytesMut::new(); 5640 buffer.resize(self.uci_packet.get_total_size(), 0); 5641 self.uci_packet.write_to(&mut buffer); 5642 buffer.freeze() 5643 } to_vec(self) -> Vec<u8>5644 fn to_vec(self) -> Vec<u8> { 5645 self.to_bytes().to_vec() 5646 } 5647 } 5648 impl From<DeviceResetCmdPacket> for Bytes { from(packet: DeviceResetCmdPacket) -> Self5649 fn from(packet: DeviceResetCmdPacket) -> Self { 5650 packet.to_bytes() 5651 } 5652 } 5653 impl From<DeviceResetCmdPacket> for Vec<u8> { from(packet: DeviceResetCmdPacket) -> Self5654 fn from(packet: DeviceResetCmdPacket) -> Self { 5655 packet.to_vec() 5656 } 5657 } 5658 impl TryFrom<UciPacketPacket> for DeviceResetCmdPacket { 5659 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>5660 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 5661 Self::new(value.uci_packet).map_err(TryFromError) 5662 } 5663 } 5664 impl DeviceResetCmdPacket { new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>5665 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 5666 let uci_packet = root; 5667 let uci_command = match &uci_packet.child { 5668 UciPacketDataChild::UciCommand(value) => (*value).clone(), 5669 _ => return Err("inconsistent state - child was not UciCommand"), 5670 }; 5671 let core_command = match &uci_command.child { 5672 UciCommandDataChild::CoreCommand(value) => (*value).clone(), 5673 _ => return Err("inconsistent state - child was not CoreCommand"), 5674 }; 5675 let device_reset_cmd = match &core_command.child { 5676 CoreCommandDataChild::DeviceResetCmd(value) => (*value).clone(), 5677 _ => return Err("inconsistent state - child was not DeviceResetCmd"), 5678 }; 5679 Ok(Self { 5680 uci_packet, 5681 uci_command, 5682 core_command, 5683 device_reset_cmd, 5684 }) 5685 } get_group_id(&self) -> GroupId5686 pub fn get_group_id(&self) -> GroupId { 5687 self.uci_packet.as_ref().group_id 5688 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag5689 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 5690 self.uci_packet.as_ref().packet_boundary_flag 5691 } get_message_type(&self) -> MessageType5692 pub fn get_message_type(&self) -> MessageType { 5693 self.uci_packet.as_ref().message_type 5694 } get_opcode(&self) -> u85695 pub fn get_opcode(&self) -> u8 { 5696 self.uci_packet.as_ref().opcode 5697 } get_reset_config(&self) -> ResetConfig5698 pub fn get_reset_config(&self) -> ResetConfig { 5699 self.device_reset_cmd.as_ref().reset_config 5700 } 5701 } 5702 impl Into<UciPacketPacket> for DeviceResetCmdPacket { into(self) -> UciPacketPacket5703 fn into(self) -> UciPacketPacket { 5704 UciPacketPacket::new(self.uci_packet).unwrap() 5705 } 5706 } 5707 impl Into<UciCommandPacket> for DeviceResetCmdPacket { into(self) -> UciCommandPacket5708 fn into(self) -> UciCommandPacket { 5709 UciCommandPacket::new(self.uci_packet).unwrap() 5710 } 5711 } 5712 impl Into<CoreCommandPacket> for DeviceResetCmdPacket { into(self) -> CoreCommandPacket5713 fn into(self) -> CoreCommandPacket { 5714 CoreCommandPacket::new(self.uci_packet).unwrap() 5715 } 5716 } 5717 impl DeviceResetCmdBuilder { build(self) -> DeviceResetCmdPacket5718 pub fn build(self) -> DeviceResetCmdPacket { 5719 let device_reset_cmd = Arc::new(DeviceResetCmdData { 5720 reset_config: self.reset_config, 5721 }); 5722 let core_command = Arc::new(CoreCommandData { 5723 child: CoreCommandDataChild::DeviceResetCmd(device_reset_cmd), 5724 }); 5725 let uci_command = Arc::new(UciCommandData { 5726 child: UciCommandDataChild::CoreCommand(core_command), 5727 }); 5728 let uci_packet = Arc::new(UciPacketData { 5729 group_id: GroupId::Core, 5730 packet_boundary_flag: PacketBoundaryFlag::Complete, 5731 message_type: MessageType::Command, 5732 opcode: 0, 5733 child: UciPacketDataChild::UciCommand(uci_command), 5734 }); 5735 DeviceResetCmdPacket::new(uci_packet).unwrap() 5736 } 5737 } 5738 impl Into<UciPacketPacket> for DeviceResetCmdBuilder { into(self) -> UciPacketPacket5739 fn into(self) -> UciPacketPacket { 5740 self.build().into() 5741 } 5742 } 5743 impl Into<UciCommandPacket> for DeviceResetCmdBuilder { into(self) -> UciCommandPacket5744 fn into(self) -> UciCommandPacket { 5745 self.build().into() 5746 } 5747 } 5748 impl Into<CoreCommandPacket> for DeviceResetCmdBuilder { into(self) -> CoreCommandPacket5749 fn into(self) -> CoreCommandPacket { 5750 self.build().into() 5751 } 5752 } 5753 macro_rules! device_reset_cmd_builder_tests { ($($name:ident: $byte_string:expr,)*) => {$( 5754 #[test] 5755 pub fn $name() { let raw_bytes = $byte_string;/* (0) */ 5756 match UciPacketPacket::parse(raw_bytes) {Ok(uci_packet_packet) => {match uci_packet_packet.specialize() {/* (1) */ 5757 UciPacketChild::UciCommand(uci_command_packet) => {match uci_command_packet.specialize() {/* (2) */ 5758 UciCommandChild::CoreCommand(core_command_packet) => {match core_command_packet.specialize() {/* (3) */ 5759 CoreCommandChild::DeviceResetCmd(packet) => {let rebuilder = DeviceResetCmdBuilder {reset_config : packet.get_reset_config(),};let rebuilder_base : UciPacketPacket = rebuilder.into();let rebuilder_bytes : &[u8] = &rebuilder_base.to_bytes();assert_eq!(rebuilder_bytes, raw_bytes);}_ => {panic!("Couldn't parse device_reset_cmd 5760 {:#02x?}", core_command_packet); }}}_ => {panic!("Couldn't parse core_command 5761 {:#02x?}", uci_command_packet); }}}_ => {panic!("Couldn't parse uci_command 5762 {:#02x?}", uci_packet_packet); }}},Err(e) => panic!("could not parse UciPacket: {:?} {:02x?}", e, raw_bytes),}})*}} 5763 device_reset_cmd_builder_tests! { device_reset_cmd_builder_test_00: b"\x20\x00\x00\x01\x00",} 5764 5765 #[derive(Debug)] 5766 struct DeviceResetRspData { 5767 status: StatusCode, 5768 } 5769 #[derive(Debug, Clone)] 5770 pub struct DeviceResetRspPacket { 5771 uci_packet: Arc<UciPacketData>, 5772 uci_response: Arc<UciResponseData>, 5773 core_response: Arc<CoreResponseData>, 5774 device_reset_rsp: Arc<DeviceResetRspData>, 5775 } 5776 #[derive(Debug)] 5777 pub struct DeviceResetRspBuilder { 5778 pub status: StatusCode, 5779 } 5780 impl DeviceResetRspData { conforms(bytes: &[u8]) -> bool5781 fn conforms(bytes: &[u8]) -> bool { 5782 if bytes.len() < 5 { 5783 return false; 5784 } 5785 true 5786 } parse(bytes: &[u8]) -> Result<Self>5787 fn parse(bytes: &[u8]) -> Result<Self> { 5788 if bytes.len() < 5 { 5789 return Err(Error::InvalidLengthError { 5790 obj: "DeviceResetRsp".to_string(), 5791 field: "status".to_string(), 5792 wanted: 5, 5793 got: bytes.len(), 5794 }); 5795 } 5796 let status = u8::from_le_bytes([bytes[4]]); 5797 let status = StatusCode::from_u8(status).ok_or_else(|| Error::InvalidEnumValueError { 5798 obj: "DeviceResetRsp".to_string(), 5799 field: "status".to_string(), 5800 value: status as u64, 5801 type_: "StatusCode".to_string(), 5802 })?; 5803 Ok(Self { status }) 5804 } write_to(&self, buffer: &mut BytesMut)5805 fn write_to(&self, buffer: &mut BytesMut) { 5806 let status = self.status.to_u8().unwrap(); 5807 buffer[4..5].copy_from_slice(&status.to_le_bytes()[0..1]); 5808 } get_total_size(&self) -> usize5809 fn get_total_size(&self) -> usize { 5810 self.get_size() 5811 } get_size(&self) -> usize5812 fn get_size(&self) -> usize { 5813 let ret = 0; 5814 let ret = ret + 1; 5815 ret 5816 } 5817 } 5818 impl Packet for DeviceResetRspPacket { to_bytes(self) -> Bytes5819 fn to_bytes(self) -> Bytes { 5820 let mut buffer = BytesMut::new(); 5821 buffer.resize(self.uci_packet.get_total_size(), 0); 5822 self.uci_packet.write_to(&mut buffer); 5823 buffer.freeze() 5824 } to_vec(self) -> Vec<u8>5825 fn to_vec(self) -> Vec<u8> { 5826 self.to_bytes().to_vec() 5827 } 5828 } 5829 impl From<DeviceResetRspPacket> for Bytes { from(packet: DeviceResetRspPacket) -> Self5830 fn from(packet: DeviceResetRspPacket) -> Self { 5831 packet.to_bytes() 5832 } 5833 } 5834 impl From<DeviceResetRspPacket> for Vec<u8> { from(packet: DeviceResetRspPacket) -> Self5835 fn from(packet: DeviceResetRspPacket) -> Self { 5836 packet.to_vec() 5837 } 5838 } 5839 impl TryFrom<UciPacketPacket> for DeviceResetRspPacket { 5840 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>5841 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 5842 Self::new(value.uci_packet).map_err(TryFromError) 5843 } 5844 } 5845 impl DeviceResetRspPacket { new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>5846 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 5847 let uci_packet = root; 5848 let uci_response = match &uci_packet.child { 5849 UciPacketDataChild::UciResponse(value) => (*value).clone(), 5850 _ => return Err("inconsistent state - child was not UciResponse"), 5851 }; 5852 let core_response = match &uci_response.child { 5853 UciResponseDataChild::CoreResponse(value) => (*value).clone(), 5854 _ => return Err("inconsistent state - child was not CoreResponse"), 5855 }; 5856 let device_reset_rsp = match &core_response.child { 5857 CoreResponseDataChild::DeviceResetRsp(value) => (*value).clone(), 5858 _ => return Err("inconsistent state - child was not DeviceResetRsp"), 5859 }; 5860 Ok(Self { 5861 uci_packet, 5862 uci_response, 5863 core_response, 5864 device_reset_rsp, 5865 }) 5866 } get_group_id(&self) -> GroupId5867 pub fn get_group_id(&self) -> GroupId { 5868 self.uci_packet.as_ref().group_id 5869 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag5870 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 5871 self.uci_packet.as_ref().packet_boundary_flag 5872 } get_message_type(&self) -> MessageType5873 pub fn get_message_type(&self) -> MessageType { 5874 self.uci_packet.as_ref().message_type 5875 } get_opcode(&self) -> u85876 pub fn get_opcode(&self) -> u8 { 5877 self.uci_packet.as_ref().opcode 5878 } get_status(&self) -> StatusCode5879 pub fn get_status(&self) -> StatusCode { 5880 self.device_reset_rsp.as_ref().status 5881 } 5882 } 5883 impl Into<UciPacketPacket> for DeviceResetRspPacket { into(self) -> UciPacketPacket5884 fn into(self) -> UciPacketPacket { 5885 UciPacketPacket::new(self.uci_packet).unwrap() 5886 } 5887 } 5888 impl Into<UciResponsePacket> for DeviceResetRspPacket { into(self) -> UciResponsePacket5889 fn into(self) -> UciResponsePacket { 5890 UciResponsePacket::new(self.uci_packet).unwrap() 5891 } 5892 } 5893 impl Into<CoreResponsePacket> for DeviceResetRspPacket { into(self) -> CoreResponsePacket5894 fn into(self) -> CoreResponsePacket { 5895 CoreResponsePacket::new(self.uci_packet).unwrap() 5896 } 5897 } 5898 impl DeviceResetRspBuilder { build(self) -> DeviceResetRspPacket5899 pub fn build(self) -> DeviceResetRspPacket { 5900 let device_reset_rsp = Arc::new(DeviceResetRspData { 5901 status: self.status, 5902 }); 5903 let core_response = Arc::new(CoreResponseData { 5904 child: CoreResponseDataChild::DeviceResetRsp(device_reset_rsp), 5905 }); 5906 let uci_response = Arc::new(UciResponseData { 5907 child: UciResponseDataChild::CoreResponse(core_response), 5908 }); 5909 let uci_packet = Arc::new(UciPacketData { 5910 group_id: GroupId::Core, 5911 packet_boundary_flag: PacketBoundaryFlag::Complete, 5912 message_type: MessageType::Response, 5913 opcode: 0, 5914 child: UciPacketDataChild::UciResponse(uci_response), 5915 }); 5916 DeviceResetRspPacket::new(uci_packet).unwrap() 5917 } 5918 } 5919 impl Into<UciPacketPacket> for DeviceResetRspBuilder { into(self) -> UciPacketPacket5920 fn into(self) -> UciPacketPacket { 5921 self.build().into() 5922 } 5923 } 5924 impl Into<UciResponsePacket> for DeviceResetRspBuilder { into(self) -> UciResponsePacket5925 fn into(self) -> UciResponsePacket { 5926 self.build().into() 5927 } 5928 } 5929 impl Into<CoreResponsePacket> for DeviceResetRspBuilder { into(self) -> CoreResponsePacket5930 fn into(self) -> CoreResponsePacket { 5931 self.build().into() 5932 } 5933 } 5934 macro_rules! device_reset_rsp_builder_tests { ($($name:ident: $byte_string:expr,)*) => {$( 5935 #[test] 5936 pub fn $name() { let raw_bytes = $byte_string;/* (0) */ 5937 match UciPacketPacket::parse(raw_bytes) {Ok(uci_packet_packet) => {match uci_packet_packet.specialize() {/* (1) */ 5938 UciPacketChild::UciResponse(uci_response_packet) => {match uci_response_packet.specialize() {/* (2) */ 5939 UciResponseChild::CoreResponse(core_response_packet) => {match core_response_packet.specialize() {/* (3) */ 5940 CoreResponseChild::DeviceResetRsp(packet) => {let rebuilder = DeviceResetRspBuilder {status : packet.get_status(),};let rebuilder_base : UciPacketPacket = rebuilder.into();let rebuilder_bytes : &[u8] = &rebuilder_base.to_bytes();assert_eq!(rebuilder_bytes, raw_bytes);}_ => {panic!("Couldn't parse device_reset_rsp 5941 {:#02x?}", core_response_packet); }}}_ => {panic!("Couldn't parse core_response 5942 {:#02x?}", uci_response_packet); }}}_ => {panic!("Couldn't parse uci_response 5943 {:#02x?}", uci_packet_packet); }}},Err(e) => panic!("could not parse UciPacket: {:?} {:02x?}", e, raw_bytes),}})*}} 5944 device_reset_rsp_builder_tests! { device_reset_rsp_builder_test_00: b"\x40\x00\x00\x01\x00",} 5945 5946 #[derive(Debug)] 5947 struct DeviceStatusNtfData { 5948 device_state: DeviceState, 5949 } 5950 #[derive(Debug, Clone)] 5951 pub struct DeviceStatusNtfPacket { 5952 uci_packet: Arc<UciPacketData>, 5953 uci_notification: Arc<UciNotificationData>, 5954 core_notification: Arc<CoreNotificationData>, 5955 device_status_ntf: Arc<DeviceStatusNtfData>, 5956 } 5957 #[derive(Debug)] 5958 pub struct DeviceStatusNtfBuilder { 5959 pub device_state: DeviceState, 5960 } 5961 impl DeviceStatusNtfData { conforms(bytes: &[u8]) -> bool5962 fn conforms(bytes: &[u8]) -> bool { 5963 if bytes.len() < 5 { 5964 return false; 5965 } 5966 true 5967 } parse(bytes: &[u8]) -> Result<Self>5968 fn parse(bytes: &[u8]) -> Result<Self> { 5969 if bytes.len() < 5 { 5970 return Err(Error::InvalidLengthError { 5971 obj: "DeviceStatusNtf".to_string(), 5972 field: "device_state".to_string(), 5973 wanted: 5, 5974 got: bytes.len(), 5975 }); 5976 } 5977 let device_state = u8::from_le_bytes([bytes[4]]); 5978 let device_state = 5979 DeviceState::from_u8(device_state).ok_or_else(|| Error::InvalidEnumValueError { 5980 obj: "DeviceStatusNtf".to_string(), 5981 field: "device_state".to_string(), 5982 value: device_state as u64, 5983 type_: "DeviceState".to_string(), 5984 })?; 5985 Ok(Self { device_state }) 5986 } write_to(&self, buffer: &mut BytesMut)5987 fn write_to(&self, buffer: &mut BytesMut) { 5988 let device_state = self.device_state.to_u8().unwrap(); 5989 buffer[4..5].copy_from_slice(&device_state.to_le_bytes()[0..1]); 5990 } get_total_size(&self) -> usize5991 fn get_total_size(&self) -> usize { 5992 self.get_size() 5993 } get_size(&self) -> usize5994 fn get_size(&self) -> usize { 5995 let ret = 0; 5996 let ret = ret + 1; 5997 ret 5998 } 5999 } 6000 impl Packet for DeviceStatusNtfPacket { to_bytes(self) -> Bytes6001 fn to_bytes(self) -> Bytes { 6002 let mut buffer = BytesMut::new(); 6003 buffer.resize(self.uci_packet.get_total_size(), 0); 6004 self.uci_packet.write_to(&mut buffer); 6005 buffer.freeze() 6006 } to_vec(self) -> Vec<u8>6007 fn to_vec(self) -> Vec<u8> { 6008 self.to_bytes().to_vec() 6009 } 6010 } 6011 impl From<DeviceStatusNtfPacket> for Bytes { from(packet: DeviceStatusNtfPacket) -> Self6012 fn from(packet: DeviceStatusNtfPacket) -> Self { 6013 packet.to_bytes() 6014 } 6015 } 6016 impl From<DeviceStatusNtfPacket> for Vec<u8> { from(packet: DeviceStatusNtfPacket) -> Self6017 fn from(packet: DeviceStatusNtfPacket) -> Self { 6018 packet.to_vec() 6019 } 6020 } 6021 impl TryFrom<UciPacketPacket> for DeviceStatusNtfPacket { 6022 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>6023 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 6024 Self::new(value.uci_packet).map_err(TryFromError) 6025 } 6026 } 6027 impl DeviceStatusNtfPacket { new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>6028 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 6029 let uci_packet = root; 6030 let uci_notification = match &uci_packet.child { 6031 UciPacketDataChild::UciNotification(value) => (*value).clone(), 6032 _ => return Err("inconsistent state - child was not UciNotification"), 6033 }; 6034 let core_notification = match &uci_notification.child { 6035 UciNotificationDataChild::CoreNotification(value) => (*value).clone(), 6036 _ => return Err("inconsistent state - child was not CoreNotification"), 6037 }; 6038 let device_status_ntf = match &core_notification.child { 6039 CoreNotificationDataChild::DeviceStatusNtf(value) => (*value).clone(), 6040 _ => return Err("inconsistent state - child was not DeviceStatusNtf"), 6041 }; 6042 Ok(Self { 6043 uci_packet, 6044 uci_notification, 6045 core_notification, 6046 device_status_ntf, 6047 }) 6048 } get_group_id(&self) -> GroupId6049 pub fn get_group_id(&self) -> GroupId { 6050 self.uci_packet.as_ref().group_id 6051 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag6052 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 6053 self.uci_packet.as_ref().packet_boundary_flag 6054 } get_message_type(&self) -> MessageType6055 pub fn get_message_type(&self) -> MessageType { 6056 self.uci_packet.as_ref().message_type 6057 } get_opcode(&self) -> u86058 pub fn get_opcode(&self) -> u8 { 6059 self.uci_packet.as_ref().opcode 6060 } get_device_state(&self) -> DeviceState6061 pub fn get_device_state(&self) -> DeviceState { 6062 self.device_status_ntf.as_ref().device_state 6063 } 6064 } 6065 impl Into<UciPacketPacket> for DeviceStatusNtfPacket { into(self) -> UciPacketPacket6066 fn into(self) -> UciPacketPacket { 6067 UciPacketPacket::new(self.uci_packet).unwrap() 6068 } 6069 } 6070 impl Into<UciNotificationPacket> for DeviceStatusNtfPacket { into(self) -> UciNotificationPacket6071 fn into(self) -> UciNotificationPacket { 6072 UciNotificationPacket::new(self.uci_packet).unwrap() 6073 } 6074 } 6075 impl Into<CoreNotificationPacket> for DeviceStatusNtfPacket { into(self) -> CoreNotificationPacket6076 fn into(self) -> CoreNotificationPacket { 6077 CoreNotificationPacket::new(self.uci_packet).unwrap() 6078 } 6079 } 6080 impl DeviceStatusNtfBuilder { build(self) -> DeviceStatusNtfPacket6081 pub fn build(self) -> DeviceStatusNtfPacket { 6082 let device_status_ntf = Arc::new(DeviceStatusNtfData { 6083 device_state: self.device_state, 6084 }); 6085 let core_notification = Arc::new(CoreNotificationData { 6086 child: CoreNotificationDataChild::DeviceStatusNtf(device_status_ntf), 6087 }); 6088 let uci_notification = Arc::new(UciNotificationData { 6089 child: UciNotificationDataChild::CoreNotification(core_notification), 6090 }); 6091 let uci_packet = Arc::new(UciPacketData { 6092 group_id: GroupId::Core, 6093 packet_boundary_flag: PacketBoundaryFlag::Complete, 6094 message_type: MessageType::Notification, 6095 opcode: 1, 6096 child: UciPacketDataChild::UciNotification(uci_notification), 6097 }); 6098 DeviceStatusNtfPacket::new(uci_packet).unwrap() 6099 } 6100 } 6101 impl Into<UciPacketPacket> for DeviceStatusNtfBuilder { into(self) -> UciPacketPacket6102 fn into(self) -> UciPacketPacket { 6103 self.build().into() 6104 } 6105 } 6106 impl Into<UciNotificationPacket> for DeviceStatusNtfBuilder { into(self) -> UciNotificationPacket6107 fn into(self) -> UciNotificationPacket { 6108 self.build().into() 6109 } 6110 } 6111 impl Into<CoreNotificationPacket> for DeviceStatusNtfBuilder { into(self) -> CoreNotificationPacket6112 fn into(self) -> CoreNotificationPacket { 6113 self.build().into() 6114 } 6115 } 6116 macro_rules! device_status_ntf_builder_tests { ($($name:ident: $byte_string:expr,)*) => {$( 6117 #[test] 6118 pub fn $name() { let raw_bytes = $byte_string;/* (0) */ 6119 match UciPacketPacket::parse(raw_bytes) {Ok(uci_packet_packet) => {match uci_packet_packet.specialize() {/* (1) */ 6120 UciPacketChild::UciNotification(uci_notification_packet) => {match uci_notification_packet.specialize() {/* (2) */ 6121 UciNotificationChild::CoreNotification(core_notification_packet) => {match core_notification_packet.specialize() {/* (3) */ 6122 CoreNotificationChild::DeviceStatusNtf(packet) => {let rebuilder = DeviceStatusNtfBuilder {device_state : packet.get_device_state(),};let rebuilder_base : UciPacketPacket = rebuilder.into();let rebuilder_bytes : &[u8] = &rebuilder_base.to_bytes();assert_eq!(rebuilder_bytes, raw_bytes);}_ => {panic!("Couldn't parse device_status_ntf 6123 {:#02x?}", core_notification_packet); }}}_ => {panic!("Couldn't parse core_notification 6124 {:#02x?}", uci_notification_packet); }}}_ => {panic!("Couldn't parse uci_notification 6125 {:#02x?}", uci_packet_packet); }}},Err(e) => panic!("could not parse UciPacket: {:?} {:02x?}", e, raw_bytes),}})*}} 6126 device_status_ntf_builder_tests! { device_status_ntf_builder_test_00: b"\x60\x01\x00\x01\x01",} 6127 6128 #[derive(Debug)] 6129 struct GetDeviceInfoCmdData {} 6130 #[derive(Debug, Clone)] 6131 pub struct GetDeviceInfoCmdPacket { 6132 uci_packet: Arc<UciPacketData>, 6133 uci_command: Arc<UciCommandData>, 6134 core_command: Arc<CoreCommandData>, 6135 get_device_info_cmd: Arc<GetDeviceInfoCmdData>, 6136 } 6137 #[derive(Debug)] 6138 pub struct GetDeviceInfoCmdBuilder {} 6139 impl GetDeviceInfoCmdData { conforms(bytes: &[u8]) -> bool6140 fn conforms(bytes: &[u8]) -> bool { 6141 if bytes.len() < 4 { 6142 return false; 6143 } 6144 true 6145 } parse(bytes: &[u8]) -> Result<Self>6146 fn parse(bytes: &[u8]) -> Result<Self> { 6147 Ok(Self {}) 6148 } write_to(&self, buffer: &mut BytesMut)6149 fn write_to(&self, buffer: &mut BytesMut) {} get_total_size(&self) -> usize6150 fn get_total_size(&self) -> usize { 6151 self.get_size() 6152 } get_size(&self) -> usize6153 fn get_size(&self) -> usize { 6154 let ret = 0; 6155 ret 6156 } 6157 } 6158 impl Packet for GetDeviceInfoCmdPacket { to_bytes(self) -> Bytes6159 fn to_bytes(self) -> Bytes { 6160 let mut buffer = BytesMut::new(); 6161 buffer.resize(self.uci_packet.get_total_size(), 0); 6162 self.uci_packet.write_to(&mut buffer); 6163 buffer.freeze() 6164 } to_vec(self) -> Vec<u8>6165 fn to_vec(self) -> Vec<u8> { 6166 self.to_bytes().to_vec() 6167 } 6168 } 6169 impl From<GetDeviceInfoCmdPacket> for Bytes { from(packet: GetDeviceInfoCmdPacket) -> Self6170 fn from(packet: GetDeviceInfoCmdPacket) -> Self { 6171 packet.to_bytes() 6172 } 6173 } 6174 impl From<GetDeviceInfoCmdPacket> for Vec<u8> { from(packet: GetDeviceInfoCmdPacket) -> Self6175 fn from(packet: GetDeviceInfoCmdPacket) -> Self { 6176 packet.to_vec() 6177 } 6178 } 6179 impl TryFrom<UciPacketPacket> for GetDeviceInfoCmdPacket { 6180 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>6181 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 6182 Self::new(value.uci_packet).map_err(TryFromError) 6183 } 6184 } 6185 impl GetDeviceInfoCmdPacket { new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>6186 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 6187 let uci_packet = root; 6188 let uci_command = match &uci_packet.child { 6189 UciPacketDataChild::UciCommand(value) => (*value).clone(), 6190 _ => return Err("inconsistent state - child was not UciCommand"), 6191 }; 6192 let core_command = match &uci_command.child { 6193 UciCommandDataChild::CoreCommand(value) => (*value).clone(), 6194 _ => return Err("inconsistent state - child was not CoreCommand"), 6195 }; 6196 let get_device_info_cmd = match &core_command.child { 6197 CoreCommandDataChild::GetDeviceInfoCmd(value) => (*value).clone(), 6198 _ => return Err("inconsistent state - child was not GetDeviceInfoCmd"), 6199 }; 6200 Ok(Self { 6201 uci_packet, 6202 uci_command, 6203 core_command, 6204 get_device_info_cmd, 6205 }) 6206 } get_group_id(&self) -> GroupId6207 pub fn get_group_id(&self) -> GroupId { 6208 self.uci_packet.as_ref().group_id 6209 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag6210 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 6211 self.uci_packet.as_ref().packet_boundary_flag 6212 } get_message_type(&self) -> MessageType6213 pub fn get_message_type(&self) -> MessageType { 6214 self.uci_packet.as_ref().message_type 6215 } get_opcode(&self) -> u86216 pub fn get_opcode(&self) -> u8 { 6217 self.uci_packet.as_ref().opcode 6218 } 6219 } 6220 impl Into<UciPacketPacket> for GetDeviceInfoCmdPacket { into(self) -> UciPacketPacket6221 fn into(self) -> UciPacketPacket { 6222 UciPacketPacket::new(self.uci_packet).unwrap() 6223 } 6224 } 6225 impl Into<UciCommandPacket> for GetDeviceInfoCmdPacket { into(self) -> UciCommandPacket6226 fn into(self) -> UciCommandPacket { 6227 UciCommandPacket::new(self.uci_packet).unwrap() 6228 } 6229 } 6230 impl Into<CoreCommandPacket> for GetDeviceInfoCmdPacket { into(self) -> CoreCommandPacket6231 fn into(self) -> CoreCommandPacket { 6232 CoreCommandPacket::new(self.uci_packet).unwrap() 6233 } 6234 } 6235 impl GetDeviceInfoCmdBuilder { build(self) -> GetDeviceInfoCmdPacket6236 pub fn build(self) -> GetDeviceInfoCmdPacket { 6237 let get_device_info_cmd = Arc::new(GetDeviceInfoCmdData {}); 6238 let core_command = Arc::new(CoreCommandData { 6239 child: CoreCommandDataChild::GetDeviceInfoCmd(get_device_info_cmd), 6240 }); 6241 let uci_command = Arc::new(UciCommandData { 6242 child: UciCommandDataChild::CoreCommand(core_command), 6243 }); 6244 let uci_packet = Arc::new(UciPacketData { 6245 group_id: GroupId::Core, 6246 packet_boundary_flag: PacketBoundaryFlag::Complete, 6247 message_type: MessageType::Command, 6248 opcode: 2, 6249 child: UciPacketDataChild::UciCommand(uci_command), 6250 }); 6251 GetDeviceInfoCmdPacket::new(uci_packet).unwrap() 6252 } 6253 } 6254 impl Into<UciPacketPacket> for GetDeviceInfoCmdBuilder { into(self) -> UciPacketPacket6255 fn into(self) -> UciPacketPacket { 6256 self.build().into() 6257 } 6258 } 6259 impl Into<UciCommandPacket> for GetDeviceInfoCmdBuilder { into(self) -> UciCommandPacket6260 fn into(self) -> UciCommandPacket { 6261 self.build().into() 6262 } 6263 } 6264 impl Into<CoreCommandPacket> for GetDeviceInfoCmdBuilder { into(self) -> CoreCommandPacket6265 fn into(self) -> CoreCommandPacket { 6266 self.build().into() 6267 } 6268 } 6269 macro_rules! get_device_info_cmd_builder_tests { ($($name:ident: $byte_string:expr,)*) => {$( 6270 #[test] 6271 pub fn $name() { let raw_bytes = $byte_string;/* (0) */ 6272 match UciPacketPacket::parse(raw_bytes) {Ok(uci_packet_packet) => {match uci_packet_packet.specialize() {/* (1) */ 6273 UciPacketChild::UciCommand(uci_command_packet) => {match uci_command_packet.specialize() {/* (2) */ 6274 UciCommandChild::CoreCommand(core_command_packet) => {match core_command_packet.specialize() {/* (3) */ 6275 CoreCommandChild::GetDeviceInfoCmd(packet) => {let rebuilder = GetDeviceInfoCmdBuilder {};let rebuilder_base : UciPacketPacket = rebuilder.into();let rebuilder_bytes : &[u8] = &rebuilder_base.to_bytes();assert_eq!(rebuilder_bytes, raw_bytes);}_ => {panic!("Couldn't parse get_device_info_cmd 6276 {:#02x?}", core_command_packet); }}}_ => {panic!("Couldn't parse core_command 6277 {:#02x?}", uci_command_packet); }}}_ => {panic!("Couldn't parse uci_command 6278 {:#02x?}", uci_packet_packet); }}},Err(e) => panic!("could not parse UciPacket: {:?} {:02x?}", e, raw_bytes),}})*}} 6279 get_device_info_cmd_builder_tests! { get_device_info_cmd_builder_test_00: b"\x20\x02\x00\x00",} 6280 6281 #[derive(Debug)] 6282 struct GetDeviceInfoRspData { 6283 status: StatusCode, 6284 uci_version: u16, 6285 mac_version: u16, 6286 phy_version: u16, 6287 uci_test_version: u16, 6288 vendor_spec_info: Vec<u8>, 6289 } 6290 #[derive(Debug, Clone)] 6291 pub struct GetDeviceInfoRspPacket { 6292 uci_packet: Arc<UciPacketData>, 6293 uci_response: Arc<UciResponseData>, 6294 core_response: Arc<CoreResponseData>, 6295 get_device_info_rsp: Arc<GetDeviceInfoRspData>, 6296 } 6297 #[derive(Debug)] 6298 pub struct GetDeviceInfoRspBuilder { 6299 pub status: StatusCode, 6300 pub uci_version: u16, 6301 pub mac_version: u16, 6302 pub phy_version: u16, 6303 pub uci_test_version: u16, 6304 pub vendor_spec_info: Vec<u8>, 6305 } 6306 impl GetDeviceInfoRspData { conforms(bytes: &[u8]) -> bool6307 fn conforms(bytes: &[u8]) -> bool { 6308 if bytes.len() < 14 { 6309 return false; 6310 } 6311 true 6312 } parse(bytes: &[u8]) -> Result<Self>6313 fn parse(bytes: &[u8]) -> Result<Self> { 6314 if bytes.len() < 5 { 6315 return Err(Error::InvalidLengthError { 6316 obj: "GetDeviceInfoRsp".to_string(), 6317 field: "status".to_string(), 6318 wanted: 5, 6319 got: bytes.len(), 6320 }); 6321 } 6322 let status = u8::from_le_bytes([bytes[4]]); 6323 let status = StatusCode::from_u8(status).ok_or_else(|| Error::InvalidEnumValueError { 6324 obj: "GetDeviceInfoRsp".to_string(), 6325 field: "status".to_string(), 6326 value: status as u64, 6327 type_: "StatusCode".to_string(), 6328 })?; 6329 if bytes.len() < 7 { 6330 return Err(Error::InvalidLengthError { 6331 obj: "GetDeviceInfoRsp".to_string(), 6332 field: "uci_version".to_string(), 6333 wanted: 7, 6334 got: bytes.len(), 6335 }); 6336 } 6337 let uci_version = u16::from_le_bytes([bytes[5], bytes[6]]); 6338 if bytes.len() < 9 { 6339 return Err(Error::InvalidLengthError { 6340 obj: "GetDeviceInfoRsp".to_string(), 6341 field: "mac_version".to_string(), 6342 wanted: 9, 6343 got: bytes.len(), 6344 }); 6345 } 6346 let mac_version = u16::from_le_bytes([bytes[7], bytes[8]]); 6347 if bytes.len() < 11 { 6348 return Err(Error::InvalidLengthError { 6349 obj: "GetDeviceInfoRsp".to_string(), 6350 field: "phy_version".to_string(), 6351 wanted: 11, 6352 got: bytes.len(), 6353 }); 6354 } 6355 let phy_version = u16::from_le_bytes([bytes[9], bytes[10]]); 6356 if bytes.len() < 13 { 6357 return Err(Error::InvalidLengthError { 6358 obj: "GetDeviceInfoRsp".to_string(), 6359 field: "uci_test_version".to_string(), 6360 wanted: 13, 6361 got: bytes.len(), 6362 }); 6363 } 6364 let uci_test_version = u16::from_le_bytes([bytes[11], bytes[12]]); 6365 if bytes.len() < 14 { 6366 return Err(Error::InvalidLengthError { 6367 obj: "GetDeviceInfoRsp".to_string(), 6368 field: "vendor_spec_info_count".to_string(), 6369 wanted: 14, 6370 got: bytes.len(), 6371 }); 6372 } 6373 let vendor_spec_info_count = u8::from_le_bytes([bytes[13]]); 6374 let want_ = 14 + ((vendor_spec_info_count as usize) * 1); 6375 if bytes.len() < want_ { 6376 return Err(Error::InvalidLengthError { 6377 obj: "GetDeviceInfoRsp".to_string(), 6378 field: "vendor_spec_info".to_string(), 6379 wanted: want_, 6380 got: bytes.len(), 6381 }); 6382 } 6383 let vendor_spec_info: Vec<u8> = bytes[14..14 + ((vendor_spec_info_count as usize) * 1)] 6384 .to_vec() 6385 .chunks_exact(1) 6386 .into_iter() 6387 .map(|i| u8::from_le_bytes([i[0]])) 6388 .collect(); 6389 Ok(Self { 6390 status, 6391 uci_version, 6392 mac_version, 6393 phy_version, 6394 uci_test_version, 6395 vendor_spec_info, 6396 }) 6397 } write_to(&self, buffer: &mut BytesMut)6398 fn write_to(&self, buffer: &mut BytesMut) { 6399 let status = self.status.to_u8().unwrap(); 6400 buffer[4..5].copy_from_slice(&status.to_le_bytes()[0..1]); 6401 let uci_version = self.uci_version; 6402 buffer[5..7].copy_from_slice(&uci_version.to_le_bytes()[0..2]); 6403 let mac_version = self.mac_version; 6404 buffer[7..9].copy_from_slice(&mac_version.to_le_bytes()[0..2]); 6405 let phy_version = self.phy_version; 6406 buffer[9..11].copy_from_slice(&phy_version.to_le_bytes()[0..2]); 6407 let uci_test_version = self.uci_test_version; 6408 buffer[11..13].copy_from_slice(&uci_test_version.to_le_bytes()[0..2]); 6409 buffer[13..14].copy_from_slice(&(self.vendor_spec_info.len() as u8).to_le_bytes()); 6410 for (i, e) in self.vendor_spec_info.iter().enumerate() { 6411 buffer[14 + i..14 + i + 1].copy_from_slice(&e.to_le_bytes()) 6412 } 6413 } get_total_size(&self) -> usize6414 fn get_total_size(&self) -> usize { 6415 self.get_size() 6416 } get_size(&self) -> usize6417 fn get_size(&self) -> usize { 6418 let ret = 0; 6419 let ret = ret + 10; 6420 let ret = ret + (self.vendor_spec_info.len() * ((/* Bits: */8 + /* Dynamic: */ 0) / 8)); 6421 ret 6422 } 6423 } 6424 impl Packet for GetDeviceInfoRspPacket { to_bytes(self) -> Bytes6425 fn to_bytes(self) -> Bytes { 6426 let mut buffer = BytesMut::new(); 6427 buffer.resize(self.uci_packet.get_total_size(), 0); 6428 self.uci_packet.write_to(&mut buffer); 6429 buffer.freeze() 6430 } to_vec(self) -> Vec<u8>6431 fn to_vec(self) -> Vec<u8> { 6432 self.to_bytes().to_vec() 6433 } 6434 } 6435 impl From<GetDeviceInfoRspPacket> for Bytes { from(packet: GetDeviceInfoRspPacket) -> Self6436 fn from(packet: GetDeviceInfoRspPacket) -> Self { 6437 packet.to_bytes() 6438 } 6439 } 6440 impl From<GetDeviceInfoRspPacket> for Vec<u8> { from(packet: GetDeviceInfoRspPacket) -> Self6441 fn from(packet: GetDeviceInfoRspPacket) -> Self { 6442 packet.to_vec() 6443 } 6444 } 6445 impl TryFrom<UciPacketPacket> for GetDeviceInfoRspPacket { 6446 type Error = TryFromError; try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error>6447 fn try_from(value: UciPacketPacket) -> std::result::Result<Self, Self::Error> { 6448 Self::new(value.uci_packet).map_err(TryFromError) 6449 } 6450 } 6451 impl GetDeviceInfoRspPacket { new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str>6452 fn new(root: Arc<UciPacketData>) -> std::result::Result<Self, &'static str> { 6453 let uci_packet = root; 6454 let uci_response = match &uci_packet.child { 6455 UciPacketDataChild::UciResponse(value) => (*value).clone(), 6456 _ => return Err("inconsistent state - child was not UciResponse"), 6457 }; 6458 let core_response = match &uci_response.child { 6459 UciResponseDataChild::CoreResponse(value) => (*value).clone(), 6460 _ => return Err("inconsistent state - child was not CoreResponse"), 6461 }; 6462 let get_device_info_rsp = match &core_response.child { 6463 CoreResponseDataChild::GetDeviceInfoRsp(value) => (*value).clone(), 6464 _ => return Err("inconsistent state - child was not GetDeviceInfoRsp"), 6465 }; 6466 Ok(Self { 6467 uci_packet, 6468 uci_response, 6469 core_response, 6470 get_device_info_rsp, 6471 }) 6472 } get_group_id(&self) -> GroupId6473 pub fn get_group_id(&self) -> GroupId { 6474 self.uci_packet.as_ref().group_id 6475 } get_packet_boundary_flag(&self) -> PacketBoundaryFlag6476 pub fn get_packet_boundary_flag(&self) -> PacketBoundaryFlag { 6477 self.uci_packet.as_ref().packet_boundary_flag 6478 } get_message_type(&self) -> MessageType6479 pub fn get_message_type(&self) -> MessageType { 6480 self.uci_packet.as_ref().message_type 6481 } get_opcode(&self) -> u86482 pub fn get_opcode(&self) -> u8 { 6483 self.uci_packet.as_ref().opcode 6484 } get_status(&self) -> StatusCode6485 pub fn get_status(&self) -> StatusCode { 6486 self.get_device_info_rsp.as_ref().status 6487 } get_uci_version(&self) -> u166488 pub fn get_uci_version(&self) -> u16 { 6489 self.get_device_info_rsp.as_ref().uci_version 6490 } get_mac_version(&self) -> u166491 pub fn get_mac_version(&self) -> u16 { 6492 self.get_device_info_rsp.as_ref().mac_version 6493 } get_phy_version(&self) -> u166494 pub fn get_phy_version(&self) -> u16 { 6495 self.get_device_info_rsp.as_ref().phy_version 6496 } get_uci_test_version(&self) -> u166497 pub fn get_uci_test_version(&self) -> u16 { 6498 self.get_device_info_rsp.as_ref().uci_test_version 6499 } get_vendor_spec_info(&self) -> &Vec<u8>6500