| |   |
| 1 | /* |
| 2 | usb.c - libusb interface for Ruby. |
| 3 | |
| 4 | Copyright (C) 2007 Tanaka Akira |
| 5 | |
| 6 | This library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | This library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with this library; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include "ruby.h" |
| 22 | #include "st.h" |
| 23 | #include <usb.h> |
| 24 | #include <errno.h> |
| 25 | |
| 26 | #ifndef RSTRING_PTR |
| 27 | # define RSTRING_PTR(s) (RSTRING(s)->ptr) |
| 28 | # define RSTRING_LEN(s) (RSTRING(s)->len) |
| 29 | #endif |
| 30 | |
| 31 | static VALUE rb_cUSB; |
| 32 | |
| 33 | static VALUE rusb_dev_handle_new(usb_dev_handle *h); |
| 34 | |
| 35 | #define define_usb_struct(c_name, ruby_name) \ |
| 36 | static VALUE rb_cUSB_ ## ruby_name; \ |
| 37 | static st_table *c_name ## _objects; \ |
| 38 | typedef struct { struct usb_ ## c_name *ptr; VALUE parent; } rusb_ ## c_name ## _t; \ |
| 39 | static void rusb_ ## c_name ## _free(void *p) { \ |
| 40 | if (p) free(p); \ |
| 41 | } \ |
| 42 | static VALUE rusb_ ## c_name ## _make(struct usb_ ## c_name *p, VALUE parent) \ |
| 43 | { \ |
| 44 | VALUE v; \ |
| 45 | rusb_ ## c_name ## _t *d; \ |
| 46 | if (p == NULL) { return Qnil; } \ |
| 47 | if (st_lookup(c_name ## _objects, (st_data_t)p, (st_data_t *)&v)) \ |
| 48 | return v; \ |
| 49 | d = (rusb_ ## c_name ## _t *)xmalloc(sizeof(*d)); \ |
| 50 | d->ptr = p; \ |
| 51 | d->parent = parent; \ |
| 52 | v = Data_Wrap_Struct(rb_cUSB_ ## ruby_name, 0, rusb_ ## c_name ## _free, d); \ |
| 53 | st_add_direct(c_name ## _objects, (st_data_t)p, (st_data_t)v); \ |
| 54 | return v; \ |
| 55 | } \ |
| 56 | static rusb_ ## c_name ## _t *check_usb_ ## c_name(VALUE v) \ |
| 57 | { \ |
| 58 | Check_Type(v, T_DATA); \ |
| 59 | if (RDATA(v)->dfree != rusb_ ## c_name ## _free) { \ |
| 60 | rb_raise(rb_eTypeError, "wrong argument type %s (expected USB::" #ruby_name ")", \ |
| 61 | rb_class2name(CLASS_OF(v))); \ |
| 62 | } \ |
| 63 | return DATA_PTR(v); \ |
| 64 | } \ |
| 65 | static rusb_ ## c_name ## _t *get_rusb_ ## c_name(VALUE v) \ |
| 66 | { \ |
| 67 | rusb_ ## c_name ## _t *d = check_usb_ ## c_name(v); \ |
| 68 | if (!d) { \ |
| 69 | rb_raise(rb_eArgError, "revoked USB::" #ruby_name); \ |
| 70 | } \ |
| 71 | return d; \ |
| 72 | } \ |
| 73 | static struct usb_ ## c_name *get_usb_ ## c_name(VALUE v) \ |
| 74 | { \ |
| 75 | return get_rusb_ ## c_name(v)->ptr; \ |
| 76 | } \ |
| 77 | static VALUE get_usb_ ## c_name ## _parent(VALUE v) \ |
| 78 | { \ |
| 79 | return get_rusb_ ## c_name(v)->parent; \ |
| 80 | } |
| 81 | |
| 82 | define_usb_struct(bus, Bus) |
| 83 | define_usb_struct(device, Device) |
| 84 | define_usb_struct(config_descriptor, Configuration) |
| 85 | define_usb_struct(interface, Interface) |
| 86 | define_usb_struct(interface_descriptor, Setting) |
| 87 | define_usb_struct(endpoint_descriptor, Endpoint) |
| 88 | |
| 89 | static int mark_data_i(st_data_t key, st_data_t val, st_data_t arg) |
| 90 | { |
| 91 | if (DATA_PTR((VALUE)val)) |
| 92 | rb_gc_mark((VALUE)val); |
| 93 | return ST_CONTINUE; |
| 94 | } |
| 95 | |
| 96 | VALUE rusb_gc_root; |
| 97 | static void rusb_gc_mark(void *p) { |
| 98 | st_foreach(bus_objects, mark_data_i, 0); |
| 99 | st_foreach(device_objects, mark_data_i, 0); |
| 100 | st_foreach(config_descriptor_objects, mark_data_i, 0); |
| 101 | st_foreach(interface_objects, mark_data_i, 0); |
| 102 | st_foreach(interface_descriptor_objects, mark_data_i, 0); |
| 103 | st_foreach(endpoint_descriptor_objects, mark_data_i, 0); |
| 104 | } |
| 105 | |
| 106 | /* -------- USB::Bus -------- */ |
| 107 | static int revoke_data_i(st_data_t key, st_data_t val, st_data_t arg) |
| 108 | { |
| 109 | DATA_PTR((VALUE)val) = NULL; |
| 110 | return ST_DELETE; |
| 111 | } |
| 112 | |
| 113 | /* USB.find_busses */ |
| 114 | static VALUE |
| 115 | rusb_find_busses(VALUE cUSB) |
| 116 | { |
| 117 | st_foreach(bus_objects, revoke_data_i, 0); |
| 118 | st_foreach(device_objects, revoke_data_i, 0); |
| 119 | st_foreach(config_descriptor_objects, revoke_data_i, 0); |
| 120 | st_foreach(interface_objects, revoke_data_i, 0); |
| 121 | st_foreach(interface_descriptor_objects, revoke_data_i, 0); |
| 122 | st_foreach(endpoint_descriptor_objects, revoke_data_i, 0); |
| 123 | return INT2NUM(usb_find_busses()); |
| 124 | } |
| 125 | |
| 126 | /* USB.find_devices */ |
| 127 | static VALUE |
| 128 | rusb_find_devices(VALUE cUSB) |
| 129 | { |
| 130 | return INT2NUM(usb_find_devices()); |
| 131 | } |
| 132 | |
| 133 | /* USB.first_bus */ |
| 134 | static VALUE |
| 135 | rusb_first_bus(VALUE cUSB) |
| 136 | { |
| 137 | struct usb_bus *bus = usb_get_busses(); |
| 138 | return rusb_bus_make(bus, Qnil); |
| 139 | } |
| 140 | |
| 141 | /* USB::Bus#revoked? */ |
| 142 | static VALUE |
| 143 | rusb_bus_revoked_p(VALUE v) |
| 144 | { |
| 145 | return RTEST(!check_usb_bus(v)); |
| 146 | } |
| 147 | |
| 148 | /* USB::Bus#prev */ |
| 149 | static VALUE rusb_bus_prev(VALUE v) { return rusb_bus_make(get_usb_bus(v)->prev, Qnil); } |
| 150 | |
| 151 | /* USB::Bus#next */ |
| 152 | static VALUE rusb_bus_next(VALUE v) { return rusb_bus_make(get_usb_bus(v)->next, Qnil); } |
| 153 | |
| 154 | /* USB::Bus#dirname */ |
| 155 | static VALUE rusb_bus_dirname(VALUE v) { return rb_str_new2(get_usb_bus(v)->dirname); } |
| 156 | |
| 157 | /* USB::Bus#location */ |
| 158 | static VALUE rusb_bus_location(VALUE v) { return UINT2NUM(get_usb_bus(v)->location); } |
| 159 | |
| 160 | /* USB::Bus#first_device */ |
| 161 | static VALUE rusb_bus_first_device(VALUE v) { return rusb_device_make(get_usb_bus(v)->devices, v); } |
| 162 | |
| 163 | /* -------- USB::Device -------- */ |
| 164 | |
| 165 | /* USB::Bus#revoked? */ |
| 166 | static VALUE |
| 167 | rusb_device_revoked_p(VALUE v) |
| 168 | { |
| 169 | return RTEST(!check_usb_device(v)); |
| 170 | } |
| 171 | |
| 172 | /* USB::Device#prev */ |
| 173 | static VALUE rusb_device_prev(VALUE v) { rusb_device_t *device = get_rusb_device(v); return rusb_device_make(device->ptr->prev, device->parent); } |
| 174 | |
| 175 | /* USB::Device#next */ |
| 176 | static VALUE rusb_device_next(VALUE v) { rusb_device_t *device = get_rusb_device(v); return rusb_device_make(device->ptr->next, device->parent); } |
| 177 | |
| 178 | /* USB::Device#filename */ |
| 179 | static VALUE rusb_device_filename(VALUE v) { return rb_str_new2(get_usb_device(v)->filename); } |
| 180 | |
| 181 | /* USB::Device#bus */ |
| 182 | static VALUE rusb_device_bus(VALUE v) { return rusb_bus_make(get_usb_device(v)->bus, Qnil); } |
| 183 | |
| 184 | /* USB::Device#devnum */ |
| 185 | static VALUE rusb_device_devnum(VALUE v) { return INT2FIX(get_usb_device(v)->devnum); } |
| 186 | |
| 187 | /* USB::Device#num_children */ |
| 188 | static VALUE rusb_device_num_children(VALUE v) { return INT2FIX(get_usb_device(v)->num_children); } |
| 189 | |
| 190 | /* USB::Device#children */ |
| 191 | static VALUE |
| 192 | rusb_device_children(VALUE vdevice) |
| 193 | { |
| 194 | rusb_device_t *d = get_rusb_device(vdevice); |
| 195 | struct usb_device *device = d->ptr; |
| 196 | int i; |
| 197 | VALUE children = rb_ary_new2(device->num_children); |
| 198 | for (i = 0; i < device->num_children; i++) |
| 199 | rb_ary_store(children, i, rusb_device_make(device->children[i], d->parent)); |
| 200 | return children; |
| 201 | } |
| 202 | |
| 203 | /* USB::Device#descriptor_bLength */ |
| 204 | static VALUE rusb_devdesc_bLength(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bLength); } |
| 205 | |
| 206 | /* USB::Device#descriptor_bDescriptorType */ |
| 207 | static VALUE rusb_devdesc_bDescriptorType(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bDescriptorType); } |
| 208 | |
| 209 | /* USB::Device#descriptor_bcdUSB */ |
| 210 | static VALUE rusb_devdesc_bcdUSB(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bcdUSB); } |
| 211 | |
| 212 | /* USB::Device#descriptor_bDeviceClass */ |
| 213 | static VALUE rusb_devdesc_bDeviceClass(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bDeviceClass); } |
| 214 | |
| 215 | /* USB::Device#descriptor_bDeviceSubClass */ |
| 216 | static VALUE rusb_devdesc_bDeviceSubClass(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bDeviceSubClass); } |
| 217 | |
| 218 | /* USB::Device#descriptor_bDeviceProtocol */ |
| 219 | static VALUE rusb_devdesc_bDeviceProtocol(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bDeviceProtocol); } |
| 220 | |
| 221 | /* USB::Device#descriptor_bMaxPacketSize0 */ |
| 222 | static VALUE rusb_devdesc_bMaxPacketSize0(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bMaxPacketSize0); } |
| 223 | |
| 224 | /* USB::Device#descriptor_idVendor */ |
| 225 | static VALUE rusb_devdesc_idVendor(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.idVendor); } |
| 226 | |
| 227 | /* USB::Device#descriptor_idProduct */ |
| 228 | static VALUE rusb_devdesc_idProduct(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.idProduct); } |
| 229 | |
| 230 | /* USB::Device#descriptor_bcdDevice */ |
| 231 | static VALUE rusb_devdesc_bcdDevice(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bcdDevice); } |
| 232 | |
| 233 | /* USB::Device#descriptor_iManufacturer */ |
| 234 | static VALUE rusb_devdesc_iManufacturer(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.iManufacturer); } |
| 235 | |
| 236 | /* USB::Device#descriptor_iProduct */ |
| 237 | static VALUE rusb_devdesc_iProduct(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.iProduct); } |
| 238 | |
| 239 | /* USB::Device#descriptor_iSerialNumber */ |
| 240 | static VALUE rusb_devdesc_iSerialNumber(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.iSerialNumber); } |
| 241 | |
| 242 | /* USB::Device#descriptor_bNumConfigurations */ |
| 243 | static VALUE rusb_devdesc_bNumConfigurations(VALUE v) { return INT2FIX(get_usb_device(v)->descriptor.bNumConfigurations); } |
| 244 | |
| 245 | /* USB::Device#configurations */ |
| 246 | static VALUE |
| 247 | rusb_device_config(VALUE v) |
| 248 | { |
| 249 | struct usb_device *device = get_usb_device(v); |
| 250 | int i; |
| 251 | VALUE children = rb_ary_new2(device->descriptor.bNumConfigurations); |
| 252 | for (i = 0; i < device->descriptor.bNumConfigurations; i++) |
| 253 | rb_ary_store(children, i, rusb_config_descriptor_make(&device->config[i], v)); |
| 254 | return children; |
| 255 | } |
| 256 | |
| 257 | /* USB::Device#usb_open */ |
| 258 | static VALUE |
| 259 | rusb_device_open(VALUE vdevice) |
| 260 | { |
| 261 | struct usb_device *device = get_usb_device(vdevice); |
| 262 | usb_dev_handle *h = usb_open(device); |
| 263 | return rusb_dev_handle_new(h); |
| 264 | } |
| 265 | |
| 266 | /* -------- USB::Configuration -------- */ |
| 267 | |
| 268 | /* USB::Configuration#revoked? */ |
| 269 | static VALUE |
| 270 | rusb_config_revoked_p(VALUE v) |
| 271 | { |
| 272 | return RTEST(!check_usb_config_descriptor(v)); |
| 273 | } |
| 274 | |
| 275 | /* USB::Configuration#device */ |
| 276 | static VALUE rusb_config_device(VALUE v) { return get_rusb_config_descriptor(v)->parent; } |
| 277 | |
| 278 | /* USB::Configuration#bLength */ |
| 279 | static VALUE rusb_config_bLength(VALUE v) { return INT2FIX(get_usb_config_descriptor(v)->bLength); } |
| 280 | |
| 281 | /* USB::Configuration#bDescriptorType */ |
| 282 | static VALUE rusb_config_bDescriptorType(VALUE v) { return INT2FIX(get_usb_config_descriptor(v)->bDescriptorType); } |
| 283 | |
| 284 | /* USB::Configuration#wTotalLength */ |
| 285 | static VALUE rusb_config_wTotalLength(VALUE v) { return INT2FIX(get_usb_config_descriptor(v)->wTotalLength); } |
| 286 | |
| 287 | /* USB::Configuration#bNumInterfaces */ |
| 288 | static VALUE rusb_config_bNumInterfaces(VALUE v) { return INT2FIX(get_usb_config_descriptor(v)->bNumInterfaces); } |
| 289 | |
| 290 | /* USB::Configuration#bConfigurationValue */ |
| 291 | static VALUE rusb_config_bConfigurationValue(VALUE v) { return INT2FIX(get_usb_config_descriptor(v)->bConfigurationValue); } |
| 292 | |
| 293 | /* USB::Configuration#iConfiguration */ |
| 294 | static VALUE rusb_config_iConfiguration(VALUE v) { return INT2FIX(get_usb_config_descriptor(v)->iConfiguration); } |
| 295 | |
| 296 | /* USB::Configuration#bmAttributes */ |
| 297 | static VALUE rusb_config_bmAttributes(VALUE v) { return INT2FIX(get_usb_config_descriptor(v)->bmAttributes); } |
| 298 | |
| 299 | /* USB::Configuration#bMaxPower */ |
| 300 | static VALUE rusb_config_bMaxPower(VALUE v) { return INT2FIX(get_usb_config_descriptor(v)->MaxPower); } |
| 301 | |
| 302 | /* USB::Configuration#interfaces */ |
| 303 | static VALUE |
| 304 | rusb_config_interfaces(VALUE v) |
| 305 | { |
| 306 | struct usb_config_descriptor *p = get_usb_config_descriptor(v); |
| 307 | int i; |
| 308 | VALUE interface = rb_ary_new2(p->bNumInterfaces); |
| 309 | for (i = 0; i < p->bNumInterfaces; i++) |
| 310 | rb_ary_store(interface, i, rusb_interface_make(&p->interface[i], v)); |
| 311 | return interface; |
| 312 | } |
| 313 | |
| 314 | /* -------- USB::Interface -------- */ |
| 315 | |
| 316 | /* USB::Interface#revoked? */ |
| 317 | static VALUE |
| 318 | rusb_interface_revoked_p(VALUE v) |
| 319 | { |
| 320 | return RTEST(!check_usb_interface(v)); |
| 321 | } |
| 322 | |
| 323 | /* USB::Interface#configuration */ |
| 324 | static VALUE rusb_interface_configuration(VALUE v) { return get_rusb_interface(v)->parent; } |
| 325 | |
| 326 | /* USB::Interface#num_altsetting */ |
| 327 | static VALUE rusb_interface_num_altsetting(VALUE v) { return INT2FIX(get_usb_interface(v)->num_altsetting); } |
| 328 | |
| 329 | /* USB::Interface#settings */ |
| 330 | static VALUE |
| 331 | rusb_interface_settings(VALUE v) |
| 332 | { |
| 333 | struct usb_interface *p = get_usb_interface(v); |
| 334 | int i; |
| 335 | VALUE altsetting = rb_ary_new2(p->num_altsetting); |
| 336 | for (i = 0; i < p->num_altsetting; i++) |
| 337 | rb_ary_store(altsetting, i, rusb_interface_descriptor_make(&p->altsetting[i], v)); |
| 338 | return altsetting; |
| 339 | } |
| 340 | |
| 341 | /* -------- USB::Setting -------- */ |
| 342 | |
| 343 | /* USB::Setting#revoked? */ |
| 344 | static VALUE |
| 345 | rusb_setting_revoked_p(VALUE v) |
| 346 | { |
| 347 | return RTEST(!check_usb_interface_descriptor(v)); |
| 348 | } |
| 349 | |
| 350 | /* USB::Interface#interface */ |
| 351 | static VALUE rusb_setting_interface(VALUE v) { return get_rusb_interface_descriptor(v)->parent; } |
| 352 | |
| 353 | /* USB::Setting#bLength */ |
| 354 | static VALUE rusb_setting_bLength(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->bLength); } |
| 355 | |
| 356 | /* USB::Setting#bDescriptorType */ |
| 357 | static VALUE rusb_setting_bDescriptorType(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->bDescriptorType); } |
| 358 | |
| 359 | /* USB::Setting#bInterfaceNumber */ |
| 360 | static VALUE rusb_setting_bInterfaceNumber(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->bInterfaceNumber); } |
| 361 | |
| 362 | /* USB::Setting#bAlternateSetting */ |
| 363 | static VALUE rusb_setting_bAlternateSetting(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->bAlternateSetting); } |
| 364 | |
| 365 | /* USB::Setting#bNumEndpoints */ |
| 366 | static VALUE rusb_setting_bNumEndpoints(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->bNumEndpoints); } |
| 367 | |
| 368 | /* USB::Setting#bInterfaceClass */ |
| 369 | static VALUE rusb_setting_bInterfaceClass(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->bInterfaceClass); } |
| 370 | |
| 371 | /* USB::Setting#bInterfaceSubClass */ |
| 372 | static VALUE rusb_setting_bInterfaceSubClass(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->bInterfaceSubClass); } |
| 373 | |
| 374 | /* USB::Setting#bInterfaceProtocol */ |
| 375 | static VALUE rusb_setting_bInterfaceProtocol(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->bInterfaceProtocol); } |
| 376 | |
| 377 | /* USB::Setting#iInterface */ |
| 378 | static VALUE rusb_setting_iInterface(VALUE v) { return INT2FIX(get_usb_interface_descriptor(v)->iInterface); } |
| 379 | |
| 380 | /* USB::Setting#endpoints */ |
| 381 | static VALUE |
| 382 | rusb_setting_endpoints(VALUE v) |
| 383 | { |
| 384 | struct usb_interface_descriptor *p = get_usb_interface_descriptor(v); |
| 385 | int i; |
| 386 | VALUE endpoint = rb_ary_new2(p->bNumEndpoints); |
| 387 | for (i = 0; i < p->bNumEndpoints; i++) |
| 388 | rb_ary_store(endpoint, i, rusb_endpoint_descriptor_make(&p->endpoint[i], v)); |
| 389 | return endpoint; |
| 390 | } |
| 391 | |
| 392 | /* -------- USB::Endpoint -------- */ |
| 393 | |
| 394 | /* USB::Endpoint#revoked? */ |
| 395 | static VALUE |
| 396 | rusb_endpoint_revoked_p(VALUE v) |
| 397 | { |
| 398 | return RTEST(!check_usb_endpoint_descriptor(v)); |
| 399 | } |
| 400 | |
| 401 | /* USB::Endpoint#setting */ |
| 402 | static VALUE rusb_endpoint_setting(VALUE v) { return get_rusb_endpoint_descriptor(v)->parent; } |
| 403 | |
| 404 | /* USB::Endpoint#bLength */ |
| 405 | static VALUE rusb_endpoint_bLength(VALUE v) { return INT2FIX(get_usb_endpoint_descriptor(v)->bLength); } |
| 406 | |
| 407 | /* USB::Endpoint#bDescriptorType */ |
| 408 | static VALUE rusb_endpoint_bDescriptorType(VALUE v) { return INT2FIX(get_usb_endpoint_descriptor(v)->bDescriptorType); } |
| 409 | |
| 410 | /* USB::Endpoint#bEndpointAddress */ |
| 411 | static VALUE rusb_endpoint_bEndpointAddress(VALUE v) { return INT2FIX(get_usb_endpoint_descriptor(v)->bEndpointAddress); } |
| 412 | |
| 413 | /* USB::Endpoint#bmAttributes */ |
| 414 | static VALUE rusb_endpoint_bmAttributes(VALUE v) { return INT2FIX(get_usb_endpoint_descriptor(v)->bmAttributes); } |
| 415 | |
| 416 | /* USB::Endpoint#wMaxPacketSize */ |
| 417 | static VALUE rusb_endpoint_wMaxPacketSize(VALUE v) { return INT2FIX(get_usb_endpoint_descriptor(v)->wMaxPacketSize); } |
| 418 | |
| 419 | /* USB::Endpoint#bInterval */ |
| 420 | static VALUE rusb_endpoint_bInterval(VALUE v) { return INT2FIX(get_usb_endpoint_descriptor(v)->bInterval); } |
| 421 | |
| 422 | /* USB::Endpoint#bRefresh */ |
| 423 | static VALUE rusb_endpoint_bRefresh(VALUE v) { return INT2FIX(get_usb_endpoint_descriptor(v)->bRefresh); } |
| 424 | |
| 425 | /* USB::Endpoint#bSynchAddress */ |
| 426 | static VALUE rusb_endpoint_bSynchAddress(VALUE v) { return INT2FIX(get_usb_endpoint_descriptor(v)->bSynchAddress); } |
| 427 | |
| 428 | /* -------- USB::DevHandle -------- */ |
| 429 | |
| 430 | static VALUE rb_cUSB_DevHandle; |
| 431 | |
| 432 | void rusb_devhandle_free(void *_h) |
| 433 | { |
| 434 | usb_dev_handle *h = (usb_dev_handle *)_h; |
| 435 | if (h) usb_close(h); |
| 436 | } |
| 437 | |
| 438 | static VALUE |
| 439 | rusb_dev_handle_new(usb_dev_handle *h) |
| 440 | { |
| 441 | return Data_Wrap_Struct(rb_cUSB_DevHandle, 0, rusb_devhandle_free, h); |
| 442 | } |
| 443 | |
| 444 | static usb_dev_handle *check_usb_devhandle(VALUE v) |
| 445 | { |
| 446 | Check_Type(v, T_DATA); |
| 447 | if (RDATA(v)->dfree != rusb_devhandle_free) { |
| 448 | rb_raise(rb_eTypeError, "wrong argument type %s (expected USB::DevHandle)", |
| 449 | rb_class2name(CLASS_OF(v))); |
| 450 | } |
| 451 | return DATA_PTR(v); |
| 452 | } |
| 453 | |
| 454 | static usb_dev_handle *get_usb_devhandle(VALUE v) |
| 455 | { |
| 456 | usb_dev_handle *p = check_usb_devhandle(v); \ |
| 457 | if (!p) { |
| 458 | rb_raise(rb_eArgError, "closed USB::DevHandle"); |
| 459 | } |
| 460 | return p; |
| 461 | } |
| 462 | |
| 463 | static int check_usb_error(char *reason, int ret) |
| 464 | { |
| 465 | if (ret < 0) { |
| 466 | errno = -ret; |
| 467 | rb_sys_fail(reason); |
| 468 | } |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | /* USB::DevHandle#usb_close */ |
| 473 | static VALUE |
| 474 | rusb_close(VALUE v) |
| 475 | { |
| 476 | usb_dev_handle *p = get_usb_devhandle(v); |
| 477 | check_usb_error("usb_close", usb_close(p)); |
| 478 | DATA_PTR(v) = NULL; |
| 479 | return Qnil; |
| 480 | } |
| 481 | |
| 482 | /* USB::DevHandle#usb_set_configuration(configuration) */ |
| 483 | static VALUE |
| 484 | rusb_set_configuration(VALUE v, VALUE configuration) |
| 485 | { |
| 486 | usb_dev_handle *p = get_usb_devhandle(v); |
| 487 | int ret = usb_set_configuration(p, NUM2INT(configuration)); |
| 488 | check_usb_error("usb_set_configuration", ret); |
| 489 | return Qnil; |
| 490 | } |
| 491 | |
| 492 | /* USB::DevHandle#usb_set_altinterface(alternate) */ |
| 493 | static VALUE |
| 494 | rusb_set_altinterface(VALUE v, VALUE alternate) |
| 495 | { |
| 496 | usb_dev_handle *p = get_usb_devhandle(v); |
| 497 | int ret = usb_set_altinterface(p, NUM2INT(alternate)); |
| 498 | check_usb_error("usb_set_altinterface", ret); |
| 499 | return Qnil; |
| 500 | } |
| 501 | |
| 502 | /* USB::DevHandle#usb_clear_halt(endpoint) */ |
| 503 | static VALUE |
| 504 | rusb_clear_halt(VALUE v, VALUE ep) |
| 505 | { |
| 506 | usb_dev_handle *p = get_usb_devhandle(v); |
| 507 | int ret = usb_clear_halt(p, NUM2UINT(ep)); |
| 508 | check_usb_error("usb_clear_halt", ret); |
| 509 | return Qnil; |
| 510 | } |
| 511 | |
| 512 | /* USB::DevHandle#usb_reset */ |
| 513 | static VALUE |
| 514 | rusb_reset(VALUE v) |
| 515 | { |
| 516 | usb_dev_handle *p = get_usb_devhandle(v); |
| 517 | int ret = usb_reset(p); |
| 518 | check_usb_error("usb_reset", ret); |
| 519 | /* xxx: call usb_close? */ |
| 520< |