1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://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,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "bluetooth_utils.h"
16 #include <map>
17 #include "__config"
18 #include "bluetooth_errorcode.h"
19 #include "bluetooth_def.h"
20 #include "bluetooth_log.h"
21 #include "iosfwd"
22 #include "string"
23
24 using namespace std;
25
26 namespace OHOS {
27 namespace Bluetooth {
28 constexpr int startPos = 6;
29 constexpr int endPos = 13;
GetEncryptAddr(std::string addr)30 std::string GetEncryptAddr(std::string addr)
31 {
32 if (addr.empty() || addr.length() != ADDRESS_LENGTH) {
33 HILOGE("addr is invalid.");
34 return std::string("");
35 }
36 std::string tmp = "**:**:**:**:**:**";
37 std::string out = addr;
38 // 00:01:**:**:**:05
39 for (int i = startPos; i <= endPos; i++) {
40 out[i] = tmp[i];
41 }
42 return out;
43 }
44
GetBtStateName(int state)45 std::string GetBtStateName(int state)
46 {
47 switch (state) {
48 case BTStateID::STATE_TURNING_ON:
49 return "STATE_TURNING_ON(0)";
50 case BTStateID::STATE_TURN_ON:
51 return "STATE_TURN_ON(1)";
52 case BTStateID::STATE_TURNING_OFF:
53 return "STATE_TURNING_OFF(2)";
54 case BTStateID::STATE_TURN_OFF:
55 return "STATE_TURN_OFF(3)";
56 default:
57 return "Unknown";
58 }
59 }
60
GetBtTransportName(int transport)61 std::string GetBtTransportName(int transport)
62 {
63 switch (transport) {
64 case BTTransport::ADAPTER_BREDR:
65 return "ADAPTER_BREDR(0)";
66 case BTTransport::ADAPTER_BLE:
67 return "ADAPTER_BLE(1)";
68 default:
69 return "Unknown";
70 }
71 }
72
GetProfileConnStateName(int state)73 std::string GetProfileConnStateName(int state)
74 {
75 switch (state) {
76 case static_cast<int>(BTConnectState::CONNECTING):
77 return "CONNECTING(0)";
78 case static_cast<int>(BTConnectState::CONNECTED):
79 return "CONNECTED(1)";
80 case static_cast<int>(BTConnectState::DISCONNECTING):
81 return "DISCONNECTING(2)";
82 case static_cast<int>(BTConnectState::DISCONNECTED):
83 return "DISCONNECTED(3)";
84 default:
85 return "Unknown";
86 }
87 }
88
89 static std::map<int32_t, std::string> BtErrCodeMap {
90 { BtErrCode::BT_SUCCESS, "BT_SUCCESS" },
91 { BtErrCode::BT_ERR_PERMISSION_FAILED, "BT_ERR_PERMISSION_FAILED" },
92 { BtErrCode::BT_ERR_SYSTEM_PERMISSION_FAILED, "BT_ERR_SYSTEM_PERMISSION_FAILED" },
93 { BtErrCode::BT_ERR_INVALID_PARAM, "BT_ERR_INVALID_PARAM" },
94 { BtErrCode::BT_ERR_API_NOT_SUPPORT, "BT_ERR_API_NOT_SUPPORT" },
95 { BtErrCode::BT_ERR_SERVICE_DISCONNECTED, "BT_ERR_SERVICE_DISCONNECTED" },
96 { BtErrCode::BT_ERR_UNBONDED_DEVICE, "BT_ERR_UNBONDED_DEVICE" },
97 { BtErrCode::BT_ERR_INVALID_STATE, "BT_ERR_INVALID_STATE" },
98 { BtErrCode::BT_ERR_PROFILE_DISABLED, "BT_ERR_PROFILE_DISABLED" },
99 { BtErrCode::BT_ERR_DEVICE_DISCONNECTED, "BT_ERR_DEVICE_DISCONNECTED" },
100 { BtErrCode::BT_ERR_MAX_CONNECTION, "BT_ERR_MAX_CONNECTION" },
101 { BtErrCode::BT_ERR_INTERNAL_ERROR, "BT_ERR_INTERNAL_ERROR" },
102 { BtErrCode::BT_ERR_IPC_TRANS_FAILED, "BT_ERR_IPC_TRANS_FAILED" },
103 { BtErrCode::BT_ERR_GATT_READ_NOT_PERMITTED, "BT_ERR_GATT_READ_NOT_PERMITTED" },
104 { BtErrCode::BT_ERR_GATT_WRITE_NOT_PERMITTED, "BT_ERR_GATT_WRITE_NOT_PERMITTED" },
105 { BtErrCode::BT_ERR_GATT_MAX_SERVER, "BT_ERR_GATT_MAX_SERVER" },
106 { BtErrCode::BT_ERR_SPP_SERVER_STATE, "BT_ERR_SPP_SERVER_STATE" },
107 { BtErrCode::BT_ERR_SPP_BUSY, "BT_ERR_SPP_BUSY" },
108 { BtErrCode::BT_ERR_SPP_DEVICE_NOT_FOUND, "BT_ERR_SPP_DEVICE_NOT_FOUND" },
109 { BtErrCode::BT_ERR_SPP_IO, "BT_ERR_SPP_IO" },
110 };
111
GetErrorCode(int32_t errCode)112 std::string GetErrorCode(int32_t errCode)
113 {
114 std::string errlog = "unknown error code: ";
115 auto iter = BtErrCodeMap.find(errCode);
116 if (iter != BtErrCodeMap.end()) {
117 errlog = iter->second;
118 }
119 errlog.append("(").append(std::to_string(errCode)).append(")");
120 return errlog;
121 }
122
123 } // namespace Bluetooth
124 } // namespace OHOS