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