|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 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::objects::EncodeValue, |
| 20 | + interaction_model::{ |
| 21 | + messages::ib::{CmdPath, CmdStatus, InvResp}, |
| 22 | + messages::msg, |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +pub enum ExpectedInvResp { |
| 27 | + Cmd(CmdPath, u8), |
| 28 | + Status(CmdStatus), |
| 29 | +} |
| 30 | + |
| 31 | +pub fn assert_inv_response(resp: &msg::InvResp, expected: &[ExpectedInvResp]) { |
| 32 | + let mut index = 0; |
| 33 | + for inv_response in resp.inv_responses.unwrap().iter() { |
| 34 | + println!("Validating index {}", index); |
| 35 | + match expected[index] { |
| 36 | + ExpectedInvResp::Cmd(e_c, e_d) => match inv_response { |
| 37 | + InvResp::Cmd(c) => { |
| 38 | + assert_eq!(e_c, c.path); |
| 39 | + match c.data { |
| 40 | + EncodeValue::Tlv(t) => { |
| 41 | + assert_eq!(e_d, t.find_tag(0).unwrap().u8().unwrap()) |
| 42 | + } |
| 43 | + _ => panic!("Incorrect CmdDataType"), |
| 44 | + } |
| 45 | + } |
| 46 | + _ => { |
| 47 | + panic!("Invalid response, expected InvResponse::Cmd"); |
| 48 | + } |
| 49 | + }, |
| 50 | + ExpectedInvResp::Status(e_status) => match inv_response { |
| 51 | + InvResp::Status(status) => { |
| 52 | + assert_eq!(e_status, status); |
| 53 | + } |
| 54 | + _ => { |
| 55 | + panic!("Invalid response, expected InvResponse::Status"); |
| 56 | + } |
| 57 | + }, |
| 58 | + } |
| 59 | + println!("Index {} success", index); |
| 60 | + index += 1; |
| 61 | + } |
| 62 | + assert_eq!(index, expected.len()); |
| 63 | +} |
| 64 | + |
| 65 | +#[macro_export] |
| 66 | +macro_rules! cmd_data { |
| 67 | + ($path:ident, $data:literal) => { |
| 68 | + CmdData::new($path, EncodeValue::Value(&($data as u32))) |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +#[macro_export] |
| 73 | +macro_rules! echo_req { |
| 74 | + ($endpoint:literal, $data:literal) => { |
| 75 | + CmdData::new( |
| 76 | + CmdPath::new( |
| 77 | + Some($endpoint), |
| 78 | + Some(echo_cluster::ID), |
| 79 | + Some(echo_cluster::Commands::EchoReq as u16), |
| 80 | + ), |
| 81 | + EncodeValue::Value(&($data as u32)), |
| 82 | + ) |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +#[macro_export] |
| 87 | +macro_rules! echo_resp { |
| 88 | + ($endpoint:literal, $data:literal) => { |
| 89 | + ExpectedInvResp::Cmd( |
| 90 | + CmdPath::new( |
| 91 | + Some($endpoint), |
| 92 | + Some(echo_cluster::ID), |
| 93 | + Some(echo_cluster::Commands::EchoResp as u16), |
| 94 | + ), |
| 95 | + $data, |
| 96 | + ) |
| 97 | + }; |
| 98 | +} |
0 commit comments