Add Internal Notification Response Event handler (#3178)
[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         internal IntPtr Name;
119
120         internal BluetoothClassStruct Class;
121
122         internal int Rssi;
123
124         [MarshalAsAttribute(UnmanagedType.I1)]
125         internal bool IsPaired;
126
127         internal IntPtr ServiceUuidList;
128
129         internal int ServiceCount;
130
131         internal BluetoothAppearanceType AppearanceType;
132
133         internal int ManufacturerDataLength;
134
135         internal IntPtr ManufacturerData;
136     }
137
138     [NativeStruct("bt_device_sdp_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
139     [StructLayout(LayoutKind.Sequential)]
140     internal struct BluetoothDeviceSdpStruct
141     {
142         [MarshalAsAttribute(UnmanagedType.LPStr)]
143         internal string DeviceAddress;
144         internal IntPtr ServiceUuid;
145         internal int ServiceCount;
146     }
147
148     [NativeStruct("bt_device_connection_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
149     [StructLayout(LayoutKind.Sequential)]
150     internal struct BluetoothDeviceConnectionStruct
151     {
152         internal string Address;
153         internal BluetoothConnectionLinkType LinkType;
154         internal BluetoothDisconnectReason DisconnectReason;
155     }
156
157     [NativeStruct("bt_socket_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
158     [StructLayout(LayoutKind.Sequential)]
159     internal struct SocketDataStruct
160     {
161         internal int SocketFd;
162         internal int DataSize;
163         internal IntPtr Data;
164     }
165
166     [NativeStruct("bt_socket_connection_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
167     [StructLayout(LayoutKind.Sequential)]
168     internal struct SocketConnectionStruct
169     {
170         internal int SocketFd;
171         internal int ServerFd;
172         internal BluetoothSocketRole LocalRole;
173         internal string Address;
174         internal string ServiceUuid;
175     }
176
177     [NativeStruct("bt_adapter_le_device_scan_result_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
178     [StructLayout(LayoutKind.Sequential)]
179     internal struct BluetoothLeScanDataStruct
180     {
181         [MarshalAsAttribute(UnmanagedType.LPStr)]
182         internal string RemoteAddress;
183
184         internal BluetoothLeDeviceAddressType AddressType;
185
186         internal int Rssi;
187
188         internal int AdvDataLength;
189
190         internal IntPtr AdvData;
191
192         internal int ScanDataLength;
193
194         internal IntPtr ScanData;
195     }
196
197     [NativeStruct("bt_adapter_le_service_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
198     [StructLayout(LayoutKind.Sequential)]
199     internal struct BluetoothLeServiceDataStruct
200     {
201         /// <summary>
202         /// The Bluetooth LE service UUID.
203         /// </summary>
204         [MarshalAs(UnmanagedType.LPStr)]
205         internal string ServiceUuid;
206         /// <summary>
207         /// The Bluetooth LE service data.
208         /// </summary>
209         internal IntPtr ServiceData;
210
211         internal int ServiceDataLength;
212     }
213
214     [NativeStruct("bt_hid_device_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
215     [StructLayout(LayoutKind.Sequential)]
216     internal struct BluetoothHidDeviceReceivedDataStruct
217     {
218         [MarshalAsAttribute(UnmanagedType.LPStr)]
219         internal string RemoteAddress;
220         internal BluetoothHidHeaderType headerType;
221         internal BluetoothHidParamType paramType;
222         internal int dataSize;
223         internal IntPtr data;
224     }
225
226     [NativeStruct("bt_avrcp_metadata_attributes_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
227     [StructLayout(LayoutKind.Sequential)]
228     internal struct TrackInfoStruct
229     {
230         internal IntPtr Title;
231         internal IntPtr Artist;
232         internal IntPtr Album;
233         internal IntPtr Genre;
234         internal uint total_tracks;
235         internal uint number;
236         internal uint duration;
237     }
238
239     [NativeStruct("bt_gatt_client_att_mtu_info_s", Include = "bluetooth_type.h", PkgConfig = "capi-network-bluetooth")]
240     [StructLayout(LayoutKind.Sequential)]
241     internal struct AttMtuInfoStruct
242     {
243         [MarshalAsAttribute(UnmanagedType.LPStr)]
244         internal string RemoteAddress;
245         internal int Mtu;
246         internal int Status;
247     }
248
249     internal static class BluetoothUtils
250     {
251         internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
252         {
253             const int DeviceNameLengthMax = 248;
254             BluetoothDevice resultDevice = new BluetoothDevice();
255             Collection<string> uuidList = null;
256
257             if (device.ServiceCount > 0)
258             {
259                 IntPtr[] extensionList = new IntPtr[device.ServiceCount];
260                 Marshal.Copy (device.ServiceUuidList, extensionList, 0, device.ServiceCount);
261                 uuidList = new Collection<string> ();
262                 foreach (IntPtr extension in extensionList) {
263                     string uuid = Marshal.PtrToStringAnsi (extension);
264                     uuidList.Add (uuid);
265                 }
266             }
267
268             resultDevice.RemoteDeviceAddress = device.Address;
269             if (device.Name != IntPtr.Zero)
270                 resultDevice.RemoteDeviceName = Marshal.PtrToStringAnsi(device.Name, DeviceNameLengthMax);
271             resultDevice.RemoteDeviceClass = new BluetoothClass();
272             resultDevice.Class.MajorType = device.Class.MajorDeviceClassType;
273             resultDevice.Class.MinorType = device.Class.MinorDeviceClassType;
274             resultDevice.Class.Mask = device.Class.MajorServiceClassMask;
275             resultDevice.RemotePaired = device.IsPaired;
276             resultDevice.RemoteConnected = device.IsConnected;
277             resultDevice.RemoteAuthorized = device.IsAuthorized;
278             resultDevice.RemoteDeviceService = uuidList;
279             resultDevice.RemoteDeviceCount = device.ServiceCount;
280             resultDevice.RemoteManufLength = device.ManufacturerDataLength;
281
282             if (device.ManufacturerData != IntPtr.Zero)
283                 resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(device.ManufacturerData, device.ManufacturerDataLength);
284
285             return resultDevice;
286         }
287
288         internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
289         {
290             BluetoothDevice resultDevice = new BluetoothDevice();
291             Collection<string> uuidList = null;
292             const int DeviceNameLengthMax = 248;
293             const int UuidLengthMax = 50;
294
295             if (structDevice.ServiceCount > 0) {
296                 IntPtr[] extensionList = new IntPtr[structDevice.ServiceCount];
297                 Marshal.Copy (structDevice.ServiceUuidList, extensionList, 0, structDevice.ServiceCount);
298                 uuidList = new Collection<string> ();
299                 foreach (IntPtr extension in extensionList) {
300                     if (extension != IntPtr.Zero)
301                     {
302                         string uuid = Marshal.PtrToStringAnsi(extension, UuidLengthMax);
303                         uuidList.Add(uuid);
304                     }
305                 }
306             }
307
308             resultDevice.RemoteDeviceAddress = structDevice.Address;
309             if (structDevice.Name != IntPtr.Zero)
310                 resultDevice.RemoteDeviceName = Marshal.PtrToStringAnsi(structDevice.Name, DeviceNameLengthMax);
311
312             resultDevice.RemoteDeviceClass = new BluetoothClass();
313             resultDevice.Class.MajorType = structDevice.Class.MajorDeviceClassType;
314             resultDevice.Class.MinorType = structDevice.Class.MinorDeviceClassType;
315             resultDevice.Class.Mask = structDevice.Class.MajorServiceClassMask;
316
317             resultDevice.RemoteDeviceRssi = structDevice.Rssi;
318             resultDevice.RemoteAppearance = structDevice.AppearanceType;
319
320             if (structDevice.ServiceCount > 0) {
321                 resultDevice.RemoteDeviceService = uuidList;
322                 resultDevice.RemoteDeviceCount = structDevice.ServiceCount;
323             }
324
325             resultDevice.RemotePaired = structDevice.IsPaired;
326             resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
327
328             if (structDevice.ManufacturerData != IntPtr.Zero)
329                 resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(structDevice.ManufacturerData, structDevice.ManufacturerDataLength);
330
331             return resultDevice;
332         }
333
334         internal static BluetoothDeviceSdpData ConvertStructToSdpData(BluetoothDeviceSdpStruct structData)
335         {
336             BluetoothDeviceSdpData resultData = new BluetoothDeviceSdpData();
337             Collection<string> uuidList = null;
338
339             if (structData.ServiceCount > 0) {
340                 IntPtr[] extensionList = new IntPtr[structData.ServiceCount];
341                 Marshal.Copy (structData.ServiceUuid, extensionList, 0, structData.ServiceCount);
342                 uuidList = new Collection<string> ();
343                 foreach (IntPtr extension in extensionList) {
344                     string uuid = Marshal.PtrToStringAnsi (extension);
345                     uuidList.Add (uuid);
346                 }
347             }
348
349             resultData.Uuid = uuidList;
350             resultData.Address = structData.DeviceAddress;
351
352             return resultData;
353         }
354
355         internal static BluetoothDeviceConnectionData ConvertStructToConnectionData(BluetoothDeviceConnectionStruct structInfo)
356         {
357             BluetoothDeviceConnectionData resultData = new BluetoothDeviceConnectionData();
358             resultData.RemoteAddress = structInfo.Address;
359             resultData.Link = structInfo.LinkType;
360             resultData.Reason = structInfo.DisconnectReason;
361             return resultData;
362         }
363
364         internal static BluetoothLeScanData ConvertStructToLeScanData(BluetoothLeScanDataStruct structScanData)
365         {
366             BluetoothLeScanData scanData = new BluetoothLeScanData();
367
368             scanData.RemoteAddress = structScanData.RemoteAddress;
369             scanData.AddressType = structScanData.AddressType;
370             scanData.Rssi = structScanData.Rssi;
371
372             if (structScanData.AdvDataLength > 0)
373             {
374                 scanData.AdvDataLength = structScanData.AdvDataLength;
375                 scanData.AdvData = new byte[scanData.AdvDataLength];
376                 Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength);
377             }
378
379             if (structScanData.ScanDataLength > 0)
380             {
381                 scanData.ScanDataLength = structScanData.ScanDataLength;
382                 scanData.ScanData = new byte[scanData.ScanDataLength];
383                 Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength);
384             }
385             return scanData;
386         }
387
388         internal static BluetoothLeScanDataStruct ConvertLeScanDataToStruct(BluetoothLeScanData scanData)
389         {
390             BluetoothLeScanDataStruct scanDataStruct = new BluetoothLeScanDataStruct();
391
392             scanDataStruct.RemoteAddress = scanData.RemoteAddress;
393             scanDataStruct.AddressType = scanData.AddressType;
394             scanDataStruct.Rssi = scanData.Rssi;
395
396             if (scanData.AdvDataLength > 0)
397             {
398                 scanDataStruct.AdvDataLength = scanData.AdvDataLength;
399                 scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength);
400                 Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength);
401             }
402
403             if (scanData.ScanDataLength > 0)
404             {
405                 scanDataStruct.ScanDataLength = scanData.ScanDataLength;
406                 scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength);
407                 Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength);
408             }
409
410             return scanDataStruct;
411         }
412
413         internal static BluetoothLeServiceData ConvertStructToLeServiceData(BluetoothLeServiceDataStruct structServiceData)
414         {
415             BluetoothLeServiceData serviceData = new BluetoothLeServiceData();
416             Log.Info(Globals.LogTag, "ServiceDataLength" + structServiceData.ServiceDataLength);
417
418             if (structServiceData.ServiceDataLength > 0)
419             {
420                 serviceData.Uuid = structServiceData.ServiceUuid;
421                 serviceData.Length = structServiceData.ServiceDataLength;
422                 serviceData.Data = new byte[serviceData.Length];
423                 Marshal.Copy(structServiceData.ServiceData, serviceData.Data, 0, serviceData.Length);
424             }
425             return serviceData;
426         }
427
428         internal static SocketData ConvertStructToSocketData(SocketDataStruct structInfo)
429         {
430             SocketData data = new SocketData();
431             Log.Info(Globals.LogTag, "SocketDataLength" + structInfo.DataSize);
432
433             data._fd = structInfo.SocketFd;
434             if (structInfo.DataSize > 0)
435             {
436                 data._dataSize = structInfo.DataSize;
437                 data._data = new byte[data._dataSize];
438                 Marshal.Copy(structInfo.Data, data._data, 0, data._dataSize);
439                 data._recvData = Marshal.PtrToStringAnsi(structInfo.Data, structInfo.DataSize);
440             }
441             return data;
442         }
443
444         internal static SocketConnection ConvertStructToSocketConnection(SocketConnectionStruct structInfo)
445         {
446             SocketConnection connectionInfo = new SocketConnection();
447             connectionInfo.Fd = structInfo.SocketFd;
448             connectionInfo.RemoteAddress = structInfo.Address;
449             connectionInfo.Uuid = structInfo.ServiceUuid;
450             connectionInfo.ServerFd = structInfo.ServerFd;
451
452             BluetoothSocket clientSocket = new BluetoothSocket();
453             clientSocket.connectedSocket = structInfo.SocketFd;
454             clientSocket.remoteAddress = structInfo.Address;
455             clientSocket.serviceUuid = structInfo.ServiceUuid;
456             connectionInfo.ClientSocket = (IBluetoothServerSocket)clientSocket;
457
458             return connectionInfo;
459         }
460     }
461 }
462