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