simplify abstraction of pn531/pn533 and remove more duplicate code

This commit is contained in:
Adam Laurie 2009-12-08 21:06:59 +00:00
parent 673986835a
commit c15fc93494
4 changed files with 41 additions and 66 deletions

View file

@ -51,27 +51,10 @@ nfc_device_desc_t * pn531_usb_pick_device (void)
bool pn531_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound)
{
int idvendor = 0x04CC;
int idproduct = 0x0531;
int idvendor_alt = 0x054c;
int idproduct_alt = 0x0193;
// array of {vendor,product} pairs for USB devices
usb_candidate_t candidates[]= {{0x04CC,0x0531},{0x054c,0x0193}};
size_t firstpass = 0;
pn53x_usb_list_devices(&pnddDevices[0], szDevices, pszDeviceFound, idvendor, idproduct, PN531_USB_DRIVER_NAME);
if(*pszDeviceFound == szDevices)
{
DBG("Found %d devices",*pszDeviceFound);
return true;
}
firstpass= *pszDeviceFound;
pn53x_usb_list_devices(&pnddDevices[firstpass], szDevices - firstpass, pszDeviceFound, idvendor_alt, idproduct_alt, PN531_USB_DRIVER_NAME);
(*pszDeviceFound) += firstpass;
DBG("Found %d devices",*pszDeviceFound);
if(*pszDeviceFound)
return true;
return false;
return pn53x_usb_list_devices(&pnddDevices[0], szDevices, pszDeviceFound, &candidates[0], sizeof(candidates) / sizeof(usb_candidate_t),PN531_USB_DRIVER_NAME);
}
nfc_device_t* pn531_usb_connect(const nfc_device_desc_t* pndd)

View file

@ -51,27 +51,10 @@ nfc_device_desc_t * pn533_usb_pick_device (void)
bool pn533_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound)
{
int idvendor = 0x04cc;
int idproduct = 0x2533;
int idvendor_alt = 0x04e6;
int idproduct_alt = 0x5591;
// array of {vendor,product} pairs for USB devices
usb_candidate_t candidates[]= {{0x04CC,0x2533},{0x04E6,0x5591}};
size_t firstpass = 0;
pn53x_usb_list_devices(&pnddDevices[0], szDevices, pszDeviceFound, idvendor, idproduct, PN533_USB_DRIVER_NAME);
if(*pszDeviceFound == szDevices)
{
DBG("Found %d devices",*pszDeviceFound);
return true;
}
firstpass= *pszDeviceFound;
pn53x_usb_list_devices(&pnddDevices[firstpass], szDevices - firstpass, pszDeviceFound, idvendor_alt, idproduct_alt, PN533_USB_DRIVER_NAME);
(*pszDeviceFound) += firstpass;
DBG("Found %d devices",*pszDeviceFound);
if(*pszDeviceFound)
return true;
return false;
return pn53x_usb_list_devices(&pnddDevices[0], szDevices, pszDeviceFound, &candidates[0], sizeof(candidates) / sizeof(usb_candidate_t),PN533_USB_DRIVER_NAME);
}
nfc_device_t* pn533_usb_connect(const nfc_device_desc_t* pndd)

View file

@ -70,15 +70,14 @@ void get_end_points(struct usb_device *dev, usb_spec_t* pus)
}
}
bool pn53x_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound,int idvendor, int idproduct, char * target_name)
bool pn53x_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound,usb_candidate_t candidates[], int num_candidates, char * target_name)
{
int ret;
int ret, i;
struct usb_bus *bus;
struct usb_device *dev;
uint32_t uiBusIndex = 0;
DBG("Looking for %s device (%04x:%04x)",target_name,idvendor,idproduct);
usb_init();
if ((ret= usb_find_busses() < 0)) return NULL;
@ -92,30 +91,36 @@ bool pn53x_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, s
{
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++)
{
DBG("Checking device %04x:%04x",dev->descriptor.idVendor,dev->descriptor.idProduct);
if (idvendor==dev->descriptor.idVendor && idproduct==dev->descriptor.idProduct)
for(i = 0; i < num_candidates; ++i)
{
// Make sure there are 2 endpoints available
// with libusb-win32 we got some null pointers so be robust before looking at endpoints:
if (dev->config == NULL || dev->config->interface == NULL || dev->config->interface->altsetting == NULL)
DBG("Checking device %04x:%04x (%04x:%04x)",dev->descriptor.idVendor,dev->descriptor.idProduct,candidates[i].idVendor,candidates[i].idProduct);
if (candidates[i].idVendor==dev->descriptor.idVendor && candidates[i].idProduct==dev->descriptor.idProduct)
{
// Nope, we maybe want the next one, let's try to find another
continue;
// Make sure there are 2 endpoints available
// with libusb-win32 we got some null pointers so be robust before looking at endpoints:
if (dev->config == NULL || dev->config->interface == NULL || dev->config->interface->altsetting == NULL)
{
// Nope, we maybe want the next one, let's try to find another
continue;
}
if (dev->config->interface->altsetting->bNumEndpoints < 2)
{
// Nope, we maybe want the next one, let's try to find another
continue;
}
strcpy(pnddDevices[*pszDeviceFound].acDevice, target_name);
pnddDevices[*pszDeviceFound].pcDriver = target_name;
pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex;
(*pszDeviceFound)++;
DBG("%s","Match!");
// Test if we reach the maximum "wanted" devices
if((*pszDeviceFound) == szDevices)
{
DBG("Found %d devices",*pszDeviceFound);
return true;
}
}
if (dev->config->interface->altsetting->bNumEndpoints < 2)
{
// Nope, we maybe want the next one, let's try to find another
continue;
}
strcpy(pnddDevices[*pszDeviceFound].acDevice, target_name);
pnddDevices[*pszDeviceFound].pcDriver = target_name;
pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex;
(*pszDeviceFound)++;
DBG("%s","Match!");
// Test if we reach the maximum "wanted" devices
if((*pszDeviceFound) == szDevices) break;
}
if((*pszDeviceFound) == szDevices) break;
}
}
DBG("Found %d devices",*pszDeviceFound);
@ -126,8 +131,6 @@ bool pn53x_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, s
nfc_device_t* pn53x_usb_connect(const nfc_device_desc_t* pndd, char * target_name, int target_chip)
{
int ret;
nfc_device_t* pnd = NULL;
usb_spec_t* pus;
usb_spec_t us;
@ -193,6 +196,7 @@ nfc_device_t* pn53x_usb_connect(const nfc_device_desc_t* pndd, char * target_nam
}
}
// We ran out of devices before the index required
DBG("%s","Device index not found!");
return NULL;
}

View file

@ -33,8 +33,13 @@ typedef struct {
uint32_t uiEndPointOut;
} usb_spec_t;
typedef struct {
u_int16_t idVendor;
u_int16_t idProduct;
} usb_candidate_t;
nfc_device_t* pn53x_usb_connect(const nfc_device_desc_t* pndd, char * target_name, int target_chip);
void get_end_points(struct usb_device *dev, usb_spec_t* pus);
void pn53x_usb_disconnect(nfc_device_t* pnd);
bool pn53x_usb_transceive(const nfc_device_spec_t nds, const byte_t* pbtTx, const size_t szTxLen, byte_t* pbtRx, size_t* pszRxLen);
bool pn53x_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound,int idvendor, int idproduct, char * target_name);
bool pn53x_usb_list_devices(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound,usb_candidate_t candidates[], int num_candidates, char * target_name);