[C# Wi-Fi Direct] Adding C# Wi-Fi Direct code
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFiDirect / Tizen.Network.WiFiDirect / WiFiDirectData.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using System.Collections.ObjectModel;
20
21 namespace Tizen.Network.WiFiDirect
22 {
23     [StructLayout(LayoutKind.Sequential)]
24     internal struct DiscoveredPeerStruct
25     {
26         [MarshalAsAttribute(UnmanagedType.LPStr)]
27         internal string _name;
28
29         [MarshalAsAttribute(UnmanagedType.LPStr)]
30         internal string _macAddress;
31
32         [MarshalAsAttribute(UnmanagedType.LPStr)]
33         internal string _interfaceAddress;
34
35         internal int _channel;
36
37         [MarshalAsAttribute(UnmanagedType.I1)]
38         internal bool _isConnected;
39
40         [MarshalAsAttribute(UnmanagedType.I1)]
41         internal bool _isGroupOwner;
42
43         [MarshalAsAttribute(UnmanagedType.I1)]
44         internal bool _isPersistentGroupOwner;
45
46         internal WiFiDirectPrimaryDeviceType _primaryType;
47
48         internal WiFiDirectSecondaryDeviceType _secondaryType;
49
50         internal int _wpsTypes;
51
52         [MarshalAsAttribute(UnmanagedType.I1)]
53         internal bool _isP2PInvitationSupported;
54
55         internal uint _serviceCount;
56
57         internal IntPtr _serviceList;
58
59         [MarshalAsAttribute(UnmanagedType.I1)]
60         internal bool _isMiracast;
61     }
62
63     [StructLayout(LayoutKind.Sequential)]
64     internal struct ConnectedPeerStruct
65     {
66         [MarshalAsAttribute(UnmanagedType.LPStr)]
67         internal string _name;
68
69         [MarshalAsAttribute(UnmanagedType.LPStr)]
70         internal string _ipAddress;
71
72         [MarshalAsAttribute(UnmanagedType.LPStr)]
73         internal string _macAddress;
74
75         [MarshalAsAttribute(UnmanagedType.LPStr)]
76         internal string _interfaceAddress;
77
78         internal int _channel;
79
80         [MarshalAsAttribute(UnmanagedType.I1)]
81         internal bool _isP2PSupport;
82
83         internal WiFiDirectPrimaryDeviceType _primaryType;
84
85         internal WiFiDirectSecondaryDeviceType _secondaryType;
86
87         internal uint _serviceCount;
88
89         internal IntPtr _serviceList;
90
91         [MarshalAsAttribute(UnmanagedType.I1)]
92         internal bool _isMiracast;
93     }
94
95     internal static class WiFiDirectUtils
96     {
97         internal static WiFiDirectPeer ConvertStructToDiscoveredPeer(DiscoveredPeerStruct peer)
98         {
99             WiFiDirectPeer resultPeer = new WiFiDirectPeer();
100             resultPeer._peerDeviceName = peer._name;
101             resultPeer._peerMacAddress = peer._macAddress;
102             resultPeer._peerInterfaceAddress = peer._interfaceAddress;
103             resultPeer._peerChannel = peer._channel;
104             resultPeer._isPeerConnected = peer._isConnected;
105             resultPeer._isPeerGroupOwner = peer._isGroupOwner;
106             resultPeer._isPeerPersistentGroupOwner = peer._isPersistentGroupOwner;
107             resultPeer._peerPrimaryType = peer._primaryType;
108             resultPeer._peerSecondaryType = peer._secondaryType;
109             resultPeer._peerWpsTypes = peer._wpsTypes;
110             resultPeer._p2PInvitationSupported = peer._isP2PInvitationSupported;
111             Collection<string> uuidList = null;
112
113             if (peer._serviceCount > 0)
114             {
115                 IntPtr[] serviceList = new IntPtr[peer._serviceCount];
116                 Marshal.Copy(peer._serviceList, serviceList, 0, (int)peer._serviceCount);
117                 uuidList = new Collection<string>();
118                 foreach (IntPtr service in serviceList)
119                 {
120                     string registeredService = Marshal.PtrToStringAnsi(service);
121                     uuidList.Add(registeredService);
122                 }
123
124                 resultPeer._peerServiceCount = peer._serviceCount;
125                 resultPeer._peerServiceList = uuidList;
126             }
127
128             resultPeer._isPeerMiracastDevice = peer._isMiracast;
129             return resultPeer;
130         }
131
132         internal static WiFiDirectPeer ConvertStructToConnectedPeer(ConnectedPeerStruct peer)
133         {
134             WiFiDirectPeer resultPeer = new WiFiDirectPeer();
135             resultPeer._peerDeviceName = peer._name;
136             resultPeer._peerIpAddress = peer._ipAddress;
137             resultPeer._peerMacAddress = peer._macAddress;
138             resultPeer._peerInterfaceAddress = peer._interfaceAddress;
139             resultPeer._peerChannel = peer._channel;
140             resultPeer._peerP2PSupport = peer._isP2PSupport;
141             resultPeer._peerPrimaryType = peer._primaryType;
142             resultPeer._peerSecondaryType = peer._secondaryType;
143             Collection<string> uuidList = null;
144
145             if (peer._serviceCount > 0)
146             {
147                 IntPtr[] serviceList = new IntPtr[peer._serviceCount];
148                 Marshal.Copy(peer._serviceList, serviceList, 0, (int)peer._serviceCount);
149                 uuidList = new Collection<string>();
150                 foreach (IntPtr service in serviceList)
151                 {
152                     string registeredService = Marshal.PtrToStringAnsi(service);
153                     uuidList.Add(registeredService);
154                 }
155
156                 resultPeer._peerServiceCount = peer._serviceCount;
157                 resultPeer._peerServiceList = uuidList;
158             }
159
160             resultPeer._isPeerMiracastDevice = peer._isMiracast;
161             return resultPeer;
162         }
163     }
164
165 }