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 #include "wifi_p2p_hal_interface.h"
17 #include "wifi_log.h"
18 #include <mutex>
19
20 #undef LOG_TAG
21 #define LOG_TAG "WifiP2PHalInterface"
22
23 namespace OHOS {
24 namespace Wifi {
GetInstance(void)25 WifiP2PHalInterface &WifiP2PHalInterface::GetInstance(void)
26 {
27 static WifiP2PHalInterface inst;
28 static int initFlag = 0;
29 static std::mutex initMutex;
30 if (initFlag == 0) {
31 std::unique_lock<std::mutex> lock(initMutex);
32 if (initFlag == 0) {
33 if (inst.InitIdlClient()) {
34 initFlag = 1;
35 }
36 }
37 }
38 return inst;
39 }
40
StartP2p(void) const41 WifiErrorNo WifiP2PHalInterface::StartP2p(void) const
42 {
43 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
44 return mIdlClient->ReqP2pStart();
45 }
46
StopP2p(void) const47 WifiErrorNo WifiP2PHalInterface::StopP2p(void) const
48 {
49 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
50 return mIdlClient->ReqP2pStop();
51 }
52
RegisterP2pCallback(const P2pHalCallback & callbacks)53 WifiErrorNo WifiP2PHalInterface::RegisterP2pCallback(const P2pHalCallback &callbacks)
54 {
55 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
56 WifiErrorNo err = mIdlClient->ReqP2pRegisterCallback(callbacks);
57 if (err == WIFI_IDL_OPT_OK || callbacks.onConnectSupplicant == nullptr) {
58 mP2pCallback = callbacks;
59 }
60 return err;
61 }
62
StartWpsPbc(const std::string & groupInterface,const std::string & bssid) const63 WifiErrorNo WifiP2PHalInterface::StartWpsPbc(const std::string &groupInterface, const std::string &bssid) const
64 {
65 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
66 return mIdlClient->ReqP2pSetupWpsPbc(groupInterface, bssid);
67 }
68
StartWpsPin(const std::string & groupInterface,const std::string & address,const std::string & pin,std::string & result) const69 WifiErrorNo WifiP2PHalInterface::StartWpsPin(
70 const std::string &groupInterface, const std::string &address, const std::string &pin, std::string &result) const
71 {
72 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
73 return mIdlClient->ReqP2pSetupWpsPin(groupInterface, address, pin, result);
74 }
75
RemoveNetwork(int networkId) const76 WifiErrorNo WifiP2PHalInterface::RemoveNetwork(int networkId) const
77 {
78 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
79 return mIdlClient->ReqP2pRemoveNetwork(networkId);
80 }
81
ListNetworks(std::map<int,WifiP2pGroupInfo> & mapGroups) const82 WifiErrorNo WifiP2PHalInterface::ListNetworks(std::map<int, WifiP2pGroupInfo> &mapGroups) const
83 {
84 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
85 return mIdlClient->ReqP2pListNetworks(mapGroups);
86 }
87
SetP2pDeviceName(const std::string & name) const88 WifiErrorNo WifiP2PHalInterface::SetP2pDeviceName(const std::string &name) const
89 {
90 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
91 return mIdlClient->ReqP2pSetDeviceName(name);
92 }
93
SetP2pDeviceType(const std::string & type) const94 WifiErrorNo WifiP2PHalInterface::SetP2pDeviceType(const std::string &type) const
95 {
96 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
97 return mIdlClient->ReqP2pSetWpsDeviceType(type);
98 }
99
SetP2pSecondaryDeviceType(const std::string & type)100 WifiErrorNo WifiP2PHalInterface::SetP2pSecondaryDeviceType(const std::string &type)
101 {
102 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
103 return mIdlClient->ReqP2pSetWpsSecondaryDeviceType(type);
104 }
105
SetP2pConfigMethods(const std::string & methods) const106 WifiErrorNo WifiP2PHalInterface::SetP2pConfigMethods(const std::string &methods) const
107 {
108 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
109 return mIdlClient->ReqP2pSetWpsConfigMethods(methods);
110 }
111
SetP2pSsidPostfix(const std::string & postfixName) const112 WifiErrorNo WifiP2PHalInterface::SetP2pSsidPostfix(const std::string &postfixName) const
113 {
114 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
115 return mIdlClient->ReqP2pSetSsidPostfixName(postfixName);
116 }
117
SetP2pGroupIdle(const std::string & groupInterface,size_t time) const118 WifiErrorNo WifiP2PHalInterface::SetP2pGroupIdle(const std::string &groupInterface, size_t time) const
119 {
120 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
121 return mIdlClient->ReqP2pSetGroupMaxIdle(groupInterface, time);
122 }
123
SetP2pPowerSave(const std::string & groupInterface,bool enable) const124 WifiErrorNo WifiP2PHalInterface::SetP2pPowerSave(const std::string &groupInterface, bool enable) const
125 {
126 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
127 return mIdlClient->ReqP2pSetPowerSave(groupInterface, enable);
128 }
129
SetWfdEnable(bool enable) const130 WifiErrorNo WifiP2PHalInterface::SetWfdEnable(bool enable) const
131 {
132 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
133 return mIdlClient->ReqP2pSetWfdEnable(enable);
134 }
135
SetWfdDeviceConfig(const std::string & config) const136 WifiErrorNo WifiP2PHalInterface::SetWfdDeviceConfig(const std::string &config) const
137 {
138 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
139 return mIdlClient->ReqP2pSetWfdDeviceConfig(config);
140 }
141
P2pFind(size_t timeout) const142 WifiErrorNo WifiP2PHalInterface::P2pFind(size_t timeout) const
143 {
144 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
145 return mIdlClient->ReqP2pStartFind(timeout);
146 }
147
P2pStopFind() const148 WifiErrorNo WifiP2PHalInterface::P2pStopFind() const
149 {
150 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
151 return mIdlClient->ReqP2pStopFind();
152 }
153
P2pConfigureListen(bool enable,size_t period,size_t interval) const154 WifiErrorNo WifiP2PHalInterface::P2pConfigureListen(bool enable, size_t period, size_t interval) const
155 {
156 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
157 return mIdlClient->ReqP2pSetExtListen(enable, period, interval);
158 }
159
SetListenChannel(size_t channel,unsigned char regClass) const160 WifiErrorNo WifiP2PHalInterface::SetListenChannel(size_t channel, unsigned char regClass) const
161 {
162 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
163 return mIdlClient->ReqP2pSetListenChannel(channel, regClass);
164 }
165
P2pFlush() const166 WifiErrorNo WifiP2PHalInterface::P2pFlush() const
167 {
168 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
169 return mIdlClient->ReqP2pFlush();
170 }
171
Connect(const WifiP2pConfigInternal & config,bool isJoinExistingGroup,std::string & pin) const172 WifiErrorNo WifiP2PHalInterface::Connect(const WifiP2pConfigInternal &config, bool isJoinExistingGroup,
173 std::string &pin) const
174 {
175 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
176 return mIdlClient->ReqP2pConnect(config, isJoinExistingGroup, pin);
177 }
178
CancelConnect() const179 WifiErrorNo WifiP2PHalInterface::CancelConnect() const
180 {
181 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
182 return mIdlClient->ReqP2pCancelConnect();
183 }
184
ProvisionDiscovery(const WifiP2pConfigInternal & config) const185 WifiErrorNo WifiP2PHalInterface::ProvisionDiscovery(const WifiP2pConfigInternal &config) const
186 {
187 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
188 return mIdlClient->ReqP2pProvisionDiscovery(config);
189 }
190
GroupAdd(bool isPersistent,int networkId,int freq) const191 WifiErrorNo WifiP2PHalInterface::GroupAdd(bool isPersistent, int networkId, int freq) const
192 {
193 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
194 return mIdlClient->ReqP2pAddGroup(isPersistent, networkId, freq);
195 }
196
GroupRemove(const std::string & groupInterface) const197 WifiErrorNo WifiP2PHalInterface::GroupRemove(const std::string &groupInterface) const
198 {
199 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
200 return mIdlClient->ReqP2pRemoveGroup(groupInterface);
201 }
202
Invite(const WifiP2pGroupInfo & group,const std::string & deviceAddr) const203 WifiErrorNo WifiP2PHalInterface::Invite(const WifiP2pGroupInfo &group, const std::string &deviceAddr) const
204 {
205 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
206 return mIdlClient->ReqP2pInvite(group, deviceAddr);
207 }
208
Reinvoke(int networkId,const std::string & deviceAddr) const209 WifiErrorNo WifiP2PHalInterface::Reinvoke(int networkId, const std::string &deviceAddr) const
210 {
211 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
212 return mIdlClient->ReqP2pReinvoke(networkId, deviceAddr);
213 }
214
GetDeviceAddress(std::string & deviceAddress) const215 WifiErrorNo WifiP2PHalInterface::GetDeviceAddress(std::string &deviceAddress) const
216 {
217 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
218 return mIdlClient->ReqP2pGetDeviceAddress(deviceAddress);
219 }
220
GetGroupCapability(const std::string & deviceAddress,uint32_t & cap) const221 WifiErrorNo WifiP2PHalInterface::GetGroupCapability(const std::string &deviceAddress, uint32_t &cap) const
222 {
223 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
224 return mIdlClient->ReqP2pGetGroupCapability(deviceAddress, cap);
225 }
226
P2pServiceAdd(const WifiP2pServiceInfo & info) const227 WifiErrorNo WifiP2PHalInterface::P2pServiceAdd(const WifiP2pServiceInfo &info) const
228 {
229 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
230 return mIdlClient->ReqP2pAddService(info);
231 }
232
P2pServiceRemove(const WifiP2pServiceInfo & info) const233 WifiErrorNo WifiP2PHalInterface::P2pServiceRemove(const WifiP2pServiceInfo &info) const
234 {
235 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
236 return mIdlClient->ReqP2pRemoveService(info);
237 }
238
FlushService() const239 WifiErrorNo WifiP2PHalInterface::FlushService() const
240 {
241 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
242 return mIdlClient->ReqP2pFlushService();
243 }
244
SaveConfig() const245 WifiErrorNo WifiP2PHalInterface::SaveConfig() const
246 {
247 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
248 return mIdlClient->ReqP2pSaveConfig();
249 }
250
ReqServiceDiscovery(const std::string & deviceAddress,const std::vector<unsigned char> & tlvs,std::string & reqID) const251 WifiErrorNo WifiP2PHalInterface::ReqServiceDiscovery(
252 const std::string &deviceAddress, const std::vector<unsigned char> &tlvs, std::string &reqID) const
253 {
254 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
255 return mIdlClient->ReqP2pReqServiceDiscovery(deviceAddress, tlvs, reqID);
256 }
257
CancelReqServiceDiscovery(const std::string & id) const258 WifiErrorNo WifiP2PHalInterface::CancelReqServiceDiscovery(const std::string &id) const
259 {
260 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
261 return mIdlClient->ReqP2pCancelServiceDiscovery(id);
262 }
263
SetRandomMacAddr(bool enable) const264 WifiErrorNo WifiP2PHalInterface::SetRandomMacAddr(bool enable) const
265 {
266 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
267 return mIdlClient->ReqP2pSetRandomMac(enable);
268 }
269
SetMiracastMode(int type) const270 WifiErrorNo WifiP2PHalInterface::SetMiracastMode(int type) const
271 {
272 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
273 return mIdlClient->ReqP2pSetMiracastType(type);
274 }
275
SetPersistentReconnect(int mode) const276 WifiErrorNo WifiP2PHalInterface::SetPersistentReconnect(int mode) const
277 {
278 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
279 return mIdlClient->ReqSetPersistentReconnect(mode);
280 }
281
RespServiceDiscovery(const WifiP2pDevice & device,int frequency,int dialogToken,const std::vector<unsigned char> & tlvs) const282 WifiErrorNo WifiP2PHalInterface::RespServiceDiscovery(
283 const WifiP2pDevice &device, int frequency, int dialogToken, const std::vector<unsigned char> &tlvs) const
284 {
285 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
286 return mIdlClient->ReqRespServiceDiscovery(device, frequency, dialogToken, tlvs);
287 }
288
SetServiceDiscoveryExternal(bool isExternalProcess) const289 WifiErrorNo WifiP2PHalInterface::SetServiceDiscoveryExternal(bool isExternalProcess) const
290 {
291 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
292 return mIdlClient->ReqSetServiceDiscoveryExternal(isExternalProcess);
293 }
294
GetP2pPeer(const std::string & deviceAddress,WifiP2pDevice & device) const295 WifiErrorNo WifiP2PHalInterface::GetP2pPeer(const std::string &deviceAddress, WifiP2pDevice &device) const
296 {
297 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
298 return mIdlClient->ReqGetP2pPeer(deviceAddress, device);
299 }
300
P2pGetSupportFrequenciesByBand(int band,std::vector<int> & frequencies) const301 WifiErrorNo WifiP2PHalInterface::P2pGetSupportFrequenciesByBand(int band, std::vector<int> &frequencies) const
302 {
303 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
304 return mIdlClient->ReqP2pGetSupportFrequencies(band, frequencies);
305 }
306
P2pSetGroupConfig(int networkId,const IdlP2pGroupConfig & config) const307 WifiErrorNo WifiP2PHalInterface::P2pSetGroupConfig(int networkId, const IdlP2pGroupConfig &config) const
308 {
309 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
310 return mIdlClient->ReqP2pSetGroupConfig(networkId, config);
311 }
312
P2pGetGroupConfig(int networkId,IdlP2pGroupConfig & config) const313 WifiErrorNo WifiP2PHalInterface::P2pGetGroupConfig(int networkId, IdlP2pGroupConfig &config) const
314 {
315 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
316 return mIdlClient->ReqP2pGetGroupConfig(networkId, config);
317 }
318
P2pAddNetwork(int & networkId) const319 WifiErrorNo WifiP2PHalInterface::P2pAddNetwork(int &networkId) const
320 {
321 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
322 return mIdlClient->ReqP2pAddNetwork(networkId);
323 }
324
GetP2pCallbackInst(void) const325 const P2pHalCallback &WifiP2PHalInterface::GetP2pCallbackInst(void) const
326 {
327 return mP2pCallback;
328 }
329
Hid2dConnect(const Hid2dConnectConfig & config) const330 WifiErrorNo WifiP2PHalInterface::Hid2dConnect(const Hid2dConnectConfig &config) const
331 {
332 CHECK_NULL_AND_RETURN(mIdlClient, WIFI_IDL_OPT_FAILED);
333 return mIdlClient->ReqP2pHid2dConnect(config);
334 }
335 } // namespace Wifi
336 } // namespace OHOS