• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_string/format.h"
18 
19 #pragma clang diagnostic ignored "-Wswitch-enum"
20 
21 namespace bt {
22 namespace {
23 
ErrorToString(att::ErrorCode ecode)24 constexpr const char* ErrorToString(att::ErrorCode ecode) {
25   switch (ecode) {
26     case att::ErrorCode::kInvalidHandle:
27       return "invalid handle";
28     case att::ErrorCode::kReadNotPermitted:
29       return "read not permitted";
30     case att::ErrorCode::kWriteNotPermitted:
31       return "write not permitted";
32     case att::ErrorCode::kInvalidPDU:
33       return "invalid PDU";
34     case att::ErrorCode::kInsufficientAuthentication:
35       return "insuff. authentication";
36     case att::ErrorCode::kRequestNotSupported:
37       return "request not supported";
38     case att::ErrorCode::kInvalidOffset:
39       return "invalid offset";
40     case att::ErrorCode::kInsufficientAuthorization:
41       return "insuff. authorization";
42     case att::ErrorCode::kPrepareQueueFull:
43       return "prepare queue full";
44     case att::ErrorCode::kAttributeNotFound:
45       return "attribute not found";
46     case att::ErrorCode::kAttributeNotLong:
47       return "attribute not long";
48     case att::ErrorCode::kInsufficientEncryptionKeySize:
49       return "insuff. encryption key size";
50     case att::ErrorCode::kInvalidAttributeValueLength:
51       return "invalid attribute value length";
52     case att::ErrorCode::kUnlikelyError:
53       return "unlikely error";
54     case att::ErrorCode::kInsufficientEncryption:
55       return "insuff. encryption";
56     case att::ErrorCode::kUnsupportedGroupType:
57       return "unsupported group type";
58     case att::ErrorCode::kInsufficientResources:
59       return "insuff. resources";
60     default:
61       break;
62   }
63 
64   return "(unknown)";
65 }
66 
67 }  // namespace
68 
ToString(att::ErrorCode ecode)69 std::string ProtocolErrorTraits<att::ErrorCode>::ToString(
70     att::ErrorCode ecode) {
71   constexpr size_t out_size =
72       sizeof("invalid attribute value length (ATT 0x0d)");
73   char out[out_size] = "";
74   pw::StatusWithSize status =
75       pw::string::Format({out, sizeof(out)},
76                          "%s (ATT %#.2hhx)",
77                          ErrorToString(ecode),
78                          static_cast<unsigned char>(ecode));
79   BT_DEBUG_ASSERT(status.ok());
80   return out;
81 }
82 
83 }  // namespace bt
84