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