1 /*
2  * Copyright (C) 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 HID_HOST_SDP_CLIENT_H
17 #define HID_HOST_SDP_CLIENT_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "base_def.h"
26 #include "btstack.h"
27 #include "hid_host_defines.h"
28 #include "sdp.h"
29 #include "raw_address.h"
30 
31 namespace OHOS {
32 namespace bluetooth {
33 /**
34  * @brief Class for discovery remote H device's SDP server and finding out
35  *        related SDP info required by HFP HF role.
36  */
37 class HidHostSdpClient {
38 public:
39     /**
40      * @brief Construct a new HidHostSdpClient object.
41      */
42     explicit HidHostSdpClient(std::string address);
43 
44     /**
45      * @brief Destroy the HidHostSdpClient object.
46      */
47     ~HidHostSdpClient();
48 
49     /**
50      * @brief Callback function of SDP discovery.
51      *
52      * @param addr Remote device address defined bt stack.
53      * @param serviceAry Array of services discovered.
54      * @param serviceNum Number of services discovered.
55      * @param context Upper layer context.
56      */
57     static void SdpCallback(const BtAddr *addr, const SdpService *serviceAry, uint16_t serviceNum, void *context);
58     static void SdpPnpCallback(const BtAddr *addr,
59         const SdpService *serviceAry, uint16_t serviceNum, void *context);
60 
61     /**
62      * @brief Start a service discovery job.
63      *
64      * @param remoteAddr Remote device address.
65      * @return Returns the error code of the discovery result.
66      */
67     int DoDiscovery(const std::string &remoteAddr);
68 
69     /**
70      * @brief Get the Remote Sdp Pnp Info.
71      *
72      * @return Returns the PnpInformation.
73      */
74     PnpInformation& GetRemoteSdpPnpInfo();
75 
76     /**
77      * @brief Get the Remote Sdp hid Info.
78      *
79      * @return Returns the HidInformation.
80      */
81     HidInformation& GetRemoteSdpHidInfo();
82 
83     bool CheckIsSdpDone();
84 
85 private:
86     int DoPnpDiscovery(const std::string &remoteAddr);
87     int DoHidDiscovery(const std::string &remoteAddr);
88     void SdpCallback_(const BtAddr *addr, const SdpService *serviceAry, uint16_t serviceNum);
89     void SdpPnpCallback_(const BtAddr *addr, const SdpService *serviceAry, uint16_t serviceNum);
90     void SendSdpComplete(int result);
91     bool ParseHidDescInfo(const SdpService *serviceAry);
92     uint8_t CheckAttributeValueLengthAvalid(SdpSequenceAttribute attribute);
93     void printHidSdpInfo();
94     // Current remote device address
95     std::string currentAddr_ {""};
96 
97     PnpInformation pnpInf_ {};
98     HidInformation hidInf_ {};
99     bool isSdpDone_ = false;
100     bool isPnpSdpDone_ = false;
101 
102     BT_DISALLOW_COPY_AND_ASSIGN(HidHostSdpClient);
103 };
104 }  // namespace bluetooth
105 }  // namespace OHOS
106 #endif // HID_HOST_SDP_CLIENT_H
107