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