Release 8.0.0.15764
[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         internal IntPtr Name;
67
68         /// <summary>
69         /// The class of the device.
70         /// </summary>
71         internal BluetoothClassStruct Class;
72
73         /// <summary>
74         /// The service UUID list of the device.
75         /// </summary>
76         internal IntPtr ServiceUuidList;
77
78         /// <summary>
79         /// The service count of the device.
80         /// </summary>
81         internal int ServiceCount;
82
83         /// <summary>
84         /// The paired state of the device.
85         /// </summary>
86         [MarshalAsAttribute(UnmanagedType.I1)]
87         internal bool IsPaired;
88
89         /// <summary>
90         /// The connection state of the device.
91         /// </summary>
92         [MarshalAsAttribute(UnmanagedType.I1)]
93         internal bool IsConnected;
94
95         /// <summary>
96         /// The authorization state of the device.
97         /// </summary>
98         [MarshalAsAttribute(UnmanagedType.I1)]
99         internal bool IsAuthorized;
100
101         /// <summary>
102         /// The length of the manufacturer the data.
103         /// </summary>
104         internal int ManufacturerDataLength;
105
106         /// <summary>
107         /// The manufacturer data.
108         /// </summary>
109         internal IntPtr ManufacturerData;
110     }
111     [NativeStruct("bt_adapter_device_discovery_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
112     [StructLayout(LayoutKind.Sequential)]
113     internal struct BluetoothDiscoveredDeviceStruct
114     {
115         [MarshalAsAttribute(UnmanagedType.LPStr)]
116         internal string Address;
117
118         [MarshalAsAttribute(UnmanagedType.LPStr)]
119         internal string Name;
120
121         internal BluetoothClassStruct Class;
122
123         internal int Rssi;
124
125         [MarshalAsAttribute(UnmanagedType.I1)]
126         internal bool IsPaired;
127
128         internal IntPtr ServiceUuidList;
129
130         internal int ServiceCount;
131
132         internal BluetoothAppearanceType AppearanceType;
133
134         internal int ManufacturerDataLength;
135
136         internal IntPtr ManufacturerData;
137     }
138
139     [NativeStruct("bt_device_sdp_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
140     [StructLayout(LayoutKind.Sequential)]
141     internal struct BluetoothDeviceSdpStruct
142     {
143         [MarshalAsAttribute(UnmanagedType.LPStr)]
144         internal string DeviceAddress;
145         internal IntPtr ServiceUuid;
146         internal int ServiceCount;
147     }
148
149     [NativeStruct("bt_device_connection_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
150     [StructLayout(LayoutKind.Sequential)]
151     internal struct BluetoothDeviceConnectionStruct
152     {
153         internal string Address;
154         internal BluetoothConnectionLinkType LinkType;
155         internal BluetoothDisconnectReason DisconnectReason;
156     }
157
158     [NativeStruct("bt_socket_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
159     [StructLayout(LayoutKind.Sequential)]
160     internal struct SocketDataStruct
161     {
162         internal int SocketFd;
163         internal int DataSize;
164         internal IntPtr Data;
165     }
166
167     [NativeStruct("bt_socket_connection_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
168     [StructLayout(LayoutKind.Sequential)]
169     internal struct SocketConnectionStruct
170     {
171         internal int SocketFd;
172         internal int ServerFd;
173         internal BluetoothSocketRole LocalRole;
174         internal string Address;
175         internal string ServiceUuid;
176     }
177
178     [NativeStruct("bt_adapter_le_device_scan_result_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
179     [StructLayout(LayoutKind.Sequential)]
180     internal struct BluetoothLeScanDataStruct
181     {
182         [MarshalAsAttribute(UnmanagedType.LPStr)]
183         internal string RemoteAddress;
184
185         internal BluetoothLeDeviceAddressType AddressType;
186
187         internal int Rssi;
188
189         internal int AdvDataLength;
190
191         internal IntPtr AdvData;
192
193         internal int ScanDataLength;
194
195         internal IntPtr ScanData;
196     }
197
198     [NativeStruct("bt_adapter_le_service_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
199     [StructLayout(LayoutKind.Sequential)]
200     internal struct BluetoothLeServiceDataStruct
201     {
202         /// <summary>
203         /// The Bluetooth LE service UUID.
204         /// </summary>
205         [MarshalAs(UnmanagedType.LPStr)]
206         internal string ServiceUuid;
207         /// <summary>
208         /// The Bluetooth LE service data.
209         /// </summary>
210         internal IntPtr ServiceData;
211
212         internal int ServiceDataLength;
213     }
214
215     [NativeStruct("bt_hid_device_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
216     [StructLayout(LayoutKind.Sequential)]
217     internal struct BluetoothHidDeviceReceivedDataStruct
218     {
219         [MarshalAsAttribute(UnmanagedType.LPStr)]
220         internal string RemoteAddress;
221         internal BluetoothHidHeaderType headerType;
222         internal BluetoothHidParamType paramType;
223         internal int dataSize;
224         internal IntPtr data;
225     }
226
227     [NativeStruct("bt_avrcp_metadata_attributes_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
228     [StructLayout(LayoutKind.Sequential)]
229     internal struct TrackInfoStruct
230     {
231         internal IntPtr Title;
232         internal IntPtr Artist;
233         internal IntPtr Album;
234         internal IntPtr Genre;
235         internal uint total_tracks;
236         internal uint number;
237         internal uint duration;
238     }
239     internal static class BluetoothUtils
240     {
241         internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
242         {
243             const int DeviceNameLengthMax = 248;
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 = Marshal.PtrToStringAnsi(device.Name, DeviceNameLengthMax);
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