269634817aba5ec202cb308426efbd8cf4e4f0e7
[platform/upstream/connectedhomeip.git] / src / messaging / ReliableMessageManager.h
1 /*
2  *    Copyright (c) 2020 Project CHIP Authors
3  *    All rights reserved.
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *    @file
20  *      This file defines the classes corresponding to CHIP reliable message
21  *      protocol.
22  */
23
24 #pragma once
25
26 #include <stdint.h>
27
28 #include <messaging/ReliableMessageProtocolConfig.h>
29
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>
36
37 namespace chip {
38 namespace Messaging {
39
40 enum class SendMessageFlags : uint16_t;
41 class ReliableMessageContext;
42
43 class ReliableMessageManager
44 {
45 public:
46     /**
47      *  @class RetransTableEntry
48      *
49      *  @brief
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.
54      *
55      */
56     struct RetransTableEntry
57     {
58         RetransTableEntry();
59
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. */
66     };
67
68 public:
69     ReliableMessageManager();
70     ~ReliableMessageManager();
71
72     void Init(chip::System::Layer & system) { mSystemLayer = &system; }
73     void Shutdown() {}
74
75     void FreeContext(ReliableMessageContext * rc);
76
77     uint64_t GetTickCounterFromTimePeriod(uint64_t period);
78     uint64_t GetTickCounterFromTimeDelta(uint64_t newTime);
79
80     void ExecuteActions();
81     void ProcessDelayedDeliveryMessage(ReliableMessageContext * rc, uint32_t PauseTimeMillis);
82     static void Timeout(System::Layer * aSystemLayer, void * aAppState, System::Error aError);
83
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);
93
94     void StartTimer();
95     void StopTimer();
96     void ExpireTicks();
97
98     // Functions for testing
99     int TestGetCountRetransTable();
100     void TestSetIntervalShift(uint16_t value) { mTimerIntervalShift = value; }
101
102 public:
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);
107
108 private:
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
113
114     /* Placeholder function to run a function for all exchanges */
115     template <typename Function>
116     void ExecuteForAllContext(Function function)
117     {}
118
119     void TicklessDebugDumpRetransTable(const char * log);
120
121     // ReliableMessageProtocol Global tables for timer context
122     RetransTableEntry RetransTable[CHIP_CONFIG_RMP_RETRANS_TABLE_SIZE];
123 };
124
125 } // namespace Messaging
126 } // namespace chip