• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2024, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef OT_GTEST_FAKE_PLATFORM_HPP_
30 #define OT_GTEST_FAKE_PLATFORM_HPP_
31 
32 #include "openthread-core-config.h"
33 
34 #include <map>
35 #include <set>
36 #include <vector>
37 
38 #include <inttypes.h>
39 
40 #include <openthread/error.h>
41 #include <openthread/instance.h>
42 #include <openthread/platform/alarm-micro.h>
43 #include <openthread/platform/alarm-milli.h>
44 #include <openthread/platform/radio.h>
45 #include <openthread/platform/time.h>
46 
47 bool operator<(const otExtAddress &aLeft, const otExtAddress &aRight);
48 
49 namespace ot {
50 
51 class FakePlatform
52 {
53 public:
54     FakePlatform();
55     virtual ~FakePlatform();
56 
CurrentPlatform()57     static FakePlatform &CurrentPlatform() { return *sPlatform; }
CurrentInstance()58     static otInstance   *CurrentInstance() { return CurrentPlatform().mInstance; }
59 
60     /**
61      * Run until something happened or timeout.
62      *
63      * @param aTimeout The timeout in us.
64      *
65      * @returns the remaining timeout.
66      */
67     uint64_t Run(uint64_t aTimeoutInUs = 0);
68 
69     void GoInUs(uint64_t aTimeoutInUs = 0);
70 
GoInMs(uint32_t aTimeoutInMs=0)71     void GoInMs(uint32_t aTimeoutInMs = 0) { GoInUs(aTimeoutInMs * OT_US_PER_MS); }
72 
GetNow() const73     virtual uint64_t GetNow() const { return mNow; }
74 
75     virtual void StartMilliAlarm(uint32_t aT0, uint32_t aDt);
76     virtual void StopMilliAlarm();
77 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
78     virtual void StartMicroAlarm(uint32_t aT0, uint32_t aDt);
79     virtual void StopMicroAlarm();
80 #endif
81 
GetReceiveChannel(void) const82     uint8_t               GetReceiveChannel(void) const { return mChannel; }
GetTransmitBuffer()83     virtual otRadioFrame *GetTransmitBuffer() { return &mTransmitFrame; }
84     virtual otError       Transmit(otRadioFrame *aFrame);
ReceiveAt(uint8_t aChannel,uint32_t aStart,uint32_t aDuration)85     virtual otError       ReceiveAt(uint8_t aChannel, uint32_t aStart, uint32_t aDuration)
86     {
87         mReceiveAtChannel = aChannel;
88         mReceiveAtStart   = mNow + aStart;
89         mReceiveAtEnd     = mReceiveAtStart + aDuration;
90 
91         return OT_ERROR_NONE;
92     }
93 
Receive(uint8_t aChannel)94     virtual otError Receive(uint8_t aChannel)
95     {
96         mChannel = aChannel;
97         return OT_ERROR_NONE;
98     }
99 
100     virtual otError SettingsGet(uint16_t aKey, uint16_t aIndex, uint8_t *aValue, uint16_t *aValueLength) const;
101     virtual otError SettingsSet(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
102     virtual otError SettingsAdd(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
103     virtual otError SettingsDelete(uint16_t aKey, int aIndex);
104     virtual void    SettingsWipe();
105 
106     virtual void     FlashInit();
107     virtual void     FlashErase(uint8_t aSwapIndex);
108     virtual void     FlashRead(uint8_t aSwapIndex, uint32_t aOffset, void *aData, uint32_t aSize) const;
109     virtual void     FlashWrite(uint8_t aSwapIndex, uint32_t aOffset, const void *aData, uint32_t aSize);
FlashGetSwapSize() const110     virtual uint32_t FlashGetSwapSize() const { return kFlashSwapSize; }
111 
GetEui64() const112     virtual uint64_t GetEui64() const { return 0; }
113 
SrcMatchEnable(bool aEnabled)114     virtual void SrcMatchEnable(bool aEnabled) { mSrcMatchEnabled = aEnabled; }
SrcMatchIsEnabled() const115     virtual bool SrcMatchIsEnabled() const { return mSrcMatchEnabled; }
SrcMatchAddShortEntry(uint16_t aShortAddr)116     virtual void SrcMatchAddShortEntry(uint16_t aShortAddr) { mSrcMatchShortAddrs.insert(aShortAddr); }
SrcMatchClearShortEntry(uint16_t aShortAddr)117     virtual void SrcMatchClearShortEntry(uint16_t aShortAddr) { mSrcMatchShortAddrs.erase(aShortAddr); }
SrcMatchHasShortEntry(uint16_t aShortAddr) const118     virtual bool SrcMatchHasShortEntry(uint16_t aShortAddr) const { return mSrcMatchShortAddrs.count(aShortAddr) != 0; }
SrcMatchAddExtEntry(const otExtAddress & aExtAddr)119     virtual void SrcMatchAddExtEntry(const otExtAddress &aExtAddr) { mSrcMatchExtAddrs.insert(aExtAddr); }
SrcMatchClearExtEntry(const otExtAddress & aExtAddr)120     virtual void SrcMatchClearExtEntry(const otExtAddress &aExtAddr) { mSrcMatchExtAddrs.erase(aExtAddr); }
SrcMatchHasExtEntry(const otExtAddress & aExtAddr) const121     virtual bool SrcMatchHasExtEntry(const otExtAddress &aExtAddr) const
122     {
123         return mSrcMatchExtAddrs.count(aExtAddr) != 0;
124     }
SrcMatchClearShortEntries(void)125     virtual void   SrcMatchClearShortEntries(void) { mSrcMatchShortAddrs.clear(); }
SrcMatchCountShortEntries(void) const126     virtual size_t SrcMatchCountShortEntries(void) const { return mSrcMatchShortAddrs.size(); }
SrcMatchClearExtEntries(void)127     virtual void   SrcMatchClearExtEntries(void) { mSrcMatchExtAddrs.clear(); }
SrcMatchCountExtEntries(void) const128     virtual size_t SrcMatchCountExtEntries(void) const { return mSrcMatchExtAddrs.size(); }
129 
130 protected:
131     void ProcessSchedules(uint64_t &aTimeout);
132 
133     static constexpr uint64_t kAlarmStop = 0xffffffffffffffffUL;
134 
135     static constexpr uint32_t kFlashSwapSize = 2048;
136     static constexpr uint32_t kFlashSwapNum  = 2;
137 
138     static FakePlatform *sPlatform;
139 
140     otInstance *mInstance = nullptr;
141 
142     uint64_t mNow = 0;
143 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
144     uint64_t mMicroAlarmStart = kAlarmStop;
145 #endif
146     uint64_t mMilliAlarmStart = kAlarmStop;
147 
148     uint64_t mReceiveAtStart = kAlarmStop;
149     uint64_t mReceiveAtEnd   = kAlarmStop;
150 
151     template <uint64_t FakePlatform::*T> void HandleSchedule();
152 
153     otRadioFrame mTransmitFrame;
154     uint8_t      mTransmitBuffer[OT_RADIO_FRAME_MAX_SIZE];
155     uint8_t      mChannel          = 0;
156     uint8_t      mReceiveAtChannel = 0;
157 
158     uint8_t mFlash[kFlashSwapSize * kFlashSwapNum];
159 
160     std::map<uint32_t, std::vector<std::vector<uint8_t>>> mSettings;
161 
162     bool                   mSrcMatchEnabled = false;
163     std::set<uint16_t>     mSrcMatchShortAddrs;
164     std::set<otExtAddress> mSrcMatchExtAddrs;
165 };
166 
HandleSchedule()167 template <> inline void FakePlatform::HandleSchedule<&FakePlatform::mMilliAlarmStart>()
168 {
169     otPlatAlarmMilliFired(mInstance);
170 }
171 
172 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
HandleSchedule()173 template <> inline void FakePlatform::HandleSchedule<&FakePlatform::mMicroAlarmStart>()
174 {
175     otPlatAlarmMicroFired(mInstance);
176 }
177 #endif
178 
179 } // namespace ot
180 
181 #endif // OT_GTEST_FAKE_PLATFORM_HPP_
182