public async Task <byte> ATTClientFindInformationAsync(byte connectionHandle, UInt16 handleRangeStart, UInt16 handleRangeEnd) { const int TIMEOUT_MS = 6000; var cmd = _bglib.BLECommandATTClientFindInformation(connectionHandle, handleRangeStart, handleRangeEnd); await _bglib.SendCommandAsync(cmd).ConfigureAwait(false); using (var cts = new CancellationTokenSource(TIMEOUT_MS)) { bool didTimeout = false; try { await _cmdRespWaitHandle.WaitAsync(cts.Token).ConfigureAwait(false); } catch (OperationCanceledException) { didTimeout = true; } if (didTimeout) { throw new TimeoutException(); } } if (_lastResult != 0) { var error = Enum.ToObject(typeof(BGErrorCode), _lastResult); string errMsg = $"ATTClientFindInformation returned: error={error} code={_lastResult:X4}"; throw new Exception(errMsg); } byte conHandle = ((FindInformationEventArgs)_lastResponseArgs).connection; return(conHandle); }
public void FetchCharacteristics(CancellationToken appToken, int connectTimeoutInMilliseconds, BgBleDeviceService deviceService) { deviceService.Characteristics.Clear(); try { var lockTaken = false; Monitor.TryEnter(ThreadLock, 150, ref lockTaken); if (lockTaken) { if (_busy) { return; } _busy = true; Monitor.Exit(ThreadLock); } _deviceService = deviceService; _currentAction = new CancellationTokenSource(); var populateClientInformationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(appToken, _currentAction.Token); _bglib.SendCommand(_serialPort, _bglib.BLECommandATTClientFindInformation(deviceService.ConnectionHandle, deviceService.Start, deviceService.End)); WaitHandle.WaitAny(new[] { populateClientInformationTokenSource.Token.WaitHandle }, connectTimeoutInMilliseconds); } catch (Exception exception) { _logger.LogWarning(exception, $"ControlPlus Repository: Unable to populate Client Information {deviceService.ServiceId}"); } _lastCharacteristic = null; _deviceService = null; _device = null; _busy = false; }