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.
69 /// The class of the device.
71 internal BluetoothClassStruct Class;
74 /// The service UUID list of the device.
76 internal IntPtr ServiceUuidList;
79 /// The service count of the device.
81 internal int ServiceCount;
84 /// The paired state of the device.
86 [MarshalAsAttribute(UnmanagedType.I1)]
87 internal bool IsPaired;
90 /// The connection state of the device.
92 [MarshalAsAttribute(UnmanagedType.I1)]
93 internal bool IsConnected;
96 /// The authorization state of the device.
98 [MarshalAsAttribute(UnmanagedType.I1)]
99 internal bool IsAuthorized;
102 /// The length of the manufacturer the data.
104 internal int ManufacturerDataLength;
107 /// The manufacturer data.
109 internal IntPtr ManufacturerData;
111 [NativeStruct("bt_adapter_device_discovery_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
112 [StructLayout(LayoutKind.Sequential)]
113 internal struct BluetoothDiscoveredDeviceStruct
115 [MarshalAsAttribute(UnmanagedType.LPStr)]
116 internal string Address;
118 [MarshalAsAttribute(UnmanagedType.LPStr)]
119 internal string Name;
121 internal BluetoothClassStruct Class;
125 [MarshalAsAttribute(UnmanagedType.I1)]
126 internal bool IsPaired;
128 internal IntPtr ServiceUuidList;
130 internal int ServiceCount;
132 internal BluetoothAppearanceType AppearanceType;
134 internal int ManufacturerDataLength;
136 internal IntPtr ManufacturerData;
139 [NativeStruct("bt_device_sdp_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
140 [StructLayout(LayoutKind.Sequential)]
141 internal struct BluetoothDeviceSdpStruct
143 [MarshalAsAttribute(UnmanagedType.LPStr)]
144 internal string DeviceAddress;
145 internal IntPtr ServiceUuid;
146 internal int ServiceCount;
149 [NativeStruct("bt_device_connection_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
150 [StructLayout(LayoutKind.Sequential)]
151 internal struct BluetoothDeviceConnectionStruct
153 internal string Address;
154 internal BluetoothConnectionLinkType LinkType;
155 internal BluetoothDisconnectReason DisconnectReason;
158 [NativeStruct("bt_socket_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
159 [StructLayout(LayoutKind.Sequential)]
160 internal struct SocketDataStruct
162 internal int SocketFd;
163 internal int DataSize;
164 internal IntPtr Data;
167 [NativeStruct("bt_socket_connection_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
168 [StructLayout(LayoutKind.Sequential)]
169 internal struct SocketConnectionStruct
171 internal int SocketFd;
172 internal int ServerFd;
173 internal BluetoothSocketRole LocalRole;
174 internal string Address;
175 internal string ServiceUuid;
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
182 [MarshalAsAttribute(UnmanagedType.LPStr)]
183 internal string RemoteAddress;
185 internal BluetoothLeDeviceAddressType AddressType;
189 internal int AdvDataLength;
191 internal IntPtr AdvData;
193 internal int ScanDataLength;
195 internal IntPtr ScanData;
198 [NativeStruct("bt_adapter_le_service_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
199 [StructLayout(LayoutKind.Sequential)]
200 internal struct BluetoothLeServiceDataStruct
203 /// The Bluetooth LE service UUID.
205 [MarshalAs(UnmanagedType.LPStr)]
206 internal string ServiceUuid;
208 /// The Bluetooth LE service data.
210 internal IntPtr ServiceData;
212 internal int ServiceDataLength;
215 [NativeStruct("bt_hid_device_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
216 [StructLayout(LayoutKind.Sequential)]
217 internal struct BluetoothHidDeviceReceivedDataStruct
219 [MarshalAsAttribute(UnmanagedType.LPStr)]
220 internal string RemoteAddress;
221 internal BluetoothHidHeaderType headerType;
222 internal BluetoothHidParamType paramType;
223 internal int dataSize;
224 internal IntPtr data;
227 [NativeStruct("bt_avrcp_metadata_attributes_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
228 [StructLayout(LayoutKind.Sequential)]
229 internal struct TrackInfoStruct
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;
239 internal static class BluetoothUtils
241 internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
243 const int DeviceNameLengthMax = 248;
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 = 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;
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;