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
18 #include "bluetooth_log.h"
19
20 #include "securec.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 using namespace std;
27
28 namespace OHOS {
29 namespace Bluetooth {
ConvertAddr(const unsigned char in[6],std::string & out)30 void ConvertAddr(const unsigned char in[6], std::string &out)
31 {
32 char temp[18] = {0};
33 int ret = sprintf_s(temp, sizeof(temp), "%02X:%02X:%02X:%02X:%02X:%02X",
34 in[0], in[1], in[2], in[3], in[4], in[5]);
35 if (ret == -1) {
36 HILOGE("ConvertAddr sprintf_s return error, ret -1");
37 }
38 out = string(temp);
39 }
40
GetAddrFromString(std::string in,unsigned char out[6])41 void GetAddrFromString(std::string in, unsigned char out[6]) {
42 int j = 0;
43 for (unsigned int i = 0; i < in.length(); i++) {
44 if (in.at(i) != ':') {
45 out[j] = strtoul(in.substr(i, 2).c_str(), 0, 16);
46 i += 2;
47 j++;
48 }
49 }
50 }
51
GetGattcResult(int ret)52 int GetGattcResult(int ret)
53 {
54 return (ret == OHOS_BT_STATUS_SUCCESS) ? OHOS_BT_STATUS_SUCCESS : OHOS_BT_STATUS_FAIL;
55 }
56
GetAddrFromByte(unsigned char in[6],std::string & out)57 void GetAddrFromByte(unsigned char in[6], std::string &out)
58 {
59 char temp[18] = {0};
60 sprintf_s(temp, sizeof(temp), "%02X:%02X:%02X:%02X:%02X:%02X",
61 in[0], in[1], in[2], in[3], in[4], in[5]);
62 out = string(temp);
63 }
64 } // namespace Bluetooth
65 } // namespace OHOS
66 #ifdef __cplusplus
67 }
68 #endif
69