Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2021 Project CHIP Authors 4 : * All rights reserved. 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 : #include <app/InteractionModelTimeout.h> 20 : #include <app/MessageDef/StatusResponseMessage.h> 21 : #include <app/StatusResponse.h> 22 : 23 : namespace chip { 24 : namespace app { 25 1313 : CHIP_ERROR StatusResponse::Send(Protocols::InteractionModel::Status aStatus, Messaging::ExchangeContext * apExchangeContext, 26 : bool aExpectResponse) 27 : { 28 1313 : VerifyOrReturnError(apExchangeContext != nullptr, CHIP_ERROR_INCORRECT_STATE); 29 1313 : System::PacketBufferHandle msgBuf = System::PacketBufferHandle::New(kMaxSecureSduLengthBytes); 30 1313 : VerifyOrReturnError(!msgBuf.IsNull(), CHIP_ERROR_NO_MEMORY); 31 : 32 1313 : System::PacketBufferTLVWriter writer; 33 1313 : writer.Init(std::move(msgBuf)); 34 : 35 1313 : StatusResponseMessage::Builder response; 36 1313 : ReturnErrorOnFailure(response.Init(&writer)); 37 1313 : response.Status(aStatus); 38 1313 : ReturnErrorOnFailure(response.GetError()); 39 1313 : ReturnErrorOnFailure(writer.Finalize(&msgBuf)); 40 1313 : apExchangeContext->UseSuggestedResponseTimeout(app::kExpectedIMProcessingTime); 41 1313 : ReturnErrorOnFailure(apExchangeContext->SendMessage(Protocols::InteractionModel::MsgType::StatusResponse, std::move(msgBuf), 42 : aExpectResponse ? Messaging::SendMessageFlags::kExpectResponse 43 : : Messaging::SendMessageFlags::kNone)); 44 1313 : return CHIP_NO_ERROR; 45 1313 : } 46 : 47 1241 : CHIP_ERROR StatusResponse::ProcessStatusResponse(System::PacketBufferHandle && aPayload, CHIP_ERROR & aStatusError) 48 : { 49 1241 : StatusResponseMessage::Parser response; 50 1241 : System::PacketBufferTLVReader reader; 51 1241 : reader.Init(std::move(aPayload)); 52 1241 : ReturnErrorOnFailure(response.Init(reader)); 53 : #if CHIP_CONFIG_IM_PRETTY_PRINT 54 1237 : response.PrettyPrint(); 55 : #endif 56 1237 : StatusIB status; 57 1237 : ReturnErrorOnFailure(response.GetStatus(status.mStatus)); 58 1237 : ChipLogProgress(InteractionModel, "Received status response, status is " ChipLogFormatIMStatus, 59 : ChipLogValueIMStatus(status.mStatus)); 60 1237 : ReturnErrorOnFailure(response.ExitContainer()); 61 : 62 1237 : aStatusError = status.ToChipError(); 63 1237 : return CHIP_NO_ERROR; 64 1241 : } 65 : } // namespace app 66 : } // namespace chip