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