754cba18a0d92165bfba3c18d67bdcf45073fb37
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothStructs.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.Reflection;
19 using System.Linq;
20 using System.Collections.Generic;
21 using System.Collections.ObjectModel;
22 using System.Collections.Specialized;
23 using System.Runtime.InteropServices;
24
25 namespace Tizen.Network.Bluetooth
26 {
27     /// <summary>
28     /// The structure of the device class type and service.
29     /// </summary>
30     [StructLayout(LayoutKind.Sequential)]
31     internal struct BluetoothClassStruct
32     {
33         /// <summary>
34         /// The type of the major device class.
35         /// </summary>
36         internal BluetoothMajorDeviceClassType MajorDeviceClassType;
37         /// <summary>
38         /// The type of the minor device class.
39         /// </summary>
40         internal BluetoothMinorDeviceClassType MinorDeviceClassType;
41         /// <summary>
42         /// The major service class mask.
43         /// </summary>
44         internal int MajorServiceClassMask;
45     }
46
47     /// <summary>
48     /// This structure contains the information of the Bluetooth device.
49     /// </summary>
50     [StructLayout(LayoutKind.Sequential)]
51     internal struct BluetoothDeviceStruct
52     {
53         /// <summary>
54         /// The address of the device.
55         /// </summary>
56         [MarshalAsAttribute(UnmanagedType.LPStr)]
57         internal string Address;
58
59         /// <summary>
60         /// The name of the device.
61         /// </summary>
62         [MarshalAsAttribute(UnmanagedType.LPStr)]
63         internal string Name;
64
65         /// <summary>
66         /// The class of the device.
67         /// </summary>
68         internal BluetoothClassStruct Class;
69
70         /// <summary>
71         /// The service UUID list of the device.
72         /// </summary>
73         internal IntPtr ServiceUuidList;
74
75         /// <summary>
76         /// The service count of the device.
77         /// </summary>
78         internal int ServiceCount;
79
80         /// <summary>
81         /// The paired state of the device.
82         /// </summary>
83         [MarshalAsAttribute(UnmanagedType.I1)]
84         internal bool IsPaired;
85
86         /// <summary>
87         /// The connection state of the device.
88         /// </summary>
89         [MarshalAsAttribute(UnmanagedType.I1)]
90         internal bool IsConnected;
91
92         /// <summary>
93         /// The authorization state of the device.
94         /// </summary>
95         [MarshalAsAttribute(UnmanagedType.I1)]
96         internal bool IsAuthorized;
97
98         /// <summary>
99         /// The length of the manufacturer the data.
100         /// </summary>
101         internal int ManufacturerDataLength;
102
103         /// <summary>
104         /// The manufacturer data.
105         /// </summary>
106         [MarshalAsAttribute(UnmanagedType.LPStr)]
107         internal string ManufacturerData;
108     }
109
110     [StructLayout(LayoutKind.Sequential)]
111     internal struct BluetoothDiscoveredDeviceStruct
112     {
113         [MarshalAsAttribute(UnmanagedType.LPStr)]
114         internal string Address;
115
116         [MarshalAsAttribute(UnmanagedType.LPStr)]
117         internal string Name;
118
119         internal BluetoothClassStruct Class;
120
121         internal int Rssi;
122
123         [MarshalAsAttribute(UnmanagedType.I1)]
124         internal bool IsPaired;
125
126         internal IntPtr ServiceUuidList;
127
128         internal int ServiceCount;
129
130         internal BluetoothAppearanceType AppearanceType;
131
132         internal int ManufacturerDataLength;
133
134         [MarshalAsAttribute(UnmanagedType.LPStr)]
135         internal string ManufacturerData;
136     }
137
138     [StructLayout(LayoutKind.Sequential)]
139     internal struct BluetoothDeviceSdpStruct
140     {
141         [MarshalAsAttribute(UnmanagedType.LPStr)]
142         internal string DeviceAddress;
143         internal IntPtr ServiceUuid;
144         internal int ServiceCount;
145     }
146
147     [StructLayout(LayoutKind.Sequential)]
148     internal struct BluetoothDeviceConnectionStruct
149     {
150         internal string Address;
151         internal BluetoothConnectionLinkType LinkType;
152         internal BluetoothDisconnectReason DisconnectReason;
153     }
154
155     [StructLayout(LayoutKind.Sequential)]
156     internal struct SocketDataStruct
157     {
158         internal int SocketFd;
159         internal int DataSize;
160         internal IntPtr Data;
161     }
162
163     [StructLayout(LayoutKind.Sequential)]
164     internal struct SocketConnectionStruct
165     {
166         internal int SocketFd;
167         internal int ServerFd;
168         internal BluetoothSocketRole LocalRole;
169         internal string Address;
170         internal string ServiceUuid;
171     }
172
173     /// </summary>
174     [StructLayout(LayoutKind.Sequential)]
175     internal struct BluetoothLeScanDataStruct
176     {
177         [MarshalAsAttribute(UnmanagedType.LPStr)]
178         internal string RemoteAddress;
179
180         internal BluetoothLeDeviceAddressType AddressType;
181
182         internal int Rssi;
183
184         internal int AdvDataLength;
185
186         internal IntPtr AdvData;
187
188         internal int ScanDataLength;
189
190         internal IntPtr ScanData;
191     }
192
193     [StructLayout(LayoutKind.Sequential)]
194     internal struct BluetoothLeServiceDataStruct
195     {
196         /// <summary>
197         /// The Bluetooth LE service UUID.
198         /// </summary>
199         [MarshalAs(UnmanagedType.LPStr)]
200         internal string ServiceUuid;
201         /// <summary>
202         /// The Bluetooth LE service data.
203         /// </summary>
204         internal IntPtr ServiceData;
205
206         internal int ServiceDataLength;
207     }
208
209     internal static class BluetoothUtils
210     {
211         internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
212         {
213             BluetoothDevice resultDevice = new BluetoothDevice();
214             Collection<string> uuidList = null;
215
216             if (device.ServiceCount > 0)
217             {
218                 IntPtr[] extensionList = new IntPtr[device.ServiceCount];
219                 Marshal.Copy (device.ServiceUuidList, extensionList, 0, device.ServiceCount);
220                 uuidList = new Collection<string> ();
221                 foreach (IntPtr extension in extensionList) {
222                     string uuid = Marshal.PtrToStringAnsi (extension);
223                     uuidList.Add (uuid);
224                 }
225             }
226
227             resultDevice.RemoteDeviceAddress = device.Address;
228             resultDevice.RemoteDeviceName = device.Name;
229             resultDevice.RemoteDeviceClass = new BluetoothClass();
230             resultDevice.Class.MajorType = device.Class.MajorDeviceClassType;
231             resultDevice.Class.MinorType = device.Class.MinorDeviceClassType;
232             resultDevice.Class.Mask = device.Class.MajorServiceClassMask;
233             resultDevice.RemotePaired = device.IsPaired;
234             resultDevice.RemoteConnected = device.IsConnected;
235             resultDevice.RemoteAuthorized = device.IsAuthorized;
236             resultDevice.RemoteDeviceService = uuidList;
237             resultDevice.RemoteDeviceCount = device.ServiceCount;
238             resultDevice.RemoteManufLength = device.ManufacturerDataLength;
239             resultDevice.RemoteManufData = device.ManufacturerData;
240
241             return resultDevice;
242         }
243
244         internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
245         {
246             BluetoothDevice resultDevice = new BluetoothDevice();
247             Collection<string> uuidList = null;
248
249             if (structDevice.ServiceCount > 0) {
250                 IntPtr[] extensionList = new IntPtr[structDevice.ServiceCount];
251                 Marshal.Copy (structDevice.ServiceUuidList, extensionList, 0, structDevice.ServiceCount);
252                 uuidList = new Collection<string> ();
253                 foreach (IntPtr extension in extensionList) {
254                     string uuid = Marshal.PtrToStringAnsi (extension);
255                     uuidList.Add (uuid);
256                 }
257             }
258
259             resultDevice.RemoteDeviceAddress = structDevice.Address;
260             resultDevice.RemoteDeviceName = structDevice.Name;
261
262             resultDevice.RemoteDeviceClass = new BluetoothClass();
263             resultDevice.Class.MajorType = structDevice.Class.MajorDeviceClassType;
264             resultDevice.Class.MinorType = structDevice.Class.MinorDeviceClassType;
265             resultDevice.Class.Mask = structDevice.Class.MajorServiceClassMask;
266
267             resultDevice.RemoteDeviceRssi = structDevice.Rssi;
268             resultDevice.RemoteAppearance = structDevice.AppearanceType;
269
270             if (structDevice.ServiceCount > 0) {
271                 resultDevice.RemoteDeviceService = uuidList;
272                 resultDevice.RemoteDeviceCount = structDevice.ServiceCount;
273             }
274
275             resultDevice.RemotePaired = structDevice.IsPaired;
276             resultDevice.RemoteManufData = structDevice.ManufacturerData;
277             resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
278             return resultDevice;
279         }
280
281         internal static BluetoothDeviceSdpData ConvertStructToSdpData(BluetoothDeviceSdpStruct structData)
282         {
283             BluetoothDeviceSdpData resultData = new BluetoothDeviceSdpData();
284             Collection<string> uuidList = null;
285
286             if (structData.ServiceCount > 0) {
287                 IntPtr[] extensionList = new IntPtr[structData.ServiceCount];
288                 Marshal.Copy (structData.ServiceUuid, extensionList, 0, structData.ServiceCount);
289                 uuidList = new Collection<string> ();
290                 foreach (IntPtr extension in extensionList) {
291                     string uuid = Marshal.PtrToStringAnsi (extension);
292                     uuidList.Add (uuid);
293                 }
294             }
295
296             resultData.Uuid = uuidList;
297             resultData.Address = structData.DeviceAddress;
298
299             return resultData;
300         }
301
302         internal static BluetoothDeviceConnectionData ConvertStructToConnectionData(BluetoothDeviceConnectionStruct structInfo)
303         {
304             BluetoothDeviceConnectionData resultData = new BluetoothDeviceConnectionData();
305             resultData.RemoteAddress = structInfo.Address;
306             resultData.Link = structInfo.LinkType;
307             resultData.Reason = structInfo.DisconnectReason;
308             return resultData;
309         }
310
311         internal static BluetoothLeScanData ConvertStructToLeScanData(BluetoothLeScanDataStruct structScanData)
312         {
313             BluetoothLeScanData scanData = new BluetoothLeScanData();
314
315             scanData.RemoteAddress = structScanData.RemoteAddress;
316             scanData.AddressType = structScanData.AddressType;
317             scanData.Rssi = structScanData.Rssi;
318
319             if (structScanData.AdvDataLength > 0)
320             {
321                 scanData.AdvDataLength = structScanData.AdvDataLength;
322                 scanData.AdvData = new byte[scanData.AdvDataLength];
323                 Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength);
324             }
325
326             if (structScanData.ScanDataLength > 0)
327             {
328                 scanData.ScanDataLength = structScanData.ScanDataLength;
329                 scanData.ScanData = new byte[scanData.ScanDataLength];
330                 Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength);
331             }
332             return scanData;
333         }
334
335         internal static BluetoothLeScanDataStruct ConvertLeScanDataToStruct(BluetoothLeScanData scanData)
336         {
337             BluetoothLeScanDataStruct scanDataStruct = new BluetoothLeScanDataStruct();
338
339             scanDataStruct.RemoteAddress = scanData.RemoteAddress;
340             scanDataStruct.AddressType = scanData.AddressType;
341             scanDataStruct.Rssi = scanData.Rssi;
342
343             if (scanData.AdvDataLength > 0)
344             {
345                 scanDataStruct.AdvDataLength = scanData.AdvDataLength;
346                 scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength);
347                 Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength);
348             }
349
350             if (scanData.ScanDataLength > 0)
351             {
352                 scanDataStruct.ScanDataLength = scanData.ScanDataLength;
353                 scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength);
354                 Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength);
355             }
356
357             return scanDataStruct;
358         }
359
360         internal static BluetoothLeServiceData ConvertStructToLeServiceData(BluetoothLeServiceDataStruct structServiceData)
361         {
362             BluetoothLeServiceData serviceData = new BluetoothLeServiceData();
363             Log.Info(Globals.LogTag, "ServiceDataLength" + structServiceData.ServiceDataLength);
364
365             if (structServiceData.ServiceDataLength > 0)
366             {
367                 serviceData.Uuid = structServiceData.ServiceUuid;
368                 serviceData.Length = structServiceData.ServiceDataLength;
369                 serviceData.Data = new byte[serviceData.Length];
370                 Marshal.Copy(structServiceData.ServiceData, serviceData.Data, 0, serviceData.Length);
371             }
372             return serviceData;
373         }
374
375         internal static SocketData ConvertStructToSocketData(SocketDataStruct structInfo)
376         {
377             SocketData data = new SocketData();
378             data.Fd = structInfo.SocketFd;
379             data.Size = structInfo.DataSize;
380             data.RecvData = Marshal.PtrToStringAnsi(structInfo.Data);
381             return data;
382         }
383
384         internal static SocketConnection ConvertStructToSocketConnection(SocketConnectionStruct structInfo)
385         {
386             SocketConnection connectionInfo = new SocketConnection();
387             connectionInfo.Fd = structInfo.SocketFd;
388             connectionInfo.RemoteAddress = structInfo.Address;
389             connectionInfo.Uuid = structInfo.ServiceUuid;
390             return connectionInfo;
391         }
392     }
393 }
394