adc2e5874abc70c02774cc440e170232a623124c
[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 using Tizen.Internals;
26
27 namespace Tizen.Network.Bluetooth
28 {
29     /// <summary>
30     /// The structure of the device class type and service.
31     /// </summary>
32     [NativeStruct("bt_class_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
33     [StructLayout(LayoutKind.Sequential)]
34     internal struct BluetoothClassStruct
35     {
36         /// <summary>
37         /// The type of the major device class.
38         /// </summary>
39         internal BluetoothMajorDeviceClassType MajorDeviceClassType;
40         /// <summary>
41         /// The type of the minor device class.
42         /// </summary>
43         internal BluetoothMinorDeviceClassType MinorDeviceClassType;
44         /// <summary>
45         /// The major service class mask.
46         /// </summary>
47         internal int MajorServiceClassMask;
48     }
49
50     /// <summary>
51     /// This structure contains the information of the Bluetooth device.
52     /// </summary>
53     [NativeStruct("bt_device_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
54     [StructLayout(LayoutKind.Sequential)]
55     internal struct BluetoothDeviceStruct
56     {
57         /// <summary>
58         /// The address of the device.
59         /// </summary>
60         [MarshalAsAttribute(UnmanagedType.LPStr)]
61         internal string Address;
62
63         /// <summary>
64         /// The name of the device.
65         /// </summary>
66         [MarshalAsAttribute(UnmanagedType.LPStr)]
67         internal string Name;
68
69         /// <summary>
70         /// The class of the device.
71         /// </summary>
72         internal BluetoothClassStruct Class;
73
74         /// <summary>
75         /// The service UUID list of the device.
76         /// </summary>
77         internal IntPtr ServiceUuidList;
78
79         /// <summary>
80         /// The service count of the device.
81         /// </summary>
82         internal int ServiceCount;
83
84         /// <summary>
85         /// The paired state of the device.
86         /// </summary>
87         [MarshalAsAttribute(UnmanagedType.I1)]
88         internal bool IsPaired;
89
90         /// <summary>
91         /// The connection state of the device.
92         /// </summary>
93         [MarshalAsAttribute(UnmanagedType.I1)]
94         internal bool IsConnected;
95
96         /// <summary>
97         /// The authorization state of the device.
98         /// </summary>
99         [MarshalAsAttribute(UnmanagedType.I1)]
100         internal bool IsAuthorized;
101
102         /// <summary>
103         /// The length of the manufacturer the data.
104         /// </summary>
105         internal int ManufacturerDataLength;
106
107         /// <summary>
108         /// The manufacturer data.
109         /// </summary>
110         internal IntPtr ManufacturerData;
111     }
112     [NativeStruct("bt_adapter_device_discovery_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
113     [StructLayout(LayoutKind.Sequential)]
114     internal struct BluetoothDiscoveredDeviceStruct
115     {
116         [MarshalAsAttribute(UnmanagedType.LPStr)]
117         internal string Address;
118
119         [MarshalAsAttribute(UnmanagedType.LPStr)]
120         internal string Name;
121
122         internal BluetoothClassStruct Class;
123
124         internal int Rssi;
125
126         [MarshalAsAttribute(UnmanagedType.I1)]
127         internal bool IsPaired;
128
129         internal IntPtr ServiceUuidList;
130
131         internal int ServiceCount;
132
133         internal BluetoothAppearanceType AppearanceType;
134
135         internal int ManufacturerDataLength;
136
137         internal IntPtr ManufacturerData;
138     }
139
140     [NativeStruct("bt_device_sdp_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
141     [StructLayout(LayoutKind.Sequential)]
142     internal struct BluetoothDeviceSdpStruct
143     {
144         [MarshalAsAttribute(UnmanagedType.LPStr)]
145         internal string DeviceAddress;
146         internal IntPtr ServiceUuid;
147         internal int ServiceCount;
148     }
149
150     [NativeStruct("bt_device_connection_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
151     [StructLayout(LayoutKind.Sequential)]
152     internal struct BluetoothDeviceConnectionStruct
153     {
154         internal string Address;
155         internal BluetoothConnectionLinkType LinkType;
156         internal BluetoothDisconnectReason DisconnectReason;
157     }
158
159     [NativeStruct("bt_socket_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
160     [StructLayout(LayoutKind.Sequential)]
161     internal struct SocketDataStruct
162     {
163         internal int SocketFd;
164         internal int DataSize;
165         internal IntPtr Data;
166     }
167
168     [NativeStruct("bt_socket_connection_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
169     [StructLayout(LayoutKind.Sequential)]
170     internal struct SocketConnectionStruct
171     {
172         internal int SocketFd;
173         internal int ServerFd;
174         internal BluetoothSocketRole LocalRole;
175         internal string Address;
176         internal string ServiceUuid;
177     }
178
179     [NativeStruct("bt_adapter_le_device_scan_result_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
180     [StructLayout(LayoutKind.Sequential)]
181     internal struct BluetoothLeScanDataStruct
182     {
183         [MarshalAsAttribute(UnmanagedType.LPStr)]
184         internal string RemoteAddress;
185
186         internal BluetoothLeDeviceAddressType AddressType;
187
188         internal int Rssi;
189
190         internal int AdvDataLength;
191
192         internal IntPtr AdvData;
193
194         internal int ScanDataLength;
195
196         internal IntPtr ScanData;
197     }
198
199     [NativeStruct("bt_adapter_le_service_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
200     [StructLayout(LayoutKind.Sequential)]
201     internal struct BluetoothLeServiceDataStruct
202     {
203         /// <summary>
204         /// The Bluetooth LE service UUID.
205         /// </summary>
206         [MarshalAs(UnmanagedType.LPStr)]
207         internal string ServiceUuid;
208         /// <summary>
209         /// The Bluetooth LE service data.
210         /// </summary>
211         internal IntPtr ServiceData;
212
213         internal int ServiceDataLength;
214     }
215
216     [NativeStruct("bt_hid_device_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
217     [StructLayout(LayoutKind.Sequential)]
218     internal struct BluetoothHidDeviceReceivedDataStruct
219     {
220         [MarshalAsAttribute(UnmanagedType.LPStr)]
221         internal string RemoteAddress;
222         internal BluetoothHidHeaderType headerType;
223         internal BluetoothHidParamType paramType;
224         internal int dataSize;
225         internal IntPtr data;
226     }
227
228     [NativeStruct("bt_avrcp_metadata_attributes_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
229     [StructLayout(LayoutKind.Sequential)]
230     internal struct TrackInfoStruct
231     {
232         internal IntPtr Title;
233         internal IntPtr Artist;
234         internal IntPtr Album;
235         internal IntPtr Genre;
236         internal uint total_tracks;
237         internal uint number;
238         internal uint duration;
239     }
240     internal static class BluetoothUtils
241     {
242         internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
243         {
244             BluetoothDevice resultDevice = new BluetoothDevice();
245             Collection<string> uuidList = null;
246
247             if (device.ServiceCount > 0)
248             {
249                 IntPtr[] extensionList = new IntPtr[device.ServiceCount];
250                 Marshal.Copy (device.ServiceUuidList, extensionList, 0, device.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 = device.Address;
259             resultDevice.RemoteDeviceName = device.Name;
260             resultDevice.RemoteDeviceClass = new BluetoothClass();
261             resultDevice.Class.MajorType = device.Class.MajorDeviceClassType;
262             resultDevice.Class.MinorType = device.Class.MinorDeviceClassType;
263             resultDevice.Class.Mask = device.Class.MajorServiceClassMask;
264             resultDevice.RemotePaired = device.IsPaired;
265             resultDevice.RemoteConnected = device.IsConnected;
266             resultDevice.RemoteAuthorized = device.IsAuthorized;
267             resultDevice.RemoteDeviceService = uuidList;
268             resultDevice.RemoteDeviceCount = device.ServiceCount;
269             resultDevice.RemoteManufLength = device.ManufacturerDataLength;
270
271             if (device.ManufacturerData != IntPtr.Zero)
272                 resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(device.ManufacturerData, device.ManufacturerDataLength);
273
274             return resultDevice;
275         }
276
277         internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
278         {
279             BluetoothDevice resultDevice = new BluetoothDevice();
280             Collection<string> uuidList = null;
281
282             if (structDevice.ServiceCount > 0) {
283                 IntPtr[] extensionList = new IntPtr[structDevice.ServiceCount];
284                 Marshal.Copy (structDevice.ServiceUuidList, extensionList, 0, structDevice.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             resultDevice.RemoteDeviceAddress = structDevice.Address;
293             resultDevice.RemoteDeviceName = structDevice.Name;
294
295             resultDevice.RemoteDeviceClass = new BluetoothClass();
296             resultDevice.Class.MajorType = structDevice.Class.MajorDeviceClassType;
297             resultDevice.Class.MinorType = structDevice.Class.MinorDeviceClassType;
298             resultDevice.Class.Mask = structDevice.Class.MajorServiceClassMask;
299
300             resultDevice.RemoteDeviceRssi = structDevice.Rssi;
301             resultDevice.RemoteAppearance = structDevice.AppearanceType;
302
303             if (structDevice.ServiceCount > 0) {
304                 resultDevice.RemoteDeviceService = uuidList;
305                 resultDevice.RemoteDeviceCount = structDevice.ServiceCount;
306             }
307
308             resultDevice.RemotePaired = structDevice.IsPaired;
309             resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
310
311             if (structDevice.ManufacturerData != IntPtr.Zero)
312                 resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(structDevice.ManufacturerData, structDevice.ManufacturerDataLength);
313
314             return resultDevice;
315         }
316
317         internal static BluetoothDeviceSdpData ConvertStructToSdpData(BluetoothDeviceSdpStruct structData)
318         {
319             BluetoothDeviceSdpData resultData = new BluetoothDeviceSdpData();
320             Collection<string> uuidList = null;
321
322             if (structData.ServiceCount > 0) {
323                 IntPtr[] extensionList = new IntPtr[structData.ServiceCount];
324                 Marshal.Copy (structData.ServiceUuid, extensionList, 0, structData.ServiceCount);
325                 uuidList = new Collection<string> ();
326                 foreach (IntPtr extension in extensionList) {
327                     string uuid = Marshal.PtrToStringAnsi (extension);
328                     uuidList.Add (uuid);
329                 }
330             }
331
332             resultData.Uuid = uuidList;
333             resultData.Address = structData.DeviceAddress;
334
335             return resultData;
336         }
337
338         internal static BluetoothDeviceConnectionData ConvertStructToConnectionData(BluetoothDeviceConnectionStruct structInfo)
339         {
340             BluetoothDeviceConnectionData resultData = new BluetoothDeviceConnectionData();
341             resultData.RemoteAddress = structInfo.Address;
342             resultData.Link = structInfo.LinkType;
343             resultData.Reason = structInfo.DisconnectReason;
344             return resultData;
345         }
346
347         internal static BluetoothLeScanData ConvertStructToLeScanData(BluetoothLeScanDataStruct structScanData)
348         {
349             BluetoothLeScanData scanData = new BluetoothLeScanData();
350
351             scanData.RemoteAddress = structScanData.RemoteAddress;
352             scanData.AddressType = structScanData.AddressType;
353             scanData.Rssi = structScanData.Rssi;
354
355             if (structScanData.AdvDataLength > 0)
356             {
357                 scanData.AdvDataLength = structScanData.AdvDataLength;
358                 scanData.AdvData = new byte[scanData.AdvDataLength];
359                 Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength);
360             }
361
362             if (structScanData.ScanDataLength > 0)
363             {
364                 scanData.ScanDataLength = structScanData.ScanDataLength;
365                 scanData.ScanData = new byte[scanData.ScanDataLength];
366                 Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength);
367             }
368             return scanData;
369         }
370
371         internal static BluetoothLeScanDataStruct ConvertLeScanDataToStruct(BluetoothLeScanData scanData)
372         {
373             BluetoothLeScanDataStruct scanDataStruct = new BluetoothLeScanDataStruct();
374
375             scanDataStruct.RemoteAddress = scanData.RemoteAddress;
376             scanDataStruct.AddressType = scanData.AddressType;
377             scanDataStruct.Rssi = scanData.Rssi;
378
379             if (scanData.AdvDataLength > 0)
380             {
381                 scanDataStruct.AdvDataLength = scanData.AdvDataLength;
382                 scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength);
383                 Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength);
384             }
385
386             if (scanData.ScanDataLength > 0)
387             {
388                 scanDataStruct.ScanDataLength = scanData.ScanDataLength;
389                 scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength);
390                 Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength);
391             }
392
393             return scanDataStruct;
394         }
395
396         internal static BluetoothLeServiceData ConvertStructToLeServiceData(BluetoothLeServiceDataStruct structServiceData)
397         {
398             BluetoothLeServiceData serviceData = new BluetoothLeServiceData();
399             Log.Info(Globals.LogTag, "ServiceDataLength" + structServiceData.ServiceDataLength);
400
401             if (structServiceData.ServiceDataLength > 0)
402             {
403                 serviceData.Uuid = structServiceData.ServiceUuid;
404                 serviceData.Length = structServiceData.ServiceDataLength;
405                 serviceData.Data = new byte[serviceData.Length];
406                 Marshal.Copy(structServiceData.ServiceData, serviceData.Data, 0, serviceData.Length);
407             }
408             return serviceData;
409         }
410
411         internal static SocketData ConvertStructToSocketData(SocketDataStruct structInfo)
412         {
413             SocketData data = new SocketData();
414             Log.Info(Globals.LogTag, "SocketDataLength" + structInfo.DataSize);
415
416             data._fd = structInfo.SocketFd;
417             if (structInfo.DataSize > 0)
418             {
419                 data._dataSize = structInfo.DataSize;
420                 data._data = new byte[data._dataSize];
421                 Marshal.Copy(structInfo.Data, data._data, 0, data._dataSize);
422                 data._recvData = Marshal.PtrToStringAnsi(structInfo.Data, structInfo.DataSize);
423             }
424             return data;
425         }
426
427         internal static SocketConnection ConvertStructToSocketConnection(SocketConnectionStruct structInfo)
428         {
429             SocketConnection connectionInfo = new SocketConnection();
430             connectionInfo.Fd = structInfo.SocketFd;
431             connectionInfo.RemoteAddress = structInfo.Address;
432             connectionInfo.Uuid = structInfo.ServiceUuid;
433             connectionInfo.ServerFd = structInfo.ServerFd;
434
435             BluetoothSocket clientSocket = new BluetoothSocket();
436             clientSocket.connectedSocket = structInfo.SocketFd;
437             clientSocket.remoteAddress = structInfo.Address;
438             clientSocket.serviceUuid = structInfo.ServiceUuid;
439             connectionInfo.ClientSocket = (IBluetoothServerSocket)clientSocket;
440
441             return connectionInfo;
442         }
443     }
444 }
445