Пример #1
0
        public async Task <byte> ATTClientAttributeWriteAsync(byte connectionHandle, UInt16 attHandle, IEnumerable <byte> value)
        {
            const int TIMEOUT_MS = 6000;

            var cmd = _bglib.BLECommandATTClientAttributeWrite(connectionHandle, attHandle, value.ToArray());

            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 = $"ATTClientAttributeWrite returned: error={error} code={_lastResult:X4}";
                throw new Exception(errMsg);
            }

            byte conHandle = ((AttributeWriteEventArgs)_lastResponseArgs).connection;

            return(conHandle);
        }
        public void AttachCharacteristic(CancellationToken appToken, int connectTimeoutInMilliseconds,
                                         Action <BgBleDeviceAttribute> attributeNotifications, BgBleDeviceCharacteristic deviceCharacteristic)
        {
            if (deviceCharacteristic.ClientCharacteristicConfiguratorDescriptorHandle == 0)
            {
                return;
            }
            try
            {
                var lockTaken = false;
                Monitor.TryEnter(ThreadLock, 150, ref lockTaken);
                if (lockTaken)
                {
                    if (_busy)
                    {
                        return;
                    }
                    _busy = true;
                    Monitor.Exit(ThreadLock);
                }
                _attributeNotifications = attributeNotifications;
                _currentAction          = new CancellationTokenSource();
                var attachCharacteristicTokenSource =
                    CancellationTokenSource.CreateLinkedTokenSource(appToken,
                                                                    _currentAction.Token);
                _bglib.SendCommand(_serialPort,
                                   _bglib.BLECommandATTClientAttributeWrite(deviceCharacteristic.ConnectionHandle,
                                                                            deviceCharacteristic.ClientCharacteristicConfiguratorDescriptorHandle, new byte[] { 0x01, 0x00 }));
                WaitHandle.WaitAny(new[] { attachCharacteristicTokenSource.Token.WaitHandle },
                                   connectTimeoutInMilliseconds);
            }
            catch (Exception exception)
            {
                _logger.LogWarning(exception,
                                   $"ControlPlus Repository: Unable to attach characteristic {deviceCharacteristic.CharacteristicId}");
            }

            _busy = false;
        }