xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/host/sm/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/sm/error.h"
16 
17 #include "pw_string/format.h"
18 
19 namespace bt {
20 namespace {
21 
ErrorToString(sm::ErrorCode ecode)22 constexpr const char* ErrorToString(sm::ErrorCode ecode) {
23   switch (ecode) {
24     case sm::ErrorCode::kPasskeyEntryFailed:
25       return "passkey entry failed";
26     case sm::ErrorCode::kOOBNotAvailable:
27       return "OOB not available";
28     case sm::ErrorCode::kAuthenticationRequirements:
29       return "authentication requirements";
30     case sm::ErrorCode::kConfirmValueFailed:
31       return "confirm value failed";
32     case sm::ErrorCode::kPairingNotSupported:
33       return "pairing not supported";
34     case sm::ErrorCode::kEncryptionKeySize:
35       return "encryption key size";
36     case sm::ErrorCode::kCommandNotSupported:
37       return "command not supported";
38     case sm::ErrorCode::kUnspecifiedReason:
39       return "unspecified reason";
40     case sm::ErrorCode::kRepeatedAttempts:
41       return "repeated attempts";
42     case sm::ErrorCode::kInvalidParameters:
43       return "invalid parameters";
44     case sm::ErrorCode::kDHKeyCheckFailed:
45       return "DHKey check failed";
46     case sm::ErrorCode::kNumericComparisonFailed:
47       return "numeric comparison failed";
48     case sm::ErrorCode::kBREDRPairingInProgress:
49       return "BR/EDR pairing in progress";
50     case sm::ErrorCode::kCrossTransportKeyDerivationNotAllowed:
51       return "cross-transport key dist. not allowed";
52     default:
53       break;
54   }
55   return "(unknown)";
56 }
57 
58 constexpr size_t kMaxErrorStringSize =
59     std::string_view(
60         ErrorToString(sm::ErrorCode::kCrossTransportKeyDerivationNotAllowed))
61         .size();
62 
63 }  // namespace
64 
65 // static
ToString(sm::ErrorCode ecode)66 std::string ProtocolErrorTraits<sm::ErrorCode>::ToString(sm::ErrorCode ecode) {
67   const size_t out_size = kMaxErrorStringSize + sizeof(" (SMP 0x0e)");
68   char out[out_size] = "";
69   pw::StatusWithSize status =
70       pw::string::Format({out, sizeof(out)},
71                          "%s (SMP %#.2x)",
72                          ErrorToString(ecode),
73                          static_cast<unsigned int>(ecode));
74   PW_DCHECK(status.ok());
75   return out;
76 }
77 
78 }  // namespace bt
79