1 /* 2 * Copyright (C) 2021 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 #ifndef OHOS_P2P_SERVICE_MANAGER_H 16 #define OHOS_P2P_SERVICE_MANAGER_H 17 18 #include <vector> 19 #include <map> 20 #include <mutex> 21 #include "wifi_p2p_service_request_list.h" 22 #include "wifi_p2p_service_response_list.h" 23 24 namespace OHOS { 25 namespace Wifi { 26 class WifiP2pServiceManager { 27 public: 28 /** 29 * @Description Construct a new WifiP2pServiceManager object. 30 * 31 */ 32 WifiP2pServiceManager(); 33 34 /** 35 * @Description Destroy the WifiP2pServiceManager object. 36 * 37 */ 38 virtual ~WifiP2pServiceManager() = default; 39 40 /** 41 * @Description - Initialization service manager. 42 * 43 */ 44 virtual void Init(); 45 46 /** 47 * @Description - Clear related records. 48 */ 49 virtual void ClearAll(); 50 51 /** 52 * @Description - Set queryId. 53 * @param setQueryId - queryId to be set 54 */ 55 virtual void SetQueryId(const std::string &setQueryId); 56 57 /** 58 * @Description - Get queryId. 59 * @return - const std::string& return the string of queryId 60 */ 61 virtual const std::string &GetQueryId() const; 62 63 /** 64 * @Description Calculate and return the latest service transaction ID. 65 * @return - unsigned char Latest returned service transaction ID after calculation 66 */ 67 virtual unsigned char GetTransId(); 68 69 /** 70 * @Description Add all responses to the device service discovery response list. 71 * 72 * @param p2pSvrRespList service response information list 73 * @return true local response list is updated 74 * @return false local response list is not updated 75 */ 76 virtual bool AddServiceResponse(const WifiP2pServiceResponseList &p2pSvrRespList); 77 78 /** 79 * @Description Delete all responses from the device service discovery response list. 80 * 81 * @param p2pSvrRespList service response information list 82 * @return true deleted successfully 83 * @return false deletion failed 84 */ 85 virtual bool RemoveServiceResponse(const WifiP2pServiceResponseList &p2pSvrRespList); 86 87 /** 88 * @Description - Deletes all matching responses based on the device address. 89 * @param deviceAddress - device address to be matched 90 * @return - bool true: updated successfully false: no update 91 */ 92 virtual bool RemoveServiceResponse(const std::string &deviceAddress); 93 94 /** 95 * @Description Clear all service responses on all devices. 96 * 97 * @return true cleared successfully 98 * @return false clearing failed 99 */ 100 virtual bool ClearAllServiceResponse(); 101 102 /** 103 * @Description Obtains all service discovery responses of all devices. 104 * 105 * @param respList a list of saving service discovery responses 106 * @return true obtained successfully 107 * @return false obtaining failed 108 */ 109 virtual bool GetServiceResponseList(std::vector<WifiP2pServiceResponseList> &respList); 110 111 /** 112 * @Description - Updates response information on the device based on responseList. 113 * @param rspList - service discovery responses 114 * @return true: update false: no update 115 */ 116 virtual bool AddDeviceResponses(const WifiP2pServiceResponseList &rspList); 117 118 /** 119 * @Description - Updates the ServiceInfo information on the peer device based on the response. 120 * @param rsp - service discovery response 121 * @param dev - device information of response 122 * @return true: update false: no update 123 */ 124 virtual bool AddDeviceService(const WifiP2pServiceResponse &rsp, const WifiP2pDevice &dev); 125 126 /** 127 * @Description - Deleting a saved peer device service. 128 * 129 * @param devAddr - peer device address 130 * @return true: deleted successfully false: deletion failed 131 */ 132 virtual bool DelServicesFormAddress(const std::string &devAddr); 133 134 /** 135 * @Description Obtain services on all known devices. 136 * @param serviceList - a list of saving all known services 137 */ 138 virtual void GetDeviceServices(std::vector<WifiP2pServiceInfo> &services); 139 140 /** 141 * @Description - Sends the received request to a specific service. After processing the request, 142 the service returns a response. 143 * @param reqList - service request list 144 * @return - WifiP2pServiceResponseList 145 */ 146 virtual WifiP2pServiceResponseList ProcessServiceRequestList(const WifiP2pServiceRequestList &reqList); 147 148 /** 149 * @Description - Send the received response to the specific WifiP2pServiceInfo for processing. 150 * @param rspList - list of responses to be submitted to specific WifiP2pServiceInfo 151 */ 152 virtual void ProcessServiceResponseList(const WifiP2pServiceResponseList &rspList) const; 153 154 /** 155 * @Description - Delete all discovered peer device services. 156 * 157 * @return true: deleted successfully false: deletion failed 158 */ 159 virtual bool ClearAllDeviceService(); 160 161 /** 162 * @Description Adding a locally registered service. 163 * 164 * @param p2pSvrInfo registered service information 165 * @return true adding succeeded 166 * @return false adding failed (already exists) 167 */ 168 virtual bool AddLocalService(const WifiP2pServiceInfo &p2pSvrInfo); 169 /** 170 * @Description Removing a locally registered service. 171 * 172 * @param p2pSvrInfo information about the removed service 173 * @return true removal succeeded 174 * @return false removal failed 175 */ 176 virtual bool RemoveLocalService(const WifiP2pServiceInfo &p2pSvrInfo); 177 /** 178 * @Description Clearing all local registered services. 179 * 180 */ 181 virtual void ClearLocalService(); 182 183 /** 184 * @Description - Check whether the service request has been recorded. 185 * @param deviceAddress - source address of the recorded request 186 * @param dialogToken - dialog ID of the recorded request 187 * @return - bool true: recorded false: not recorded 188 */ 189 virtual bool IsRecordedRequest(const std::string &deviceAddress, int dialogToken); 190 191 /** 192 * @Description - Add a request record. 193 * @param deviceAddress - source address of the recorded request 194 * @param dialogToken - dialog ID of the recorded request 195 */ 196 virtual void AddRequestRecord(const std::string &deviceAddress, int dialogToken); 197 198 /** 199 * @Description - Delete a request record. 200 * @param deviceAddress - source address of the recorded request 201 * @param dialogToken - dialog ID of the recorded request 202 */ 203 virtual void RemoveRequestRecord(const std::string &deviceAddress, int dialogToken); 204 205 /** 206 * @Description - Remove all records. 207 */ 208 virtual void ClearAllRequestRecord(); 209 /** 210 * @Description Obtains the service information list of the local service. 211 * 212 * @return const std::vector<WifiP2pServiceInfo>& 213 */ 214 virtual const std::vector<WifiP2pServiceInfo> &GetLocalServiceList(); 215 /** 216 * @Description - Update the information about the corresponding service name based on the parameter. 217 * 218 * @param devAddr - device address 219 * @param svrInfo - service info 220 * @return true - updated successfully 221 * @return false - update failed 222 */ 223 virtual bool UpdateServiceName(const std::string &devAddr, const WifiP2pServiceResponse &svrInfo); 224 225 private: 226 /* non-zero service transaction ID */ 227 unsigned char transId; 228 /** 229 * Indicates the reqID of a request record. This parameter is used to cancel a specified req. When a request is 230 * responded to, this parameter needs to be cleared. 231 */ 232 std::string queryId; 233 /* Records the current request, which is used to intercept repeated requests. */ 234 std::map<std::string, std::vector<int>> requestRecord; 235 /* services on devices discovered by the service */ 236 std::map<std::string, std::vector<WifiP2pServiceInfo>> deviceService; 237 /* locally received responses from the peer end (target device + all responses from the target device) */ 238 std::map<std::string, WifiP2pServiceResponseList> serviceRespons; 239 /* Locally registered services. Theoretically, all services are provided. */ 240 std::vector<WifiP2pServiceInfo> localServicesInfo; 241 std::mutex serviceMutex; 242 }; 243 } // namespace Wifi 244 } // namespace OHOS 245 246 #endif /* OHOS_P2P_SERVICE_MANAGER_H */ 247