Example #1
0
        /// <inheritdoc/>
        public bool TryOpen(OpenConfiguration openConfig, out SerialStream stream)
        {
            DeviceStream baseStream;
            bool         result = base.TryOpen(openConfig, out baseStream);

            stream = (SerialStream)baseStream; return(result);
        }
Example #2
0
 public bool TryOpen(OpenConfiguration openConfig, out DeviceStream stream, out Exception exception)
 {
     try
     {
         stream = Open(openConfig); exception = null; return(true);
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         stream = null; exception = e; return(false);
     }
 }
        public DeviceOpenUtility(Device device, OpenConfiguration openConfig)
        {
            _device = device;

            _syncRoot       = new object();
            _resourcePrefix = GetResourcePrefix(device.DevicePath);

            _priority               = (OpenPriority)openConfig.GetOption(OpenOption.Priority);
            _interruptible          = (bool)openConfig.GetOption(OpenOption.Interruptible);
            _transient              = (bool)openConfig.GetOption(OpenOption.Transient);
            _timeoutIfInterruptible = (int)openConfig.GetOption(OpenOption.TimeoutIfInterruptible);
            _timeoutIfTransient     = (int)openConfig.GetOption(OpenOption.TimeoutIfTransient);

            Debug.WriteLine(string.Format("** HIDSharp is opening a device. Our priority is {0}, our interruptible state is {1}, and our transient state is {2}.",
                                          _priority, _interruptible, _transient));
        }
Example #4
0
        public DeviceOpenUtility(Device device, string streamPath, OpenConfiguration openConfig)
        {
            _device = device;

            _syncRoot       = new object();
            _resourcePrefix = GetResourcePrefix(streamPath);

            _priority               = (OpenPriority)openConfig.GetOption(OpenOption.Priority);
            _interruptible          = (bool)openConfig.GetOption(OpenOption.Interruptible);
            _transient              = (bool)openConfig.GetOption(OpenOption.Transient);
            _timeoutIfInterruptible = (int)openConfig.GetOption(OpenOption.TimeoutIfInterruptible);
            _timeoutIfTransient     = (int)openConfig.GetOption(OpenOption.TimeoutIfTransient);

            HidSharpDiagnostics.Trace("Opening a device. Our priority is {0}, our interruptible state is {1}, and our transient state is {2}.",
                                      _priority, _interruptible, _transient);
        }
Example #5
0
        protected virtual DeviceStream OpenDeviceAndRestrictAccess(OpenConfiguration openConfig)
        {
            bool exclusive = (bool)openConfig.GetOption(OpenOption.Exclusive);

            DeviceOpenUtility openUtility = null;

            if (exclusive)
            {
                string streamPath = GetStreamPath(openConfig);
                openUtility = new DeviceOpenUtility(this, streamPath, openConfig);
                openUtility.Open();
            }

            DeviceStream stream;

            try
            {
                stream = OpenDeviceDirectly(openConfig);
                if (exclusive)
                {
                    stream.Closed += (sender, e) => openUtility.Close();
                    openUtility.InterruptRequested += (sender, e) =>
                    {
                        stream.OnInterruptRequested();
                        HidSharpDiagnostics.Trace("Delivered an interrupt request.");
                    };
                }
            }
            catch
            {
                if (exclusive)
                {
                    openUtility.Close();
                }
                throw;
            }

            return(stream);
        }
Example #6
0
        public bool TryOpen(OpenConfiguration openConfig, out DeviceStream stream)
        {
            Exception exception;

            return(TryOpen(openConfig, out stream, out exception));
        }
Example #7
0
 // Used for exclusion... and also may be used inside OpenDeviceDirectly if desired.
 protected virtual string GetStreamPath(OpenConfiguration openConfig)
 {
     return(DevicePath);
 }
Example #8
0
 protected abstract DeviceStream OpenDeviceDirectly(OpenConfiguration openConfig);
Example #9
0
 public DeviceStream Open(OpenConfiguration openConfig)
 {
     return(OpenDeviceAndRestrictAccess(openConfig ?? new OpenConfiguration()));
 }
Example #10
0
 /// <inheritdoc/>
 public new SerialStream Open(OpenConfiguration openConfig)
 {
     return((SerialStream)base.Open(openConfig));
 }
Example #11
0
 /// <inheritdoc/>
 public new HidStream Open(OpenConfiguration openConfig)
 {
     return((HidStream)base.Open(openConfig));
 }