195489c19Sopenharmony_ci/* 295489c19Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 495489c19Sopenharmony_ci * you may not use this file except in compliance with the License. 595489c19Sopenharmony_ci * You may obtain a copy of the License at 695489c19Sopenharmony_ci * 795489c19Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 895489c19Sopenharmony_ci * 995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and 1395489c19Sopenharmony_ci * limitations under the License. 1495489c19Sopenharmony_ci */ 1595489c19Sopenharmony_ci 1695489c19Sopenharmony_ci#include "raw_address.h" 1795489c19Sopenharmony_ci#include <vector> 1895489c19Sopenharmony_ci#include "securec.h" 1995489c19Sopenharmony_ci#include <cstdlib> 2095489c19Sopenharmony_ci#include "string" 2195489c19Sopenharmony_ci 2295489c19Sopenharmony_cinamespace OHOS { 2395489c19Sopenharmony_cinamespace bluetooth { 2495489c19Sopenharmony_civoid RawAddress::ConvertToUint8(uint8_t *dst, const size_t size) const 2595489c19Sopenharmony_ci{ 2695489c19Sopenharmony_ci if (dst != nullptr && address_.length() == BT_ADDRESS_STR_LEN && size >= BT_ADDRESS_BYTE_LEN) { 2795489c19Sopenharmony_ci std::vector<std::string> token; 2895489c19Sopenharmony_ci std::size_t startPostion = 0; 2995489c19Sopenharmony_ci std::size_t colonPosition = address_.find(':', startPostion); 3095489c19Sopenharmony_ci while (colonPosition != std::string::npos) { 3195489c19Sopenharmony_ci token.push_back(address_.substr(startPostion, colonPosition - startPostion)); 3295489c19Sopenharmony_ci startPostion = colonPosition + BT_COLON_BYTE_SIZE; 3395489c19Sopenharmony_ci colonPosition = address_.find(':', startPostion); 3495489c19Sopenharmony_ci } 3595489c19Sopenharmony_ci if (startPostion != BT_ADDRESS_STR_LEN) { 3695489c19Sopenharmony_ci token.push_back(address_.substr(startPostion)); 3795489c19Sopenharmony_ci } 3895489c19Sopenharmony_ci if (token.size() != BT_ADDRESS_BYTE_LEN) { 3995489c19Sopenharmony_ci return; 4095489c19Sopenharmony_ci } 4195489c19Sopenharmony_ci for (int i = 0; i < BT_ADDRESS_BYTE_LEN; ++i) { 4295489c19Sopenharmony_ci char *tmp = nullptr; 4395489c19Sopenharmony_ci dst[i] = strtol(token[BT_ADDRESS_BYTE_LEN - 1 - i].c_str(), &tmp, BT_ADDRESS_STR_LEN - 1); 4495489c19Sopenharmony_ci if (tmp[0] != '\0') { 4595489c19Sopenharmony_ci return; 4695489c19Sopenharmony_ci } 4795489c19Sopenharmony_ci } 4895489c19Sopenharmony_ci } 4995489c19Sopenharmony_ci} 5095489c19Sopenharmony_ci 5195489c19Sopenharmony_ciRawAddress RawAddress::ConvertToString(const uint8_t *src, const size_t size) 5295489c19Sopenharmony_ci{ 5395489c19Sopenharmony_ci char token[BT_ADDRESS_STR_LEN + 1] = {0}; 5495489c19Sopenharmony_ci if (size >= BT_ADDRESS_BYTE_LEN) { 5595489c19Sopenharmony_ci (void)sprintf_s(token, 5695489c19Sopenharmony_ci BT_ADDRESS_STR_LEN + 1, 5795489c19Sopenharmony_ci "%02X:%02X:%02X:%02X:%02X:%02X", 5895489c19Sopenharmony_ci src[BT_LAP_HIGH_BYTE], 5995489c19Sopenharmony_ci src[BT_LAP_MIDDLE_BYTE], 6095489c19Sopenharmony_ci src[BT_LAP_LOW_BYTE], 6195489c19Sopenharmony_ci src[BT_UAP_BYTE], 6295489c19Sopenharmony_ci src[BT_NAP_HIGH_BYTE], 6395489c19Sopenharmony_ci src[BT_NAP_LOW_BYTE]); 6495489c19Sopenharmony_ci } 6595489c19Sopenharmony_ci return RawAddress(token); 6695489c19Sopenharmony_ci} 6795489c19Sopenharmony_ci 6895489c19Sopenharmony_cibool RawAddress::operator<(const RawAddress &rhs) const 6995489c19Sopenharmony_ci{ 7095489c19Sopenharmony_ci return (address_.compare(rhs.address_) < 0); 7195489c19Sopenharmony_ci} 7295489c19Sopenharmony_ci 7395489c19Sopenharmony_cibool RawAddress::operator==(const RawAddress &rhs) const 7495489c19Sopenharmony_ci{ 7595489c19Sopenharmony_ci return (address_.compare(rhs.address_) == 0); 7695489c19Sopenharmony_ci} 7795489c19Sopenharmony_ci} // namespace bluetooth 7895489c19Sopenharmony_ci} // namespace OHOS