lib/hidapi/device.rb
def open
      if open?
        self.open_count += 1
        if open_count < 1
          HIDAPI.debug "open_count for open device #{path} is #{open_count}"
          self.open_count = 1
        end
        return self
      end
      self.open_count = 0
      begin
        self.handle = usb_device.open
        raise 'no handle returned' unless handle

        begin
          if handle.kernel_driver_active?(interface)
            handle.detach_kernel_driver(interface)
          end
        rescue LIBUSB::ERROR_NOT_SUPPORTED
          HIDAPI.debug 'cannot determine kernel driver status, continuing to open device'
        end

        handle.claim_interface(interface)

        self.input_endpoint = self.output_endpoint = nil

        # now we need to find the endpoints.
        usb_device.settings
            .keep_if {|item| item.bInterfaceNumber == interface}
            .each do |intf_desc|
          intf_desc.endpoints.each do |ep|
            if ep.transfer_type == :interrupt
              if input_endpoint.nil? && ep.direction == :in
                self.input_endpoint = ep.bEndpointAddress
                self.input_ep_max_packet_size = ep.wMaxPacketSize
              end
              if output_endpoint.nil? && ep.direction == :out
                self.output_endpoint = ep.bEndpointAddress
              end
            end
            break if input_endpoint && output_endpoint
          end
        end

        # output_ep is optional, input_ep is required
        raise 'failed to locate input endpoint' unless input_endpoint

        # start the read thread
        self.input_reports = []
        self.thread_initialized = false
        self.shutdown_thread = false
        self.thread = Thread.start(self) { |dev| dev.send(:execute_read_thread) }
        sleep 0.001 until thread_initialized

      rescue =>e
        handle.close rescue nil
        self.handle = nil
        HIDAPI.debug "failed to open device #{path}: #{e.inspect}"
        raise DeviceOpenFailed, e.inspect
      end
      HIDAPI.debug "opened device #{path}"
      self.open_count = 1
      self
    end
def open
      if open?
        self.open_count += 1
        if open_count < 1
          HIDAPI.debug "open_count for open device #{path} is #{open_count}"
          self.open_count = 1
        end
        return self
      end
      self.open_count = 0
      begin
        self.handle = usb_device.open
        raise 'no handle returned' unless handle

        begin
          if handle.kernel_driver_active?(interface)
            handle.detach_kernel_driver(interface)
          end
        rescue LIBUSB::ERROR_NOT_SUPPORTED
          HIDAPI.debug 'cannot determine kernel driver status, continuing to open device'
        end

        handle.claim_interface(interface)

        self.input_endpoint = self.output_endpoint = nil

        # now we need to find the endpoints.
        usb_device.settings
            .keep_if {|item| item.bInterfaceNumber == interface}
            .each do |intf_desc|
          intf_desc.endpoints.each do |ep|
            if ep.transfer_type == :interrupt
              if input_endpoint.nil? && ep.direction == :in
                self.input_endpoint = ep.bEndpointAddress
                self.input_ep_max_packet_size = ep.wMaxPacketSize
              end
              if output_endpoint.nil? && ep.direction == :out
                self.output_endpoint = ep.bEndpointAddress
              end
            end
            break if input_endpoint && output_endpoint
          end
        end

        # output_ep is optional, input_ep is required
        raise 'failed to locate input endpoint' unless input_endpoint

        # start the read thread
        self.input_reports = []
        self.thread_initialized = false
        self.shutdown_thread = false
        self.thread = Thread.start(self) { |dev| dev.send(:execute_read_thread) }
        sleep 0.001 until thread_initialized

      rescue =>e
        handle.close rescue nil
        self.handle = nil
        HIDAPI.debug "failed to open device #{path}: #{e.inspect}"
        raise DeviceOpenFailed, e.inspect
      end
      HIDAPI.debug "opened device #{path}"
      self.open_count = 1
      self
    end
Opens the device.

 Returns the device.