• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "wifi_p2p_impl.h"
16 #include "iservice_registry.h"
17 #include "if_config.h"
18 #include "mac_address.h"
19 #include "wifi_logger.h"
20 
21 DEFINE_WIFILOG_P2P_LABEL("WifiP2pImpl");
22 namespace OHOS {
23 namespace Wifi {
24 
25 #define RETURN_IF_FAIL(cond)                          \
26     do {                                              \
27         if (!(cond)) {                                \
28             WIFI_LOGI("'%{public}s' failed.", #cond); \
29             return WIFI_OPT_FAILED;                   \
30         }                                             \
31     } while (0)
32 
WifiP2pImpl(int systemAbilityId)33 WifiP2pImpl::WifiP2pImpl(int systemAbilityId) : systemAbilityId_(systemAbilityId), client_(nullptr)
34 {}
35 
~WifiP2pImpl()36 WifiP2pImpl::~WifiP2pImpl()
37 {}
38 
Init(void)39 bool WifiP2pImpl::Init(void)
40 {
41     sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     if (sa_mgr == nullptr) {
43         WIFI_LOGE("failed to get SystemAbilityManager");
44         return false;
45     }
46 
47     sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
48     if (object == nullptr) {
49         WIFI_LOGE("failed to get P2P_SERVICE");
50         return false;
51     }
52 
53     client_ = iface_cast<IWifiP2p>(object);
54     if (client_ == nullptr) {
55         client_ = new (std::nothrow) WifiP2pProxy(object);
56     }
57 
58     if (client_ == nullptr) {
59         WIFI_LOGE("wifi p2p init failed. %{public}d", systemAbilityId_);
60         return false;
61     }
62 
63     return true;
64 }
65 
EnableP2p(void)66 ErrCode WifiP2pImpl::EnableP2p(void)
67 {
68     RETURN_IF_FAIL(client_);
69     return client_->EnableP2p();
70 }
71 
DisableP2p(void)72 ErrCode WifiP2pImpl::DisableP2p(void)
73 {
74     RETURN_IF_FAIL(client_);
75     return client_->DisableP2p();
76 }
77 
DiscoverDevices(void)78 ErrCode WifiP2pImpl::DiscoverDevices(void)
79 {
80     RETURN_IF_FAIL(client_);
81     return client_->DiscoverDevices();
82 }
83 
StopDiscoverDevices(void)84 ErrCode WifiP2pImpl::StopDiscoverDevices(void)
85 {
86     RETURN_IF_FAIL(client_);
87     return client_->StopDiscoverDevices();
88 }
89 
DiscoverServices(void)90 ErrCode WifiP2pImpl::DiscoverServices(void)
91 {
92     RETURN_IF_FAIL(client_);
93     return client_->DiscoverServices();
94 }
95 
StopDiscoverServices(void)96 ErrCode WifiP2pImpl::StopDiscoverServices(void)
97 {
98     RETURN_IF_FAIL(client_);
99     return client_->StopDiscoverServices();
100 }
101 
RequestService(const WifiP2pDevice & device,const WifiP2pServiceRequest & request)102 ErrCode WifiP2pImpl::RequestService(const WifiP2pDevice &device, const WifiP2pServiceRequest &request)
103 {
104     RETURN_IF_FAIL(client_);
105     return client_->RequestService(device, request);
106 }
107 
PutLocalP2pService(const WifiP2pServiceInfo & srvInfo)108 ErrCode WifiP2pImpl::PutLocalP2pService(const WifiP2pServiceInfo &srvInfo)
109 {
110     RETURN_IF_FAIL(client_);
111     return client_->PutLocalP2pService(srvInfo);
112 }
113 
DeleteLocalP2pService(const WifiP2pServiceInfo & srvInfo)114 ErrCode WifiP2pImpl::DeleteLocalP2pService(const WifiP2pServiceInfo &srvInfo)
115 {
116     RETURN_IF_FAIL(client_);
117     return client_->DeleteLocalP2pService(srvInfo);
118 }
119 
StartP2pListen(int period,int interval)120 ErrCode WifiP2pImpl::StartP2pListen(int period, int interval)
121 {
122     RETURN_IF_FAIL(client_);
123     return client_->StartP2pListen(period, interval);
124 }
125 
StopP2pListen(void)126 ErrCode WifiP2pImpl::StopP2pListen(void)
127 {
128     RETURN_IF_FAIL(client_);
129     return client_->StopP2pListen();
130 }
131 
FormGroup(const WifiP2pConfig & config)132 ErrCode WifiP2pImpl::FormGroup(const WifiP2pConfig &config)
133 {
134     RETURN_IF_FAIL(client_);
135     return client_->FormGroup(config);
136 }
137 
RemoveGroup(void)138 ErrCode WifiP2pImpl::RemoveGroup(void)
139 {
140     RETURN_IF_FAIL(client_);
141     return client_->RemoveGroup();
142 }
143 
DeleteGroup(const WifiP2pGroupInfo & group)144 ErrCode WifiP2pImpl::DeleteGroup(const WifiP2pGroupInfo &group)
145 {
146     RETURN_IF_FAIL(client_);
147     return client_->DeleteGroup(group);
148 }
149 
P2pConnect(const WifiP2pConfig & config)150 ErrCode WifiP2pImpl::P2pConnect(const WifiP2pConfig &config)
151 {
152     RETURN_IF_FAIL(client_);
153     return client_->P2pConnect(config);
154 }
155 
P2pDisConnect(void)156 ErrCode WifiP2pImpl::P2pDisConnect(void)
157 {
158     RETURN_IF_FAIL(client_);
159     return client_->P2pDisConnect();
160 }
161 
QueryP2pLinkedInfo(WifiP2pLinkedInfo & linkedInfo)162 ErrCode WifiP2pImpl::QueryP2pLinkedInfo(WifiP2pLinkedInfo &linkedInfo)
163 {
164     RETURN_IF_FAIL(client_);
165     return client_->QueryP2pLinkedInfo(linkedInfo);
166 }
167 
GetCurrentGroup(WifiP2pGroupInfo & group)168 ErrCode WifiP2pImpl::GetCurrentGroup(WifiP2pGroupInfo &group)
169 {
170     RETURN_IF_FAIL(client_);
171     return client_->GetCurrentGroup(group);
172 }
173 
GetP2pEnableStatus(int & status)174 ErrCode WifiP2pImpl::GetP2pEnableStatus(int &status)
175 {
176     RETURN_IF_FAIL(client_);
177     return client_->GetP2pEnableStatus(status);
178 }
179 
GetP2pDiscoverStatus(int & status)180 ErrCode WifiP2pImpl::GetP2pDiscoverStatus(int &status)
181 {
182     RETURN_IF_FAIL(client_);
183     return client_->GetP2pDiscoverStatus(status);
184 }
185 
GetP2pConnectedStatus(int & status)186 ErrCode WifiP2pImpl::GetP2pConnectedStatus(int &status)
187 {
188     RETURN_IF_FAIL(client_);
189     return client_->GetP2pConnectedStatus(status);
190 }
191 
QueryP2pDevices(std::vector<WifiP2pDevice> & devives)192 ErrCode WifiP2pImpl::QueryP2pDevices(std::vector<WifiP2pDevice> &devives)
193 {
194     RETURN_IF_FAIL(client_);
195     return client_->QueryP2pDevices(devives);
196 }
197 
QueryP2pGroups(std::vector<WifiP2pGroupInfo> & groups)198 ErrCode WifiP2pImpl::QueryP2pGroups(std::vector<WifiP2pGroupInfo> &groups)
199 {
200     RETURN_IF_FAIL(client_);
201     return client_->QueryP2pGroups(groups);
202 }
203 
QueryP2pServices(std::vector<WifiP2pServiceInfo> & services)204 ErrCode WifiP2pImpl::QueryP2pServices(std::vector<WifiP2pServiceInfo> &services)
205 {
206     RETURN_IF_FAIL(client_);
207     return client_->QueryP2pServices(services);
208 }
209 
RegisterCallBack(const sptr<IWifiP2pCallback> & callback)210 ErrCode WifiP2pImpl::RegisterCallBack(const sptr<IWifiP2pCallback> &callback)
211 {
212     RETURN_IF_FAIL(client_);
213     return client_->RegisterCallBack(callback);
214 }
215 
GetSupportedFeatures(long & features)216 ErrCode WifiP2pImpl::GetSupportedFeatures(long &features)
217 {
218     RETURN_IF_FAIL(client_);
219     return client_->GetSupportedFeatures(features);
220 }
221 
IsFeatureSupported(long feature)222 bool WifiP2pImpl::IsFeatureSupported(long feature)
223 {
224     RETURN_IF_FAIL(client_);
225     long tmpFeatures = 0;
226     if (client_->GetSupportedFeatures(tmpFeatures) != WIFI_OPT_SUCCESS) {
227         return false;
228     }
229     return ((tmpFeatures & feature) == feature);
230 }
231 
SetP2pDeviceName(const std::string & deviceName)232 ErrCode WifiP2pImpl::SetP2pDeviceName(const std::string &deviceName)
233 {
234     RETURN_IF_FAIL(client_);
235     return client_->SetP2pDeviceName(deviceName);
236 }
237 
SetP2pWfdInfo(const WifiP2pWfdInfo & wfdInfo)238 ErrCode WifiP2pImpl::SetP2pWfdInfo(const WifiP2pWfdInfo &wfdInfo)
239 {
240     RETURN_IF_FAIL(client_);
241     return client_->SetP2pWfdInfo(wfdInfo);
242 }
243 
Hid2dRequestGcIp(const std::string & gcMac,std::string & ipAddr)244 ErrCode WifiP2pImpl::Hid2dRequestGcIp(const std::string& gcMac, std::string& ipAddr)
245 {
246     RETURN_IF_FAIL(client_);
247     return client_->Hid2dRequestGcIp(gcMac, ipAddr);
248 }
249 
Hid2dSharedlinkIncrease()250 ErrCode WifiP2pImpl::Hid2dSharedlinkIncrease()
251 {
252     RETURN_IF_FAIL(client_);
253     return client_->Hid2dSharedlinkIncrease();
254 }
255 
Hid2dSharedlinkDecrease()256 ErrCode WifiP2pImpl::Hid2dSharedlinkDecrease()
257 {
258     RETURN_IF_FAIL(client_);
259     return client_->Hid2dSharedlinkDecrease();
260 }
261 
Hid2dCreateGroup(const int frequency,FreqType type)262 ErrCode WifiP2pImpl::Hid2dCreateGroup(const int frequency, FreqType type)
263 {
264     RETURN_IF_FAIL(client_);
265     return client_->Hid2dCreateGroup(frequency, type);
266 }
267 
Hid2dRemoveGcGroup(const std::string & gcIfName)268 ErrCode WifiP2pImpl::Hid2dRemoveGcGroup(const std::string& gcIfName)
269 {
270     RETURN_IF_FAIL(client_);
271     return client_->Hid2dRemoveGcGroup(gcIfName);
272 }
273 
Hid2dConnect(const Hid2dConnectConfig & config)274 ErrCode WifiP2pImpl::Hid2dConnect(const Hid2dConnectConfig& config)
275 {
276     RETURN_IF_FAIL(client_);
277     return client_->Hid2dConnect(config);
278 }
279 
Hid2dConfigIPAddr(const std::string & ifName,const IpAddrInfo & ipInfo)280 ErrCode WifiP2pImpl::Hid2dConfigIPAddr(const std::string& ifName, const IpAddrInfo& ipInfo)
281 {
282     RETURN_IF_FAIL(client_);
283     return client_->Hid2dConfigIPAddr(ifName, ipInfo);
284 }
285 
Hid2dReleaseIPAddr(const std::string & ifName)286 ErrCode WifiP2pImpl::Hid2dReleaseIPAddr(const std::string& ifName)
287 {
288     RETURN_IF_FAIL(client_);
289     return client_->Hid2dReleaseIPAddr(ifName);
290 }
291 
Hid2dGetRecommendChannel(const RecommendChannelRequest & request,RecommendChannelResponse & response)292 ErrCode WifiP2pImpl::Hid2dGetRecommendChannel(const RecommendChannelRequest& request,
293     RecommendChannelResponse& response)
294 {
295     RETURN_IF_FAIL(client_);
296     return client_->Hid2dGetRecommendChannel(request, response);
297 }
298 
Hid2dGetChannelListFor5G(std::vector<int> & vecChannelList)299 ErrCode WifiP2pImpl::Hid2dGetChannelListFor5G(std::vector<int>& vecChannelList)
300 {
301     RETURN_IF_FAIL(client_);
302     return client_->Hid2dGetChannelListFor5G(vecChannelList);
303 }
304 
Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType,char cfgData[CFG_DATA_MAX_BYTES],int * getDatValidLen)305 ErrCode WifiP2pImpl::Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType, char cfgData[CFG_DATA_MAX_BYTES], int* getDatValidLen)
306 {
307     RETURN_IF_FAIL(client_);
308     return client_->Hid2dGetSelfWifiCfgInfo(cfgType, cfgData, getDatValidLen);
309 }
310 
Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType,char cfgData[CFG_DATA_MAX_BYTES],int setDataValidLen)311 ErrCode WifiP2pImpl::Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType, char cfgData[CFG_DATA_MAX_BYTES], int setDataValidLen)
312 {
313     RETURN_IF_FAIL(client_);
314     return client_->Hid2dSetPeerWifiCfgInfo(cfgType, cfgData, setDataValidLen);
315 }
316 }  // namespace Wifi
317 }  // namespace OHOS