libusb 0.3.3-x64-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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 "test/unit"
17
+ require "libusb/compat"
18
+
19
+ class TestLibusbCompatMassStorage < Test::Unit::TestCase
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,181 @@
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 "test/unit"
17
+ require "libusb"
18
+
19
+ class TestLibusbDescriptors < Test::Unit::TestCase
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_constants
117
+ assert_equal 7, CLASS_PRINTER, "Printer class id should be defined"
118
+ assert_equal 48, ISO_USAGE_TYPE_MASK, "iso usage type should be defined"
119
+ end
120
+
121
+ def test_device_filter_mass_storages
122
+ devs1 = []
123
+ usb.devices.each do |dev|
124
+ dev.settings.each do |if_desc|
125
+ if if_desc.bInterfaceClass == CLASS_MASS_STORAGE &&
126
+ ( if_desc.bInterfaceSubClass == 0x01 ||
127
+ if_desc.bInterfaceSubClass == 0x06 ) &&
128
+ if_desc.bInterfaceProtocol == 0x50
129
+
130
+ devs1 << dev
131
+ end
132
+ end
133
+ end
134
+
135
+ devs2 = usb.devices( :bClass=>CLASS_MASS_STORAGE, :bSubClass=>0x01, :bProtocol=>0x50 )
136
+ devs2 += usb.devices( :bClass=>CLASS_MASS_STORAGE, :bSubClass=>0x06, :bProtocol=>0x50 )
137
+ assert_equal devs1.sort, devs2.sort, "devices and devices with filter should deliver the same device"
138
+
139
+ devs3 = usb.devices( :bClass=>[CLASS_MASS_STORAGE], :bSubClass=>[0x01,0x06], :bProtocol=>[0x50] )
140
+ assert_equal devs1.sort, devs3.sort, "devices and devices with array-filter should deliver the same device"
141
+ end
142
+
143
+ def test_device_filter_hubs
144
+ devs1 = []
145
+ usb.devices.each do |dev|
146
+ dev.settings.each do |if_desc|
147
+ if if_desc.bInterfaceClass == CLASS_HUB
148
+ devs1 << dev
149
+ end
150
+ end
151
+ end
152
+
153
+ devs2 = usb.devices( :bClass=>CLASS_HUB )
154
+ assert_equal devs1.sort, devs2.sort, "devices and devices with filter should deliver the same device"
155
+ end
156
+
157
+ def test_device_methods
158
+ usb.devices.each do |dev|
159
+ ep = dev.endpoints.first
160
+ if ep
161
+ assert_operator dev.max_packet_size(ep), :>, 0, "#{dev.inspect} should have a usable packet size"
162
+ assert_operator dev.max_packet_size(ep.bEndpointAddress), :>, 0, "#{dev.inspect} should have a usable packet size"
163
+ assert_operator dev.max_iso_packet_size(ep), :>, 0, "#{dev.inspect} should have a usable iso packet size"
164
+ assert_operator dev.max_iso_packet_size(ep.bEndpointAddress), :>, 0, "#{dev.inspect} should have a usable iso packet size"
165
+ assert_operator dev.bus_number, :>=, 0, "#{dev.inspect} should have a bus_number"
166
+ assert_operator dev.device_address, :>=, 0, "#{dev.inspect} should have a device_address"
167
+ assert_operator([:SPEED_UNKNOWN, :SPEED_LOW, :SPEED_FULL, :SPEED_HIGH, :SPEED_SUPER], :include?, dev.device_speed, "#{dev.inspect} should have a device_speed")
168
+ path = dev.port_path
169
+ assert_kind_of Array, path, "#{dev.inspect} should have a port_path"
170
+ path.each do |port|
171
+ assert_operator port, :>, 0, "#{dev.inspect} should have proper port_path entries"
172
+ end
173
+ assert_equal path[-1], dev.port_number, "#{dev.inspect} should have a port number out of the port_path"
174
+ if parent=dev.parent
175
+ assert_kind_of Device, parent, "#{dev.inspect} should have a parent"
176
+ assert_equal path[-2], parent.port_number, "#{dev.inspect} should have a parent port number out of the port_path"
177
+ end
178
+ end
179
+ end
180
+ end
181
+ 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 "test/unit"
17
+ require "libusb"
18
+ require "libusb/eventmachine"
19
+ require "eventmachine"
20
+
21
+ class TestLibusbEventMachine < Test::Unit::TestCase
22
+ include LIBUSB
23
+ BOMS_GET_MAX_LUN = 0xFE
24
+
25
+ attr_accessor :context
26
+ attr_accessor :device
27
+ attr_accessor :devh
28
+ attr_accessor :endpoint_in
29
+ attr_accessor :endpoint_out
30
+
31
+ def setup
32
+ @context = Context.new
33
+ @context.debug = 3
34
+
35
+ @device = context.devices( :bClass=>CLASS_MASS_STORAGE, :bSubClass=>[0x06,0x01], :bProtocol=>0x50 ).last
36
+ skip "no mass storage device found" unless @device
37
+
38
+ @endpoint_in = @device.endpoints.find{|ep| ep.bEndpointAddress&ENDPOINT_IN != 0 }
39
+ @endpoint_out = @device.endpoints.find{|ep| ep.bEndpointAddress&ENDPOINT_IN == 0 }
40
+ @devh = @device.open
41
+
42
+ if RUBY_PLATFORM=~/linux/i && devh.kernel_driver_active?(0)
43
+ devh.detach_kernel_driver(0)
44
+ end
45
+ devh.claim_interface(0)
46
+
47
+ # clear any pending data
48
+ devh.clear_halt(endpoint_in)
49
+ end
50
+
51
+ def teardown
52
+ end
53
+
54
+ def em_run
55
+ EventMachine.run do
56
+ @context.eventmachine_register
57
+
58
+ EventMachine.add_shutdown_hook do
59
+ @devh.release_interface(0) if @devh
60
+ @devh.close if @devh
61
+ @context.eventmachine_unregister
62
+ end
63
+
64
+ yield
65
+ end
66
+ end
67
+
68
+
69
+ def test_bulk_transfer
70
+ em_run do
71
+ ticks = 0
72
+ tr = devh.eventmachine_bulk_transfer(
73
+ :endpoint => @endpoint_in,
74
+ :timeout => 1500,
75
+ :dataIn => 123 )
76
+ # puts "started usb transfer #{tr}"
77
+
78
+ tr.callback do |data|
79
+ # puts "recved: #{data.inspect}"
80
+
81
+ assert false, "the bulk transfer shouldn't succeed"
82
+ EventMachine.stop
83
+ end
84
+ tr.errback do |text|
85
+ # puts "recv-err: #{text}"
86
+ assert true, "the bulk transfer should fail"
87
+
88
+ assert_operator ticks, :>=, 4
89
+ EventMachine.stop
90
+ end
91
+
92
+ EventMachine.add_periodic_timer(0.333) do
93
+ ticks += 1
94
+ end
95
+ end
96
+ end
97
+
98
+ def test_event_loop
99
+ em_run do
100
+ tr = devh.eventmachine_control_transfer(
101
+ :bmRequestType=>ENDPOINT_IN|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE,
102
+ :bRequest=>BOMS_GET_MAX_LUN,
103
+ :wValue=>0, :wIndex=>0, :dataIn=>1 )
104
+
105
+ # puts "started usb transfer #{tr}"
106
+ tr.callback do |data|
107
+ # puts "recved: #{data.inspect}"
108
+ assert true, "the control transfer should succeed"
109
+ EventMachine.stop
110
+ end
111
+ tr.errback do |text|
112
+ # puts "recv-err: #{text}"
113
+ assert false, "the control transfer shouldn't fail"
114
+ EventMachine.stop
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,37 @@
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
+ # These tests should be started with valgrind to check for
17
+ # invalid memmory access.
18
+
19
+ require "test/unit"
20
+ require "libusb"
21
+
22
+ class TestLibusbGc < Test::Unit::TestCase
23
+ include LIBUSB
24
+
25
+ def get_some_endpoint
26
+ Context.new.devices.each do |dev|
27
+ return dev.endpoints.last unless dev.endpoints.empty?
28
+ end
29
+ end
30
+
31
+ def test_descriptors
32
+ ep = get_some_endpoint
33
+ ps = ep.wMaxPacketSize
34
+ GC.start
35
+ assert_equal ps, ep.wMaxPacketSize, "GC should not free EndpointDescriptor"
36
+ end
37
+ 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 "test/unit"
17
+ require "libusb"
18
+
19
+ class TestLibusbIsoTransfer < Test::Unit::TestCase
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_iso_transfer
37
+ tr = IsochronousTransfer.new 10, :dev_handle=>@dev
38
+ assert_equal 10, tr.num_packets, "number of packets should match"
39
+
40
+ tr.buffer = " "*130
41
+ tr.packet_lengths = 13
42
+ tr[7].length = 12
43
+ assert_equal 12, tr[7].length, "packet length should be set"
44
+ assert_equal 13, tr[8].length, "packet length should be set"
45
+
46
+ assert_raise(LIBUSB::ERROR_IO, "the randomly choosen device will probably not handle iso transfer") do
47
+ tr.submit!
48
+ end
49
+ end
50
+ end