|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2020-2022 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +use matter::{ |
| 19 | + data_model::{ |
| 20 | + cluster_basic_information as basic_info, cluster_on_off as onoff, |
| 21 | + objects::{EncodeValue, GlobalElements}, |
| 22 | + sdm::{admin_commissioning as adm_comm, general_commissioning as gen_comm, noc}, |
| 23 | + system_model::{access_control as acl, descriptor}, |
| 24 | + }, |
| 25 | + interaction_model::{ |
| 26 | + core::{IMStatusCode, OpCode}, |
| 27 | + messages::GenericPath, |
| 28 | + messages::{ |
| 29 | + ib::{AttrData, AttrPath, AttrResp}, |
| 30 | + msg::{ReadReq, ReportDataMsg, StatusResp}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + tlv::{self, ElementType, FromTLV, TLVElement, TagType, ToTLV}, |
| 34 | + transport::{ |
| 35 | + exchange::{self, Exchange}, |
| 36 | + udp::MAX_RX_BUF_SIZE, |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +use crate::{ |
| 41 | + attr_data, |
| 42 | + common::{ |
| 43 | + attributes::*, |
| 44 | + echo_cluster as echo, |
| 45 | + im_engine::{ImEngine, ImInput}, |
| 46 | + }, |
| 47 | +}; |
| 48 | + |
| 49 | +pub struct LongRead { |
| 50 | + im_engine: ImEngine, |
| 51 | +} |
| 52 | + |
| 53 | +impl LongRead { |
| 54 | + pub fn new() -> Self { |
| 55 | + let mut im_engine = ImEngine::new(); |
| 56 | + // Use the same exchange for all parts of the transaction |
| 57 | + im_engine.exch = Some(Exchange::new(1, 0, exchange::Role::Responder)); |
| 58 | + Self { im_engine } |
| 59 | + } |
| 60 | + |
| 61 | + pub fn process<'a>( |
| 62 | + &mut self, |
| 63 | + action: OpCode, |
| 64 | + data: &dyn ToTLV, |
| 65 | + data_out: &'a mut [u8], |
| 66 | + ) -> (u8, &'a mut [u8]) { |
| 67 | + let input = ImInput::new(action, data); |
| 68 | + let (response, output) = self.im_engine.process(&input, data_out); |
| 69 | + (response, output) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn wildcard_read_resp(part: u8) -> Vec<AttrResp<'static>> { |
| 74 | + // For brevity, we only check the AttrPath, not the actual 'data' |
| 75 | + let dont_care = ElementType::U8(0); |
| 76 | + let part1 = vec![ |
| 77 | + attr_data!(0, 29, GlobalElements::FeatureMap, dont_care), |
| 78 | + attr_data!(0, 29, GlobalElements::AttributeList, dont_care), |
| 79 | + attr_data!(0, 29, descriptor::Attributes::DeviceTypeList, dont_care), |
| 80 | + attr_data!(0, 29, descriptor::Attributes::ServerList, dont_care), |
| 81 | + attr_data!(0, 29, descriptor::Attributes::PartsList, dont_care), |
| 82 | + attr_data!(0, 29, descriptor::Attributes::ClientList, dont_care), |
| 83 | + attr_data!(0, 40, GlobalElements::FeatureMap, dont_care), |
| 84 | + attr_data!(0, 40, GlobalElements::AttributeList, dont_care), |
| 85 | + attr_data!(0, 40, basic_info::Attributes::DMRevision, dont_care), |
| 86 | + attr_data!(0, 40, basic_info::Attributes::VendorId, dont_care), |
| 87 | + attr_data!(0, 40, basic_info::Attributes::ProductId, dont_care), |
| 88 | + attr_data!(0, 40, basic_info::Attributes::HwVer, dont_care), |
| 89 | + attr_data!(0, 40, basic_info::Attributes::SwVer, dont_care), |
| 90 | + attr_data!(0, 40, basic_info::Attributes::SwVerString, dont_care), |
| 91 | + attr_data!(0, 40, basic_info::Attributes::SerialNo, dont_care), |
| 92 | + attr_data!(0, 48, GlobalElements::FeatureMap, dont_care), |
| 93 | + attr_data!(0, 48, GlobalElements::AttributeList, dont_care), |
| 94 | + attr_data!(0, 48, gen_comm::Attributes::BreadCrumb, dont_care), |
| 95 | + attr_data!(0, 48, gen_comm::Attributes::RegConfig, dont_care), |
| 96 | + attr_data!(0, 48, gen_comm::Attributes::LocationCapability, dont_care), |
| 97 | + attr_data!( |
| 98 | + 0, |
| 99 | + 48, |
| 100 | + gen_comm::Attributes::BasicCommissioningInfo, |
| 101 | + dont_care |
| 102 | + ), |
| 103 | + attr_data!(0, 49, GlobalElements::FeatureMap, dont_care), |
| 104 | + attr_data!(0, 49, GlobalElements::AttributeList, dont_care), |
| 105 | + attr_data!(0, 60, GlobalElements::FeatureMap, dont_care), |
| 106 | + attr_data!(0, 60, GlobalElements::AttributeList, dont_care), |
| 107 | + attr_data!(0, 60, adm_comm::Attributes::WindowStatus, dont_care), |
| 108 | + attr_data!(0, 60, adm_comm::Attributes::AdminFabricIndex, dont_care), |
| 109 | + attr_data!(0, 60, adm_comm::Attributes::AdminVendorId, dont_care), |
| 110 | + attr_data!(0, 62, GlobalElements::FeatureMap, dont_care), |
| 111 | + attr_data!(0, 62, GlobalElements::AttributeList, dont_care), |
| 112 | + attr_data!(0, 62, noc::Attributes::CurrentFabricIndex, dont_care), |
| 113 | + attr_data!(0, 62, noc::Attributes::Fabrics, dont_care), |
| 114 | + attr_data!(0, 62, noc::Attributes::SupportedFabrics, dont_care), |
| 115 | + attr_data!(0, 62, noc::Attributes::CommissionedFabrics, dont_care), |
| 116 | + attr_data!(0, 31, GlobalElements::FeatureMap, dont_care), |
| 117 | + attr_data!(0, 31, GlobalElements::AttributeList, dont_care), |
| 118 | + attr_data!(0, 31, acl::Attributes::Acl, dont_care), |
| 119 | + attr_data!(0, 31, acl::Attributes::Extension, dont_care), |
| 120 | + attr_data!(0, 31, acl::Attributes::SubjectsPerEntry, dont_care), |
| 121 | + attr_data!(0, 31, acl::Attributes::TargetsPerEntry, dont_care), |
| 122 | + attr_data!(0, 31, acl::Attributes::EntriesPerFabric, dont_care), |
| 123 | + attr_data!(0, echo::ID, GlobalElements::FeatureMap, dont_care), |
| 124 | + attr_data!(0, echo::ID, GlobalElements::AttributeList, dont_care), |
| 125 | + attr_data!(0, echo::ID, echo::Attributes::Att1, dont_care), |
| 126 | + attr_data!(0, echo::ID, echo::Attributes::Att2, dont_care), |
| 127 | + attr_data!(0, echo::ID, echo::Attributes::AttCustom, dont_care), |
| 128 | + attr_data!(1, 29, GlobalElements::FeatureMap, dont_care), |
| 129 | + attr_data!(1, 29, GlobalElements::AttributeList, dont_care), |
| 130 | + attr_data!(1, 29, descriptor::Attributes::DeviceTypeList, dont_care), |
| 131 | + ]; |
| 132 | + |
| 133 | + let part2 = vec![ |
| 134 | + attr_data!(1, 29, descriptor::Attributes::ServerList, dont_care), |
| 135 | + attr_data!(1, 29, descriptor::Attributes::PartsList, dont_care), |
| 136 | + attr_data!(1, 29, descriptor::Attributes::ClientList, dont_care), |
| 137 | + attr_data!(1, 6, GlobalElements::FeatureMap, dont_care), |
| 138 | + attr_data!(1, 6, GlobalElements::AttributeList, dont_care), |
| 139 | + attr_data!(1, 6, onoff::Attributes::OnOff, dont_care), |
| 140 | + attr_data!(1, echo::ID, GlobalElements::FeatureMap, dont_care), |
| 141 | + attr_data!(1, echo::ID, GlobalElements::AttributeList, dont_care), |
| 142 | + attr_data!(1, echo::ID, echo::Attributes::Att1, dont_care), |
| 143 | + attr_data!(1, echo::ID, echo::Attributes::Att2, dont_care), |
| 144 | + attr_data!(1, echo::ID, echo::Attributes::AttCustom, dont_care), |
| 145 | + ]; |
| 146 | + |
| 147 | + if part == 1 { |
| 148 | + part1 |
| 149 | + } else { |
| 150 | + part2 |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +#[test] |
| 155 | +fn test_long_read_success() { |
| 156 | + // Read the entire attribute database, which requires 2 reads to complete |
| 157 | + let _ = env_logger::try_init(); |
| 158 | + let mut lr = LongRead::new(); |
| 159 | + let mut output = [0_u8; MAX_RX_BUF_SIZE + 100]; |
| 160 | + |
| 161 | + let wc_path = GenericPath::new(None, None, None); |
| 162 | + |
| 163 | + let read_all = [AttrPath::new(&wc_path)]; |
| 164 | + let read_req = ReadReq::new(true).set_attr_requests(&read_all); |
| 165 | + let expected_part1 = wildcard_read_resp(1); |
| 166 | + let (out_code, out_data) = lr.process(OpCode::ReadRequest, &read_req, &mut output); |
| 167 | + let root = tlv::get_root_node_struct(out_data).unwrap(); |
| 168 | + let report_data = ReportDataMsg::from_tlv(&root).unwrap(); |
| 169 | + assert_attr_report_skip_data(&report_data, &expected_part1); |
| 170 | + assert_eq!(report_data.more_chunks, Some(true)); |
| 171 | + assert_eq!(out_code, OpCode::ReportData as u8); |
| 172 | + |
| 173 | + // Ask for the next read by sending a status report |
| 174 | + let status_report = StatusResp { |
| 175 | + status: IMStatusCode::Success, |
| 176 | + }; |
| 177 | + let expected_part2 = wildcard_read_resp(2); |
| 178 | + let (out_code, out_data) = lr.process(OpCode::StatusResponse, &status_report, &mut output); |
| 179 | + let root = tlv::get_root_node_struct(out_data).unwrap(); |
| 180 | + let report_data = ReportDataMsg::from_tlv(&root).unwrap(); |
| 181 | + assert_attr_report_skip_data(&report_data, &expected_part2); |
| 182 | + assert_eq!(report_data.more_chunks, None); |
| 183 | + assert_eq!(out_code, OpCode::ReportData as u8); |
| 184 | +} |
0 commit comments