Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2020 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 : /** 20 : * @file 21 : * Definition of a handler for timed interactions. 22 : * 23 : */ 24 : 25 : #pragma once 26 : 27 : #include <messaging/ExchangeContext.h> 28 : #include <messaging/ExchangeDelegate.h> 29 : #include <system/SystemClock.h> 30 : #include <system/SystemLayer.h> 31 : #include <system/SystemPacketBuffer.h> 32 : #include <transport/raw/MessageHeader.h> 33 : 34 : /** 35 : * A TimedHandler handles a Timed Request action and then waits for a 36 : * subsequent Invoke or Write action and hands those on to 37 : * InteractionModelEngine if they arrive soon enough. 38 : * 39 : * Lifetime handling: 40 : * 41 : * A TimedHandler is initially allocated when the Timed Request is received and 42 : * becomes the delegate for that exchange. After that it remains alive until 43 : * either the exchange is closed or the interaction is handed on to the 44 : * InteractionModelEngine. 45 : */ 46 : 47 : namespace chip { 48 : namespace app { 49 : 50 : class TimedHandler : public Messaging::ExchangeDelegate 51 : { 52 : public: 53 3 : TimedHandler() {} 54 3 : ~TimedHandler() override {} 55 : 56 : // ExchangeDelegate implementation. 57 : CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * aExchangeContext, const PayloadHeader & aPayloadHeader, 58 : System::PacketBufferHandle && aPayload) override; 59 : 60 : private: 61 : // ExchangeDelegate implementation. 62 0 : void OnResponseTimeout(Messaging::ExchangeContext *) override 63 : { /* We just want to allow the exchange to close */ 64 0 : } 65 : void OnExchangeClosing(Messaging::ExchangeContext * aExchangeContext) override; 66 : 67 : void CancelTimer(); 68 : 69 : /** 70 : * Handler for the Timed Request action. This returns success if the Timed 71 : * Request action is parsed successfully and the success Status Response 72 : * action is sent, failure otherwise. 73 : */ 74 : CHIP_ERROR HandleTimedRequestAction(Messaging::ExchangeContext * aExchangeContext, const PayloadHeader & aPayloadHeader, 75 : System::PacketBufferHandle && aPayload); 76 : 77 : enum class State : uint8_t 78 : { 79 : kExpectingTimedAction, // Initial state: expecting a timed action. 80 : kReceivedTimedAction, // Have received the timed action. This can 81 : // be a terminal state if the action ends up 82 : // malformed. 83 : kExpectingFollowingAction, // Expecting write or invoke. 84 : }; 85 : 86 : // Because we have a vtable pointer and mTimeLimit needs to be 8-byte 87 : // aligned on ARM, putting mState first here means we fit in 16 bytes on 88 : // 32-bit ARM, whereas if we put it second we'd be 24 bytes. 89 : // On platforms where either vtable pointers are 8 bytes or 64-bit ints can 90 : // be 4-byte-aligned the ordering here does not matter. 91 : State mState = State::kExpectingTimedAction; 92 : // We keep track of the time limit for message reception, in case our 93 : // exchange's "response expected" timer gets delayed and does not fire when 94 : // the time runs out. 95 : System::Clock::Timestamp mTimeLimit; 96 : }; 97 : 98 : } // namespace app 99 : } // namespace chip