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 internal IntPtr Name;
120 internal BluetoothClassStruct Class;
124 [MarshalAsAttribute(UnmanagedType.I1)]
125 internal bool IsPaired;
127 internal IntPtr ServiceUuidList;
129 internal int ServiceCount;
131 internal BluetoothAppearanceType AppearanceType;
133 internal int ManufacturerDataLength;
135 internal IntPtr ManufacturerData;
138 [NativeStruct("bt_device_sdp_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
139 [StructLayout(LayoutKind.Sequential)]
140 internal struct BluetoothDeviceSdpStruct
142 [MarshalAsAttribute(UnmanagedType.LPStr)]
143 internal string DeviceAddress;
144 internal IntPtr ServiceUuid;
145 internal int ServiceCount;
148 [NativeStruct("bt_device_connection_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
149 [StructLayout(LayoutKind.Sequential)]
150 internal struct BluetoothDeviceConnectionStruct
152 internal string Address;
153 internal BluetoothConnectionLinkType LinkType;
154 internal BluetoothDisconnectReason DisconnectReason;
157 [NativeStruct("bt_socket_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
158 [StructLayout(LayoutKind.Sequential)]
159 internal struct SocketDataStruct
161 internal int SocketFd;
162 internal int DataSize;
163 internal IntPtr Data;
166 [NativeStruct("bt_socket_connection_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
167 [StructLayout(LayoutKind.Sequential)]
168 internal struct SocketConnectionStruct
170 internal int SocketFd;
171 internal int ServerFd;
172 internal BluetoothSocketRole LocalRole;
173 internal string Address;
174 internal string ServiceUuid;
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
181 [MarshalAsAttribute(UnmanagedType.LPStr)]
182 internal string RemoteAddress;
184 internal BluetoothLeDeviceAddressType AddressType;
188 internal int AdvDataLength;
190 internal IntPtr AdvData;
192 internal int ScanDataLength;
194 internal IntPtr ScanData;
197 [NativeStruct("bt_adapter_le_service_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
198 [StructLayout(LayoutKind.Sequential)]
199 internal struct BluetoothLeServiceDataStruct
202 /// The Bluetooth LE service UUID.
204 [MarshalAs(UnmanagedType.LPStr)]
205 internal string ServiceUuid;
207 /// The Bluetooth LE service data.
209 internal IntPtr ServiceData;
211 internal int ServiceDataLength;
214 [NativeStruct("bt_hid_device_received_data_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
215 [StructLayout(LayoutKind.Sequential)]
216 internal struct BluetoothHidDeviceReceivedDataStruct
218 [MarshalAsAttribute(UnmanagedType.LPStr)]
219 internal string RemoteAddress;
220 internal BluetoothHidHeaderType headerType;
221 internal BluetoothHidParamType paramType;
222 internal int dataSize;
223 internal IntPtr data;
226 [NativeStruct("bt_avrcp_metadata_attributes_info_s", Include="bluetooth_type.h", PkgConfig="capi-network-bluetooth")]
227 [StructLayout(LayoutKind.Sequential)]
228 internal struct TrackInfoStruct
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;
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
243 [MarshalAsAttribute(UnmanagedType.LPStr)]
244 internal string RemoteAddress;
249 internal static class BluetoothUtils
251 internal static BluetoothDevice ConvertStructToDeviceClass(BluetoothDeviceStruct device)
253 const int DeviceNameLengthMax = 248;
254 BluetoothDevice resultDevice = new BluetoothDevice();
255 Collection<string> uuidList = null;
257 if (device.ServiceCount > 0)
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);
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;
282 if (device.ManufacturerData != IntPtr.Zero)
283 resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(device.ManufacturerData, device.ManufacturerDataLength);
288 internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
290 BluetoothDevice resultDevice = new BluetoothDevice();
291 Collection<string> uuidList = null;
292 const int DeviceNameLengthMax = 248;
293 const int UuidLengthMax = 50;
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)
302 string uuid = Marshal.PtrToStringAnsi(extension, UuidLengthMax);
308 resultDevice.RemoteDeviceAddress = structDevice.Address;
309 if (structDevice.Name != IntPtr.Zero)
310 resultDevice.RemoteDeviceName = Marshal.PtrToStringAnsi(structDevice.Name, DeviceNameLengthMax);
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;
317 resultDevice.RemoteDeviceRssi = structDevice.Rssi;
318 resultDevice.RemoteAppearance = structDevice.AppearanceType;
320 if (structDevice.ServiceCount > 0) {
321 resultDevice.RemoteDeviceService = uuidList;
322 resultDevice.RemoteDeviceCount = structDevice.ServiceCount;
325 resultDevice.RemotePaired = structDevice.IsPaired;
326 resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
328 if (structDevice.ManufacturerData != IntPtr.Zero)
329 resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(structDevice.ManufacturerData, structDevice.ManufacturerDataLength);
334 internal static BluetoothDeviceSdpData ConvertStructToSdpData(BluetoothDeviceSdpStruct structData)
336 BluetoothDeviceSdpData resultData = new BluetoothDeviceSdpData();
337 Collection<string> uuidList = null;
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);
349 resultData.Uuid = uuidList;
350 resultData.Address = structData.DeviceAddress;
355 internal static BluetoothDeviceConnectionData ConvertStructToConnectionData(BluetoothDeviceConnectionStruct structInfo)
357 BluetoothDeviceConnectionData resultData = new BluetoothDeviceConnectionData();
358 resultData.RemoteAddress = structInfo.Address;
359 resultData.Link = structInfo.LinkType;
360 resultData.Reason = structInfo.DisconnectReason;
364 internal static BluetoothLeScanData ConvertStructToLeScanData(BluetoothLeScanDataStruct structScanData)
366 BluetoothLeScanData scanData = new BluetoothLeScanData();
368 scanData.RemoteAddress = structScanData.RemoteAddress;
369 scanData.AddressType = structScanData.AddressType;
370 scanData.Rssi = structScanData.Rssi;
372 if (structScanData.AdvDataLength > 0)
374 scanData.AdvDataLength = structScanData.AdvDataLength;
375 scanData.AdvData = new byte[scanData.AdvDataLength];
376 Marshal.Copy (structScanData.AdvData, scanData.AdvData, 0, scanData.AdvDataLength);
379 if (structScanData.ScanDataLength > 0)
381 scanData.ScanDataLength = structScanData.ScanDataLength;
382 scanData.ScanData = new byte[scanData.ScanDataLength];
383 Marshal.Copy (structScanData.ScanData, scanData.ScanData, 0, scanData.ScanDataLength);
388 internal static BluetoothLeScanDataStruct ConvertLeScanDataToStruct(BluetoothLeScanData scanData)
390 BluetoothLeScanDataStruct scanDataStruct = new BluetoothLeScanDataStruct();
392 scanDataStruct.RemoteAddress = scanData.RemoteAddress;
393 scanDataStruct.AddressType = scanData.AddressType;
394 scanDataStruct.Rssi = scanData.Rssi;
396 if (scanData.AdvDataLength > 0)
398 scanDataStruct.AdvDataLength = scanData.AdvDataLength;
399 scanDataStruct.AdvData = Marshal.AllocHGlobal(scanData.AdvDataLength);
400 Marshal.Copy (scanData.AdvData, 0, scanDataStruct.AdvData, scanData.AdvDataLength);
403 if (scanData.ScanDataLength > 0)
405 scanDataStruct.ScanDataLength = scanData.ScanDataLength;
406 scanDataStruct.ScanData = Marshal.AllocHGlobal(scanData.ScanDataLength);
407 Marshal.Copy (scanData.ScanData, 0, scanDataStruct.ScanData, scanData.ScanDataLength);
410 return scanDataStruct;
413 internal static BluetoothLeServiceData ConvertStructToLeServiceData(BluetoothLeServiceDataStruct structServiceData)
415 BluetoothLeServiceData serviceData = new BluetoothLeServiceData();
416 Log.Info(Globals.LogTag, "ServiceDataLength" + structServiceData.ServiceDataLength);
418 if (structServiceData.ServiceDataLength > 0)
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);
428 internal static SocketData ConvertStructToSocketData(SocketDataStruct structInfo)
430 SocketData data = new SocketData();
431 Log.Info(Globals.LogTag, "SocketDataLength" + structInfo.DataSize);
433 data._fd = structInfo.SocketFd;
434 if (structInfo.DataSize > 0)
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);
444 internal static SocketConnection ConvertStructToSocketConnection(SocketConnectionStruct structInfo)
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;
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;
458 return connectionInfo;