/* update current status of printer */ void sig_usr1(int sig) { char prninfo[256]/*, status[32]*/; int isUsb; //fp=fopen("/etc/linuxigd/printer.log","w"); memset( prninfo , 0 , sizeof(prninfo) ); if (check_par_usb_prn() == TRUE) //uese USB when return TRUE { //fputs( "INTERFACE=USB\n" , fp ); nvram_set("printer_ifname", "usb"); readUsbPrnID(prninfo); isUsb = 1; } else { // Goto this step, then printer must be turn on //fputs( "INTERFACE=PAR\n" , fp ); nvram_set("printer_ifname", "par"); readParPrnID(prninfo); isUsb = 0; } if (strcmp(prninfo, " ")==0 || strlen(prninfo)==0) { //fprintf(fp, "MODEL=\"\"\n"); nvram_set("printer_model_t", ""); if (!isUsb) { /* Retry, only when paraport is plugged and no device id is returned */ printf("Try again 2 seconds later\n"); g_retry++; if (g_retry<MAX_GETID_RETRY) alarm(6); else g_retry = 0; } } else { //fprintf(fp, "MODEL=\"%s\"\n", prninfo); nvram_set("printer_model_t", prninfo); } /* update status of printer */ if (nvram_invmatch("printer_model_t", "")) { readPrnStatus(); } else { nvram_set("printer_status_t", ""); nvram_set("printer_user_t", ""); } // PRINT("Enter sig_usr1\n"); // nosiy... }
void readPrnID(char *prninfo) { // char *token; if (check_par_usb_prn() == TRUE) //uese USB when return TRUE { readUsbPrnID(prninfo); } else { readParPrnID(prninfo); } }
/*#######Lisa: Raw printing########## * open printer() * copy stream() * ProcessReq_Raw() ##################################*/ int open_printer(void) { int f; check_par_usb_prn(); if(g_useUsb==TRUE) { if ((f=open("/dev/usb/lp0",O_RDWR)) < 0 ) { syslog(LOGOPTS, "cannot open /dev/usb/lp0: %m\n"); exit(1); } } else { if ((f=open("dev/lp0",O_RDWR)) < 0) { // syslog(LOGOPTS, "Open Parallel port error"); exit(1); } } return (f); }
int Job_status( int *sock, char *input ) { char *s, *t, *name, *hash_key; int displayformat, status_lines = 0, i, n; struct line_list l, listv; struct line_list done_list; char error[SMALLBUFFER], buffer[16]; int db, dbflag; FILE *READSTATUSFILE;//JY1120 char readbuffer[SMALLBUFFER];//JY1120 char *str_index;//JY1120 #if !defined(JYWENG20031104status) if( input && *input ) ++input;//JY1114 if(get_queue_name(input)) { printf("QueueName is not LPRServer\n"); send_ack_packet(sock, ACK_FAIL);//JY1120 return(0); } else printf("QueueName is LPRServer\n"); int prnstatus=1; char buffertosend[LARGEBUFFER]; int fdPRNPARorUSB=0;/*JYWENG20031104*/ if((check_par_usb_prn())== 1) fdPRNPARorUSB=open("/dev/usb/lp0",O_RDWR); else fdPRNPARorUSB=open("/dev/lp0",O_RDWR); if(fdPRNPARorUSB == 0) { printf("file descriptor not created\n"); send_ack_packet(sock, ACK_FAIL);//JY1120 return(0); } // ioctl(fdPRNPARorUSB, 0x060b, &prnstatus); // if(prnstatus == 0) //JY1120 if((READSTATUSFILE=fopen("/var/state/printstatus.txt", "r")) == NULL) { printf("open /var/state/printstatus.txt failed!\n"); send_ack_packet(sock, ACK_FAIL);//JY1120 return(0); } while( fgets(readbuffer, SMALLBUFFER, READSTATUSFILE) != NULL) { if((str_index = strstr(readbuffer, "PRINTER_STATUS=\""))) { str_index += 16;//moving to status strncpy(printerstatus, str_index, strlen(str_index) - 2 ); } } //JY1120 SNPRINTF(buffertosend, sizeof(buffertosend))"Status: %s\n", printerstatus); // else // SNPRINTF(buffertosend, sizeof(buffertosend))"Status: Off line\n"); // if( Write_fd_str( *sock, buffertosend ) < 0 ) cleanup(0); if( write( *sock, buffertosend, strlen(buffertosend) ) < 0 ) cleanup(0); exit(0);//JY1120 #endif #ifdef REMOVE Init_line_list(&l); Init_line_list(&listv); Init_line_list(&done_list); db = Debug; dbflag = DbgFlag; Name = "Job_status"; /* get the format */ if( (s = safestrchr(input, '\n' )) ) *s = 0; displayformat = *input++; /* * if we get a short/long request from these hosts, * reverse the sense of question */ if( Reverse_lpq_status_DYN && (displayformat == REQ_DSHORT || displayformat==REQ_DLONG) ){ Free_line_list(&l); Split(&l,Reverse_lpq_status_DYN,File_sep,0,0,0,0,0,0); if( Match_ipaddr_value( &l, &RemoteHost_IP ) == 0 ){ DEBUGF(DLPQ1)("Job_status: reversing status sense"); if( displayformat == REQ_DSHORT ){ displayformat = REQ_DLONG; } else { displayformat = REQ_DSHORT; } } Free_line_list(&l); } /* * we have a list of hosts with format of the form: * Key=list; Key=list;... * key is s for short, l for long */ DEBUGF(DLPQ1)("Job_status: Force_lpq_status_DYN '%s'", Force_lpq_status_DYN); if( Force_lpq_status_DYN ){ Free_line_list(&listv); Split(&listv,Force_lpq_status_DYN,";",0,0,0,0,0,0); for(i = 0; i < listv.count; ++i ){ s = listv.list[i]; if( (t = safestrpbrk(s,File_sep)) ) *t++ = 0; Free_line_list(&l); Split(&l,t,Value_sep,0,0,0,0,0,0); DEBUGF(DLPQ1)("Job_status: Force_lpq_status '%s'='%s'", s,t); if( Match_ipaddr_value( &l, &RemoteHost_IP ) == 0 ){ DEBUGF(DLPQ1)("Job_status: forcing status '%s'", s); if( safestrcasecmp(s,"s") == 0 ){ displayformat = REQ_DSHORT; } else if( safestrcasecmp(s,"l") == 0 ){ displayformat = REQ_DLONG; } status_lines = Short_status_length_DYN; break; } } Free_line_list(&l); Free_line_list(&listv); } /* * check for short status to be returned */ if( Return_short_status_DYN && displayformat == REQ_DLONG ){ Free_line_list(&l); Split(&l,Return_short_status_DYN,File_sep,0,0,0,0,0,0); if( Match_ipaddr_value( &l, &RemoteHost_IP ) == 0 ){ status_lines = Short_status_length_DYN; DEBUGF(DLPQ1)("Job_status: truncating status to %d", status_lines); } Free_line_list(&l); } DEBUGF(DLPQ1)("Job_status: doing '%s'", input ); Free_line_list(&l); Split(&l,input,Whitespace,0,0,0,0,0,0); if( l.count == 0 ){ SNPRINTF( error, sizeof(error)) "zero length command line"); goto error; }
int processReq(int sockfd) { char recv_buf[4]; //buffer to receive header static char waittime = 1; int iCount; struct print_buffer pbuf; char chPortOpened = FALSE; /***************************************/ /** We reset the printer only when **/ /** user wants to cancel a job or **/ /** error occurs **/ /***************************************/ //Process the request from cleint //return when error or job complete while(TRUE) { LPT_CMD_PKT_HDR *pHdrCmd = NULL; LPT_DATA_PKT_HDR *pHdrData = NULL; LPT_RES_PKT_HDR pktRes; WORD body[1]; int rcv; //records how many bytes being received char *para_buf = NULL; //buffer to store parameter memset(recv_buf,0,sizeof(recv_buf)); iCount = sizeof(recv_buf); if(waittime < 5) { waittime ++; } //Receive the complete header here while( iCount > 0 ) { rcv = RECV(sockfd , recv_buf + (4 - iCount) , iCount , 60); if( rcv < 1) { //receive error //PRINT("1. rcv -> %d\n",rcv); if(rcv < 0) { //perror("ERR:"); } closesocket(sockfd); if(chPortOpened == TRUE) { reset_printer(10); } return 0; } iCount = iCount - rcv; } //Check Service ID switch(recv_buf[0]) { case NET_SERVICE_ID_LPT_EMU: //PRINT("Service ID -> NET_SERVICE_ID_LPT_EMU \n"); break; default: //PRINT("Service ID -> Not Supported \n"); closesocket(sockfd); if(chPortOpened == TRUE) { reset_printer(10); } return(0); break; } //Check Packet Type switch(recv_buf[1]) { case NET_PACKET_TYPE_CMD: //PRINT(">>> TYPE_CMD "); pHdrCmd = (LPT_CMD_PKT_HDR *)recv_buf; break; case NET_PACKET_TYPE_RES: //We should not recevice Response Packet in Server //PRINT("Packet Type -> NET_PACKET_TYPE_RES Error!!! \n"); closesocket(sockfd); if(chPortOpened == TRUE) { reset_printer(10); } return(0); break; case NET_PACKET_TYPE_DATA: //PRINT("$$$ TYPE_DATA "); pHdrData = (LPT_DATA_PKT_HDR *)recv_buf; break; default: //PRINT("Packet Type -> Not Supported \n"); closesocket(sockfd); if(chPortOpened == TRUE) { reset_printer(10); } return(0); break; } if( pHdrCmd != NULL) { //We receive command para_buf = NULL; iCount = pHdrCmd->ParaLength; if (iCount!=0) { para_buf = (char *)malloc(pHdrCmd->ParaLength); } //PRINT("HdrCmd Length %d\n", iCount); // para_buf may be NULL but this command still work // 2004/06/07 by Joey if(iCount!=0 && para_buf == NULL) { //perror("malloc error 1:"); closesocket(sockfd); if(chPortOpened == TRUE) { reset_printer(10); } return(0); } while( iCount > 0 ) { rcv = RECV(sockfd , (para_buf + (pHdrCmd->ParaLength - iCount )) , iCount , 30); if( rcv < 1) { //receive error //perror("2. RECV ERR:"); closesocket(sockfd); free(para_buf); if(chPortOpened == TRUE) { reset_printer(10); } return 0; } iCount = iCount - rcv; } switch(pHdrCmd->CommandID) { case NET_CMD_ID_OPEN: //remark PRINT("NET_CMD_ID_OPEN\n"); /************************************/ /************************************/ /*** TODO: add code here to check ***/ /*** the printer status ***/ /************************************/ /************************************/ pktRes.ServiceID = NET_SERVICE_ID_LPT_EMU; pktRes.PacketType = NET_PACKET_TYPE_RES; pktRes.CommandID = NET_CMD_ID_OPEN; pktRes.ResLength = 2; if(busy == FALSE) { int prnstatus=0; FILE *statusFp = NULL; //remark PRINT("--------lptstatus.busy == FALSE\n"); /* add by James Yeh to support usb printer */ /* 2002/12/25 */ check_par_usb_prn(); //Open printer port -modified by PaN if(g_useUsb == TRUE) { fdPRN = open("/dev/usb/lp0",O_RDWR); } else { fdPRN = open("/dev/lp0",O_RDWR); } if(fdPRN == 0) { //Failed to open printer port //PRINT("Can not open lp0 errno -> %d \n",errno); //perror("ERR:"); //Send header send(sockfd , (const char *)&pktRes , sizeof(pktRes) , 0); //Send body body[0] = ERR_SERVER_LPT_FAIL; send(sockfd , body , sizeof(body) , 0); free(para_buf); return(0); } ioctl(fdPRN,LPGETSTATUS,&prnstatus); if(prnstatus != 0) { //remark PRINT("prnstatus != 0\n"); body[0] = ERR_SERVER_OCCUPIED; /*******************************************************************************/ /* why we are using ERR_SERVER_OCCUPIED instead of ERR_SERVER_LPT_FAIL here ?? */ /* Because ERR_SERVER_OCCUPIED will let user try again & again. Using */ /* ERR_SERVER_LPT_FAIL will fail the reques */ /*******************************************************************************/ //Send header send(sockfd , (const char *)&pktRes , sizeof(pktRes) , 0); //Send body send(sockfd , body , sizeof(body) , 0); free(para_buf); return(0); } statusFp = fopen(STATUS_FILE , "w"); if(statusFp != NULL) { fprintf(statusFp,"PRN_CLIENT=\"%s\"\n",lptstatus.addr); fclose(statusFp); } else { //perror("Open status file failed: "); } chPortOpened = TRUE; body[0] = ERR_SUCCESS; } else { //PRINT("*********lptstatus.busy == TRUE\n"); body[0] = ERR_SERVER_OCCUPIED; //Send header send(sockfd , (const char *)&pktRes , sizeof(pktRes) , 0); //Send body send(sockfd , body , sizeof(body) , 0); free(para_buf); return(0); } //Send header send(sockfd , (const char *)&pktRes , sizeof(pktRes) , 0); //Send body send(sockfd , body , sizeof(body) , 0); break; case NET_CMD_ID_CLOSE: { char bCancel = FALSE; //remark PRINT("NET_CMD_ID_CLOSE\n"); /*****************************************************/ /* Check if user normally or abnormally end this job */ /* James 2002/06/05 */ /*****************************************************/ if(pHdrCmd->ParaLength != 1) //Length should be 1 byte long { //remark PRINT("NET_CMD_ID_CLOSE length error -- %d\n",pHdrCmd->ParaLength); closesocket(sockfd); free(para_buf); if(chPortOpened == TRUE) { reset_printer(10); } return(0); } //remark PRINT("para_buf[0] - %02X\n",para_buf[0]); if(para_buf[0] == 1) { bCancel = TRUE; } pktRes.ServiceID = NET_SERVICE_ID_LPT_EMU; pktRes.PacketType = NET_PACKET_TYPE_RES; pktRes.CommandID = NET_CMD_ID_CLOSE; pktRes.ResLength = 2; //Send header send(sockfd , (const char *)&pktRes , sizeof(pktRes) , 0); //Send body body[0] = ERR_SUCCESS; send(sockfd , body , sizeof(body) , 0); closesocket(sockfd); free(para_buf); //close printer port modified by PaN sleep(1); if(bCancel == TRUE) { //reset printer //PRINT("Reset Printer\n"); reset_printer(10); } close(fdPRN); return(0); break; }//case NET_CMD_ID_CLOSE: case NET_CMD_ID_READ: { LPT_DATA_PKT_HDR pktData; WORD len; int res_fread = 0; PBYTE pReadbuf; len = (para_buf[1] << 8) + para_buf[0]; //remark PRINT("NET_CMD_ID_READ len -> %d\n",len); /************************************/ /************************************/ /*** TODO: add code here to read ***/ /*** the printer port ***/ /************************************/ /************************************/ pReadbuf = (char *)malloc(len + 1); if(pReadbuf == NULL) { //perror("malloc error 2:"); closesocket(sockfd); free(para_buf); if(chPortOpened == TRUE) { reset_printer(10); } return(0); } pbuf.len = len; pbuf.buf = pReadbuf; //PRINT("Start Read\n"); res_fread = ioctl(fdPRN,LPREADDATA,&pbuf); //PRINT("End Read\n"); //PRINT("---- res_fread %d\n",res_fread); if(res_fread > 0) { len = res_fread; pReadbuf[res_fread]= 0; //PRINT("*** %s\n",pReadbuf); } else { len = 0; } pktData.ServiceID = NET_SERVICE_ID_LPT_EMU; pktData.PacketType = NET_PACKET_TYPE_DATA; pktData.DataLength = len; //can not exceed 0xFFFF /********** Sending Header **********/ send(sockfd , (const char *)&pktData , sizeof(pktData) , 0); if( len > 0) { /********** Sending Body **********/ send(sockfd , (const char *)pReadbuf , len , 0); } free(pReadbuf); break; } free(para_buf); }//switch(pHdrCmd->CommandID) }//if( pHdrCmd != NULL) if( pHdrData != NULL) { //We receive Data int res_fwrite; int res_total_fwrite; int write_len; int total_len; PBYTE write_buf; int prnstatus=0; iCount = pHdrData->DataLength; if (iCount!=0) { para_buf = (char *)malloc(pHdrData->DataLength); } //remark //PRINT("pHdrData->DataLength -- %d\n",pHdrData->DataLength); if(iCount!=0 && para_buf == NULL) { //perror("malloc error 3:"); closesocket(sockfd); if(chPortOpened == TRUE) { reset_printer(10); } return(0); } //PRINT("DATA HDR OK...\n"); while( iCount > 0 ) { rcv = RECV(sockfd , (para_buf + (pHdrData->DataLength - iCount )) , iCount , 30); if( rcv < 1) { //receive error //perror("3. RECV ERR:"); closesocket(sockfd); free(para_buf); if(chPortOpened == TRUE) { reset_printer(10); } return 0; } iCount = iCount - rcv; } //PRINT("DATA BODY OK...\n"); pbuf.len = pHdrData->DataLength; pbuf.buf = para_buf; write_len = 0; total_len = pHdrData->DataLength; write_buf = para_buf; res_fwrite = 0; res_total_fwrite = 0; //remark PRINT("total_len %d\n",total_len); while(total_len > 0) { if(total_len > 4096) { pbuf.len = 4096; pbuf.buf = write_buf; res_fwrite = ioctl(fdPRN,LPWRITEDATA,&pbuf); if(res_fwrite != 4096) { DWORD retry = 0; ioctl(fdPRN,LPGETSTATUS,&prnstatus); //remark PRINT("prnstatus %d\n",prnstatus); while( ((prnstatus == 0) || (prnstatus & LP_PBUSY)) && (res_fwrite != 4096) && (retry < 3)) { //remark PRINT("}}}}}}}}}}}} res_fwrite %d\n",res_fwrite); pbuf.len = 4096 - res_fwrite; pbuf.buf = &(write_buf[res_fwrite]); //usleep(500); //why we don't use usleep here ?? becuse usleep will sleep longer then we expect in iBox sleep(1); res_fwrite = res_fwrite + ioctl(fdPRN,LPWRITEDATA,&pbuf); //remark PRINT("}}}}}}}}}}}} res_fwrite %d\n",res_fwrite); ioctl(fdPRN,LPGETSTATUS,&prnstatus); //remark PRINT("retry %d\n",retry); retry ++; } } res_total_fwrite = res_total_fwrite + res_fwrite; if(res_fwrite != 4096) { break; } else { total_len = total_len - 4096; if(total_len == 0) { //remark PRINT("total_len == 0 \n"); break; } write_buf = &(write_buf[4096]); } //remark PRINT("res_total_fwrite %d -- res_fwrite %d \n",res_total_fwrite,res_fwrite); } else { pbuf.len = total_len; pbuf.buf = write_buf; res_fwrite = ioctl(fdPRN,LPWRITEDATA,&pbuf); //remark PRINT("PPPPPPP res_fwrite %d\n",res_fwrite); if(res_fwrite != total_len) { DWORD retry = 0; ioctl(fdPRN,LPGETSTATUS,&prnstatus); //remark PRINT("prnstatus %d\n",prnstatus); while( ((prnstatus == 0) || (prnstatus & LP_PBUSY)) && (res_fwrite != total_len) && (retry < 3)) { pbuf.len = total_len - res_fwrite; pbuf.buf = &(write_buf[res_fwrite]); //usleep(500); //why we don't use usleep here ?? becuse usleep will sleep longer then we expect in iBox sleep(1); res_fwrite = res_fwrite + ioctl(fdPRN,LPWRITEDATA,&pbuf); //remark PRINT("}}}}}}}}}}}} res_fwrite %d\n",res_fwrite); ioctl(fdPRN,LPGETSTATUS,&prnstatus); //remark PRINT("retry %d\n",retry); retry ++; } } res_total_fwrite = res_total_fwrite + res_fwrite; //remark PRINT("res_total_fwrite %d -- res_fwrite %d \n",res_total_fwrite,res_fwrite); break; } } //remark PRINT("WritePrint %d - %d bytes\n",res_total_fwrite,pHdrData->DataLength); if(res_total_fwrite != pHdrData->DataLength) { int tmp=0; //remark PRINT("res_total_fwrite != pHdrData->DataLength \n"); ioctl(fdPRN,LPGETSTATUS,&prnstatus); //remark PRINT("prnstatus %08X \n",prnstatus); if((prnstatus & LP_PERRORP) == 1 ) //Printer off-line { //remark PRINT("Printer off-line -- prnstatus %d\n",prnstatus); res_total_fwrite = res_total_fwrite|0x8000; } else { //remark PRINT("Paper Empty -- prnstatus %d\n",prnstatus); res_total_fwrite = res_total_fwrite|0x4000; } check_prn_status("BUSY or ERROR", clientaddr); } else{//JY1113 add /*JY1113*/ check_prn_status("Printing", clientaddr); /**/ } //remark PRINT("res_total_fwrite %08X\n",res_total_fwrite); body[0] = res_total_fwrite; pktRes.ServiceID = NET_SERVICE_ID_LPT_EMU; pktRes.PacketType = NET_PACKET_TYPE_RES; pktRes.CommandID = NET_CMD_ID_DATA_RES; pktRes.ResLength = 2; //Send header send(sockfd , (const char *)&pktRes , sizeof(pktRes) , 0); //Send body send(sockfd , body , sizeof(body) , 0); free(para_buf); }//if( pHdrData != NULL) } //remark PRINT("Thread Over\n"); return(0); }