Esempio n. 1
0
        async Task OnAdvertismentReceived(AdvertismentReceivedEvent arg)
        {
            DeviceViewModel deviceViewModel = null;

            try
            {
                bool added = false;
                deviceViewModel = _devices.GetOrAdd(arg.DeviceId, (id) =>
                {
                    added         = true;
                    bool isBonded = ConnectionManager.IsBonded(id);
                    return(new DeviceViewModel(BleUtils.ConnectionIdToMac(id), isBonded, _hub));
                });

                if (added)
                {
                    NotifyPropertyChanged(nameof(Devices));
                }

                await deviceViewModel.TryConnect();
            }
            catch (Exception ex)
            {
                if (deviceViewModel != null)
                {
                    deviceViewModel.CustomError = ex.Message;
                }
            }
        }
Esempio n. 2
0
        public bool IsIgnored(string mac)
        {
            lock (_lock)
            {
                RemoveTimedOutRecords();

                return(_ignoreList.Any(m => m == BleUtils.ConnectionIdToMac(mac)));
            }
        }
Esempio n. 3
0
        void BleConnectionManager_AdvertismentReceived(object sender, AdvertismentReceivedEventArgs e)
        {
            lock (_lock)
            {
                RemoveTimedOutRecords();

                var shortMac = BleUtils.ConnectionIdToMac(e.Id);
                if (_ignoreList.Any(m => m == shortMac))
                {
                    var proximity = BleUtils.RssiToProximity(e.Rssi);

                    if (proximity > _workstationSettingsManager.Settings.LockProximity)
                    {
                        _lastAdvRecTime[shortMac] = DateTime.UtcNow;
                    }
                }
            }
        }
        async Task UnlockByTap(AdvertismentReceivedEventArgs adv)
        {
            if (!isRunning)
            {
                return;
            }

            if (adv == null)
            {
                return;
            }

            if (adv.Rssi > SdkConfig.TapProximityUnlockThreshold)
            {
                if (Interlocked.CompareExchange(ref _isConnecting, 1, 0) == 0)
                {
                    try
                    {
                        var mac = BleUtils.ConnectionIdToMac(adv.Id);
                        await _connectionFlowProcessor.ConnectAndUnlock(mac, OnUnlockAttempt);
                    }
                    catch (Exception)
                    {
                        // Silent handling. Log is already printed inside of _connectionFlowProcessor.ConnectAndUnlock()
                    }
                    finally
                    {
                        // this delay allows a user to move away the device from the dongle
                        // and prevents the repeated call of this method
                        await Task.Delay(SdkConfig.DelayAfterMainWorkflow);

                        Interlocked.Exchange(ref _isConnecting, 0);
                    }
                }
            }
        }
        async Task ConnectByProximity(AdvertismentReceivedEventArgs adv)
        {
            if (!isRunning)
            {
                return;
            }

            if (adv == null)
            {
                return;
            }

            if (_isConnecting == 1)
            {
                return;
            }

            if (_macListToConnect.Count == 0)
            {
                return;
            }

            var mac = BleUtils.ConnectionIdToMac(adv.Id);

            if (!_macListToConnect.Any(m => m == mac))
            {
                return;
            }

            var proximity = BleUtils.RssiToProximity(adv.Rssi);
            var settings  = _workstationSettingsManager.Settings;

            if (proximity < settings.UnlockProximity)
            {
                return;
            }

            if (_advIgnoreListMonitor.IsIgnored(mac))
            {
                return;
            }

            if (!_hesAccessManager.HasAccessKey())
            {
                return;
            }

            if (Interlocked.CompareExchange(ref _isConnecting, 1, 0) == 0)
            {
                try
                {
                    var device = _bleDeviceManager.Devices.FirstOrDefault(d => d.Mac == mac && !d.IsRemote && !d.IsBoot);

                    // Unlocked Workstation, Device not found OR Device not connected - dont add to ignore
                    if (!_workstationUnlocker.IsConnected && (device == null || (device != null && !device.IsConnected)))
                    {
                        return;
                    }

                    try
                    {
                        // Unlocked Workstation, Device connected - add to ignore
                        if (!_workstationUnlocker.IsConnected && device != null && device.IsConnected)
                        {
                            return;
                        }

                        // Locked Workstation, Device not found OR not connected - connect add to ignore
                        if (_workstationUnlocker.IsConnected && (device == null || (device != null && !device.IsConnected)))
                        {
                            await _connectionFlowProcessor.ConnectAndUnlock(mac, OnUnlockAttempt);
                        }
                    }
                    catch (Exception)
                    {
                        // Silent handling. Log is already printed inside of _connectionFlowProcessor.ConnectAndUnlock()
                    }
                    finally
                    {
                        _advIgnoreListMonitor.Ignore(mac);
                    }
                }
                finally
                {
                    Interlocked.Exchange(ref _isConnecting, 0);
                }
            }
        }