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 : 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 typedef uint8_t tBTM_STATUS;
59 
btm_status_value(const tBTM_STATUS & status)60 inline uint8_t btm_status_value(const tBTM_STATUS& status) {
61   return static_cast<uint8_t>(status);
62 }
63 
to_btm_status(const uint8_t & value)64 inline tBTM_STATUS to_btm_status(const uint8_t& value) {
65   if (value >= BTM_MAX_STATUS_VALUE) return BTM_UNDEFINED;
66   return static_cast<tBTM_STATUS>(value);
67 }
68 
btm_status_text(const tBTM_STATUS & status)69 inline std::string btm_status_text(const tBTM_STATUS& status) {
70   switch (status) {
71     CASE_RETURN_TEXT(BTM_SUCCESS);
72     CASE_RETURN_TEXT(BTM_CMD_STARTED);
73     CASE_RETURN_TEXT(BTM_BUSY);
74     CASE_RETURN_TEXT(BTM_NO_RESOURCES);
75     CASE_RETURN_TEXT(BTM_MODE_UNSUPPORTED);
76     CASE_RETURN_TEXT(BTM_ILLEGAL_VALUE);
77     CASE_RETURN_TEXT(BTM_WRONG_MODE);
78     CASE_RETURN_TEXT(BTM_UNKNOWN_ADDR);
79     CASE_RETURN_TEXT(BTM_DEVICE_TIMEOUT);
80     CASE_RETURN_TEXT(BTM_BAD_VALUE_RET);
81     CASE_RETURN_TEXT(BTM_ERR_PROCESSING);
82     CASE_RETURN_TEXT(BTM_NOT_AUTHORIZED);
83     CASE_RETURN_TEXT(BTM_DEV_RESET);
84     CASE_RETURN_TEXT(BTM_CMD_STORED);
85     CASE_RETURN_TEXT(BTM_ILLEGAL_ACTION);
86     CASE_RETURN_TEXT(BTM_DELAY_CHECK);
87     CASE_RETURN_TEXT(BTM_SCO_BAD_LENGTH);
88     CASE_RETURN_TEXT(BTM_SUCCESS_NO_SECURITY);
89     CASE_RETURN_TEXT(BTM_FAILED_ON_SECURITY);
90     CASE_RETURN_TEXT(BTM_REPEATED_ATTEMPTS);
91     CASE_RETURN_TEXT(BTM_MODE4_LEVEL4_NOT_SUPPORTED);
92     CASE_RETURN_TEXT(BTM_DEV_RESTRICT_LISTED);
93     CASE_RETURN_TEXT(BTM_ERR_KEY_MISSING);
94     CASE_RETURN_TEXT(BTM_NOT_AUTHENTICATED);
95     CASE_RETURN_TEXT(BTM_NOT_ENCRYPTED);
96     CASE_RETURN_TEXT(BTM_INSUFFICIENT_ENCRYPT_KEY_SIZE);
97     default:
98       return base::StringPrintf("UNKNOWN[%hhu]", status);
99   }
100 }
101