Boris Zbarsky | 3397fdc | 2021-12-06 14:01:12 -0500 | [diff] [blame] | 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 "TimedRequest.h" |
| 20 | |
| 21 | #include <app/MessageDef/TimedRequestMessage.h> |
| 22 | #include <app/StatusResponse.h> |
| 23 | #include <protocols/interaction_model/Constants.h> |
| 24 | #include <system/TLVPacketBufferBackingStore.h> |
| 25 | #include <transport/SessionManager.h> |
| 26 | |
| 27 | namespace chip { |
| 28 | namespace app { |
| 29 | |
| 30 | using namespace Protocols::InteractionModel; |
| 31 | using namespace Messaging; |
| 32 | |
| 33 | CHIP_ERROR TimedRequest::Send(ExchangeContext * aExchangeContext, uint16_t aTimeoutMs) |
| 34 | { |
| 35 | // The payload is an anonymous struct (2 bytes) containing a single |
yunhanw-google | 86b7b7f | 2022-01-28 16:53:51 -0800 | [diff] [blame] | 36 | // 16-bit integer with two context tag (1 control byte, 1 byte tag, at |
| 37 | // most 2 bytes for the timeout integer and 1 control byte, 1 byte tag, one byte for InteractionModelRevision). Use |
| 38 | // MessagePacketBuffer::New to account for other message-global overheads (MIC, etc). |
| 39 | System::PacketBufferHandle payload = MessagePacketBuffer::New(9); |
Boris Zbarsky | 3397fdc | 2021-12-06 14:01:12 -0500 | [diff] [blame] | 40 | VerifyOrReturnError(!payload.IsNull(), CHIP_ERROR_NO_MEMORY); |
| 41 | |
| 42 | System::PacketBufferTLVWriter writer; |
| 43 | writer.Init(std::move(payload)); |
| 44 | |
| 45 | TimedRequestMessage::Builder builder; |
| 46 | ReturnErrorOnFailure(builder.Init(&writer)); |
| 47 | |
| 48 | builder.TimeoutMs(aTimeoutMs); |
| 49 | ReturnErrorOnFailure(builder.GetError()); |
| 50 | |
| 51 | ReturnErrorOnFailure(writer.Finalize(&payload)); |
| 52 | |
| 53 | return aExchangeContext->SendMessage(MsgType::TimedRequest, std::move(payload), SendMessageFlags::kExpectResponse); |
| 54 | } |
| 55 | |
Boris Zbarsky | 3397fdc | 2021-12-06 14:01:12 -0500 | [diff] [blame] | 56 | } // namespace app |
| 57 | } // namespace chip |