InetError.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. *
  3. * Copyright (c) 2020 Project CHIP Authors
  4. * Copyright (c) 2019 Google LLC.
  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 contains functions for working with Inet Layer errors.
  21. */
  22. #include <stddef.h>
  23. #include <inet/Inet.h>
  24. #include <inet/InetError.h>
  25. #include <support/ErrorStr.h>
  26. extern void FormatError(char * buf, uint16_t bufSize, const char * subsys, int32_t err, const char * desc);
  27. namespace chip {
  28. namespace Inet {
  29. /**
  30. * Register a text error formatter for Inet Layer errors.
  31. */
  32. void RegisterLayerErrorFormatter()
  33. {
  34. static chip::ErrorFormatter sInetLayerErrorFormatter = { FormatLayerError, nullptr };
  35. RegisterErrorFormatter(&sInetLayerErrorFormatter);
  36. }
  37. /**
  38. * Given an Inet Layer error, returns a human-readable NULL-terminated C string
  39. * describing the error.
  40. *
  41. * @param[in] buf Buffer into which the error string will be placed.
  42. * @param[in] bufSize Size of the supplied buffer in bytes.
  43. * @param[in] err The error to be described.
  44. *
  45. * @return true If a description string was written into the supplied buffer.
  46. * @return false If the supplied error was not an Inet Layer error.
  47. *
  48. */
  49. bool FormatLayerError(char * buf, uint16_t bufSize, CHIP_ERROR err)
  50. {
  51. const char * desc = nullptr;
  52. if (!ChipError::IsPart(ChipError::SdkPart::kInet, err))
  53. {
  54. return false;
  55. }
  56. #if !CHIP_CONFIG_SHORT_ERROR_STR
  57. switch (err)
  58. {
  59. case INET_ERROR_WRONG_ADDRESS_TYPE:
  60. desc = "Wrong address type";
  61. break;
  62. case CHIP_ERROR_CONNECTION_ABORTED:
  63. desc = "TCP connection aborted";
  64. break;
  65. case INET_ERROR_PEER_DISCONNECTED:
  66. desc = "Peer disconnected";
  67. break;
  68. case CHIP_ERROR_INCORRECT_STATE:
  69. desc = "Incorrect state";
  70. break;
  71. case CHIP_ERROR_MESSAGE_TOO_LONG:
  72. desc = "Message too long";
  73. break;
  74. case CHIP_ERROR_NO_CONNECTION_HANDLER:
  75. desc = "No TCP connection handler";
  76. break;
  77. case CHIP_ERROR_NO_MEMORY:
  78. desc = "No memory";
  79. break;
  80. case CHIP_ERROR_OUTBOUND_MESSAGE_TOO_BIG:
  81. desc = "Outbound message truncated";
  82. break;
  83. case CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG:
  84. desc = "Inbound message too big";
  85. break;
  86. case INET_ERROR_HOST_NOT_FOUND:
  87. desc = "Host not found";
  88. break;
  89. case INET_ERROR_DNS_TRY_AGAIN:
  90. desc = "DNS try again";
  91. break;
  92. case INET_ERROR_DNS_NO_RECOVERY:
  93. desc = "DNS no recovery";
  94. break;
  95. case CHIP_ERROR_INVALID_ARGUMENT:
  96. desc = "Bad arguments";
  97. break;
  98. case INET_ERROR_WRONG_PROTOCOL_TYPE:
  99. desc = "Wrong protocol type";
  100. break;
  101. case INET_ERROR_UNKNOWN_INTERFACE:
  102. desc = "Unknown interface";
  103. break;
  104. case CHIP_ERROR_NOT_IMPLEMENTED:
  105. desc = "Not implemented";
  106. break;
  107. case INET_ERROR_ADDRESS_NOT_FOUND:
  108. desc = "Address not found";
  109. break;
  110. case INET_ERROR_HOST_NAME_TOO_LONG:
  111. desc = "Host name too long";
  112. break;
  113. case INET_ERROR_INVALID_HOST_NAME:
  114. desc = "Invalid host name";
  115. break;
  116. case CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE:
  117. desc = "Not supported";
  118. break;
  119. case CHIP_ERROR_ENDPOINT_POOL_FULL:
  120. desc = "No more TCP endpoints";
  121. break;
  122. case INET_ERROR_IDLE_TIMEOUT:
  123. desc = "Idle timeout";
  124. break;
  125. case CHIP_ERROR_UNEXPECTED_EVENT:
  126. desc = "Unexpected event";
  127. break;
  128. case INET_ERROR_INVALID_IPV6_PKT:
  129. desc = "Invalid IPv6 Packet";
  130. break;
  131. case INET_ERROR_INTERFACE_INIT_FAILURE:
  132. desc = "Failure to initialize interface";
  133. break;
  134. case INET_ERROR_TCP_USER_TIMEOUT:
  135. desc = "TCP User Timeout";
  136. break;
  137. case INET_ERROR_TCP_CONNECT_TIMEOUT:
  138. desc = "TCP Connect Timeout";
  139. break;
  140. case INET_ERROR_INCOMPATIBLE_IP_ADDRESS_TYPE:
  141. desc = "Incompatible IP address type";
  142. break;
  143. }
  144. #endif // !CHIP_CONFIG_SHORT_ERROR_STR
  145. FormatError(buf, bufSize, "Inet", err, desc);
  146. return true;
  147. }
  148. } // namespace Inet
  149. } // namespace chip