3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2013-2018 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file implements constants, globals and interfaces common to
22 * and used by all CHP Inet layer library test applications and
25 * NOTE: These do not comprise a public part of the CHIP API and
26 * are subject to change without notice.
30 #ifndef __STDC_LIMIT_MACROS
31 #define __STDC_LIMIT_MACROS
33 #ifndef __STDC_FORMAT_MACROS
34 #define __STDC_FORMAT_MACROS
37 #include "TestInetCommon.h"
44 #include <sys/types.h>
46 #include <support/CHIPMem.h>
47 #include <support/ErrorStr.h>
48 #include <support/ScopedBuffer.h>
49 #include <system/SystemTimer.h>
51 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
52 #include <arpa/inet.h>
53 #include <sys/select.h>
54 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
57 using namespace chip::Inet;
59 System::Layer gSystemLayer;
61 Inet::InetLayer gInet;
63 char gDefaultTapDeviceName[32];
66 void InetFailError(int32_t err, const char * msg)
68 if (err != INET_NO_ERROR)
70 LOG_ERR("%s: %s", msg, ErrorStr(err));
75 void InitTestInetCommon()
77 chip::Platform::MemoryInit();
80 void InitSystemLayer()
82 gSystemLayer.Init(nullptr);
85 void ShutdownSystemLayer()
87 gSystemLayer.Shutdown();
92 void * lContext = nullptr;
94 gInet.Init(gSystemLayer, lContext);
97 void ServiceEvents(struct ::timeval & aSleepTime)
99 static bool printed = false;
104 LOG_INF("CHIP node ready to service events");
108 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
109 fd_set readFDs, writeFDs, exceptFDs;
116 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
117 if (gSystemLayer.State() == System::kLayerState_Initialized)
118 gSystemLayer.PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
119 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
121 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
122 if (gInet.State == InetLayer::kState_Initialized)
123 gInet.PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
124 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
126 int selectRes = select(numFDs, &readFDs, &writeFDs, &exceptFDs, &aSleepTime);
129 LOG_INF("select failed: %s", ErrorStr(System::MapErrorPOSIX(errno)));
132 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
134 if (gSystemLayer.State() == System::kLayerState_Initialized)
137 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
139 gSystemLayer.HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
141 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
144 if (gInet.State == InetLayer::kState_Initialized)
146 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
148 gInet.HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
150 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
154 void ShutdownNetwork()
159 #define DUMP_BUF_LEN 80
161 void DumpMemory(const uint8_t * mem, uint32_t len, const char * prefix, uint32_t rowWidth)
163 int indexWidth = snprintf(nullptr, 0, "%X", len);
168 for (uint32_t i = 0; i < len; i += rowWidth)
170 char buf[DUMP_BUF_LEN];
172 const char * buf_end = buf + DUMP_BUF_LEN;
175 int result = snprintf(ptr, DUMP_BUF_LEN, "%s%0*X: ", prefix, indexWidth, i);
176 if (result < 0 || result >= DUMP_BUF_LEN)
180 rowEnd = i + rowWidth;
183 for (; j < rowEnd && j < len; j++)
185 result = snprintf(ptr, buf_end - ptr, "%02X ", mem[j]);
186 if (result < 0 || result >= buf_end - ptr)
191 for (; j < rowEnd; j++)
193 result = snprintf(ptr, buf_end - ptr, " ");
194 if (result < 0 || result >= buf_end - ptr)
199 for (j = i; j < rowEnd && j < len; j++)
201 if (isprint(static_cast<char>(mem[j])))
202 result = snprintf(ptr, buf_end - ptr, "%c", mem[j]);
204 result = snprintf(ptr, buf_end - ptr, ".");
205 if (result < 0 || result >= buf_end - ptr)
211 if (result < 0 || result >= buf_end - ptr)
213 LOG_ERR("Dump buffer overflow");
215 if (ptr > buf && ptr < buf + DUMP_BUF_LEN)
223 void DumpMemory(const uint8_t * mem, uint32_t len, const char * prefix)
225 const uint32_t kRowWidth = 16;
227 DumpMemory(mem, len, prefix, kRowWidth);