Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2020-2022 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 : #include <lib/core/CHIPError.h> 19 : 20 : #include <lib/core/CHIPConfig.h> 21 : #include <lib/core/ErrorStr.h> 22 : 23 : namespace chip { 24 : 25 : /** 26 : * Register a text error formatter for CHIP core errors. 27 : */ 28 42 : void RegisterCHIPLayerErrorFormatter() 29 : { 30 : static ErrorFormatter sCHIPErrorFormatter = { FormatCHIPError, nullptr }; 31 : 32 42 : RegisterErrorFormatter(&sCHIPErrorFormatter); 33 42 : } 34 : 35 : /** 36 : * Given a CHIP error, returns a human-readable NULL-terminated C string 37 : * describing the error. 38 : * 39 : * @param[in] buf Buffer into which the error string will be placed. 40 : * @param[in] bufSize Size of the supplied buffer in bytes. 41 : * @param[in] err The error to be described. 42 : * 43 : * @return true If a description string was written into the supplied buffer. 44 : * @return false If the supplied error was not a CHIP error. 45 : * 46 : */ 47 558 : bool FormatCHIPError(char * buf, uint16_t bufSize, CHIP_ERROR err) 48 : { 49 558 : const char * desc = nullptr; 50 : 51 558 : if (!err.IsPart(ChipError::SdkPart::kCore)) 52 : { 53 0 : return false; 54 : } 55 : 56 : #if !CHIP_CONFIG_SHORT_ERROR_STR 57 558 : switch (err.AsInteger()) 58 : { 59 1 : case CHIP_ERROR_SENDING_BLOCKED.AsInteger(): 60 1 : desc = "Sending blocked"; 61 1 : break; 62 5 : case CHIP_ERROR_CONNECTION_ABORTED.AsInteger(): 63 5 : desc = "Connection aborted"; 64 5 : break; 65 13 : case CHIP_ERROR_INCORRECT_STATE.AsInteger(): 66 13 : desc = "Incorrect state"; 67 13 : break; 68 1 : case CHIP_ERROR_MESSAGE_TOO_LONG.AsInteger(): 69 1 : desc = "Message too long"; 70 1 : break; 71 1 : case CHIP_ERROR_RECURSION_DEPTH_LIMIT.AsInteger(): 72 1 : desc = "Recursion depth limit reached"; 73 1 : break; 74 1 : case CHIP_ERROR_TOO_MANY_UNSOLICITED_MESSAGE_HANDLERS.AsInteger(): 75 1 : desc = "Too many unsolicited message handlers"; 76 1 : break; 77 1 : case CHIP_ERROR_NO_UNSOLICITED_MESSAGE_HANDLER.AsInteger(): 78 1 : desc = "No unsolicited message handler"; 79 1 : break; 80 1 : case CHIP_ERROR_NO_CONNECTION_HANDLER.AsInteger(): 81 1 : desc = "No connection handler"; 82 1 : break; 83 1 : case CHIP_ERROR_TOO_MANY_PEER_NODES.AsInteger(): 84 1 : desc = "Too many peer nodes"; 85 1 : break; 86 1 : case CHIP_ERROR_SENTINEL.AsInteger(): 87 1 : desc = "Internal sentinel"; 88 1 : break; 89 393 : case CHIP_ERROR_NO_MEMORY.AsInteger(): 90 393 : desc = "No memory"; 91 393 : break; 92 1 : case CHIP_ERROR_NO_MESSAGE_HANDLER.AsInteger(): 93 1 : desc = "No message handler"; 94 1 : break; 95 1 : case CHIP_ERROR_MESSAGE_INCOMPLETE.AsInteger(): 96 1 : desc = "Message incomplete"; 97 1 : break; 98 1 : case CHIP_ERROR_DATA_NOT_ALIGNED.AsInteger(): 99 1 : desc = "Data not aligned"; 100 1 : break; 101 1 : case CHIP_ERROR_UNKNOWN_KEY_TYPE.AsInteger(): 102 1 : desc = "Unknown key type"; 103 1 : break; 104 2 : case CHIP_ERROR_KEY_NOT_FOUND.AsInteger(): 105 2 : desc = "Key not found"; 106 2 : break; 107 1 : case CHIP_ERROR_WRONG_ENCRYPTION_TYPE.AsInteger(): 108 1 : desc = "Wrong encryption type"; 109 1 : break; 110 1 : case CHIP_ERROR_INVALID_UTF8.AsInteger(): 111 1 : desc = "Character string is not a valid utf-8 encoding"; 112 1 : break; 113 1 : case CHIP_ERROR_INTEGRITY_CHECK_FAILED.AsInteger(): 114 1 : desc = "Integrity check failed"; 115 1 : break; 116 1 : case CHIP_ERROR_INVALID_SIGNATURE.AsInteger(): 117 1 : desc = "Invalid signature"; 118 1 : break; 119 1 : case CHIP_ERROR_INVALID_TLV_CHAR_STRING.AsInteger(): 120 1 : desc = "Invalid TLV Char string encoding."; 121 1 : break; 122 1 : case CHIP_ERROR_UNSUPPORTED_SIGNATURE_TYPE.AsInteger(): 123 1 : desc = "Unsupported signature type"; 124 1 : break; 125 1 : case CHIP_ERROR_INVALID_MESSAGE_LENGTH.AsInteger(): 126 1 : desc = "Invalid message length"; 127 1 : break; 128 1 : case CHIP_ERROR_BUFFER_TOO_SMALL.AsInteger(): 129 1 : desc = "Buffer too small"; 130 1 : break; 131 1 : case CHIP_ERROR_DUPLICATE_KEY_ID.AsInteger(): 132 1 : desc = "Duplicate key id"; 133 1 : break; 134 1 : case CHIP_ERROR_WRONG_KEY_TYPE.AsInteger(): 135 1 : desc = "Wrong key type"; 136 1 : break; 137 1 : case CHIP_ERROR_UNINITIALIZED.AsInteger(): 138 1 : desc = "Uninitialized"; 139 1 : break; 140 0 : case CHIP_ERROR_INVALID_IPK.AsInteger(): 141 0 : desc = "Invalid IPK"; 142 0 : break; 143 1 : case CHIP_ERROR_INVALID_STRING_LENGTH.AsInteger(): 144 1 : desc = "Invalid string length"; 145 1 : break; 146 1 : case CHIP_ERROR_INVALID_LIST_LENGTH.AsInteger(): 147 1 : desc = "Invalid list length"; 148 1 : break; 149 5 : case CHIP_END_OF_TLV.AsInteger(): 150 5 : desc = "End of TLV"; 151 5 : break; 152 1 : case CHIP_ERROR_TLV_UNDERRUN.AsInteger(): 153 1 : desc = "TLV underrun"; 154 1 : break; 155 1 : case CHIP_ERROR_INVALID_TLV_ELEMENT.AsInteger(): 156 1 : desc = "Invalid TLV element"; 157 1 : break; 158 1 : case CHIP_ERROR_INVALID_TLV_TAG.AsInteger(): 159 1 : desc = "Invalid TLV tag"; 160 1 : break; 161 1 : case CHIP_ERROR_UNKNOWN_IMPLICIT_TLV_TAG.AsInteger(): 162 1 : desc = "Unknown implicit TLV tag"; 163 1 : break; 164 1 : case CHIP_ERROR_WRONG_TLV_TYPE.AsInteger(): 165 1 : desc = "Wrong TLV type"; 166 1 : break; 167 1 : case CHIP_ERROR_TLV_CONTAINER_OPEN.AsInteger(): 168 1 : desc = "TLV container open"; 169 1 : break; 170 2 : case CHIP_ERROR_INVALID_MESSAGE_TYPE.AsInteger(): 171 2 : desc = "Invalid message type"; 172 2 : break; 173 6 : case CHIP_ERROR_UNEXPECTED_TLV_ELEMENT.AsInteger(): 174 6 : desc = "Unexpected TLV element"; 175 6 : break; 176 1 : case CHIP_ERROR_NOT_IMPLEMENTED.AsInteger(): 177 1 : desc = "Not Implemented"; 178 1 : break; 179 1 : case CHIP_ERROR_INVALID_ADDRESS.AsInteger(): 180 1 : desc = "Invalid address"; 181 1 : break; 182 2 : case CHIP_ERROR_INVALID_ARGUMENT.AsInteger(): 183 2 : desc = "Invalid argument"; 184 2 : break; 185 1 : case CHIP_ERROR_TLV_TAG_NOT_FOUND.AsInteger(): 186 1 : desc = "TLV tag not found"; 187 1 : break; 188 1 : case CHIP_ERROR_MISSING_SECURE_SESSION.AsInteger(): 189 1 : desc = "Missing secure session"; 190 1 : break; 191 1 : case CHIP_ERROR_INVALID_ADMIN_SUBJECT.AsInteger(): 192 1 : desc = "CaseAdminSubject is not valid"; 193 1 : break; 194 1 : case CHIP_ERROR_INSUFFICIENT_PRIVILEGE.AsInteger(): 195 1 : desc = "Required privilege was insufficient during an operation"; 196 1 : break; 197 1 : case CHIP_ERROR_IM_MALFORMED_COMMAND_DATA_IB.AsInteger(): 198 1 : desc = "Malformed Interacton Model Command Data IB"; 199 1 : break; 200 1 : case CHIP_ERROR_INVALID_PATH_LIST.AsInteger(): 201 1 : desc = "Invalid TLV path list"; 202 1 : break; 203 1 : case CHIP_ERROR_INVALID_DATA_LIST.AsInteger(): 204 1 : desc = "Invalid TLV data list"; 205 1 : break; 206 1 : case CHIP_ERROR_TRANSACTION_CANCELED.AsInteger(): 207 1 : desc = "Transaction canceled"; 208 1 : break; 209 1 : case CHIP_ERROR_INVALID_SUBSCRIPTION.AsInteger(): 210 1 : desc = "Invalid Subscription Id"; 211 1 : break; 212 5 : case CHIP_ERROR_TIMEOUT.AsInteger(): 213 5 : desc = "Timeout"; 214 5 : break; 215 1 : case CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR.AsInteger(): 216 1 : desc = "Invalid device descriptor"; 217 1 : break; 218 1 : case CHIP_ERROR_INVALID_PASE_PARAMETER.AsInteger(): 219 1 : desc = "Invalid PASE parameter"; 220 1 : break; 221 1 : case CHIP_ERROR_INVALID_USE_OF_SESSION_KEY.AsInteger(): 222 1 : desc = "Invalid use of session key"; 223 1 : break; 224 1 : case CHIP_ERROR_CONNECTION_CLOSED_UNEXPECTEDLY.AsInteger(): 225 1 : desc = "Connection closed unexpectedly"; 226 1 : break; 227 1 : case CHIP_ERROR_MISSING_TLV_ELEMENT.AsInteger(): 228 1 : desc = "Missing TLV element"; 229 1 : break; 230 1 : case CHIP_ERROR_RANDOM_DATA_UNAVAILABLE.AsInteger(): 231 1 : desc = "Random data unavailable"; 232 1 : break; 233 1 : case CHIP_ERROR_HOST_PORT_LIST_EMPTY.AsInteger(): 234 1 : desc = "Host/port empty"; 235 1 : break; 236 1 : case CHIP_ERROR_FORCED_RESET.AsInteger(): 237 1 : desc = "Service manager forced reset"; 238 1 : break; 239 1 : case CHIP_ERROR_NO_ENDPOINT.AsInteger(): 240 1 : desc = "No endpoint was available to send the message"; 241 1 : break; 242 1 : case CHIP_ERROR_INVALID_DESTINATION_NODE_ID.AsInteger(): 243 1 : desc = "Invalid destination node id"; 244 1 : break; 245 1 : case CHIP_ERROR_NOT_CONNECTED.AsInteger(): 246 1 : desc = "Not connected"; 247 1 : break; 248 1 : case CHIP_ERROR_CA_CERT_NOT_FOUND.AsInteger(): 249 1 : desc = "CA certificate not found"; 250 1 : break; 251 1 : case CHIP_ERROR_CERT_PATH_LEN_CONSTRAINT_EXCEEDED.AsInteger(): 252 1 : desc = "Certificate path length constraint exceeded"; 253 1 : break; 254 1 : case CHIP_ERROR_CERT_PATH_TOO_LONG.AsInteger(): 255 1 : desc = "Certificate path too long"; 256 1 : break; 257 1 : case CHIP_ERROR_CERT_USAGE_NOT_ALLOWED.AsInteger(): 258 1 : desc = "Requested certificate usage is not allowed"; 259 1 : break; 260 1 : case CHIP_ERROR_CERT_EXPIRED.AsInteger(): 261 1 : desc = "Certificate expired"; 262 1 : break; 263 1 : case CHIP_ERROR_CERT_NOT_VALID_YET.AsInteger(): 264 1 : desc = "Certificate not yet valid"; 265 1 : break; 266 1 : case CHIP_ERROR_UNSUPPORTED_CERT_FORMAT.AsInteger(): 267 1 : desc = "Unsupported certificate format"; 268 1 : break; 269 1 : case CHIP_ERROR_UNSUPPORTED_ELLIPTIC_CURVE.AsInteger(): 270 1 : desc = "Unsupported elliptic curve"; 271 1 : break; 272 1 : case CHIP_ERROR_CERT_NOT_FOUND.AsInteger(): 273 1 : desc = "Certificate not found"; 274 1 : break; 275 1 : case CHIP_ERROR_INVALID_CASE_PARAMETER.AsInteger(): 276 1 : desc = "Invalid CASE parameter"; 277 1 : break; 278 1 : case CHIP_ERROR_CERT_LOAD_FAILED.AsInteger(): 279 1 : desc = "Unable to load certificate"; 280 1 : break; 281 1 : case CHIP_ERROR_CERT_NOT_TRUSTED.AsInteger(): 282 1 : desc = "Certificate not trusted"; 283 1 : break; 284 1 : case CHIP_ERROR_WRONG_CERT_DN.AsInteger(): 285 1 : desc = "Wrong certificate distinguished name"; 286 1 : break; 287 1 : case CHIP_ERROR_WRONG_NODE_ID.AsInteger(): 288 1 : desc = "Wrong node ID"; 289 1 : break; 290 1 : case CHIP_ERROR_RETRANS_TABLE_FULL.AsInteger(): 291 1 : desc = "Retransmit Table is already full"; 292 1 : break; 293 2 : case CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE.AsInteger(): 294 2 : desc = "Required feature not supported by this configuration"; 295 2 : break; 296 3 : case CHIP_ERROR_UNSOLICITED_MSG_NO_ORIGINATOR.AsInteger(): 297 3 : desc = "Unsolicited msg with originator bit clear"; 298 3 : break; 299 1 : case CHIP_ERROR_INVALID_FABRIC_INDEX.AsInteger(): 300 1 : desc = "Invalid Fabric Index"; 301 1 : break; 302 1 : case CHIP_ERROR_TOO_MANY_CONNECTIONS.AsInteger(): 303 1 : desc = "Too many connections"; 304 1 : break; 305 1 : case CHIP_ERROR_SHUT_DOWN.AsInteger(): 306 1 : desc = "The operation was cancelled because a shut down was initiated"; 307 1 : break; 308 1 : case CHIP_ERROR_CANCELLED.AsInteger(): 309 1 : desc = "The operation has been cancelled"; 310 1 : break; 311 1 : case CHIP_ERROR_MESSAGE_COUNTER_EXHAUSTED.AsInteger(): 312 1 : desc = "Message counter exhausted"; 313 1 : break; 314 1 : case CHIP_ERROR_FABRIC_EXISTS.AsInteger(): 315 1 : desc = "Trying to add a NOC for a fabric that already exists"; 316 1 : break; 317 1 : case CHIP_ERROR_ENDPOINT_EXISTS.AsInteger(): 318 1 : desc = "Trying to add dynamic endpoint that already exists"; 319 1 : break; 320 1 : case CHIP_ERROR_WRONG_ENCRYPTION_TYPE_FROM_PEER.AsInteger(): 321 1 : desc = "Wrong encryption type error code received from peer"; 322 1 : break; 323 1 : case CHIP_ERROR_INVALID_KEY_ID.AsInteger(): 324 1 : desc = "Invalid key identifier"; 325 1 : break; 326 1 : case CHIP_ERROR_INVALID_TIME.AsInteger(): 327 1 : desc = "Valid time value is not available"; 328 1 : break; 329 1 : case CHIP_ERROR_SCHEMA_MISMATCH.AsInteger(): 330 1 : desc = "Schema mismatch"; 331 1 : break; 332 1 : case CHIP_ERROR_INVALID_INTEGER_VALUE.AsInteger(): 333 1 : desc = "Invalid integer value"; 334 1 : break; 335 1 : case CHIP_ERROR_BAD_REQUEST.AsInteger(): 336 1 : desc = "Request cannot be processed or fulfilled"; 337 1 : break; 338 1 : case CHIP_ERROR_WRONG_CERT_TYPE.AsInteger(): 339 1 : desc = "Wrong certificate type"; 340 1 : break; 341 1 : case CHIP_ERROR_PERSISTED_STORAGE_FAILED.AsInteger(): 342 1 : desc = "Persisted storage failed"; 343 1 : break; 344 3 : case CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND.AsInteger(): 345 3 : desc = "Value not found in the persisted storage"; 346 3 : break; 347 1 : case CHIP_ERROR_IM_FABRIC_DELETED.AsInteger(): 348 1 : desc = "The fabric is deleted, and the corresponding IM resources are released"; 349 1 : break; 350 1 : case CHIP_ERROR_IN_PROGRESS.AsInteger(): 351 1 : desc = "The operation is still in progress"; 352 1 : break; 353 2 : case CHIP_ERROR_ACCESS_DENIED.AsInteger(): 354 2 : desc = "The CHIP message is not granted access"; 355 2 : break; 356 1 : case CHIP_ERROR_UNKNOWN_RESOURCE_ID.AsInteger(): 357 1 : desc = "Unknown resource ID"; 358 1 : break; 359 1 : case CHIP_ERROR_VERSION_MISMATCH.AsInteger(): 360 1 : desc = "Version mismatch"; 361 1 : break; 362 1 : case CHIP_EVENT_ID_FOUND.AsInteger(): 363 1 : desc = "Event ID matching criteria was found"; 364 1 : break; 365 2 : case CHIP_ERROR_INTERNAL.AsInteger(): 366 2 : desc = "Internal error"; 367 2 : break; 368 1 : case CHIP_ERROR_OPEN_FAILED.AsInteger(): 369 1 : desc = "Open file failed"; 370 1 : break; 371 1 : case CHIP_ERROR_READ_FAILED.AsInteger(): 372 1 : desc = "Read from file failed"; 373 1 : break; 374 1 : case CHIP_ERROR_WRITE_FAILED.AsInteger(): 375 1 : desc = "Write to file failed"; 376 1 : break; 377 1 : case CHIP_ERROR_DECODE_FAILED.AsInteger(): 378 1 : desc = "Decoding failed"; 379 1 : break; 380 1 : case CHIP_ERROR_MDNS_COLLISION.AsInteger(): 381 1 : desc = "mDNS collision"; 382 1 : break; 383 1 : case CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_PATH_IB.AsInteger(): 384 1 : desc = "Malformed Interacton Model Attribute Path IB"; 385 1 : break; 386 1 : case CHIP_ERROR_IM_MALFORMED_EVENT_PATH_IB.AsInteger(): 387 1 : desc = "Malformed Interacton Model Event Path IB"; 388 1 : break; 389 1 : case CHIP_ERROR_IM_MALFORMED_EVENT_DATA_IB.AsInteger(): 390 1 : desc = "Malformed Interacton Model Event Data IB"; 391 1 : break; 392 1 : case CHIP_ERROR_PEER_NODE_NOT_FOUND.AsInteger(): 393 1 : desc = "Unable to find the peer node"; 394 1 : break; 395 1 : case CHIP_ERROR_HSM.AsInteger(): 396 1 : desc = "Hardware security module"; 397 1 : break; 398 1 : case CHIP_ERROR_REAL_TIME_NOT_SYNCED.AsInteger(): 399 1 : desc = "Real time not synchronized"; 400 1 : break; 401 1 : case CHIP_ERROR_UNEXPECTED_EVENT.AsInteger(): 402 1 : desc = "Unexpected event"; 403 1 : break; 404 0 : case CHIP_ERROR_ENDPOINT_POOL_FULL.AsInteger(): 405 0 : desc = "Endpoint pool full"; 406 0 : break; 407 0 : case CHIP_ERROR_INBOUND_MESSAGE_TOO_BIG.AsInteger(): 408 0 : desc = "Inbound message too big"; 409 0 : break; 410 0 : case CHIP_ERROR_OUTBOUND_MESSAGE_TOO_BIG.AsInteger(): 411 0 : desc = "Outbound message too big"; 412 0 : break; 413 0 : case CHIP_ERROR_DUPLICATE_MESSAGE_RECEIVED.AsInteger(): 414 0 : desc = "Duplicate message received"; 415 0 : break; 416 0 : case CHIP_ERROR_INVALID_PUBLIC_KEY.AsInteger(): 417 0 : desc = "Invalid public key"; 418 0 : break; 419 0 : case CHIP_ERROR_FABRIC_MISMATCH_ON_ICA.AsInteger(): 420 0 : desc = "Fabric mismatch on ICA"; 421 0 : break; 422 0 : case CHIP_ERROR_NO_SHARED_TRUSTED_ROOT.AsInteger(): 423 0 : desc = "No shared trusted root"; 424 0 : break; 425 1 : case CHIP_ERROR_IM_STATUS_CODE_RECEIVED.AsInteger(): 426 1 : desc = "Interaction Model Error"; 427 1 : break; 428 1 : case CHIP_ERROR_IM_MALFORMED_DATA_VERSION_FILTER_IB.AsInteger(): 429 1 : desc = "Malformed Interaction Model Data Version Filter IB"; 430 1 : break; 431 1 : case CHIP_ERROR_NOT_FOUND.AsInteger(): 432 1 : desc = "The item referenced in the function call was not found"; 433 1 : break; 434 2 : case CHIP_ERROR_INVALID_FILE_IDENTIFIER.AsInteger(): 435 2 : desc = "The file identifier, encoded in the first few bytes of a processed file, has unexpected value"; 436 2 : break; 437 3 : case CHIP_ERROR_BUSY.AsInteger(): 438 3 : desc = "The Resource is busy and cannot process the request"; 439 3 : break; 440 1 : case CHIP_ERROR_MAX_RETRY_EXCEEDED.AsInteger(): 441 1 : desc = "The maximum retry limit has been exceeded"; 442 1 : break; 443 1 : case CHIP_ERROR_PROVIDER_LIST_EXHAUSTED.AsInteger(): 444 1 : desc = "The provider list has been exhausted"; 445 1 : break; 446 1 : case CHIP_ERROR_INVALID_SCHEME_PREFIX.AsInteger(): 447 1 : desc = "The scheme field contains an invalid prefix"; 448 1 : break; 449 1 : case CHIP_ERROR_MISSING_URI_SEPARATOR.AsInteger(): 450 1 : desc = "The URI separator is missing"; 451 1 : break; 452 1 : case CHIP_ERROR_HANDLER_NOT_SET.AsInteger(): 453 1 : desc = "Callback function or callable object is not set"; 454 1 : break; 455 : } 456 : #endif // !CHIP_CONFIG_SHORT_ERROR_STR 457 : 458 558 : FormatError(buf, bufSize, "CHIP", err, desc); 459 : 460 558 : return true; 461 : } 462 : 463 : } // namespace chip