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.Collections.Generic;
19 using System.Runtime.CompilerServices;
20 using System.Runtime.InteropServices;
22 using System.Threading.Tasks;
24 namespace Tizen.Network.Bluetooth
26 internal class BluetoothGattServerImpl
28 private BluetoothGattServerHandle _handle;
29 internal event EventHandler<NotificationSentEventArg> _notificationSent;
31 Dictionary<int, TaskCompletionSource<bool>> _sendIndicationTaskSource = new Dictionary<int, TaskCompletionSource<bool>>();
32 Interop.Bluetooth.BtGattServerNotificationSentCallback _sendIndicationCallback;
34 internal BluetoothGattServerImpl()
36 _sendIndicationCallback = SendIndicationCallback;
38 int err = Interop.Bluetooth.BtGattServerInitialize();
39 GattUtil.ThrowForError(err, "Failed to initialize server");
41 err = Interop.Bluetooth.BtGattServerCreate(out _handle);
42 GattUtil.ThrowForError(err, "Failed to create server");
47 int err = Interop.Bluetooth.BtGattServerStart();
48 GattUtil.ThrowForError(err, "Failed to start server");
51 internal void RegisterGattService(BluetoothGattServer server, BluetoothGattService service)
53 int err = Interop.Bluetooth.BtGattServerRegisterService(_handle, service.GetHandle());
54 GattUtil.ThrowForError(err, "Failed to Register service");
56 service.SetParent(server);
59 internal void UnregisterGattService(BluetoothGattService service)
61 int err = Interop.Bluetooth.BtGattServerUnregisterService(_handle, service.GetHandle());
62 GattUtil.ThrowForError(err, "Failed to Unregister service");
64 service.UnregisterService();
67 internal void UnregisterAllGattServices(BluetoothGattServer server)
69 int err = Interop.Bluetooth.BtGattServerUnregisterAllServices(_handle);
70 GattUtil.ThrowForError(err, "Failed to Unregister all services");
73 internal BluetoothGattService GetService(BluetoothGattServer server, string uuid)
75 BluetoothGattAttributeHandle serviceHandle;
76 int err = Interop.Bluetooth.BtGattServerGetService(_handle, uuid, out serviceHandle);
79 GattUtil.Error(err, string.Format("Failed to get service with UUID ({0})", uuid));
83 BluetoothGattService service = new BluetoothGattService(new BluetoothGattServiceImpl(serviceHandle), uuid); ;
84 service.SetParent(server);
88 internal IEnumerable<BluetoothGattService> GetServices(BluetoothGattServer server)
90 List<BluetoothGattService> attribututeList = new List<BluetoothGattService>();
91 Interop.Bluetooth.BtGattForeachCallback cb = (total, index, attributeHandle, userData) =>
93 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
94 BluetoothGattService service = BluetoothGattServiceImpl.CreateBluetoothGattService(handle, ""); ;
97 service.SetParent(server);
98 attribututeList.Add(service);
103 int err = Interop.Bluetooth.BtGattServerForeachServices(_handle, cb, IntPtr.Zero);
104 GattUtil.Error(err, "Failed to get all services");
106 return attribututeList;
109 internal void SendResponse(int requestId, int request_type, int status, byte[] value, int offset)
111 int err = Interop.Bluetooth.BtGattServerSendResponse(requestId, request_type, offset, status, value, value.Length);
112 GattUtil.ThrowForError(err, string.Format("Failed to send response for request Id {0}", requestId));
115 internal void SendNotification(BluetoothGattCharacteristic characteristic, string clientAddress)
117 int err = Interop.Bluetooth.BtGattServerNotify(characteristic.GetHandle(), null, clientAddress, IntPtr.Zero);
118 GattUtil.ThrowForError(err, string.Format("Failed to send value changed notification for characteristic uuid {0}", characteristic.Uuid));
121 void SendIndicationCallback(int result, string clientAddress, IntPtr serverHandle, IntPtr characteristicHandle, bool completed, IntPtr userData)
123 int requestId = (int)userData;
124 if (_sendIndicationTaskSource.ContainsKey(requestId))
126 _notificationSent?.Invoke(this, new NotificationSentEventArg(null, clientAddress, result, completed));
129 _sendIndicationTaskSource[requestId].SetResult(true);
133 _sendIndicationTaskSource[requestId].SetResult(false);
135 _sendIndicationTaskSource.Remove(requestId);
139 internal Task<bool> SendIndicationAsync(BluetoothGattServer server, BluetoothGattCharacteristic characteristic, string clientAddress)
141 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
146 requestId = _requestId++;
147 _sendIndicationTaskSource[requestId] = task;
150 int err = Interop.Bluetooth.BtGattServerNotify(characteristic.GetHandle(), _sendIndicationCallback, clientAddress, (IntPtr)requestId);
153 GattUtil.Error(err, string.Format("Failed to send value changed indication for characteristic uuid {0}", characteristic.Uuid));
154 task.SetResult(false);
155 _sendIndicationTaskSource.Remove(requestId);
156 BluetoothErrorFactory.ThrowBluetoothException(err);
161 internal BluetoothGattServerHandle GetHandle()
167 internal class BluetoothGattClientImpl
169 private BluetoothGattClientHandle _handle;
171 Dictionary<int, TaskCompletionSource<bool>> _readValueTaskSource = new Dictionary<int, TaskCompletionSource<bool>>();
172 Interop.Bluetooth.BtGattClientRequestCompletedCallback _readValueCallback;
173 Dictionary<int, TaskCompletionSource<bool>> _writeValueTaskSource = new Dictionary<int, TaskCompletionSource<bool>>();
174 Interop.Bluetooth.BtGattClientRequestCompletedCallback _writeValueCallback;
176 internal BluetoothGattClientImpl(string remoteAddress)
178 _readValueCallback = ReadValueCallback;
179 _writeValueCallback = WriteValueCallback;
181 if (BluetoothAdapter.IsBluetoothEnabled)
183 int err = Interop.Bluetooth.BtGattClientCreate(remoteAddress, out _handle);
184 GattUtil.ThrowForError(err, "Failed to get native client handle");
188 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
192 internal void Connect(string remoteAddress, bool autoConnect)
194 int err = Interop.Bluetooth.GattConnect(remoteAddress, autoConnect);
195 GattUtil.ThrowForError(err, "Failed to connect to remote address");
198 internal void Disconnect(string remoteAddress)
200 int err = Interop.Bluetooth.GattDisconnect(remoteAddress);
201 GattUtil.ThrowForError(err, "Failed to disconnect to remote address");
204 internal string GetRemoteAddress()
206 string remoteAddress;
207 int err = Interop.Bluetooth.BtGattClientGetRemoteAddress(_handle, out remoteAddress);
208 GattUtil.ThrowForError(err, "Failed to get remote address for this client");
210 return remoteAddress;
213 internal BluetoothGattService GetService(BluetoothGattClient client, string uuid)
215 BluetoothGattAttributeHandle serviceHandle;
216 int err = Interop.Bluetooth.BtGattClientGetService(_handle, uuid, out serviceHandle);
219 GattUtil.Error(err, string.Format("Failed to get service with UUID ({0})", uuid));
223 BluetoothGattService service = new BluetoothGattService(new BluetoothGattServiceImpl(serviceHandle), uuid); ;
224 service.SetParent(client);
228 internal IEnumerable<BluetoothGattService> GetServices(BluetoothGattClient client)
230 List<BluetoothGattService> attribututeList = new List<BluetoothGattService>();
231 Interop.Bluetooth.BtGattForeachCallback cb = (total, index, attributeHandle, userData) =>
233 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
234 BluetoothGattService service = BluetoothGattServiceImpl.CreateBluetoothGattService(handle, "");
237 service.SetParent(client);
238 attribututeList.Add(service);
243 int err = Interop.Bluetooth.BtGattClientForeachServices(_handle, cb, IntPtr.Zero);
244 GattUtil.Error(err, "Failed to get all services");
246 return attribututeList;
249 void ReadValueCallback(int result, IntPtr requestHandle, IntPtr userData)
251 int requestId = (int)userData;
252 if (_readValueTaskSource.ContainsKey(requestId))
254 if (result == (int)BluetoothError.None)
256 _readValueTaskSource[requestId].SetResult(true);
260 _readValueTaskSource[requestId].SetResult(false);
263 _readValueTaskSource.Remove(requestId);
266 internal Task<bool> ReadValueAsyncTask(BluetoothGattAttributeHandle handle)
268 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
273 requestId = _requestId++;
274 _readValueTaskSource[requestId] = task;
277 int err = Interop.Bluetooth.BtGattClientReadValue(handle, _readValueCallback, (IntPtr)requestId);
280 GattUtil.Error(err, "Failed to read value from remote device");
281 task.SetResult(false);
282 _readValueTaskSource.Remove(requestId);
283 BluetoothErrorFactory.ThrowBluetoothException(err);
288 void WriteValueCallback(int result, IntPtr requestHandle, IntPtr userData)
290 int requestId = (int)userData;
291 if (_writeValueTaskSource.ContainsKey(requestId))
293 if (result == (int)BluetoothError.None)
295 _writeValueTaskSource[requestId].SetResult(true);
299 _writeValueTaskSource[requestId].SetResult(false);
302 _writeValueTaskSource.Remove(requestId);
305 internal Task<bool> WriteValueAsyncTask(BluetoothGattAttributeHandle handle)
307 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
312 requestId = _requestId++;
313 _writeValueTaskSource[requestId] = task;
316 int err = Interop.Bluetooth.BtGattClientWriteValue(handle, _writeValueCallback, (IntPtr)requestId);
319 GattUtil.Error(err, "Failed to write value to remote device");
320 task.SetResult(false);
321 _writeValueTaskSource.Remove(requestId);
322 BluetoothErrorFactory.ThrowBluetoothException(err);
327 internal BluetoothGattClientHandle GetHandle()
333 internal class BluetoothGattServiceImpl : BluetoothGattAttributeImpl
335 internal BluetoothGattServiceImpl(string uuid, BluetoothGattServiceType type)
337 int err = Interop.Bluetooth.BtGattServiceCreate(uuid, (int)type, out _handle);
338 GattUtil.ThrowForError(err, "Failed to get native service handle");
341 internal BluetoothGattServiceImpl(BluetoothGattAttributeHandle handle)
346 internal static BluetoothGattService CreateBluetoothGattService(BluetoothGattAttributeHandle handle, string uuid)
350 int err = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
351 GattUtil.ThrowForError(err, "Failed to get UUID");
354 BluetoothGattServiceImpl impl = new BluetoothGattServiceImpl(handle);
355 return new BluetoothGattService(impl, uuid);
358 internal void AddCharacteristic(BluetoothGattCharacteristic characteristic)
360 int err = Interop.Bluetooth.BtGattServiceAddCharacteristic(_handle, characteristic.GetHandle());
361 GattUtil.ThrowForError(err, string.Format("Failed to add characteristic with UUID ({0})", characteristic.Uuid));
364 internal BluetoothGattCharacteristic GetCharacteristic(BluetoothGattService service, string uuid)
366 BluetoothGattAttributeHandle attributeHandle;
367 int err = Interop.Bluetooth.BtGattServiceGetCharacteristic(_handle, uuid, out attributeHandle);
370 GattUtil.Error(err, string.Format("Failed to get Characteristic with UUID ({0})", uuid));
374 BluetoothGattCharacteristic Characteristic = BluetoothGattCharacteristicImpl.CreateBluetoothGattGattCharacteristic(attributeHandle, uuid);
375 Characteristic.SetParent(service);
376 return Characteristic;
379 internal IEnumerable<BluetoothGattCharacteristic> GetCharacteristics(BluetoothGattService service)
381 List<BluetoothGattCharacteristic> attribututeList = new List<BluetoothGattCharacteristic>();
382 Interop.Bluetooth.BtGattForeachCallback cb = (total, index, attributeHandle, userData) =>
384 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
385 BluetoothGattCharacteristic Characteristic = BluetoothGattCharacteristicImpl.CreateBluetoothGattGattCharacteristic(handle, "");
386 if (Characteristic != null)
388 Characteristic.SetParent(service);
389 attribututeList.Add(Characteristic);
394 int err = Interop.Bluetooth.BtGattServiceForeachCharacteristics(service.GetHandle(), cb, IntPtr.Zero);
395 GattUtil.Error(err, "Failed to get all Characteristic");
397 return attribututeList;
400 internal void AddIncludeService(BluetoothGattService includedService)
402 int err = Interop.Bluetooth.BtGattServiceAddIncludedService(_handle, includedService.GetHandle());
403 GattUtil.ThrowForError(err, string.Format("Failed to add service with UUID ({0})", includedService.Uuid));
406 internal BluetoothGattService GetIncludeService(BluetoothGattService parentService, string uuid)
408 BluetoothGattAttributeHandle attributeHandle;
409 int err = Interop.Bluetooth.BtGattServiceGetIncludedService(_handle, uuid, out attributeHandle);
412 GattUtil.Error(err, string.Format("Failed to get included service with UUID ({0})", uuid));
416 BluetoothGattService service = new BluetoothGattService(new BluetoothGattServiceImpl(attributeHandle), uuid);
417 service.SetParent(parentService);
421 internal IEnumerable<BluetoothGattService> GetIncludeServices(BluetoothGattService parentService)
423 List<BluetoothGattService> attribututeList = new List<BluetoothGattService>();
424 Interop.Bluetooth.BtGattForeachCallback cb = (total, index, attributeHandle, userData) =>
426 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
427 BluetoothGattService service = BluetoothGattServiceImpl.CreateBluetoothGattService(handle, "");
430 service.SetParent(parentService);
431 attribututeList.Add(service);
436 int err = Interop.Bluetooth.BtGattServiceForeachIncludedServices(parentService.GetHandle(), cb, IntPtr.Zero);
437 GattUtil.Error(err, "Failed to get all services");
439 return attribututeList;
443 internal class BluetoothGattCharacteristicImpl : BluetoothGattAttributeImpl
445 internal BluetoothGattCharacteristicImpl(string uuid, BluetoothGattPermission permission, BluetoothGattProperty property, byte[] value)
447 int err = Interop.Bluetooth.BtGattCharacteristicCreate(uuid, (int)permission, (int)property, value, value.Length, out _handle);
448 GattUtil.ThrowForError(err, "Failed to get native characteristic handle");
451 internal BluetoothGattCharacteristicImpl(BluetoothGattAttributeHandle handle)
456 internal static BluetoothGattCharacteristic CreateBluetoothGattGattCharacteristic(BluetoothGattAttributeHandle handle, string uuid)
459 int err = Interop.Bluetooth.BtGattCharacteristicGetPermissions(handle, out permission);
460 GattUtil.ThrowForError(err, "Failed to get permissions");
464 err = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
465 GattUtil.ThrowForError(err, "Failed to get UUID");
468 BluetoothGattCharacteristicImpl impl = new BluetoothGattCharacteristicImpl(handle);
469 return new BluetoothGattCharacteristic(impl, uuid, (BluetoothGattPermission)permission);
472 internal void SetCharacteristicValueChangedEvent(Interop.Bluetooth.BtClientCharacteristicValueChangedCallback callback)
474 int err = Interop.Bluetooth.BtGattClientSetCharacteristicValueChangedCallback(_handle, callback, IntPtr.Zero);
475 GattUtil.Error(err, "Failed to set client characteristic value changed callback");
478 internal void UnsetCharacteristicValueChangedEvent()
480 int err = Interop.Bluetooth.BtGattClientUnsetCharacteristicValueChangedCallback(_handle);
481 GattUtil.Error(err, "Failed to unset client characteristic value changed callback");
484 internal void SetNotificationStateChangedEvent(Interop.Bluetooth.BtGattServerNotificationStateChangeCallback callback)
486 int err = Interop.Bluetooth.BtGattServeSetNotificationStateChangeCallback(_handle, callback, IntPtr.Zero);
487 GattUtil.Error(err, "Failed to set characteristic notification state changed callback");
490 internal BluetoothGattProperty GetProperties()
493 int err = Interop.Bluetooth.BtGattCharacteristicGetProperties(_handle, out properties);
494 GattUtil.Error(err, "Failed to get characteristic properties");
495 return (BluetoothGattProperty)properties;
498 internal void SetProperties(BluetoothGattProperty perperties)
500 int err = Interop.Bluetooth.BtGattCharacteristicSetProperties(_handle, (int)perperties);
501 GattUtil.Error(err, "Failed to set characteristic properties");
504 internal BluetoothGattWriteType GetWriteType()
507 int err = Interop.Bluetooth.BtGattCharacteristicGetWriteType(_handle, out writeType);
508 GattUtil.Error(err, "Failed to get characteristic writetype");
509 return (BluetoothGattWriteType) writeType;
512 internal void SetWriteType(BluetoothGattWriteType writeType)
514 int err = Interop.Bluetooth.BtGattCharacteristicSetWriteType(_handle, (int)writeType);
515 GattUtil.Error(err, "Failed to get characteristic writetype");
518 internal void AddDescriptor(BluetoothGattDescriptor descriptor)
520 int err = Interop.Bluetooth.BtGattCharacteristicAddDescriptor(_handle, descriptor.GetHandle());
521 GattUtil.ThrowForError(err, string.Format("Failed to add descriptor with UUID ({0})", descriptor.Uuid));
524 internal BluetoothGattDescriptor GetDescriptor(BluetoothGattCharacteristic characteristic, string uuid)
526 BluetoothGattAttributeHandle handle;
527 int err = Interop.Bluetooth.BtGattCharacteristicGetDescriptor(_handle, uuid, out handle);
530 GattUtil.Error(err, string.Format("Failed to get descriptor with UUID ({0})", uuid));
533 BluetoothGattDescriptor descriptor = BluetoothGattDescriptorImpl.CreateBluetoothGattDescriptor(handle, uuid);
534 descriptor.SetParent(characteristic);
538 internal IEnumerable<BluetoothGattDescriptor> GetDescriptors(BluetoothGattCharacteristic characteristic)
540 List<BluetoothGattDescriptor> attribututeList = new List<BluetoothGattDescriptor>();
541 Interop.Bluetooth.BtGattForeachCallback cb = (total, index, attributeHandle, userData) =>
543 BluetoothGattAttributeHandle handle = new BluetoothGattAttributeHandle(attributeHandle, false);
544 BluetoothGattDescriptor descriptor = BluetoothGattDescriptorImpl.CreateBluetoothGattDescriptor(handle, "");
545 if (descriptor != null)
547 descriptor.SetParent(characteristic);
548 attribututeList.Add(descriptor);
553 int err = Interop.Bluetooth.BtGattCharacteristicForeachDescriptors(characteristic.GetHandle(), cb, IntPtr.Zero);
554 GattUtil.Error(err, "Failed to get all descriptor");
556 return attribututeList;
560 internal class BluetoothGattDescriptorImpl : BluetoothGattAttributeImpl
562 internal BluetoothGattDescriptorImpl(string uuid, BluetoothGattPermission permission, byte[] value)
564 int err = Interop.Bluetooth.BtGattDescriptorCreate(uuid, (int)permission, value, value.Length, out _handle);
565 GattUtil.ThrowForError(err, "Failed to get native descriptor handle");
568 internal BluetoothGattDescriptorImpl(BluetoothGattAttributeHandle handle)
573 internal static BluetoothGattDescriptor CreateBluetoothGattDescriptor(BluetoothGattAttributeHandle handle, string uuid)
576 int err = Interop.Bluetooth.BtGattDescriptorGetPermissions(handle, out permission);
577 GattUtil.ThrowForError(err, string.Format("Failed to get permissions with UUID ({0})", uuid));
581 int ret = Interop.Bluetooth.BtGattGetUuid(handle, out uuid);
582 GattUtil.ThrowForError(ret, "Failed to get UUID");
585 BluetoothGattDescriptorImpl impl = new BluetoothGattDescriptorImpl(handle);
586 return new BluetoothGattDescriptor(impl, uuid, (BluetoothGattPermission)permission);
590 internal abstract class BluetoothGattAttributeImpl
592 protected BluetoothGattAttributeHandle _handle;
594 internal string GetUuid()
597 int err = Interop.Bluetooth.BtGattGetUuid(_handle, out uuid);
598 GattUtil.Error(err, "Failed to get attribute uuid");
603 internal byte[] GetValue()
606 int nativeValueLength;
607 int err = Interop.Bluetooth.BtGattGetValue(_handle, out nativeValue, out nativeValueLength);
608 GattUtil.Error(err, "Failed to get attribute value");
610 return GattUtil.IntPtrToByteArray(nativeValue, nativeValueLength);
613 internal void SetValue(byte[] value)
615 int err = Interop.Bluetooth.BtGattSetValue(_handle, value, value.Length);
616 GattUtil.ThrowForError(err, "Failed to set attribute value");
619 internal string GetValue(int offset)
621 byte[] value = GetValue();
623 int nullPos = value.Length - offset;
624 for (int i = offset; i < value.Length; ++i)
626 if (value[i] == '\0')
633 string strValue = "";
634 strValue = Encoding.UTF8.GetString(value, offset, nullPos - offset);
638 internal int GetValue(IntDataType type, int offset)
641 int err = Interop.Bluetooth.BtGattGetIntValue(_handle, (int)type, offset, out value);
642 GattUtil.Error(err, "Failed to get attribute int value at offset");
646 internal void SetValue(IntDataType type, int value, int offset)
648 int err = Interop.Bluetooth.BtGattSetIntValue(_handle, (int)type, value, offset);
649 GattUtil.ThrowForError(err, "Failed to set attribute int value at offset");
652 internal float GetValue(FloatDataType type, int offset)
655 int err = Interop.Bluetooth.BtGattGetFloatValue(_handle, (int)type, offset, out value);
656 GattUtil.Error(err, "Failed to get attribute float value at offset");
660 internal void SetValue(FloatDataType type, int mantissa, int exponent, int offset)
662 int err = Interop.Bluetooth.BtGattSetFloatValue(_handle, (int)type, mantissa, exponent, offset);
663 GattUtil.ThrowForError(err, "Failed to set attribute float value at offset");
666 internal void SetReadValueRequestedEventCallback(Interop.Bluetooth.BtGattServerReadValueRequestedCallback callback)
668 int err = Interop.Bluetooth.BtGattServerSetReadValueRequestedCallback(_handle, callback, IntPtr.Zero);
669 GattUtil.ThrowForError(err, "Failed to set attribute read value requested callback");
672 internal void SetWriteValueRequestedEventCallback(Interop.Bluetooth.BtGattServerWriteValueRequestedCallback callback)
674 int err = Interop.Bluetooth.BtGattServerSetWriteValueRequestedCallback(_handle, callback, IntPtr.Zero);
675 GattUtil.ThrowForError(err, "Failed to set attribute write value requested callback");
678 internal BluetoothGattAttributeHandle GetHandle()
683 internal void ReleaseHandleOwnership()
685 _handle.ReleaseOwnership();
690 internal class BluetoothGattAttributeHandle : BluetoothGattHandle
692 public BluetoothGattAttributeHandle(IntPtr nativeHandle, bool hasOwnership) : base(nativeHandle, hasOwnership)
696 public BluetoothGattAttributeHandle()
700 protected override bool ReleaseHandle()
702 if (_hasOwnership == true)
704 Interop.Bluetooth.BtGattDestroy(handle);
706 SetHandle(IntPtr.Zero);
711 internal class BluetoothGattClientHandle : BluetoothGattHandle
713 protected override bool ReleaseHandle()
715 if (_hasOwnership == true)
717 Interop.Bluetooth.BtGattClientDestroy(handle);
719 SetHandle(IntPtr.Zero);
724 internal class BluetoothGattServerHandle : BluetoothGattHandle
726 protected override bool ReleaseHandle()
728 if (_hasOwnership == true)
732 err = Interop.Bluetooth.BtGattServerDestroy(handle);
735 Log.Error(Globals.LogTag, "Failed to destroy the server instance");
739 err = Interop.Bluetooth.BtGattServerDeinitialize();
742 Log.Error(Globals.LogTag, "Failed to deinitialize");
743 SetHandle(IntPtr.Zero);
747 SetHandle(IntPtr.Zero);
752 internal abstract class BluetoothGattHandle : SafeHandle
754 protected bool _hasOwnership;
756 public BluetoothGattHandle() : base(IntPtr.Zero, true)
758 _hasOwnership = true;
761 public BluetoothGattHandle(IntPtr nativeHandle, bool hasOwnership) : base(nativeHandle, true)
763 _hasOwnership = hasOwnership;
766 public override bool IsInvalid
768 get { return handle == IntPtr.Zero; }
771 public void ReleaseOwnership()
773 _hasOwnership = false;
777 internal static class GattUtil
779 internal static byte[] IntPtrToByteArray(IntPtr nativeValue, int lenght)
781 byte[] value = new byte[lenght];
782 if (nativeValue != IntPtr.Zero)
784 Marshal.Copy(nativeValue, value, 0, lenght);
789 internal static void Error(int err, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
793 Log.Error(Globals.LogTag, string.Format("{0}, err: {1}", message, (BluetoothError)err), file, func, line);
797 internal static void ThrowForError(int err, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
801 Log.Error(Globals.LogTag, string.Format("{0}, err: {1}", message, (BluetoothError)err), file, func, line);
802 BluetoothErrorFactory.ThrowBluetoothException(err);
806 internal static bool IsFailed(this int err)
808 return err != (int)BluetoothError.None;