Release 4.0.0-preview1-00285
[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     [StructLayout(LayoutKind.Sequential)]
174     internal struct BluetoothLeScanDataStruct
175     {
176         [MarshalAsAttribute(UnmanagedType.LPStr)]
177         internal string RemoteAddress;
178
179         internal BluetoothLeDeviceAddressType AddressType;
180
181         internal int Rssi;
182
183         internal int AdvDataLength;
184
185         internal IntPtr AdvData;
186
187         internal int ScanDataLength;
188
189         internal IntPtr ScanData;
190     }
191
192     [StructLayout(LayoutKind.Sequential)]
193     internal struct BluetoothLeServiceDataStruct
194     {
195         /// <summary>
196         /// The Bluetooth LE service UUID.
197         /// </summary>
198         [MarshalAs(UnmanagedType.LPStr)]
199         internal string ServiceUuid;
200         /// <summary>
201         /// The Bluetooth LE service data.
202         /// </summary>
203         internal IntPtr ServiceData;
204
205         internal int ServiceDataLength;
206     }
207
208     internal static class BluetoothUtils
209     {
210         internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
211         {
212             BluetoothDevice resultDevice = new BluetoothDevice();
213             Collection<string> uuidList = null;
214
215             if (device.ServiceCount > 0)
216             {
217                 IntPtr[] extensionList = new IntPtr[device.ServiceCount];
218                 Marshal.Copy (device.ServiceUuidList, extensionList, 0, device.ServiceCount);
219                 uuidList = new Collection<string> ();
220                 foreach (IntPtr extension in extensionList) {
221                     string uuid = Marshal.PtrToStringAnsi (extension);
222                     uuidList.Add (uuid);
223                 }
224             }
225
226             resultDevice.RemoteDeviceAddress = device.Address;
227             resultDevice.RemoteDeviceName = device.Name;
228             resultDevice.RemoteDeviceClass = new BluetoothClass();
229             resultDevice.Class.MajorType = device.Class.MajorDeviceClassType;
230             resultDevice.Class.MinorType = device.Class.MinorDeviceClassType;
231             resultDevice.Class.Mask = device.Class.MajorServiceClassMask;
232             resultDevice.RemotePaired = device.IsPaired;
233             resultDevice.RemoteConnected = device.IsConnected;
234             resultDevice.RemoteAuthorized = device.IsAuthorized;
235             resultDevice.RemoteDeviceService = uuidList;
236             resultDevice.RemoteDeviceCount = device.ServiceCount;
237             resultDevice.RemoteManufLength = device.ManufacturerDataLength;
238             resultDevice.RemoteManufData = device.ManufacturerData;
239
240             return resultDevice;
241         }
242
243         internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
244         {
245             BluetoothDevice resultDevice = new BluetoothDevice();
246             Collection<string> uuidList = null;
247
248             if (structDevice.ServiceCount > 0) {
249                 IntPtr[] extensionList = new IntPtr[structDevice.ServiceCount];
250                 Marshal.Copy (structDevice.ServiceUuidList, extensionList, 0, structDevice.ServiceCount);
251                 uuidList = new Collection<string> ();
252                 foreach (IntPtr extension in extensionList) {
253                     string uuid = Marshal.PtrToStringAnsi (extension);
254                     uuidList.Add (uuid);
255                 }
256             }
257
258             resultDevice.RemoteDeviceAddress = structDevice.Address;
259             resultDevice.RemoteDeviceName = structDevice.Name;
260
261             resultDevice.RemoteDeviceClass = new BluetoothClass();
262             resultDevice.Class.MajorType = structDevice.Class.MajorDeviceClassType;
263             resultDevice.Class.MinorType = structDevice.Class.MinorDeviceClassType;
264             resultDevice.Class.Mask = structDevice.Class.MajorServiceClassMask;
265
266             resultDevice.RemoteDeviceRssi = structDevice.Rssi;
267             resultDevice.RemoteAppearance = structDevice.AppearanceType;
268
269             if (structDevice.ServiceCount > 0) {
270                 resultDevice.RemoteDeviceService = uuidList;
271                 resultDevice.RemoteDeviceCount = structDevice.ServiceCount;
272             }
273
274             resultDevice.RemotePaired = structDevice.IsPaired;
275             resultDevice.RemoteManufData = structDevice.ManufacturerData;
276             resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
277             return resultDevice;
278         }
279
280         internal static BluetoothDeviceSdpData ConvertStructToSdpData(BluetoothDeviceSdpStruct structData)
281         {
282             BluetoothDeviceSdpData resultData = new BluetoothDeviceSdpData();
283             Collection<string> uuidList = null;
284
285             if (structData.ServiceCount > 0) {
286                 IntPtr[] extensionList = new IntPtr[structData.ServiceCount];
287                 Marshal.Copy (structData.ServiceUuid, extensionList, 0, structData.ServiceCount);
288                 uuidList = new Collection<string> ();
289                 foreach (IntPtr extension in extensionList) {
290                     string uuid = Marshal.PtrToStringAnsi (extension);
291                     uuidList.Add (uuid);
292                 }
293             }
294
295             resultData.Uuid = uuidList;
296             resultData.Address = structData.DeviceAddress;
297
298             return resultData;
299         }
300
301         internal static BluetoothDeviceConnectionData ConvertStructToConnectionData(BluetoothDeviceConnectionStruct structInfo)
302         {
303             BluetoothDeviceConnectionData resultData = new BluetoothDeviceConnectionData();
304             resultData.RemoteAddress = structInfo.Address;
305             resultData.Link = structInfo.LinkType;
306             resultData.Reason = structInfo.DisconnectReason;
307             return resultData;
308         }
309
310         internal static BluetoothLeScanData ConvertStructToLeScanData(BluetoothLeScanDataStruct structScanData)
311         {
312             BluetoothLeScanData scanData = new BluetoothLeScanData();
313
314             scanData.RemoteAddress = structScanData.RemoteAddress;
315             scanData.AddressType = structScanData.AddressType;
316             scanData.Rssi = structScanData.Rssi;
317
318             if (structScanData.AdvDataLength > 0)
319             {
320                 scanData.AdvDataLength = structScanData.AdvDataLength;
321                 scanData.AdvData = new byte[scanData.AdvDataLength];
322                 Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength);
323             }
324
325             if (structScanData.ScanDataLength > 0)
326             {
327                 scanData.ScanDataLength = structScanData.ScanDataLength;
328                 scanData.ScanData = new byte[scanData.ScanDataLength];
329                 Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength);
330             }
331             return scanData;
332         }
333
334         internal static BluetoothLeScanDataStruct ConvertLeScanDataToStruct(BluetoothLeScanData scanData)
335         {
336             BluetoothLeScanDataStruct scanDataStruct = new BluetoothLeScanDataStruct();
337
338             scanDataStruct.RemoteAddress = scanData.RemoteAddress;
339             scanDataStruct.AddressType = scanData.AddressType;
340             scanDataStruct.Rssi = scanData.Rssi;
341
342             if (scanData.AdvDataLength > 0)
343             {
344                 scanDataStruct.AdvDataLength = scanData.AdvDataLength;
345                 scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength);
346                 Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength);
347             }
348
349             if (scanData.ScanDataLength > 0)
350             {
351                 scanDataStruct.ScanDataLength = scanData.ScanDataLength;
352                 scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength);
353                 Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength);
354             }
355
356             return scanDataStruct;
357         }
358
359         internal static BluetoothLeServiceData ConvertStructToLeServiceData(BluetoothLeServiceDataStruct structServiceData)
360         {
361             BluetoothLeServiceData serviceData = new BluetoothLeServiceData();
362             Log.Info(Globals.LogTag, "ServiceDataLength" + structServiceData.ServiceDataLength);
363
364             if (structServiceData.ServiceDataLength > 0)
365             {
366                 serviceData.Uuid = structServiceData.ServiceUuid;
367                 serviceData.Length = structServiceData.ServiceDataLength;
368                 serviceData.Data = new byte[serviceData.Length];
369                 Marshal.Copy(structServiceData.ServiceData, serviceData.Data, 0, serviceData.Length);
370             }
371             return serviceData;
372         }
373
374         internal static SocketData ConvertStructToSocketData(SocketDataStruct structInfo)
375         {
376             SocketData data = new SocketData();
377             data.Fd = structInfo.SocketFd;
378             data.Size = structInfo.DataSize;
379             data.RecvData = Marshal.PtrToStringAnsi(structInfo.Data);
380             return data;
381         }
382
383         internal static SocketConnection ConvertStructToSocketConnection(SocketConnectionStruct structInfo)
384         {
385             SocketConnection connectionInfo = new SocketConnection();
386             connectionInfo.Fd = structInfo.SocketFd;
387             connectionInfo.RemoteAddress = structInfo.Address;
388             connectionInfo.Uuid = structInfo.ServiceUuid;
389             return connectionInfo;
390         }
391     }
392 }
393