NO

Author Topic: Libraries !!  (Read 4741 times)

esby_2013

  • Guest
Libraries !!
« on: April 13, 2013, 11:33:31 PM »
New to C and struggling with my first task !

I was working on a v-usb (libusb) project from here :http://codeandlife.com/2012/01/29/avr-attiny-usb-tutorial-part-3/
I've done all the C programming to the AVR with AtmelStudio, and all good as far as I can tell !

Now I have to so the host-side coding to do....
The site  provides a usbtest.c file ... and from the libusb download, I have a 'usb.h' file, plus libusb.dll  (all codes pasted below).
The instructions then simply say 'make usbtest.exe' ...

I wasn't comfortable with MinGW, so thought I'd try Pelles.

As far as Pelles C goes, I like the interface and would like to get it to work for me.
So, I have :
> created a Console Project.
> made usbtest.c  under 'source files' with the code linked above.
> put usb.h into my project folder and it now appears in Pelles tree under 'include files'. (code pasetd below)

Yet I cannot for the life of me work out for the life of me how to link in the library.
and I asume this is why I am getting errors such as :

Building usbTest.obj.
Building VUSB_COnsole.exe.
POLINK: error: Unresolved external symbol '_usb_control_msg'.
POLINK: error: Unresolved external symbol '_usb_init'.
POLINK: error: Unresolved external symbol '_usb_find_busses'.
POLINK: error: Unresolved external symbol '_usb_find_devices'.
POLINK: error: Unresolved external symbol '_usb_get_busses'.
POLINK: error: Unresolved external symbol '_usb_open'.
POLINK: error: Unresolved external symbol '_usb_strerror'.
POLINK: error: Unresolved external symbol '_usb_close'.
POLINK: fatal error: 8 unresolved external(s).
*** Error code: 1 ***
Done.


I'm feeling foolish, all the code has been written for me ... yet I still can't work out how to put it all together and compile it !

Hope someone can kindly point me in the right direction ?


usbtest.c
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* this is libusb, see http://libusb.sourceforge.net/ */
#include "usb.h"

// Same as in main.c
#define USB_LED_OFF 0
#define USB_LED_ON  1

/* Used to get descriptor strings for device identification */
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
                                  char *buf, int buflen) {
    char buffer[256];
    int rval, i;

// make standard request GET_DESCRIPTOR, type string and given index
    // (e.g. dev->iProduct)
rval = usb_control_msg(dev,
        USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
        USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid,
        buffer, sizeof(buffer), 1000);
       
    if(rval < 0) // error
return rval;

    // rval should be bytes read, but buffer[0] contains the actual response size
if((unsigned char)buffer[0] < rval)
rval = (unsigned char)buffer[0]; // string is shorter than bytes read

if(buffer[1] != USB_DT_STRING) // second byte is the data type
return 0; // invalid return type

// we're dealing with UTF-16LE here so actual chars is half of rval,
// and index 0 doesn't count
rval /= 2;

/* lossy conversion to ISO Latin1 */
for(i = 1; i < rval && i < buflen; i++) {
if(buffer[2 * i + 1] == 0)
buf[i-1] = buffer[2 * i];
else
buf[i-1] = '?'; /* outside of ISO Latin1 range */
}
buf[i-1] = 0;

return i-1;
}

static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName,
                                      int product, char *productName) {
struct usb_bus *bus;
struct usb_device *dev;
char devVendor[256], devProduct[256];
   
usb_dev_handle * handle = NULL;

usb_init();
usb_find_busses();
usb_find_devices();

for(bus=usb_get_busses(); bus; bus=bus->next) {
for(dev=bus->devices; dev; dev=dev->next) {
if(dev->descriptor.idVendor != vendor ||
               dev->descriptor.idProduct != product)
                continue;
               
            /* we need to open the device in order to query strings */
            if(!(handle = usb_open(dev))) {
                fprintf(stderr, "Warning: cannot open USB device: %s\n",
                    usb_strerror());
                continue;
            }
           
            /* get vendor name */
            if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x0409, devVendor, sizeof(devVendor)) < 0) {
                fprintf(stderr,
                    "Warning: cannot query manufacturer for device: %s\n",
                    usb_strerror());
                usb_close(handle);
                continue;
            }
           
            /* get product name */
            if(usbGetDescriptorString(handle, dev->descriptor.iProduct,
               0x0409, devProduct, sizeof(devVendor)) < 0) {
                fprintf(stderr,
                    "Warning: cannot query product for device: %s\n",
                    usb_strerror());
                usb_close(handle);
                continue;
            }
           
            if(strcmp(devVendor, vendorName) == 0 &&
               strcmp(devProduct, productName) == 0)
                return handle;
            else
                usb_close(handle);
}
}

return NULL;
}

int main(int argc, char **argv) {
usb_dev_handle *handle = NULL;
    int nBytes = 0;
    char buffer[256];

if(argc < 2) {
printf("Usage:\n");
printf("usbtext.exe on\n");
printf("usbtext.exe off\n");
exit(1);
}

handle = usbOpenDevice(0x16C0, "esby", 0x05DC, "vUSB_Test");

if(handle == NULL) {
fprintf(stderr, "Could not find USB device!\n");
exit(1);
}

if(strcmp(argv[1], "on") == 0) {
nBytes = usb_control_msg(handle,
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
USB_LED_ON, 0, 0, (char *)buffer, sizeof(buffer), 5000);
} else if(strcmp(argv[1], "off") == 0) {
nBytes = usb_control_msg(handle,
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
USB_LED_OFF, 0, 0, (char *)buffer, sizeof(buffer), 5000);
}

if(nBytes < 0)
fprintf(stderr, "USB error: %s\n", usb_strerror());

usb_close(handle);

return 0;
}


usb.h
Code: [Select]
#ifndef __USB_H__
#define __USB_H__

#include <stdlib.h>
#include <windows.h>

/*
 * 'interface' is defined somewhere in the Windows header files. This macro
 * is deleted here to avoid conflicts and compile errors.
 */

#ifdef interface
#undef interface
#endif

/*
 * PATH_MAX from limits.h can't be used on Windows if the dll and
 * import libraries are build/used by different compilers
 */

#define LIBUSB_PATH_MAX 512


/*
 * USB spec information
 *
 * This is all stuff grabbed from various USB specs and is pretty much
 * not subject to change
 */

/*
 * Device and/or Interface Class codes
 */
#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
#define USB_CLASS_AUDIO       1
#define USB_CLASS_COMM       2
#define USB_CLASS_HID         3
#define USB_CLASS_PRINTER       7
#define USB_CLASS_MASS_STORAGE 8
#define USB_CLASS_HUB         9
#define USB_CLASS_DATA       10
#define USB_CLASS_VENDOR_SPEC   0xff

/*
 * Descriptor types
 */
#define USB_DT_DEVICE 0x01
#define USB_DT_CONFIG 0x02
#define USB_DT_STRING 0x03
#define USB_DT_INTERFACE 0x04
#define USB_DT_ENDPOINT 0x05

#define USB_DT_HID 0x21
#define USB_DT_REPORT 0x22
#define USB_DT_PHYSICAL 0x23
#define USB_DT_HUB 0x29

/*
 * Descriptor sizes per descriptor type
 */
#define USB_DT_DEVICE_SIZE 18
#define USB_DT_CONFIG_SIZE 9
#define USB_DT_INTERFACE_SIZE 9
#define USB_DT_ENDPOINT_SIZE 7
#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
#define USB_DT_HUB_NONVAR_SIZE 7


/* ensure byte-packed structures */
#include <pshpack1.h>


/* All standard descriptors have these 2 fields in common */
struct usb_descriptor_header
{
    unsigned char  bLength;
    unsigned char  bDescriptorType;
};

/* String descriptor */
struct usb_string_descriptor
{
    unsigned char  bLength;
    unsigned char  bDescriptorType;
    unsigned short wData[1];
};

/* HID descriptor */
struct usb_hid_descriptor
{
    unsigned char  bLength;
    unsigned char  bDescriptorType;
    unsigned short bcdHID;
    unsigned char  bCountryCode;
    unsigned char  bNumDescriptors;
};

/* Endpoint descriptor */
#define USB_MAXENDPOINTS 32
struct usb_endpoint_descriptor
{
    unsigned char  bLength;
    unsigned char  bDescriptorType;
    unsigned char  bEndpointAddress;
    unsigned char  bmAttributes;
    unsigned short wMaxPacketSize;
    unsigned char  bInterval;
    unsigned char  bRefresh;
    unsigned char  bSynchAddress;

    unsigned char *extra; /* Extra descriptors */
    int extralen;
};

#define USB_ENDPOINT_ADDRESS_MASK 0x0f    /* in bEndpointAddress */
#define USB_ENDPOINT_DIR_MASK   0x80

#define USB_ENDPOINT_TYPE_MASK 0x03    /* in bmAttributes */
#define USB_ENDPOINT_TYPE_CONTROL     0
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
#define USB_ENDPOINT_TYPE_BULK     2
#define USB_ENDPOINT_TYPE_INTERRUPT   3

/* Interface descriptor */
#define USB_MAXINTERFACES 32
struct usb_interface_descriptor
{
    unsigned char  bLength;
    unsigned char  bDescriptorType;
    unsigned char  bInterfaceNumber;
    unsigned char  bAlternateSetting;
    unsigned char  bNumEndpoints;
    unsigned char  bInterfaceClass;
    unsigned char  bInterfaceSubClass;
    unsigned char  bInterfaceProtocol;
    unsigned char  iInterface;

    struct usb_endpoint_descriptor *endpoint;

    unsigned char *extra; /* Extra descriptors */
    int extralen;
};

#define USB_MAXALTSETTING 128 /* Hard limit */

struct usb_interface
{
    struct usb_interface_descriptor *altsetting;

    int num_altsetting;
};

/* Configuration descriptor information.. */
#define USB_MAXCONFIG 8
struct usb_config_descriptor
{
    unsigned char  bLength;
    unsigned char  bDescriptorType;
    unsigned short wTotalLength;
    unsigned char  bNumInterfaces;
    unsigned char  bConfigurationValue;
    unsigned char  iConfiguration;
    unsigned char  bmAttributes;
    unsigned char  MaxPower;

    struct usb_interface *interface;

    unsigned char *extra; /* Extra descriptors */
    int extralen;
};

/* Device descriptor */
struct usb_device_descriptor
{
    unsigned char  bLength;
    unsigned char  bDescriptorType;
    unsigned short bcdUSB;
    unsigned char  bDeviceClass;
    unsigned char  bDeviceSubClass;
    unsigned char  bDeviceProtocol;
    unsigned char  bMaxPacketSize0;
    unsigned short idVendor;
    unsigned short idProduct;
    unsigned short bcdDevice;
    unsigned char  iManufacturer;
    unsigned char  iProduct;
    unsigned char  iSerialNumber;
    unsigned char  bNumConfigurations;
};

struct usb_ctrl_setup
{
    unsigned char  bRequestType;
    unsigned char  bRequest;
    unsigned short wValue;
    unsigned short wIndex;
    unsigned short wLength;
};

/*
 * Standard requests
 */
#define USB_REQ_GET_STATUS     0x00
#define USB_REQ_CLEAR_FEATURE     0x01
/* 0x02 is reserved */
#define USB_REQ_SET_FEATURE     0x03
/* 0x04 is reserved */
#define USB_REQ_SET_ADDRESS     0x05
#define USB_REQ_GET_DESCRIPTOR 0x06
#define USB_REQ_SET_DESCRIPTOR 0x07
#define USB_REQ_GET_CONFIGURATION 0x08
#define USB_REQ_SET_CONFIGURATION 0x09
#define USB_REQ_GET_INTERFACE   0x0A
#define USB_REQ_SET_INTERFACE   0x0B
#define USB_REQ_SYNCH_FRAME     0x0C

#define USB_TYPE_STANDARD (0x00 << 5)
#define USB_TYPE_CLASS (0x01 << 5)
#define USB_TYPE_VENDOR (0x02 << 5)
#define USB_TYPE_RESERVED (0x03 << 5)

#define USB_RECIP_DEVICE 0x00
#define USB_RECIP_INTERFACE 0x01
#define USB_RECIP_ENDPOINT 0x02
#define USB_RECIP_OTHER 0x03

/*
 * Various libusb API related stuff
 */

#define USB_ENDPOINT_IN 0x80
#define USB_ENDPOINT_OUT 0x00

/* Error codes */
#define USB_ERROR_BEGIN 500000

/*
 * This is supposed to look weird. This file is generated from autoconf
 * and I didn't want to make this too complicated.
 */
#define USB_LE16_TO_CPU(x)

/*
 * Device reset types for usb_reset_ex.
 * http://msdn.microsoft.com/en-us/library/ff537269%28VS.85%29.aspx
 * http://msdn.microsoft.com/en-us/library/ff537243%28v=vs.85%29.aspx
 */
#define USB_RESET_TYPE_RESET_PORT (1 << 0)
#define USB_RESET_TYPE_CYCLE_PORT (1 << 1)
#define USB_RESET_TYPE_FULL_RESET (USB_RESET_TYPE_CYCLE_PORT | USB_RESET_TYPE_RESET_PORT)


/* Data types */
/* struct usb_device; */
/* struct usb_bus; */

struct usb_device
{
    struct usb_device *next, *prev;

    char filename[LIBUSB_PATH_MAX];

    struct usb_bus *bus;

    struct usb_device_descriptor descriptor;
    struct usb_config_descriptor *config;

    void *dev; /* Darwin support */

    unsigned char devnum;

    unsigned char num_children;
    struct usb_device **children;
};

struct usb_bus
{
    struct usb_bus *next, *prev;

    char dirname[LIBUSB_PATH_MAX];

    struct usb_device *devices;
    unsigned long location;

    struct usb_device *root_dev;
};

/* Version information, Windows specific */
struct usb_version
{
    struct
    {
        int major;
        int minor;
        int micro;
        int nano;
    } dll;
    struct
    {
        int major;
        int minor;
        int micro;
        int nano;
    } driver;
};


struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;

/* Variables */
#ifndef __USB_C__
#define usb_busses usb_get_busses()
#endif



#include <poppack.h>


#ifdef __cplusplus
extern "C"
{
#endif

    /* Function prototypes */

    /* usb.c */
    usb_dev_handle *usb_open(struct usb_device *dev);
    int usb_close(usb_dev_handle *dev);
    int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
                       size_t buflen);
    int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
                              size_t buflen);

    /* descriptors.c */
    int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep,
                                       unsigned char type, unsigned char index,
                                       void *buf, int size);
    int usb_get_descriptor(usb_dev_handle *udev, unsigned char type,
                           unsigned char index, void *buf, int size);

    /* <arch>.c */
    int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
                       int timeout);
    int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
                      int timeout);
    int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
                            int timeout);
    int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
                           int timeout);
    int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
                        int value, int index, char *bytes, int size,
                        int timeout);
    int usb_set_configuration(usb_dev_handle *dev, int configuration);
    int usb_claim_interface(usb_dev_handle *dev, int interface);
    int usb_release_interface(usb_dev_handle *dev, int interface);
    int usb_set_altinterface(usb_dev_handle *dev, int alternate);
    int usb_resetep(usb_dev_handle *dev, unsigned int ep);
    int usb_clear_halt(usb_dev_handle *dev, unsigned int ep);
    int usb_reset(usb_dev_handle *dev);
    int usb_reset_ex(usb_dev_handle *dev, unsigned int reset_type);

    char *usb_strerror(void);

    void usb_init(void);
    void usb_set_debug(int level);
    int usb_find_busses(void);
    int usb_find_devices(void);
    struct usb_device *usb_device(usb_dev_handle *dev);
    struct usb_bus *usb_get_busses(void);


    /* Windows specific functions */

#define LIBUSB_HAS_INSTALL_SERVICE_NP 1
    int usb_install_service_np(void);
    void CALLBACK usb_install_service_np_rundll(HWND wnd, HINSTANCE instance,
            LPSTR cmd_line, int cmd_show);

#define LIBUSB_HAS_UNINSTALL_SERVICE_NP 1
    int usb_uninstall_service_np(void);
    void CALLBACK usb_uninstall_service_np_rundll(HWND wnd, HINSTANCE instance,
            LPSTR cmd_line, int cmd_show);

#define LIBUSB_HAS_INSTALL_DRIVER_NP 1
    int usb_install_driver_np(const char *inf_file);
    void CALLBACK usb_install_driver_np_rundll(HWND wnd, HINSTANCE instance,
            LPSTR cmd_line, int cmd_show);

#define LIBUSB_HAS_TOUCH_INF_FILE_NP 1
    int usb_touch_inf_file_np(const char *inf_file);
    void CALLBACK usb_touch_inf_file_np_rundll(HWND wnd, HINSTANCE instance,
            LPSTR cmd_line, int cmd_show);

#define LIBUSB_HAS_INSTALL_NEEDS_RESTART_NP 1
    int usb_install_needs_restart_np(void);

#define LIBUSB_HAS_INSTALL_NP 1
    int usb_install_npW(HWND hwnd, HINSTANCE instance, LPCWSTR cmd_line, int starg_arg);
    int usb_install_npA(HWND hwnd, HINSTANCE instance, LPCSTR cmd_line, int starg_arg);
#define usb_install_np usb_install_npA
    void CALLBACK usb_install_np_rundll(HWND wnd, HINSTANCE instance,
            LPSTR cmd_line, int cmd_show);

    const struct usb_version *usb_get_version(void);

    int usb_isochronous_setup_async(usb_dev_handle *dev, void **context,
                                    unsigned char ep, int pktsize);
    int usb_bulk_setup_async(usb_dev_handle *dev, void **context,
                             unsigned char ep);
    int usb_interrupt_setup_async(usb_dev_handle *dev, void **context,
                                  unsigned char ep);

    int usb_submit_async(void *context, char *bytes, int size);
    int usb_reap_async(void *context, int timeout);
    int usb_reap_async_nocancel(void *context, int timeout);
    int usb_cancel_async(void *context);
    int usb_free_async(void **context);


#ifdef __cplusplus
}
#endif

#endif /* __USB_H__ */



Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Libraries !!
« Reply #1 on: April 14, 2013, 07:05:46 AM »
Code: [Select]
...
#define WIN32_LEAN_AND_MEAN
#include "usb.h"

#pragma comment(lib, "libusb.lib")
...
or insert libusb.lib to Project -> Project options... -> Linker -> Library and object files:
May the source be with you

esby_2013

  • Guest
Re: Libraries !!
« Reply #2 on: April 14, 2013, 02:50:35 PM »
Thanks for that.

So, I did :
 > Project -> Project options... -> Linker -> Library and object files
 > added libusb.lib

Next build reported that libusb.lib could not be found.
> So I copied libusb.lib file into my project directory
Next build obviously then found it because it returned to reporting the previous errors.

I looked at the first error: POLINK: error: Unresolved external symbol '_usb_control_msg'.
I see that 'usb_control_msg' (no leading underscore) is called in usbTest.c
Looking in usb.h, I can see :
Code: [Select]
int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
                        int value, int index, char *bytes, int size,
                        int timeout);

... so that's where 'usb_control_msg'  is defined.

I must be missing the relevance of why the error report is putting an '_' before the fiunction name ...
... I am sure that's my next clue to understanding the error.



Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Libraries !!
« Reply #3 on: April 17, 2013, 10:06:53 PM »
Select Undecorated exported functions in the project options compiler tab.
---
Stefan

Proud member of the UltraDefrag Development Team