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#include "bluetooth_device_class.h"
1695489c19Sopenharmony_ci
1795489c19Sopenharmony_ci#include "bluetooth_device.h"
1895489c19Sopenharmony_ci#include "bluetooth_log.h"
1995489c19Sopenharmony_ci
2095489c19Sopenharmony_cinamespace OHOS {
2195489c19Sopenharmony_cinamespace Bluetooth {
2295489c19Sopenharmony_ciBluetoothDeviceClass::BluetoothDeviceClass() : class_(0)
2395489c19Sopenharmony_ci{}
2495489c19Sopenharmony_ci
2595489c19Sopenharmony_ciBluetoothDeviceClass::BluetoothDeviceClass(int deviceClass) : class_(deviceClass)
2695489c19Sopenharmony_ci{}
2795489c19Sopenharmony_ci
2895489c19Sopenharmony_ciBluetoothDeviceClass::~BluetoothDeviceClass()
2995489c19Sopenharmony_ci{}
3095489c19Sopenharmony_ci
3195489c19Sopenharmony_ciint BluetoothDeviceClass::GetMajorClass() const
3295489c19Sopenharmony_ci{
3395489c19Sopenharmony_ci    return (class_ & BluetoothDevice::MAJOR_BITMASK);
3495489c19Sopenharmony_ci}
3595489c19Sopenharmony_ci
3695489c19Sopenharmony_ciint BluetoothDeviceClass::GetMajorMinorClass() const
3795489c19Sopenharmony_ci{
3895489c19Sopenharmony_ci    int res = (class_ & BluetoothDevice::DEVICE_BITMASK);
3995489c19Sopenharmony_ci    HILOGD("MajorMinorClass: 0x%{public}X", res);
4095489c19Sopenharmony_ci    return res;
4195489c19Sopenharmony_ci}
4295489c19Sopenharmony_ci
4395489c19Sopenharmony_ciint BluetoothDeviceClass::GetClassOfDevice() const
4495489c19Sopenharmony_ci{
4595489c19Sopenharmony_ci    return class_;
4695489c19Sopenharmony_ci}
4795489c19Sopenharmony_ci
4895489c19Sopenharmony_cibool BluetoothDeviceClass::IsProfileSupported(int profileId) const
4995489c19Sopenharmony_ci{
5095489c19Sopenharmony_ci    if (profileId == BluetoothDevice::PROFILE_A2DP) {
5195489c19Sopenharmony_ci        return IsA2dpSupported();
5295489c19Sopenharmony_ci    } else if (profileId == BluetoothDevice::PROFILE_A2DP_SINK) {
5395489c19Sopenharmony_ci        return IsA2dpSinkSupported();
5495489c19Sopenharmony_ci    } else if (profileId == BluetoothDevice::PROFILE_HEADSET) {
5595489c19Sopenharmony_ci        return IsHeadSetSupported();
5695489c19Sopenharmony_ci    } else if (profileId == BluetoothDevice::PROFILE_OPP) {
5795489c19Sopenharmony_ci        return IsOppSupported();
5895489c19Sopenharmony_ci    } else if (profileId == BluetoothDevice::PROFILE_HID) {
5995489c19Sopenharmony_ci        return (GetMajorMinorClass() & BluetoothDevice::MAJOR_PERIPHERAL) ==
6095489c19Sopenharmony_ci               BluetoothDevice::MAJOR_PERIPHERAL;
6195489c19Sopenharmony_ci    } else if (profileId == BluetoothDevice::PROFILE_PANU ||
6295489c19Sopenharmony_ci               profileId == BluetoothDevice::PROFILE_NAP) {
6395489c19Sopenharmony_ci        if (IsServiceSupported(BluetoothDevice::SERVICE_NETWORKING)) {
6495489c19Sopenharmony_ci            return true;
6595489c19Sopenharmony_ci        }
6695489c19Sopenharmony_ci        return (GetMajorMinorClass() & BluetoothDevice::MAJOR_NETWORKING) ==
6795489c19Sopenharmony_ci               BluetoothDevice::MAJOR_NETWORKING;
6895489c19Sopenharmony_ci    } else {
6995489c19Sopenharmony_ci        return false;
7095489c19Sopenharmony_ci    }
7195489c19Sopenharmony_ci}
7295489c19Sopenharmony_ci
7395489c19Sopenharmony_cibool BluetoothDeviceClass::IsA2dpSupported() const
7495489c19Sopenharmony_ci{
7595489c19Sopenharmony_ci    if (IsServiceSupported(BluetoothDevice::SERVICE_RENDER)) {
7695489c19Sopenharmony_ci        HILOGI("service supported.");
7795489c19Sopenharmony_ci        return true;
7895489c19Sopenharmony_ci    }
7995489c19Sopenharmony_ci    switch (GetMajorMinorClass()) {
8095489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_HIFI_AUDIO:
8195489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_HEADPHONES:
8295489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_LOUDSPEAKER:
8395489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_CAR_AUDIO:
8495489c19Sopenharmony_ci            return true;
8595489c19Sopenharmony_ci        default:
8695489c19Sopenharmony_ci            return false;
8795489c19Sopenharmony_ci    }
8895489c19Sopenharmony_ci}
8995489c19Sopenharmony_ci
9095489c19Sopenharmony_cibool BluetoothDeviceClass::IsA2dpSinkSupported() const
9195489c19Sopenharmony_ci{
9295489c19Sopenharmony_ci    if (IsServiceSupported(BluetoothDevice::SERVICE_CAPTURE)) {
9395489c19Sopenharmony_ci        HILOGI("service supported.");
9495489c19Sopenharmony_ci        return true;
9595489c19Sopenharmony_ci    }
9695489c19Sopenharmony_ci    switch (GetMajorMinorClass()) {
9795489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_HIFI_AUDIO:
9895489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_SET_TOP_BOX:
9995489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_VCR:
10095489c19Sopenharmony_ci            return true;
10195489c19Sopenharmony_ci        default:
10295489c19Sopenharmony_ci            return false;
10395489c19Sopenharmony_ci    }
10495489c19Sopenharmony_ci}
10595489c19Sopenharmony_ci
10695489c19Sopenharmony_cibool BluetoothDeviceClass::IsHeadSetSupported() const
10795489c19Sopenharmony_ci{
10895489c19Sopenharmony_ci    if (IsServiceSupported(BluetoothDevice::SERVICE_RENDER)) {
10995489c19Sopenharmony_ci        HILOGI("service supported.");
11095489c19Sopenharmony_ci        return true;
11195489c19Sopenharmony_ci    }
11295489c19Sopenharmony_ci    switch (GetMajorMinorClass()) {
11395489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_HANDSFREE:
11495489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_WEARABLE_HEADSET:
11595489c19Sopenharmony_ci        case BluetoothDevice::AUDIO_VIDEO_CAR_AUDIO:
11695489c19Sopenharmony_ci            return true;
11795489c19Sopenharmony_ci        default:
11895489c19Sopenharmony_ci            return false;
11995489c19Sopenharmony_ci    }
12095489c19Sopenharmony_ci}
12195489c19Sopenharmony_ci
12295489c19Sopenharmony_cibool BluetoothDeviceClass::IsOppSupported() const
12395489c19Sopenharmony_ci{
12495489c19Sopenharmony_ci    if (IsServiceSupported(BluetoothDevice::SERVICE_OBJECT_TRANSFER)) {
12595489c19Sopenharmony_ci        return true;
12695489c19Sopenharmony_ci    }
12795489c19Sopenharmony_ci
12895489c19Sopenharmony_ci    switch (GetMajorMinorClass()) {
12995489c19Sopenharmony_ci        case BluetoothDevice::COMPUTER_UNCATEGORIZED:
13095489c19Sopenharmony_ci        case BluetoothDevice::COMPUTER_DESKTOP:
13195489c19Sopenharmony_ci        case BluetoothDevice::COMPUTER_SERVER:
13295489c19Sopenharmony_ci        case BluetoothDevice::COMPUTER_LAPTOP:
13395489c19Sopenharmony_ci        case BluetoothDevice::COMPUTER_HANDHELD_PC_PDA:
13495489c19Sopenharmony_ci        case BluetoothDevice::COMPUTER_PALM_SIZE_PC_PDA:
13595489c19Sopenharmony_ci        case BluetoothDevice::COMPUTER_WEARABLE:
13695489c19Sopenharmony_ci        case BluetoothDevice::PHONE_UNCATEGORIZED:
13795489c19Sopenharmony_ci        case BluetoothDevice::PHONE_CELLULAR:
13895489c19Sopenharmony_ci        case BluetoothDevice::PHONE_CORDLESS:
13995489c19Sopenharmony_ci        case BluetoothDevice::PHONE_SMART:
14095489c19Sopenharmony_ci        case BluetoothDevice::PHONE_MODEM_OR_GATEWAY:
14195489c19Sopenharmony_ci        case BluetoothDevice::PHONE_ISDN:
14295489c19Sopenharmony_ci            return true;
14395489c19Sopenharmony_ci        default:
14495489c19Sopenharmony_ci            return false;
14595489c19Sopenharmony_ci    }
14695489c19Sopenharmony_ci}
14795489c19Sopenharmony_ci
14895489c19Sopenharmony_cibool BluetoothDeviceClass::IsServiceSupported(int service) const
14995489c19Sopenharmony_ci{
15095489c19Sopenharmony_ci    return ((class_ & BluetoothDevice::SERVICE_BITMASK & service) != 0);
15195489c19Sopenharmony_ci}
15295489c19Sopenharmony_ci}  // namespace Bluetooth
15395489c19Sopenharmony_ci}  // namespace OHOS