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