venkat_330

libusb_reset_every_command

Jan 11th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <err.h>
  4. #include <error.h>
  5. #include <errno.h>
  6. #include <usb.h>
  7. #include <math.h>
  8. #define USB_DEBUG_VERBOSE 3 // Complete log
  9. #define CLAIM_INTERFACE_RETRY_ON_NODEV_MAX 3
  10. #define VID 0x0DD4 /* Custom receipt printer Vendor ID */
  11. #define PID 0x01A8 /* Custom receipt printer Product ID */
  12. #define BOUT_EP 0x02 /* Custom receipt printer bulk-out end point */
  13. #define BOUT_EPMAXPKTSIZE 64 /* Custom receipt printer bulk-out end point maximum packet size in bytes */
  14. #define BIN_EP 0x81 /* Custom receipt printer bulk-in end point */
  15. #define BIN_EPMAXPKTSIZE 64  /* Custom receipt printer bulk-in end point maximum packet size in bytes */
  16. #define DEBUG_LOG 1
  17.  
  18. struct usb_dev_handle
  19. {
  20.     int fd;
  21.     struct usb_bus *bus;
  22.     struct usb_device *device;
  23.     int config;
  24.     int interface;
  25.     int altsetting;
  26.     void *impl_info;
  27. };
  28.  
  29. typedef struct usb_device usb_dev;
  30.  
  31.  
  32. static usb_dev *device_custom;
  33.  
  34. void debug_log(char *, int t);
  35. usb_dev_handle * USBOpenDevice(usb_dev *device, usb_dev_handle *devHandlePtr);
  36. int USBCloseDeviceHandle(usb_dev_handle *devHandlePtr);
  37. int USBSetActiveConfiguration(usb_dev *device, usb_dev_handle *devHandlePtr);
  38. int USBClaimInterface(usb_dev *device, usb_dev_handle *devHandlePtr);
  39. int USBReleaseInterface(usb_dev *device, usb_dev_handle *devHandlePtr);
  40. int USBClearHaltOnEP(usb_dev_handle *devHandlePtr, const uint8_t ep);
  41. int USBBulkWriteOnEP(usb_dev_handle *devHandlePtr, unsigned char *txBuffer, unsigned int numBytesToSend, const uint8_t ep, const unsigned int epPktSize);
  42. int USBBulkReadOnEP(usb_dev_handle *devHandlePtr, unsigned char *rxBuffer, unsigned int numOfBytesToRx, const uint8_t ep, const unsigned int epPktSize);
  43. int writeToUSB(usb_dev *device, usb_dev_handle *devHandlePtr, unsigned char *txBuffer, unsigned int numBytesToSend, const uint8_t ep, const unsigned int epPktSize);
  44. int readFromUSB(usb_dev *device, usb_dev_handle *devHandlePtr, unsigned char *rxBuffer, unsigned int numOfBytesToRx, const uint8_t ep, const unsigned int epPktSize);
  45. usb_dev *devUsbExist(unsigned int vendorId, unsigned int productId, usb_dev *devHandle, uint8_t numDev);
  46.  
  47.  
  48.  
  49.  
  50.  
  51. typedef enum
  52. {
  53.     DEV_READY,
  54.     DEV_COMM_ERROR,
  55.     DEV_HEAD_UP,
  56.     DEV_NO_PAPER,
  57.     DEV_PAPER_JAM,
  58.     DEV_CUTTER_ERROR,
  59.     DEV_PAPER_INALIGN,
  60.     DEV_BM_ERROR,
  61.     DEV_NOT_ENABLED,
  62.     DEV_LOWPAPER,
  63.     DEV_PAPER_FULL,
  64.     DEV_NO_HOME,
  65.     DEV_REV_MOTOR_ERR,
  66.     DEV_PRINTER_BUSY,
  67.     DEV_PRESENT_BLOCK,
  68.     DEV_EJECT_BLOCK,
  69.     DEV_PRINT_HEAD_OVERHEAT,
  70.     DEV_VOLTAGE_EXCEEDED,
  71.     DEV_FLASH_ERROR,
  72.  
  73.     DEV_RP, // Used for identification of Nippon receipt and journal printers
  74.     DEV_JP // Used for identification of Nippon receipt and journal printers
  75. }printer_status_t;
  76.  
  77. static usb_dev *device_custom;
  78. static usb_dev_handle *devHandlePtr_custom;
  79. #define BIN_EP 0x81
  80.  
  81. /*
  82.  Sequence to use libusb-0.1 API s
  83.  1. Initialise USB
  84.  2. Find busses
  85.  3. Find devices
  86.  4. Get busses
  87.  5. Iterate through busses to find a required device
  88.  6. Open the device
  89.  7. Set the active configuration(when multiple number of interface)
  90.  8. Set the alternate setting(optional)
  91.  9. Claim the interface
  92. 10. Do your processing
  93. 11. Release the interface
  94. 12. close the device
  95. */
  96. void debug_log(char *data,int level)
  97. {
  98.     printf("%s\n",data);
  99. }
  100.  
  101. usb_dev *devUsbExist(unsigned int vendorId, unsigned int productId, usb_dev *device, uint8_t numDev)
  102. {
  103.     struct usb_bus *bussesPtr;
  104.     struct usb_bus *bus;
  105.     int c = 0, i = 0, a = 0;
  106.     char usbDevice = 0;
  107.     //char charBuf[128] = {'\0'};
  108.     static unsigned int vid = 0, pid = 0;
  109.     static int temp = 0;
  110.     int gotoNextDev = 0;
  111.  
  112.     usb_init();
  113.     usb_find_busses();
  114.     usb_find_devices();
  115.  
  116.     bussesPtr = usb_get_busses();
  117.  
  118.     for(bus = bussesPtr; bus; bus = bus->next)
  119.     {
  120.         for(device = bus->devices; device; device = device->next, gotoNextDev = 0)
  121.         {
  122.             //memset(charBuf, '\0', sizeof(charBuf));
  123.             //sprintf(charBuf, "bdeviceClass=%2d\n", device->descriptor.bDeviceClass);
  124.             //debug_log(charBuf, DEBUG_LOG);
  125.  
  126.             /* Loop through all of the configurations */
  127.             for(c = 0; (gotoNextDev != 1) && (c < device->descriptor.bNumConfigurations); c++)
  128.             {
  129.                 /* Loop through all of the interfaces */
  130.                 for(i = 0; (gotoNextDev != 1) && (i < device->config[c].bNumInterfaces); i++)
  131.                 {
  132.                     /* Loop through all of the alternate settings */
  133.                     for(a = 0; (gotoNextDev != 1) && (a < device->config[c].interface[i].num_altsetting); a++)
  134.                     {
  135.                         //memset(charBuf, '\0', sizeof(charBuf));
  136.                         //sprintf(charBuf, "bdeviceClass=%2d, bInterfaceClass=%3d", device->descriptor.bDeviceClass, device->config[c].interface[i].altsetting[a].bInterfaceClass);
  137.                         //debug_log(charBuf, DEBUG_LOG);
  138.  
  139.                         if((device->descriptor.idVendor == vendorId) && (device->descriptor.idProduct == productId))
  140.                         {
  141.                             if((vendorId != vid) && (productId != pid))
  142.                                 temp = 1;
  143.                             else
  144.                                 temp++;
  145.  
  146.                             if(temp == 1)
  147.                             {
  148.                                 //memset(charBuf, '\0', sizeof(charBuf));
  149.                                 //sprintf(charBuf, "Device Exists ! device=%p", device);
  150.                                 //debug_log(charBuf, DEBUG_LOG);
  151.                             }
  152.  
  153.                             vid = vendorId;
  154.                             pid = productId;
  155.  
  156.                             usbDevice = 1;
  157. //                          debug_log(" devUsbExist: Stop", DEBUG_LOG);
  158.                             if(--numDev == 0)
  159.                                 return device;
  160.                             else
  161.                                 gotoNextDev = 1;
  162.                         }
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.     }
  168.  
  169.     if(usbDevice == 0)
  170.         device = NULL;
  171.  
  172.     debug_log(" devUsbExist: Stop", DEBUG_LOG);
  173.     return device;
  174. }
  175.  
  176.  
  177. int8_t printer_exist_custom(uint8_t numDev)
  178. {
  179.     int8_t ret = 0;
  180.  
  181.     debug_log("printer_exist_custom", DEBUG_LOG);
  182.  
  183.     device_custom = devUsbExist(VID, PID, device_custom, numDev);
  184.     ret = (device_custom != NULL) ? DEV_READY : DEV_COMM_ERROR;
  185.  
  186.     return ret;
  187. }
  188.  
  189. int8_t printer_openUSBport_custom(void)
  190. {
  191.     int ret = 0;
  192.  
  193.     debug_log("printer_openUSBport_ Start", DEBUG_LOG);
  194.  
  195.     devHandlePtr_custom = USBOpenDevice(device_custom, devHandlePtr_custom);
  196.     ret = (devHandlePtr_custom != NULL) ? DEV_READY : DEV_COMM_ERROR;
  197.  
  198.     debug_log("printer_openUSBport_ Stop", DEBUG_LOG);
  199.  
  200.     return ret;
  201. }
  202.  
  203. int8_t printer_closeUSBport_custom(void)
  204. {
  205.     int ret = 0;
  206.  
  207.     debug_log("printer_closeUSBport_ Start", DEBUG_LOG);
  208.  
  209.     ret = USBCloseDeviceHandle(devHandlePtr_custom);
  210.     if(ret == 0)
  211.     {
  212.         device_custom = NULL;
  213.         devHandlePtr_custom = NULL;
  214.         ret = DEV_READY;
  215.     }
  216.     else
  217.         ret = DEV_COMM_ERROR;
  218.  
  219.     debug_log("printer_closeUSBport_ Stop", DEBUG_LOG);
  220.  
  221.     return ret;
  222. }
  223. int8_t printer_tx_custom(unsigned char *cmd, unsigned int numBytesToTx)
  224. {
  225.     int numBytesTx = 0;
  226.     int8_t status = 0;
  227.  
  228.     numBytesTx = writeToUSB(device_custom, devHandlePtr_custom, &cmd[0], numBytesToTx, BOUT_EP, BOUT_EPMAXPKTSIZE);
  229.  
  230.     if(numBytesToTx == numBytesTx)
  231.     {
  232.         debug_log("Send Success !", DEBUG_LOG);
  233.         status = DEV_READY;
  234.     }
  235.     else
  236.     {
  237.         numBytesTx = writeToUSB(device_custom, devHandlePtr_custom, &cmd[0], numBytesToTx, BOUT_EP, BOUT_EPMAXPKTSIZE);
  238.  
  239.         if(numBytesToTx == numBytesTx)
  240.         {
  241.             debug_log("Send Success !", DEBUG_LOG);
  242.             status = DEV_READY;
  243.         }
  244.         else
  245.         {
  246.             debug_log("Send Failed !!", DEBUG_LOG);
  247.             status = DEV_COMM_ERROR;
  248.         }
  249.     }
  250.     return status;
  251. }
  252.  
  253. int8_t printer_rx_custom(unsigned char *rxBuf, unsigned int numBytesToRx)
  254. {
  255.     int numBytesRx = 0;
  256.     int8_t status = 0;
  257.     int i = 0;
  258.     char charBuf[128] = {'\0'};
  259.     char tempBuf[8] = {'\0'};
  260.     char logBuf[128] = {'\0'};
  261.  
  262.     numBytesRx = readFromUSB(device_custom, devHandlePtr_custom, &rxBuf[0], numBytesToRx, BIN_EP, BIN_EPMAXPKTSIZE);
  263.     if(numBytesToRx == numBytesRx)
  264.     {
  265.         memset(&charBuf, '\0', sizeof(charBuf));
  266.         sprintf(charBuf, "Receive Success. Data(hex):");
  267.         for(i = 0; i < numBytesRx; i++)
  268.         {
  269.             sprintf(tempBuf, " %02x", rxBuf[i]);
  270.             strcat(logBuf, tempBuf);
  271.         }
  272.         strcat(charBuf, logBuf);
  273.         debug_log(charBuf, DEBUG_LOG);
  274.         status = DEV_READY;
  275.     }
  276.     else
  277.     {
  278.         numBytesRx = readFromUSB(device_custom, devHandlePtr_custom, &rxBuf[0], numBytesToRx, BIN_EP, BIN_EPMAXPKTSIZE);
  279.         if(numBytesToRx == numBytesRx)
  280.         {
  281.             memset(charBuf, '\0', sizeof(charBuf));
  282.             sprintf(charBuf, "Receive Success. Data(hex):");
  283.             for(i = 0; i < numBytesRx; i++)
  284.             {
  285.                 sprintf(tempBuf, " %02x", rxBuf[i]);
  286.                 strcat(logBuf, tempBuf);
  287.             }
  288.             strcat(charBuf, logBuf);
  289.             debug_log(charBuf, DEBUG_LOG);
  290.             status = DEV_READY;
  291.         }
  292.         else
  293.         {
  294.             memset(charBuf, '\0', sizeof(charBuf));
  295.             sprintf(charBuf, "Receive Failed !! ToRX=%d, RX=%d ", numBytesToRx, numBytesRx);
  296.             debug_log(charBuf, DEBUG_LOG);
  297.             status = DEV_COMM_ERROR;
  298.         }
  299.     }
  300.  
  301.     return status;
  302. }
  303.  
  304.  
  305.  
  306. int8_t printer_fwversion_custom(int type)
  307. {
  308.     unsigned char fwCmd[] = { 0x1D, 0x49, 0x03 };
  309.     unsigned char response[5];
  310.     float version = 0.0;
  311.     int numBytesToTx = 0;
  312.     int8_t status = 0;
  313.  
  314.     numBytesToTx = sizeof(fwCmd);
  315.     status = printer_tx_custom(&fwCmd[0], numBytesToTx);
  316.     if(status == DEV_READY)
  317.     {
  318.         memset(response,0,sizeof(response));
  319.         status = printer_rx_custom(&response[0], 4);
  320.         if(status == DEV_READY)
  321.         {
  322.             version = (response[0]-0x30) + atoi((char *)&response[2]) / pow(10,strlen((char *)&response[2]));
  323.            
  324.             printf("\n\n FW_VERSION: %f \n", version);
  325.         }
  326.     }
  327.  
  328.     return status;
  329. }
  330.  
  331. int8_t printer_openIfExist_custom(void)
  332. {
  333.     int8_t ret = 0;
  334.  
  335.     //debug_log("printer_openIfExist_custom", DEBUG_LOG);
  336.  
  337.     printer_closeUSBport_custom();
  338.     ret = printer_exist_custom(1);
  339.     if(ret == DEV_READY)
  340.         ret = printer_openUSBport_custom();
  341.  
  342.     return ret;
  343. }
  344.  
  345. int8_t printer_handleExist_custom(void)
  346. {
  347.     return (device_custom != NULL) ? DEV_READY : printer_openIfExist_custom();
  348. }
  349.  
  350.  
  351.  
  352. int8_t printer_powerInit_custom()
  353. {
  354.     //if(printer_handleExist_custom() == DEV_READY)
  355.     if(printer_exist_custom(1) == DEV_READY)
  356.     {
  357.         devHandlePtr_custom = usb_open(device_custom);
  358.         if(devHandlePtr_custom != NULL)
  359.         {
  360.             if( usb_set_configuration(devHandlePtr_custom,device_custom->config->bConfigurationValue)<0 )
  361.             {
  362.                 if( usb_detach_kernel_driver_np(devHandlePtr_custom,device_custom->config->interface->altsetting->bInterfaceNumber)<0 )
  363.                 {
  364.                     fprintf(stderr,"usb_set_configuration Error.\n");
  365.                     fprintf(stderr,"usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror());
  366.                 }
  367.             }
  368.              
  369.             if( usb_claim_interface(devHandlePtr_custom,device_custom->config->interface->altsetting->bInterfaceNumber)<0 )
  370.             {
  371.                 if( usb_detach_kernel_driver_np(devHandlePtr_custom,device_custom->config->interface->altsetting->bInterfaceNumber)<0 )
  372.                 {
  373.                     fprintf(stderr,"usb_claim_interface Error.\n");
  374.                     fprintf(stderr,"usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror());
  375.                 }
  376.             }
  377.              
  378.             if( usb_claim_interface(devHandlePtr_custom,device_custom->config->interface->altsetting->bInterfaceNumber)<0 )
  379.             {
  380.                 fprintf(stderr,"usb_claim_interface Error.(%s)\n",usb_strerror());
  381.             }
  382.         }
  383.         else
  384.             printf("\n usb_open failed!! ");
  385.    
  386.         printer_closeUSBport_custom();
  387.     }
  388.     else
  389.     {
  390.    
  391.         printf("\n ERROR IN POWER INIT");
  392.         return 1;
  393.     }
  394.        
  395.     return 0;
  396. }
  397.  
  398. usb_dev_handle * USBOpenDevice(usb_dev *device, usb_dev_handle *devHandlePtr)
  399. {
  400.     if(!device)
  401.     {
  402.         debug_log(" Device not found !!", DEBUG_LOG);
  403.         devHandlePtr = NULL;
  404.     }
  405.     else
  406.     {
  407.         // open the device
  408.         devHandlePtr = usb_open(device);
  409.  
  410.         if(devHandlePtr == NULL)
  411.         {
  412.             debug_log(" Device open failed !!", DEBUG_LOG);
  413.             devHandlePtr = NULL;
  414.         }
  415.         else
  416.         {
  417.  
  418.         }
  419.     }
  420.  
  421.     return devHandlePtr;
  422. }
  423.  
  424. int USBCloseDeviceHandle(usb_dev_handle *devHandlePtr)
  425. {
  426.     int ret = 0;
  427.     char charBuf[128] = {'\0'};
  428.  
  429.     if(devHandlePtr != NULL)
  430.     {
  431.         ret = usb_reset(devHandlePtr);
  432.                
  433.         //ret = usb_close(devHandlePtr);
  434.         if(ret < 0)
  435.         {
  436.             memset(charBuf, '\0', sizeof(charBuf));
  437.             sprintf(charBuf, "Close ret = %d # %s #", ret, strerror(abs(ret)));
  438.             debug_log(charBuf, DEBUG_LOG);
  439.         }
  440.     }
  441.     else
  442.         ret = -1;
  443.  
  444.     return ret;
  445. }
  446.  
  447. int USBSetActiveConfiguration(usb_dev *device, usb_dev_handle *devHandlePtr)
  448. {
  449.     int ret = 0;
  450.     char charBuf[128] = {'\0'};
  451.  
  452.     if((device != NULL) && (devHandlePtr != NULL))
  453.     {
  454.         if(device->descriptor.bNumConfigurations > 1)
  455.         {
  456.             ret = usb_set_configuration(devHandlePtr, device->config->bConfigurationValue);
  457.             if(ret < 0)
  458.             {
  459.                 memset(charBuf, '\0', sizeof(charBuf));
  460.                 sprintf(charBuf, "Set configuration ret = %d # %s #\n", ret, strerror(abs(ret)));
  461.                 debug_log(charBuf, DEBUG_LOG);
  462.                 ret = 1;
  463.             }
  464.         }
  465.     }
  466.     else
  467.         ret = -1;
  468.  
  469.     return ret;
  470. }
  471.  
  472. int USBClaimInterface(usb_dev *device, usb_dev_handle *devHandlePtr)
  473. {
  474.     int i = 0, ret = 0;
  475.     char charBuf[256] = { '\0' };
  476.    
  477.     //debug_log("1",DEBUG_LOG);
  478.    
  479.     if((device != NULL) && (devHandlePtr != NULL))
  480.     {
  481.         //debug_log("2",DEBUG_LOG);
  482.        
  483.         ret = usb_claim_interface(devHandlePtr, 0);
  484.         if(ret == 0)
  485.         {
  486.             //debug_log("3",DEBUG_LOG);
  487.            
  488.             ret = usb_release_interface(devHandlePtr, 0);
  489.             if(ret == 0)
  490.             {
  491.                 memset(charBuf, 0, sizeof(charBuf));
  492.                 sprintf(charBuf,"LOOP: %d", device->config->bNumInterfaces);
  493.                 //debug_log(charBuf, DEBUG_LOG);
  494.            
  495.                 for(i = 0; i < device->config->bNumInterfaces; i++)
  496.                 {
  497.                     memset(charBuf, 0, sizeof(charBuf));
  498.                     sprintf(charBuf,"bInterfaceNumber: %d", device->config->interface[i].altsetting->bInterfaceNumber);
  499.                     //debug_log(charBuf, DEBUG_LOG);
  500.                
  501.                     if(device->config->interface[i].altsetting->bInterfaceClass != USB_CLASS_COMM)
  502.                     {
  503.                         //debug_log("usb_claim_interface()",DEBUG_LOG);
  504.                         ret = usb_claim_interface(devHandlePtr, (int)device->config->interface[i].altsetting->bInterfaceNumber);
  505.                         //usleep(1000);
  506.                        
  507.                         //printf("\nreturn: %d\n", ret);
  508.  
  509.                         if(ret < 0)
  510.                         {
  511.                             if(device->config->interface[i].altsetting->bInterfaceNumber != 0)
  512.                             {
  513.                                 memset(charBuf, '\0', sizeof(charBuf));
  514.                                 sprintf(charBuf, "Claim interface %d: ret = %d # %s #", device->config->interface[i].altsetting->bInterfaceNumber, ret, strerror(abs(ret)));
  515.                                 debug_log(charBuf, DEBUG_LOG);
  516.                             }
  517.  
  518.                             //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.
  519. //                          if(abs(ret) == EBUSY)
  520. //                          {
  521. //                              ret = usb_detach_kernel_driver_np(devHandlePtr, device->config->interface[i].altsetting->bInterfaceNumber);
  522. //                              memset(charBuf, '\0', sizeof(charBuf));
  523. //                              sprintf(charBuf, "Detach a kernel driver from interface %d: ret = %d # %s #", device->config->interface[i].altsetting->bInterfaceNumber, ret, strerror(abs(ret)));
  524. //                              debug_log(charBuf, DEBUG_LOG);
  525. //                              i--;
  526. //                          }
  527.                         }
  528.                         else
  529.                         {
  530.                            
  531.                         }
  532.                     }
  533.                 }
  534.             }
  535.             else
  536.             {
  537.                 memset(charBuf, '\0', sizeof(charBuf));
  538.                 sprintf(charBuf, "Claim-Release interface 0-else try: ret = %d # %s #", ret, strerror(abs(ret)));
  539.                 debug_log(charBuf, DEBUG_LOG);
  540.             }
  541.         }
  542.         else
  543.         {
  544.             memset(charBuf, '\0', sizeof(charBuf));
  545.             sprintf(charBuf, "Claim interface 0 try: ret = %d-else # %s #", ret, strerror(abs(ret)));
  546.             debug_log(charBuf, DEBUG_LOG);
  547.         }
  548.     }
  549.     else
  550.     {
  551.         // 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.
  552.         ret = -ENODEV;
  553.     }
  554.  
  555.     return ret;
  556. }
  557.  
  558. int USBReleaseInterface(usb_dev *device, usb_dev_handle *devHandlePtr)
  559. {
  560.     int i = 0, ret = 0;
  561.     char charBuf[128] = { '\0' };
  562.  
  563.     if((device != NULL) && (devHandlePtr != NULL))
  564.     {
  565.         ret = usb_release_interface(devHandlePtr, 0);
  566.         if(ret == 0)
  567.         {
  568.             for(i = 0; i < device->config->bNumInterfaces; i++)
  569.             {
  570.                 if(device->config->interface[i].altsetting->bInterfaceClass != USB_CLASS_COMM)
  571.                 {
  572.                     ret = usb_release_interface(devHandlePtr, (int)device->config->interface[i].altsetting->bInterfaceNumber);
  573.  
  574.                     if((device->config->interface[i].altsetting->bInterfaceNumber != 0) && (ret < 0))
  575.                     {
  576.                         memset(charBuf, '\0', sizeof(charBuf));
  577.                         sprintf(charBuf, "Release interface %d: ret = %d # %s #", device->config->interface[i].altsetting->bInterfaceNumber, ret, strerror(abs(ret)));
  578.                         debug_log(charBuf, DEBUG_LOG);
  579.                     }
  580.                     else
  581.                         ret = 0;
  582.                 }
  583.             }
  584.         }
  585.         else
  586.         {
  587.             memset(charBuf, '\0', sizeof(charBuf));
  588.             sprintf(charBuf, "Release interface 0 try:ret = %d # %s #", ret, strerror(abs(ret)));
  589.             debug_log(charBuf, DEBUG_LOG);
  590.         }
  591.     }
  592.     else
  593.     {
  594.         // 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.
  595.         ret = -ENODEV;
  596.     }
  597.     return ret;
  598. }
  599.  
  600. int USBClearHaltOnEP(usb_dev_handle *devHandlePtr, const unsigned char ep)
  601. {
  602.     int ret = 0;
  603.     int retryClearHaltCnt = 0, clearHaltCnt = 0;
  604.     char charBuf[128] = {'\0'};
  605.  
  606.     if(devHandlePtr != NULL)
  607.     {
  608.         for(retryClearHaltCnt = 0, clearHaltCnt = 0; retryClearHaltCnt < 5; retryClearHaltCnt++)
  609.         {
  610.             // Clear any halt status on an end point
  611.             ret = usb_clear_halt(devHandlePtr, ep);
  612.             if(ret <= 0)
  613.             {
  614.                 if(++clearHaltCnt == 2)
  615.                         break;
  616.             }
  617.             else if(ret < 0)
  618.             {
  619.                 memset(charBuf, '\0', sizeof(charBuf));
  620.                 sprintf(charBuf, "Clear halt ret = %d # %s #", ret, strerror(abs(ret)));
  621.                 debug_log(charBuf, DEBUG_LOG);
  622.                 //sleep(1);
  623.             }
  624.         }
  625.     }
  626.     else
  627.         clearHaltCnt = -1;
  628.    
  629.     return clearHaltCnt;
  630. }
  631.  
  632. int USBBulkWriteOnEP(usb_dev_handle *devHandlePtr, unsigned char *txBuffer, unsigned int numBytesToSend, const uint8_t ep, const unsigned int epPktSize)
  633. {
  634.     int ret = 0, i = 0;
  635.     unsigned long int timeout = 2000;//numBytesToSend * 30; // ms
  636.  
  637.     unsigned int numBytesToSendRem = 0;
  638.     int numBytesSent = 0;
  639.  
  640.     char charBuf[153600] = {'\0'};
  641.     char tempBuf[8] = {'\0'};
  642.  
  643.     // FIXME: Wrote a character as a way to initiate communication.
  644. //  ret = usb_bulk_write(devHandlePtr, ep, ".", 1, timeout);
  645. //  if(ret < 0)
  646. //  {
  647. //      memset(charBuf, '\0', sizeof(charBuf));
  648. //      sprintf(charBuf, "Write ret = %d # %s #", ret, strerror(abs(ret)));
  649. //      debug_log(charBuf, DEBUG_LOG);
  650. //      return -1;
  651. //  }
  652.  
  653.     if(devHandlePtr != NULL)
  654.     {
  655.         for(numBytesToSendRem = numBytesToSend, numBytesSent = 0; numBytesSent < numBytesToSend; )
  656.         {
  657.             ret = usb_bulk_write(devHandlePtr, ep, (char*)&txBuffer[numBytesSent], MIN(numBytesToSendRem, epPktSize), timeout);
  658.             if(ret <= 0)
  659.             {
  660.                 memset(charBuf, '\0', sizeof(charBuf));
  661.                 sprintf(charBuf, "TX. ret = %d # %s #", ret, strerror(abs(ret)));
  662.                 debug_log(charBuf, DEBUG_LOG);
  663.                 return numBytesSent;
  664.             }
  665.  
  666.             numBytesSent += ret;
  667.  
  668.             if(numBytesToSendRem >= epPktSize)
  669.                 numBytesToSendRem -= epPktSize;
  670.         }
  671.     }
  672.     else
  673.         numBytesSent = -1;
  674.  
  675.     memset(charBuf, '\0', sizeof(charBuf));
  676.     sprintf(charBuf, "TX:sent=%d Bytes, Data(hex):", numBytesSent);
  677.     if((50 + (numBytesToSend * 3)) < sizeof(charBuf))
  678.     {
  679.         for(i = 0; i < numBytesToSend; i++)
  680.         {
  681.             sprintf(tempBuf, " 0x%02x,", txBuffer[i]);
  682.             strcat(charBuf, tempBuf);
  683.         }
  684.     }
  685.     else
  686.         strcat(charBuf, "Data exceeded limit");
  687.     debug_log(charBuf, DEBUG_LOG); //SP_!!
  688.  
  689.     return numBytesSent;
  690. }
  691.  
  692. int USBBulkReadOnEP(usb_dev_handle *devHandlePtr, unsigned char *rxBuffer, unsigned int numOfBytesToRx, const uint8_t ep, const unsigned int epPktSize)
  693. {
  694.     int ret = 0, i = 0;
  695.     unsigned long int timeout = 10000;// ms
  696.  
  697.     int numOfBytesReceived = 0;
  698.     char charBuf[30720] = {'\0'};
  699.     char tempBuf[8] = {'\0'};
  700.  
  701.     memset(charBuf, '\0', sizeof(charBuf));
  702.     sprintf(charBuf, "ep=%#x, epPktSize=%d, numOfBytesToRx=%d", ep, epPktSize, numOfBytesToRx);
  703.     debug_log(charBuf, DEBUG_LOG);
  704.  
  705.     memset(rxBuffer, '\0', numOfBytesToRx);
  706.     if(devHandlePtr != NULL)
  707.     {
  708.         ret = usb_bulk_read(devHandlePtr, ep, (char*)rxBuffer, numOfBytesToRx, timeout);
  709.         if(ret >= 0)
  710.         {
  711.             if(ret == 0)
  712.                 memset(rxBuffer, '\0', numOfBytesToRx);
  713.  
  714.             numOfBytesReceived = ret;
  715.  
  716.             memset(charBuf, '\0', sizeof(charBuf));
  717.             sprintf(charBuf, "RX:recvd=%d Bytes, Data(hex):", numOfBytesReceived);
  718.             if((50 + (numOfBytesReceived * 3)) < sizeof(charBuf))
  719.             {
  720.                 for(i = 0; i < numOfBytesReceived; i++)
  721.                 {
  722.                     sprintf(tempBuf, " %02x", rxBuffer[i]);
  723.                     strcat(charBuf, tempBuf);
  724.                 }
  725.             }
  726.             else
  727.                 strcat(charBuf, "Data exceeded limit");
  728.             debug_log(charBuf, DEBUG_LOG);
  729.         }
  730.         else
  731.         {
  732.             // Remove the below check for debugging purposes only
  733.             //if(abs(ret) != ETIMEDOUT)
  734.             //{
  735.                 memset(charBuf, '\0', sizeof(charBuf));
  736.                 sprintf(charBuf, "RX. Failed !! ret = %d # %s", ret, strerror(abs(ret)));
  737.                 debug_log(charBuf, DEBUG_LOG);
  738.             //}
  739.         }
  740.     }
  741.     else
  742.         numOfBytesReceived = -1;
  743.  
  744.     return numOfBytesReceived;
  745. }
  746.  
  747. int writeToUSB(usb_dev *device, usb_dev_handle *devHandlePtr, unsigned char *txBuffer, unsigned int numBytesToSend, const uint8_t ep, const unsigned int epPktSize)
  748. {
  749.     int i = 0;
  750.     int ret = 0;
  751.  
  752.     debug_log("WRITE_TO_USB:: is invoked!\n",DEBUG_LOG);
  753.     printf("ENODEV: %d, EBUSY: %d\n", ENODEV, EBUSY);
  754.    
  755.     for(i = 0; i < CLAIM_INTERFACE_RETRY_ON_NODEV_MAX; i++)
  756.     {
  757.         ret = USBClaimInterface(device, devHandlePtr);
  758.         printf("return: %d\n", ret);
  759.         if(ret == 0)
  760.         {
  761.             ret = USBBulkWriteOnEP(devHandlePtr, txBuffer, numBytesToSend, ep, epPktSize);
  762.             USBReleaseInterface(device, devHandlePtr);
  763.             break;
  764.         }
  765.         else if(abs(ret) == ENODEV)
  766.         {
  767.             printf("NO DEvice error!\n");
  768.             break;
  769.         }
  770.         else if(abs(ret) == EBUSY)
  771.         {
  772.             usleep(500);
  773.         }
  774.         debug_log("write claim retry\n", DEBUG_LOG);
  775.     }
  776.  
  777.     if(ret < 0)
  778.         ret = -1;
  779.  
  780.     return ret;
  781. }
  782.  
  783. int readFromUSB(usb_dev *device, usb_dev_handle *devHandlePtr, unsigned char *rxBuffer, unsigned int numOfBytesToRx, const uint8_t ep, const unsigned int epPktSize)
  784. {
  785.     int i = 0;
  786.     int ret = 0;
  787.  
  788.     debug_log("READ_FROM_USB:: is invoked!\n",DEBUG_LOG);
  789.     printf("ENODEV: %d, EBUSY: %d\n", ENODEV, EBUSY);
  790.  
  791.     for(i = 0; i < CLAIM_INTERFACE_RETRY_ON_NODEV_MAX; i++)
  792.     {
  793.         ret = USBClaimInterface(device, devHandlePtr);
  794.         printf("return: %d\n", ret);
  795.         if(ret == 0)
  796.         {
  797.             if(USBClearHaltOnEP(devHandlePtr, ep) == 2)
  798.             {
  799.                 ret = USBBulkReadOnEP(devHandlePtr, rxBuffer, numOfBytesToRx, ep, epPktSize);
  800.                 printf("ret: %d\n", ret);
  801.             }
  802.             USBReleaseInterface(device, devHandlePtr);
  803.             break;
  804.         }
  805.         else if(abs(ret) == ENODEV)
  806.         {
  807.             printf("NO DEvice error!\n");
  808.             break;
  809.         }
  810.         else if(abs(ret) == EBUSY)
  811.         {
  812.             usleep(500);
  813.         }
  814.         debug_log("read claim retry\n", DEBUG_LOG);
  815.     }
  816.  
  817.     if(ret < 0)
  818.         ret = -1;
  819.  
  820.     return ret;
  821. }
  822.  
  823. int main()
  824.  
  825. {
  826.         //ret = usb_reset(devHandlePtr);
  827.        
  828.         //memset(charBuf, '\0', sizeof(charBuf));
  829.         //sprintf(charBuf, " Reset_Device = %d ", ret);
  830.         //debug_log(charBuf, DEBUG_LOG);
  831.         //printf("\n RESET_DEVICE: %d", ret);
  832.     if(printer_powerInit_custom() == 0)
  833.     {
  834.         debug_log("\nprinter is connected!\n",DEBUG_LOG);  
  835.         if(printer_handleExist_custom() == DEV_READY)
  836.         {
  837.             printer_fwversion_custom(1);
  838.         }
  839.        
  840.     }
  841. }
Add Comment
Please, Sign In to add comment