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 
16 #ifndef BLE_FEATURE_H
17 #define BLE_FEATURE_H
18 
19 #include <vector>
20 
21 #include "btm.h"
22 #include "gap_le_if.h"
23 
24 /*
25  * @brief The bluetooth system.
26  */
27 namespace OHOS {
28 namespace bluetooth {
29 /*
30  * @brief Ble feature.
31  */
32 class BleFeature {
33 public:
34     /**
35      * @brief Get ble feature instance.
36      *
37      * @return @c feature instance.
38      */
GetInstance()39     static BleFeature &GetInstance()
40     {
41         static BleFeature instance;
42         return instance;
43     }
44 
45     /**
46      * @brief Get ble feature extended scan supported.
47      *
48      * @return @c true:supported; false:not supported.
49      */
IsExtendedScanSupported()50     bool IsExtendedScanSupported() const
51     {
52         return IsFeatureSupported(EXTENDED_SCANNER_FILTER_POLICIES);
53     }
54 
55     /**
56      * @brief Get ble feature 2M phy supported.
57      *
58      * @return @c true:supported; false:not supported.
59      */
IsLe2MPhySupported()60     bool IsLe2MPhySupported() const
61     {
62         return IsFeatureSupported(LE_2M_PHY);
63     }
64 
65     /**
66      * @brief Get ble feature coded phy supported.
67      *
68      * @return @c true:supported; false:not supported.
69      */
IsLeCodedPhySupported()70     bool IsLeCodedPhySupported() const
71     {
72         return IsFeatureSupported(LE_CODED_PHY);
73     }
74 
75     /**
76      * @brief Get ble feature extended advertising supported.
77      *
78      * @return @c true:supported; false:not supported.
79      */
IsLeExtendedAdvertisingSupported()80     bool IsLeExtendedAdvertisingSupported() const
81     {
82         return IsFeatureSupported(LE_EXTENDED_ADVERTISING);
83     }
84 
85     /**
86      * @brief Get ble feature periodic advertising supported.
87      *
88      * @return @c true:supported; false:not supported.
89      */
IsLePeriodicAdvertisingSupported()90     bool IsLePeriodicAdvertisingSupported() const
91     {
92         return IsFeatureSupported(LE_PERIODIC_ADVERTISING);
93     }
94 
95     /**
96      * @brief Get ble feature privacy supported.
97      *
98      * @return @c true:supported; false:not supported.
99      */
IsPrivacySupported()100     bool IsPrivacySupported() const
101     {
102         return IsFeatureSupported(LL_PRIVACY);
103     }
104 
105     /**
106      * @brief Get ble feature maximum advertising data length.
107      *
108      * @return @c data length.
109      */
GetBleMaximumAdvertisingDataLength()110     int GetBleMaximumAdvertisingDataLength() const
111     {
112         if (IsFeatureSupported(LE_DATA_PACKET_LENGTH_EXTENSION)) {
113             uint16_t length = BLE_LEGACY_ADV_DATA_LEN_MAX;
114             int ret = GAPIF_LeExAdvGetMaxDataLen(&length);
115             if (ret == BT_SUCCESS) {
116                 return length;
117             }
118             return BLE_LEGACY_ADV_DATA_LEN_MAX;
119         } else {
120             return BLE_LEGACY_ADV_DATA_LEN_MAX;
121         }
122     }
123 
124     /**
125      * @brief Get ble feature extended advertising max handle number.
126      *
127      * @return @c handle number.
128      */
GetBleExAdvGetMaxHandleNum()129     static uint8_t GetBleExAdvGetMaxHandleNum()
130     {
131         uint8_t num = BLE_INVALID_ADVERTISING_HANDLE;
132         int ret = GAPIF_LeExAdvGetMaxHandleNum(&num);
133         if (ret == BT_SUCCESS) {
134             return num;
135         }
136         return num;
137     }
138 
139     /**
140      * @brief Get ble supported feature.
141      *
142      * @return @c true:supported; false:not supported.
143      */
IsFeatureSupported(FEATURE_SUPPORTED feature)144     static bool IsFeatureSupported(FEATURE_SUPPORTED feature)
145     {
146         switch (feature) {
147             case LE_2M_PHY:
148                 return BTM_IsControllerSupportLe2MPhy();
149                 break;
150             case LE_CODED_PHY:
151                 return BTM_IsControllerSupportLeCodedPhy();
152                 break;
153             case LE_EXTENDED_ADVERTISING:
154             case EXTENDED_SCANNER_FILTER_POLICIES:
155                 return BTM_IsControllerSupportLeExtendedAdvertising();
156                 break;
157             case LE_PERIODIC_ADVERTISING:
158                 return BTM_IsControllerSupportLePeriodicAdvertising();
159                 break;
160             case LL_PRIVACY:
161                 return BTM_IsControllerSupportLlPrivacy();
162                 break;
163             case LE_DATA_PACKET_LENGTH_EXTENSION:
164                 return BTM_IsControllerSupportLeDataPacketLengthExtension();
165                 break;
166             default:
167                 break;
168         }
169         return false;
170     }
171 
172 private:
173     /**
174      * @brief Constructor.
175      */
BleFeature()176     BleFeature()
177     {}
178 
179     /**
180      * @brief Destructor.
181      */
~BleFeature()182     ~BleFeature()
183     {}
184     /**
185      * @brief Constructor.
186      */
187     BleFeature(BleFeature &) = delete;
188 
189     /**
190      * @brief Constructor.
191      */
192     BleFeature &operator=(const BleFeature &) = delete;
193 };
194 }  // namespace bluetooth
195 }  // namespace OHOS
196 
197 #endif  // BLE_FEATURE_H