Hey

I'm having a bit of problems with shifting my code to libusb1.0

I'm not sure of the best way to send my old or new code so I'm just copying and pasting it, I apologise for the messyness :S

Old code (for libusb-0.1) works fine:
#include <stdio.h>
#include <usb.h>
#include <signal.h>
#include <string.h>
#include <iostream>

#define INTERFACE 1
#define ENDPOINT 0x82

static struct usb_device * findKeyboard(uint16_t vendor, uint16_t product)
{
    struct usb_bus *bus;
    struct usb_device *dev;
    struct usb_bus *busses;
    usb_init();
    usb_find_busses();
    usb_find_devices();
   
    busses = usb_get_busses();
   
    for (bus = busses; bus; bus=bus->next)
        for (dev = bus->devices; dev; dev = dev->next)
            if ((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
             return dev;
    return NULL;
}

int main (int argc,char **argv)
{
    char bytes[8];
    struct usb_device * dev;
    struct usb_dev_handle *dev_handle;
    if (argc < 18)
    {
        printf("Too few arguments\n");
        return 1;
    }
   
    dev = findKeyboard(0x1038,0x0100);
    if (dev == NULL) {
        printf("Device not found\n");
        return 1;
    }
    dev_handle = usb_open(dev); //open usb device
    if (!dev_handle) //if we didnt get a device handle
    {
        printf("Failed to access usb device\n");
        return 1;
    }
    if (usb_detach_kernel_driver_np(dev_handle,INTERFACE))
        printf("Failed to disabled kernel driver. Driver is possibly already disabled.\n");
    if (usb_claim_interface(dev_handle,INTERFACE)) //if we cannot claim interface
    {
        printf("Could not connect to interface\n");
        return 1;
    }
    //start reading keys
    while(1)
    {
        usb_interrupt_read(dev_handle, ENDPOINT, bytes, 0x0008, 0);
        for (int i = 0; i<8; i++)
            printf("Bytes%i: %i ", i, bytes[i]);
        printf("\n"); 
    }
    usb_release_interface(dev_handle,INTERFACE);
    usb_close(dev_handle);
    return 0;
}
This code produces nice output showing me the values of all the keysignals its receiving.

New code (for libusb-1.0) doesn't work:
#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>
#include <iostream>

#define VENDOR_ID 0x1038
#define PRODUCT_ID 0x0100
#define INTERFACE 1
#define ENDPOINT 0x82

int main (int argc, char **argv)
{
    unsigned char buf[8];
    int *length = NULL;
   
    libusb_init(NULL);
    libusb_set_debug(NULL, 3);
    libusb_device_handle *zboard = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID);

    if (!zboard)
    {
        fprintf(stderr, "Could not find Zboard.\n");
        exit(-1);
    }
    if (libusb_kernel_driver_active (zboard, INTERFACE))
    {
        if (libusb_detach_kernel_driver(zboard,INTERFACE))
        {
            fprintf(stderr, "Failed to free interface.\n");
            exit(-1);
        }
    }
   
    if (libusb_claim_interface(zboard,INTERFACE))
    {
        fprintf(stderr, "Failed to claim interface.\n");
        exit(-1);
    }
    libusb_clear_halt (zboard,ENDPOINT);
    while(1)
    {
        int err = libusb_interrupt_transfer (zboard, ENDPOINT, buf, 8, length, 0);
        printf("error: %i", err);
        for (int i = 0; i<8; i++)
            printf("Bytes%i: %i ", i, buf[i]);
        printf("\n");
    }   
    libusb_release_interface(zboard,INTERFACE);
    libusb_attach_kernel_driver(zboard,INTERFACE);
    libusb_close(zboard);
    libusb_exit(NULL);
    return 0;
}

I know the while loop that never ends is a problem, but i'll fix that after i have it working XP.
Basically the code appears to work until it gets into the while loop then, as soon as I receive a signal from the device (its a keyboard), I get a segmentation fault. The other thing that occurs (just running the program again a few times) is that the consol keeps writing new empty lines, then when I press a key (on the interface im getting signals from) it takes me back to the command prompt without any output. I have tried removing the for and printf's in the while loop, and it still segfaults, so the problem is in how I have used the libusb_interrupt_transfer but I have no idea what I am doing wrong, could someone please explain? :D

Thx


Find someone to light your fire this winter at Match.co.nz Brrr... its getting cold out there…