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 /// WiFiDirectPeer class is used to handle the connection with remote devices using WiFi Direct.
29 /// <privilege> http://tizen.org/privilege/wifidirect </privilege>
30 public class WiFiDirectPeer
32 private event EventHandler<ConnectionStateChangedEventArgs> _connectionStateChanged;
33 private event EventHandler<IpAddressAssignedEventArgs> _ipAddressAssigned;
34 private event EventHandler<ServiceStateChangedEventArgs> _serviceStateChanged;
36 private Interop.WiFiDirect.ConnectionStateChangedCallback _connectionStateChangedCallback;
37 private Interop.WiFiDirect.ClientIpAddressAssignedCallback _ipAddressAssignedCallback;
38 private Interop.WiFiDirect.ServiceStateChangedCallback _serviceStateChangedCallback;
40 internal string _peerDeviceName;
41 internal string _peerIpAddress;
42 internal string _peerMacAddress;
43 internal string _peerInterfaceAddress;
44 internal int _peerChannel;
45 internal bool _isPeerConnected;
46 internal bool _isPeerGroupOwner;
47 internal bool _isPeerPersistentGroupOwner;
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 /// Name of Peer device.
68 return _peerDeviceName;
73 /// Ip address of the peer device.
75 public string IpAddress
79 return _peerIpAddress;
84 /// Mac address of the peer device.
86 public string MacAddress
90 return _peerMacAddress;
95 /// Interface address of the peer device.
97 public string InterfaceAddress
101 return _peerInterfaceAddress;
106 /// Listening channel of the peer device.
117 /// Connected state of the peer device.
119 public bool IsConnected
123 return _isPeerConnected;
128 /// P2P group state of the peer device.
130 public bool IsGroupOwner
134 return _isPeerGroupOwner;
139 /// Persistent group state of the peer device.
141 public bool IsPersistentGroupOwner
145 return _isPeerPersistentGroupOwner;
150 /// P2P state of the peer device.
152 public bool P2PSupport
156 return _peerP2PSupport;
161 /// Primary catagory of the peer device.
163 public WiFiDirectPrimaryDeviceType PrimaryType
167 return _peerPrimaryType;
172 /// Sub category of the peer device.
174 public WiFiDirectSecondaryDeviceType SecondaryType
178 return _peerSecondaryType;
183 /// List of supported WPS type of the peer device.
189 return _peerWpsTypes;
194 /// P2P invitation state of the peer device.
196 public bool IsP2PInvitationSupported
200 return _p2PInvitationSupported;
205 /// Number of registered services of the peer device.
207 public uint ServiceCount
211 return _peerServiceCount;
216 /// List of registered services of the peer device.
218 public IEnumerable<string> ServiceList
222 return _peerServiceList;
227 /// Checks if peer device is a wifi display device.
229 public bool IsMiracastDevice
233 return _isPeerMiracastDevice;
238 /// Wi-Fi Display device type of the peer device.
241 /// Wi-Fi Direct must be activated.
242 /// If there is any error, default value of WiFiDirectDisplayType will be returned.
244 public WiFiDirectDisplayType Display
248 if (Globals.IsActivated)
250 WiFiDirectDisplayType displayType;
251 int ret = Interop.WiFiDirect.GetDisplayType(_peerMacAddress, out displayType);
252 if (ret != (int)WiFiDirectError.None)
254 Log.Error(Globals.LogTag, "Failed to get the peer display type, Error - " + (WiFiDirectError)ret);
262 return default(WiFiDirectDisplayType);
268 /// Wi-Fi Display Session Availability of the peer device.
271 /// Wi-Fi Direct must be activated.
272 /// If there is any error, false will be returned.
274 public bool DisplayAvailability
278 if (Globals.IsActivated)
280 bool displayAvailability;
281 int ret = Interop.WiFiDirect.GetDisplayAvailability(_peerMacAddress, out displayAvailability);
282 if (ret != (int)WiFiDirectError.None)
284 Log.Error(Globals.LogTag, "Failed to get the peer display availability, Error - " + (WiFiDirectError)ret);
287 return displayAvailability;
298 /// Hdcp information of the peer device.
301 /// Wi-Fi Direct must be activated.
302 /// If there is any error, -1 will be returned.
308 if (Globals.IsActivated)
311 int ret = Interop.WiFiDirect.GetDisplayHdcp(_peerMacAddress, out hdcpSupport);
312 if (ret != (int)WiFiDirectError.None)
314 Log.Error(Globals.LogTag, "Failed to get the peer display hdcp support, Error - " + (WiFiDirectError)ret);
329 /// Port of the connected peer device.
332 /// Wi-Fi Direct must be activated.
333 /// If there is any error, -1 will be returned.
339 if (Globals.IsActivated)
342 int ret = Interop.WiFiDirect.GetDisplayPort(_peerMacAddress, out displayPort);
343 if (ret != (int)WiFiDirectError.None)
345 Log.Error(Globals.LogTag, "Failed to get the peer display port, Error - " + (WiFiDirectError)ret);
360 /// WiFi Display max throughput of the peer device.
363 /// Wi-Fi Direct must be activated.
364 /// If there is any error, -1 will be returned.
366 public int Throughput
370 if (Globals.IsActivated)
372 int displayThroughput;
373 int ret = Interop.WiFiDirect.GetDisplayThroughput(_peerMacAddress, out displayThroughput);
374 if (ret != (int)WiFiDirectError.None)
376 Log.Error(Globals.LogTag, "Failed to get the peer display max throughput, Error - " + (WiFiDirectError)ret);
380 return displayThroughput;
391 /// (event) ConnectionStateChanged event is raised when the connection state of the peer device changes.
393 public event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
397 if (Globals.IsInitialize)
399 if (_connectionStateChanged == null)
401 RegisterConnectionStateChangedEvent();
404 _connectionStateChanged += value;
410 if (Globals.IsInitialize)
412 _connectionStateChanged -= value;
413 if (_connectionStateChanged == null)
415 UnregisterConnectionStateChangedEvent();
422 /// (event) IpAddressAssigned event is raised when ip address of the peer device is assigned.
424 public event EventHandler<IpAddressAssignedEventArgs> IpAddressAssigned
428 if (Globals.IsInitialize)
430 if (_ipAddressAssigned == null)
432 RegisterIpAddressAssignedEvent();
435 _ipAddressAssigned += value;
441 if (Globals.IsInitialize)
443 _ipAddressAssigned -= value;
444 if (_ipAddressAssigned == null)
446 UnregisterIpAddressAssignedEvent();
453 /// (event) ServiceStateChanged is raised when state of service discovery is changed.
455 public event EventHandler<ServiceStateChangedEventArgs> ServiceStateChanged
459 if (Globals.IsInitialize)
461 if (_serviceStateChanged == null)
463 RegisterServiceStateChangedEvent();
466 _serviceStateChanged += value;
472 if (Globals.IsInitialize)
474 _serviceStateChanged -= value;
475 if (_serviceStateChanged == null)
477 UnregisterServiceStateChangedEvent();
483 private void RegisterConnectionStateChangedEvent()
485 _connectionStateChangedCallback = (int result, WiFiDirectConnectionState state, string address, IntPtr userData) =>
487 if (_connectionStateChanged != null)
489 WiFiDirectError res = (WiFiDirectError)result;
490 _connectionStateChanged(null, new ConnectionStateChangedEventArgs(res, state, address));
493 int ret = Interop.WiFiDirect.SetConnectionChangedCallback(_connectionStateChangedCallback, IntPtr.Zero);
494 if (ret != (int)WiFiDirectError.None)
496 Log.Error(Globals.LogTag, "Failed to set connection state changed callback, Error - " + (WiFiDirectError)ret);
500 private void UnregisterConnectionStateChangedEvent()
502 int ret = Interop.WiFiDirect.UnsetConnectionChangedCallback();
503 if (ret != (int)WiFiDirectError.None)
505 Log.Error(Globals.LogTag, "Failed to unset connection state changed callback, Error - " + (WiFiDirectError)ret);
509 private void RegisterIpAddressAssignedEvent()
511 _ipAddressAssignedCallback = (string macAddress, string ipAddress, string interfaceAddress, IntPtr userData) =>
513 if (_ipAddressAssigned != null)
515 _ipAddressAssigned(null, new IpAddressAssignedEventArgs(macAddress, ipAddress, interfaceAddress));
518 int ret = Interop.WiFiDirect.SetIpAddressAssignedCallback(_ipAddressAssignedCallback, IntPtr.Zero);
519 if (ret != (int)WiFiDirectError.None)
521 Log.Error(Globals.LogTag, "Failed to set ip address assigned callback, Error - " + (WiFiDirectError)ret);
525 private void UnregisterIpAddressAssignedEvent()
527 int ret = Interop.WiFiDirect.UnsetIpAddressAssignedCallback();
528 if (ret != (int)WiFiDirectError.None)
530 Log.Error(Globals.LogTag, "Failed to unset ip address assigned callback, Error - " + (WiFiDirectError)ret);
534 private void RegisterServiceStateChangedEvent()
536 _serviceStateChangedCallback = (int result, WiFiDirectServiceDiscoveryState stateInfo, WiFiDirectServiceType typeInfo, IntPtr responseData, string address, IntPtr userData) =>
538 if (_serviceStateChanged != null)
540 WiFiDirectError error = (WiFiDirectError)result;
541 WiFiDirectServiceDiscoveryState state = stateInfo;
542 WiFiDirectServiceType type = typeInfo;
543 string response = Marshal.PtrToStringAnsi(responseData);
545 Interop.WiFiDirect.GetDiscoveredPeerInfo(address, out peer);
546 DiscoveredPeerStruct peerStruct = (DiscoveredPeerStruct)Marshal.PtrToStructure(peer, typeof(DiscoveredPeerStruct));
548 _serviceStateChanged(null, new ServiceStateChangedEventArgs(error, state, type, response, WiFiDirectUtils.ConvertStructToDiscoveredPeer(peerStruct)));
551 int ret = Interop.WiFiDirect.SetServiceStateChangedCallback(_serviceStateChangedCallback, IntPtr.Zero);
552 if (ret != (int)WiFiDirectError.None)
554 Log.Error(Globals.LogTag, "Failed to set service state changed callback, Error - " + (WiFiDirectError)ret);
558 private void UnregisterServiceStateChangedEvent()
560 int ret = Interop.WiFiDirect.UnsetServiceStateChangedCallback();
561 if (ret != (int)WiFiDirectError.None)
563 Log.Error(Globals.LogTag, "Failed to unset service state changed callback, Error - " + (WiFiDirectError)ret);
568 /// Connects to a specified remote device.
571 /// Wi-Fi Direct must be activated.
572 /// If this succeeds, ConnectionStateChanged event will be invoked.
574 /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception>
575 public void Connect()
577 if (Globals.IsActivated)
579 int ret = Interop.WiFiDirect.Connect(_peerMacAddress);
580 if (ret != (int)WiFiDirectError.None)
582 Log.Error(Globals.LogTag, "Failed to connect, Error - " + (WiFiDirectError)ret);
583 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
589 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
590 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
595 /// Cancels the connection now in progress.
598 /// Wi-Fi Direct must be activated.
600 /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception>
601 public void CancelConnection()
603 if (Globals.IsActivated)
605 int ret = Interop.WiFiDirect.CancelConnection(_peerMacAddress);
606 if (ret != (int)WiFiDirectError.None)
608 Log.Error(Globals.LogTag, "Failed to cancel the connection, Error - " + (WiFiDirectError)ret);
609 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
615 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
616 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
621 /// Disconnects the specified remote device.
624 /// Wi-Fi Direct must be activated.
625 /// If this succeeds, ConnectionStateChanged event will be invoked.
627 /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception>
628 public void Disconnect()
630 if (Globals.IsActivated)
632 int ret = Interop.WiFiDirect.Disconnect(_peerMacAddress);
633 if (ret != (int)WiFiDirectError.None)
635 Log.Error(Globals.LogTag, "Failed to disconnect, Error - " + (WiFiDirectError)ret);
636 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
642 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
643 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
648 /// Allows a device to connect automatically.
651 /// Wi-Fi Direct must be activated.
653 /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception>
654 public void SetAutoConnect()
656 if (Globals.IsActivated)
658 int ret = Interop.WiFiDirect.SetAutoConnectionPeer(_peerMacAddress);
659 if (ret != (int)WiFiDirectError.None)
661 Log.Error(Globals.LogTag, "Failed to set auto connection, Error - " + (WiFiDirectError)ret);
662 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
668 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
669 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
674 /// Starts the Wi-Fi Direct service discovery.
677 /// Wi-Fi Direct must be activated.
678 /// If this succeeds, ServiceStateChanged event will be invoked.
680 /// <exception cref="NotSupportedException">
681 /// Thrown during one of the following cases :
682 /// 1. When the wifidirect is not supported
683 /// 2. When the wifidirect service discovery is not supported
685 /// <param name="type">Type of service.</param>
686 public void StartServiceDiscovery(WiFiDirectServiceType type)
688 if (Globals.IsActivated)
690 int ret = Interop.WiFiDirect.StartServiceDiscovery(_peerMacAddress, type);
691 if (ret != (int)WiFiDirectError.None)
693 Log.Error(Globals.LogTag, "Failed to start Wi-Fi Direct service discovery, Error - " + (WiFiDirectError)ret);
694 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
700 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
701 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);
706 /// Stops the Wi-Fi Direct service discovery.
709 /// Wi-Fi Direct must be activated.
711 /// <exception cref="NotSupportedException">
712 /// Thrown during one of the following cases :
713 /// 1. When the wifidirect is not supported
714 /// 2. When the wifidirect service discovery is not supported
716 /// <param name="type">Type of service.</param>
717 public void CancelServiceDiscovery(WiFiDirectServiceType type)
719 if (Globals.IsActivated)
721 int ret = Interop.WiFiDirect.StopServiceDiscovery(_peerMacAddress, type);
722 if (ret != (int)WiFiDirectError.None)
724 Log.Error(Globals.LogTag, "Failed to stop Wi-Fi Direct service discovery, Error - " + (WiFiDirectError)ret);
725 WiFiDirectErrorFactory.ThrowWiFiDirectException(ret);
731 Log.Error(Globals.LogTag, "Wifi-direct is not activated");
732 WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted);