Example #1
0
        public LocalDeviceInfo GetLocalInfo(string deviceMac)
        {
            if (_cacheRootKey == null)
            {
                return(null);
            }

            var keyName = BleUtils.MacToConnectionId(deviceMac);

            var cacheKey = _cacheRootKey.OpenSubKey(keyName);

            if (cacheKey == null)
            {
                return(null);
            }

            var info = new LocalDeviceInfo();

            info.SerialNo   = (string)cacheKey.GetValue(nameof(info.SerialNo));
            info.RFID       = (string)cacheKey.GetValue(nameof(info.RFID));
            info.OwnerName  = (string)cacheKey.GetValue(nameof(info.OwnerName));
            info.OwnerEmail = (string)cacheKey.GetValue(nameof(info.OwnerEmail));
            info.Mac        = deviceMac;

            return(info);
        }
Example #2
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;
                }
            }
        }
Example #3
0
        Task OnDeviceBatteryChanged(DeviceBatteryChangedMessage obj)
        {
            lock (filterLock)
            {
                // Todo: Might need a better algorithm for detecting low battery
                var mac = BleUtils.DeviceIdToMac(obj.DeviceId);
                if (!vaultIdFilter.Contains(mac) && obj.Battery != 0 && obj.Battery <= 25)
                {
                    var device = _deviceManager.Devices.FirstOrDefault(d => d.Mac == mac);
                    if (device != null && device.IsConnected && device.FinishedMainFlow)
                    {
                        vaultIdFilter.Add(mac);

                        _messenger.Publish(new ShowLowBatteryNotificationMessage(
                                               TranslationSource.Instance["LowBatteryNotification.Message"],
                                               TranslationSource.Instance["LowBatteryNotification.Title"],
                                               new NotificationOptions {
                            CloseTimeout = NotificationOptions.NoTimeout
                        },
                                               obj.DeviceId + NOTIFICATION_ID_SUFFIX));
                    }
                }
            }

            return(Task.CompletedTask);
        }
Example #4
0
        public bool IsIgnored(string mac)
        {
            lock (_lock)
            {
                RemoveTimedOutRecords();

                return(_ignoreList.Any(m => m == BleUtils.ConnectionIdToMac(mac)));
            }
        }
Example #5
0
        public void RemoveLocalInfo(string deviceMac)
        {
            if (_cacheRootKey == null)
            {
                return;
            }

            var keyName = BleUtils.MacToConnectionId(deviceMac);

            _cacheRootKey.DeleteSubKeyTree(keyName);
        }
Example #6
0
        public void SaveLocalInfo(LocalDeviceInfo info)
        {
            if (_cacheRootKey == null)
            {
                return;
            }

            var keyName = BleUtils.MacToConnectionId(info.Mac);

            var cacheKey = _cacheRootKey.CreateSubKey(keyName);

            cacheKey.SetValue(nameof(info.SerialNo), info.SerialNo ?? string.Empty, RegistryValueKind.String);
            cacheKey.SetValue(nameof(info.RFID), info.RFID ?? string.Empty, RegistryValueKind.String);
            cacheKey.SetValue(nameof(info.OwnerName), info.OwnerName ?? string.Empty, RegistryValueKind.String);
            cacheKey.SetValue(nameof(info.OwnerEmail), info.OwnerEmail ?? string.Empty, RegistryValueKind.String);
        }
Example #7
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);
                }
            }
        }