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 #include "bluetooth_device_class.h"
16 
17 #include "bluetooth_device.h"
18 #include "bluetooth_log.h"
19 
20 namespace OHOS {
21 namespace Bluetooth {
BluetoothDeviceClass()22 BluetoothDeviceClass::BluetoothDeviceClass() : class_(0)
23 {}
24 
BluetoothDeviceClass(int deviceClass)25 BluetoothDeviceClass::BluetoothDeviceClass(int deviceClass) : class_(deviceClass)
26 {}
27 
~BluetoothDeviceClass()28 BluetoothDeviceClass::~BluetoothDeviceClass()
29 {}
30 
GetMajorClass() const31 int BluetoothDeviceClass::GetMajorClass() const
32 {
33     return (class_ & BluetoothDevice::MAJOR_BITMASK);
34 }
35 
GetMajorMinorClass() const36 int BluetoothDeviceClass::GetMajorMinorClass() const
37 {
38     int res = (class_ & BluetoothDevice::DEVICE_BITMASK);
39     HILOGD("MajorMinorClass: 0x%{public}X", res);
40     return res;
41 }
42 
GetClassOfDevice() const43 int BluetoothDeviceClass::GetClassOfDevice() const
44 {
45     return class_;
46 }
47 
IsProfileSupported(int profileId) const48 bool BluetoothDeviceClass::IsProfileSupported(int profileId) const
49 {
50     if (profileId == BluetoothDevice::PROFILE_A2DP) {
51         return IsA2dpSupported();
52     } else if (profileId == BluetoothDevice::PROFILE_A2DP_SINK) {
53         return IsA2dpSinkSupported();
54     } else if (profileId == BluetoothDevice::PROFILE_HEADSET) {
55         return IsHeadSetSupported();
56     } else if (profileId == BluetoothDevice::PROFILE_OPP) {
57         return IsOppSupported();
58     } else if (profileId == BluetoothDevice::PROFILE_HID) {
59         return (GetMajorMinorClass() & BluetoothDevice::MAJOR_PERIPHERAL) ==
60                BluetoothDevice::MAJOR_PERIPHERAL;
61     } else if (profileId == BluetoothDevice::PROFILE_PANU ||
62                profileId == BluetoothDevice::PROFILE_NAP) {
63         if (IsServiceSupported(BluetoothDevice::SERVICE_NETWORKING)) {
64             return true;
65         }
66         return (GetMajorMinorClass() & BluetoothDevice::MAJOR_NETWORKING) ==
67                BluetoothDevice::MAJOR_NETWORKING;
68     } else {
69         return false;
70     }
71 }
72 
IsA2dpSupported() const73 bool BluetoothDeviceClass::IsA2dpSupported() const
74 {
75     if (IsServiceSupported(BluetoothDevice::SERVICE_RENDER)) {
76         HILOGI("service supported.");
77         return true;
78     }
79     switch (GetMajorMinorClass()) {
80         case BluetoothDevice::AUDIO_VIDEO_HIFI_AUDIO:
81         case BluetoothDevice::AUDIO_VIDEO_HEADPHONES:
82         case BluetoothDevice::AUDIO_VIDEO_LOUDSPEAKER:
83         case BluetoothDevice::AUDIO_VIDEO_CAR_AUDIO:
84             return true;
85         default:
86             return false;
87     }
88 }
89 
IsA2dpSinkSupported() const90 bool BluetoothDeviceClass::IsA2dpSinkSupported() const
91 {
92     if (IsServiceSupported(BluetoothDevice::SERVICE_CAPTURE)) {
93         HILOGI("service supported.");
94         return true;
95     }
96     switch (GetMajorMinorClass()) {
97         case BluetoothDevice::AUDIO_VIDEO_HIFI_AUDIO:
98         case BluetoothDevice::AUDIO_VIDEO_SET_TOP_BOX:
99         case BluetoothDevice::AUDIO_VIDEO_VCR:
100             return true;
101         default:
102             return false;
103     }
104 }
105 
IsHeadSetSupported() const106 bool BluetoothDeviceClass::IsHeadSetSupported() const
107 {
108     if (IsServiceSupported(BluetoothDevice::SERVICE_RENDER)) {
109         HILOGI("service supported.");
110         return true;
111     }
112     switch (GetMajorMinorClass()) {
113         case BluetoothDevice::AUDIO_VIDEO_HANDSFREE:
114         case BluetoothDevice::AUDIO_VIDEO_WEARABLE_HEADSET:
115         case BluetoothDevice::AUDIO_VIDEO_CAR_AUDIO:
116             return true;
117         default:
118             return false;
119     }
120 }
121 
IsOppSupported() const122 bool BluetoothDeviceClass::IsOppSupported() const
123 {
124     if (IsServiceSupported(BluetoothDevice::SERVICE_OBJECT_TRANSFER)) {
125         return true;
126     }
127 
128     switch (GetMajorMinorClass()) {
129         case BluetoothDevice::COMPUTER_UNCATEGORIZED:
130         case BluetoothDevice::COMPUTER_DESKTOP:
131         case BluetoothDevice::COMPUTER_SERVER:
132         case BluetoothDevice::COMPUTER_LAPTOP:
133         case BluetoothDevice::COMPUTER_HANDHELD_PC_PDA:
134         case BluetoothDevice::COMPUTER_PALM_SIZE_PC_PDA:
135         case BluetoothDevice::COMPUTER_WEARABLE:
136         case BluetoothDevice::PHONE_UNCATEGORIZED:
137         case BluetoothDevice::PHONE_CELLULAR:
138         case BluetoothDevice::PHONE_CORDLESS:
139         case BluetoothDevice::PHONE_SMART:
140         case BluetoothDevice::PHONE_MODEM_OR_GATEWAY:
141         case BluetoothDevice::PHONE_ISDN:
142             return true;
143         default:
144             return false;
145     }
146 }
147 
IsServiceSupported(int service) const148 bool BluetoothDeviceClass::IsServiceSupported(int service) const
149 {
150     return ((class_ & BluetoothDevice::SERVICE_BITMASK & service) != 0);
151 }
152 }  // namespace Bluetooth
153 }  // namespace OHOS