1diff --git a/backend/usb.c b/backend/usb.c 2index a4dfbc0b..79050681 100644 3--- a/backend/usb.c 4+++ b/backend/usb.c 5@@ -110,6 +110,63 @@ print_device(const char *uri, /* I - Device URI */ 6 } 7 #endif /* HAVE_LIBUSB */ 8 9+#define HAVE_USB_MONITOR 10+#define BEGIN_WAIT_TIME 2 11+ 12+#ifdef HAVE_USB_MONITOR 13+#include "usb_monitor.h" 14+static char oldReasons[PRINTER_STATE_REASONS_SIZE] = {0}; 15+ 16+void UpdatePrinterState(PrinterStatus* jobData) 17+{ 18+ if (jobData == NULL) { 19+ fprintf(stderr, "DEBUG: USB_MONITOR UpdatePrinterState fail, jobData is NULL\n"); 20+ return; 21+ } 22+ char* addedReasons = NULL; 23+ char* deletedReasons = NULL; 24+ ComparePrinterStateReasons(oldReasons, jobData->printerStateReasons, &addedReasons, &deletedReasons); 25+ if (addedReasons != NULL) { 26+ fprintf(stderr, "STATE: +%s\nSTATE: -none\n", addedReasons); 27+ } 28+ if (deletedReasons != NULL) { 29+ fprintf(stderr, "STATE: -%s\nSTATE: -none\n", deletedReasons); 30+ } 31+ FreeCompareStringsResult(&addedReasons, &deletedReasons); 32+ if (strcpy_s(oldReasons, PRINTER_STATE_REASONS_SIZE, jobData->printerStateReasons) != 0) { 33+ fprintf(stderr, "DEBUG: USB_MONITOR UpdatePrinterState strcpy_s fail\n"); 34+ } 35+ if (strcmp(jobData->printerStateReasons, "none") == 0) { 36+ fprintf(stderr, "STATE: cups-waiting-for-job-completed\n"); 37+ return; 38+ } 39+} 40+ 41+static void HandleTerm(int32_t sig) 42+{ 43+ fprintf(stderr, "DEBUG: USB_MONITOR HandleTerm, set terminal singal\n"); 44+ SetTerminalSingal(); 45+} 46+ 47+static void* MonitorPrinterThread(void* arg) 48+{ 49+ if (arg == NULL) { 50+ fprintf(stderr, "DEBUG: USB_MONITOR arg is nullptr\n"); 51+ return NULL; 52+ } 53+ sleep(BEGIN_WAIT_TIME); 54+ char* uri = (char*)arg; 55+ if (IsSupportIppOverUsb(uri)) { 56+ if (StartMonitorIppPrinter(UpdatePrinterState, uri)) { 57+ fprintf(stderr, "DEBUG: USB_MONITOR StartMonitorIppPrinter finished\n"); 58+ } else { 59+ fprintf(stderr, "STATE: none\n"); 60+ } 61+ } 62+ free(uri); 63+ return NULL; 64+} 65+#endif /* HAVE_USB_MONITOR */ 66 67 /* 68 * 'main()' - Send a file to the specified USB port. 69@@ -231,6 +288,13 @@ main(int argc, /* I - Number of command-line arguments (6 or 7) */ 70 copies = atoi(argv[4]); 71 } 72 73+#ifdef HAVE_USB_MONITOR 74+signal(SIGTERM, HandleTerm); 75+fprintf(stderr, "STATE: cups-waiting-for-job-completed\n"); 76+char* uriCopy = strdup(uri); 77+pthread_t monitorThread = _cupsThreadCreate((_cups_thread_func_t)MonitorPrinterThread, uriCopy); 78+#endif /* HAVE_USB_MONITOR */ 79+ 80 /* 81 * Finally, send the print file... 82 */ 83@@ -245,5 +309,15 @@ main(int argc, /* I - Number of command-line arguments (6 or 7) */ 84 if (print_fd != 0) 85 close(print_fd); 86 87+#ifdef HAVE_USB_MONITOR 88+if (status != CUPS_BACKEND_OK) { 89+ fprintf(stderr, "DEBUG: USB_MONITOR print_device fail, status = %d\n", (int)status); 90+ SetTerminalSingal(); 91+ status = CUPS_BACKEND_FAILED; 92+ fprintf(stderr, "STATE: stopped\n"); 93+} 94+pthread_join(monitorThread, NULL); 95+#endif /* HAVE_USB_MONITOR */ 96+ 97 return (status); 98 } 99