Add SendData(byte[] data) method and deprecated previous method (#1390)
[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     [StructLayout(LayoutKind.Sequential)]
209     internal struct BluetoothHidDeviceReceivedDataStruct
210     {
211         [MarshalAsAttribute(UnmanagedType.LPStr)]
212         internal string RemoteAddress;
213         internal BluetoothHidHeaderType headerType;
214         internal BluetoothHidParamType paramType;
215         internal int dataSize;
216         internal IntPtr data;
217     }
218
219     internal static class BluetoothUtils
220     {
221         internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
222         {
223             BluetoothDevice resultDevice = new BluetoothDevice();
224             Collection<string> uuidList = null;
225
226             if (device.ServiceCount > 0)
227             {
228                 IntPtr[] extensionList = new IntPtr[device.ServiceCount];
229                 Marshal.Copy (device.ServiceUuidList, extensionList, 0, device.ServiceCount);
230                 uuidList = new Collection<string> ();
231                 foreach (IntPtr extension in extensionList) {
232                     string uuid = Marshal.PtrToStringAnsi (extension);
233                     uuidList.Add (uuid);
234                 }
235             }
236
237             resultDevice.RemoteDeviceAddress = device.Address;
238             resultDevice.RemoteDeviceName = device.Name;
239             resultDevice.RemoteDeviceClass = new BluetoothClass();
240             resultDevice.Class.MajorType = device.Class.MajorDeviceClassType;
241             resultDevice.Class.MinorType = device.Class.MinorDeviceClassType;
242             resultDevice.Class.Mask = device.Class.MajorServiceClassMask;
243             resultDevice.RemotePaired = device.IsPaired;
244             resultDevice.RemoteConnected = device.IsConnected;
245             resultDevice.RemoteAuthorized = device.IsAuthorized;
246             resultDevice.RemoteDeviceService = uuidList;
247             resultDevice.RemoteDeviceCount = device.ServiceCount;
248             resultDevice.RemoteManufLength = device.ManufacturerDataLength;
249             resultDevice.RemoteManufData = device.ManufacturerData;
250
251             return resultDevice;
252         }
253
254         internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
255         {
256             BluetoothDevice resultDevice = new BluetoothDevice();
257             Collection<string> uuidList = null;
258
259             if (structDevice.ServiceCount > 0) {
260                 IntPtr[] extensionList = new IntPtr[structDevice.ServiceCount];
261                 Marshal.Copy (structDevice.ServiceUuidList, extensionList, 0, structDevice.ServiceCount);
262                 uuidList = new Collection<string> ();
263                 foreach (IntPtr extension in extensionList) {
264                     string uuid = Marshal.PtrToStringAnsi (extension);
265                     uuidList.Add (uuid);
266                 }
267             }
268
269             resultDevice.RemoteDeviceAddress = structDevice.Address;
270             resultDevice.RemoteDeviceName = structDevice.Name;
271
272             resultDevice.RemoteDeviceClass = new BluetoothClass();
273             resultDevice.Class.MajorType = structDevice.Class.MajorDeviceClassType;
274             resultDevice.Class.MinorType = structDevice.Class.MinorDeviceClassType;
275             resultDevice.Class.Mask = structDevice.Class.MajorServiceClassMask;
276
277             resultDevice.RemoteDeviceRssi = structDevice.Rssi;
278             resultDevice.RemoteAppearance = structDevice.AppearanceType;
279
280             if (structDevice.ServiceCount > 0) {
281                 resultDevice.RemoteDeviceService = uuidList;
282                 resultDevice.RemoteDeviceCount = structDevice.ServiceCount;
283             }
284
285             resultDevice.RemotePaired = structDevice.IsPaired;
286             resultDevice.RemoteManufData = structDevice.ManufacturerData;
287             resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
288             return resultDevice;
289         }
290
291         internal static BluetoothDeviceSdpData ConvertStructToSdpData(BluetoothDeviceSdpStruct structData)
292         {
293             BluetoothDeviceSdpData resultData = new BluetoothDeviceSdpData();
294             Collection<string> uuidList = null;
295
296             if (structData.ServiceCount > 0) {
297                 IntPtr[] extensionList = new IntPtr[structData.ServiceCount];
298                 Marshal.Copy (structData.ServiceUuid, extensionList, 0, structData.ServiceCount);
299                 uuidList = new Collection<string> ();
300                 foreach (IntPtr extension in extensionList) {
301                     string uuid = Marshal.PtrToStringAnsi (extension);
302                     uuidList.Add (uuid);
303                 }
304             }
305
306             resultData.Uuid = uuidList;
307             resultData.Address = structData.DeviceAddress;
308
309             return resultData;
310         }
311
312         internal static BluetoothDeviceConnectionData ConvertStructToConnectionData(BluetoothDeviceConnectionStruct structInfo)
313         {
314             BluetoothDeviceConnectionData resultData = new BluetoothDeviceConnectionData();
315             resultData.RemoteAddress = structInfo.Address;
316             resultData.Link = structInfo.LinkType;
317             resultData.Reason = structInfo.DisconnectReason;
318             return resultData;
319         }
320
321         internal static BluetoothLeScanData ConvertStructToLeScanData(BluetoothLeScanDataStruct structScanData)
322         {
323             BluetoothLeScanData scanData = new BluetoothLeScanData();
324
325             scanData.RemoteAddress = structScanData.RemoteAddress;
326             scanData.AddressType = structScanData.AddressType;
327             scanData.Rssi = structScanData.Rssi;
328
329             if (structScanData.AdvDataLength > 0)
330             {
331                 scanData.AdvDataLength = structScanData.AdvDataLength;
332                 scanData.AdvData = new byte[scanData.AdvDataLength];
333                 Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength);
334             }
335
336             if (structScanData.ScanDataLength > 0)
337             {
338                 scanData.ScanDataLength = structScanData.ScanDataLength;
339                 scanData.ScanData = new byte[scanData.ScanDataLength];
340                 Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength);
341             }
342             return scanData;
343         }
344
345         internal static BluetoothLeScanDataStruct ConvertLeScanDataToStruct(BluetoothLeScanData scanData)
346         {
347             BluetoothLeScanDataStruct scanDataStruct = new BluetoothLeScanDataStruct();
348
349             scanDataStruct.RemoteAddress = scanData.RemoteAddress;
350             scanDataStruct.AddressType = scanData.AddressType;
351             scanDataStruct.Rssi = scanData.Rssi;
352
353             if (scanData.AdvDataLength > 0)
354             {
355                 scanDataStruct.AdvDataLength = scanData.AdvDataLength;
356                 scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength);
357                 Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength);
358             }
359
360             if (scanData.ScanDataLength > 0)
361             {
362                 scanDataStruct.ScanDataLength = scanData.ScanDataLength;
363                 scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength);
364                 Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength);
365             }
366
367             return scanDataStruct;
368         }
369
370         internal static BluetoothLeServiceData ConvertStructToLeServiceData(BluetoothLeServiceDataStruct structServiceData)
371         {
372             BluetoothLeServiceData serviceData = new BluetoothLeServiceData();
373             Log.Info(Globals.LogTag, "ServiceDataLength" + structServiceData.ServiceDataLength);
374
375             if (structServiceData.ServiceDataLength > 0)
376             {
377                 serviceData.Uuid = structServiceData.ServiceUuid;
378                 serviceData.Length = structServiceData.ServiceDataLength;
379                 serviceData.Data = new byte[serviceData.Length];
380                 Marshal.Copy(structServiceData.ServiceData, serviceData.Data, 0, serviceData.Length);
381             }
382             return serviceData;
383         }
384
385         internal static SocketData ConvertStructToSocketData(SocketDataStruct structInfo)
386         {
387             SocketData data = new SocketData();
388             Log.Info(Globals.LogTag, "SocketDataLength" + structInfo.DataSize);
389
390             data._fd = structInfo.SocketFd;
391             if (structInfo.DataSize > 0)
392             {
393                 data._dataSize = structInfo.DataSize;
394                 data._data = new byte[data._dataSize];
395                 Marshal.Copy(structInfo.Data, data._data, 0, data._dataSize);
396                 data._recvData = Marshal.PtrToStringAnsi(structInfo.Data, structInfo.DataSize);
397             }
398             return data;
399         }
400
401         internal static SocketConnection ConvertStructToSocketConnection(SocketConnectionStruct structInfo)
402         {
403             SocketConnection connectionInfo = new SocketConnection();
404             connectionInfo.Fd = structInfo.SocketFd;
405             connectionInfo.RemoteAddress = structInfo.Address;
406             connectionInfo.Uuid = structInfo.ServiceUuid;
407             connectionInfo.ServerFd = structInfo.ServerFd;
408
409             BluetoothSocket clientSocket = new BluetoothSocket();
410             clientSocket.connectedSocket = structInfo.SocketFd;
411             clientSocket.remoteAddress = structInfo.Address;
412             clientSocket.serviceUuid = structInfo.ServiceUuid;
413             connectionInfo.ClientSocket = (IBluetoothServerSocket)clientSocket;
414
415             return connectionInfo;
416         }
417     }
418 }
419