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