Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <err.h>
- #include <error.h>
- #include <errno.h>
- #include <usb.h>
- #include <math.h>
- #define USB_DEBUG_VERBOSE 3 // Complete log
- #define CLAIM_INTERFACE_RETRY_ON_NODEV_MAX 3
- #define VID 0x0DD4 /* Custom receipt printer Vendor ID */
- #define PID 0x01A8 /* Custom receipt printer Product ID */
- #define BOUT_EP 0x02 /* Custom receipt printer bulk-out end point */
- #define BOUT_EPMAXPKTSIZE 64 /* Custom receipt printer bulk-out end point maximum packet size in bytes */
- #define BIN_EP 0x81 /* Custom receipt printer bulk-in end point */
- #define BIN_EPMAXPKTSIZE 64 /* Custom receipt printer bulk-in end point maximum packet size in bytes */
- #define DEBUG_LOG 1
- struct usb_dev_handle
- {
- int fd;
- struct usb_bus *bus;
- struct usb_device *device;
- int config;
- int interface;
- int altsetting;
- void *impl_info;
- };
- typedef struct usb_device usb_dev;
- static usb_dev *device_custom;
- void debug_log(char *, int t);
- usb_dev_handle * USBOpenDevice(usb_dev *device, usb_dev_handle *devHandlePtr);
- int USBCloseDeviceHandle(usb_dev_handle *devHandlePtr);
- int USBSetActiveConfiguration(usb_dev *device, usb_dev_handle *devHandlePtr);
- int USBClaimInterface(usb_dev *device, usb_dev_handle *devHandlePtr);
- int USBReleaseInterface(usb_dev *device, usb_dev_handle *devHandlePtr);
- int USBClearHaltOnEP(usb_dev_handle *devHandlePtr, const uint8_t ep);
- int USBBulkWriteOnEP(usb_dev_handle *devHandlePtr, unsigned char *txBuffer, unsigned int numBytesToSend, const uint8_t ep, const unsigned int epPktSize);
- int USBBulkReadOnEP(usb_dev_handle *devHandlePtr, unsigned char *rxBuffer, unsigned int numOfBytesToRx, const uint8_t ep, const unsigned int epPktSize);
- int writeToUSB(usb_dev *device, usb_dev_handle *devHandlePtr, unsigned char *txBuffer, unsigned int numBytesToSend, const uint8_t ep, const unsigned int epPktSize);
- int readFromUSB(usb_dev *device, usb_dev_handle *devHandlePtr, unsigned char *rxBuffer, unsigned int numOfBytesToRx, const uint8_t ep, const unsigned int epPktSize);
- usb_dev *devUsbExist(unsigned int vendorId, unsigned int productId, usb_dev *devHandle, uint8_t numDev);
- typedef enum
- {
- DEV_READY,
- DEV_COMM_ERROR,
- DEV_HEAD_UP,
- DEV_NO_PAPER,
- DEV_PAPER_JAM,
- DEV_CUTTER_ERROR,
- DEV_PAPER_INALIGN,
- DEV_BM_ERROR,
- DEV_NOT_ENABLED,
- DEV_LOWPAPER,
- DEV_PAPER_FULL,
- DEV_NO_HOME,
- DEV_REV_MOTOR_ERR,
- DEV_PRINTER_BUSY,
- DEV_PRESENT_BLOCK,
- DEV_EJECT_BLOCK,
- DEV_PRINT_HEAD_OVERHEAT,
- DEV_VOLTAGE_EXCEEDED,
- DEV_FLASH_ERROR,
- DEV_RP, // Used for identification of Nippon receipt and journal printers
- DEV_JP // Used for identification of Nippon receipt and journal printers
- }printer_status_t;
- static usb_dev *device_custom;
- static usb_dev_handle *devHandlePtr_custom;
- #define BIN_EP 0x81
- /*
- Sequence to use libusb-0.1 API s
- 1. Initialise USB
- 2. Find busses
- 3. Find devices
- 4. Get busses
- 5. Iterate through busses to find a required device
- 6. Open the device
- 7. Set the active configuration(when multiple number of interface)
- 8. Set the alternate setting(optional)
- 9. Claim the interface
- 10. Do your processing
- 11. Release the interface
- 12. close the device
- */
- void debug_log(char *data,int level)
- {
- printf("%s\n",data);
- }
- usb_dev *devUsbExist(unsigned int vendorId, unsigned int productId, usb_dev *device, uint8_t numDev)
- {
- struct usb_bus *bussesPtr;
- struct usb_bus *bus;
- int c = 0, i = 0, a = 0;
- char usbDevice = 0;
- //char charBuf[128] = {'\0'};
- static unsigned int vid = 0, pid = 0;
- static int temp = 0;
- int gotoNextDev = 0;
- usb_init();
- usb_find_busses();
- usb_find_devices();
- bussesPtr = usb_get_busses();
- for(bus = bussesPtr; bus; bus = bus->next)
- {
- for(device = bus->devices; device; device = device->next, gotoNextDev = 0)
- {
- //memset(charBuf, '\0', sizeof(charBuf));
- //sprintf(charBuf, "bdeviceClass=%2d\n", device->descriptor.bDeviceClass);
- //debug_log(charBuf, DEBUG_LOG);
- /* Loop through all of the configurations */
- for(c = 0; (gotoNextDev != 1) && (c < device->descriptor.bNumConfigurations); c++)
- {
- /* Loop through all of the interfaces */
- for(i = 0; (gotoNextDev != 1) && (i < device->config[c].bNumInterfaces); i++)
- {
- /* Loop through all of the alternate settings */
- for(a = 0; (gotoNextDev != 1) && (a < device->config[c].interface[i].num_altsetting); a++)
- {
- //memset(charBuf, '\0', sizeof(charBuf));
- //sprintf(charBuf, "bdeviceClass=%2d, bInterfaceClass=%3d", device->descriptor.bDeviceClass, device->config[c].interface[i].altsetting[a].bInterfaceClass);
- //debug_log(charBuf, DEBUG_LOG);
- if((device->descriptor.idVendor == vendorId) && (device->descriptor.idProduct == productId))
- {
- if((vendorId != vid) && (productId != pid))
- temp = 1;
- else
- temp++;
- if(temp == 1)
- {
- //memset(charBuf, '\0', sizeof(charBuf));
- //sprintf(charBuf, "Device Exists ! device=%p", device);
- //debug_log(charBuf, DEBUG_LOG);
- }
- vid = vendorId;
- pid = productId;
- usbDevice = 1;
- // debug_log(" devUsbExist: Stop", DEBUG_LOG);
- if(--numDev == 0)
- return device;
- else
- gotoNextDev = 1;
- }
- }
- }
- }
- }
- }
- if(usbDevice == 0)
- device = NULL;
- debug_log(" devUsbExist: Stop", DEBUG_LOG);
- return device;
- }
- int8_t printer_exist_custom(uint8_t numDev)
- {
- int8_t ret = 0;
- debug_log("printer_exist_custom", DEBUG_LOG);
- device_custom = devUsbExist(VID, PID, device_custom, numDev);
- ret = (device_custom != NULL) ? DEV_READY : DEV_COMM_ERROR;
- return ret;
- }
- int8_t printer_openUSBport_custom(void)
- {
- int ret = 0;
- debug_log("printer_openUSBport_ Start", DEBUG_LOG);
- devHandlePtr_custom = USBOpenDevice(device_custom, devHandlePtr_custom);
- ret = (devHandlePtr_custom != NULL) ? DEV_READY : DEV_COMM_ERROR;
- debug_log("printer_openUSBport_ Stop", DEBUG_LOG);
- return ret;
- }
- int8_t printer_closeUSBport_custom(void)
- {
- int ret = 0;
- debug_log("printer_closeUSBport_ Start", DEBUG_LOG);
- ret = USBCloseDeviceHandle(devHandlePtr_custom);
- if(ret == 0)
- {
- device_custom = NULL;
- devHandlePtr_custom = NULL;
- ret = DEV_READY;
- }
- else
- ret = DEV_COMM_ERROR;
- debug_log("printer_closeUSBport_ Stop", DEBUG_LOG);
- return ret;
- }
- int8_t printer_tx_custom(unsigned char *cmd, unsigned int numBytesToTx)
- {
- int numBytesTx = 0;
- int8_t status = 0;
- numBytesTx = writeToUSB(device_custom, devHandlePtr_custom, &cmd[0], numBytesToTx, BOUT_EP, BOUT_EPMAXPKTSIZE);
- if(numBytesToTx == numBytesTx)
- {
- debug_log("Send Success !", DEBUG_LOG);
- status = DEV_READY;
- }
- else
- {
- numBytesTx = writeToUSB(device_custom, devHandlePtr_custom, &cmd[0], numBytesToTx, BOUT_EP, BOUT_EPMAXPKTSIZE);
- if(numBytesToTx == numBytesTx)
- {
- debug_log("Send Success !", DEBUG_LOG);
- status = DEV_READY;
- }
- else
- {
- debug_log("Send Failed !!", DEBUG_LOG);
- status = DEV_COMM_ERROR;
- }
- }
- return status;
- }
- int8_t printer_rx_custom(unsigned char *rxBuf, unsigned int numBytesToRx)
- {
- int numBytesRx = 0;
- int8_t status = 0;
- int i = 0;
- char charBuf[128] = {'\0'};
- char tempBuf[8] = {'\0'};
- char logBuf[128] = {'\0'};
- numBytesRx = readFromUSB(device_custom, devHandlePtr_custom, &rxBuf[0], numBytesToRx, BIN_EP, BIN_EPMAXPKTSIZE);
- if(numBytesToRx == numBytesRx)
- {
- memset(&charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Receive Success. Data(hex):");
- for(i = 0; i < numBytesRx; i++)
- {
- sprintf(tempBuf, " %02x", rxBuf[i]);
- strcat(logBuf, tempBuf);
- }
- strcat(charBuf, logBuf);
- debug_log(charBuf, DEBUG_LOG);
- status = DEV_READY;
- }
- else
- {
- numBytesRx = readFromUSB(device_custom, devHandlePtr_custom, &rxBuf[0], numBytesToRx, BIN_EP, BIN_EPMAXPKTSIZE);
- if(numBytesToRx == numBytesRx)
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Receive Success. Data(hex):");
- for(i = 0; i < numBytesRx; i++)
- {
- sprintf(tempBuf, " %02x", rxBuf[i]);
- strcat(logBuf, tempBuf);
- }
- strcat(charBuf, logBuf);
- debug_log(charBuf, DEBUG_LOG);
- status = DEV_READY;
- }
- else
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Receive Failed !! ToRX=%d, RX=%d ", numBytesToRx, numBytesRx);
- debug_log(charBuf, DEBUG_LOG);
- status = DEV_COMM_ERROR;
- }
- }
- return status;
- }
- int8_t printer_fwversion_custom(int type)
- {
- unsigned char fwCmd[] = { 0x1D, 0x49, 0x03 };
- unsigned char response[5];
- float version = 0.0;
- int numBytesToTx = 0;
- int8_t status = 0;
- numBytesToTx = sizeof(fwCmd);
- status = printer_tx_custom(&fwCmd[0], numBytesToTx);
- if(status == DEV_READY)
- {
- memset(response,0,sizeof(response));
- status = printer_rx_custom(&response[0], 4);
- if(status == DEV_READY)
- {
- version = (response[0]-0x30) + atoi((char *)&response[2]) / pow(10,strlen((char *)&response[2]));
- printf("\n\n FW_VERSION: %f \n", version);
- }
- }
- return status;
- }
- int8_t printer_openIfExist_custom(void)
- {
- int8_t ret = 0;
- //debug_log("printer_openIfExist_custom", DEBUG_LOG);
- printer_closeUSBport_custom();
- ret = printer_exist_custom(1);
- if(ret == DEV_READY)
- ret = printer_openUSBport_custom();
- return ret;
- }
- int8_t printer_handleExist_custom(void)
- {
- return (device_custom != NULL) ? DEV_READY : printer_openIfExist_custom();
- }
- int8_t printer_powerInit_custom()
- {
- //if(printer_handleExist_custom() == DEV_READY)
- if(printer_exist_custom(1) == DEV_READY)
- {
- devHandlePtr_custom = usb_open(device_custom);
- if(devHandlePtr_custom != NULL)
- {
- if( usb_set_configuration(devHandlePtr_custom,device_custom->config->bConfigurationValue)<0 )
- {
- if( usb_detach_kernel_driver_np(devHandlePtr_custom,device_custom->config->interface->altsetting->bInterfaceNumber)<0 )
- {
- fprintf(stderr,"usb_set_configuration Error.\n");
- fprintf(stderr,"usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror());
- }
- }
- if( usb_claim_interface(devHandlePtr_custom,device_custom->config->interface->altsetting->bInterfaceNumber)<0 )
- {
- if( usb_detach_kernel_driver_np(devHandlePtr_custom,device_custom->config->interface->altsetting->bInterfaceNumber)<0 )
- {
- fprintf(stderr,"usb_claim_interface Error.\n");
- fprintf(stderr,"usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror());
- }
- }
- if( usb_claim_interface(devHandlePtr_custom,device_custom->config->interface->altsetting->bInterfaceNumber)<0 )
- {
- fprintf(stderr,"usb_claim_interface Error.(%s)\n",usb_strerror());
- }
- }
- else
- printf("\n usb_open failed!! ");
- printer_closeUSBport_custom();
- }
- else
- {
- printf("\n ERROR IN POWER INIT");
- return 1;
- }
- return 0;
- }
- usb_dev_handle * USBOpenDevice(usb_dev *device, usb_dev_handle *devHandlePtr)
- {
- if(!device)
- {
- debug_log(" Device not found !!", DEBUG_LOG);
- devHandlePtr = NULL;
- }
- else
- {
- // open the device
- devHandlePtr = usb_open(device);
- if(devHandlePtr == NULL)
- {
- debug_log(" Device open failed !!", DEBUG_LOG);
- devHandlePtr = NULL;
- }
- else
- {
- }
- }
- return devHandlePtr;
- }
- int USBCloseDeviceHandle(usb_dev_handle *devHandlePtr)
- {
- int ret = 0;
- char charBuf[128] = {'\0'};
- if(devHandlePtr != NULL)
- {
- ret = usb_reset(devHandlePtr);
- //ret = usb_close(devHandlePtr);
- if(ret < 0)
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Close ret = %d # %s #", ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- }
- }
- else
- ret = -1;
- return ret;
- }
- int USBSetActiveConfiguration(usb_dev *device, usb_dev_handle *devHandlePtr)
- {
- int ret = 0;
- char charBuf[128] = {'\0'};
- if((device != NULL) && (devHandlePtr != NULL))
- {
- if(device->descriptor.bNumConfigurations > 1)
- {
- ret = usb_set_configuration(devHandlePtr, device->config->bConfigurationValue);
- if(ret < 0)
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Set configuration ret = %d # %s #\n", ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- ret = 1;
- }
- }
- }
- else
- ret = -1;
- return ret;
- }
- int USBClaimInterface(usb_dev *device, usb_dev_handle *devHandlePtr)
- {
- int i = 0, ret = 0;
- char charBuf[256] = { '\0' };
- //debug_log("1",DEBUG_LOG);
- if((device != NULL) && (devHandlePtr != NULL))
- {
- //debug_log("2",DEBUG_LOG);
- ret = usb_claim_interface(devHandlePtr, 0);
- if(ret == 0)
- {
- //debug_log("3",DEBUG_LOG);
- ret = usb_release_interface(devHandlePtr, 0);
- if(ret == 0)
- {
- memset(charBuf, 0, sizeof(charBuf));
- sprintf(charBuf,"LOOP: %d", device->config->bNumInterfaces);
- //debug_log(charBuf, DEBUG_LOG);
- for(i = 0; i < device->config->bNumInterfaces; i++)
- {
- memset(charBuf, 0, sizeof(charBuf));
- sprintf(charBuf,"bInterfaceNumber: %d", device->config->interface[i].altsetting->bInterfaceNumber);
- //debug_log(charBuf, DEBUG_LOG);
- if(device->config->interface[i].altsetting->bInterfaceClass != USB_CLASS_COMM)
- {
- //debug_log("usb_claim_interface()",DEBUG_LOG);
- ret = usb_claim_interface(devHandlePtr, (int)device->config->interface[i].altsetting->bInterfaceNumber);
- //usleep(1000);
- //printf("\nreturn: %d\n", ret);
- if(ret < 0)
- {
- if(device->config->interface[i].altsetting->bInterfaceNumber != 0)
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Claim interface %d: ret = %d # %s #", device->config->interface[i].altsetting->bInterfaceNumber, ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- }
- //This is not necessary here, if the required device is busy at any instance it will enter into this case and detaches the reference of usb device, and claims for next port. In that case, if another Nippon get detected and its handler will be used instead of actual device. which will lead to communication error on sending actual peripherals command.
- // if(abs(ret) == EBUSY)
- // {
- // ret = usb_detach_kernel_driver_np(devHandlePtr, device->config->interface[i].altsetting->bInterfaceNumber);
- // memset(charBuf, '\0', sizeof(charBuf));
- // sprintf(charBuf, "Detach a kernel driver from interface %d: ret = %d # %s #", device->config->interface[i].altsetting->bInterfaceNumber, ret, strerror(abs(ret)));
- // debug_log(charBuf, DEBUG_LOG);
- // i--;
- // }
- }
- else
- {
- }
- }
- }
- }
- else
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Claim-Release interface 0-else try: ret = %d # %s #", ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- }
- }
- else
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Claim interface 0 try: ret = %d-else # %s #", ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- }
- }
- else
- {
- // If Empty handler is passed here we need to return no device as response. Since the value of ENODEV is 19 we make it as negative value and return it to the caller function which always expects for positive value.
- ret = -ENODEV;
- }
- return ret;
- }
- int USBReleaseInterface(usb_dev *device, usb_dev_handle *devHandlePtr)
- {
- int i = 0, ret = 0;
- char charBuf[128] = { '\0' };
- if((device != NULL) && (devHandlePtr != NULL))
- {
- ret = usb_release_interface(devHandlePtr, 0);
- if(ret == 0)
- {
- for(i = 0; i < device->config->bNumInterfaces; i++)
- {
- if(device->config->interface[i].altsetting->bInterfaceClass != USB_CLASS_COMM)
- {
- ret = usb_release_interface(devHandlePtr, (int)device->config->interface[i].altsetting->bInterfaceNumber);
- if((device->config->interface[i].altsetting->bInterfaceNumber != 0) && (ret < 0))
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Release interface %d: ret = %d # %s #", device->config->interface[i].altsetting->bInterfaceNumber, ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- }
- else
- ret = 0;
- }
- }
- }
- else
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Release interface 0 try:ret = %d # %s #", ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- }
- }
- else
- {
- // If Empty handler is passed here we need to return no device as response. Since the value of ENODEV is 19 we make it as negative value and return it to the caller function which always expects for positive value.
- ret = -ENODEV;
- }
- return ret;
- }
- int USBClearHaltOnEP(usb_dev_handle *devHandlePtr, const unsigned char ep)
- {
- int ret = 0;
- int retryClearHaltCnt = 0, clearHaltCnt = 0;
- char charBuf[128] = {'\0'};
- if(devHandlePtr != NULL)
- {
- for(retryClearHaltCnt = 0, clearHaltCnt = 0; retryClearHaltCnt < 5; retryClearHaltCnt++)
- {
- // Clear any halt status on an end point
- ret = usb_clear_halt(devHandlePtr, ep);
- if(ret <= 0)
- {
- if(++clearHaltCnt == 2)
- break;
- }
- else if(ret < 0)
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "Clear halt ret = %d # %s #", ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- //sleep(1);
- }
- }
- }
- else
- clearHaltCnt = -1;
- return clearHaltCnt;
- }
- int USBBulkWriteOnEP(usb_dev_handle *devHandlePtr, unsigned char *txBuffer, unsigned int numBytesToSend, const uint8_t ep, const unsigned int epPktSize)
- {
- int ret = 0, i = 0;
- unsigned long int timeout = 2000;//numBytesToSend * 30; // ms
- unsigned int numBytesToSendRem = 0;
- int numBytesSent = 0;
- char charBuf[153600] = {'\0'};
- char tempBuf[8] = {'\0'};
- // FIXME: Wrote a character as a way to initiate communication.
- // ret = usb_bulk_write(devHandlePtr, ep, ".", 1, timeout);
- // if(ret < 0)
- // {
- // memset(charBuf, '\0', sizeof(charBuf));
- // sprintf(charBuf, "Write ret = %d # %s #", ret, strerror(abs(ret)));
- // debug_log(charBuf, DEBUG_LOG);
- // return -1;
- // }
- if(devHandlePtr != NULL)
- {
- for(numBytesToSendRem = numBytesToSend, numBytesSent = 0; numBytesSent < numBytesToSend; )
- {
- ret = usb_bulk_write(devHandlePtr, ep, (char*)&txBuffer[numBytesSent], MIN(numBytesToSendRem, epPktSize), timeout);
- if(ret <= 0)
- {
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "TX. ret = %d # %s #", ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- return numBytesSent;
- }
- numBytesSent += ret;
- if(numBytesToSendRem >= epPktSize)
- numBytesToSendRem -= epPktSize;
- }
- }
- else
- numBytesSent = -1;
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "TX:sent=%d Bytes, Data(hex):", numBytesSent);
- if((50 + (numBytesToSend * 3)) < sizeof(charBuf))
- {
- for(i = 0; i < numBytesToSend; i++)
- {
- sprintf(tempBuf, " 0x%02x,", txBuffer[i]);
- strcat(charBuf, tempBuf);
- }
- }
- else
- strcat(charBuf, "Data exceeded limit");
- debug_log(charBuf, DEBUG_LOG); //SP_!!
- return numBytesSent;
- }
- int USBBulkReadOnEP(usb_dev_handle *devHandlePtr, unsigned char *rxBuffer, unsigned int numOfBytesToRx, const uint8_t ep, const unsigned int epPktSize)
- {
- int ret = 0, i = 0;
- unsigned long int timeout = 10000;// ms
- int numOfBytesReceived = 0;
- char charBuf[30720] = {'\0'};
- char tempBuf[8] = {'\0'};
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "ep=%#x, epPktSize=%d, numOfBytesToRx=%d", ep, epPktSize, numOfBytesToRx);
- debug_log(charBuf, DEBUG_LOG);
- memset(rxBuffer, '\0', numOfBytesToRx);
- if(devHandlePtr != NULL)
- {
- ret = usb_bulk_read(devHandlePtr, ep, (char*)rxBuffer, numOfBytesToRx, timeout);
- if(ret >= 0)
- {
- if(ret == 0)
- memset(rxBuffer, '\0', numOfBytesToRx);
- numOfBytesReceived = ret;
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "RX:recvd=%d Bytes, Data(hex):", numOfBytesReceived);
- if((50 + (numOfBytesReceived * 3)) < sizeof(charBuf))
- {
- for(i = 0; i < numOfBytesReceived; i++)
- {
- sprintf(tempBuf, " %02x", rxBuffer[i]);
- strcat(charBuf, tempBuf);
- }
- }
- else
- strcat(charBuf, "Data exceeded limit");
- debug_log(charBuf, DEBUG_LOG);
- }
- else
- {
- // Remove the below check for debugging purposes only
- //if(abs(ret) != ETIMEDOUT)
- //{
- memset(charBuf, '\0', sizeof(charBuf));
- sprintf(charBuf, "RX. Failed !! ret = %d # %s", ret, strerror(abs(ret)));
- debug_log(charBuf, DEBUG_LOG);
- //}
- }
- }
- else
- numOfBytesReceived = -1;
- return numOfBytesReceived;
- }
- int writeToUSB(usb_dev *device, usb_dev_handle *devHandlePtr, unsigned char *txBuffer, unsigned int numBytesToSend, const uint8_t ep, const unsigned int epPktSize)
- {
- int i = 0;
- int ret = 0;
- debug_log("WRITE_TO_USB:: is invoked!\n",DEBUG_LOG);
- printf("ENODEV: %d, EBUSY: %d\n", ENODEV, EBUSY);
- for(i = 0; i < CLAIM_INTERFACE_RETRY_ON_NODEV_MAX; i++)
- {
- ret = USBClaimInterface(device, devHandlePtr);
- printf("return: %d\n", ret);
- if(ret == 0)
- {
- ret = USBBulkWriteOnEP(devHandlePtr, txBuffer, numBytesToSend, ep, epPktSize);
- USBReleaseInterface(device, devHandlePtr);
- break;
- }
- else if(abs(ret) == ENODEV)
- {
- printf("NO DEvice error!\n");
- break;
- }
- else if(abs(ret) == EBUSY)
- {
- usleep(500);
- }
- debug_log("write claim retry\n", DEBUG_LOG);
- }
- if(ret < 0)
- ret = -1;
- return ret;
- }
- int readFromUSB(usb_dev *device, usb_dev_handle *devHandlePtr, unsigned char *rxBuffer, unsigned int numOfBytesToRx, const uint8_t ep, const unsigned int epPktSize)
- {
- int i = 0;
- int ret = 0;
- debug_log("READ_FROM_USB:: is invoked!\n",DEBUG_LOG);
- printf("ENODEV: %d, EBUSY: %d\n", ENODEV, EBUSY);
- for(i = 0; i < CLAIM_INTERFACE_RETRY_ON_NODEV_MAX; i++)
- {
- ret = USBClaimInterface(device, devHandlePtr);
- printf("return: %d\n", ret);
- if(ret == 0)
- {
- if(USBClearHaltOnEP(devHandlePtr, ep) == 2)
- {
- ret = USBBulkReadOnEP(devHandlePtr, rxBuffer, numOfBytesToRx, ep, epPktSize);
- printf("ret: %d\n", ret);
- }
- USBReleaseInterface(device, devHandlePtr);
- break;
- }
- else if(abs(ret) == ENODEV)
- {
- printf("NO DEvice error!\n");
- break;
- }
- else if(abs(ret) == EBUSY)
- {
- usleep(500);
- }
- debug_log("read claim retry\n", DEBUG_LOG);
- }
- if(ret < 0)
- ret = -1;
- return ret;
- }
- int main()
- {
- //ret = usb_reset(devHandlePtr);
- //memset(charBuf, '\0', sizeof(charBuf));
- //sprintf(charBuf, " Reset_Device = %d ", ret);
- //debug_log(charBuf, DEBUG_LOG);
- //printf("\n RESET_DEVICE: %d", ret);
- if(printer_powerInit_custom() == 0)
- {
- debug_log("\nprinter is connected!\n",DEBUG_LOG);
- if(printer_handleExist_custom() == DEV_READY)
- {
- printer_fwversion_custom(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment