Line data Source code
1 : /** 2 : * 3 : * Copyright (c) 2020 Project CHIP Authors 4 : * Copyright (c) 2016-2017 Nest Labs, Inc. 5 : * 6 : * Licensed under the Apache License, Version 2.0 (the "License"); 7 : * you may not use this file except in compliance with the License. 8 : * You may obtain a copy of the License at 9 : * 10 : * http://www.apache.org/licenses/LICENSE-2.0 11 : * 12 : * Unless required by applicable law or agreed to in writing, software 13 : * distributed under the License is distributed on an "AS IS" BASIS, 14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 : * See the License for the specific language governing permissions and 16 : * limitations under the License. 17 : */ 18 : /** 19 : * @file 20 : * This file defines Status Information Block in Interaction Model 21 : * 22 : */ 23 : 24 : #pragma once 25 : 26 : #include "StructBuilder.h" 27 : #include "StructParser.h" 28 : 29 : #include <app/AppConfig.h> 30 : #include <app/util/basic-types.h> 31 : #include <lib/core/CHIPCore.h> 32 : #include <lib/core/Optional.h> 33 : #include <lib/core/TLV.h> 34 : #include <lib/support/CodeUtils.h> 35 : #include <lib/support/logging/CHIPLogging.h> 36 : #include <protocols/interaction_model/Constants.h> 37 : #include <protocols/secure_channel/Constants.h> 38 : 39 : namespace chip { 40 : namespace app { 41 : struct StatusIB 42 : { 43 13638 : StatusIB() = default; 44 2439 : StatusIB(Protocols::InteractionModel::Status imStatus) : mStatus(imStatus) {} 45 2 : StatusIB(Protocols::InteractionModel::Status imStatus, ClusterStatus clusterStatus) : 46 2 : mStatus(imStatus), mClusterStatus(clusterStatus) 47 2 : {} 48 5 : explicit StatusIB(CHIP_ERROR error) { InitFromChipError(error); } 49 : 50 : enum class Tag : uint8_t 51 : { 52 : kStatus = 0, 53 : kClusterStatus = 1, 54 : }; 55 : 56 : class Parser : public StructParser 57 : { 58 : public: 59 : #if CHIP_CONFIG_IM_PRETTY_PRINT 60 : CHIP_ERROR PrettyPrint() const; 61 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT 62 : /** 63 : * Decode the StatusIB 64 : * 65 : * @return CHIP_ERROR codes returned by chip::TLV objects. CHIP_END_OF_TLV if either 66 : * element is missing. CHIP_ERROR_WRONG_TLV_TYPE if the elements are of the wrong 67 : * type. 68 : */ 69 : CHIP_ERROR DecodeStatusIB(StatusIB & aStatusIB) const; 70 : }; 71 : 72 : class Builder : public StructBuilder 73 : { 74 : public: 75 : /** 76 : * Write the StatusIB into TLV and close the container 77 : * 78 : * @return CHIP_ERROR codes returned by chip::TLV objects. CHIP_END_OF_TLV if either 79 : * element is missing. CHIP_ERROR_WRONG_TLV_TYPE if the elements are of the wrong 80 : * type. 81 : */ 82 : StatusIB::Builder & EncodeStatusIB(const StatusIB & aStatusIB); 83 : }; 84 : 85 : /** 86 : * Encapsulate a StatusIB in a CHIP_ERROR. This can be done for any 87 : * StatusIB, but will treat all success codes (including cluster-specific 88 : * ones) as CHIP_NO_ERROR. The resulting CHIP_ERROR will either be 89 : * CHIP_NO_ERROR or test true for IsIMStatus(). 90 : */ 91 : CHIP_ERROR ToChipError() const; 92 : 93 : /** 94 : * Extract a CHIP_ERROR into this StatusIB. If IsIMStatus() is false for 95 : * the error, this might do a best-effort attempt to come up with a 96 : * corresponding StatusIB, defaulting to a generic Status::Failure. 97 : */ 98 : void InitFromChipError(CHIP_ERROR aError); 99 : 100 : /** 101 : * Test whether this status is a success. 102 : */ 103 2485 : bool IsSuccess() const { return mStatus == Protocols::InteractionModel::Status::Success; } 104 : 105 : /** 106 : * Test whether this status is a failure. 107 : */ 108 : bool IsFailure() const { return !IsSuccess(); } 109 : 110 : /** 111 : * Register the StatusIB error formatter. 112 : */ 113 : static void RegisterErrorFormatter(); 114 : 115 : Protocols::InteractionModel::Status mStatus = Protocols::InteractionModel::Status::Success; 116 : Optional<ClusterStatus> mClusterStatus = Optional<ClusterStatus>::Missing(); 117 : 118 : }; // struct StatusIB 119 : 120 : }; // namespace app 121 : }; // namespace chip