Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / nrfconnect / util / test / TestInetCommon.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2013-2018 Nest Labs, Inc.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *      This file implements constants, globals and interfaces common to
22  *      and used by all CHP Inet layer library test applications and
23  *      tools.
24  *
25  *      NOTE: These do not comprise a public part of the CHIP API and
26  *            are subject to change without notice.
27  *
28  */
29
30 #ifndef __STDC_LIMIT_MACROS
31 #define __STDC_LIMIT_MACROS
32 #endif
33 #ifndef __STDC_FORMAT_MACROS
34 #define __STDC_FORMAT_MACROS
35 #endif
36
37 #include "TestInetCommon.h"
38
39 #include <vector>
40
41 #include <inttypes.h>
42 #include <stdint.h>
43 #include <string.h>
44 #include <sys/types.h>
45
46 #include <support/CHIPMem.h>
47 #include <support/ErrorStr.h>
48 #include <support/ScopedBuffer.h>
49 #include <system/SystemTimer.h>
50
51 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
52 #include <arpa/inet.h>
53 #include <sys/select.h>
54 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
55
56 using namespace chip;
57 using namespace chip::Inet;
58
59 System::Layer gSystemLayer;
60
61 Inet::InetLayer gInet;
62
63 char gDefaultTapDeviceName[32];
64 bool gDone = false;
65
66 void InetFailError(int32_t err, const char * msg)
67 {
68     if (err != INET_NO_ERROR)
69     {
70         LOG_ERR("%s: %s", msg, ErrorStr(err));
71         exit(-1);
72     }
73 }
74
75 void InitTestInetCommon()
76 {
77     chip::Platform::MemoryInit();
78 }
79
80 void InitSystemLayer()
81 {
82     gSystemLayer.Init(nullptr);
83 }
84
85 void ShutdownSystemLayer()
86 {
87     gSystemLayer.Shutdown();
88 }
89
90 void InitNetwork()
91 {
92     void * lContext = nullptr;
93
94     gInet.Init(gSystemLayer, lContext);
95 }
96
97 void ServiceEvents(struct ::timeval & aSleepTime)
98 {
99     static bool printed = false;
100
101     if (!printed)
102     {
103         {
104             LOG_INF("CHIP node ready to service events");
105             printed = true;
106         }
107     }
108 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
109     fd_set readFDs, writeFDs, exceptFDs;
110     int numFDs = 0;
111
112     FD_ZERO(&readFDs);
113     FD_ZERO(&writeFDs);
114     FD_ZERO(&exceptFDs);
115
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
120
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
125
126     int selectRes = select(numFDs, &readFDs, &writeFDs, &exceptFDs, &aSleepTime);
127     if (selectRes < 0)
128     {
129         LOG_INF("select failed: %s", ErrorStr(System::MapErrorPOSIX(errno)));
130         return;
131     }
132 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
133
134     if (gSystemLayer.State() == System::kLayerState_Initialized)
135     {
136
137 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
138
139         gSystemLayer.HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
140
141 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
142     }
143
144     if (gInet.State == InetLayer::kState_Initialized)
145     {
146 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
147
148         gInet.HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
149
150 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
151     }
152 }
153
154 void ShutdownNetwork()
155 {
156     gInet.Shutdown();
157 }
158
159 #define DUMP_BUF_LEN 80
160
161 void DumpMemory(const uint8_t * mem, uint32_t len, const char * prefix, uint32_t rowWidth)
162 {
163     int indexWidth = snprintf(nullptr, 0, "%X", len);
164
165     if (indexWidth < 4)
166         indexWidth = 4;
167
168     for (uint32_t i = 0; i < len; i += rowWidth)
169     {
170         char buf[DUMP_BUF_LEN];
171         char * ptr           = buf;
172         const char * buf_end = buf + DUMP_BUF_LEN;
173         uint32_t rowEnd;
174         uint32_t j;
175         int result = snprintf(ptr, DUMP_BUF_LEN, "%s%0*X: ", prefix, indexWidth, i);
176         if (result < 0 || result >= DUMP_BUF_LEN)
177             goto print_line;
178
179         ptr += result;
180         rowEnd = i + rowWidth;
181
182         j = i;
183         for (; j < rowEnd && j < len; j++)
184         {
185             result = snprintf(ptr, buf_end - ptr, "%02X ", mem[j]);
186             if (result < 0 || result >= buf_end - ptr)
187                 goto print_line;
188             ptr += result;
189         }
190
191         for (; j < rowEnd; j++)
192         {
193             result = snprintf(ptr, buf_end - ptr, "   ");
194             if (result < 0 || result >= buf_end - ptr)
195                 goto print_line;
196             ptr += result;
197         }
198
199         for (j = i; j < rowEnd && j < len; j++)
200         {
201             if (isprint(static_cast<char>(mem[j])))
202                 result = snprintf(ptr, buf_end - ptr, "%c", mem[j]);
203             else
204                 result = snprintf(ptr, buf_end - ptr, ".");
205             if (result < 0 || result >= buf_end - ptr)
206                 goto print_line;
207             ptr += result;
208         }
209
210     print_line:
211         if (result < 0 || result >= buf_end - ptr)
212         {
213             LOG_ERR("Dump buffer overflow");
214         }
215         if (ptr > buf && ptr < buf + DUMP_BUF_LEN)
216         {
217             *ptr = '\0';
218             LOG_INF(buf);
219         }
220     }
221 }
222
223 void DumpMemory(const uint8_t * mem, uint32_t len, const char * prefix)
224 {
225     const uint32_t kRowWidth = 16;
226
227     DumpMemory(mem, len, prefix, kRowWidth);
228 }