1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <base/strings/stringprintf.h>
20 
21 #include <cstdint>
22 
23 #include "macros.h"
24 
25 /* BTM application return status codes */
26 enum class tBTM_STATUS : uint8_t {
27   BTM_SUCCESS = 0,                   /* 0  Command succeeded                 */
28   BTM_CMD_STARTED,                   /* 1  Command started OK.               */
29   BTM_BUSY,                          /* 2  Device busy with another command  */
30   BTM_NO_RESOURCES,                  /* 3  No resources to issue command     */
31   BTM_MODE_UNSUPPORTED,              /* 4  Request for 1 or more unsupported modes */
32   BTM_ILLEGAL_VALUE,                 /* 5  Illegal parameter value           */
33   BTM_WRONG_MODE,                    /* 6  Device in wrong mode for request  */
34   BTM_UNKNOWN_ADDR,                  /* 7  Unknown remote BD address         */
35   BTM_DEVICE_TIMEOUT,                /* 8  Device timeout                    */
36   BTM_BAD_VALUE_RET,                 /* 9  A bad value was received from HCI */
37   BTM_ERR_PROCESSING,                /* 10 Generic error                     */
38   BTM_NOT_AUTHORIZED,                /* 11 Authorization failed              */
39   BTM_DEV_RESET,                     /* 12 Device has been reset             */
40   BTM_CMD_STORED,                    /* 13 request is stored in control block */
41   BTM_ILLEGAL_ACTION,                /* 14 state machine gets illegal command */
42   BTM_DELAY_CHECK,                   /* 15 delay the check on encryption */
43   BTM_SCO_BAD_LENGTH,                /* 16 Bad SCO over HCI data length */
44   BTM_SUCCESS_NO_SECURITY,           /* 17 security passed, no security set  */
45   BTM_FAILED_ON_SECURITY,            /* 18 security failed                   */
46   BTM_REPEATED_ATTEMPTS,             /* 19 repeated attempts for LE security requests */
47   BTM_MODE4_LEVEL4_NOT_SUPPORTED,    /* 20 Secure Connections Only Mode can't be
48                                         supported */
49   BTM_DEV_RESTRICT_LISTED,           /* 21 The device is restrict listed */
50   BTM_ERR_KEY_MISSING,               /* 22 Handle for Pin or Key Missing */
51   BTM_NOT_AUTHENTICATED,             /* 23 the link is not authenticated */
52   BTM_NOT_ENCRYPTED,                 /* 24 the link is not encrypted */
53   BTM_INSUFFICIENT_ENCRYPT_KEY_SIZE, /* 25 the encrypt key size not sufficient
54                                       */
55   BTM_MAX_STATUS_VALUE,
56   BTM_UNDEFINED = 0xFF,
57 };
58 
btm_status_value(const tBTM_STATUS & status)59 inline uint8_t btm_status_value(const tBTM_STATUS& status) { return static_cast<uint8_t>(status); }
60 
to_btm_status(const uint8_t & value)61 inline tBTM_STATUS to_btm_status(const uint8_t& value) {
62   if (value >= static_cast<unsigned>(tBTM_STATUS::BTM_MAX_STATUS_VALUE)) {
63     return tBTM_STATUS::BTM_UNDEFINED;
64   }
65   return static_cast<tBTM_STATUS>(value);
66 }
67 
btm_status_text(const tBTM_STATUS & status)68 inline std::string btm_status_text(const tBTM_STATUS& status) {
69   switch (status) {
70     CASE_RETURN_TEXT(tBTM_STATUS::BTM_SUCCESS);
71     CASE_RETURN_TEXT(tBTM_STATUS::BTM_CMD_STARTED);
72     CASE_RETURN_TEXT(tBTM_STATUS::BTM_BUSY);
73     CASE_RETURN_TEXT(tBTM_STATUS::BTM_NO_RESOURCES);
74     CASE_RETURN_TEXT(tBTM_STATUS::BTM_MODE_UNSUPPORTED);
75     CASE_RETURN_TEXT(tBTM_STATUS::BTM_ILLEGAL_VALUE);
76     CASE_RETURN_TEXT(tBTM_STATUS::BTM_WRONG_MODE);
77     CASE_RETURN_TEXT(tBTM_STATUS::BTM_UNKNOWN_ADDR);
78     CASE_RETURN_TEXT(tBTM_STATUS::BTM_DEVICE_TIMEOUT);
79     CASE_RETURN_TEXT(tBTM_STATUS::BTM_BAD_VALUE_RET);
80     CASE_RETURN_TEXT(tBTM_STATUS::BTM_ERR_PROCESSING);
81     CASE_RETURN_TEXT(tBTM_STATUS::BTM_NOT_AUTHORIZED);
82     CASE_RETURN_TEXT(tBTM_STATUS::BTM_DEV_RESET);
83     CASE_RETURN_TEXT(tBTM_STATUS::BTM_CMD_STORED);
84     CASE_RETURN_TEXT(tBTM_STATUS::BTM_ILLEGAL_ACTION);
85     CASE_RETURN_TEXT(tBTM_STATUS::BTM_DELAY_CHECK);
86     CASE_RETURN_TEXT(tBTM_STATUS::BTM_SCO_BAD_LENGTH);
87     CASE_RETURN_TEXT(tBTM_STATUS::BTM_SUCCESS_NO_SECURITY);
88     CASE_RETURN_TEXT(tBTM_STATUS::BTM_FAILED_ON_SECURITY);
89     CASE_RETURN_TEXT(tBTM_STATUS::BTM_REPEATED_ATTEMPTS);
90     CASE_RETURN_TEXT(tBTM_STATUS::BTM_MODE4_LEVEL4_NOT_SUPPORTED);
91     CASE_RETURN_TEXT(tBTM_STATUS::BTM_DEV_RESTRICT_LISTED);
92     CASE_RETURN_TEXT(tBTM_STATUS::BTM_ERR_KEY_MISSING);
93     CASE_RETURN_TEXT(tBTM_STATUS::BTM_NOT_AUTHENTICATED);
94     CASE_RETURN_TEXT(tBTM_STATUS::BTM_NOT_ENCRYPTED);
95     CASE_RETURN_TEXT(tBTM_STATUS::BTM_INSUFFICIENT_ENCRYPT_KEY_SIZE);
96     default:
97       return base::StringPrintf("UNKNOWN[%hhu]", status);
98   }
99 }
100 
101 namespace std {
102 template <>
103 struct formatter<tBTM_STATUS> : enum_formatter<tBTM_STATUS> {};
104 }  // namespace std
105