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