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 HFP_HF_SDP_CLIENT_H
17 #define HFP_HF_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 "hfp_hf_defines.h"
28 #include "sdp.h"
29 
30 namespace OHOS {
31 namespace bluetooth {
32 typedef struct {
33     uint16_t attributeId {0};
34     SdpDataType type {SDP_TYPE_UINT_8};
35     uint16_t attributeValue {0};
36 } HfpHfSdpAttribute;
37 
38 typedef struct {
39     std::vector<SdpProtocolDescriptor> descriptors {};        // AG protocol descriptor
40     std::vector<SdpProfileDescriptor> profileDescriptors {};  // AG profile descriptor
41     std::vector<HfpHfSdpAttribute> attributes {};             // AG attribute descriptor
42 } HfpHfRemoteSdpService;
43 
44 typedef struct {
45     std::vector<HfpHfRemoteSdpService> services {};  // AG service descriptor
46 } HfpHfRemoteSdpServiceArray;
47 
48 /**
49  * @brief Class for discovery remote AG device's SDP server and finding out
50  *        related SDP info required by HFP HF role.
51  */
52 class HfpHfSdpClient {
53 public:
54     /**
55      * @brief Construct a new HfpHfSdpClient object.
56      */
57     HfpHfSdpClient() = default;
58 
59     /**
60      * @brief Destroy the HfpHfSdpClient object.
61      */
62     ~HfpHfSdpClient();
63 
64     /**
65      * @brief Callback function of SDP discovery.
66      *
67      * @param addr Remote device address defined bt stack.
68      * @param serviceAry Array of services discovered.
69      * @param serviceNum Number of services discovered.
70      * @param context Upper layer context.
71      */
72     static void SdpCallback(const BtAddr *addr, const SdpService *serviceAry, uint16_t serviceNum, void *context);
73 
74     /**
75      * @brief Start a service discovery job.
76      *
77      * @param remoteAddr Remote device address.
78      * @param role Role in connection.
79      * @return Returns the error code of the discovery result.
80      */
81     int DoDiscovery(const std::string &remoteAddr, int role);
82 
83     /**
84      * @brief Callback function of SDP discovery for HSP.
85      *
86      * @param addr Remote device address defined bt stack.
87      * @param serviceAry Array of services discovered.
88      * @param serviceNum Number of services discovered.
89      * @param context Upper layer context.
90      */
91     static void SdpHspCallback(const BtAddr *addr, const SdpService *serviceAry, uint16_t serviceNum, void *context);
92 
93     /**
94      * @brief Start a service discovery job.
95      *
96      * @param remoteAddr Remote device address.
97      * @param role Role in connection.
98      * @return Returns the error code of the discovery result.
99      */
100     int DoHspAgDiscovery(const std::string &remoteAddr);
101 
102     /**
103      * @brief Start a find service attributes job.
104      *
105      * @param remoteAddr Remote device address.
106      * @param role Role in connection.
107      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
108      */
109     bool FindAttributes(const std::string &remoteAddr, int role);
110 
111     /**
112      * @brief Get the Remote Sdp Info object.
113      *
114      * @return Returns the HfpAgRemoteSdpInfo object.
115      */
116     HfpHfRemoteSdpInfo GetRemoteSdpInfo() const;
117 
118 private:
119     /**
120      * @brief Cache SDP result data locally.
121      *
122      * @param remoteAddr Remote device address.
123      * @param serviceAry Array of services discovered.
124      * @param serviceNum Number of services discovered.
125      */
126     static void CopySdpServiceArray(const std::string &remoteAddr, const SdpService *serviceAry, uint16_t serviceNum);
127 
128     /**
129      * @brief Delete local cached SDP result data.
130      *
131      * @param array Array of services Reserved.
132      */
133     static void DeleteSdpServiceArray(HfpHfRemoteSdpServiceArray &array);
134 
135     /**
136      * @brief Loop all protocol to find rfcomm scn.
137      *
138      * @param loopNum Loop Number.
139      * @param array Remote AG device SDP service array.
140      * @param scn Server channel number.
141      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
142      */
143     bool LoopAllProtocolRfcomm(uint16_t &loopNum, const HfpHfRemoteSdpServiceArray &array, uint8_t &scn) const;
144 
145     /**
146      * @brief Find out remote server channel number.
147      *
148      * @param protocols Protocol descriptors.
149      * @param scn[out] Remote server channel number.
150      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
151      */
152     static bool FindProtocolRfcomm(const std::vector<SdpProtocolDescriptor> &protocols, uint8_t &scn);
153 
154     /**
155      * @brief Find out remote profile version.
156      *
157      * @param profiles Profile descriptors.
158      * @param version[out] Remote profile version.
159      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
160      */
161     static bool FindProfileVersion(const std::vector<SdpProfileDescriptor> &profiles, uint16_t &version);
162 
163     /**
164      * @brief Find out remote AG features.
165      *
166      * @param attributes Attribute descriptors.
167      * @param features[out] Remote AG features.
168      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
169      */
170     static bool FindProfileFeatures(const std::vector<HfpHfSdpAttribute> &attributes, uint16_t &features);
171 
172     /**
173      * @brief Find out remote AG network for ability to reject calls in Attribute descriptor.
174      *
175      * @param attributes Attribute descriptors.
176      * @param network Network value.
177      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
178      */
179     static bool FindProfileNetwork(const std::vector<HfpHfSdpAttribute> &attributes, uint8_t &network);
180 
181     /**
182      * @brief Select remote AG features and codec.
183      *
184      * @param features[out] Remote AG features.
185      * @param codec [out] Remote AG features.
186      */
187     static void SelectProfileFeatures(uint16_t &features, int &codec);
188 
189     // Attribute occupy one byte
190     inline static constexpr int ATTRIBUTE_LENGTH_UINT8 = 1;
191 
192     // Attribute occupy two bytes
193     inline static constexpr int ATTRIBUTE_LENGTH_UINT16 = 2;
194 
195     inline static constexpr uint16_t HFP_HF_CLIENT_CLASSID_NUM = 1;
196     inline static constexpr uint16_t HFP_HF_CLIENT_INITIATOR_ATTR_NUM = 5;
197     inline static constexpr uint16_t HFP_HF_CLIENT_ACCEPTOR_ATTR_NUM = 4;
198     inline static constexpr int SERVICE_CLASS_ID_LIST_INDEX = 0;
199     inline static constexpr int PROTOCOL_DESCRIPTOR_LIST_INDEX = 1;
200     inline static constexpr int INITIATOR_PROFILE_DESCRIPTOR_LIST_INDEX = 2;
201     inline static constexpr int ACCEPTER_PROFILE_DESCRIPTOR_LIST_INDEX = 1;
202     inline static constexpr int INITIATOR_SUPPORTED_FEATURES_INDEX = 3;
203     inline static constexpr int ACCEPTER_SUPPORTED_FEATURES_INDEX = 2;
204     inline static constexpr int INITIATOR_DATA_STORES_OR_NETWORK_INDEX = 4;
205     inline static constexpr int ACCEPTER_DATA_STORES_OR_NETWORK_INDEX = 3;
206 
207     // Remote device SDP info after DoDiscovery()
208     static std::map<std::string, HfpHfRemoteSdpServiceArray> g_remoteSdpServiceArrays;
209 
210     // Current remote device address
211     std::string currentAddr_ {""};
212 
213     // Remote device SDP info after last FindAttributes()
214     HfpHfRemoteSdpInfo remoteSdpInfo_ {};
215 
216     // The mutex variable
217     static std::recursive_mutex g_hfpSdpMutex;
218 
219     int hspState_ = 1;
220 
221     BT_DISALLOW_COPY_AND_ASSIGN(HfpHfSdpClient);
222 };
223 }  // namespace bluetooth
224 }  // namespace OHOS
225 #endif // HFP_HF_SDP_CLIENT_H