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 "raw_address.h"
17 #include <vector>
18 #include "securec.h"
19 #include <cstdlib>
20 #include "string"
21 
22 namespace OHOS {
23 namespace bluetooth {
ConvertToUint8(uint8_t * dst,const size_t size) const24 void RawAddress::ConvertToUint8(uint8_t *dst, const size_t size) const
25 {
26     if (dst != nullptr && address_.length() == BT_ADDRESS_STR_LEN && size >= BT_ADDRESS_BYTE_LEN) {
27         std::vector<std::string> token;
28         std::size_t startPostion = 0;
29         std::size_t colonPosition = address_.find(':', startPostion);
30         while (colonPosition != std::string::npos) {
31             token.push_back(address_.substr(startPostion, colonPosition - startPostion));
32             startPostion = colonPosition + BT_COLON_BYTE_SIZE;
33             colonPosition = address_.find(':', startPostion);
34         }
35         if (startPostion != BT_ADDRESS_STR_LEN) {
36             token.push_back(address_.substr(startPostion));
37         }
38         if (token.size() != BT_ADDRESS_BYTE_LEN) {
39             return;
40         }
41         for (int i = 0; i < BT_ADDRESS_BYTE_LEN; ++i) {
42             char *tmp = nullptr;
43             dst[i] = strtol(token[BT_ADDRESS_BYTE_LEN - 1 - i].c_str(), &tmp, BT_ADDRESS_STR_LEN - 1);
44             if (tmp[0] != '\0') {
45                 return;
46             }
47         }
48     }
49 }
50 
ConvertToString(const uint8_t * src,const size_t size)51 RawAddress RawAddress::ConvertToString(const uint8_t *src, const size_t size)
52 {
53     char token[BT_ADDRESS_STR_LEN + 1] = {0};
54     if (size >= BT_ADDRESS_BYTE_LEN) {
55         (void)sprintf_s(token,
56             BT_ADDRESS_STR_LEN + 1,
57             "%02X:%02X:%02X:%02X:%02X:%02X",
58             src[BT_LAP_HIGH_BYTE],
59             src[BT_LAP_MIDDLE_BYTE],
60             src[BT_LAP_LOW_BYTE],
61             src[BT_UAP_BYTE],
62             src[BT_NAP_HIGH_BYTE],
63             src[BT_NAP_LOW_BYTE]);
64     }
65     return RawAddress(token);
66 }
67 
operator <(const RawAddress & rhs) const68 bool RawAddress::operator<(const RawAddress &rhs) const
69 {
70     return (address_.compare(rhs.address_) < 0);
71 }
72 
operator ==(const RawAddress & rhs) const73 bool RawAddress::operator==(const RawAddress &rhs) const
74 {
75     return (address_.compare(rhs.address_) == 0);
76 }
77 }  // namespace bluetooth
78 }  // namespace OHOS