libusb 0.6.0-x86-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +17 -0
  4. data/.yardopts +6 -0
  5. data/COPYING +165 -0
  6. data/Gemfile +11 -0
  7. data/History.md +124 -0
  8. data/README.md +159 -0
  9. data/Rakefile +145 -0
  10. data/appveyor.yml +23 -0
  11. data/lib/libusb.rb +58 -0
  12. data/lib/libusb/bos.rb +306 -0
  13. data/lib/libusb/call.rb +446 -0
  14. data/lib/libusb/compat.rb +376 -0
  15. data/lib/libusb/configuration.rb +155 -0
  16. data/lib/libusb/constants.rb +160 -0
  17. data/lib/libusb/context.rb +426 -0
  18. data/lib/libusb/dependencies.rb +7 -0
  19. data/lib/libusb/dev_handle.rb +564 -0
  20. data/lib/libusb/device.rb +365 -0
  21. data/lib/libusb/endpoint.rb +194 -0
  22. data/lib/libusb/eventmachine.rb +183 -0
  23. data/lib/libusb/interface.rb +60 -0
  24. data/lib/libusb/setting.rb +132 -0
  25. data/lib/libusb/ss_companion.rb +69 -0
  26. data/lib/libusb/stdio.rb +25 -0
  27. data/lib/libusb/transfer.rb +377 -0
  28. data/lib/libusb/version_gem.rb +19 -0
  29. data/lib/libusb/version_struct.rb +63 -0
  30. data/libusb.gemspec +30 -0
  31. data/test/test_libusb_bos.rb +118 -0
  32. data/test/test_libusb_bulk_stream_transfer.rb +50 -0
  33. data/test/test_libusb_capability.rb +23 -0
  34. data/test/test_libusb_compat.rb +78 -0
  35. data/test/test_libusb_compat_mass_storage.rb +81 -0
  36. data/test/test_libusb_descriptors.rb +212 -0
  37. data/test/test_libusb_event_machine.rb +118 -0
  38. data/test/test_libusb_gc.rb +37 -0
  39. data/test/test_libusb_hotplug.rb +127 -0
  40. data/test/test_libusb_iso_transfer.rb +50 -0
  41. data/test/test_libusb_mass_storage.rb +268 -0
  42. data/test/test_libusb_mass_storage2.rb +96 -0
  43. data/test/test_libusb_structs.rb +58 -0
  44. data/test/test_libusb_threads.rb +89 -0
  45. data/test/test_libusb_version.rb +40 -0
  46. data/wireshark-usb-sniffer.png +0 -0
  47. metadata +150 -0
data/libusb.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "libusb/version_gem"
4
+ require "libusb/dependencies"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "libusb"
8
+ s.version = LIBUSB::VERSION
9
+ s.authors = ["Lars Kanis"]
10
+ s.email = ["lars@greiz-reinsdorf.de"]
11
+ s.homepage = "http://github.com/larskanis/libusb"
12
+ s.summary = %q{Access USB devices from Ruby via libusb-1.0}
13
+ s.description = %q{LIBUSB is a Ruby binding that gives Ruby programmers access to arbitrary USB devices}
14
+ s.licenses = ['LGPL-3.0']
15
+ s.rdoc_options = %w[--main README.md --charset=UTF-8]
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.files << "ports/archives/libusb-#{LIBUSB::LIBUSB_VERSION}.tar.bz2"
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ s.extensions = ['ext/extconf.rb']
23
+
24
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9.3")
25
+ s.add_runtime_dependency 'ffi', '~> 1.0'
26
+ s.add_runtime_dependency 'mini_portile2', LIBUSB::MINI_PORTILE_VERSION
27
+ s.add_development_dependency 'rake-compiler', '~> 0.9'
28
+ s.add_development_dependency 'rake-compiler-dock', '~> 0.2'
29
+ s.add_development_dependency 'bundler', '~> 1.0'
30
+ end
@@ -0,0 +1,118 @@
1
+ # This file is part of Libusb for Ruby.
2
+ #
3
+ # Libusb for Ruby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # Libusb for Ruby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "minitest/autorun"
17
+ require "libusb"
18
+
19
+ class TestLibusbBos < Minitest::Test
20
+ include LIBUSB
21
+
22
+ attr_accessor :usb
23
+
24
+ def setup
25
+ @usb = Context.new
26
+ @usb.debug = 0
27
+ end
28
+
29
+ def test_bos
30
+ did_bos = false
31
+ did_cap = false
32
+ usb.devices.each do |dev|
33
+ # devices with USB version >= 0x201 could support bos
34
+ # devices with USB version >= 0x210 must support bos
35
+ if dev.bcdUSB >= 0x210
36
+ dev.open do |devh|
37
+ bos = devh.bos
38
+ did_bos = true
39
+
40
+ assert_equal 5, bos.bLength
41
+ assert_equal 0x0f, bos.bDescriptorType
42
+ assert_operator 5, :<=, bos.wTotalLength
43
+ assert_operator 0, :<=, bos.bNumDeviceCaps
44
+
45
+ caps = bos.device_capabilities
46
+ assert_equal bos.bNumDeviceCaps, caps.length
47
+ cap_types = bos.device_capability_types
48
+ assert_equal bos.bNumDeviceCaps, cap_types.length
49
+
50
+ cap_types.each do |cap_type|
51
+ assert_operator [
52
+ :BT_WIRELESS_USB_DEVICE_CAPABILITY,
53
+ :BT_USB_2_0_EXTENSION,
54
+ :BT_SS_USB_DEVICE_CAPABILITY,
55
+ :BT_CONTAINER_ID,
56
+ ], :include?, cap_type
57
+ end
58
+
59
+
60
+ caps.each do |cap|
61
+ did_cap = true
62
+
63
+ assert_operator 4, :<=, cap.bLength
64
+ assert_equal LIBUSB::DT_DEVICE_CAPABILITY, cap.bDescriptorType
65
+ assert_kind_of String, cap.dev_capability_data, "should provide binary capability data"
66
+ assert_kind_of String, cap.inspect, "should respond to inspect"
67
+ assert_operator 1, :<=, cap.dev_capability_data.length, "dev_capability_data should be at least one byte"
68
+
69
+ case cap
70
+ when Bos::DeviceCapability
71
+ assert_kind_of Integer, cap.bDevCapabilityType
72
+
73
+ when Bos::Usb20Extension
74
+ assert_equal 2, cap.bDevCapabilityType
75
+ assert_operator 0, :<=, cap.bmAttributes
76
+ assert_operator [true, false], :include?, cap.bm_lpm_support?
77
+
78
+ when Bos::SsUsbDeviceCapability
79
+ assert_equal 3, cap.bDevCapabilityType
80
+ assert_operator 0, :<=, cap.bmAttributes
81
+ assert_operator [true, false], :include?, cap.bm_ltm_support?
82
+ assert_operator 0, :<=, cap.wSpeedSupported
83
+ assert_kind_of Array, cap.supported_speeds
84
+ assert_operator 0, :<=, cap.bFunctionalitySupport
85
+ assert_operator 0, :<=, cap.bU1DevExitLat
86
+ assert_operator 0, :<=, cap.bU2DevExitLat
87
+
88
+ when Bos::ContainerId
89
+ assert_equal 4, cap.bDevCapabilityType
90
+ assert_operator 0, :<=, cap.bReserved
91
+ assert_operator 16, :==, cap.container_id.bytesize, "container_id should be 16 bytes long"
92
+
93
+ else
94
+ refute true, "invalid device capability class"
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ skip "no device with BOS available" unless did_bos
101
+ skip "no device with BOS capability available" unless did_cap
102
+ end
103
+
104
+ def test_no_bos
105
+ did_failing_bos = false
106
+
107
+ # devices with USB version < 0x201 shouldn't support bos
108
+ if dev=usb.devices.find{|dev| dev.bcdUSB < 0x201 }
109
+ dev.open do |devh|
110
+ assert_raises LIBUSB::ERROR_PIPE do
111
+ devh.bos
112
+ end
113
+ did_failing_bos = true
114
+ end
115
+ end
116
+ skip "no device without BOS available" unless did_failing_bos
117
+ end
118
+ end
@@ -0,0 +1,50 @@
1
+ # This file is part of Libusb for Ruby.
2
+ #
3
+ # Libusb for Ruby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # Libusb for Ruby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "minitest/autorun"
17
+ require "libusb"
18
+
19
+ class TestLibusbBulkStreamTransfer < Minitest::Test
20
+ include LIBUSB
21
+
22
+ def setup
23
+ c = Context.new
24
+ begin
25
+ @dev = c.devices.first.open
26
+ rescue LIBUSB::ERROR_ACCESS
27
+ @dev = nil
28
+ skip "error opening device"
29
+ end
30
+ end
31
+
32
+ def teardown
33
+ @dev.close if @dev
34
+ end
35
+
36
+ def test_alloc_streams
37
+ assert_raises(ERROR_NOT_SUPPORTED, "TODO: test with a OS that supports bulk streams and against a real device") do
38
+ nr_allocated = @dev.alloc_streams( 2, @dev.device.endpoints )
39
+ end
40
+
41
+ assert_raises(ERROR_NOT_SUPPORTED) do
42
+ @dev.free_streams( [0x01,0x82] )
43
+ end
44
+ end
45
+
46
+ def test_bulk_stream_transfer
47
+ tr = BulkStreamTransfer.new dev_handle: @dev, stream_id: 123, buffer: ' '*100
48
+ assert_equal 123, tr.stream_id, "stream_id should match"
49
+ end
50
+ end
@@ -0,0 +1,23 @@
1
+ # This file is part of Libusb for Ruby.
2
+ #
3
+ # Libusb for Ruby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # Libusb for Ruby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "minitest/autorun"
17
+ require "libusb"
18
+
19
+ class TestLibusbCapability < Minitest::Test
20
+ def test_version_parts
21
+ assert LIBUSB.has_capability?(:CAP_HAS_CAPABILITY)
22
+ end
23
+ end
@@ -0,0 +1,78 @@
1
+ # This file is part of Libusb for Ruby.
2
+ #
3
+ # Libusb for Ruby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # Libusb for Ruby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "minitest/autorun"
17
+ require "libusb/compat"
18
+
19
+ class TestLibusbCompat < Minitest::Test
20
+ include USB
21
+
22
+ attr_accessor :usb
23
+
24
+ def test_find
25
+ devlist = USB.devices
26
+ assert_kind_of Array, devlist, "Bus#find should return an Array"
27
+ assert_kind_of Device, devlist.first, "Bus#find should return Devices"
28
+ devlist.each do |dev|
29
+ assert_match( /Device/, dev.inspect, "Device#inspect should work")
30
+ end
31
+ end
32
+
33
+ def test_constants
34
+ assert_equal 7, USB_CLASS_PRINTER, "Printer class id should be defined"
35
+ assert_equal 32, USB_TYPE_CLASS, "type class should be defined"
36
+ end
37
+
38
+ def test_descriptors
39
+ USB.devices.each do |dev|
40
+ assert_match(/Device/, dev.inspect, "Device#inspect should work")
41
+ dev.configurations.each do |config_desc|
42
+ assert_match(/Configuration/, config_desc.inspect, "Configuration#inspect should work")
43
+ assert dev.configurations.include?(config_desc), "Device#configurations should include this one"
44
+
45
+ config_desc.interfaces.each do |interface|
46
+ assert_match(/Interface/, interface.inspect, "Interface#inspect should work")
47
+
48
+ assert dev.interfaces.include?(interface), "Device#interfaces should include this one"
49
+ assert config_desc.interfaces.include?(interface), "Configuration#interfaces should include this one"
50
+
51
+ interface.settings.each do |if_desc|
52
+ assert_match(/Setting/, if_desc.inspect, "Setting#inspect should work")
53
+
54
+ assert dev.settings.include?(if_desc), "Device#settings should include this one"
55
+ assert config_desc.settings.include?(if_desc), "Configuration#settings should include this one"
56
+ assert interface.settings.include?(if_desc), "Interface#settings should include this one"
57
+
58
+ if_desc.endpoints.each do |ep|
59
+ assert_match(/Endpoint/, ep.inspect, "Endpoint#inspect should work")
60
+
61
+ assert dev.endpoints.include?(ep), "Device#endpoints should include this one"
62
+ assert config_desc.endpoints.include?(ep), "Configuration#endpoints should include this one"
63
+ assert interface.endpoints.include?(ep), "Interface#endpoints should include this one"
64
+ assert if_desc.endpoints.include?(ep), "Setting#endpoints should include this one"
65
+
66
+ assert_equal if_desc, ep.setting, "backref should be correct"
67
+ assert_equal interface, ep.interface, "backref should be correct"
68
+ assert_equal config_desc, ep.configuration, "backref should be correct"
69
+ assert_equal dev, ep.device, "backref should be correct"
70
+
71
+ assert_operator 0, :<=, ep.wMaxPacketSize, "packet size should be > 0"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,81 @@
1
+ # This file is part of Libusb for Ruby.
2
+ #
3
+ # Libusb for Ruby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # Libusb for Ruby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "minitest/autorun"
17
+ require "libusb/compat"
18
+
19
+ class TestLibusbCompatMassStorage < Minitest::Test
20
+ include USB
21
+
22
+ attr_accessor :devh
23
+
24
+ BOMS_RESET = 0xFF
25
+ BOMS_GET_MAX_LUN = 0xFE
26
+
27
+ def setup
28
+ devs = []
29
+ USB.each_device_by_class(USB_CLASS_MASS_STORAGE, 0x01, 0x50){|dev| devs << dev }
30
+ USB.each_device_by_class(USB_CLASS_MASS_STORAGE, 0x06, 0x50){|dev| devs << dev }
31
+
32
+ dev = devs.last
33
+ skip "no mass storage device found" unless dev
34
+ devh = dev.open
35
+ if RUBY_PLATFORM=~/linux/i
36
+ data = " "*1000
37
+ begin
38
+ devh.usb_get_driver_np 0, data
39
+ rescue Errno::ENODATA
40
+ data = "nodata exception".ljust(1000, "\0")
41
+ end
42
+ assert_match(/\w+/, data, "There should be a driver or an exception")
43
+ begin
44
+ # second param is needed because of a bug in ruby-usb
45
+ devh.usb_detach_kernel_driver_np 0, 123
46
+ rescue RuntimeError => e
47
+ assert_match(/ERROR_NOT_FOUND/, e.to_s, "Raise proper exception, if no kernel driver is active")
48
+ end
49
+ end
50
+
51
+ endpoint_in = dev.endpoints.find{|ep| ep.bEndpointAddress&USB_ENDPOINT_IN != 0 }
52
+ endpoint_out = dev.endpoints.find{|ep| ep.bEndpointAddress&USB_ENDPOINT_IN == 0 }
53
+
54
+ devh.set_configuration 1
55
+ devh.set_configuration dev.configurations.first
56
+ devh.claim_interface dev.settings.first
57
+ devh.clear_halt(endpoint_in)
58
+ devh.clear_halt(endpoint_out.bEndpointAddress)
59
+ @devh = devh
60
+ end
61
+
62
+ def teardown
63
+ if devh
64
+ devh.release_interface 0
65
+ devh.usb_close
66
+ end
67
+ end
68
+
69
+ def test_mass_storage_reset
70
+ res = devh.usb_control_msg(USB_ENDPOINT_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
71
+ BOMS_RESET, 0, 0, "", 2000)
72
+ assert_equal 0, res, "BOMS_RESET response should be 0 byte"
73
+ end
74
+
75
+ def test_read_max_lun
76
+ bytes = [].pack('x1')
77
+ devh.usb_control_msg(USB_ENDPOINT_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
78
+ BOMS_GET_MAX_LUN, 0, 0, bytes, 100)
79
+ assert [0].pack("C")==bytes || [1].pack("C")==bytes, "BOMS_GET_MAX_LUN response is usually 0 or 1"
80
+ end
81
+ end
@@ -0,0 +1,212 @@
1
+ # This file is part of Libusb for Ruby.
2
+ #
3
+ # Libusb for Ruby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # Libusb for Ruby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require "minitest/autorun"
17
+ require "libusb"
18
+
19
+ class TestLibusbDescriptors < Minitest::Test
20
+ include LIBUSB
21
+
22
+ attr_accessor :usb
23
+
24
+ def setup
25
+ @usb = Context.new
26
+ @usb.debug = 0
27
+ end
28
+
29
+ def test_descriptors
30
+ usb.devices.each do |dev|
31
+ assert_match(/Device/, dev.inspect, "Device#inspect should work")
32
+
33
+ assert_kind_of Integer, dev.bLength
34
+ assert_equal 1, dev.bDescriptorType
35
+ assert_kind_of Integer, dev.bcdUSB
36
+ assert_kind_of Integer, dev.bDeviceClass
37
+ assert_kind_of Integer, dev.bDeviceSubClass
38
+ assert_kind_of Integer, dev.bDeviceProtocol
39
+ assert_kind_of Integer, dev.bMaxPacketSize0
40
+ assert_kind_of Integer, dev.idVendor
41
+ assert_kind_of Integer, dev.idProduct
42
+ assert_kind_of Integer, dev.bcdDevice
43
+ assert_kind_of Integer, dev.iManufacturer
44
+ assert_kind_of Integer, dev.iProduct
45
+ assert_kind_of Integer, dev.iSerialNumber
46
+ assert_kind_of Integer, dev.bNumConfigurations
47
+
48
+ dev.configurations.each do |config_desc|
49
+ assert_match(/Configuration/, config_desc.inspect, "ConfigDescriptor#inspect should work")
50
+ assert dev.configurations.include?(config_desc), "Device#configurations should include this one"
51
+
52
+ assert_kind_of Integer, config_desc.bLength
53
+ assert_equal 2, config_desc.bDescriptorType
54
+ assert_kind_of Integer, config_desc.wTotalLength
55
+ assert_equal config_desc.interfaces.length, config_desc.bNumInterfaces
56
+ assert_kind_of Integer, config_desc.bConfigurationValue
57
+ assert_kind_of Integer, config_desc.iConfiguration
58
+ assert_kind_of Integer, config_desc.bmAttributes
59
+ assert_kind_of Integer, config_desc.bMaxPower
60
+ assert_kind_of Integer, config_desc.maxPower # deprecated
61
+ assert_kind_of String, config_desc.extra if config_desc.extra
62
+
63
+ config_desc.interfaces.each do |interface|
64
+ assert_match(/Interface/, interface.inspect, "Interface#inspect should work")
65
+
66
+ assert dev.interfaces.include?(interface), "Device#interfaces should include this one"
67
+ assert config_desc.interfaces.include?(interface), "ConfigDescriptor#interfaces should include this one"
68
+
69
+ interface.alt_settings.each do |if_desc|
70
+ assert_match(/Setting/, if_desc.inspect, "InterfaceDescriptor#inspect should work")
71
+
72
+ assert dev.settings.include?(if_desc), "Device#settings should include this one"
73
+ assert config_desc.settings.include?(if_desc), "ConfigDescriptor#settings should include this one"
74
+ assert interface.alt_settings.include?(if_desc), "Inteerface#alt_settings should include this one"
75
+
76
+ assert_kind_of Integer, if_desc.bLength
77
+ assert_equal 4, if_desc.bDescriptorType
78
+ assert_kind_of Integer, if_desc.bInterfaceNumber
79
+ assert_kind_of Integer, if_desc.bAlternateSetting
80
+ assert_equal if_desc.endpoints.length, if_desc.bNumEndpoints
81
+ assert_kind_of Integer, if_desc.bInterfaceClass
82
+ assert_kind_of Integer, if_desc.bInterfaceSubClass
83
+ assert_kind_of Integer, if_desc.bInterfaceProtocol
84
+ assert_kind_of Integer, if_desc.iInterface
85
+ assert_kind_of String, if_desc.extra if if_desc.extra
86
+
87
+ if_desc.endpoints.each do |ep|
88
+ assert_match(/Endpoint/, ep.inspect, "EndpointDescriptor#inspect should work")
89
+
90
+ assert dev.endpoints.include?(ep), "Device#endpoints should include this one"
91
+ assert config_desc.endpoints.include?(ep), "ConfigDescriptor#endpoints should include this one"
92
+ assert interface.endpoints.include?(ep), "Inteerface#endpoints should include this one"
93
+ assert if_desc.endpoints.include?(ep), "InterfaceDescriptor#endpoints should include this one"
94
+
95
+ assert_equal if_desc, ep.setting, "backref should be correct"
96
+ assert_equal interface, ep.interface, "backref should be correct"
97
+ assert_equal config_desc, ep.configuration, "backref should be correct"
98
+ assert_equal dev, ep.device, "backref should be correct"
99
+
100
+ assert_kind_of Integer, ep.bLength
101
+ assert_equal 5, ep.bDescriptorType
102
+ assert_kind_of Integer, ep.bEndpointAddress
103
+ assert_kind_of Integer, ep.bmAttributes
104
+ assert_operator 0, :<=, ep.wMaxPacketSize, "packet size should be > 0"
105
+ assert_kind_of Integer, ep.bInterval
106
+ assert_kind_of Integer, ep.bRefresh
107
+ assert_kind_of Integer, ep.bSynchAddress
108
+ assert_kind_of String, ep.extra if ep.extra
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ def test_ss_companion
117
+ did_cc_companion = false
118
+ did_failing_cc_companion = false
119
+
120
+ usb.devices.each do |dev|
121
+ dev.endpoints.each do |ep|
122
+ if dev.device_speed == :SPEED_SUPER
123
+ ss = ep.ss_companion
124
+ assert_match(/SsCompanion/, ss.inspect, "SsCompanion#inspect should work")
125
+
126
+ assert_kind_of Integer, ss.bLength
127
+ assert_equal LIBUSB::DT_SS_ENDPOINT_COMPANION, ss.bDescriptorType
128
+ assert_kind_of Integer, ss.bMaxBurst
129
+ assert_kind_of Integer, ss.bmAttributes
130
+ assert_kind_of Integer, ss.wBytesPerInterval
131
+ did_cc_companion = true
132
+ elsif !did_failing_cc_companion
133
+ assert_raises ERROR_NOT_FOUND do
134
+ ep.ss_companion
135
+ end
136
+ did_failing_cc_companion = true
137
+ end
138
+ end
139
+ end
140
+
141
+ skip "no device with cc_companion available" unless did_cc_companion
142
+ skip "no device without cc_companion available" unless did_failing_cc_companion
143
+ end
144
+
145
+ def test_constants
146
+ assert_equal 7, CLASS_PRINTER, "Printer class id should be defined"
147
+ assert_equal 48, ISO_USAGE_TYPE_MASK, "iso usage type should be defined"
148
+ end
149
+
150
+ def test_device_filter_mass_storages
151
+ devs1 = []
152
+ usb.devices.each do |dev|
153
+ dev.settings.each do |if_desc|
154
+ if if_desc.bInterfaceClass == CLASS_MASS_STORAGE &&
155
+ ( if_desc.bInterfaceSubClass == 0x01 ||
156
+ if_desc.bInterfaceSubClass == 0x06 ) &&
157
+ if_desc.bInterfaceProtocol == 0x50
158
+
159
+ devs1 << dev
160
+ end
161
+ end
162
+ end
163
+
164
+ devs2 = usb.devices( bClass: CLASS_MASS_STORAGE, bSubClass: 0x01, bProtocol: 0x50 )
165
+ devs2 += usb.devices( bClass: CLASS_MASS_STORAGE, bSubClass: 0x06, bProtocol: 0x50 )
166
+ assert_equal devs1.sort, devs2.sort, "devices and devices with filter should deliver the same device"
167
+
168
+ devs3 = usb.devices( bClass: [CLASS_MASS_STORAGE], bSubClass: [0x01,0x06], bProtocol: [0x50] )
169
+ assert_equal devs1.sort, devs3.sort, "devices and devices with array-filter should deliver the same device"
170
+ end
171
+
172
+ def test_device_filter_hubs
173
+ devs1 = []
174
+ usb.devices.each do |dev|
175
+ dev.settings.each do |if_desc|
176
+ if if_desc.bInterfaceClass == CLASS_HUB
177
+ devs1 << dev
178
+ end
179
+ end
180
+ end
181
+
182
+ devs2 = usb.devices( bClass: CLASS_HUB )
183
+ assert_equal devs1.sort, devs2.sort, "devices and devices with filter should deliver the same device"
184
+ end
185
+
186
+ def test_device_methods
187
+ usb.devices.each do |dev|
188
+ ep = dev.endpoints.first
189
+ if ep
190
+ assert_operator dev.max_packet_size(ep), :>, 0, "#{dev.inspect} should have a usable packet size"
191
+ assert_operator dev.max_packet_size(ep.bEndpointAddress), :>, 0, "#{dev.inspect} should have a usable packet size"
192
+ assert_operator dev.max_iso_packet_size(ep), :>, 0, "#{dev.inspect} should have a usable iso packet size"
193
+ assert_operator dev.max_iso_packet_size(ep.bEndpointAddress), :>, 0, "#{dev.inspect} should have a usable iso packet size"
194
+ assert_operator dev.bus_number, :>=, 0, "#{dev.inspect} should have a bus_number"
195
+ assert_operator dev.device_address, :>=, 0, "#{dev.inspect} should have a device_address"
196
+ assert_operator([:SPEED_UNKNOWN, :SPEED_LOW, :SPEED_FULL, :SPEED_HIGH, :SPEED_SUPER], :include?, dev.device_speed, "#{dev.inspect} should have a device_speed")
197
+ path = dev.port_numbers
198
+ assert_kind_of Array, path, "#{dev.inspect} should have port_numbers"
199
+ path = dev.port_path
200
+ assert_kind_of Array, path, "#{dev.inspect} should have a port_path"
201
+ path.each do |port|
202
+ assert_operator port, :>, 0, "#{dev.inspect} should have proper port_path entries"
203
+ end
204
+ assert_equal path[-1], dev.port_number, "#{dev.inspect} should have a port number out of the port_path"
205
+ if parent=dev.parent
206
+ assert_kind_of Device, parent, "#{dev.inspect} should have a parent"
207
+ assert_equal path[-2], parent.port_number, "#{dev.inspect} should have a parent port number out of the port_path"
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end