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 #ifndef OHOS_WIFI_P2P_PROXY_H
16 #define OHOS_WIFI_P2P_PROXY_H
17 
18 #include "i_wifi_p2p.h"
19 #include "iremote_proxy.h"
20 #include "wifi_errcode.h"
21 #include "wifi_p2p_msg.h"
22 
23 namespace OHOS {
24 namespace Wifi {
25 class WifiP2pProxy : public IRemoteProxy<IWifiP2p> {
26 public:
27     explicit WifiP2pProxy(const sptr<IRemoteObject> &impl);
28     ~WifiP2pProxy();
29 
30     /**
31      * @Description Enabling the P2P Mode
32      *
33      * @return ErrCode - operation result
34      */
35     ErrCode EnableP2p(void) override;
36 
37     /**
38      * @Description Disable the P2P mode
39      *
40      * @return ErrCode - operation result
41      */
42     ErrCode DisableP2p(void) override;
43 
44     /**
45      * @Description Start Wi-Fi P2P device search
46      *
47      * @return ErrCode - operation result
48      */
49     ErrCode DiscoverDevices(void) override;
50 
51     /**
52      * @Description Stop Wi-Fi P2P device search
53      *
54      * @return ErrCode - operation result
55      */
56     ErrCode StopDiscoverDevices(void) override;
57 
58     /**
59      * @Description Start the search for the Wi-Fi P2P service
60      *
61      * @return ErrCode - operation result
62      */
63     ErrCode DiscoverServices(void) override;
64 
65     /**
66      * @Description Stop the search for the Wi-Fi P2P service
67      *
68      * @return ErrCode - operation result
69      */
70     ErrCode StopDiscoverServices(void) override;
71 
72     /**
73      * @Description request the P2P service
74      *
75      * @param device - WifiP2pDevice object
76      * @param request - WifiP2pServiceRequest object
77      * @return ErrCode - operation result
78      */
79     ErrCode RequestService(const WifiP2pDevice &device, const WifiP2pServiceRequest &request) override;
80 
81     /**
82      * @Description Register the local P2P service
83      *
84      * @param srvInfo - WifiP2pServiceInfo object
85      * @return ErrCode - operation result
86      */
87     ErrCode PutLocalP2pService(const WifiP2pServiceInfo &srvInfo) override;
88 
89     /**
90      * @Description Delete the local P2P service
91      *
92      * @param srvInfo - WifiP2pServiceInfo object
93      * @return ErrCode - operation result
94      */
95     ErrCode DeleteLocalP2pService(const WifiP2pServiceInfo &srvInfo) override;
96 
97     /**
98      * @Description Enable Wi-Fi P2P listening
99      *
100      * @param period - period
101      * @param interval - interval
102      * @return ErrCode - operation result
103      */
104     ErrCode StartP2pListen(int period, int interval) override;
105 
106     /**
107      * @Description Disable Wi-Fi P2P listening
108      *
109      * @return ErrCode - operation result
110      */
111     ErrCode StopP2pListen(void) override;
112 
113     /**
114      * @Description Creating a P2P Group
115      *
116      * @param config - WifiP2pGroupInfo object
117      * @return ErrCode - operation result
118      */
119     ErrCode CreateGroup(const WifiP2pConfig &config) override;
120 
121     /**
122      * @Description Remove a P2P Group
123      *
124      *
125      * @return ErrCode - operation result
126      */
127     ErrCode RemoveGroup(void) override;
128 
129     /**
130      * @Description Remove a P2P client of current group.
131      *
132      * @param info - client gcInfo
133      * @return ErrCode - operation result
134      */
135     ErrCode RemoveGroupClient(const GcInfo &info) override;
136 
137     /**
138      * @Description Delete a p2p Group
139      *
140      * @param group - WifiP2pGroupInfo object
141      * @return ErrCode - operation result
142      */
143     ErrCode DeleteGroup(const WifiP2pGroupInfo &group) override;
144 
145     /**
146      * @Description P2P connection
147      *
148      * @param config - WifiP2pConfig object
149      * @return ErrCode - operation result
150      */
151     ErrCode P2pConnect(const WifiP2pConfig &config) override;
152 
153     /**
154      * @Description Canceling a P2P connection
155      *
156      * @return ErrCode - operation result
157      */
158     ErrCode P2pCancelConnect(void) override;
159 
160     /**
161      * @Description Querying Wi-Fi P2P Connection Information
162      *
163      * @param linkedInfo - Get the WifiP2pLinkedInfo msg
164      * @return ErrCode - operation result
165      */
166     ErrCode QueryP2pLinkedInfo(WifiP2pLinkedInfo &linkedInfo) override;
167 
168     /**
169      * @Description Get the Current Group object
170      *
171      * @param group - the WifiP2pGroupInfo object
172      * @return ErrCode - operation result
173      */
174     ErrCode GetCurrentGroup(WifiP2pGroupInfo &group) override;
175 
176     /**
177      * @Description Obtains the P2P switch status
178      *
179      * @param status - the P2P switch status
180      * @return ErrCode - operation result
181      */
182     ErrCode GetP2pEnableStatus(int &status) override;
183 
184     /**
185      * @Description Obtains the P2P discovery status
186      *
187      * @param status - the P2P discovery status
188      * @return ErrCode
189      */
190     ErrCode GetP2pDiscoverStatus(int &status) override;
191 
192     /**
193      * @Description Obtains the P2P connection status
194      *
195      * @param status - the P2P connection status
196      * @return ErrCode - operation result
197      */
198     ErrCode GetP2pConnectedStatus(int &status) override;
199 
200     /**
201      * @Description Query the information about the found devices
202      *
203      * @param devices - Get result vector of WifiP2pDevice
204      * @return ErrCode - operation result
205      */
206     ErrCode QueryP2pDevices(std::vector<WifiP2pDevice> &devices) override;
207 
208     /**
209      * @Description Query the information about local device info
210      *
211      * @param devices - Get result of WifiP2pDevice
212      * @return ErrCode - operation result
213      */
214     ErrCode QueryP2pLocalDevice(WifiP2pDevice &device) override;
215 
216     /**
217      * @Description Query the information about the found groups
218      *
219      * @param groups - Get result vector of WifiP2pGroupInfo
220      * @return ErrCode - operation result
221      */
222     ErrCode QueryP2pGroups(std::vector<WifiP2pGroupInfo> &groups) override;
223 
224     /**
225      * @Description Query the service information
226      *
227      * @param services - Get result vector of Device
228      * @return ErrCode - operation result
229      */
230     ErrCode QueryP2pServices(std::vector<WifiP2pServiceInfo> &services) override;
231 
232     /**
233      * @Description Register callback function
234      *
235      * @param callback - IWifiP2pCallback object
236      * @return ErrCode - operation result
237      */
238     ErrCode RegisterCallBack(const sptr<IWifiP2pCallback> &callback, const std::vector<std::string> &event) override;
239 
240     /**
241      * @Description Get supported features
242      *
243      * @param features - return supported features
244      * @return ErrCode - operation result
245      */
246     ErrCode GetSupportedFeatures(long &features) override;
247 
248     /**
249      * @Description set the device name
250      *
251      * @param deviceName - device name
252      * @return ErrCode - operation result
253      */
254     ErrCode SetP2pDeviceName(const std::string &deviceName) override;
255 
256     /**
257      * @Description set p2p wifi display info
258      *
259      * @param wfdInfo - wifi display info
260      * @return ErrCode - operation result
261      */
262     ErrCode SetP2pWfdInfo(const WifiP2pWfdInfo &wfdInfo) override;
263 
264     /**
265      * @Description Request an IP address to the Gc from the IP address pool, used on the GO side.
266      *
267      * @param gcMac - gc mac address
268      * @param ipAddr - applied ip address
269      * @return ErrCode - operation result
270      */
271     ErrCode Hid2dRequestGcIp(const std::string& gcMac, std::string& ipAddr) override;
272 
273     /**
274      * @Description Increase(+1) hid2d shared link reference counting
275      *
276      * @return ErrCode - operation result
277      */
278     ErrCode Hid2dSharedlinkIncrease() override;
279 
280     /**
281      * @Description Decrease(-1) hid2d shared link reference counting
282      *
283      * @return ErrCode - operation result
284      */
285     ErrCode Hid2dSharedlinkDecrease() override;
286 
287     /**
288      * @Description Create hid2d group, used on the GO side.
289      *
290      * @param frequency - frequency
291      * @param type - frequency type
292      * @return ErrCode - operation result
293      */
294     ErrCode Hid2dCreateGroup(const int frequency, FreqType type) override;
295 
296     /**
297      * @Description The GC side actively disconnects from the GO, used on the GC side.
298      *
299      * @param gcIfName - network interface name
300      * @return ErrCode - operation result
301      */
302     ErrCode Hid2dRemoveGcGroup(const std::string& gcIfName) override;
303 
304     /**
305      * @Description Connect to a specified group using hid2d, used on the GC side.
306      *
307      * @param config - connection parameters
308      * @return ErrCode - operation result
309      */
310     ErrCode Hid2dConnect(const Hid2dConnectConfig& config) override;
311 
312     /**
313      * @Description Configuring IP addresses for P2P network interfaces, used on the GC side.
314      *
315      * @param ifName - network interface name
316      * @param ipInfo - IP infos
317      * @return ErrCode - operation result
318      */
319     ErrCode Hid2dConfigIPAddr(const std::string& ifName, const IpAddrInfo& ipInfo) override;
320 
321     /**
322      * @Description Clear IP address when the P2P connection is disconnected, used on the GC side.
323      *
324      * @param ifName - network interface name
325      * @return ErrCode - operation result
326      */
327     ErrCode Hid2dReleaseIPAddr(const std::string& ifName) override;
328 
329      /**
330      * @Description Obtain the recommended channel and bandwidth for link setup
331      *
332      * @param request - request data
333      * @param response - response result
334      * @return ErrCode - operation result
335      */
336     ErrCode Hid2dGetRecommendChannel(const RecommendChannelRequest& request,
337         RecommendChannelResponse& response) override;
338 
339     /**
340      * @Description get 5G channel list
341      *
342      * @param vecChannelList - result for channel list
343      * @return ErrCode - operation result
344      */
345     ErrCode Hid2dGetChannelListFor5G(std::vector<int>& vecChannelList) override;
346 
347     /**
348      * @Description get the self wifi configuration information
349      *
350      * @param cfgType - configuration type
351      * @param cfgData - the queried data of wifi configuration
352      * @param getDatValidLen - the valid data length in the array `cfgData`
353      * @return ErrCode - operation result
354      */
355     ErrCode Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType,
356         char cfgData[CFG_DATA_MAX_BYTES], int* getDatValidLen) override;
357 
358     /**
359      * @Description set the peer wifi configuration information
360      *
361      * @param cfgType - configuration type
362      * @param cfgData - the wifi configuration data to be set
363      * @param setDataValidLen - the valid data length in the array `cfgData`
364      * @return ErrCode - operation result
365      */
366     ErrCode Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType,
367         char cfgData[CFG_DATA_MAX_BYTES], int setDataValidLen) override;
368 
369     /**
370      * @Description Set the scene of upper layer
371      *
372      * @param ifName - interface name
373      * @param scene - scene
374      * @return ErrCode - operate result
375      */
376     ErrCode Hid2dSetUpperScene(const std::string& ifName, const Hid2dUpperScene& scene) override;
377 
378     ErrCode DiscoverPeers(int32_t channelid) override;
379 
380     ErrCode DisableRandomMac(int setmode) override;
381 
382     /**
383      * @Description Check can use P2P
384      *
385      * @return ErrCode - operation result
386      */
387     ErrCode CheckCanUseP2p() override;
388 
389     /**
390     * @Description Handle remote object died event.
391     * @param remoteObject remote object.
392     */
393     void OnRemoteDied(const wptr<IRemoteObject> &remoteObject);
394 
395     /**
396      * @Description Check whether service is died.
397      *
398      * @return bool - true: service is died, false: service is not died.
399      */
400     bool IsRemoteDied(void) override;
401 
402     /**
403      * @Description Hid2d Is Wide Bandwidth Supported
404      *
405      * @param isSupport - is support 160M
406      * @return ErrCode - operation result
407      */
408     ErrCode Hid2dIsWideBandwidthSupported(bool &isSupport) override;
409 
410 private:
411     class WifiDeathRecipient : public IRemoteObject::DeathRecipient {
412     public:
WifiDeathRecipient(WifiP2pProxy & client)413         explicit WifiDeathRecipient(WifiP2pProxy &client) : client_(client) {}
414         ~WifiDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)415         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
416         {
417             client_.OnRemoteDied(remote);
418         }
419 
420     private:
421         WifiP2pProxy &client_;
422     };
423 
424     void RemoveDeathRecipient(void);
425     void ReadWifiP2pServiceInfo(MessageParcel &reply, WifiP2pServiceInfo &info) const;
426     void WriteWifiP2pServiceInfo(MessageParcel &data, const WifiP2pServiceInfo &info) const;
427     void WriteWifiP2pServiceRequest(
428         MessageParcel &data, const WifiP2pDevice &device, const WifiP2pServiceRequest &request) const;
429     void WriteWifiP2pDeviceData(MessageParcel &data, const WifiP2pDevice &device) const;
430     void ReadWifiP2pDeviceData(MessageParcel &reply, WifiP2pDevice &device) const;
431     void WriteWifiP2pGroupData(MessageParcel &data, const WifiP2pGroupInfo &info) const;
432     void ReadWifiP2pGroupData(MessageParcel &reply, WifiP2pGroupInfo &info) const;
433     void WriteWifiP2pConfigData(MessageParcel &data, const WifiP2pConfig &config) const;
434 
435 private:
436     std::atomic<bool> mRemoteDied;
437     static BrokerDelegator<WifiP2pProxy> delegator;
438     sptr<IRemoteObject> remote_ = nullptr;
439     std::mutex mutex_;
440     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
441 };
442 }  // namespace Wifi
443 }  // namespace OHOS
444 #endif