int isPartPakfile(const char *filename) { FILE *file = fopen(filename, "rb"); if (file == NULL) err_exit("Can't open file %s\n", filename); struct p2_partmap_info partinfo; size_t size = sizeof(struct p2_partmap_info); fread(&partinfo, 1, size, file); char *cmagic; asprintf(&cmagic, "%x", partinfo.magic); if (isdatetime((char *)cmagic)) { printf("Found valid partpak magic 0x%x in %s\n", partinfo.magic, filename); } else { return 0; } detect_model(&(partinfo.dev)); fclose(file); if (part_type == STRUCT_INVALID) return 0; else return 1; }
/*----------------------------------------------------------------------------- Name : detect_usb Purpose : Detect USB printers Inputs : printers : printers array max : printers array size Outputs : Fills printers array with model and port information Return : number of printers detected -----------------------------------------------------------------------------*/ static int detect_usb(aps_printer_t *printers,int max) { aps_port_t *p[MAX_USB_PORTS]; aps_error_t errnum; char identity[APS_IDENTITY_MAX+1]; char uri[APS_URI_MAX+1]; int i,n; int model; int detected; /*list and open USB ports*/ n = usb_list_ports(p,MAX_USB_PORTS); for (i=0; i<n; i++) { aps_open(p[i]); } /*detect printers*/ detected = 0; for (i=0; i<n && detected<max; i++) { errnum = APS_OK; #if 1 { char buf[256]; usb_get_uri(p[i],buf,256); printf("DEBUG: port:%s\n",buf); } #endif if (errnum==APS_OK) { errnum = aps_set_write_timeout(p[i],100); } if (errnum==APS_OK) { model = MODEL_INVALID; if (model<0) { model = detect_model(p[i],identity,sizeof(identity),&cmd_aps); } if (model<0) { model = detect_model(p[i],identity,sizeof(identity),&cmd_escpos); } } #if 1 { printf("DEBUG: model:%d\n",model); } #endif if (errnum==APS_OK && model>=0) { if (aps_get_port_uri(p[i],uri,sizeof(uri))>=0) { printers->model = model; strcpy(printers->identity,identity); strcpy(printers->uri,uri); printers++; detected++; } } } /*close and destroy ports*/ for (i=0; i<n; i++) { aps_flush(p[i]); aps_close(p[i]); aps_destroy_port(p[i]); } return detected; }
/*----------------------------------------------------------------------------- Name : detect_parallel Purpose : Detect parallel printers Inputs : printers : printers array max : printers array size Outputs : Fills printers array with model and port information Return : number of printers detected -----------------------------------------------------------------------------*/ static int detect_parallel(aps_printer_t *printers,int max) { aps_port_t *p[MAX_PARALLEL_PORTS]; aps_error_t errnum; char device[DEVICE_MAX+1]; char identity[APS_IDENTITY_MAX+1]; char uri[APS_URI_MAX+1]; int model; int i,n; /*create parallel ports and wake-up printers*/ for (i=0; i<MAX_PARALLEL_PORTS; i++) { snprintf(device,sizeof(device),"/dev/parport%d",i); p[i] = aps_create_parallel_port(device); if (p[i]==NULL) { errnum = APS_INVALID_PORT; } else { errnum = aps_get_error(p[i]); } if (errnum==APS_OK) { /*detect parallel port mode*/ if (detect_parallel_irq(i)) { errnum = aps_parallel_set_mode(p[i],APS_IRQ); } else { errnum = aps_parallel_set_mode(p[i],APS_POLL); } } if (errnum==APS_OK) { errnum = aps_open(p[i]); } if (errnum==APS_OK) { errnum = aps_parallel_reset(p[i]); } } /*wait for printers to wake up*/ sleep(2); /*detect printers*/ n = 0; for (i=0; i<MAX_PARALLEL_PORTS && n<max; i++) { if (p[i]==NULL) { errnum = APS_INVALID_PORT; } else { errnum = APS_OK; } /*TODO: check if printer is connected*/ if (errnum==APS_OK) { errnum = aps_set_write_timeout(p[i],100); } if (errnum==APS_OK) { model = MODEL_INVALID; if (model<0) { model = detect_model(p[i],identity,sizeof(identity),&cmd_aps); } if (model<0) { model = detect_model(p[i],identity,sizeof(identity),&cmd_escpos); } } if (errnum==APS_OK && model>=0) { if (aps_get_port_uri(p[i],uri,sizeof(uri))>=0) { printers->model = model; strcpy(printers->identity,identity); strcpy(printers->uri,uri); printers++; n++; } } } /*close and destroy ports*/ for (i=0; i<MAX_PARALLEL_PORTS; i++) { if (p[i]!=NULL) { aps_flush(p[i]); aps_close(p[i]); aps_destroy_port(p[i]); p[i] = NULL; } } return n; }
/*----------------------------------------------------------------------------- Name : detect_serial Purpose : Detect serial printers Inputs : printers : printers array max : printers array size Outputs : Fills printers array with model and port information Return : number of printers detected -----------------------------------------------------------------------------*/ static int detect_serial(aps_printer_t *printers,int max) { aps_port_t *p[MAX_SERIAL_PORTS]; aps_error_t errnum; char device[DEVICE_MAX+1]; char identity[APS_IDENTITY_MAX+1]; char uri[APS_URI_MAX+1]; int baudrate; int handshake; int model; int i,n; /*create serial ports and wake-up printers*/ for (i=0; i<MAX_SERIAL_PORTS; i++) { snprintf(device,sizeof(device),"/dev/ttyS%d",i); p[i] = aps_create_serial_port(device); if (p[i]==NULL) { errnum = APS_INVALID_PORT; } else { errnum = aps_get_error(p[i]); } if (errnum==APS_OK) { errnum = aps_open(p[i]); } if (errnum==APS_OK) { errnum = aps_serial_set_baudrate(p[i],APS_B1200); } if (errnum==APS_OK) { errnum = aps_serial_set_handshake(p[i],APS_NONE); } if (errnum==APS_OK) { unsigned char c = NUL; errnum = aps_write(p[i],&c,1); /*wake-up*/ } } /*wait for printers to wake up*/ sleep(2); /*detect printers*/ n = 0; for (i=0; i<MAX_SERIAL_PORTS && n<max; i++) { if (p[i]==NULL) { errnum = APS_INVALID_PORT; } else { errnum = APS_OK; } /*TODO: check if printer is connected*/ if (errnum==APS_OK) { errnum = aps_set_write_timeout(p[i],100); } if (errnum==APS_OK) { model = MODEL_INVALID; for (baudrate=APS_B115200; baudrate>=APS_B1200; baudrate--) { if ((errnum = aps_serial_set_baudrate(p[i],baudrate))<0) { break; } if ((errnum = aps_serial_set_handshake(p[i],APS_NONE))<0) { break; } memset(identity, 0, sizeof identity); if (model<0) { model = detect_model(p[i],identity,sizeof(identity),&cmd_aps); } memset(identity, 0, sizeof identity); if (model<0) { model = detect_model(p[i],identity,sizeof(identity),&cmd_escpos); } if (model>=0) { errnum = detect_serial_handshake(p[i],model); if (errnum>=0) { handshake = errnum; errnum = aps_serial_set_handshake(p[i],handshake); } break; } } } if (errnum==APS_OK && model>=0) { if (aps_get_port_uri(p[i],uri,sizeof(uri))>=0) { printers->model = model; strcpy(printers->identity,identity); strcpy(printers->uri,uri); printers++; n++; } } } /*close and destroy ports*/ for (i=0; i<MAX_SERIAL_PORTS; i++) { if (p[i]!=NULL) { aps_flush(p[i]); aps_close(p[i]); aps_destroy_port(p[i]); p[i] = NULL; } } return n; }