2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Reflection;
20 using System.Collections.Generic;
21 using System.Collections.ObjectModel;
22 using System.Collections.Specialized;
23 using System.Runtime.InteropServices;
25 using Tizen.Internals;
27 namespace Tizen.Network.Bluetooth
30 /// The structure of the device class type and service.
32 [NativeStruct("bt_class_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
33 [StructLayout(LayoutKind.Sequential)]
34 internal struct BluetoothClassStruct
37 /// The type of the major device class.
39 internal BluetoothMajorDeviceClassType MajorDeviceClassType;
41 /// The type of the minor device class.
43 internal BluetoothMinorDeviceClassType MinorDeviceClassType;
45 /// The major service class mask.
47 internal int MajorServiceClassMask;
51 /// This structure contains the information of the Bluetooth device.
53 [NativeStruct("bt_device_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
54 [StructLayout(LayoutKind.Sequential)]
55 internal struct BluetoothDeviceStruct
58 /// The address of the device.
60 [MarshalAsAttribute(UnmanagedType.LPStr)]
61 internal string Address;
64 /// The name of the device.
66 [MarshalAsAttribute(UnmanagedType.LPStr)]
70 /// The class of the device.
72 internal BluetoothClassStruct Class;
75 /// The service UUID list of the device.
77 internal IntPtr ServiceUuidList;
80 /// The service count of the device.
82 internal int ServiceCount;
85 /// The paired state of the device.
87 [MarshalAsAttribute(UnmanagedType.I1)]
88 internal bool IsPaired;
91 /// The connection state of the device.
93 [MarshalAsAttribute(UnmanagedType.I1)]
94 internal bool IsConnected;
97 /// The authorization state of the device.
99 [MarshalAsAttribute(UnmanagedType.I1)]
100 internal bool IsAuthorized;
103 /// The length of the manufacturer the data.
105 internal int ManufacturerDataLength;
108 /// The manufacturer data.
110 internal IntPtr ManufacturerData;
112 [NativeStruct("bt_adapter_device_discovery_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
113 [StructLayout(LayoutKind.Sequential)]
114 internal struct BluetoothDiscoveredDeviceStruct
116 [MarshalAsAttribute(UnmanagedType.LPStr)]
117 internal string Address;
119 [MarshalAsAttribute(UnmanagedType.LPStr)]
120 internal string Name;
122 internal BluetoothClassStruct Class;
126 [MarshalAsAttribute(UnmanagedType.I1)]
127 internal bool IsPaired;
129 internal IntPtr ServiceUuidList;
131 internal int ServiceCount;
133 internal BluetoothAppearanceType AppearanceType;
135 internal int ManufacturerDataLength;
137 internal IntPtr ManufacturerData;
140 [NativeStruct("bt_device_sdp_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
141 [StructLayout(LayoutKind.Sequential)]
142 internal struct BluetoothDeviceSdpStruct
144 [MarshalAsAttribute(UnmanagedType.LPStr)]
145 internal string DeviceAddress;
146 internal IntPtr ServiceUuid;
147 internal int ServiceCount;
150 [NativeStruct("bt_device_connection_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
151 [StructLayout(LayoutKind.Sequential)]
152 internal struct BluetoothDeviceConnectionStruct
154 internal string Address;
155 internal BluetoothConnectionLinkType LinkType;
156 internal BluetoothDisconnectReason DisconnectReason;
159 [NativeStruct("bt_socket_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
160 [StructLayout(LayoutKind.Sequential)]
161 internal struct SocketDataStruct
163 internal int SocketFd;
164 internal int DataSize;
165 internal IntPtr Data;
168 [NativeStruct("bt_socket_connection_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
169 [StructLayout(LayoutKind.Sequential)]
170 internal struct SocketConnectionStruct
172 internal int SocketFd;
173 internal int ServerFd;
174 internal BluetoothSocketRole LocalRole;
175 internal string Address;
176 internal string ServiceUuid;
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
183 [MarshalAsAttribute(UnmanagedType.LPStr)]
184 internal string RemoteAddress;
186 internal BluetoothLeDeviceAddressType AddressType;
190 internal int AdvDataLength;
192 internal IntPtr AdvData;
194 internal int ScanDataLength;
196 internal IntPtr ScanData;
199 [NativeStruct("bt_adapter_le_service_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
200 [StructLayout(LayoutKind.Sequential)]
201 internal struct BluetoothLeServiceDataStruct
204 /// The Bluetooth LE service UUID.
206 [MarshalAs(UnmanagedType.LPStr)]
207 internal string ServiceUuid;
209 /// The Bluetooth LE service data.
211 internal IntPtr ServiceData;
213 internal int ServiceDataLength;
216 [NativeStruct("bt_hid_device_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
217 [StructLayout(LayoutKind.Sequential)]
218 internal struct BluetoothHidDeviceReceivedDataStruct
220 [MarshalAsAttribute(UnmanagedType.LPStr)]
221 internal string RemoteAddress;
222 internal BluetoothHidHeaderType headerType;
223 internal BluetoothHidParamType paramType;
224 internal int dataSize;
225 internal IntPtr data;
228 [NativeStruct("bt_avrcp_metadata_attributes_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
229 [StructLayout(LayoutKind.Sequential)]
230 internal struct TrackInfoStruct
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;
240 internal static class BluetoothUtils
242 internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
244 BluetoothDevice resultDevice = new BluetoothDevice();
245 Collection<string> uuidList = null;
247 if (device.ServiceCount > 0)
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);
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;
271 if (device.ManufacturerData != IntPtr.Zero)
272 resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(device.ManufacturerData, device.ManufacturerDataLength);
277 internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
279 BluetoothDevice resultDevice = new BluetoothDevice();
280 Collection<string> uuidList = null;
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);
292 resultDevice.RemoteDeviceAddress = structDevice.Address;
293 resultDevice.RemoteDeviceName = structDevice.Name;
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;
300 resultDevice.RemoteDeviceRssi = structDevice.Rssi;
301 resultDevice.RemoteAppearance = structDevice.AppearanceType;
303 if (structDevice.ServiceCount > 0) {
304 resultDevice.RemoteDeviceService = uuidList;
305 resultDevice.RemoteDeviceCount = structDevice.ServiceCount;
308 resultDevice.RemotePaired = structDevice.IsPaired;
309 resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
311 if (structDevice.ManufacturerData != IntPtr.Zero)
312 resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(structDevice.ManufacturerData, structDevice.ManufacturerDataLength);
317 internal static BluetoothDeviceSdpData ConvertStructToSdpData(BluetoothDeviceSdpStruct structData)
319 BluetoothDeviceSdpData resultData = new BluetoothDeviceSdpData();
320 Collection<string> uuidList = null;
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);
332 resultData.Uuid = uuidList;
333 resultData.Address = structData.DeviceAddress;
338 internal static BluetoothDeviceConnectionData ConvertStructToConnectionData(BluetoothDeviceConnectionStruct structInfo)
340 BluetoothDeviceConnectionData resultData = new BluetoothDeviceConnectionData();
341 resultData.RemoteAddress = structInfo.Address;
342 resultData.Link = structInfo.LinkType;
343 resultData.Reason = structInfo.DisconnectReason;
347 internal static BluetoothLeScanData ConvertStructToLeScanData(BluetoothLeScanDataStruct structScanData)
349 BluetoothLeScanData scanData = new BluetoothLeScanData();
351 scanData.RemoteAddress = structScanData.RemoteAddress;
352 scanData.AddressType = structScanData.AddressType;
353 scanData.Rssi = structScanData.Rssi;
355 if (structScanData.AdvDataLength > 0)
357 scanData.AdvDataLength = structScanData.AdvDataLength;
358 scanData.AdvData = new byte[scanData.AdvDataLength];
359 Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength);
362 if (structScanData.ScanDataLength > 0)
364 scanData.ScanDataLength = structScanData.ScanDataLength;
365 scanData.ScanData = new byte[scanData.ScanDataLength];
366 Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength);
371 internal static BluetoothLeScanDataStruct ConvertLeScanDataToStruct(BluetoothLeScanData scanData)
373 BluetoothLeScanDataStruct scanDataStruct = new BluetoothLeScanDataStruct();
375 scanDataStruct.RemoteAddress = scanData.RemoteAddress;
376 scanDataStruct.AddressType = scanData.AddressType;
377 scanDataStruct.Rssi = scanData.Rssi;
379 if (scanData.AdvDataLength > 0)
381 scanDataStruct.AdvDataLength = scanData.AdvDataLength;
382 scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength);
383 Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength);
386 if (scanData.ScanDataLength > 0)
388 scanDataStruct.ScanDataLength = scanData.ScanDataLength;
389 scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength);
390 Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength);
393 return scanDataStruct;
396 internal static BluetoothLeServiceData ConvertStructToLeServiceData(BluetoothLeServiceDataStruct structServiceData)
398 BluetoothLeServiceData serviceData = new BluetoothLeServiceData();
399 Log.Info(Globals.LogTag, "ServiceDataLength" + structServiceData.ServiceDataLength);
401 if (structServiceData.ServiceDataLength > 0)
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);
411 internal static SocketData ConvertStructToSocketData(SocketDataStruct structInfo)
413 SocketData data = new SocketData();
414 Log.Info(Globals.LogTag, "SocketDataLength" + structInfo.DataSize);
416 data._fd = structInfo.SocketFd;
417 if (structInfo.DataSize > 0)
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);
427 internal static SocketConnection ConvertStructToSocketConnection(SocketConnectionStruct structInfo)
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;
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;
441 return connectionInfo;