3 * Copyright (c) 2020-2021 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.
18 #include "NetworkTestHelpers.h"
20 #include <inet/tests/TestInetCommon.h>
22 #include <support/CHIPMem.h>
23 #include <support/CodeUtils.h>
24 #include <support/ErrorStr.h>
29 CHIP_ERROR IOContext::Init(nlTestSuite * suite)
31 CHIP_ERROR err = Platform::MemoryInit();
33 gSystemLayer.Init(nullptr);
38 mSystemLayer = &gSystemLayer;
44 // Shutdown all layers, finalize operations
45 CHIP_ERROR IOContext::Shutdown()
47 CHIP_ERROR err = CHIP_NO_ERROR;
50 Platform::MemoryShutdown();
55 void IOContext::DriveIO()
57 // Set the select timeout to 100ms
58 struct timeval aSleepTime;
59 aSleepTime.tv_sec = 0;
60 aSleepTime.tv_usec = 100 * 1000;
62 ServiceEvents(aSleepTime);
65 void IOContext::DriveIOUntil(unsigned maxWaitMs, std::function<bool(void)> completionFunction)
67 uint64_t mStartTime = mSystemLayer->GetClock_MonotonicMS();
71 DriveIO(); // at least one IO loop is guaranteed
73 if (completionFunction() || ((mSystemLayer->GetClock_MonotonicMS() - mStartTime) >= maxWaitMs))