2 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * This file defines the classes corresponding to CHIP reliable message
28 #include <messaging/ReliableMessageProtocolConfig.h>
30 #include <core/CHIPError.h>
31 #include <support/BitFlags.h>
32 #include <system/SystemLayer.h>
33 #include <system/SystemPacketBuffer.h>
34 #include <system/SystemTimer.h>
35 #include <transport/raw/MessageHeader.h>
40 enum class SendMessageFlags : uint16_t;
41 class ReliableMessageContext;
43 class ReliableMessageManager
47 * @class RetransTableEntry
50 * This class is part of the CHIP Reliable Messaging Protocol and is used
51 * to keep track of CHIP messages that have been sent and are expecting an
52 * acknowledgment back. If the acknowledgment is not received within a
53 * specific timeout, the message would be retransmitted from this table.
56 struct RetransTableEntry
60 ReliableMessageContext * rc; /**< The context for the stored CHIP message. */
61 System::PacketBuffer * msgBuf; /**< A pointer to the PacketBuffer object holding the CHIP message. */
62 uint32_t msgId; /**< The message identifier of the CHIP message awaiting acknowledgment. */
63 uint16_t msgSendFlags;
64 uint16_t nextRetransTimeTick; /**< A counter representing the next retransmission time for the message. */
65 uint8_t sendCount; /**< A counter representing the number of times the message has been sent. */
69 ReliableMessageManager();
70 ~ReliableMessageManager();
72 void Init(chip::System::Layer & system) { mSystemLayer = &system; }
75 void FreeContext(ReliableMessageContext * rc);
77 uint64_t GetTickCounterFromTimePeriod(uint64_t period);
78 uint64_t GetTickCounterFromTimeDelta(uint64_t newTime);
80 void ExecuteActions();
81 void ProcessDelayedDeliveryMessage(ReliableMessageContext * rc, uint32_t PauseTimeMillis);
82 static void Timeout(System::Layer * aSystemLayer, void * aAppState, System::Error aError);
84 CHIP_ERROR AddToRetransTable(ReliableMessageContext * rc, System::PacketBuffer * msgBuf, uint32_t messageId,
85 uint16_t msgSendFlags, RetransTableEntry ** rEntry);
86 void PauseRetransTable(ReliableMessageContext * rc, uint32_t PauseTimeMillis);
87 void ResumeRetransTable(ReliableMessageContext * rc);
88 bool CheckAndRemRetransTable(ReliableMessageContext * rc, uint32_t msgId);
89 CHIP_ERROR SendFromRetransTable(RetransTableEntry * entry);
90 void ClearRetransmitTable(ReliableMessageContext * rc);
91 void ClearRetransmitTable(RetransTableEntry & rEntry);
92 void FailRetransmitTableEntries(ReliableMessageContext * rc, CHIP_ERROR err);
98 // Functions for testing
99 int TestGetCountRetransTable();
100 void TestSetIntervalShift(uint16_t value) { mTimerIntervalShift = value; }
103 // public functions for ReliableMessageProtocol internal usage
104 CHIP_ERROR SendMessage(ReliableMessageContext * context, System::PacketBuffer * msgBuf, uint16_t sendFlags);
105 CHIP_ERROR SendMessage(ReliableMessageContext * context, uint32_t profileId, uint8_t msgType, System::PacketBuffer * msgBuf,
106 BitFlags<uint16_t, SendMessageFlags> sendFlags);
109 chip::System::Layer * mSystemLayer;
110 uint64_t mTimeStampBase; // ReliableMessageProtocol timer base value to add offsets to evaluate timeouts
111 System::Timer::Epoch mCurrentTimerExpiry; // Tracks when the ReliableMessageProtocol timer will next expire
112 uint16_t mTimerIntervalShift; // ReliableMessageProtocol Timer tick period shift
114 /* Placeholder function to run a function for all exchanges */
115 template <typename Function>
116 void ExecuteForAllContext(Function function)
119 void TicklessDebugDumpRetransTable(const char * log);
121 // ReliableMessageProtocol Global tables for timer context
122 RetransTableEntry RetransTable[CHIP_CONFIG_RMP_RETRANS_TABLE_SIZE];
125 } // namespace Messaging