2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Collections.Concurrent;
21 using System.Collections.ObjectModel;
22 using System.Collections.Specialized;
24 namespace Tizen.Network.WiFiDirect
27 /// The WiFiDirectPeer class is used to handle the connection with remote devices using Wi-Fi Direct.
29 /// <privilege> http://tizen.org/privilege/wifidirect </privilege>
30 /// <since_tizen> 3 </since_tizen>
31 public class WiFiDirectPeer
33 private event EventHandler<ConnectionStateChangedEventArgs> _connectionStateChanged;
34 private event EventHandler<IpAddressAssignedEventArgs> _ipAddressAssigned;
35 private event EventHandler<ServiceStateChangedEventArgs> _serviceStateChanged;
37 private Interop.WiFiDirect.ConnectionStateChangedCallback _connectionStateChangedCallback;
38 private Interop.WiFiDirect.ClientIpAddressAssignedCallback _ipAddressAssignedCallback;
39 private Interop.WiFiDirect.ServiceStateChangedCallback _serviceStateChangedCallback;
41 internal string _peerDeviceName;
42 internal string _peerIpAddress;
43 internal string _peerMacAddress;
44 internal string _peerInterfaceAddress;
45 internal int _peerChannel;
46 internal bool _isPeerConnected;
47 internal bool _isPeerGroupOwner;
48 internal bool _peerP2PSupport;
49 internal WiFiDirectPrimaryDeviceType _peerPrimaryType;
50 internal WiFiDirectSecondaryDeviceType _peerSecondaryType;
51 internal int _peerWpsTypes;
52 internal bool _p2PInvitationSupported;
53 internal uint _peerServiceCount;
54 internal IEnumerable<string> _peerServiceList;
55 internal bool _isPeerMiracastDevice;
57 internal WiFiDirectPeer()
62 /// The name of the peer device.
64 /// <since_tizen> 3 </since_tizen>
69 return _peerDeviceName;
74 /// The IP address of the peer device.
76 /// <since_tizen> 3 </since_tizen>
77 public string IpAddress
81 return _peerIpAddress;
86 /// The MAC address of the peer device.
88 /// <since_tizen> 3 </since_tizen>
89 public string MacAddress
93 return _peerMacAddress;
98 /// The Interface address of the peer device.
100 /// <since_tizen> 3 </since_tizen>
101 public string InterfaceAddress
105 return _peerInterfaceAddress;
110 /// The listening channel of the peer device.
112 /// <since_tizen> 3 </since_tizen>
122 /// The connected state of the peer device.
124 /// <since_tizen> 3 </since_tizen>
125 public bool IsConnected
129 return _isPeerConnected;
134 /// The P2P group state of the peer device.
136 /// <since_tizen> 3 </since_tizen>
137 public bool IsGroupOwner
141 return _isPeerGroupOwner;
146 /// The P2P state of the peer device.
148 /// <since_tizen> 3 </since_tizen>
149 public bool P2PSupport
153 return _peerP2PSupport;
158 /// The primary catagory of the peer device.
160 /// <since_tizen> 3 </since_tizen>
161 public WiFiDirectPrimaryDeviceType PrimaryType
165 return _peerPrimaryType;
170 /// The sub category of the peer device.
172 /// <since_tizen> 3 </since_tizen>
173 public WiFiDirectSecondaryDeviceType SecondaryType
177 return _peerSecondaryType;
182 /// The list of the supported WPS type of the peer device.
184 /// <since_tizen> 3 </since_tizen>
189 return _peerWpsTypes;
194 /// The P2P invitation state of the peer device.
196 /// <since_tizen> 3 </since_tizen>
197 public bool IsP2PInvitationSupported
201 return _p2PInvitationSupported;
206 /// The number of registered services of the peer device.
208 /// <since_tizen> 3 </since_tizen>
209 public uint ServiceCount
213 return _peerServiceCount;
218 /// The list of registered services of the peer device.
220 /// <since_tizen> 3 </since_tizen>
221 public IEnumerable<string> ServiceList
225 return _peerServiceList;
230 /// Checks if the peer device is a Wi-Fi display device.
232 /// <since_tizen> 3 </since_tizen>
233 public bool IsMiracastDevice
237 return _isPeerMiracastDevice;
242 /// The Wi-Fi display device type of the peer device.
245 /// Wi-Fi Direct must be activated.
246 /// If there is any error, a default value of WiFiDirectDisplayType will be returned.
248 /// <since_tizen> 3 </since_tizen>
249 public WiFiDirectDisplayType Display
253 if (Globals.IsActivated)
255 WiFiDirectDisplayType displayType;
256 int ret = Interop.WiFiDirect.GetDisplayType(_peerMacAddress, out displayType);
257 if (ret != (int)WiFiDirectError.None)
259 Log.Error(Globals.LogTag, "Failed to get the peer display type, Error - " + (WiFiDirectError)ret);
267 return default(WiFiDirectDisplayType);
273 /// The Wi-Fi display session availability of the peer device.
276 /// Wi-Fi Direct must be activated.
277 /// If there is any error, false will be returned.
279 /// <since_tizen> 3 </since_tizen>
280 public bool DisplayAvailability
284 if (Globals.IsActivated)
286 bool displayAvailability;
287 int ret = Interop.WiFiDirect.GetDisplayAvailability(_peerMacAddress, out displayAvailability);
288 if (ret != (int)WiFiDirectError.None)
290 Log.Error(Globals.LogTag, "Failed to get the peer display availability, Error - " + (WiFiDirectError)ret);
293 return displayAvailability;
304 /// The HDCP information of the peer device.
307 /// Wi-Fi Direct must be activated.
308 /// If there is any error, -1 will be returned.
310 /// <since_tizen> 3 </since_tizen>
315 if (Globals.IsActivated)
318 int ret = Interop.WiFiDirect.GetDisplayHdcp(_peerMacAddress, out hdcpSupport);
319 if (ret != (int)WiFiDirectError.None)
321 Log.Error(Globals.LogTag, "Failed to get the peer display hdcp support, Error - " + (WiFiDirectError)ret);
336 /// The port of the connected peer device.
339 /// Wi-Fi Direct must be activated.
340 /// If there is any error, -1 will be returned.
342 /// <since_tizen> 3 </since_tizen>
347 if (Globals.IsActivated)
350 int ret = Interop.WiFiDirect.GetDisplayPort(_peerMacAddress, out displayPort);
351 if (ret != (int)WiFiDirectError.None)
353 Log.Error(Globals.LogTag, "Failed to get the peer display port, Error - " + (WiFiDirectError)ret);
368 /// The Wi-Fi display maximum throughput of the peer device.
371 /// Wi-Fi Direct must be activated.
372 /// If there is any error, -1 will be returned.
374 /// <since_tizen> 3 </since_tizen>
375 public int Throughput
379 if (Globals.IsActivated)
381 int displayThroughput;
382 int ret = Interop.WiFiDirect.GetDisplayThroughput(_peerMacAddress, out displayThroughput);
383 if (ret != (int)WiFiDirectError.None)
385 Log.Error(Globals.LogTag, "Failed to get the peer display max throughput, Error - " + (WiFiDirectError)ret);
389 return displayThroughput;
400 /// The Wi-Fi RSSI value of the peer device.
403 /// Wi-Fi Direct must be activated.
404 /// If there is any error, -1 will be returned.
406 /// <since_tizen> 3 </since_tizen>
411 if (Globals.IsActivated)
414 int ret = Interop.WiFiDirect.GetRssi(_peerMacAddress, out rssi);
415 if (ret != (int)WiFiDirectError.None)
417 Log.Error(Globals.LogTag, "Failed to get the peer RSSI, Error - " + (WiFiDirectError)ret);
431 /// The ConnectionStateChanged event is raised when the connection state of the peer device changes.
433 /// <since_tizen> 3 </since_tizen>
434 public event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
438 if (Globals.IsInitialize)
440 if (_connectionStateChanged == null)
442 RegisterConnectionStateChangedEvent();
445 _connectionStateChanged += value;
451 if (Globals.IsInitialize)
453 _connectionStateChanged -= value;
454 if (_connectionStateChanged == null)
456 UnregisterConnectionStateChangedEvent();
463 /// The IpAddressAssigned event is raised when the IP address of the peer device is assigned.
465 /// <since_tizen> 3 </since_tizen>
466 public event EventHandler<IpAddressAssignedEventArgs> IpAddressAssigned
470 if (Globals.IsInitialize)
472 if (_ipAddressAssigned == null)
474 RegisterIpAddressAssignedEvent();
477 _ipAddressAssigned += value;
483 if (Globals.IsInitialize)
485 _ipAddressAssigned -= value;
486 if (_ipAddressAssigned == null)
488 UnregisterIpAddressAssignedEvent();
495 /// The ServiceStateChanged event is raised when the state of service discovery is changed.
497 /// <since_tizen> 3 </since_tizen>
498 public event EventHandler<ServiceStateChangedEventArgs> ServiceStateChanged
502 if (Globals.IsInitialize)
504 if (_serviceStateChanged == null)
506 RegisterServiceStateChangedEvent();
509 _serviceStateChanged += value;
515 if (Globals.IsInitialize)
517 _serviceStateChanged -= value;
518 if (_serviceStateChanged == null)
520 UnregisterServiceStateChangedEvent();
526 private void RegisterConnectionStateChangedEvent()
528 _connectionStateChangedCallback = (int result, WiFiDirectConnectionState state, string address, IntPtr userData) =>
530 if (_connectionStateChanged != null)
532 WiFiDirectError res = (WiFiDirectError)result;
533 _connectionStateChanged(null, new ConnectionStateChangedEventArgs(res, state, address));
536 int ret = Interop.WiFiDirect.SetConnectionChangedCallback(_connectionStateChangedCallback, IntPtr.Zero);
537 if (ret != (int)WiFiDirectError.None)
539 Log.Error(Globals.LogTag, "Failed to set connection state changed callback, Error - " + (WiFiDirectError)ret);
543 private void UnregisterConnectionStateChangedEvent()
545 int ret = Interop.WiFiDirect.UnsetConnectionChangedCallback();
546 if (ret != (int)WiFiDirectError.None)
548 Log.Error(Globals.LogTag, "Failed to unset connection state changed callback, Error - " + (WiFiDirectError)ret);
552 private void RegisterIpAddressAssignedEvent()
554 _ipAddressAssignedCallback = (string macAddress, string ipAddress, string interfaceAddress, IntPtr userData) =>
556 if (_ipAddressAssigned != null)
558 _ipAddressAssigned(null, new IpAddressAssignedEventArgs(macAddress, ipAddress, interfaceAddress));
561 int ret = Interop.WiFiDirect.SetIpAddressAssignedCallback(_ipAddressAssignedCallback, IntPtr.Zero);
562 if (ret != (int)WiFiDirectError.None)
564 Log.Error(Globals.LogTag, "Failed to set ip address assigned callback, Error - " + (WiFiDirectError)ret);
568 private void UnregisterIpAddressAssignedEvent()
570 int ret = Interop.WiFiDirect.UnsetIpAddressAssignedCallback();
571 if (ret != (int)WiFiDirectError.None)
573 Log.Error(Globals.LogTag, "Failed to unset ip address assigned callback, Error - " + (WiFiDirectError)ret);
577 private void RegisterServiceStateChangedEvent()
579 _serviceStateChangedCallback = (int result, WiFiDirectServiceDiscoveryState stateInfo, WiFiDirectServiceType typeInfo, IntPtr responseData, string address, IntPtr userData) =>
581 if (_serviceStateChanged != null)
583 WiFiDirectError error = (WiFiDirectError)result;
584 WiFiDirectServiceDiscoveryState state = stateInfo;
585 WiFiDirectServiceType type = typeInfo;
586 string response = Marshal.PtrToStringAnsi(responseData);
588 Interop.WiFiDirect.GetDiscoveredPeerInfo(address, out peer);
589 DiscoveredPeerStruct peerStruct = (DiscoveredPeerStruct)Marshal.PtrToStructure(peer, typeof(DiscoveredPeerStruct));
591 _serviceStateChanged(null, new ServiceStateChangedEventArgs(error, state, type, response, WiFiDirectUtils.ConvertStructToDiscoveredPeer(peerStruct)));
594 int ret = Interop.WiFiDirect.SetServiceStateChangedCallback(_serviceStateChangedCallback, IntPtr.Zero);
595 if (ret != (int)WiFiDirectError.None)
597 Log.Error(Globals.LogTag, "Failed to set service state changed callback, Error - " + (WiFiDirectError)ret);
601 private void UnregisterServiceStateChangedEvent()
603 int ret = Interop.WiFiDirect.UnsetServiceStateChangedCallback();
604 if (ret != (int)WiFiDirectError.None)
606 Log.Error(Globals.LogTag, "Failed to unset service state changed callback, Error - " + (WiFiDirectError)ret);
611 /// Connects to a specified remote device.
614 /// Wi-Fi Direct must be activated.
615 /// If this succeeds, the ConnectionStateChanged event will be invoked.
617 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi Direct is not supported</exception>
618 /// <since_tizen> 3 </since_tizen>
619 public void Connect()
621 if (Globals.IsActivated)
623 int ret = Interop.WiFiDirect.Connect(_peerMacAddress);
624 if (ret != (int)WiFiDirectError.None)
626 Log.Error(Globals.LogTag, "Failed to connect, Error - " + (WiFiDirectError)ret);
627 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
633 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
634 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
639 /// Cancels the connection now in progress.
642 /// Wi-Fi Direct must be activated.
644 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi Direct is not supported.</exception>
645 /// <since_tizen> 3 </since_tizen>
646 public void CancelConnection()
648 if (Globals.IsActivated)
650 int ret = Interop.WiFiDirect.CancelConnection(_peerMacAddress);
651 if (ret != (int)WiFiDirectError.None)
653 Log.Error(Globals.LogTag, "Failed to cancel the connection, Error - " + (WiFiDirectError)ret);
654 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
660 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
661 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
666 /// Disconnects the specified remote device.
669 /// Wi-Fi Direct must be activated.
670 /// If this succeeds, the ConnectionStateChanged event will be invoked.
672 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi Direct is not supported.</exception>
673 /// <since_tizen> 3 </since_tizen>
674 public void Disconnect()
676 if (Globals.IsActivated)
678 int ret = Interop.WiFiDirect.Disconnect(_peerMacAddress);
679 if (ret != (int)WiFiDirectError.None)
681 Log.Error(Globals.LogTag, "Failed to disconnect, Error - " + (WiFiDirectError)ret);
682 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
688 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
689 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
694 /// Allows a device to connect automatically.
697 /// Wi-Fi Direct must be activated.
699 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi Direct is not supported.</exception>
700 /// <since_tizen> 3 </since_tizen>
701 public void SetAutoConnect()
703 if (Globals.IsActivated)
705 int ret = Interop.WiFiDirect.SetAutoConnectionPeer(_peerMacAddress);
706 if (ret != (int)WiFiDirectError.None)
708 Log.Error(Globals.LogTag, "Failed to set auto connection, Error - " + (WiFiDirectError)ret);
709 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
715 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
716 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
721 /// Starts the Wi-Fi Direct service discovery.
724 /// Wi-Fi Direct must be activated.
725 /// If this succeeds, the ServiceStateChanged event will be invoked.
727 /// <exception cref="NotSupportedException">
728 /// Thrown during one of the following cases:
729 /// 1. When the Wi-Fi Direct is not supported.
730 /// 2. When the Wi-Fi Direct service discovery is not supported.
732 /// <param name="type">The type of the service.</param>
733 /// <since_tizen> 3 </since_tizen>
734 public void StartServiceDiscovery(WiFiDirectServiceType type)
736 if (Globals.IsActivated)
738 int ret = Interop.WiFiDirect.StartServiceDiscovery(_peerMacAddress, type);
739 if (ret != (int)WiFiDirectError.None)
741 Log.Error(Globals.LogTag, "Failed to start Wi-Fi Direct service discovery, Error - " + (WiFiDirectError)ret);
742 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
748 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
749 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
754 /// Stops the Wi-Fi Direct service discovery.
757 /// Wi-Fi Direct must be activated.
759 /// <exception cref="NotSupportedException">
760 /// Thrown during one of the following cases:
761 /// 1. When the Wi-Fi Direct is not supported.
762 /// 2. When the Wi-Fi Direct service discovery is not supported.
764 /// <param name="type">The type of the service.</param>
765 /// <since_tizen> 3 </since_tizen>
766 public void CancelServiceDiscovery(WiFiDirectServiceType type)
768 if (Globals.IsActivated)
770 int ret = Interop.WiFiDirect.StopServiceDiscovery(_peerMacAddress, type);
771 if (ret != (int)WiFiDirectError.None)
773 Log.Error(Globals.LogTag, "Failed to stop Wi-Fi Direct service discovery, Error - " + (WiFiDirectError)ret);
774 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
780 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
781 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);