diff options
Diffstat (limited to 'Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect')
8 files changed, 4534 insertions, 0 deletions
diff --git a/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectData.cs b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectData.cs new file mode 100644 index 0000000..10e2862 --- /dev/null +++ b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectData.cs @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; +using System.Collections.ObjectModel; + +namespace Tizen.Network.WiFiDirect +{ + [StructLayout(LayoutKind.Sequential)] + internal struct DiscoveredPeerStruct + { + [MarshalAsAttribute(UnmanagedType.LPStr)] + internal string _name; + + [MarshalAsAttribute(UnmanagedType.LPStr)] + internal string _macAddress; + + [MarshalAsAttribute(UnmanagedType.LPStr)] + internal string _interfaceAddress; + + internal int _channel; + + [MarshalAsAttribute(UnmanagedType.I1)] + internal bool _isConnected; + + [MarshalAsAttribute(UnmanagedType.I1)] + internal bool _isGroupOwner; + + [MarshalAsAttribute(UnmanagedType.I1)] + internal bool _isPersistentGroupOwner; + + internal WiFiDirectPrimaryDeviceType _primaryType; + + internal WiFiDirectSecondaryDeviceType _secondaryType; + + internal int _wpsTypes; + + [MarshalAsAttribute(UnmanagedType.I1)] + internal bool _isP2PInvitationSupported; + + internal uint _serviceCount; + + internal IntPtr _serviceList; + + [MarshalAsAttribute(UnmanagedType.I1)] + internal bool _isMiracast; + } + + [StructLayout(LayoutKind.Sequential)] + internal struct ConnectedPeerStruct + { + [MarshalAsAttribute(UnmanagedType.LPStr)] + internal string _name; + + [MarshalAsAttribute(UnmanagedType.LPStr)] + internal string _ipAddress; + + [MarshalAsAttribute(UnmanagedType.LPStr)] + internal string _macAddress; + + [MarshalAsAttribute(UnmanagedType.LPStr)] + internal string _interfaceAddress; + + internal int _channel; + + [MarshalAsAttribute(UnmanagedType.I1)] + internal bool _isP2PSupport; + + internal WiFiDirectPrimaryDeviceType _primaryType; + + internal WiFiDirectSecondaryDeviceType _secondaryType; + + internal uint _serviceCount; + + internal IntPtr _serviceList; + + [MarshalAsAttribute(UnmanagedType.I1)] + internal bool _isMiracast; + } + + internal static class WiFiDirectUtils + { + internal static WiFiDirectPeer ConvertStructToDiscoveredPeer(DiscoveredPeerStruct peer) + { + WiFiDirectPeer resultPeer = new WiFiDirectPeer(); + resultPeer._peerDeviceName = peer._name; + resultPeer._peerMacAddress = peer._macAddress; + resultPeer._peerInterfaceAddress = peer._interfaceAddress; + resultPeer._peerChannel = peer._channel; + resultPeer._isPeerConnected = peer._isConnected; + resultPeer._isPeerGroupOwner = peer._isGroupOwner; + resultPeer._isPeerPersistentGroupOwner = peer._isPersistentGroupOwner; + resultPeer._peerPrimaryType = peer._primaryType; + resultPeer._peerSecondaryType = peer._secondaryType; + resultPeer._peerWpsTypes = peer._wpsTypes; + resultPeer._p2PInvitationSupported = peer._isP2PInvitationSupported; + Collection<string> uuidList = null; + + if (peer._serviceCount > 0) + { + IntPtr[] serviceList = new IntPtr[peer._serviceCount]; + Marshal.Copy(peer._serviceList, serviceList, 0, (int)peer._serviceCount); + uuidList = new Collection<string>(); + foreach (IntPtr service in serviceList) + { + string registeredService = Marshal.PtrToStringAnsi(service); + uuidList.Add(registeredService); + } + + resultPeer._peerServiceCount = peer._serviceCount; + resultPeer._peerServiceList = uuidList; + } + + resultPeer._isPeerMiracastDevice = peer._isMiracast; + return resultPeer; + } + + internal static WiFiDirectPeer ConvertStructToConnectedPeer(ConnectedPeerStruct peer) + { + WiFiDirectPeer resultPeer = new WiFiDirectPeer(); + resultPeer._peerDeviceName = peer._name; + resultPeer._peerIpAddress = peer._ipAddress; + resultPeer._peerMacAddress = peer._macAddress; + resultPeer._peerInterfaceAddress = peer._interfaceAddress; + resultPeer._peerChannel = peer._channel; + resultPeer._peerP2PSupport = peer._isP2PSupport; + resultPeer._peerPrimaryType = peer._primaryType; + resultPeer._peerSecondaryType = peer._secondaryType; + Collection<string> uuidList = null; + + if (peer._serviceCount > 0) + { + IntPtr[] serviceList = new IntPtr[peer._serviceCount]; + Marshal.Copy(peer._serviceList, serviceList, 0, (int)peer._serviceCount); + uuidList = new Collection<string>(); + foreach (IntPtr service in serviceList) + { + string registeredService = Marshal.PtrToStringAnsi(service); + uuidList.Add(registeredService); + } + + resultPeer._peerServiceCount = peer._serviceCount; + resultPeer._peerServiceList = uuidList; + } + + resultPeer._isPeerMiracastDevice = peer._isMiracast; + return resultPeer; + } + } + +} diff --git a/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectEnumerations.cs b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectEnumerations.cs new file mode 100644 index 0000000..e15f92e --- /dev/null +++ b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectEnumerations.cs @@ -0,0 +1,651 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using Tizen.Internals.Errors; + +namespace Tizen.Network.WiFiDirect +{ + /// <summary> + /// Enumeration for Wi-Fi Direct discovery state. + /// </summary> + public enum WiFiDirectDiscoveryState + { + /// <summary> + /// Only listen has started. + /// </summary> + OnlyListenStarted = 0, + /// <summary> + /// Discovery started. + /// </summary> + Started, + /// <summary> + /// A remote peer is found. + /// </summary> + Found, + /// <summary> + /// Discovery finished. + /// </summary> + Finished, + /// <summary> + /// A remote peer is lost. + /// </summary> + Lost + } + + /// <summary> + /// Enumeration for Wi-Fi Direct display device type. + /// </summary> + public enum WiFiDirectDisplayType + { + /// <summary> + /// Configure as WFD source. + /// </summary> + Source = 0, + /// <summary> + /// Configure as WFD primary sink. + /// </summary> + Prisink, + /// <summary> + /// Configure as WFD secondary sink. + /// </summary> + Secsink, + /// <summary> + /// Configure as WFD dual role. + /// </summary> + Dual + } + + /// <summary> + /// Enumeration for Wi-Fi Discovery channel. + /// </summary> + public enum WiFiDirectDiscoveryChannel + { + /// <summary> + /// Scan full channel. + /// </summary> + FullScan = 0, + /// <summary> + /// The social channel. + /// </summary> + SocialChannel = 1611, + /// <summary> + /// Scan channel 1. + /// </summary> + Channel1 = 1, + /// <summary> + /// Scan channel 6. + /// </summary> + Channel6 = 6, + /// <summary> + /// Scan channel 11. + /// </summary> + Channel11 = 11 + } + + /// <summary> + /// Enumeration for Wi-Fi Direct connection state. + /// </summary> + public enum WiFiDirectConnectionState + { + /// <summary> + /// Connection is requested. + /// </summary> + ConnectionRequest, + /// <summary> + /// Wps is requested. + /// </summary> + ConnectionWpsRequest, + /// <summary> + /// Connection in progress. + /// </summary> + ConnectionInProgress, + /// <summary> + /// Connected . + /// </summary> + ConnectionRsp, + /// <summary> + /// Disconnected by remote group client. + /// </summary> + DisassociationInd, + /// <summary> + /// Disconnected by local device. + /// </summary> + DisconnectRsp, + /// <summary> + /// Disconnected by remote group owner. + /// </summary> + DisconnectInd, + /// <summary> + /// Group is created. + /// </summary> + GroupCreated, + /// <summary> + /// Group is destroyed. + /// </summary> + GroupDestroyed + } + + /// <summary> + /// Enumeration for Wi-Fi Direct primary device type. + /// </summary> + public enum WiFiDirectPrimaryDeviceType + { + /// <summary> + /// Computer. + /// </summary> + Computer = 1, + /// <summary> + /// Input device. + /// </summary> + InputDevice = 2, + /// <summary> + /// Printer. + /// </summary> + Printer = 3, + /// <summary> + /// Camera. + /// </summary> + Camera = 4, + /// <summary> + /// Storage. + /// </summary> + Storage = 5, + /// <summary> + /// Network Infrastructure. + /// </summary> + NetworkInfrastructure = 6, + /// <summary> + /// Display. + /// </summary> + Display = 7, + /// <summary> + /// Multimedia device. + /// </summary> + MultimediaDevice = 8, + /// <summary> + /// Game device. + /// </summary> + GameDevice = 9, + /// <summary> + /// Telephone. + /// </summary> + Telephone = 10, + /// <summary> + /// Audio. + /// </summary> + Audio = 11, + /// <summary> + /// Others. + /// </summary> + Other = 255 + } + + /// <summary> + /// Enumeration for Wi-Fi Direct secondary device type. + /// </summary> + /// </summary> + public enum WiFiDirectSecondaryDeviceType + { + /// <summary> + /// Computer PC. + /// </summary> + ComputerPc = 1, + /// <summary> + /// Computer server. + /// </summary> + ComputerServer = 2, + /// <summary> + /// Computer media center. + /// </summary> + ComputerMediaCenter = 3, + /// <summary> + /// Computer UMPC. + /// </summary> + ComputerUmpc = 4, + /// <summary> + /// Computer notebook. + /// </summary> + ComputerNotebook = 5, + /// <summary> + /// Computer desktop + /// </summary> + ComputerDesktop = 6, + /// <summary> + /// Computer MID. + /// </summary> + ComputerMid = 7, + /// <summary> + /// Computer netbook. + /// </summary> + ComputerNetbook = 8, + /// <summary> + /// Input keyboard. + /// </summary> + InputKeyboard = 1, + /// <summary> + /// Input mouse. + /// </summary> + InputMouse = 2, + /// <summary> + /// Input joystick. + /// </summary> + InputJoystick = 3, + /// <summary> + /// Input trackball. + /// </summary> + InputTrackball = 4, + /// <summary> + /// Input controller. + /// </summary> + InputController = 5, + /// <summary> + /// Inpute remote. + /// </summary> + InputRemote = 6, + /// <summary> + /// Input touch screen. + /// </summary> + InputTouchScreen = 7, + /// <summary> + /// Input biometric reader. + /// </summary> + InputBiometricReader = 8, + /// <summary> + /// Input barcode reader. + /// </summary> + InputBarcodeReader = 9, + /// <summary> + /// Printer. + /// </summary> + Printer = 1, + /// <summary> + /// Printer scanner. + /// </summary> + PrinterScanner = 2, + /// <summary> + /// Printer fax. + /// </summary> + PrinterFax = 3, + /// <summary> + /// Printer copier. + /// </summary> + PrinterCopier = 4, + /// <summary> + /// Printer all-in-one. + /// </summary> + PrinterAllInOne = 5, + /// <summary> + /// Digital still camera. + /// </summary> + CameraDigital = 1, + /// <summary> + /// Video camera. + /// </summary> + CameraVideo = 2, + /// <summary> + /// Webcam. + /// </summary> + CameraWebcam = 3, + /// <summary> + /// Security camera. + /// </summary> + CameraSecurity = 4, + /// <summary> + /// Storage NAS. + /// </summary> + StorageNas = 1, + /// <summary> + /// Network ap. + /// </summary> + NetworkAp = 1, + /// <summary> + /// Network router. + /// </summary> + NetworkRouter = 2, + /// <summary> + /// Network switch. + /// </summary> + NetworkSwitch = 3, + /// <summary> + /// Network gateway. + /// </summary> + NetworkGateway = 4, + /// <summary> + /// Display tv. + /// </summary> + DisplayTv = 1, + /// <summary> + /// Display picture frame. + /// </summary> + DisplayPicFrame = 2, + /// <summary> + /// Display projector. + /// </summary> + DisplayProjector = 3, + /// <summary> + /// Display monitor. + /// </summary> + DisplayMonitor = 4, + /// <summary> + /// Multimedia DAR. + /// </summary> + MultimediaDar = 1, + /// <summary> + /// Multimedia PVR. + /// </summary> + MultimediaPvr = 2, + /// <summary> + /// Multimedia MCX. + /// </summary> + MultimediaMcx = 3, + /// <summary> + /// Multimedia set-top box. + /// </summary> + MultimediaStb = 4, + /// <summary> + /// Media Server / Media Adapter / Media Extender. + /// </summary> + MultimediaMsMaMe = 5, + /// <summary> + /// Multimedia portable video player. + /// </summary> + MultimediaPvp = 6, + /// <summary> + /// Game xbox. + /// </summary> + GameXbox = 1, + /// <summary> + /// The game xbox 360. + /// </summary> + GameXbox360, + /// <summary> + /// Game play station. + /// </summary> + GamePlayStation = 2, + /// <summary> + /// Game console. + /// </summary> + GameConsole = 3, + /// <summary> + /// Game portable. + /// </summary> + GamePortable = 4, + /// <summary> + /// Windows mobile. + /// </summary> + TelephoneWindowsMobile = 1, + /// <summary> + /// Phone - single mode. + /// </summary> + TelephonePhoneSingle = 2, + /// <summary> + /// Phone - dual mode. + /// </summary> + TelephonePhoneDual = 3, + /// <summary> + /// Smart Phone - single mode. + /// </summary> + TelephoneSmartphoneSingle = 4, + /// <summary> + /// Smart Phone - dual mode. + /// </summary> + TelephoneSmartphoneDual = 5, + /// <summary> + /// Audio tuner. + /// </summary> + AudioTuner = 1, + /// <summary> + /// Audio speaker. + /// </summary> + AudioSpeaker = 2, + /// <summary> + /// Audio pmp. + /// </summary> + AudioPmp = 3, + /// <summary> + /// Audio headset. + /// </summary> + AudioHeadset = 4, + /// <summary> + /// Audio headphone. + /// </summary> + AudioHeadphone = 5, + /// <summary> + /// Audio microphone. + /// </summary> + AudioMic = 6 + } + + /// <summary> + /// Enumeration for Wi-Fi Direct link status. + /// </summary> + /// </summary> + public enum WiFiDirectState + { + /// <summary> + /// Deactivated. + /// </summary> + Deactivated = 0, + /// <summary> + /// Deactivating. + /// </summary> + Deactivating, + /// <summary> + /// Activating. + /// </summary> + Activating, + /// <summary> + /// Activated. + /// </summary> + Activated, + /// <summary> + /// Discovering. + /// </summary> + Discovering, + /// <summary> + /// Connecting. + /// </summary> + Connecting, + /// <summary> + /// Disconnecting. + /// </summary> + Disconnecting, + /// <summary> + /// Connected. + /// </summary> + Connected, + /// <summary> + /// Group owner. + /// </summary> + GroupOwner + } + + /// <summary> + /// Enumeration for Wi-Fi WPS type. + /// </summary> + public enum WiFiDirectWpsType + { + /// <summary> + /// No WPS type. + /// </summary> + None = 0x00, + /// <summary> + /// Push button configuration. + /// </summary> + Pbc = 0x01, + /// <summary> + /// Display pin code. + /// </summary> + PinDisplay = 0x02, + /// <summary> + /// Provide the keypad to input the pin. + /// </summary> + PinKeypad = 0x04 + } + + /// <summary> + /// Enumeration for Service Discovery type. + /// </summary> + public enum WiFiDirectServiceType + { + /// <summary> + /// Service discovery Type all. + /// </summary> + All, + /// <summary> + /// Service discovery Type bonjour. + /// </summary> + Bonjour, + /// <summary> + /// Service discovery Type UPNP. + /// </summary> + Upnp, + /// <summary> + /// Service discovery Type ws discovery. + /// </summary> + WsDiscovery, + /// <summary> + /// Service discovery Type wifi-display. + /// </summary> + WiFiDisplay, + /// <summary> + /// Service discovery Type bt address. + /// </summary> + BtAddress, + /// <summary> + /// Service discovery Type contact info. + /// </summary> + ContactInfo, + /// <summary> + /// Service discovery Type vendor-specific. + /// </summary> + Vendor + } + + /// <summary> + /// Enumeration for Wi-Fi Direct service Discovery state. + /// </summary> + public enum WiFiDirectServiceDiscoveryState + { + /// <summary> + /// Service discovery started. + /// </summary> + Started, + /// <summary> + /// Service discovery found. + /// </summary> + Found, + /// <summary> + /// Service discovery finished. + /// </summary> + Finished + } + + /// <summary> + /// Enumeration for Wi-Fi Direct device state. + /// </summary> + public enum WiFiDirectDeviceState + { + /// <summary> + /// Activated. + /// </summary> + Activated, + /// <summary> + /// Deactivated. + /// </summary> + Deactivated + } + + /// <summary> + /// Enumeration for Wi-Fi Direct error code. + /// </summary> + public enum WiFiDirectError + { + /// <summary> + /// Successful. + /// </summary> + None = ErrorCode.None, + /// <summary> + /// Operation not permitted. + /// </summary> + NotPermitted = ErrorCode.NotPermitted, + /// <summary> + /// Out of memory. + /// </summary> + OutOfMemory = ErrorCode.OutOfMemory, + /// <summary> + /// Permission denied. + /// </summary> + PermissionDenied = ErrorCode.PermissionDenied, + /// <summary> + /// Device or resource busy. + /// </summary> + ResourceBusy = ErrorCode.ResourceBusy, + /// <summary> + /// Invalid function parameter. + /// </summary> + InvalidParameter = ErrorCode.InvalidParameter, + /// <summary> + /// Connection timed out. + /// </summary> + ConnectionTimeOut = ErrorCode.ConnectionTimeout, + /// <summary> + /// Not supported. + /// </summary> + NotSupported = ErrorCode.NotSupported, + /// <summary> + /// Not initialized. + /// </summary> + NotInitialized = -0x01C60000 | 0x01, + /// <summary> + /// I/O error. + /// </summary> + CommunicationFailed = -0x01C60000 | 0x02, + /// <summary> + /// WiFi is being used. + /// </summary> + WiFiUsed = -0x01C60000 | 0x03, + /// <summary> + /// Mobile AP is being used. + /// </summary> + MobileApUsed = -0x01C60000 | 0x04, + /// <summary> + /// Connection failed. + /// </summary> + ConnectionFailed = -0x01C60000 | 0x05, + /// <summary> + /// Authentication failed. + /// </summary> + AuthFailed = -0x01C60000 | 0x06, + /// <summary> + /// Operation failed. + /// </summary> + OperationFailed = -0x01C60000 | 0x07, + /// <summary> + /// Too many client. + /// </summary> + TooManyClient = -0x01C60000 | 0x08, + /// <summary> + /// Already initialized client. + /// </summary> + AlreadyInitialized = -0x01C60000 | 0x09, + /// <summary> + /// Connection cancelled by local device. + /// </summary> + ConnectionCancelled = -0x01C60000 | 0x10 + } +} diff --git a/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectError.cs b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectError.cs new file mode 100644 index 0000000..325a5c9 --- /dev/null +++ b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectError.cs @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; + +namespace Tizen.Network.WiFiDirect +{ + internal static class WiFiDirectErrorFactory + { + internal static void ThrowWiFiDirectException(int exception) + { + WiFiDirectError _error = (WiFiDirectError)exception; + switch (_error) + { + case WiFiDirectError.InvalidParameter: + throw new InvalidOperationException("Invalid parameter"); + case WiFiDirectError.AlreadyInitialized: + throw new InvalidOperationException("Already initialized"); + case WiFiDirectError.AuthFailed: + throw new InvalidOperationException("Authentication failed"); + case WiFiDirectError.CommunicationFailed: + throw new InvalidOperationException("Communication failed"); + case WiFiDirectError.ConnectionCancelled: + throw new InvalidOperationException("Connection cancelled"); + case WiFiDirectError.ConnectionFailed: + throw new InvalidOperationException("Connection failed"); + case WiFiDirectError.ConnectionTimeOut: + throw new InvalidOperationException("Connection timed out"); + case WiFiDirectError.MobileApUsed: + throw new InvalidOperationException("Mobile Ap is being used"); + case WiFiDirectError.NotInitialized: + throw new InvalidOperationException("Not initialized"); + case WiFiDirectError.NotPermitted: + throw new InvalidOperationException("Not permitted"); + case WiFiDirectError.NotSupported: + throw new NotSupportedException("Not supported"); + case WiFiDirectError.OperationFailed: + throw new InvalidOperationException("Operation failed"); + case WiFiDirectError.OutOfMemory: + throw new InvalidOperationException("Out of memory"); + case WiFiDirectError.PermissionDenied: + throw new InvalidOperationException("Permission denied"); + case WiFiDirectError.ResourceBusy: + throw new InvalidOperationException("Resource is busy"); + case WiFiDirectError.TooManyClient: + throw new InvalidOperationException("Too many client"); + case WiFiDirectError.WiFiUsed: + throw new InvalidOperationException("Wi-fi is being used"); + } + } + } +} diff --git a/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectEventArgs.cs b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectEventArgs.cs new file mode 100644 index 0000000..b87af3b --- /dev/null +++ b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectEventArgs.cs @@ -0,0 +1,382 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; + +namespace Tizen.Network.WiFiDirect +{ + /// <summary> + /// An extended EventArgs class which contains changed connection state during connecting or disconnecting peer device. + /// </summary> + public class ConnectionStateChangedEventArgs : EventArgs + { + private WiFiDirectError _error; + private WiFiDirectConnectionState _state; + private string _macAddress; + + internal ConnectionStateChangedEventArgs(WiFiDirectError error, WiFiDirectConnectionState state, string macAddress) + { + _error = error; + _state = state; + _macAddress = macAddress; + } + + /// <summary> + /// Wi-Fi Direct result. + /// </summary> + public WiFiDirectError Error + { + get + { + return _error; + } + } + + /// <summary> + /// Wi-Fi Direct connection state of peer. + /// </summary> + public WiFiDirectConnectionState State + { + get + { + return _state; + } + } + + /// <summary> + /// MacAddress of peer. + /// </summary> + public string MacAddress + { + get + { + return _macAddress; + } + } + } + + /// <summary> + /// An extended EventArgs class which contains address properties of the peer when it connects to a group owner. + /// </summary> + public class IpAddressAssignedEventArgs : EventArgs + { + private string _macAddress; + private string _ipAddress; + private string _interfaceAddress; + + internal IpAddressAssignedEventArgs(string macAddress, string ipAddress, string interfaceAddress) + { + _macAddress = macAddress; + _ipAddress = ipAddress; + _interfaceAddress = interfaceAddress; + } + + /// <summary> + /// MacAddress of connected peer. + /// </summary> + public string MacAddress + { + get + { + return _macAddress; + } + } + + /// <summary> + /// IpAddress of connected peer. + /// </summary> + public string IpAddress + { + get + { + return _ipAddress; + } + } + + /// <summary> + /// InterfaceAddress of connected peer. + /// </summary> + public string InterfaceAddress + { + get + { + return _interfaceAddress; + } + } + } + + /// <summary> + /// An extended EventArgs class which contains changed Wi-Fi Direct state of local device. + /// </summary> + public class StateChangedEventArgs : EventArgs + { + private WiFiDirectState _state; + + internal StateChangedEventArgs(WiFiDirectState state) + { + _state = state; + } + + /// <summary> + /// Wi-Fi Direct state. + /// </summary> + public WiFiDirectState State + { + get + { + return _state; + } + } + } + + /// <summary> + /// An extended EventArgs class which contains changed Wi-Fi Direct discovery state during Wi-Fi Direct scan operation. + /// </summary> + public class DiscoveryStateChangedEventArgs : EventArgs + { + private WiFiDirectError _error; + private WiFiDirectDiscoveryState _state; + + internal DiscoveryStateChangedEventArgs(WiFiDirectError error, WiFiDirectDiscoveryState state) + { + _error = error; + _state = state; + } + + /// <summary> + /// Wi-Fi Direct result. + /// </summary> + public WiFiDirectError Error + { + get + { + return _error; + } + } + + /// <summary> + /// Wi-Fi Direct Discovery state. + /// </summary> + public WiFiDirectDiscoveryState DiscoveryState + { + get + { + return _state; + } + } + } + + /// <summary> + /// An extended EventArgs class which contains found peer information during Wi-Fi Direct scan operation. + /// </summary> + public class PeerFoundEventArgs : EventArgs + { + private WiFiDirectError _error; + private WiFiDirectDiscoveryState _state; + private WiFiDirectPeer _peer; + + internal PeerFoundEventArgs(WiFiDirectError error, WiFiDirectDiscoveryState state, WiFiDirectPeer peer) + { + _error = error; + _state = state; + _peer = peer; + } + + /// <summary> + /// Wi-Fi Direct result. + /// </summary> + public WiFiDirectError Error + { + get + { + return _error; + } + } + + /// <summary> + /// Wi-Fi Direct Discovery state. + /// </summary> + public WiFiDirectDiscoveryState DiscoveryState + { + get + { + return _state; + } + } + + /// <summary> + /// Found peer. + /// </summary> + public WiFiDirectPeer Peer + { + get + { + return _peer; + } + } + } + + /// <summary> + /// An extended EventArgs class which contains changed device state during activation or deactivation. + /// </summary> + public class DeviceStateChangedEventArgs : EventArgs + { + private WiFiDirectError _error; + private WiFiDirectDeviceState _state; + + internal DeviceStateChangedEventArgs(WiFiDirectError error, WiFiDirectDeviceState state) + { + _error = error; + _state = state; + } + + /// <summary> + /// Wi-Fi Direct result. + /// </summary> + public WiFiDirectError Error + { + get + { + return _error; + } + } + + /// <summary> + /// State of the device. + /// </summary> + public WiFiDirectDeviceState DeviceState + { + get + { + return _state; + } + } + } + + /// <summary> + /// An extended EventArgs class which contains changed service information during service discovery. + /// </summary> + public class ServiceStateChangedEventArgs : EventArgs + { + private WiFiDirectError _error; + private WiFiDirectServiceDiscoveryState _state; + private WiFiDirectServiceType _type; + private string _response; + private WiFiDirectPeer _peer; + + internal ServiceStateChangedEventArgs(WiFiDirectError error, WiFiDirectServiceDiscoveryState state, WiFiDirectServiceType type, string response, WiFiDirectPeer peer) + { + _error = error; + _state = state; + _type = type; + _response = response; + _peer = peer; + } + + /// <summary> + /// Wi-Fi Direct result. + /// </summary> + public WiFiDirectError Error + { + get + { + return _error; + } + } + + /// <summary> + /// Service discovery state. + /// </summary> + public WiFiDirectServiceDiscoveryState ServiceDiscoveryState + { + get + { + return _state; + } + } + + /// <summary> + /// Types of service. + /// </summary> + public WiFiDirectServiceType ServiceType + { + get + { + return _type; + } + } + + /// <summary> + /// Received response. + /// </summary> + public string Response + { + get + { + return _response; + } + } + + /// <summary> + /// Peer servicing device. + /// </summary> + public WiFiDirectPeer Peer + { + get + { + return _peer; + } + } + } + + /// <summary> + /// An extended EventArgs class which contains changed connection state during disconnect all peers or group related operations. + /// </summary> + public class ConnectionStatusChangedEventArgs : EventArgs + { + private WiFiDirectError _error; + private WiFiDirectConnectionState _state; + + internal ConnectionStatusChangedEventArgs(WiFiDirectError error, WiFiDirectConnectionState state) + { + _error = error; + _state = state; + } + + /// <summary> + /// Wi-Fi Direct result. + /// </summary> + public WiFiDirectError Error + { + get + { + return _error; + } + } + + /// <summary> + /// Connection state. + /// </summary> + public WiFiDirectConnectionState ConnectionState + { + get + { + return _state; + } + } + } +} diff --git a/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectManager.cs b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectManager.cs new file mode 100644 index 0000000..139e0c5 --- /dev/null +++ b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectManager.cs @@ -0,0 +1,1237 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; + +namespace Tizen.Network.WiFiDirect +{ + /// <summary> + /// A class which is used to manage settings of Wi-Fi Direct.<br> + /// This class is used to discover peer devices and manage settings of Wi-Fi Direct. + /// </summary> + /// <privilege> http://tizen.org/privilege/wifidirect </privilege> + public static class WiFiDirectManager + { + /// <summary> + /// A property to check whether the device is group owner or not. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, false will be returned. + /// </remarks> + public static bool IsGroupOwner + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.IsGroupOwner; + } + + else + { + return false; + } + } + } + + /// <summary> + /// A property to check whether the current group is the autonomous group or not. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, false will be returned. + /// </remarks> + public static bool IsAutonomousGroup + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.IsAutonomousGroup; + } + + else + { + return false; + } + } + } + + /// <summary> + /// SSID of local device. + /// </summary> + /// <remarks> + /// If there is any error, null will be returned. + /// </remarks> + public static string Ssid + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.Ssid; + } + + else + { + return null; + } + } + } + + /// <summary> + /// Name of network interface. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, null will be returned. + /// </remarks> + public static string NetworkInterface + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.NetworkInterface; + } + + else + { + return null; + } + } + } + + /// <summary> + /// IP address of a local device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, null will be returned. + /// </remarks> + public static string IpAddress + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.IpAddress; + } + + else + { + return null; + } + } + } + + /// <summary> + /// Subnet mask. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, null will be returned. + /// </remarks> + public static string SubnetMask + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.SubnetMask; + } + + else + { + return null; + } + } + } + + /// <summary> + /// Gateway address. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, null will be returned. + /// </remarks> + public static string GatewayAddress + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.GatewayAddress; + } + + else + { + return null; + } + } + } + + /// <summary> + /// Mac address of a local device. + /// </summary> + /// <remarks> + /// If there is any error, null will be returned. + /// </remarks> + public static string MacAddress + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.MacAddress; + } + + else + { + return null; + } + } + } + + /// <summary> + /// State of Wi-Fi direct service. + /// </summary> + public static WiFiDirectState State + { + get + { + return WiFiDirectManagerImpl.Instance.State; + } + } + + /// <summary> + /// A property to check whether the device is discoverable or not by P2P discovery. + /// </summary> + public static bool IsDiscoverable + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.IsDiscoverable; + } + + else + { + return false; + } + } + } + + /// <summary> + /// A property to check whether the local device is listening only. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, false will be returned. + /// </remarks> + public static bool IsListenOnly + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.IsListenOnly; + } + + else + { + return false; + } + } + } + + /// <summary> + /// Primary device type of local device. + /// </summary> + /// <remarks> + /// If there is any error, 0 will be returned. + /// </remarks> + public static WiFiDirectPrimaryDeviceType PrimaryType + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.PrimaryType; + } + + else + { + return default(WiFiDirectPrimaryDeviceType); + } + } + } + + /// <summary> + /// Secondary device type of local device. + /// </summary> + /// <remarks> + /// If there is any error, 0 will be returned. + /// </remarks> + public static WiFiDirectSecondaryDeviceType SecondaryType + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.SecondaryType; + } + + else + { + return default(WiFiDirectSecondaryDeviceType); + } + } + } + + /// <summary> + /// Supported WPS (Wi-Fi Protected Setup) types at local device. + /// </summary> + /// <remarks> + /// If there is any error, -1 will be returned. + /// </remarks> + public static int WpsMode + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.WpsMode; + } + + else + { + return -1; + } + } + } + + /// <summary> + /// WPS (Wi-Fi Protected Setup) type. + /// </summary> + public static WiFiDirectWpsType Wps + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.WpsType; + } + + else + { + return default(WiFiDirectWpsType); + } + } + } + + /// <summary> + /// Channel number on which the P2P Device is operating as the P2P Group. + /// </summary> + /// <remarks> + /// If there is any error, -1 will be returned. + /// </remarks> + public static int OperatingChannel + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.OperatingChannel; + } + + else + { + return -1; + } + } + } + + /// <summary> + /// A property to check whether persistent group is enabled. + /// </summary> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static bool PersistentGroupEnabled + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.PersistentGroupEnabled; + } + + else + { + return false; + } + } + + set + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.PersistentGroupEnabled = value; + } + } + } + + /// <summary> + /// Autoconnection mode status. + /// </summary> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static bool AutoConnect + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.AutoConnect; + } + + else + { + return false; + } + } + + set + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.AutoConnect = value; + } + } + } + + /// <summary> + /// WPS PIN number. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, null will be returned during get and Not permitted exception message will be returned during set. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static string WpsPin + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.WpsPin; + } + + else + { + return null; + } + } + + set + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.WpsPin = value; + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + } + + /// <summary> + /// Name of local device. + /// </summary> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static string Name + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.Name; + } + + else + { + return null; + } + } + + set + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.Name = value; + } + } + } + + /// <summary> + /// Requested WPS (Wi-Fi Protected Setup) type. + /// </summary> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static WiFiDirectWpsType RequestedWps + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.RequestedWps; + } + + else + { + return default(WiFiDirectWpsType); + } + } + + set + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.RequestedWps = value; + } + } + } + + /// <summary> + /// Intent of the group owner. + /// </summary> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static int GroupOwnerIntent + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.GroupOwnerIntent; + } + + else + { + return -1; + } + } + + set + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.GroupOwnerIntent = value; + } + } + } + + /// <summary> + /// Max number of clients. + /// </summary> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static int MaxClients + { + get + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.MaxClients; + } + + else + { + return -1; + } + } + + set + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.MaxClients = value; + } + } + } + + /// <summary> + /// Wi-Fi Protected Access (WPA) password. + /// It is used during Wi-Fi Direct Group creation. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, null will be returned during get and Not permitted exception message will be returned during set. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static string Passphrase + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.Passphrase; + } + + else + { + return null; + } + } + + set + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.Passphrase = value; + } + + else + { + Log.Error(Globals.LogTag, "Wi-Fi direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + } + + /// <summary> + /// Connection session timer value in second. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If it is deactivated, -1 will be returned during get and Not permitted exception message will be returned during set. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown while setting this property when the wifidirect is not supported</exception> + public static int SessionTimer + { + get + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.SessionTimer; + } + + else + { + return -1; + } + } + + set + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.SessionTimer = value; + } + + else + { + Log.Error(Globals.LogTag, "Wi-Fi direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + } + + /// <summary> + /// (event) StateChanged is raised when Wi-Fi Direct state is changed. + /// </summary> + public static event EventHandler<StateChangedEventArgs> StateChanged + { + add + { + WiFiDirectManagerImpl.Instance.StateChanged += value; + } + + remove + { + WiFiDirectManagerImpl.Instance.StateChanged -= value; + } + } + + /// <summary> + /// (event) DiscoveryStateChanged is raised when Wi-Fi Direct discovery state is changed. + /// </summary> + public static event EventHandler<DiscoveryStateChangedEventArgs> DiscoveryStateChanged + { + add + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.DiscoveryStateChanged += value; + } + } + + remove + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.DiscoveryStateChanged -= value; + } + } + } + + /// <summary> + /// (event) DeviceStateChanged is raised when device state is changed. + /// </summary> + public static event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged + { + add + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.DeviceStateChanged += value; + } + } + + remove + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.DeviceStateChanged -= value; + } + } + } + + /// <summary> + /// (event) PeerFound is raised when peer is found. + /// </summary> + public static event EventHandler<PeerFoundEventArgs> PeerFound + { + add + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.PeerFound += value; + } + } + + remove + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.PeerFound -= value; + } + } + } + + /// <summary> + /// (event) ConnectionStatusChanged is raised when status of connection is changed. + /// </summary> + public static event EventHandler<ConnectionStatusChangedEventArgs> ConnectionStatusChanged + { + add + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.ConnectionStatusChanged += value; + } + } + + remove + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.ConnectionStatusChanged -= value; + } + } + } + + /// <summary> + /// Activates the Wi-Fi Direct service. + /// </summary> + /// <remarks> + /// If this succeeds, DeviceStateChanged event will be invoked. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public static void Activate() + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.Activate(); + } + + else + { + Log.Error(Globals.LogTag, "Wi-Fi direct is not initialized"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotInitialized); + } + } + + /// <summary> + /// Deactivates the Wi-Fi Direct service. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If this succeeds, DeviceStateChanged event will be invoked. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public static void Deactivate() + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.Deactivate(); + } + + else + { + Log.Error(Globals.LogTag, "Wi-Fi direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Starts discovery to find all P2P capable devices. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If this succeeds, DiscoveryStateChanged and PeerFound event will be invoked. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + /// <param name="listenOnly">Listen status.If False, then cycle between Scan and Listen.If True, then skip the initial 802.11 Scan and enter Listen state.</param> + /// <param name="duration">Duration of discovery period, in seconds.</param> + /// <param name="channel">Discovery channel.It is optional, default enum value FullScan is assigned.</param> + public static void StartDiscovery(bool listenOnly, int duration, WiFiDirectDiscoveryChannel channel = WiFiDirectDiscoveryChannel.FullScan) + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.StartDiscovery(listenOnly, duration, channel); + } + + else + { + Log.Error(Globals.LogTag, "Wi-Fi direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Cancels discovery process. + /// </summary> + /// <remarks> + /// Discovery must be started by StartDiscovery. + /// If this succeeds, DiscoveryStateChanged and PeerFound event will be invoked. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public static void CancelDiscovery() + { + if (WiFiDirectManager.State == WiFiDirectState.Discovering) + { + WiFiDirectManagerImpl.Instance.CancelDiscovery(); + } + + else + { + Log.Error(Globals.LogTag, "Wi-Fi direct discovery is not started"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Gets the information of discovered peers. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + /// <returns> List of discovered peer objects.</returns> + public static IEnumerable<WiFiDirectPeer> GetDiscoveredPeers() + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.GetDiscoveredPeers(); + } + + else + { + return null; + } + } + + /// <summary> + /// Gets the information of connected peers. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + /// <returns> List of connected peer objects.</returns> + public static IEnumerable<WiFiDirectPeer> GetConnectedPeers() + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.GetConnectedPeers(); + } + + else + { + return null; + } + } + + /// <summary> + /// Disconnects all connected links to peers. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If this succeeds, ConnectionStatusChanged event will be invoked. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public static void DisconnectAll() + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.DisconnectAll(); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Creates a Wi-Fi Direct group and sets up device as the group owner. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If this succeeds, ConnectionStatusChanged event will be invoked with GroupCreated. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public static void CreateGroup() + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.CreateGroup(); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Destroys the Wi-Fi Direct group owned by a local device.If creating a group is in progress, this API cancels that process. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If this succeeds, ConnectionStatusChanged event will be invoked with GroupDestroyed. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public static void DestroyGroup() + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.DestroyGroup(); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Set the WPS config PBC as preferred method for connection. + /// </summary> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public static void ActivatePushButton() + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.ActivatePushButton(); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Gets the supported WPS types. + /// </summary> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + /// <returns>The list of supported wps types.</returns> + public static IEnumerable<WiFiDirectWpsType> GetSupportedWpsTypes() + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.GetSupportedWpsTypes(); + } + + else + { + return null; + } + } + + /// <summary> + /// Gets the persistent groups. + /// </summary> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + /// <returns>List of the persistent group objects.</returns> + public static IEnumerable<WiFiDirectPersistentGroup> GetPersistentGroups() + { + if (Globals.IsInitialize) + { + return WiFiDirectManagerImpl.Instance.GetPersistentGroups(); + } + + else + { + return null; + } + } + + /// <summary> + /// Removes a persistent group. + /// </summary> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + /// <param name="group">Persistent group owner.</param> + public static void RemovePersistentGroup(WiFiDirectPersistentGroup group) + { + if (Globals.IsInitialize) + { + WiFiDirectManagerImpl.Instance.RemovePersistentGroup(group); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotInitialized); + } + } + + /// <summary> + /// Initializes or Deintializes the WiFi-Direct Display(MIRACAST) service. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect display feature is not supported + /// </exception> + /// <param name="enable">Enables/Disables service.</param> + public static void InitMiracast(bool enable) + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.InitMiracast(enable); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Enables Wi-Fi Display functionality. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect display feature is not supported + /// </exception> + public static void InitDisplay() + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.InitDisplay(); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Disable Wi-Fi Display(WFD) functionality and disable the support of WFD Information Element(IE). + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated and WFD must be enabled. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect display feature is not supported + /// </exception> + public static void DeinitDisplay() + { + if (Globals.IsActivated && Globals.s_isDisplay) + { + WiFiDirectManagerImpl.Instance.DeinitDisplay(); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated and/or Wi-Fi display is not enabled"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Sets the Wi-Fi Display parameters for the WFD IE of local device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated and WFD must be enabled. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect display feature is not supported + /// </exception> + /// <param name="type">WFD Device Type: define the Role of WFD device like source or sink.</param> + /// <param name="port">Specifies Session Management Control Port number. It should be 2 bytes(0~65535).</param> + /// <param name="hdcp">CP support bit: (1 = enable the hdcp support, 0 = disable the hdcp support).</param> + public static void SetDisplay(WiFiDirectDisplayType type, int port, int hdcp) + { + if (Globals.IsActivated && Globals.s_isDisplay) + { + WiFiDirectManagerImpl.Instance.SetDisplay(type, port, hdcp); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated and/or Wi-Fi display is not enabled"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Sets the Wi-Fi Display session availability. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated and WFD must be enabled. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect display feature is not supported + /// </exception> + /// <param name="availability">Wi-Fi Display session availability.</param> + public static void SetDisplayAvailability(bool availability) + { + if (Globals.IsActivated && Globals.s_isDisplay) + { + WiFiDirectManagerImpl.Instance.SetDisplayAvailability(availability); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated and/or Wi-Fi display is not enabled"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Sets the automatic group removal feature when all peers are disconnected. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// ConnectionStatusChanged event will be invoked with GroupDestroyed when this feature is enabled and there's no connected group client and if device is group owner. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + /// <param name="enable">Enables/Disables group removal feature.</param> + public static void SetAutoGroupRemove(bool enable) + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.SetAutoGroupRemove(enable); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Registers the service. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If there is any error while registering service, 0 will be returned. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect service discovery is not supported + /// </exception> + /// <returns>The service Id of service getting registered.</returns> + /// <param name="type">Type of Wi-Fi Direct Service.</param> + /// <param name="info">Service specific information.</param> + /// <param name="serviceInfo">Service information.</param> + public static uint RegisterService(WiFiDirectServiceType type, string info, string serviceInfo) + { + if (Globals.IsActivated) + { + return WiFiDirectManagerImpl.Instance.RegisterService(type, info, serviceInfo); + } + + else + { + return 0; + } + } + + /// <summary> + /// Deregisters for a service used for WiFi Direct Service Discovery. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect service discovery is not supported + /// </exception> + /// <param name="serviceId"> Service ID for which service has to be deregistered.</param> + public static void DeregisterService(uint serviceId) + { + if (Globals.IsActivated) + { + WiFiDirectManagerImpl.Instance.DeregisterService(serviceId); + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + } +} diff --git a/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectManagerImpl.cs b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectManagerImpl.cs new file mode 100644 index 0000000..f376264 --- /dev/null +++ b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectManagerImpl.cs @@ -0,0 +1,1241 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Threading; + +namespace Tizen.Network.WiFiDirect +{ + internal class WiFiDirectThreadLocal + { + private int _threadId; + internal WiFiDirectThreadLocal(int id) + { + _threadId = id; + } + + public int ThreadId + { + get + { + return _threadId; + } + } + + ~WiFiDirectThreadLocal() + { + Log.Info(Globals.LogTag, "Deinitializing Wi-Fi direct"); + int ret = Interop.WiFiDirect.Deinitialize(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to deinitialize Wi-Fi direct, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + else + { + Globals.s_isInitialize = false; + } + } + } + + internal static class Globals + { + internal const string LogTag = "Tizen.Network.WiFiDirect"; + internal static bool s_isInitialize = false; + internal static bool s_isDisplay = false; + private static ThreadLocal<WiFiDirectThreadLocal> s_threadName = new ThreadLocal<WiFiDirectThreadLocal>(() => + { + Log.Info(Globals.LogTag, "In threadlocal delegate"); + return new WiFiDirectThreadLocal(Thread.CurrentThread.ManagedThreadId); + }); + internal static bool IsActivated + { + get + { + WiFiDirectState _state = WiFiDirectManager.State; + if (IsInitialize) + { + if (_state == WiFiDirectState.Deactivated || _state == WiFiDirectState.Deactivating) + { + return false; + } + + else + { + return true; + } + } + + else + { + return false; + } + } + } + + private static bool IsUniqueThread() + { + if (s_threadName.IsValueCreated) + { + Log.Info(Globals.LogTag, "This thread is old"); + return false; + } + + else + { + WiFiDirectThreadLocal obj = s_threadName.Value; + Log.Info(Globals.LogTag, "This thread is new , Id = " + obj.ThreadId); + return true; + } + } + + internal static bool IsInitialize + { + get + { + if(Globals.IsUniqueThread() || !Globals.s_isInitialize) + { + WiFiDirectManagerImpl.Instance.Initialize(); + } + + return (Globals.s_isInitialize); + } + } + } + + /// <summary> + /// Implementation of Wi-Fi Direct Apis + /// </summary> + internal partial class WiFiDirectManagerImpl : IDisposable + { + private event EventHandler<StateChangedEventArgs> _stateChanged; + private event EventHandler<DiscoveryStateChangedEventArgs > _discoveryStateChanged; + private event EventHandler<PeerFoundEventArgs > _peerFound; + private event EventHandler<DeviceStateChangedEventArgs > _deviceStateChanged; + private event EventHandler<ConnectionStatusChangedEventArgs > _connectionStatusChanged; + + private Interop.WiFiDirect.StateChangedCallback _stateChangedCallback; + private Interop.WiFiDirect.DiscoveryStateChangedCallback _discoveryStateChangedCallback; + private Interop.WiFiDirect.PeerFoundCallback _peerFoundCallback; + private Interop.WiFiDirect.DeviceStateChangedCallback _deviceStateChangedCallback; + private Interop.WiFiDirect.ConnectionStateChangedCallback _connectionChangedCallback; + + internal event EventHandler<StateChangedEventArgs> StateChanged + { + add + { + if (_stateChanged == null) + { + RegisterStateChangedEvent(); + } + + _stateChanged += value; + } + + remove + { + _stateChanged -= value; + if (_stateChanged == null) + { + UnregisterStateChangedEvent(); + } + } + } + + internal event EventHandler<DiscoveryStateChangedEventArgs> DiscoveryStateChanged + { + add + { + if (_discoveryStateChanged == null) + { + RegisterDiscoveryStateChangedEvent(); + } + + _discoveryStateChanged += value; + } + + remove + { + _discoveryStateChanged -= value; + if (_discoveryStateChanged == null) + { + UnregisterDiscoveryStateChangedEvent(); + } + } + } + + internal event EventHandler<PeerFoundEventArgs> PeerFound + { + add + { + if (_peerFound == null) + { + RegisterPeerFoundEvent(); + } + + _peerFound += value; + } + + remove + { + _peerFound -= value; + if (_peerFound == null) + { + UnregisterPeerFoundEvent(); + } + } + } + + internal event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged + { + add + { + if (_deviceStateChanged == null) + { + RegisterDeviceStateChangedEvent(); + } + + _deviceStateChanged += value; + } + + remove + { + _deviceStateChanged -= value; + if (_deviceStateChanged == null) + { + UnregisterDeviceStateChangedEvent(); + } + } + } + + internal event EventHandler<ConnectionStatusChangedEventArgs> ConnectionStatusChanged + { + add + { + if (_connectionStatusChanged == null) + { + RegisterConnectionStatusChangedEvent(); + } + + _connectionStatusChanged += value; + } + + remove + { + _connectionStatusChanged -= value; + if (_connectionStatusChanged == null) + { + UnregisterConnectionStatusChangedEvent(); + } + } + } + + private bool _disposed = false; + private static WiFiDirectManagerImpl _instance; + + private void RegisterStateChangedEvent() + { + _stateChangedCallback = (WiFiDirectState stateInfo, IntPtr userData) => + { + if (_stateChanged != null) + { + WiFiDirectState state = stateInfo; + _stateChanged(null, new StateChangedEventArgs(state)); + } + }; + int ret = Interop.WiFiDirect.SetStateChangedCallback(_stateChangedCallback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set Wi-Fi Direct state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void UnregisterStateChangedEvent() + { + int ret = Interop.WiFiDirect.UnsetStateChangedCallback(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to unset Wi-Fi Direct state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void RegisterDiscoveryStateChangedEvent() + { + _discoveryStateChangedCallback = (int result, WiFiDirectDiscoveryState stateInfo, IntPtr userData) => + { + if (_discoveryStateChanged != null) + { + WiFiDirectError error = (WiFiDirectError)result; + WiFiDirectDiscoveryState state = stateInfo; + _discoveryStateChanged(null, new DiscoveryStateChangedEventArgs(error, state)); + } + }; + int ret = Interop.WiFiDirect.SetDiscoveryStateChangedCallback(_discoveryStateChangedCallback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set Wi-Fi Direct discovery state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void UnregisterDiscoveryStateChangedEvent() + { + int ret = Interop.WiFiDirect.UnsetDiscoveryStateChangedCallback(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to unset Wi-Fi Direct discovery state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void RegisterPeerFoundEvent() + { + _peerFoundCallback = (int result, WiFiDirectDiscoveryState stateInfo, string address, IntPtr userData) => + { + if (_peerFound != null) + { + WiFiDirectError error = (WiFiDirectError)result; + WiFiDirectDiscoveryState state = stateInfo; + IntPtr peer; + Interop.WiFiDirect.GetDiscoveredPeerInfo(address, out peer); + DiscoveredPeerStruct peerStruct = (DiscoveredPeerStruct)Marshal.PtrToStructure(peer, typeof(DiscoveredPeerStruct)); + _peerFound(null, new PeerFoundEventArgs(error, state, WiFiDirectUtils.ConvertStructToDiscoveredPeer(peerStruct))); + } + }; + int ret = Interop.WiFiDirect.SetPeerFoundCallback(_peerFoundCallback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set Wi-Fi Direct discovery state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void UnregisterPeerFoundEvent() + { + int ret = Interop.WiFiDirect.UnsetPeerFoundCallback(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to unset Wi-Fi Direct discovery state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void RegisterDeviceStateChangedEvent() + { + _deviceStateChangedCallback = (int result, WiFiDirectDeviceState stateInfo, IntPtr userData) => + { + if (_deviceStateChanged != null) + { + WiFiDirectError error = (WiFiDirectError)result; + WiFiDirectDeviceState state = stateInfo; + _deviceStateChanged(null, new DeviceStateChangedEventArgs(error, state)); + } + }; + int ret = Interop.WiFiDirect.SetDeviceStateChangedCallback(_deviceStateChangedCallback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set device state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void UnregisterDeviceStateChangedEvent() + { + int ret = Interop.WiFiDirect.UnsetDeviceStateChangedCallback(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to unset device state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void RegisterConnectionStatusChangedEvent() + { + _connectionChangedCallback = (int result, WiFiDirectConnectionState stateInfo, string address, IntPtr userData) => + { + if (_connectionStatusChanged != null) + { + WiFiDirectError error = (WiFiDirectError)result; + WiFiDirectConnectionState state = stateInfo; + + _connectionStatusChanged(null, new ConnectionStatusChangedEventArgs(error, state)); + } + }; + int ret = Interop.WiFiDirect.SetConnectionChangedCallback(_connectionChangedCallback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set connection status changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void UnregisterConnectionStatusChangedEvent() + { + int ret = Interop.WiFiDirect.UnsetConnectionChangedCallback(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to unset connection status changed callback, Error - " + (WiFiDirectError)ret); + } + } + + internal bool IsGroupOwner + { + get + { + bool isGroupOwner; + int ret = Interop.WiFiDirect.IsGroupOwner(out isGroupOwner); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get whether this device is the group owner or not, Error - " + (WiFiDirectError)ret); + } + + return isGroupOwner; + } + } + + internal bool IsAutonomousGroup + { + get + { + bool isAutonomousGroup; + int ret = Interop.WiFiDirect.IsAutonomousGroup(out isAutonomousGroup); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to check whether the current group is autonomous or not, Error - " + (WiFiDirectError)ret); + } + + return isAutonomousGroup; + } + } + + internal string Ssid + { + get + { + string ssid; + int ret = Interop.WiFiDirect.GetSsid(out ssid); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get SSID of local device, Error - " + (WiFiDirectError)ret); + return null; + } + + return ssid; + } + } + + internal string NetworkInterface + { + get + { + string networkInterface; + int ret = Interop.WiFiDirect.GetInterfaceName(out networkInterface); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get name of network interface, Error - " + (WiFiDirectError)ret); + return ""; + } + + return networkInterface; + } + } + + internal string IpAddress + { + get + { + string ipAddress; + int ret = Interop.WiFiDirect.GetIpAddress(out ipAddress); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get IP address of local device, Error - " + (WiFiDirectError)ret); + return ""; + } + + return ipAddress; + } + } + + internal string SubnetMask + { + get + { + string subnetMask; + int ret = Interop.WiFiDirect.GetSubnetMask(out subnetMask); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get subnet mask, Error - " + (WiFiDirectError)ret); + return ""; + } + + return subnetMask; + } + } + + internal string GatewayAddress + { + get + { + string gatewayAddress; + int ret = Interop.WiFiDirect.GetGatewayAddress(out gatewayAddress); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get gateway address, Error - " + (WiFiDirectError)ret); + return ""; + } + + return gatewayAddress; + } + } + + internal string MacAddress + { + get + { + string macAddress; + int ret = Interop.WiFiDirect.GetMacAddress(out macAddress); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get mac address, Error - " + (WiFiDirectError)ret); + return null; + } + + return macAddress; + } + } + + internal WiFiDirectState State + { + get + { + WiFiDirectState state; + int ret = Interop.WiFiDirect.GetState(out state); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get state of Wi-Fi direct service, Error - " + (WiFiDirectError)ret); + } + + return state; + } + } + + internal bool IsDiscoverable + { + get + { + bool isDiscoverable; + int ret = Interop.WiFiDirect.IsDiscoverable(out isDiscoverable); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to check whether the device is discoverable, Error - " + (WiFiDirectError)ret); + } + + return isDiscoverable; + } + } + + internal bool IsListenOnly + { + get + { + bool isListenOnly; + int ret = Interop.WiFiDirect.IsListeningOnly(out isListenOnly); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to check whether the local device is listening only, Error - " + (WiFiDirectError)ret); + } + + return isListenOnly; + } + } + + internal WiFiDirectPrimaryDeviceType PrimaryType + { + get + { + WiFiDirectPrimaryDeviceType primaryType; + int ret = Interop.WiFiDirect.GetPrimaryType(out primaryType); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the primary device type of local device, Error - " + (WiFiDirectError)ret); + } + + return primaryType; + } + } + + internal WiFiDirectSecondaryDeviceType SecondaryType + { + get + { + WiFiDirectSecondaryDeviceType secondaryType; + int ret = Interop.WiFiDirect.GetSecondaryType(out secondaryType); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the secondary device type of local device, Error - " + (WiFiDirectError)ret); + } + + return secondaryType; + } + } + + internal int WpsMode + { + get + { + int mode; + int ret = Interop.WiFiDirect.GetWpsMode(out mode); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get supproted wps modes at local device, Error - " + (WiFiDirectError)ret); + return -1; + } + + return mode; + } + } + + internal WiFiDirectWpsType WpsType + { + get + { + WiFiDirectWpsType wpsType; + int ret = Interop.WiFiDirect.GetLocalWpsType(out wpsType); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the WPS type, Error - " + (WiFiDirectError)ret); + } + + return wpsType; + } + } + + internal int OperatingChannel + { + get + { + int channel; + int ret = Interop.WiFiDirect.GetChannel(out channel); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get operating channel, Error - " + (WiFiDirectError)ret); + return -1; + } + + return channel; + } + } + + internal bool PersistentGroupEnabled + { + get + { + bool isEnabled; + int ret = Interop.WiFiDirect.GetPersistentGroupState(out isEnabled); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to check persistent group state, Error - " + (WiFiDirectError)ret); + } + + return isEnabled; + } + + set + { + int ret = Interop.WiFiDirect.SetPersistentGroupState(value); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the persistent group state, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal bool AutoConnect + { + get + { + bool isAutoConnect; + int ret = Interop.WiFiDirect.GetAutoConnectionMode(out isAutoConnect); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get autoconnection mode status, Error - " + (WiFiDirectError)ret); + } + + return isAutoConnect; + } + + set + { + int ret = Interop.WiFiDirect.SetAutoConnectionMode(value); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the autoconnection mode, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal string WpsPin + { + get + { + string pin; + int ret = Interop.WiFiDirect.GetWpsPin(out pin); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get WPS pin, Error - " + (WiFiDirectError)ret); + } + + return pin; + } + + set + { + int ret = Interop.WiFiDirect.SetWpsPin(value.ToString()); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set or update WPS pin, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal string Name + { + get + { + string name; + int ret = Interop.WiFiDirect.GetName(out name); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get name of local device, Error - " + (WiFiDirectError)ret); + return null; + } + + return name; + } + + set + { + int ret = Interop.WiFiDirect.SetName(value.ToString()); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set name of local device, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal WiFiDirectWpsType RequestedWps + { + get + { + WiFiDirectWpsType wpsType; + int ret = Interop.WiFiDirect.GetReqWpsType(out wpsType); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the requested WPS type, Error - " + (WiFiDirectError)ret); + } + + return wpsType; + } + + set + { + int ret = Interop.WiFiDirect.SetReqWpsType(value); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the requested WPS type, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal int GroupOwnerIntent + { + get + { + int intent; + int ret = Interop.WiFiDirect.GetIntent(out intent); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the intent of the group owner, Error - " + (WiFiDirectError)ret); + } + + return intent; + } + + set + { + int ret = Interop.WiFiDirect.SetIntent(value); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the intent of the group owner, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal int MaxClients + { + get + { + int maxClients; + int ret = Interop.WiFiDirect.GetMaxClients(out maxClients); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the max number of clients, Error - " + (WiFiDirectError)ret); + } + + return maxClients; + } + + set + { + int ret = Interop.WiFiDirect.SetMaxClients(value); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the max number of clients, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal string Passphrase + { + get + { + string passphrase; + int ret = Interop.WiFiDirect.GetPassPhrase(out passphrase); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get Wi-Fi Protected Access password, Error - " + (WiFiDirectError)ret); + return ""; + } + + return passphrase; + } + + set + { + int ret = Interop.WiFiDirect.SetPassPhrase(value.ToString()); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set or update Wi-Fi Protected Access password, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal int SessionTimer + { + get + { + int sessionTimer; + int ret = Interop.WiFiDirect.GetSessionTimer(out sessionTimer); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the timer used to expire the connection session, Error - " + (WiFiDirectError)ret); + } + + return sessionTimer; + } + + set + { + int ret = Interop.WiFiDirect.SetSessionTimer(value); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the timer used to expire the connection session, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + } + + internal void Activate() + { + int ret = Interop.WiFiDirect.Activate(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to activate Wi-Fi direct service, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void Deactivate() + { + int ret = Interop.WiFiDirect.Deactivate(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to deactivate Wi-Fi direct service, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void StartDiscovery(bool listenOnly, int duration, WiFiDirectDiscoveryChannel channel = WiFiDirectDiscoveryChannel.FullScan) + { + int ret = Interop.WiFiDirect.StartDiscoveryInChannel(listenOnly, duration, channel); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to start discovery, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void CancelDiscovery() + { + int ret = Interop.WiFiDirect.StopDiscovery(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to cancel discovery, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal IEnumerable<WiFiDirectPeer> GetDiscoveredPeers() + { + List<WiFiDirectPeer> discoveredPeerList = new List<WiFiDirectPeer>(); + Interop.WiFiDirect.DiscoveredPeerCallback callback = (ref DiscoveredPeerStruct peer, IntPtr userData) => + { + if (!peer.Equals(null)) + { + discoveredPeerList.Add(WiFiDirectUtils.ConvertStructToDiscoveredPeer(peer)); + } + + return true; + }; + int ret = Interop.WiFiDirect.GetDiscoveredPeers(callback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get information of discovered peers, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + return discoveredPeerList; + } + + internal IEnumerable<WiFiDirectPeer> GetConnectedPeers() + { + List<WiFiDirectPeer> connectedPeerList = new List<WiFiDirectPeer>(); + Interop.WiFiDirect.ConnectedPeerCallback callback = (ref ConnectedPeerStruct peer, IntPtr userData) => + { + if (!peer.Equals(null)) + { + connectedPeerList.Add(WiFiDirectUtils.ConvertStructToConnectedPeer(peer)); + } + + return true; + }; + int ret = Interop.WiFiDirect.GetConnectedPeers(callback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get information of connected peers, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + return connectedPeerList; + } + + internal void DisconnectAll() + { + int ret = Interop.WiFiDirect.DisconnectAll(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to disconnect all connected links, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void CreateGroup() + { + int ret = Interop.WiFiDirect.CreateGroup(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to create a WiFi-Direct group, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void DestroyGroup() + { + int ret = Interop.WiFiDirect.DestroyGroup(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to destroy the WiFi-Direct group, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void ActivatePushButton() + { + int ret = Interop.WiFiDirect.ActivatePushButton(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the Wps config PBC, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal IEnumerable<WiFiDirectWpsType> GetSupportedWpsTypes() + { + List<WiFiDirectWpsType> wpsList = new List<WiFiDirectWpsType>(); + Interop.WiFiDirect.WpsTypeCallback callback = (WiFiDirectWpsType type, IntPtr userData) => + { + if (!type.Equals(null)) + { + wpsList.Add(type); + } + + return true; + }; + int ret = Interop.WiFiDirect.GetWpsTypes(callback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the supported WPS types, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + return wpsList; + } + + internal IEnumerable<WiFiDirectPersistentGroup> GetPersistentGroups() + { + List<WiFiDirectPersistentGroup> persistentGroupList = new List<WiFiDirectPersistentGroup>(); + Interop.WiFiDirect.PersistentGroupCallback callback = (string address, string ssid, IntPtr userData) => + { + if (address != null && ssid != null) + { + persistentGroupList.Add(new WiFiDirectPersistentGroup(address, ssid)); + } + + return true; + }; + int ret = Interop.WiFiDirect.GetPersistentGroups(callback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the persistent groups, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + return persistentGroupList; + } + + internal void RemovePersistentGroup(WiFiDirectPersistentGroup group) + { + int ret = Interop.WiFiDirect.RemovePersistentGroup(group.MacAddress, group.Ssid); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to remove a persistent group, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void InitMiracast(bool enable) + { + int ret = Interop.WiFiDirect.InitMiracast(enable); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the WiFi-Direct Display(MIRACAST) service, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void InitDisplay() + { + int ret = Interop.WiFiDirect.InitDisplay(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to enable Wi-Fi Display functionality, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + else + { + Globals.s_isDisplay = true; + } + } + + internal void DeinitDisplay() + { + int ret = Interop.WiFiDirect.DeinitDisplay(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to disable Wi-Fi Display functionality, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + else + { + Globals.s_isDisplay = false; + } + } + + internal void SetDisplay(WiFiDirectDisplayType type, int port, int hdcp) + { + int ret = Interop.WiFiDirect.SetDisplay(type, port, hdcp); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the Wi-Fi Display parameters, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void SetDisplayAvailability(bool availability) + { + int ret = Interop.WiFiDirect.SetDisplayAvailability(availability); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set the Wi-Fi Display session availability, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal void SetAutoGroupRemove(bool enable) + { + int ret = Interop.WiFiDirect.SetAutoGroupRemoval(enable); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set automatic group removal feature when all peers are disconnected, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal uint RegisterService(WiFiDirectServiceType type, string info, string serviceInfo) + { + uint serviceId; + int ret = Interop.WiFiDirect.RegisterService(type, info, serviceInfo, out serviceId); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to register for service, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + return serviceId; + } + + internal void DeregisterService(uint serviceId) + { + int ret = Interop.WiFiDirect.DeregisterService(serviceId); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to deregister service, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + internal static WiFiDirectManagerImpl Instance + { + get + { + if (_instance == null) + { + _instance = new WiFiDirectManagerImpl(); + } + + return _instance; + } + } + + private WiFiDirectManagerImpl() + { + } + + internal void Initialize() + { + int ret = Interop.WiFiDirect.Initialize(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to initialize Wi-Fi direct, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + else + { + Globals.s_isInitialize = true; + } + } + + ~WiFiDirectManagerImpl() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + if (disposing) + { + // Free managed objects. + } + + //Free unmanaged objects + RemoveAllRegisteredEvent(); + Deinitialize(); + _disposed = true; + } + + private void Deinitialize() + { + int ret = Interop.WiFiDirect.Deinitialize(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to deinitialize Wi-Fi direct, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + + else + { + Globals.s_isInitialize = false; + } + } + + private void RemoveAllRegisteredEvent() + { + //unregister all remaining events when this object is released. + if (_stateChanged != null) + { + UnregisterStateChangedEvent(); + } + + if (_discoveryStateChanged != null) + { + UnregisterDiscoveryStateChangedEvent(); + } + + if (_peerFound != null) + { + UnregisterPeerFoundEvent(); + } + + if (_deviceStateChanged != null) + { + UnregisterDeviceStateChangedEvent(); + } + + if (_connectionStatusChanged != null) + { + UnregisterConnectionStatusChangedEvent(); + } + } + } +} diff --git a/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectPeer.cs b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectPeer.cs new file mode 100644 index 0000000..a97e466 --- /dev/null +++ b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectPeer.cs @@ -0,0 +1,736 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Collections.Concurrent; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +namespace Tizen.Network.WiFiDirect +{ + /// <summary> + /// WiFiDirectPeer class is used to handle the connection with remote devices using WiFi Direct. + /// </summary> + /// <privilege> http://tizen.org/privilege/wifidirect </privilege> + public class WiFiDirectPeer + { + private event EventHandler<ConnectionStateChangedEventArgs> _connectionStateChanged; + private event EventHandler<IpAddressAssignedEventArgs> _ipAddressAssigned; + private event EventHandler<ServiceStateChangedEventArgs> _serviceStateChanged; + + private Interop.WiFiDirect.ConnectionStateChangedCallback _connectionStateChangedCallback; + private Interop.WiFiDirect.ClientIpAddressAssignedCallback _ipAddressAssignedCallback; + private Interop.WiFiDirect.ServiceStateChangedCallback _serviceStateChangedCallback; + + internal string _peerDeviceName; + internal string _peerIpAddress; + internal string _peerMacAddress; + internal string _peerInterfaceAddress; + internal int _peerChannel; + internal bool _isPeerConnected; + internal bool _isPeerGroupOwner; + internal bool _isPeerPersistentGroupOwner; + internal bool _peerP2PSupport; + internal WiFiDirectPrimaryDeviceType _peerPrimaryType; + internal WiFiDirectSecondaryDeviceType _peerSecondaryType; + internal int _peerWpsTypes; + internal bool _p2PInvitationSupported; + internal uint _peerServiceCount; + internal IEnumerable<string> _peerServiceList; + internal bool _isPeerMiracastDevice; + + internal WiFiDirectPeer() + { + } + + /// <summary> + /// Name of Peer device. + /// </summary> + public string Name + { + get + { + return _peerDeviceName; + } + } + + /// <summary> + /// Ip address of the peer device. + /// </summary> + public string IpAddress + { + get + { + return _peerIpAddress; + } + } + + /// <summary> + /// Mac address of the peer device. + /// </summary> + public string MacAddress + { + get + { + return _peerMacAddress; + } + } + + /// <summary> + /// Interface address of the peer device. + /// </summary> + public string InterfaceAddress + { + get + { + return _peerInterfaceAddress; + } + } + + /// <summary> + /// Listening channel of the peer device. + /// </summary> + public int Channel + { + get + { + return _peerChannel; + } + } + + /// <summary> + /// Connected state of the peer device. + /// </summary> + public bool IsConnected + { + get + { + return _isPeerConnected; + } + } + + /// <summary> + /// P2P group state of the peer device. + /// </summary> + public bool IsGroupOwner + { + get + { + return _isPeerGroupOwner; + } + } + + /// <summary> + /// Persistent group state of the peer device. + /// </summary> + public bool IsPersistentGroupOwner + { + get + { + return _isPeerPersistentGroupOwner; + } + } + + /// <summary> + /// P2P state of the peer device. + /// </summary> + public bool P2PSupport + { + get + { + return _peerP2PSupport; + } + } + + /// <summary> + /// Primary catagory of the peer device. + /// </summary> + public WiFiDirectPrimaryDeviceType PrimaryType + { + get + { + return _peerPrimaryType; + } + } + + /// <summary> + /// Sub category of the peer device. + /// </summary> + public WiFiDirectSecondaryDeviceType SecondaryType + { + get + { + return _peerSecondaryType; + } + } + + /// <summary> + /// List of supported WPS type of the peer device. + /// </summary> + public int WpsTypes + { + get + { + return _peerWpsTypes; + } + } + + /// <summary> + /// P2P invitation state of the peer device. + /// </summary> + public bool IsP2PInvitationSupported + { + get + { + return _p2PInvitationSupported; + } + } + + /// <summary> + /// Number of registered services of the peer device. + /// </summary> + public uint ServiceCount + { + get + { + return _peerServiceCount; + } + } + + /// <summary> + /// List of registered services of the peer device. + /// </summary> + public IEnumerable<string> ServiceList + { + get + { + return _peerServiceList; + } + } + + /// <summary> + /// Checks if peer device is a wifi display device. + /// </summary> + public bool IsMiracastDevice + { + get + { + return _isPeerMiracastDevice; + } + } + + /// <summary> + /// Wi-Fi Display device type of the peer device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If there is any error, default value of WiFiDirectDisplayType will be returned. + /// </remarks> + public WiFiDirectDisplayType Display + { + get + { + if (Globals.IsActivated) + { + WiFiDirectDisplayType displayType; + int ret = Interop.WiFiDirect.GetDisplayType(_peerMacAddress, out displayType); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the peer display type, Error - " + (WiFiDirectError)ret); + } + + return displayType; + } + + else + { + return default(WiFiDirectDisplayType); + } + } + } + + /// <summary> + /// Wi-Fi Display Session Availability of the peer device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If there is any error, false will be returned. + /// </remarks> + public bool DisplayAvailability + { + get + { + if (Globals.IsActivated) + { + bool displayAvailability; + int ret = Interop.WiFiDirect.GetDisplayAvailability(_peerMacAddress, out displayAvailability); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the peer display availability, Error - " + (WiFiDirectError)ret); + } + + return displayAvailability; + } + + else + { + return false; + } + } + } + + /// <summary> + /// Hdcp information of the peer device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If there is any error, -1 will be returned. + /// </remarks> + public int Hdcp + { + get + { + if (Globals.IsActivated) + { + int hdcpSupport; + int ret = Interop.WiFiDirect.GetDisplayHdcp(_peerMacAddress, out hdcpSupport); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the peer display hdcp support, Error - " + (WiFiDirectError)ret); + return -1; + } + + return hdcpSupport; + } + + else + { + return -1; + } + } + } + + /// <summary> + /// Port of the connected peer device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If there is any error, -1 will be returned. + /// </remarks> + public int Port + { + get + { + if (Globals.IsActivated) + { + int displayPort; + int ret = Interop.WiFiDirect.GetDisplayPort(_peerMacAddress, out displayPort); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the peer display port, Error - " + (WiFiDirectError)ret); + return -1; + } + + return displayPort; + } + + else + { + return -1; + } + } + } + + /// <summary> + /// WiFi Display max throughput of the peer device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If there is any error, -1 will be returned. + /// </remarks> + public int Throughput + { + get + { + if (Globals.IsActivated) + { + int displayThroughput; + int ret = Interop.WiFiDirect.GetDisplayThroughput(_peerMacAddress, out displayThroughput); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to get the peer display max throughput, Error - " + (WiFiDirectError)ret); + return -1; + } + + return displayThroughput; + } + + else + { + return -1; + } + } + } + + /// <summary> + /// (event) ConnectionStateChanged event is raised when the connection state of the peer device changes. + /// </summary> + public event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged + { + add + { + if (Globals.IsInitialize) + { + if (_connectionStateChanged == null) + { + RegisterConnectionStateChangedEvent(); + } + + _connectionStateChanged += value; + } + } + + remove + { + if (Globals.IsInitialize) + { + _connectionStateChanged -= value; + if (_connectionStateChanged == null) + { + UnregisterConnectionStateChangedEvent(); + } + } + } + } + + /// <summary> + /// (event) IpAddressAssigned event is raised when ip address of the peer device is assigned. + /// </summary> + public event EventHandler<IpAddressAssignedEventArgs> IpAddressAssigned + { + add + { + if (Globals.IsInitialize) + { + if (_ipAddressAssigned == null) + { + RegisterIpAddressAssignedEvent(); + } + + _ipAddressAssigned += value; + } + } + + remove + { + if (Globals.IsInitialize) + { + _ipAddressAssigned -= value; + if (_ipAddressAssigned == null) + { + UnregisterIpAddressAssignedEvent(); + } + } + } + } + + /// <summary> + /// (event) ServiceStateChanged is raised when state of service discovery is changed. + /// </summary> + public event EventHandler<ServiceStateChangedEventArgs> ServiceStateChanged + { + add + { + if (Globals.IsInitialize) + { + if (_serviceStateChanged == null) + { + RegisterServiceStateChangedEvent(); + } + + _serviceStateChanged += value; + } + } + + remove + { + if (Globals.IsInitialize) + { + _serviceStateChanged -= value; + if (_serviceStateChanged == null) + { + UnregisterServiceStateChangedEvent(); + } + } + } + } + + private void RegisterConnectionStateChangedEvent() + { + _connectionStateChangedCallback = (int result, WiFiDirectConnectionState state, string address, IntPtr userData) => + { + if (_connectionStateChanged != null) + { + WiFiDirectError res = (WiFiDirectError)result; + _connectionStateChanged(null, new ConnectionStateChangedEventArgs(res, state, address)); + } + }; + int ret = Interop.WiFiDirect.SetConnectionChangedCallback(_connectionStateChangedCallback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set connection state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void UnregisterConnectionStateChangedEvent() + { + int ret = Interop.WiFiDirect.UnsetConnectionChangedCallback(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to unset connection state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void RegisterIpAddressAssignedEvent() + { + _ipAddressAssignedCallback = (string macAddress, string ipAddress, string interfaceAddress, IntPtr userData) => + { + if (_ipAddressAssigned != null) + { + _ipAddressAssigned(null, new IpAddressAssignedEventArgs(macAddress, ipAddress, interfaceAddress)); + } + }; + int ret = Interop.WiFiDirect.SetIpAddressAssignedCallback(_ipAddressAssignedCallback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set ip address assigned callback, Error - " + (WiFiDirectError)ret); + } + } + + private void UnregisterIpAddressAssignedEvent() + { + int ret = Interop.WiFiDirect.UnsetIpAddressAssignedCallback(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to unset ip address assigned callback, Error - " + (WiFiDirectError)ret); + } + } + + private void RegisterServiceStateChangedEvent() + { + _serviceStateChangedCallback = (int result, WiFiDirectServiceDiscoveryState stateInfo, WiFiDirectServiceType typeInfo, IntPtr responseData, string address, IntPtr userData) => + { + if (_serviceStateChanged != null) + { + WiFiDirectError error = (WiFiDirectError)result; + WiFiDirectServiceDiscoveryState state = stateInfo; + WiFiDirectServiceType type = typeInfo; + string response = Marshal.PtrToStringAnsi(responseData); + IntPtr peer; + Interop.WiFiDirect.GetDiscoveredPeerInfo(address, out peer); + DiscoveredPeerStruct peerStruct = (DiscoveredPeerStruct)Marshal.PtrToStructure(peer, typeof(DiscoveredPeerStruct)); + + _serviceStateChanged(null, new ServiceStateChangedEventArgs(error, state, type, response, WiFiDirectUtils.ConvertStructToDiscoveredPeer(peerStruct))); + } + }; + int ret = Interop.WiFiDirect.SetServiceStateChangedCallback(_serviceStateChangedCallback, IntPtr.Zero); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set service state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + private void UnregisterServiceStateChangedEvent() + { + int ret = Interop.WiFiDirect.UnsetServiceStateChangedCallback(); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to unset service state changed callback, Error - " + (WiFiDirectError)ret); + } + } + + /// <summary> + /// Connects to a specified remote device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If this succeeds, ConnectionStateChanged event will be invoked. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public void Connect() + { + if (Globals.IsActivated) + { + int ret = Interop.WiFiDirect.Connect(_peerMacAddress); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to connect, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Cancels the connection now in progress. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public void CancelConnection() + { + if (Globals.IsActivated) + { + int ret = Interop.WiFiDirect.CancelConnection(_peerMacAddress); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to cancel the connection, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Disconnects the specified remote device. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If this succeeds, ConnectionStateChanged event will be invoked. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public void Disconnect() + { + if (Globals.IsActivated) + { + int ret = Interop.WiFiDirect.Disconnect(_peerMacAddress); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to disconnect, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Allows a device to connect automatically. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// </remarks> + /// <exception cref="NotSupportedException">Thrown when the wifidirect is not supported</exception> + public void SetAutoConnect() + { + if (Globals.IsActivated) + { + int ret = Interop.WiFiDirect.SetAutoConnectionPeer(_peerMacAddress); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to set auto connection, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Starts the Wi-Fi Direct service discovery. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// If this succeeds, ServiceStateChanged event will be invoked. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect service discovery is not supported + /// </exception> + /// <param name="type">Type of service.</param> + public void StartServiceDiscovery(WiFiDirectServiceType type) + { + if (Globals.IsActivated) + { + int ret = Interop.WiFiDirect.StartServiceDiscovery(_peerMacAddress, type); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to start Wi-Fi Direct service discovery, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + + /// <summary> + /// Stops the Wi-Fi Direct service discovery. + /// </summary> + /// <remarks> + /// Wi-Fi Direct must be activated. + /// </remarks> + /// <exception cref="NotSupportedException"> + /// Thrown during one of the following cases : + /// 1. When the wifidirect is not supported + /// 2. When the wifidirect service discovery is not supported + /// </exception> + /// <param name="type">Type of service.</param> + public void CancelServiceDiscovery(WiFiDirectServiceType type) + { + if (Globals.IsActivated) + { + int ret = Interop.WiFiDirect.StopServiceDiscovery(_peerMacAddress, type); + if (ret != (int)WiFiDirectError.None) + { + Log.Error(Globals.LogTag, "Failed to stop Wi-Fi Direct service discovery, Error - " + (WiFiDirectError)ret); + WiFiDirectErrorFactory.ThrowWiFiDirectException(ret); + } + } + + else + { + Log.Error(Globals.LogTag, "Wifi-direct is not activated"); + WiFiDirectErrorFactory.ThrowWiFiDirectException((int)WiFiDirectError.NotPermitted); + } + } + } +} diff --git a/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectPersistentGroup.cs b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectPersistentGroup.cs new file mode 100644 index 0000000..10274c9 --- /dev/null +++ b/Tizen.Network.WiFiDirect/Tizen.Network.WiFiDirect/WiFiDirectPersistentGroup.cs @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; + +namespace Tizen.Network.WiFiDirect +{ + /// <summary> + /// A class to handle persistent groups. + /// </summary> + public class WiFiDirectPersistentGroup + { + private string _address; + private string _ssid; + + internal WiFiDirectPersistentGroup(string address, string id) + { + _address = address; + _ssid = id; + } + + /// <summary> + /// MAC address of the persistent group owner. + /// </summary> + public string MacAddress + { + get + { + return _address; + } + } + + /// <summary> + /// SSID (Service Set Identifier) of the persistent group owner. + /// </summary> + public string Ssid + { + get + { + return _ssid; + } + } + } +} |