yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Copyright (c) 2021 Project CHIP Authors |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <inttypes.h> |
| 18 | #include <stdarg.h> |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | #include "InvokeResponseMessage.h" |
| 22 | #include "MessageDefHelper.h" |
| 23 | |
Daniel Nicoara | 641e3ad | 2022-07-25 15:32:03 -0400 | [diff] [blame] | 24 | #include <app/AppConfig.h> |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 25 | |
| 26 | namespace chip { |
| 27 | namespace app { |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 28 | #if CHIP_CONFIG_IM_PRETTY_PRINT |
| 29 | CHIP_ERROR InvokeResponseMessage::Parser::PrettyPrint() const |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 30 | { |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 31 | CHIP_ERROR err = CHIP_NO_ERROR; |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 32 | TLV::TLVReader reader; |
| 33 | |
| 34 | PRETTY_PRINT("InvokeResponseMessage ="); |
| 35 | PRETTY_PRINT("{"); |
| 36 | |
| 37 | // make a copy of the reader |
| 38 | reader.Init(mReader); |
| 39 | |
| 40 | while (CHIP_NO_ERROR == (err = reader.Next())) |
| 41 | { |
Boris Zbarsky | 4432d08 | 2022-04-27 13:42:10 -0400 | [diff] [blame] | 42 | if (!TLV::IsContextTag(reader.GetTag())) |
| 43 | { |
| 44 | continue; |
| 45 | } |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 46 | uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag()); |
| 47 | switch (tagNum) |
| 48 | { |
| 49 | case to_underlying(Tag::kSuppressResponse): |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 50 | #if CHIP_DETAIL_LOGGING |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 51 | { |
| 52 | bool suppressResponse; |
| 53 | ReturnErrorOnFailure(reader.Get(suppressResponse)); |
| 54 | PRETTY_PRINT("\tsuppressResponse = %s, ", suppressResponse ? "true" : "false"); |
| 55 | } |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 56 | #endif // CHIP_DETAIL_LOGGING |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 57 | break; |
| 58 | case to_underlying(Tag::kInvokeResponses): { |
| 59 | InvokeResponseIBs::Parser invokeResponses; |
| 60 | ReturnErrorOnFailure(invokeResponses.Init(reader)); |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 61 | |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 62 | PRETTY_PRINT_INCDEPTH(); |
| 63 | ReturnErrorOnFailure(invokeResponses.PrettyPrint()); |
| 64 | PRETTY_PRINT_DECDEPTH(); |
| 65 | } |
| 66 | break; |
yunhanw-google | 86b7b7f | 2022-01-28 16:53:51 -0800 | [diff] [blame] | 67 | case kInteractionModelRevisionTag: |
| 68 | ReturnErrorOnFailure(MessageParser::CheckInteractionModelRevision(reader)); |
| 69 | break; |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 70 | default: |
| 71 | PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum); |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | PRETTY_PRINT("},"); |
Boris Zbarsky | 6c4b54e | 2022-06-02 13:28:19 -0400 | [diff] [blame] | 77 | PRETTY_PRINT_BLANK_LINE(); |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 78 | |
| 79 | if (CHIP_END_OF_TLV == err) |
| 80 | { |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 81 | err = CHIP_NO_ERROR; |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | ReturnErrorOnFailure(err); |
yunhanw-google | 731ae5a | 2022-06-01 23:02:05 -0700 | [diff] [blame] | 85 | return reader.ExitContainer(mOuterContainerType); |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 86 | } |
yunhanw-google | 751d0dd | 2022-11-16 19:09:58 -0800 | [diff] [blame] | 87 | #endif // CHIP_CONFIG_IM_PRETTY_PRINT |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 88 | |
| 89 | CHIP_ERROR InvokeResponseMessage::Parser::GetSuppressResponse(bool * const apSuppressResponse) const |
| 90 | { |
| 91 | return GetSimpleValue(to_underlying(Tag::kSuppressResponse), TLV::kTLVType_Boolean, apSuppressResponse); |
| 92 | } |
| 93 | |
yunhanw-google | f111727 | 2021-12-02 19:59:44 -0800 | [diff] [blame] | 94 | CHIP_ERROR InvokeResponseMessage::Parser::GetInvokeResponses(InvokeResponseIBs::Parser * const apStatus) const |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 95 | { |
| 96 | TLV::TLVReader reader; |
| 97 | ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kInvokeResponses)), reader)); |
yunhanw-google | 731ae5a | 2022-06-01 23:02:05 -0700 | [diff] [blame] | 98 | return apStatus->Init(reader); |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | InvokeResponseMessage::Builder & InvokeResponseMessage::Builder::SuppressResponse(const bool aSuppressResponse) |
| 102 | { |
| 103 | if (mError == CHIP_NO_ERROR) |
| 104 | { |
| 105 | mError = mpWriter->PutBoolean(TLV::ContextTag(to_underlying(Tag::kSuppressResponse)), aSuppressResponse); |
| 106 | } |
| 107 | return *this; |
| 108 | } |
| 109 | |
yunhanw-google | f111727 | 2021-12-02 19:59:44 -0800 | [diff] [blame] | 110 | InvokeResponseIBs::Builder & InvokeResponseMessage::Builder::CreateInvokeResponses() |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 111 | { |
| 112 | if (mError == CHIP_NO_ERROR) |
| 113 | { |
| 114 | mError = mInvokeResponses.Init(mpWriter, to_underlying(Tag::kInvokeResponses)); |
| 115 | } |
| 116 | return mInvokeResponses; |
| 117 | } |
| 118 | |
| 119 | InvokeResponseMessage::Builder & InvokeResponseMessage::Builder::EndOfInvokeResponseMessage() |
| 120 | { |
yunhanw-google | 86b7b7f | 2022-01-28 16:53:51 -0800 | [diff] [blame] | 121 | if (mError == CHIP_NO_ERROR) |
| 122 | { |
| 123 | mError = MessageBuilder::EncodeInteractionModelRevision(); |
| 124 | } |
| 125 | if (mError == CHIP_NO_ERROR) |
| 126 | { |
| 127 | EndOfContainer(); |
| 128 | } |
yunhanw-google | 36e61e3 | 2021-11-02 08:31:34 -0700 | [diff] [blame] | 129 | return *this; |
| 130 | } |
| 131 | } // namespace app |
| 132 | } // namespace chip |