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 "classic_utils.h"
17 
18 #include <sstream>
19 
20 #include "classic_defs.h"
21 #include "securec.h"
22 
23 namespace OHOS {
24 namespace bluetooth {
IntToHexString(uint8_t value)25 std::string ClassicUtils::IntToHexString(uint8_t value)
26 {
27     std::stringstream strStream;
28     char token[HEX_FORMAT_SIZE] = {0};
29     (void)sprintf_s(token, HEX_FORMAT_SIZE, "%02X", value);
30     strStream << token;
31     return strStream.str();
32 }
33 
ConvertIntToHexString(const std::vector<uint8_t> & value)34 std::string ClassicUtils::ConvertIntToHexString(const std::vector<uint8_t> &value)
35 {
36     std::string strs;
37     for (auto iter = value.begin(); iter != value.end(); iter++) {
38         uint8_t temp = *iter;
39         strs += IntToHexString(temp);
40     }
41     return strs;
42 }
43 
ConvertHexStringToInt(const std::string & str,std::vector<uint8_t> & value)44 void ClassicUtils::ConvertHexStringToInt(const std::string &str, std::vector<uint8_t> &value)
45 {
46     for (size_t i = 0; i < str.size(); i = i + 2) {  // 2 is the length of str to be truncated
47         long k = std::stol(str.substr(i, 2), nullptr, 16);
48         value.push_back(k);
49     }
50 }
51 
ConvertUuidToString(const std::vector<Uuid> & uuids)52 std::string ClassicUtils::ConvertUuidToString(const std::vector<Uuid> &uuids)
53 {
54     std::string uuidStr = "";
55     if (uuids.empty()) {
56         return uuidStr;
57     }
58 
59     for (auto &uuid : uuids) {
60         std::array<uint8_t, UUID128_BYTES_TYPE> value;
61         value = uuid.ConvertTo128Bits();
62         std::vector<uint8_t> vecUuid;
63         for (int idx = 0; idx < UUID128_BYTES_TYPE; idx++) {
64             vecUuid.push_back(value[idx]);
65         }
66         uuidStr += ConvertIntToHexString(vecUuid) + ",";
67     }
68     return uuidStr;
69 }
70 
ConvertStringToUuid(const std::string & value)71 std::vector<Uuid> ClassicUtils::ConvertStringToUuid(const std::string &value)
72 {
73     std::vector<Uuid> uuids;
74     if (value.empty()) {
75         return uuids;
76     }
77 
78     size_t index = value.find(',', 0);
79     int startPos = 0;
80     while (index != (value.length() - 1)) {
81         std::string uuidStr = value.substr(startPos, (index - startPos));
82         Uuid uuid = Uuid::ConvertFromString(uuidStr);
83         uuids.push_back(uuid);
84         startPos = index + 1;
85         index = value.find(',', startPos);
86     }
87     return uuids;
88 }
89 }  // namespace bluetooth
90 }  // namespace OHOS