Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / transport / raw / tests / NetworkTestHelpers.cpp
1 /*
2  *
3  *    Copyright (c) 2020-2021 Project CHIP Authors
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 #include "NetworkTestHelpers.h"
19
20 #include <inet/tests/TestInetCommon.h>
21
22 #include <support/CHIPMem.h>
23 #include <support/CodeUtils.h>
24 #include <support/ErrorStr.h>
25
26 namespace chip {
27 namespace Test {
28
29 CHIP_ERROR IOContext::Init(nlTestSuite * suite)
30 {
31     CHIP_ERROR err = Platform::MemoryInit();
32
33     gSystemLayer.Init(nullptr);
34
35     InitNetwork();
36
37     mSuite       = suite;
38     mSystemLayer = &gSystemLayer;
39     mInetLayer   = &gInet;
40
41     return err;
42 }
43
44 // Shutdown all layers, finalize operations
45 CHIP_ERROR IOContext::Shutdown()
46 {
47     CHIP_ERROR err = CHIP_NO_ERROR;
48
49     ShutdownNetwork();
50     Platform::MemoryShutdown();
51
52     return err;
53 }
54
55 void IOContext::DriveIO()
56 {
57     // Set the select timeout to 100ms
58     struct timeval aSleepTime;
59     aSleepTime.tv_sec  = 0;
60     aSleepTime.tv_usec = 100 * 1000;
61
62     ServiceEvents(aSleepTime);
63 }
64
65 void IOContext::DriveIOUntil(unsigned maxWaitMs, std::function<bool(void)> completionFunction)
66 {
67     uint64_t mStartTime = mSystemLayer->GetClock_MonotonicMS();
68
69     while (true)
70     {
71         DriveIO(); // at least one IO loop is guaranteed
72
73         if (completionFunction() || ((mSystemLayer->GetClock_MonotonicMS() - mStartTime) >= maxWaitMs))
74         {
75             break;
76         }
77     }
78 }
79
80 } // namespace Test
81 } // namespace chip