1 // Copyright 2023 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 #include "pw_bluetooth_sapphire/internal/host/att/error.h" 16 17 #include <pw_assert/check.h> 18 19 #include "pw_preprocessor/compiler.h" 20 #include "pw_string/format.h" 21 22 namespace bt { 23 namespace { 24 ErrorToString(att::ErrorCode ecode)25constexpr const char* ErrorToString(att::ErrorCode ecode) { 26 PW_MODIFY_DIAGNOSTICS_PUSH(); 27 PW_MODIFY_DIAGNOSTIC(ignored, "-Wswitch-enum"); 28 switch (ecode) { 29 case att::ErrorCode::kInvalidHandle: 30 return "invalid handle"; 31 case att::ErrorCode::kReadNotPermitted: 32 return "read not permitted"; 33 case att::ErrorCode::kWriteNotPermitted: 34 return "write not permitted"; 35 case att::ErrorCode::kInvalidPDU: 36 return "invalid PDU"; 37 case att::ErrorCode::kInsufficientAuthentication: 38 return "insuff. authentication"; 39 case att::ErrorCode::kRequestNotSupported: 40 return "request not supported"; 41 case att::ErrorCode::kInvalidOffset: 42 return "invalid offset"; 43 case att::ErrorCode::kInsufficientAuthorization: 44 return "insuff. authorization"; 45 case att::ErrorCode::kPrepareQueueFull: 46 return "prepare queue full"; 47 case att::ErrorCode::kAttributeNotFound: 48 return "attribute not found"; 49 case att::ErrorCode::kAttributeNotLong: 50 return "attribute not long"; 51 case att::ErrorCode::kInsufficientEncryptionKeySize: 52 return "insuff. encryption key size"; 53 case att::ErrorCode::kInvalidAttributeValueLength: 54 return "invalid attribute value length"; 55 case att::ErrorCode::kUnlikelyError: 56 return "unlikely error"; 57 case att::ErrorCode::kInsufficientEncryption: 58 return "insuff. encryption"; 59 case att::ErrorCode::kUnsupportedGroupType: 60 return "unsupported group type"; 61 case att::ErrorCode::kInsufficientResources: 62 return "insuff. resources"; 63 default: 64 break; 65 } 66 PW_MODIFY_DIAGNOSTICS_POP(); 67 68 return "(unknown)"; 69 } 70 71 } // namespace 72 ToString(att::ErrorCode ecode)73std::string ProtocolErrorTraits<att::ErrorCode>::ToString( 74 att::ErrorCode ecode) { 75 constexpr size_t out_size = 76 sizeof("invalid attribute value length (ATT 0x0d)"); 77 char out[out_size] = ""; 78 pw::StatusWithSize status = 79 pw::string::Format({out, sizeof(out)}, 80 "%s (ATT %#.2hhx)", 81 ErrorToString(ecode), 82 static_cast<unsigned char>(ecode)); 83 PW_DCHECK(status.ok()); 84 return out; 85 } 86 87 } // namespace bt 88