TestInetCommon.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. * @file
  20. * This file implements constants, globals and interfaces common to
  21. * and used by all CHP Inet layer library test applications and
  22. * tools.
  23. *
  24. * NOTE: These do not comprise a public part of the CHIP API and
  25. * are subject to change without notice.
  26. *
  27. */
  28. #ifndef __STDC_LIMIT_MACROS
  29. #define __STDC_LIMIT_MACROS
  30. #endif
  31. #ifndef __STDC_FORMAT_MACROS
  32. #define __STDC_FORMAT_MACROS
  33. #endif
  34. #include "TestInetCommon.h"
  35. #include <vector>
  36. #include <inttypes.h>
  37. #include <stdint.h>
  38. #include <string.h>
  39. #include <sys/types.h>
  40. #include <lib/support/CHIPMem.h>
  41. #include <lib/support/ErrorStr.h>
  42. #include <lib/support/ScopedBuffer.h>
  43. #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
  44. #include <arpa/inet.h>
  45. #include <sys/select.h>
  46. #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
  47. using namespace chip;
  48. using namespace chip::Inet;
  49. System::LayerImpl gSystemLayer;
  50. Inet::InetLayer gInet;
  51. char gDefaultTapDeviceName[32];
  52. bool gDone = false;
  53. void InetFailError(int32_t err, const char * msg)
  54. {
  55. if (err != CHIP_NO_ERROR)
  56. {
  57. LOG_ERR("%s: %s", msg, ErrorStr(err));
  58. exit(-1);
  59. }
  60. }
  61. void InitTestInetCommon()
  62. {
  63. chip::Platform::MemoryInit();
  64. }
  65. void ShutdownTestInetCommon()
  66. {
  67. chip::Platform::MemoryShutdown();
  68. }
  69. void InitSystemLayer()
  70. {
  71. gSystemLayer.Init();
  72. }
  73. void ShutdownSystemLayer()
  74. {
  75. gSystemLayer.Shutdown();
  76. }
  77. void InitNetwork()
  78. {
  79. void * lContext = nullptr;
  80. gInet.Init(gSystemLayer, lContext);
  81. }
  82. void ServiceEvents(struct ::timeval & aSleepTime)
  83. {
  84. static bool printed = false;
  85. if (!printed)
  86. {
  87. {
  88. LOG_INF("CHIP node ready to service events");
  89. printed = true;
  90. }
  91. }
  92. #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
  93. fd_set readFDs, writeFDs, exceptFDs;
  94. int numFDs = 0;
  95. FD_ZERO(&readFDs);
  96. FD_ZERO(&writeFDs);
  97. FD_ZERO(&exceptFDs);
  98. #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
  99. if (gSystemLayer.IsInitialized())
  100. {
  101. gSystemLayer.PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
  102. }
  103. #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
  104. #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
  105. if (gInet.State == InetLayer::kState_Initialized)
  106. gInet.PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
  107. #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
  108. int selectRes = select(numFDs, &readFDs, &writeFDs, &exceptFDs, &aSleepTime);
  109. if (selectRes < 0)
  110. {
  111. LOG_INF("select failed: %s", ErrorStr(CHIP_ERROR_POSIX(errno)));
  112. return;
  113. }
  114. #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
  115. if (gSystemLayer.IsInitialized())
  116. {
  117. #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
  118. gSystemLayer.HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
  119. #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
  120. }
  121. if (gInet.State == InetLayer::kState_Initialized)
  122. {
  123. #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
  124. gInet.HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
  125. #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
  126. }
  127. }
  128. void ShutdownNetwork()
  129. {
  130. gInet.Shutdown();
  131. }
  132. #define DUMP_BUF_LEN 80
  133. void DumpMemory(const uint8_t * mem, uint32_t len, const char * prefix, uint32_t rowWidth)
  134. {
  135. int indexWidth = snprintf(nullptr, 0, "%X", len);
  136. if (indexWidth < 4)
  137. indexWidth = 4;
  138. for (uint32_t i = 0; i < len; i += rowWidth)
  139. {
  140. char buf[DUMP_BUF_LEN];
  141. char * ptr = buf;
  142. const char * buf_end = buf + DUMP_BUF_LEN;
  143. uint32_t rowEnd;
  144. uint32_t j;
  145. int result = snprintf(ptr, DUMP_BUF_LEN, "%s%0*X: ", prefix, indexWidth, i);
  146. if (result < 0 || result >= DUMP_BUF_LEN)
  147. goto print_line;
  148. ptr += result;
  149. rowEnd = i + rowWidth;
  150. j = i;
  151. for (; j < rowEnd && j < len; j++)
  152. {
  153. result = snprintf(ptr, buf_end - ptr, "%02X ", mem[j]);
  154. if (result < 0 || result >= buf_end - ptr)
  155. goto print_line;
  156. ptr += result;
  157. }
  158. for (; j < rowEnd; j++)
  159. {
  160. result = snprintf(ptr, buf_end - ptr, " ");
  161. if (result < 0 || result >= buf_end - ptr)
  162. goto print_line;
  163. ptr += result;
  164. }
  165. for (j = i; j < rowEnd && j < len; j++)
  166. {
  167. if (isprint(static_cast<char>(mem[j])))
  168. result = snprintf(ptr, buf_end - ptr, "%c", mem[j]);
  169. else
  170. result = snprintf(ptr, buf_end - ptr, ".");
  171. if (result < 0 || result >= buf_end - ptr)
  172. goto print_line;
  173. ptr += result;
  174. }
  175. print_line:
  176. if (result < 0 || result >= buf_end - ptr)
  177. {
  178. LOG_ERR("Dump buffer overflow");
  179. }
  180. if (ptr > buf && ptr < buf + DUMP_BUF_LEN)
  181. {
  182. *ptr = '\0';
  183. LOG_INF(buf);
  184. }
  185. }
  186. }
  187. void DumpMemory(const uint8_t * mem, uint32_t len, const char * prefix)
  188. {
  189. const uint32_t kRowWidth = 16;
  190. DumpMemory(mem, len, prefix, kRowWidth);
  191. }