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 CLASSIC_ADAPTER_H
17 #define CLASSIC_ADAPTER_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include "base_def.h"
23 #include "bt_uuid.h"
24 #include "btm.h"
25 #include "classic_adapter_properties.h"
26 #include "classic_battery_observer.h"
27 #include "classic_bluetooth_data.h"
28 #include "classic_remote_device.h"
29 #include "context.h"
30 #include "gap_if.h"
31 #include "interface_adapter_classic.h"
32 #include "log.h"
33 #include "log_util.h"
34 #include "raw_address.h"
35 #include "sdp.h"
36 
37 /**
38  * @addtogroup Bluetooth
39  * @{
40  *
41  * @brief Defines adapter classic common functions.
42  *
43  */
44 
45 /**
46  * @file classic_adapter.h
47  *
48  * @brief Adapter classic.
49  *
50  */
51 
52 namespace OHOS {
53 namespace bluetooth {
54 /**
55  * @brief Represents classic adapter.
56  *
57  */
58 class ClassicAdapter : public IAdapterClassic, public utility::Context {
59 public:
60     /**
61      * @brief A constructor used to create a <b>ClassicAdapter</b> instance.
62      *
63      */
64     ClassicAdapter();
65 
66     /**
67      * @brief A destructor used to delete the <b>ClassicAdapter</b> instance.
68      *
69      */
70     ~ClassicAdapter();
71 
72     /**
73      * @brief Get local device address.
74      *
75      * @return Returns local device address.
76      */
77     std::string GetLocalAddress() const override;
78 
79     /**
80      * @brief Get local device name.
81      *
82      * @return Returns local device name.
83      */
84     std::string GetLocalName() const override;
85 
86     /**
87      * @brief Set local device name.
88      *
89      * @param name Device name.
90      * @return Returns <b>true</b> if the operation is successful;
91      *         returns <b>false</b> if the operation fails.
92      */
93     bool SetLocalName(const std::string &name) const override;
94 
95     /**
96      * @brief Set bondable mode.
97      *
98      * @param mode Bondable mode.
99      * @return Returns <b>true</b> if the operation is successful;
100      *         returns <b>false</b> if the operation fails.
101      */
102     bool SetBondableMode(int mode) const override;
103 
104     /**
105      * @brief Get bondable mode.
106      *
107      * @return Returns bondable mode.
108      */
109     int GetBondableMode() const override;
110 
111     /**
112      * @brief Get remote device name.
113      *
114      * @param device Remote device.
115      * @return Returns device name.
116      */
117     std::string GetDeviceName(const RawAddress &device) const override;
118 
119     /**
120      * @brief Get remote device uuids.
121      *
122      * @param device Remote device.
123      * @return Returns device uuids.
124      */
125     std::vector<Uuid> GetDeviceUuids(const RawAddress &device) const override;
126 
127     /**
128      * @brief Get paired devices.
129      *
130      * @return Returns device list.
131      */
132     std::vector<RawAddress> GetPairedDevices() const override;
133 
134     /**
135      * @brief Local device start pair.
136      *
137      * @param device Remote device.
138      * @return Returns <b>true</b> if the operation is successful;
139      *         returns <b>false</b> if the operation fails.
140      */
141     bool StartPair(const RawAddress &device) override;
142 
143     /**
144      * @brief Check if remote device was bonded from local.
145      *
146      * @param device Remote device.
147      * @return Returns <b>true</b> if device was bonded from local;
148      *         returns <b>false</b> if device was not bonded from local.
149      */
150     bool IsBondedFromLocal(const RawAddress &device) const override;
151 
152     /**
153      * @brief Cancel pair operation.
154      *
155      * @param device Remote device.
156      * @return Returns <b>true</b> if the operation is successful;
157      *         returns <b>false</b> if the operation fails.
158      */
159     bool CancelPairing(const RawAddress &device) override;
160 
161     /**
162      * @brief Remove pair.
163      *
164      * @param device Remote device.
165      * @return Returns <b>true</b> if the operation is successful;
166      *         returns <b>false</b> if the operation fails.
167      */
168     bool RemovePair(const RawAddress &device) override;
169 
170     /**
171      * @brief Get device pair state.
172      *
173      * @param device Remote device.
174      * @return Returns device pair state.
175      */
176     int GetPairState(const RawAddress &device) const override;
177 
178     /**
179      * @brief Set device pairing confirmation.
180      *
181      * @param device Remote device.
182      * @param accept Set gap accept flag.
183      * @return Returns <b>true</b> if the operation is successful;
184      *         returns <b>false</b> if the operation fails.
185      */
186     bool SetDevicePairingConfirmation(const RawAddress &device, bool accept) const override;
187 
188     /**
189      * @brief Set device pair passkey.
190      *
191      * @param device Remote device.
192      * @param passkey Device passkey.
193      * @param accept Set gap accept flag.
194      * @return Returns <b>true</b> if the operation is successful;
195      *         returns <b>false</b> if the operation fails.
196      */
197     bool SetDevicePasskey(const RawAddress &device, int passkey, bool accept) const override;
198 
199     /**
200      * @brief Check remote device pair request reply.
201      *
202      * @param device Remote device.
203      * @param accept Set gap accept flag.
204      * @return Returns <b>true</b> if the operation is successful;
205      *         returns <b>false</b> if the operation fails.
206      */
207     bool PairRequestReply(const RawAddress &device, bool accept) const override;
208 
209     /**
210      * @brief Check if acl connected with remote device.
211      *
212      * @param device Remote device.
213      * @return Returns <b>true</b> if device acl connected;
214      *         returns <b>false</b> if device does not acl connect.
215      */
216     bool IsAclConnected(const RawAddress &device) const override;
217 
218     /**
219      * @brief Check if remote device acl Encrypted.
220      *
221      * @return Returns <b>true</b> if device acl Encrypted;
222      *         returns <b>false</b> if device does not acl Encrypt.
223      */
224     bool IsAclEncrypted(const RawAddress &device) const override;
225 
226     /**
227      * @brief Get the instance of the ClassicAdapter.
228      *
229      * @return Returns the instance of the ClassicAdapter.
230      */
231     utility::Context *GetContext() override;
232 
233     /**
234      * @brief Enable classic service.
235      *
236      */
237     void Enable() override;
238 
239     /**
240      * @brief Disable classic service.
241      *
242      */
243     void Disable() override;
244 
245     /**
246      * @brief PostEnable classic service.
247      *
248      */
249     void PostEnable() override;
250 
251     /**
252      * @brief Get remote device type.
253      *
254      * @param device Remote device.
255      * @return Returns remote device type.
256      */
257     int GetDeviceType(const RawAddress &device) const override;
258 
259     /**
260      * @brief Get remote device class.
261      *
262      * @param device Remote device.
263      * @return Returns remote device class.
264      */
265     int GetDeviceClass(const RawAddress &device) const override;
266 
267     /**
268      * @brief Get remote device alias name.
269      *
270      * @param device Remote device
271      * @return Returns remote device alias name.
272      */
273     std::string GetAliasName(const RawAddress &device) const override;
274 
275     /**
276      * @brief Set remote device alias name.
277      *
278      * @param device Remote device which setted alias name.
279      * @param name Alias name.
280      * @return Returns <b>true</b> if the operation is successful;
281      *         returns <b>false</b> if the operation fails.
282      */
283     bool SetAliasName(const RawAddress &device, const std::string &name) const override;
284 
285     /**
286      * @brief Register remote device observer.
287      *
288      * @param observer Class IClassicRemoteDeviceObserver pointer to register observer.
289      * @return Returns <b>true</b> if the operation is successful;
290      *         returns <b>false</b> if the operation fails.
291      */
292     bool RegisterRemoteDeviceObserver(IClassicRemoteDeviceObserver &observer) const override;
293 
294     /**
295      * @brief Deregister remote device observer.
296      *
297      * @param observer Class IClassicRemoteDeviceObserver pointer to register observer.
298      * @return Returns <b>true</b> if the operation is successful;
299      *         returns <b>false</b> if the operation fails.
300      */
301     bool DeregisterRemoteDeviceObserver(IClassicRemoteDeviceObserver &observer) const override;
302 
303     /**
304      * @brief Register classic adapter observer.
305      *
306      * @param observer Class IAdapterClassicObserver pointer to register observer.
307      * @return Returns <b>true</b> if the operation is successful;
308      *         returns <b>false</b> if the operation fails.
309      */
310     bool RegisterClassicAdapterObserver(IAdapterClassicObserver &observer) const override;
311 
312     /**
313      * @brief Deregister classic adapter observer.
314      *
315      * @param observer Class IAdapterClassicObserver pointer to register observer.
316      * @return Returns <b>true</b> if the operation is successful;
317      *         returns <b>false</b> if the operation fails.
318      */
319     bool DeregisterClassicAdapterObserver(IAdapterClassicObserver &observer) const override;
320 
321     /**
322      * @brief Remove all pairs.
323      *
324      * @return Returns <b>true</b> if the operation is successful;
325      *         returns <b>false</b> if the operation fails.
326      */
327     bool RemoveAllPairs() override;
328 
329     /**
330      * @brief Set device pair pin.
331      *
332      * @param device Remote device address.
333      * @param pin Pin code.
334      * @param length Pin code length.
335      * @return Returns <b>true</b> if the operation is successful;
336      *         returns <b>false</b> if the operation fails.
337      */
338     bool SetDevicePin(const RawAddress &device, const std::string &pinCode) const override;
339 
340     /**
341      * @brief Set device scan mode.
342      *
343      * @param mode Scan mode.
344      * @param duration Scan time.
345      * @return Returns <b>true</b> if the operation is successful;
346      *         returns <b>false</b> if the operation fails.
347      */
348     bool SetBtScanMode(int mode, int duration) override;
349 
350     /**
351      * @brief Get device scan mode.
352      *
353      * @return Returns bluetooth scan mode.
354      */
355     int GetBtScanMode() const override;
356 
357     /**
358      * @brief Get local device class.
359      *
360      * @return Returns local device class.
361      */
362     int GetLocalDeviceClass() const override;
363 
364     /**
365      * @brief Set local device class.
366      *
367      * @param deviceClass Device class.
368      * @return Returns <b>true</b> if the operation is successful;
369      *         returns <b>false</b> if the operation fails.
370      */
371     bool SetLocalDeviceClass(int deviceClass) const override;
372 
373     /**
374      * @brief Get device address.
375      *
376      * @return Returns <b>true</b> if the operation is successful;
377      *         returns <b>false</b> if the operation fails.
378      */
379     bool StartBtDiscovery() override;
380 
381     /**
382      * @brief Cancel device discovery.
383      *
384      * @return Returns <b>true</b> if the operation is successful;
385      *         returns <b>false</b> if the operation fails.
386      */
387     bool CancelBtDiscovery() override;
388 
389     /**
390      * @brief Check if device is discovering.
391      *
392      * @return Returns <b>true</b> if device is discovering;
393      *         returns <b>false</b> if device is not discovering.
394      */
395     bool IsBtDiscovering() const override;
396 
397     /**
398      * @brief Get device discovery end time.
399      *
400      * @return Returns device discovery end time.
401      */
402     long GetBtDiscoveryEndMillis() const override;
403     void SetDeviceBatteryLevel(const RawAddress &device, int batteryLevel) const override;
404     bool IsHfpCodSupported(const RawAddress &device) override;
405 
406 private:
407     /**
408      * @brief Inquiry result callback.
409      *
410      * @param addr Device address.
411      * @param classOfDevice Device class.
412      * @param context Classic adapter object.
413      */
414     static void InquiryResultCallback(const BtAddr *addr, uint32_t classOfDevice, void *context);
415 
416     /**
417      * @brief Inquiry result rssi callback.
418      *
419      * @param addr Device address.
420      * @param classOfDevice Device class.
421      * @param rssi Device rssi.
422      * @param context Classic adapter object.
423      */
424     static void InquiryResultRssiCallback(const BtAddr *addr, uint32_t classOfDevice, int8_t rssi, void *context);
425 
426     /**
427      * @brief Extended inquiry result callback.
428      *
429      * @param addr Device address.
430      * @param classOfDevice Device class.
431      * @param rssi Device rssi.
432      * @param eir Eir data.
433      * @param context Classic adapter object.
434      */
435     static void ExtendedInquiryResultCallback(const BtAddr *addr, uint32_t classOfDevice, int8_t rssi,
436         const uint8_t eir[MAX_EXTEND_INQUIRY_RESPONSE_LEN], void *context);
437 
438     /**
439      * @brief Inquiry complete callback.
440      *
441      * @param status Inquire status.
442      * @param context Classic adapter object.
443      */
444     static void InquiryCompleteCallback(uint8_t status, void *context);
445 
446     /**
447      * @brief Remote device name callback.
448      *
449      * @param status Inquire status.
450      * @param addr Device address.
451      * @param name Device name.
452      * @param context Classic adapter object.
453      */
454     static void RemoteNameCallback(
455         uint8_t status, const BtAddr *addr, const uint8_t name[MAX_LOC_BT_NAME_LEN], void *context);
456     /**
457      * @brief User confirm request callback.
458      *
459      * @param addr Device address.
460      * @param number Pass key.
461      * @param context Classic adapter object.
462      */
463     static void UserConfirmReqCallback(const BtAddr *addr, uint32_t number, int localMitmRequired, int remoteMitmRequired, void *context);
464 
465     /**
466      * @brief User pass key request callback.
467      *
468      * @param addr Device address.
469      * @param context Classic adapter object.
470      */
471     static void UserPasskeyReqCallback(const BtAddr *addr, void *context);
472 
473     /**
474      * @brief User pass key notification callback.
475      *
476      * @param addr Device address.
477      * @param number Pass key.
478      * @param context Classic adapter object.
479      */
480     static void UserPasskeyNotificationCallback(const BtAddr *addr, uint32_t number, void *context);
481 
482     /**
483      * @brief Remote oob key request callback.
484      *
485      * @param addr Device address.
486      * @param context Classic adapter object.
487      */
488     static void RemoteOobReqCallback(const BtAddr *addr, void *context);
489 
490     /**
491      * @brief Pin code request callback.
492      *
493      * @param addr Device address.
494      * @param context Classic adapter object.
495      */
496     static void PinCodeReqCallback(const BtAddr *addr, void *context);
497 
498     /**
499      * @brief Link key request callback.
500      *
501      * @param addr Device address.
502      * @param context Classic adapter object.
503      */
504     static void LinkKeyReqCallback(const BtAddr *addr, void *context);
505 
506     /**
507      * @brief Io capability request callback.
508      *
509      * @param addr Device address.
510      * @param context Classic adapter object.
511      */
512     static void IoCapabilityReqCallback(const BtAddr *addr, void *context);
513 
514     /**
515      * @brief Io capability response callback.
516      *
517      * @param addr Device address.
518      * @param ioCapability Device iocapability.
519      * @param context Classic adapter object.
520      */
521     static void IoCapabilityRspCallback(const BtAddr *addr, uint8_t ioCapability, void *context);
522 
523     /**
524      * @brief Link key notification.
525      *
526      * @param addr Device address.
527      * @param linkkey Link key.
528      * @param keyType Link key type.
529      * @param context Classic adapter object.
530      */
531     static void LinkKeyNotification(
532         const BtAddr *addr, const uint8_t linkKey[GAP_LINKKEY_SIZE], uint8_t keyType, void *context);
533 
534     /**
535      * @brief Simple pair complete callback.
536      *
537      * @param addr Device address.
538      * @param status Pair status.
539      * @param context Classic adapter object.
540      */
541     static void SimplePairCompleteCallback(const BtAddr *addr, uint8_t status, void *context);
542 
543     /**
544      * @brief Authentication complete callback.
545      *
546      * @param addr Device address.
547      * @param status Authentication status.
548      * @param context Classic adapter object.
549      */
550     static void AuthenticationCompleteCallback(const BtAddr *addr, uint8_t status, void *context);
551 
552     /**
553      * @brief Encryption change complete callback.
554      *
555      * @param addr Device address.
556      * @param status Encryption status.
557      * @param context Classic adapter object.
558      */
559     static void EncryptionChangeCallback(const BtAddr *addr, uint8_t status, void *context);
560 
561     /**
562      * @brief Authorize ind callback.
563      *
564      * @param addr Device address.
565      * @param service Gap service.
566      * @param context Classic adapter object.
567      */
568     static void AuthorizeIndCallback(const BtAddr *addr, GAP_Service service, void *context);
569 
570     /// BTM ACL Change status callback.
571     /**
572      * @brief Acl connection complete callback.
573      *
574      * @param status Connection status.
575      * @param connectionHandle Connection handle.
576      * @param addr Device address.
577      * @param encyptionEnabled Encyption enable status.
578      * @param context Classic adapter object.
579      */
580     static void ConnectionComplete(const BtmAclConnectCompleteParam *param, void *context);
581 
582     /**
583      * @brief Acl disconnection complete callback.
584      *
585      * @param status Connection status.
586      * @param connectionHandle Connection handle.
587      * @param reason Disconnection reason.
588      * @param context Classic adapter object.
589      */
590     static void DisconnectionComplete(uint8_t status, uint16_t connectionHandle, uint8_t reason, void *context);
591 
592     /// SDP search remote device supported uuid callback.
593     /**
594      * @brief Service search attribute callback.
595      *
596      * @param addr Device address.
597      * @param serviceArray Service array.
598      * @param serviceNum Service number.
599      * @param context Classic adapter object.
600      */
601     static void ServiceSearchAttributeCb(
602         const BtAddr *addr, const SdpService *serviceArray, uint16_t serviceNum, void *context);
603 
604     /**
605      * @brief Handle inquiry event.
606      *
607      * @param event inquiry callback event.
608      * @param param Gap callback paramer.
609      */
610     void HandleInquiryEvent(GAP_CB_EVENT event, const GapCallbackParam &param);
611 
612     /**
613      * @brief Handle security event.
614      *
615      * @param event security callback event.
616      * @param param Gap callback paramer.
617      */
618     void HandleSecurityEvent(GAP_CB_EVENT event, const GapCallbackParam &param);
619 
620     /**
621      * @brief ClassicAdater start up. Load config file and enable stack.
622      *
623      */
624     void StartUp();
625 
626     /**
627      * @brief ClassicAdater shut down. Save config file and disable stack.
628      *
629      */
630     void ShutDown();
631 
632     /**
633      * @brief Process post enable.
634      *
635      */
636     void ProcessPostEnable();
637 
638     /**
639      * @brief Set scan mode.
640      *
641      * @param mode Scan mode.
642      * @return Returns <b>true</b> if the operation is successful;
643      *         returns <b>false</b> if the operation fails.
644      */
645     bool SetScanMode(int mode);
646 
647     /**
648      * @brief Set scan mode result callback.
649      *
650      * @param status Scan status.
651      * @param context ClassicAdapterProperties object.
652      */
653     static void SetScanModeResultCallback(uint8_t status, void *context);
654 
655     /**
656      * @brief Receive SetScanMode callback.
657      *
658      * @param status SetScanMode result.
659      */
660     void ReceiveSetScanModeCallback(uint8_t status);
661 
662     /**
663      * @brief ScanMode timeout.
664      *
665      */
666     void ScanModeTimeout();
667 
668     /**
669      * @brief Hw(Controller) process timeout.
670      *
671      */
672     void HwProcessTimeout();
673     void HwTimeout();
674 
675     /**
676      * @brief Register callback.
677      *
678      */
679     bool RegisterCallback();
680 
681     /**
682      * @brief Deregister callback.
683      *
684      */
685     bool DeregisterCallback() const;
686 
687     /**
688      * @brief Get remote device name.
689      *
690      * @param addr Device address.
691      * @return Returns <b>true</b> if the operation is successful;
692      *         returns <b>false</b> if the operation fails.
693      */
694     bool GetRemoteName(const BtAddr &addr) const;
695 
696     /**
697      * @brief Set link key.
698      *
699      * @param addr Device address.
700      */
701     void SetLinkKey(const BtAddr &addr);
702 
703     /**
704      * @brief Set Io capability.
705      *
706      * @param addr Device address.
707      */
708     void SetIoCapability(const BtAddr &addr);
709 
710     /**
711      * @brief Parser eir data.
712      *
713      * @param remote Remote device.
714      * @param data Eir data.
715      */
716     void ParserEirData(std::shared_ptr<ClassicRemoteDevice> remote, const std::vector<uint8_t> &data) const;
717 
718     /**
719      * @brief Load paired device info from config file.
720      *
721      */
722     void LoadPairedDeviceInfo();
723 
724     /**
725      * @brief Save paired devices info.
726      *
727      */
728     void SavePairedDevices() const;
729 
730     /**
731      * @brief Get service uuid from device uuid.
732      *
733      * @param uuid Device uuid.
734      * @return Returns service uuid.
735      */
736     Uuid GetUuidFromBtUuid(const BtUuid &inUuid) const;
737 
738     /**
739      * @brief Get local support uuids then update the uuids to EIR data.
740      *
741      */
742     void UpdateSupportedUuids() const;
743 
744     /**
745      * @brief Save remote device Io capability.
746      *
747      * @param addr Remote device address.
748      * @param ioCapability Device Io capability
749      */
750     void SaveRemoteIoCapability(const BtAddr &addr, uint8_t ioCapability);
751 
752     /**
753      * @brief Send scan mode changed.
754      *
755      * @param mode ScamMode.
756      */
757     void SendScanModeChanged(int mode) const;
758 
759     /**
760      * @brief Send discovery state changed.
761      *
762      */
763     void SendDiscoveryStateChanged(int discoveryState) const;
764 
765     /**
766      * @brief Send discovery result.
767      *
768      * @param device Remote device.
769      * @param rssi Rssi of device.
770      * @param deviceName Name of device.
771      * @param deviceClass Class of device.
772      */
773     void SendDiscoveryResult(
774         const RawAddress &device, int rssi, const std::string deviceName, int deviceClass) const;
775 
776     /**
777      * @brief Send remote device cod changed.
778      *
779      * @param device Remote device.
780      * @param cod Remote device cod.
781      */
782     void SendRemoteCodChanged(const RawAddress &device, int cod) const;
783 
784     /**
785      * @brief Send remote device battery level changed.
786      *
787      * @param device Remote device.
788      * @param batteryLevel Remote device battery level.
789      */
790     void SendRemoteBatteryLevelChanged(const RawAddress &device, int batteryLevel) const;
791 
792     /**
793      * @brief Send remote device name changed.
794      *
795      * @param device Remote device.
796      * @param deviceName Remote device name.
797      */
798     void SendRemoteNameChanged(const RawAddress &device, const std::string &deviceName) const;
799 
800     /**
801      * @brief Send pair status changed.
802      *
803      * @param transport Remote device transport.
804      * @param device Remote device.
805      * @param status Pair status.
806      */
807     void SendPairStatusChanged(const BTTransport transport, const RawAddress &device, int status) const;
808 
809     /**
810      * @brief Send pair request.
811      *
812      * @param transport Remote device transport.
813      * @param device Remote device.
814      */
815     void SendPairRequested(const BTTransport transport, const RawAddress &device) const;
816 
817     /**
818      * @brief Send pair confirmed.
819      *
820      * @param device Remote device.
821      * @param reqType confirmed type (SSP/Pin Code).
822      * @param number confirmed number.
823      */
824     void SendPairConfirmed(const RawAddress &device, int reqType, int number) const;
825 
826     /**
827      * @brief Send remote device uuid changed.
828      *
829      * @param device Remote device.
830      * @param uuids Device uuid.
831      */
832     void SendRemoteUuidChanged(const RawAddress &device, const std::vector<Uuid> &uuids) const;
833 
834     /**
835      * @brief Send remote alias name changed.
836      *
837      * @param device Remote device.
838      * @param aliasName alias name.
839      */
840     void SendRemoteAliasChanged(const RawAddress &device, const std::string &aliasName) const;
841 
842     /**
843      * @brief Handle inquire result.
844      *
845      * @param addr Device address.
846      * @param classOfDevice Device class.
847      * @param rssi Device rssi.
848      * @param eir Eir data.
849      */
850     void HandleInquiryResult(const BtAddr &addr, uint32_t classOfDevice, std::vector<uint8_t> eir, int8_t rssi = 0);
851 
852     /**
853      * @brief Handle inquire complete.
854      *
855      * @param status Inquire status.
856      */
857     void HandleInquiryComplete(uint8_t status);
858 
859     /**
860      * @brief Receive remote device name.
861      *
862      * @param status Receive status.
863      * @param addr Remote device address.
864      * @param name  Remote device name.
865      */
866     void ReceiveRemoteName(uint8_t status, const BtAddr &addr, const uint8_t name[MAX_LOC_BT_NAME_LEN]);
867 
868     /**
869      * @brief SSP confirm request.
870      *
871      * @param addr Remote device address.
872      * @param reqType  Request type.
873      * @param number Pass key.
874      */
875     void SSPConfirmReq(const BtAddr &addr, int reqType, int number, int localMitmRequired, int remoteMitmRequired);
876 
877     /**
878      * @brief Set authorize response.
879      *
880      * @param addr Remote device address.
881      * @param service Gap service.
882      */
883     void SetAuthorizeRes(const BtAddr &addr, GAP_Service service) const;
884 
885     /**
886      * @brief Receive link key notification.
887      *
888      * @param addr Remote device address.
889      * @param linkKey Link key.
890      * @param keyType Link key type.
891      */
892     void ReceiveLinkKeyNotification(const BtAddr &addr, const uint8_t linkKey[GAP_LINKKEY_SIZE], uint8_t keyType);
893 
894     /**
895      * @brief Receive simple pair complete.
896      *
897      * @param addr Remote device address.
898      * @param status Pair status.
899      */
900     void ReceiveSimplePairComplete(const BtAddr &addr, uint8_t status);
901 
902     /**
903      * @brief Receive authentication complete.
904      *
905      * @param addr Remote device address.
906      * @param status Authentication status.
907      */
908     void ReceiveAuthenticationComplete(const BtAddr &addr, uint8_t status);
909 
910     /**
911      * @brief Receive encryption change.
912      *
913      * @param addr Remote device address.
914      * @param status Encryption status.
915      */
916     void ReceiveEncryptionChange(const BtAddr &addr, uint8_t status);
917 
918     /**
919      * @brief Receive acl connection complete.
920      *
921      * @param status Connection status.
922      * @param connectionHandle Connection handle.
923      * @param remoteAddr Device address.
924      * @param encyptionEnabled Encyption enable status.
925      */
926     void ReceiveConnectionComplete(uint8_t status, uint16_t connectionHandle, const RawAddress &device,
927         uint32_t classOfDevice, bool encyptionEnabled);
928 
929     /**
930      * @brief Receive acl disconnection complete.
931      *
932      * @param status Connection status.
933      * @param connectionHandle Connection handle.
934      * @param reason Disconnection reason.
935      */
936     void ReceiveDisconnectionComplete(uint8_t status, uint16_t connectionHandle, uint8_t reason);
937 
938     /**
939      * @brief User confirm auto reply.
940      *
941      * @param device Remote device.
942      * @param reqType Request type.
943      * @param accept Request accept or not.
944      */
945     void UserConfirmAutoReply(const RawAddress &device, int reqType, bool accept) const;
946 
947     /**
948      * @brief Set pin code.
949      *
950      * @param device Remote device.
951      * @param pin Pin code.
952      * @param accept Request accept or not.
953      * @return Returns <b>true</b> if the operation is successful;
954      *         returns <b>false</b> if the operation fails.
955      */
956     bool SetPinCode(const RawAddress &device, const std::vector<uint8_t> &pin, bool accept) const;
957 
958     /**
959      * @brief Check if auto reply.
960      *
961      * @param remoteIo Remote device Io capability.
962      * @return Returns <b>true</b> if the operation is successful;
963      *         returns <b>false</b> if the operation fails.
964      */
965     bool CheckAutoReply(int remoteIo, int localMitmRequired, int remoteMitmRequired) const;
966 
967     /**
968      * @brief Check ssp confirm type.
969      *
970      * @param remoteIo Remote device Io capability.
971      * @param type Confirm type.
972      * @return Returns confirm type.
973      */
974     int CheckSspConfirmType(int remoteIo, int type) const;
975 
976     /**
977      * @brief Find remote device.
978      *
979      * @param remoteAddr Remote device address.
980      * @return Returns remote device.
981      */
982     std::shared_ptr<ClassicRemoteDevice> FindRemoteDevice(const RawAddress &device);
983 
984     /**
985      * @brief Parser remote name from eir data.
986      *
987      * @param remote Remote device.
988      * @param type type of Eir data structure.
989      * @param value Eir data of Eir data structure.
990      * @return Returns <b>true</b> if the operation is successful;
991      *         returns <b>false</b> if the operation fails.
992      */
993     bool ParserRemoteNameFromEir(
994         std::shared_ptr<ClassicRemoteDevice> remote, int type, const std::vector<uint8_t> &value) const;
995 
996     /**
997      * @brief Parser service uuid from eir data.
998      *
999      * @param type type of Eir data structure.
1000      * @param value Eir data of Eir data structure.
1001      * @return Returns uuids parser from eir data.
1002      */
1003     std::vector<Uuid> ParserUuidFromEir(int type, const std::vector<uint8_t> &value) const;
1004 
1005     /**
1006      * @brief Parser 16 bit service uuid from eir data.
1007      *
1008      * @param value Eir data of Eir data structure.
1009      * @return Returns 16 Bit uuids parser from eir data.
1010      */
1011     std::vector<Uuid> Parser16BitUuidFromEir(const std::vector<uint8_t> &value) const;
1012 
1013     /**
1014      * @brief Parser 32 bit service uuid from eir data.
1015      *
1016      * @param value Eir data of Eir data structure.
1017      * @return Returns 32 Bit uuids parses from eir data.
1018      */
1019     std::vector<Uuid> Parser32BitUuidFromEir(const std::vector<uint8_t> &value) const;
1020 
1021     /**
1022      * @brief Parser 128 bit service uuid from eir data.
1023      *
1024      * @param value Eir data of Eir data structure.
1025      * @return Returns 128 Bit uuids parser from eir data.
1026      */
1027     std::vector<Uuid> Parser128BitUuidFromEir(const std::vector<uint8_t> &value) const;
1028 
1029     /**
1030      * @brief Save remote device uuids.
1031      *        If uuids is changed, send OnRemoteUuidChanged to client.
1032      *
1033      * @param remote Remote device.
1034      * @param uuids Remote device uuids.
1035      */
1036     void SaveRemoteDeviceUuids(std::shared_ptr<ClassicRemoteDevice> remote, const std::vector<Uuid> &uuids) const;
1037 
1038     /**
1039      * @brief Check uuids1 is equal with uuids2.
1040      *
1041      * @param uuids1 uuids.
1042      * @param uuids2 uuids.
1043      * @return Returns <b>true</b> if the operation is successful;
1044      *         returns <b>false</b> if the operation fails.
1045      */
1046     bool IsUuidsEqual(const std::vector<Uuid> &uuids1, const std::vector<Uuid> &uuids2) const;
1047 
1048     void InitMode();
1049     void FreeMemory();
1050     void DisableBTM();
1051     bool DiscoverRemoteName();
1052     bool CancelGetRemoteName() const;
1053     void SearchRemoteUuids(const RawAddress &device, uint16_t uuid);
1054     void ResetScanMode();
1055     void DeleteLinkKey(std::shared_ptr<ClassicRemoteDevice> remoteDevice) const;
1056     BtAddr ConvertToBtAddr(const RawAddress &device) const;
1057     void DisablePairProcess();
1058     void SearchAttributeEnd(const RawAddress &device, const std::vector<Uuid> &uuids);
1059     void PinCodeReq(const BtAddr &addr);
1060     ClassicAdapterProperties &adapterProperties_;
1061     std::unique_ptr<utility::Timer> timer_ {};
1062     std::unique_ptr<utility::Timer> hwTimer_ {};
1063     int discoveryState_ {};
1064     int scanMode_ {};
1065     long discoveryEndMs_ {};
1066     bool pinMode_ {};
1067     bool btmEnableSuccess_ {};
1068     bool isDisable_ {};
1069     bool receiveInquiryComplete_ {};
1070     bool cancelDiscovery_ {};
1071     bool waitPairResult_ {};
1072     uint16_t searchUuid_ {};
1073     std::vector<Uuid> uuids_ {};
1074     std::string remoteNameAddr_ {INVALID_MAC_ADDRESS};
1075     std::map<std::string, std::shared_ptr<ClassicRemoteDevice>> devices_ {};
1076     BtmAclCallbacks btmAclCbs_ {};
1077     ClassicBluetoothData eirData_ {};
1078     std::unique_ptr<ClassicBatteryObserverHf> batteryObserverHf_ {};
1079     IProfileHfpHf *hfService_ {nullptr};
1080     std::unique_ptr<ClassicBatteryObserverAg> batteryObserverAg_ {};
1081     IProfileHfpAg *agService_ {nullptr};
1082 
1083     DECLARE_IMPL();
1084     BT_DISALLOW_COPY_AND_ASSIGN(ClassicAdapter);
1085 };
1086 }  // namespace bluetooth
1087 }  // namespace OHOS
1088 #endif  // CLASSIC_ADAPTER_H