1 /*
2 * Copyright (C) 2021 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_c_adapter_adapter_utils"
17 #endif
18
19 #include "ohos_bt_adapter_utils.h"
20 #include "__config"
21 #include "bluetooth_def.h"
22 #include "bluetooth_log.h"
23 #include "ohos_bt_def.h"
24 #include "securec.h"
25 #include <cstdlib>
26 #include "string"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 using namespace std;
33
34 namespace OHOS {
35 namespace Bluetooth {
36 constexpr int HEADSET_SUPPORT_VLINK_PRODUCT_ID_MIN = 0x3500;
37 constexpr int HEADSET_SUPPORT_VLINK_PRODUCT_ID_MAX = 0x48ff;
38 constexpr int GLASSES_SUPPORT_VLINK_PRODUCT_ID_MIN = 0x1700;
39 constexpr int GLASSES_SUPPORT_VLINK_PRODUCT_ID_MAX = 0x18ff;
40
ConvertAddr(const unsigned char in[6],std::string & out)41 void ConvertAddr(const unsigned char in[6], std::string &out)
42 {
43 char temp[18] = {0}; // convert addr len.
44 int ret = sprintf_s(temp, sizeof(temp), "%02X:%02X:%02X:%02X:%02X:%02X",
45 in[0], in[1], in[2], in[3], in[4], in[5]);
46 if (ret == -1) {
47 HILOGE("ConvertAddr sprintf_s return error, ret -1");
48 }
49 out = string(temp);
50 }
51
GetAddrFromString(std::string in,unsigned char out[6])52 void GetAddrFromString(std::string in, unsigned char out[6])
53 {
54 int j = 0;
55 for (unsigned int i = 0; i < in.length(); i++) {
56 if (in.at(i) != ':') {
57 out[j] = strtoul(in.substr(i, 2).c_str(), 0, 16); // 2,16 作为截取字符的开始位置或截取长度
58 i += 2; // for循环中每轮i值增加2
59 j++;
60 }
61 }
62 }
63
GetGattcResult(int ret)64 int GetGattcResult(int ret)
65 {
66 return (ret == OHOS_BT_STATUS_SUCCESS) ? OHOS_BT_STATUS_SUCCESS : OHOS_BT_STATUS_FAIL;
67 }
68
GetAddrFromByte(unsigned char in[6],std::string & out)69 void GetAddrFromByte(unsigned char in[6], std::string &out)
70 {
71 char temp[18] = {0};
72 (void)sprintf_s(temp, sizeof(temp), "%02X:%02X:%02X:%02X:%02X:%02X",
73 in[0], in[1], in[2], in[3], in[4], in[5]); // 0, 1, 2, 3, 4, 5, 指MAC地址的6个数据段
74 out = string(temp);
75 }
76
GetAclStateName(int transport,int state,std::string & out)77 void GetAclStateName(int transport, int state, std::string &out)
78 {
79 switch (transport) {
80 case ADAPTER_BREDR:
81 if (state == ACL_CONNECTION_STATE_CONNECTED) {
82 out = "OHOS_GAP_ACL_STATE_CONNECTED(0)";
83 } else if (state == ACL_CONNECTION_STATE_DISCONNECTED) {
84 out = "OHOS_GAP_ACL_STATE_DISCONNECTED(1)";
85 } else {
86 out = "Unknown state";
87 }
88 break;
89 case ADAPTER_BLE:
90 if (state == ACL_CONNECTION_STATE_CONNECTED) {
91 out = "OHOS_GAP_ACL_STATE_LE_CONNECTED(2)";
92 } else if (state == ACL_CONNECTION_STATE_DISCONNECTED) {
93 out = "OHOS_GAP_ACL_STATE_LE_DISCONNECTED(3)";
94 } else {
95 out = "Unknown state";
96 }
97 break;
98 default:
99 out = "Unknown transport";
100 break;
101 }
102 }
103
ConvertAclState(int transport,int state)104 GapAclState ConvertAclState(int transport, int state)
105 {
106 if (transport == ADAPTER_BREDR && state == ACL_CONNECTION_STATE_CONNECTED) {
107 return OHOS_GAP_ACL_STATE_CONNECTED;
108 } else if (transport == ADAPTER_BREDR && state == ACL_CONNECTION_STATE_DISCONNECTED) {
109 return OHOS_GAP_ACL_STATE_DISCONNECTED;
110 } else if (transport == ADAPTER_BLE && state == ACL_CONNECTION_STATE_CONNECTED) {
111 return OHOS_GAP_ACL_STATE_LE_CONNECTED;
112 } else if (transport == ADAPTER_BLE && state == ACL_CONNECTION_STATE_DISCONNECTED) {
113 return OHOS_GAP_ACL_STATE_LE_DISCONNECTED;
114 }
115
116 HILOGE("invalid transport: %{public}d, state: %{public}d", transport, state);
117 return OHOS_GAP_ACL_STATE_INVALID;
118 }
119
ConvertDataToHex(const unsigned char * data,unsigned int dataLen,std::string & outStr)120 void ConvertDataToHex(const unsigned char *data, unsigned int dataLen, std::string &outStr)
121 {
122 const std::string hex = "0123456789ABCDEF";
123 const uint8_t sizeFour = 4;
124 for (size_t i = 0; i < dataLen; ++i) {
125 uint8_t n = data[i];
126 outStr.push_back(hex[n >> sizeFour]);
127 outStr.push_back(hex[n & 0xF]);
128 }
129 }
130
ConvertBtTransport(const int ohosTransport,int & innerTransport)131 void ConvertBtTransport(const int ohosTransport, int &innerTransport)
132 {
133 if (ohosTransport == OHOS_BT_TRANSPORT_BR_EDR) {
134 innerTransport = BT_TRANSPORT_BREDR;
135 } else if (ohosTransport == OHOS_BT_TRANSPORT_LE) {
136 innerTransport = BT_TRANSPORT_BLE;
137 } else {
138 innerTransport = BT_TRANSPORT_NONE;
139 }
140 }
141
IsSupportVlinkFeature(int productId)142 bool IsSupportVlinkFeature(int productId)
143 {
144 return
145 ((productId >= HEADSET_SUPPORT_VLINK_PRODUCT_ID_MIN) && (productId <= HEADSET_SUPPORT_VLINK_PRODUCT_ID_MAX)) ||
146 ((productId >= GLASSES_SUPPORT_VLINK_PRODUCT_ID_MIN) && (productId <= GLASSES_SUPPORT_VLINK_PRODUCT_ID_MAX));
147 }
148 } // namespace Bluetooth
149 } // namespace OHOS
150 #ifdef __cplusplus
151 }
152 #endif
153