• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 AVRCP_TG_CONNECTION_H
17 #define AVRCP_TG_CONNECTION_H
18 
19 #include "avrcp_tg_browse.h"
20 #include "avrcp_tg_internal.h"
21 #include "avrcp_tg_pass_through.h"
22 #include "avrcp_tg_sub_unit_info.h"
23 #include "avrcp_tg_unit_info.h"
24 #include "avrcp_tg_vendor.h"
25 #include "packet.h"
26 #include "time.h"
27 
28 namespace OHOS {
29 namespace bluetooth {
30 /**
31  * @brief This class provides a set of attributes for saving the connection information.
32  */
33 class AvrcTgConnectInfo {
34 public:
35     /**
36      * @brief A constructor used to create an <b>AvrcTgConnectInfo</b> instance.
37      */
38     AvrcTgConnectInfo(const std::string &btAddr, uint8_t connectId, uint8_t role, uint16_t controlMtu,
39         uint16_t browseMtu, uint32_t companyId, uint16_t uidCounter, AvctChannelEventCallback eventCallback,
40         AvctMsgCallback msgCallback);
41 
42     /**
43      * @brief A destructor used to delete the <b>AvrcTgConnectInfo</b> instance.
44      */
45     ~AvrcTgConnectInfo();
46 
47     /**
48      * @brief A deleted default constructor.
49      */
50     AvrcTgConnectInfo() = delete;
51 
52     // The pointer to a callback function for receiving the events, which has been registered to the AVCTP.
53     AvctChannelEventCallback eventCallback_ {nullptr};
54     // The pointer to a callback function for receiving the messages, which has been registered to the AVCTP.
55     AvctMsgCallback msgCallback_ {nullptr};
56     uint8_t connectId_;                // The id of the connection.
57     uint8_t role_;                     // The role of the connection.(Initiator or Acceptor)
58     uint16_t controlMtu_;              // The the maximum transmission unit of the control channel.
59     uint16_t browseMtu_;               // The the maximum transmission unit of the browse channel.
60     uint32_t companyId_;               // The SIG company ID of the bluetooth device.
61     uint16_t uidCounter_;              // The UID counter.
62     std::string btAddr_;               // The address of the bluetooth device.
63     std::map<uint8_t, bool> notes_ {};  // The notifications are enabled or not.
64 
65     /**
66      * @brief This struct provides a set of attributes for saving the data of the <b>PASS THROUGH</b> command.
67      */
68     struct PassInfo {
69         // The shared pointer to the instance of the <b>utility::Timer</b>
70         std::shared_ptr<utility::Timer> timer_ {nullptr};
71         // The shared pointer to the instance of the <b>AvrcTgPassPacket</b>
72         std::shared_ptr<AvrcTgPassPacket> pkt_ {nullptr};
73     } ptInfo_ {};
74 
75     /**
76      * @brief This struct provides a set of attributes for saving the data of the <b>UNIT INFO</b> and <b>SUB UNIT
77      * INFO</b> command.
78      */
79     struct UnitInfo {
80         // The shared pointer to the instance of the <b>utility::Timer</b>.
81         std::shared_ptr<utility::Timer> timer_ {nullptr};
82         // The shared pointer to the instance of the <b>AvrcTgUnitPacket</b>.
83         std::shared_ptr<AvrcTgUnitPacket> pkt_ {nullptr};
84     } unInfo_ {};
85 
86     /**
87      * @brief This struct provides the attributes for saving the data of the <b>VENDOR DEPENDENT</b> command.
88      */
89     struct VendorInfo {
90         // The shared pointer to the instance of the <b>utility::Timer</b>.
91         std::shared_ptr<utility::Timer> timer_ {nullptr};
92         // The shared pointer to the instance of the <b>AvrcTgVendorPacket</b>.
93         std::shared_ptr<AvrcTgVendorPacket> pkt_ {nullptr};
94     } vdInfo_ {};
95 
96     /**
97      * @brief This struct provides the attributes for saving the data of the <b>BROWSING</b> command.
98      */
99     struct BrowseInfo {
100         // The shared pointer to the instance of the <b>utility::Timer</b>.
101         std::shared_ptr<utility::Timer> timer_ {nullptr};
102         // The shared pointer to the instance of the <b>AvrcTgBrowsePacket</b>.
103         std::shared_ptr<AvrcTgBrowsePacket> pkt_ {nullptr};
104     } brInfo_ {};
105 };
106 
107 /**
108  * @brief This class provides a set of methods for adding/deleting/getting/setting attributes of the connection
109  * information.
110  *
111  * @detail This class implements the singleton pattern.
112  */
113 class AvrcTgConnectManager {
114 public:
115     /**
116      * @brief A destructor used to delete the <b>AvrcTgConnectManager</b> instance.
117      */
118     ~AvrcTgConnectManager();
119 
120     /**
121      * @brief Gets the instance.
122      *
123      * @return The instance of the AvrcCtConnectManager.
124      */
125     static AvrcTgConnectManager *GetInstance(void);
126 
127     /**
128      * @brief Adds some properties of the connection information of the specified bluetooth address.
129      *
130      * @param[in] rawAddr      The address of the bluetooth device.
131      * @param[in] connectId    The ID of the connection.
132      * @param[in] role         The role of the connection.(Initiator of the connection or Acceptor of the connection)
133      * @param[in] controlMtu   The MTU size of the control channel.
134      * @param[in] browseMtu    The MTU size of the browse channel.
135      * @param[in] companyId    The SIG company ID of the bluetooth device.
136      * @param[in] uidCounter   The uid counter.
137      * @param[in] eventCallback The callback function for receiving the channel event.
138      * @param[in] msgCallback  The callback function for receiving the message.
139      * @return @c BT_SUCCESS  : The connection information was added successfully.
140      *         @C RET_BAD_PARAM : The connection information already exists.
141      */
142     int Add(const RawAddress &rawAddr, uint8_t connectId, uint8_t role, uint16_t controlMtu, uint16_t browseMtu,
143         uint32_t companyId, uint16_t uidCounter, AvctChannelEventCallback eventCallback, AvctMsgCallback msgCallback);
144 
145     /**
146      * @brief Deletes the whole connection information of the specified bluetooth device.
147      *
148      * @param[in] rawAddr The address of the bluetooth device.
149      */
150     void Delete(const RawAddress &rawAddr);
151 
152     /**
153      * @brief Gets the whole connection information of the specified bluetooth device.
154      *
155      * @param[in] rawAddr The address of the bluetooth device.
156      * @return The read only pointer to the instance of the <b>AvrcTgConnectInfo</b> class.
157      */
158     const AvrcTgConnectInfo *GetConnectInfo(const RawAddress &rawAddr);
159 
160     /**
161      * @brief Gets the raw address of the specified connection id.
162      *
163      * @param[in] connectId The connection id.
164      * @return The raw address of the bluetooth device.
165      */
166     RawAddress GetRawAddress(uint8_t connectId) const;
167 
168     /**
169      * @brief Gets the address of the connected devices.
170      *
171      * @return The list of the address.
172      */
173     std::list<std::string> GetDeviceAddresses(void);
174 
175     /**
176      * @brief Sets the address of the active bluetooth device.
177      *
178      * @param[in] rawAddr The address of the bluetooth device.
179      */
180     void SetActiveDevice(const std::string addr);
181 
182     /**
183      * @brief Gets the address of the active bluetooth device.
184      *
185      * @param[in] rawAddr The address of the bluetooth device.
186      */
187     const std::string &GetActiveDevice(void);
188 
189     /**
190      * @brief Adds the address of the disconnected bluetooth device.
191      *
192      * @param[in] rawAddr The address of the bluetooth device.
193      */
194     void AddDisconnectedDevice(const std::string addr);
195 
196     /**
197      * @brief Deletes the address of the disconnected bluetooth device.
198      *
199      * @param[in] rawAddr The address of the bluetooth device.
200      */
201     void DeleteDisconnectedDevice(const std::string addr);
202 
203     /**
204      * @brief Get all of the address of the disconnected bluetooth devices.
205      *
206      * @param[in] rawAddr The address of the bluetooth device.
207      */
208     const std::list<std::string> &GetAllDisconnectedDevices(void);
209 
210     /**
211      * @brief Checks the connection information of the specified bluetooth device is empty or not.
212      *
213      * @return The result of the method execution.
214      * @retval true  The connection information is empty.
215      * @retval false The connection information is not empty.
216      */
217     bool IsConnectInfoEmpty(void) const;
218 
219     /**
220      * @brief Gets the connection id of the specified bluetooth device.
221      *
222      * @param[in] rawAddr The address of the bluetooth device.
223      * @return The connection id.
224      */
225     uint8_t GetConnectId(const RawAddress &rawAddr);
226 
227     /**
228      * @brief Sets the connection id of the specified bluetooth device.
229      *
230      * @param[in] rawAddr   The address of the bluetooth device.
231      * @param[in] connectId The connection id.
232      */
233     void SetConnectId(const RawAddress &rawAddr, uint8_t connectId);
234 
235     /**
236      * @brief Gets the vendor mtu of the specified bluetooth device.
237      *
238      * @param[in] rawAddr The address of the bluetooth device.
239      * @return The vendor mtu.
240      */
241     uint16_t GetControlMtu(const RawAddress &rawAddr);
242 
243     /**
244      * @brief Sets the vendor mtu of the specified bluetooth device.
245      *
246      * @param[in] rawAddr The address of the bluetooth device.
247      * @param[in] mtu     The vendor mtu.
248      */
249     void SetControlMtu(const RawAddress &rawAddr, uint16_t mtu);
250 
251     /**
252      * @brief Gets the browse mtu of the specified bluetooth device.
253      *
254      * @param[in] rawAddr The address of the bluetooth device.
255      * @return The browse mtu.
256      */
257     uint16_t GetBrowseMtu(const RawAddress &rawAddr);
258 
259     /**
260      * @brief Sets the browse mtu of the specified bluetooth device.
261      *
262      * @param[in] rawAddr The address of the bluetooth device.
263      * @param[in] mtu     The browse mtu.
264      */
265     void SetBrowseMtu(const RawAddress &rawAddr, uint16_t mtu);
266 
267     /**
268      * @brief Enables the notification of the specified bluetooth device.
269      *
270      * @param[in] rawAddr     The address of the bluetooth device.
271      * @param[in] notifyState The specified notify state.
272      */
273     void EnableNotifyState(const RawAddress &rawAddr, uint8_t notification);
274 
275     /**
276      * @brief Disables the notification of the specified bluetooth device.
277      *
278      * @param[in] rawAddr      The address of the bluetooth device.
279      * @param[in] notification The specified notify state.
280      */
281     void DisableNotifyState(const RawAddress &rawAddr, uint8_t notification);
282 
283     /**
284      * @brief Checks the notification of the specified bluetooth device is enabled or not.
285      *
286      * @param[in] rawAddr      The address of the bluetooth device.
287      * @param[in] notification The specified notify state.
288      * @return The result of the method execution.
289      * @retval true  The notification is enabled.
290      * @retval false The notification is disabled.
291      */
292     bool IsNotifyStateEnabled(const RawAddress &rawAddr, uint8_t notification);
293 
294     /**
295      * @brief Gets company id of the specified bluetooth device.
296      *
297      * @param[in] rawAddr The address of the bluetooth device.
298      * @return The company id.
299      */
300     uint32_t GetCompanyId(const RawAddress &rawAddr);
301 
302     /**
303      * @brief Gets the uid counter of the specified bluetooth device.
304      *
305      * @param[in] rawAddr The address of the bluetooth device.
306      * @return The uid counter.
307      */
308     uint16_t GetUidCounter(const RawAddress &rawAddr);
309 
310     /**
311      * @brief Sets the uid counter of the specified bluetooth device.
312      *
313      * @param[in] rawAddr    The address of the bluetooth device.
314      * @param[in] UidCounter The uid counter.
315      */
316     void SetUidCounter(const RawAddress &rawAddr, uint16_t uidCounter);
317 
318     /******************************************************************
319      * PASS THROUGH COMMAND                                           *
320      ******************************************************************/
321 
322     /**
323      * @brief Gets the frame packet related to the <b>PASS THROUGH</b> command.
324      *
325      * @param[in] rawAddr The address of the bluetooth device.
326      * @return The shared pointer of the frame packet.
327      */
328     std::shared_ptr<AvrcTgPassPacket> GetPassPacket(const RawAddress &rawAddr);
329 
330     /**
331      * @brief Sets the frame packet related to the <b>PASS THROUGH</b> command.
332      *
333      * @param[in] rawAddr The address of the bluetooth device.
334      * @param[in] pkt     The shared pointer of the frame packet.
335      */
336     void SetPassPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcTgPassPacket> &pkt);
337 
338     /**
339      * @brief Checks the timer related to the <b>PASS THROUGH</b> command is empty or not.
340      *
341      * @return The result of the method execution.
342      * @retval true  The timer is empty.
343      * @retval false The timer is not empty.
344      */
345     bool IsPassTimerEmpty(const RawAddress &rawAddr);
346 
347     /**
348      * @brief Sets the timer related to the <b>PASS THROUGH</b> command.
349      *
350      * @param[in] rawAddr    The address of the bluetooth device.
351      * @param[in] callback   The callback for processing timeout.
352      * @param[in] ms         The number of milliseconds used to count.
353      * @param[in] isPeriodic The timer is periodic or not.
354      */
355     void SetPassTimer(const RawAddress &rawAddr, std::function<void()> callback, int ms, bool isPeriodic = false);
356 
357     /**
358      * @brief Clears the frame packet related to the <b>PASS THROUGH</b> command.
359      *
360      * @param[in] rawAddr The address of the bluetooth device.
361      */
362     void ClearPassPacket(const RawAddress &rawAddr);
363 
364     /**
365      * @brief Clears the timer related to the <b>PASS THROUGH</b> command.
366      *
367      * @param[in] rawAddr The address of the bluetooth device.
368      */
369     void ClearPassTimer(const RawAddress &rawAddr);
370 
371     /**
372      * @brief Clears all saved data related to the <b>PASS THROUGH</b> command.
373      *
374      * @param[in] rawAddr The address of the bluetooth device.
375      */
376     void ClearPassInfo(const RawAddress &rawAddr);
377 
378     /******************************************************************
379      * UNIT INFO / SUB UNIT INFO COMMAND                              *
380      ******************************************************************/
381 
382     /**
383      * @brief Gets the frame packet related to the <b>UNIT INFO</b> and <b>SUB UNIT INFO</b> command.
384      *
385      * @param[in] rawAddr The address of the bluetooth device.
386      * @return The shared pointer of the frame packet.
387      */
388     std::shared_ptr<AvrcTgUnitPacket> GetUnitPacket(const RawAddress &rawAddr);
389 
390     /**
391      * @brief Sets the frame packet related to the <b>UNIT INFO</b> and <b>SUB UNIT INFO</b> command.
392      *
393      * @param[in] rawAddr The address of the bluetooth device.
394      * @param[in] pkt     The frame packet.
395      */
396     void SetUnitPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcTgUnitPacket> &pkt);
397 
398     /**
399      * @brief Clears the frame packet related to the <b>UNIT INFO</b> and <b>SUB UNIT INFO</b> command.
400      *
401      * @param[in] rawAddr The address of the bluetooth device.
402      */
403     void ClearUnitPacket(const RawAddress &rawAddr);
404 
405     /**
406      * @brief Clears all saved data related to the <b>UNIT INFO</b> and <b>SUB UNIT INFO</b> command.
407      *
408      * @param[in] rawAddr The address of the bluetooth device.
409      */
410     void ClearUnitInfo(const RawAddress &rawAddr);
411 
412     /******************************************************************
413      * VENDOR DEPENDENT COMMAND                                       *
414      ******************************************************************/
415 
416     /**
417      * @brief Gets the frame packet related to the <b>VENDOR DEPENDENT</b> command.
418      *
419      * @param[in] rawAddr The address of the bluetooth device.
420      * @return The shared pointer of the frame packet.
421      */
422     std::shared_ptr<AvrcTgVendorPacket> GetVendorPacket(const RawAddress &rawAddr);
423 
424     /**
425      * @brief Sets the frame packet related to the <b>VENDOR DEPENDENT</b> command.
426      *
427      * @param[in] rawAddr The address of the bluetooth device.
428      * @param[in] pkt     The frame packet.
429      */
430     void SetVendorPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcTgVendorPacket> &pkt);
431 
432     /**
433      * @brief Clears the frame packet related to the <b>VENDOR DEPENDENT</b> command.
434      *
435      * @param[in] rawAddr The address of the bluetooth device.
436      */
437     void ClearVendorPacket(const RawAddress &rawAddr);
438 
439     /**
440      * @brief Clears all saved data related to the <b>VENDOR DEPENDENT</b> command.
441      *
442      * @param[in] rawAddr The address of the bluetooth device.
443      */
444     void ClearVendorInfo(const RawAddress &rawAddr);
445 
446     /******************************************************************
447      * BROWSING COMMAND                                               *
448      ******************************************************************/
449 
450     /**
451      * @brief Gets the frame packet related to the <b>BROWSING</b> command.
452      *
453      * @param[in] rawAddr The address of the bluetooth device.
454      * @return The shared pointer of the frame packet.
455      */
456     std::shared_ptr<AvrcTgBrowsePacket> GetBrowsePacket(const RawAddress &rawAddr);
457 
458     /**
459      * @brief Sets the frame packet related to the <b>BROWSING</b> command.
460      *
461      * @param[in] rawAddr The address of the bluetooth device.
462      * @param[in] pkt     The frame packet.
463      */
464     void SetBrowsePacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcTgBrowsePacket> &pkt);
465 
466     /**
467      * @brief Clears the frame packet related to the <b>BROWSING</b> command.
468      *
469      * @param[in] rawAddr The address of the bluetooth device.
470      */
471     void ClearBrowsePacket(const RawAddress &rawAddr);
472 
473     /**
474      * @brief Clears all saved data related to the <b>BROWSING</b> command.
475      *
476      * @param[in] rawAddr The address of the bluetooth device.
477      */
478     void ClearBrowseInfo(const RawAddress &rawAddr);
479 
480 private:
481     // Locks the local variable in a multi-threaded environment.
482     std::mutex mutex_ {};
483     // The active address of the bluetooth device.
484     std::string activeAddr_ {AVRC_TG_DEFAULT_BLUETOOTH_ADDRESS};
485     // The disconnected address of the bluetooth devices.
486     std::list<std::string> disconnectedDevices_ {};
487     // Saves the connection information according to the address of the bluetooth device.
488     std::map<std::string, AvrcTgConnectInfo> infos_ {};
489     // The static pointer to the instance of the <b>AvrcTgConnectManager</b> class.
490     static AvrcTgConnectManager *g_instance;
491 
492     /**
493      * @brief A constructor used to create an <b>AvrcTgConnectManager</b> instance.
494      */
495     AvrcTgConnectManager() = default;
496 
497     /**
498      * @brief Gets the connection information of the specified bluetooth address.
499      *
500      * @param[in] btAddr The address of the bluetooth device.
501      * @return The pointer to the instance of the <b>AvrcTgConnectInfo</b> class.
502      */
503     AvrcTgConnectInfo *GetConnectInfo(const std::string &btAddr);
504 
505     BT_DISALLOW_COPY_AND_ASSIGN(AvrcTgConnectManager);
506 };
507 }  // namespace bluetooth
508 }  // namespace OHOS
509 
510 #endif  // !AVRCP_TG_CONNECTION_H
511