Esempio n. 1
0
 private void VerifyAttribute(object sender, AttributeValueEventArgs e)
 {
     if (e.connection == this.Connection && e.atthandle == this.AttributeHandle)
     {
         _attrValueResponse = e;
     }
 }
Esempio n. 2
0
 private void OnClientAttributeValue(object sender, AttributeValueEventArgs e)
 {
     if (e.connection == _connectionHandle && CharacteristicsByHandle.ContainsKey(e.atthandle))
     {
         var characteristic = CharacteristicsByHandle[e.atthandle];
         characteristic.TriggerCharacteristicValueChanged(e.value);
     }
 }
Esempio n. 3
0
        private void HandleAttributeValue(object sender, AttributeValueEventArgs e)
        {
            BlePeripheralCharacteristic characteristic;

            if (this.PeripheralMap.FindCharacteristicByHandle(e.atthandle, out characteristic))
            {
                OnCharacteristicValueChanged(this, characteristic.AttributeUUID, e.value);
            }
        }
Esempio n. 4
0
        private async Task <(ProcedureCompletedEventArgs, AttributeValueEventArgs)> ReadLong(byte connection, ushort attributeHandle, CancellationToken cancellationToken, int timeout = DefaultTimeout)
        {
            Logger?.LogDebug($"Read long device characteristic, Connection={connection}, AttributeHandle={attributeHandle}");

            var taskCompletionSource = new TaskCompletionSource <(ProcedureCompletedEventArgs, AttributeValueEventArgs)>();

            using (var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
            {
                cancellationTokenSource.CancelAfter(timeout);

                AttributeValueEventArgs attributeValueEventArgs = null;
                var attributeValueBuffer = new List <byte>();

                void OnAttributeValue(object sender, AttributeValueEventArgs e)
                {
                    if (e.connection == connection && e.atthandle == attributeHandle)
                    {
                        attributeValueEventArgs = e;
                        attributeValueBuffer.AddRange(e.value);
                    }
                }

                void OnProcedureCompleted(object sender, ProcedureCompletedEventArgs e)
                {
                    if (e.connection == connection && e.chrhandle == attributeHandle)
                    {
                        if (attributeValueEventArgs != null)
                        {
                            taskCompletionSource.SetResult((e, new AttributeValueEventArgs(attributeValueEventArgs.connection, attributeValueEventArgs.atthandle, attributeValueEventArgs.type, attributeValueBuffer.ToArray())));
                        }
                        else
                        {
                            taskCompletionSource.SetException(new Exception("Didn't receive any attribute values"));
                        }
                    }
                }

                try
                {
                    BgLib.BLEEventATTClientAttributeValue     += OnAttributeValue;
                    BgLib.BLEEventATTClientProcedureCompleted += OnProcedureCompleted;

                    using (cancellationTokenSource.Token.Register(() => taskCompletionSource.SetCanceled(), false))
                    {
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandATTClientReadLong(connection, attributeHandle));

                        return(await taskCompletionSource.Task.ConfigureAwait(false));
                    }
                }
                finally
                {
                    BgLib.BLEEventATTClientAttributeValue     -= OnAttributeValue;
                    BgLib.BLEEventATTClientProcedureCompleted -= OnProcedureCompleted;
                }
            }
        }
        private void OnClientAttributeValueEvent(object sender, AttributeValueEventArgs e)
        {
            var attribute = BleMappings.MapBleDeviceAttribute(e);

            if (attribute == null)
            {
                return;
            }
            _attributeNotifications?.Invoke(attribute);
        }
Esempio n. 6
0
 public static BgBleDeviceAttribute MapBleDeviceAttribute(AttributeValueEventArgs e)
 {
     return(new BgBleDeviceAttribute
     {
         ConnectionHandle = e.connection,
         Handle = e.atthandle,
         Type = e.type,
         Data = e.value,
         Value = ByteArrayToHexString(e.value)
     });
 }
Esempio n. 7
0
        public void DataReceivedFromTappy(object sender, AttributeValueEventArgs e)
        {
            if (e.value == null)
            {
                return;
            }

            lock (bufferLock)
                tappyBuffer.AddRange(e.value);

            OnDataReceived(e);
        }
Esempio n. 8
0
        private void OnAttributeValue(object sender, AttributeValueEventArgs e)
        {
            var notifyOrIndicate = e.Type == AttributeValueType.Notify || e.Type == AttributeValueType.Indicate;

            if (!notifyOrIndicate ||
                !_peripherals.TryGetValue(e.Connection, out var peripheral) ||
                !peripheral.Services.TryGetValue(e.AttHandle, out var service) ||
                !peripheral.Characteristics.TryGetValue(e.AttHandle, out var characteristic))
            {
                return;
            }
            var eventArgs = new GattCharacteristicValueEventArgs(peripheral, service, characteristic, e.Value);

            CharacteristicValueChanged?.Invoke(this, eventArgs);
        }
        private void BGApi_NotificationAndIndicationHandler(object sender, AttributeValueEventArgs e)
        {
            try {
                lock (StateLock)
                    if (State == CharacteristicState.Disposed)
                    {
                        return;
                    }

                if (CharacteristicValueHandle == null || e.atthandle != CharacteristicValueHandle)
                {
                    return;
                }

                var type = (BGATTClientAttributeValueType)Enum.ToObject(typeof(BGATTClientAttributeValueType), e.type);

                switch (type)
                {
                case BGATTClientAttributeValueType.Notify:
                    Debug.WriteLine($"Notification received: charValHand={CharacteristicValueHandle:X4} : conn={e.connection:X2} atthandle={e.atthandle:X4} type={e.type:X2} value={e.value.ToHexString( false, ":" )}");
                    if (!IsNotificationsEnabled)
                    {
                        break;
                    }
                    OnNotification(e.value);
                    break;

                case BGATTClientAttributeValueType.Indicate:
                    Debug.WriteLine($"Indication received: charValHand={CharacteristicValueHandle:X4} : conn={e.connection:X2} atthandle={e.atthandle:X4} type={e.type:X2} value={e.value.ToHexString( false, ":" )}");
                    if (!IsIndicationsEnabled)
                    {
                        break;
                    }
                    OnIndication(e.value);
                    break;
                }
            }
            catch (Exception ex) {
                if (_exceptionToRethrow == null)
                {
                    _exceptionToRethrow = ExceptionDispatchInfo.Capture(ex);
                }
            }
        }
Esempio n. 10
0
        private void BGApi_ATTClientAttributeValue(object sender, AttributeValueEventArgs e)
        {
            try {
                if (State == ServiceState.Disposed)
                {
                    return;
                }

                Debug.WriteLine($"Found attribute: conn={e.connection:X2} attuuid={e.atthandle:X4} type={e.type:X2} value={e.value.ToHexString( false, "-" )} ");

                switch (_uuidOfAttributeBeingRead)
                {
                case GATTAttributeType.CharacteristicUUID:
                    var characteristic = new BGCharacteristic(LocalPeripheral, _bgApi)
                    {
                        CharacteristicDeclarationHandle = (UInt16)e.atthandle
                    };
                    characteristic.ParseCharacteristicDeclarationData(e.value);
                    AddCharacteristic(characteristic);
                    Debug.WriteLine("Created and added characteristic object: " + characteristic.UUID.ToHexString(true));
                    break;

                case GATTAttributeType.CharacteristicClientConfigurationUUID:
                    _clientConfigurationAttributeHandles.Add((UInt16)e.atthandle);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception ex) {
                if (_exceptionToRethrow == null)
                {
                    _exceptionToRethrow = ExceptionDispatchInfo.Capture(ex);
                }
            }
        }
        private void BGApi_ATTClientAttributeValue(object sender, AttributeValueEventArgs e)
        {
            try {
                lock (StateLock)
                    if (State == CharacteristicState.Disposed)
                    {
                        return;
                    }

                Debug.WriteLine($"Read attribute: conn={e.connection:X2} atthandle={e.atthandle:X4} type={e.type:X2} value={e.value.ToHexString( false, ":" )}");

                _handleOfLastAccessedAttribute = e.atthandle;
                _valueOfLastAttributeRead      = e.value;
            }
            catch (Exception ex) {
                if (_exceptionToRethrow == null)
                {
                    _exceptionToRethrow = ExceptionDispatchInfo.Capture(ex);
                }
            }
            finally {
                _attributeValueWaitHandle.Set();
            }
        }
Esempio n. 12
0
 void bglib_BLEEventATTClientAttributeValue(object sender, AttributeValueEventArgs e)
 {
     //TODO once we write to the attribute and the too updates we should get notified here
     throw new NotImplementedException();
 }
Esempio n. 13
0
 private void Bglib_BLEEventATTClientAttributeValue(object sender, AttributeValueEventArgs e) => ATTClientAttributeValue?.Invoke(this, e);