195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2022 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#include "bluetooth_utils.h"
1695489c19Sopenharmony_ci#include <map>
1795489c19Sopenharmony_ci#include <chrono>
1895489c19Sopenharmony_ci#include <random>
1995489c19Sopenharmony_ci#if defined(IOS_PLATFORM)
2095489c19Sopenharmony_ci#include <regex>
2195489c19Sopenharmony_ci#endif
2295489c19Sopenharmony_ci#include "securec.h"
2395489c19Sopenharmony_ci#include "__config"
2495489c19Sopenharmony_ci#include "bluetooth_def.h"
2595489c19Sopenharmony_ci#include "bluetooth_host_proxy.h"
2695489c19Sopenharmony_ci#include "bluetooth_log.h"
2795489c19Sopenharmony_ci#include "iosfwd"
2895489c19Sopenharmony_ci#include "iservice_registry.h"
2995489c19Sopenharmony_ci#include "string"
3095489c19Sopenharmony_ci#include "system_ability_definition.h"
3195489c19Sopenharmony_ci
3295489c19Sopenharmony_ciusing namespace std;
3395489c19Sopenharmony_ci
3495489c19Sopenharmony_cinamespace OHOS {
3595489c19Sopenharmony_cinamespace Bluetooth {
3695489c19Sopenharmony_ciconstexpr int startPos = 6;
3795489c19Sopenharmony_ciconstexpr int endPos = 13;
3895489c19Sopenharmony_ci#if defined(IOS_PLATFORM)
3995489c19Sopenharmony_ciconstexpr int START_POS_IOS_PLATFORM = 9;
4095489c19Sopenharmony_ciconstexpr int END_POS_IOS_PLATFORM = 22;
4195489c19Sopenharmony_ci#endif
4295489c19Sopenharmony_ciconstexpr int RANDOM_ADDR_ARRAY_SIZE = 4;
4395489c19Sopenharmony_ciconstexpr int RANDOM_ADDR_MAC_BIT_SIZE = 12;
4495489c19Sopenharmony_ciconstexpr int RANDOM_ADDR_FIRST_BIT = 1;
4595489c19Sopenharmony_ciconstexpr int RANDOM_ADDR_LAST_BIT = 11;
4695489c19Sopenharmony_ciconstexpr int RANDOM_ADDR_SPLIT_SIZE = 2;
4795489c19Sopenharmony_ciconstexpr int HEX_BASE = 16;
4895489c19Sopenharmony_ciconstexpr int OCT_BASE = 8;
4995489c19Sopenharmony_ci
5095489c19Sopenharmony_cistd::string GetEncryptAddr(std::string addr)
5195489c19Sopenharmony_ci{
5295489c19Sopenharmony_ci#if defined(IOS_PLATFORM)
5395489c19Sopenharmony_ci    const std::regex deviceIdRegex("^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$");
5495489c19Sopenharmony_ci    if (!regex_match(addr, deviceIdRegex)) {
5595489c19Sopenharmony_ci        return std::string("");
5695489c19Sopenharmony_ci    }
5795489c19Sopenharmony_ci    std::string tmp = "********-****-****-****-************";
5895489c19Sopenharmony_ci    std::string out = addr;
5995489c19Sopenharmony_ci    for (int i = START_POS_IOS_PLATFORM; i <= END_POS_IOS_PLATFORM; i++) {
6095489c19Sopenharmony_ci        out[i] = tmp[i];
6195489c19Sopenharmony_ci    }
6295489c19Sopenharmony_ci    return out;
6395489c19Sopenharmony_ci#else
6495489c19Sopenharmony_ci    if (addr.empty() || addr.length() != ADDRESS_LENGTH) {
6595489c19Sopenharmony_ci        HILOGD("addr is invalid.");
6695489c19Sopenharmony_ci        return std::string("");
6795489c19Sopenharmony_ci    }
6895489c19Sopenharmony_ci    std::string tmp = "**:**:**:**:**:**";
6995489c19Sopenharmony_ci    std::string out = addr;
7095489c19Sopenharmony_ci    // 00:01:**:**:**:05
7195489c19Sopenharmony_ci    for (int i = startPos; i <= endPos; i++) {
7295489c19Sopenharmony_ci        out[i] = tmp[i];
7395489c19Sopenharmony_ci    }
7495489c19Sopenharmony_ci    return out;
7595489c19Sopenharmony_ci#endif
7695489c19Sopenharmony_ci}
7795489c19Sopenharmony_ci
7895489c19Sopenharmony_cistd::string GetBtStateName(int state)
7995489c19Sopenharmony_ci{
8095489c19Sopenharmony_ci    switch (state) {
8195489c19Sopenharmony_ci        case BTStateID::STATE_TURNING_ON:
8295489c19Sopenharmony_ci            return "STATE_TURNING_ON(0)";
8395489c19Sopenharmony_ci        case BTStateID::STATE_TURN_ON:
8495489c19Sopenharmony_ci            return "STATE_TURN_ON(1)";
8595489c19Sopenharmony_ci        case BTStateID::STATE_TURNING_OFF:
8695489c19Sopenharmony_ci            return "STATE_TURNING_OFF(2)";
8795489c19Sopenharmony_ci        case BTStateID::STATE_TURN_OFF:
8895489c19Sopenharmony_ci            return "STATE_TURN_OFF(3)";
8995489c19Sopenharmony_ci        default:
9095489c19Sopenharmony_ci            return "Unknown";
9195489c19Sopenharmony_ci    }
9295489c19Sopenharmony_ci}
9395489c19Sopenharmony_ci
9495489c19Sopenharmony_cistd::string GetBtTransportName(int transport)
9595489c19Sopenharmony_ci{
9695489c19Sopenharmony_ci    switch (transport) {
9795489c19Sopenharmony_ci        case BTTransport::ADAPTER_BREDR:
9895489c19Sopenharmony_ci            return "ADAPTER_BREDR(0)";
9995489c19Sopenharmony_ci        case BTTransport::ADAPTER_BLE:
10095489c19Sopenharmony_ci            return "ADAPTER_BLE(1)";
10195489c19Sopenharmony_ci        default:
10295489c19Sopenharmony_ci            return "Unknown";
10395489c19Sopenharmony_ci    }
10495489c19Sopenharmony_ci}
10595489c19Sopenharmony_ci
10695489c19Sopenharmony_cistd::string GetProfileConnStateName(int state)
10795489c19Sopenharmony_ci{
10895489c19Sopenharmony_ci    switch (state) {
10995489c19Sopenharmony_ci        case static_cast<int>(BTConnectState::CONNECTING):
11095489c19Sopenharmony_ci            return "CONNECTING(0)";
11195489c19Sopenharmony_ci        case static_cast<int>(BTConnectState::CONNECTED):
11295489c19Sopenharmony_ci            return "CONNECTED(1)";
11395489c19Sopenharmony_ci        case static_cast<int>(BTConnectState::DISCONNECTING):
11495489c19Sopenharmony_ci            return "DISCONNECTING(2)";
11595489c19Sopenharmony_ci        case static_cast<int>(BTConnectState::DISCONNECTED):
11695489c19Sopenharmony_ci            return "DISCONNECTED(3)";
11795489c19Sopenharmony_ci        default:
11895489c19Sopenharmony_ci            return "Unknown";
11995489c19Sopenharmony_ci    }
12095489c19Sopenharmony_ci}
12195489c19Sopenharmony_ci
12295489c19Sopenharmony_cistd::string GetUpdateOutputStackActionName(int action)
12395489c19Sopenharmony_ci{
12495489c19Sopenharmony_ci    switch (action) {
12595489c19Sopenharmony_ci        case static_cast<int>(UpdateOutputStackAction::ACTION_WEAR):
12695489c19Sopenharmony_ci            return "WEAR(0)";
12795489c19Sopenharmony_ci        case static_cast<int>(UpdateOutputStackAction::ACTION_UNWEAR):
12895489c19Sopenharmony_ci            return "UNWEAR(1)";
12995489c19Sopenharmony_ci        case static_cast<int>(UpdateOutputStackAction::ACTION_ENABLE_FROM_REMOTE):
13095489c19Sopenharmony_ci            return "ENABLE_FROM_REMOTE(2)";
13195489c19Sopenharmony_ci        case static_cast<int>(UpdateOutputStackAction::ACTION_DISABLE_FROM_REMOTE):
13295489c19Sopenharmony_ci            return "DISABLE_FROM_REMOTE(3)";
13395489c19Sopenharmony_ci        case static_cast<int>(UpdateOutputStackAction::ACTION_ENABLE_WEAR_DETECTION):
13495489c19Sopenharmony_ci            return "ENABLE_WEAR_DETECTION(4)";
13595489c19Sopenharmony_ci        case static_cast<int>(UpdateOutputStackAction::ACTION_DISABLE_WEAR_DETECTION):
13695489c19Sopenharmony_ci            return "DISABLE_WEAR_DETECTION(5)";
13795489c19Sopenharmony_ci        case static_cast<int>(UpdateOutputStackAction::ACTION_USER_OPERATION):
13895489c19Sopenharmony_ci            return "USER_OPERATION(6)";
13995489c19Sopenharmony_ci        case static_cast<int>(UpdateOutputStackAction::ACTION_STOP_VIRTUAL_CALL):
14095489c19Sopenharmony_ci            return "STOP_VIRTUAL_CALL(7)";
14195489c19Sopenharmony_ci        default:
14295489c19Sopenharmony_ci            return "Unknown";
14395489c19Sopenharmony_ci    }
14495489c19Sopenharmony_ci}
14595489c19Sopenharmony_ci
14695489c19Sopenharmony_cistatic std::map<int32_t, std::string> BtErrCodeMap {
14795489c19Sopenharmony_ci    { BtErrCode::BT_NO_ERROR, "BT_NO_ERROR" },
14895489c19Sopenharmony_ci    { BtErrCode::BT_ERR_PERMISSION_FAILED, "BT_ERR_PERMISSION_FAILED" },
14995489c19Sopenharmony_ci    { BtErrCode::BT_ERR_SYSTEM_PERMISSION_FAILED, "BT_ERR_SYSTEM_PERMISSION_FAILED" },
15095489c19Sopenharmony_ci    { BtErrCode::BT_ERR_INVALID_PARAM, "BT_ERR_INVALID_PARAM" },
15195489c19Sopenharmony_ci    { BtErrCode::BT_ERR_API_NOT_SUPPORT, "BT_ERR_API_NOT_SUPPORT" },
15295489c19Sopenharmony_ci    { BtErrCode::BT_ERR_SERVICE_DISCONNECTED, "BT_ERR_SERVICE_DISCONNECTED" },
15395489c19Sopenharmony_ci    { BtErrCode::BT_ERR_UNBONDED_DEVICE, "BT_ERR_UNBONDED_DEVICE" },
15495489c19Sopenharmony_ci    { BtErrCode::BT_ERR_INVALID_STATE, "BT_ERR_INVALID_STATE" },
15595489c19Sopenharmony_ci    { BtErrCode::BT_ERR_PROFILE_DISABLED, "BT_ERR_PROFILE_DISABLED" },
15695489c19Sopenharmony_ci    { BtErrCode::BT_ERR_DEVICE_DISCONNECTED, "BT_ERR_DEVICE_DISCONNECTED" },
15795489c19Sopenharmony_ci    { BtErrCode::BT_ERR_MAX_CONNECTION, "BT_ERR_MAX_CONNECTION" },
15895489c19Sopenharmony_ci    { BtErrCode::BT_ERR_INTERNAL_ERROR, "BT_ERR_INTERNAL_ERROR" },
15995489c19Sopenharmony_ci    { BtErrCode::BT_ERR_IPC_TRANS_FAILED, "BT_ERR_IPC_TRANS_FAILED" },
16095489c19Sopenharmony_ci    { BtErrCode::BT_ERR_GATT_READ_NOT_PERMITTED, "BT_ERR_GATT_READ_NOT_PERMITTED" },
16195489c19Sopenharmony_ci    { BtErrCode::BT_ERR_GATT_WRITE_NOT_PERMITTED, "BT_ERR_GATT_WRITE_NOT_PERMITTED" },
16295489c19Sopenharmony_ci    { BtErrCode::BT_ERR_GATT_MAX_SERVER, "BT_ERR_GATT_MAX_SERVER" },
16395489c19Sopenharmony_ci    { BtErrCode::BT_ERR_SPP_SERVER_STATE, "BT_ERR_SPP_SERVER_STATE" },
16495489c19Sopenharmony_ci    { BtErrCode::BT_ERR_SPP_BUSY, "BT_ERR_SPP_BUSY" },
16595489c19Sopenharmony_ci    { BtErrCode::BT_ERR_SPP_DEVICE_NOT_FOUND, "BT_ERR_SPP_DEVICE_NOT_FOUND" },
16695489c19Sopenharmony_ci    { BtErrCode::BT_ERR_SPP_IO, "BT_ERR_SPP_IO" },
16795489c19Sopenharmony_ci    { BtErrCode::BT_ERR_NO_ACTIVE_HFP_DEVICE, "Active hfp device is not exist." },
16895489c19Sopenharmony_ci    { BtErrCode::BT_ERR_NULL_HFP_STATE_MACHINE, "Hfp state machine is not null." },
16995489c19Sopenharmony_ci    { BtErrCode::BT_ERR_HFP_NOT_CONNECT, "Hfp is not connected." },
17095489c19Sopenharmony_ci    { BtErrCode::BT_ERR_SCO_HAS_BEEN_CONNECTED, "Sco has been connected." },
17195489c19Sopenharmony_ci    { BtErrCode::BT_ERR_VR_HAS_BEEN_STARTED, "Voice recognition has been started." },
17295489c19Sopenharmony_ci    { BtErrCode::BT_ERR_AUDIO_NOT_IDLE, "Audio is not idle." },
17395489c19Sopenharmony_ci    { BtErrCode::BT_ERR_VIRTUAL_CALL_NOT_STARTED, "Virtual call is not started." },
17495489c19Sopenharmony_ci    { BtErrCode::BT_ERR_DISCONNECT_SCO_FAILED, "Disconnect sco failed." },
17595489c19Sopenharmony_ci};
17695489c19Sopenharmony_ci
17795489c19Sopenharmony_cistd::string GetErrorCode(int32_t errCode)
17895489c19Sopenharmony_ci{
17995489c19Sopenharmony_ci    std::string errlog = "unknown error code: ";
18095489c19Sopenharmony_ci    auto iter = BtErrCodeMap.find(errCode);
18195489c19Sopenharmony_ci    if (iter != BtErrCodeMap.end()) {
18295489c19Sopenharmony_ci        errlog = iter->second;
18395489c19Sopenharmony_ci    }
18495489c19Sopenharmony_ci    errlog.append("(").append(std::to_string(errCode)).append(")");
18595489c19Sopenharmony_ci    return errlog;
18695489c19Sopenharmony_ci}
18795489c19Sopenharmony_ci
18895489c19Sopenharmony_civoid ToUpper(char* arr)
18995489c19Sopenharmony_ci{
19095489c19Sopenharmony_ci    for (size_t i = 0; i < strlen(arr); ++i) {
19195489c19Sopenharmony_ci        if (arr[i] >= 'a' && arr[i] <= 'z') {
19295489c19Sopenharmony_ci            arr[i] = toupper(arr[i]);
19395489c19Sopenharmony_ci        }
19495489c19Sopenharmony_ci    }
19595489c19Sopenharmony_ci}
19695489c19Sopenharmony_ci
19795489c19Sopenharmony_cistd::string GenerateRandomMacAddress()
19895489c19Sopenharmony_ci{
19995489c19Sopenharmony_ci    std::string randomMac = "";
20095489c19Sopenharmony_ci    char strMacTmp[RANDOM_ADDR_ARRAY_SIZE] = {0};
20195489c19Sopenharmony_ci    std::mt19937_64 gen(std::chrono::high_resolution_clock::now().time_since_epoch().count());
20295489c19Sopenharmony_ci    for (int i = 0; i < RANDOM_ADDR_MAC_BIT_SIZE; i++) {
20395489c19Sopenharmony_ci        int ret = -1;
20495489c19Sopenharmony_ci        if (i != RANDOM_ADDR_FIRST_BIT) {
20595489c19Sopenharmony_ci            std::uniform_int_distribution<> distribution(0, HEX_BASE - 1);
20695489c19Sopenharmony_ci            ret = sprintf_s(strMacTmp, RANDOM_ADDR_ARRAY_SIZE, "%x", distribution(gen));
20795489c19Sopenharmony_ci        } else {
20895489c19Sopenharmony_ci            std::uniform_int_distribution<> distribution(0, OCT_BASE - 1);
20995489c19Sopenharmony_ci            ret = sprintf_s(strMacTmp, RANDOM_ADDR_ARRAY_SIZE, "%x", RANDOM_ADDR_SPLIT_SIZE * distribution(gen));
21095489c19Sopenharmony_ci        }
21195489c19Sopenharmony_ci        if (ret == -1) {
21295489c19Sopenharmony_ci            HILOGE("GenerateRandomMacAddress failed, sprintf_s return -1!");
21395489c19Sopenharmony_ci        }
21495489c19Sopenharmony_ci        ToUpper(strMacTmp);
21595489c19Sopenharmony_ci        randomMac += strMacTmp;
21695489c19Sopenharmony_ci        if ((i % RANDOM_ADDR_SPLIT_SIZE != 0 && (i != RANDOM_ADDR_LAST_BIT))) {
21795489c19Sopenharmony_ci            randomMac.append(":");
21895489c19Sopenharmony_ci        }
21995489c19Sopenharmony_ci    }
22095489c19Sopenharmony_ci    return randomMac;
22195489c19Sopenharmony_ci}
22295489c19Sopenharmony_ci
22395489c19Sopenharmony_cibool CheckConnectionStrategyInvalid(int32_t strategy)
22495489c19Sopenharmony_ci{
22595489c19Sopenharmony_ci    if (strategy == static_cast<int32_t>(BTStrategyType::CONNECTION_ALLOWED) ||
22695489c19Sopenharmony_ci        strategy == static_cast<int32_t>(BTStrategyType::CONNECTION_FORBIDDEN)) {
22795489c19Sopenharmony_ci        return true;
22895489c19Sopenharmony_ci    }
22995489c19Sopenharmony_ci    return false;
23095489c19Sopenharmony_ci}
23195489c19Sopenharmony_ci
23295489c19Sopenharmony_cibool CheckAccessAuthorizationInvalid(int32_t accessAuthorization)
23395489c19Sopenharmony_ci{
23495489c19Sopenharmony_ci    if (accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_UNKNOWN) ||
23595489c19Sopenharmony_ci        accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_ALLOWED) ||
23695489c19Sopenharmony_ci        accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_FORBIDDEN)) {
23795489c19Sopenharmony_ci        return true;
23895489c19Sopenharmony_ci    }
23995489c19Sopenharmony_ci    return false;
24095489c19Sopenharmony_ci}
24195489c19Sopenharmony_ci
24295489c19Sopenharmony_ci}  // namespace Bluetooth
24395489c19Sopenharmony_ci}  // namespace OHOS