Esempio n. 1
0
int copy_stream(int fd,int f)
{
    int		nread,nwrite;
    char		buffer[8192];
    int 		timeout=20,wait;
    PRINT("copy_stream\n");
    while ((nread = read(fd, buffer, sizeof(buffer))) > 0)
    {
        int 		index=0,countread;
        //	nwrite=fwrite(buffer, sizeof(char), nread, f);
        /* Lisa:fwrite => write */
        while	(index < nread )
        {
            countread=nread-index;
            nwrite=write(f,&buffer[index],countread);
            index += nwrite;
            if ((wait=waitsock(f,timeout,0))==0)
                check_prn_status("Busy or Error",clientaddr);
        }
        check_prn_status("Printing",clientaddr);  //Add by Lisa
    }
    (void)fflush(f); // (STS)
    {   // (STS) wait 30 seconds for completion
        fd_set rfds;
        struct timeval tv;
        FD_ZERO(&rfds);
        FD_SET(f, &rfds);
        tv.tv_sec = 30;
        tv.tv_usec = 0;
        select(1, NULL, &rfds, NULL, &tv);
    }
    check_prn_status("ONLINE",""); //Add by Lisa
    return (nread);
}
Esempio n. 2
0
void sig_child(int sig)
{
    int childpid;


    childpid = waitpid(-1, NULL , WNOHANG);

    while( childpid > 0)
    {

        //remark PRINT("sig_child %d\n",childpid);

        if( lptstatus.pid == childpid )
        {
            FILE *statusFp = NULL;

            statusFp = fopen(STATUS_FILE , "w");

            if(statusFp != NULL)
            {
                fprintf(statusFp,"PRN_CLIENT=\"\"\n");
                fclose(statusFp);
            }
            else
            {
                perror("Open status file failed: ");
            }


            /*** Wait 10 seconds here      ***/
            /*** Because some slow printer ***/
            /*** need some time to consume ***/
            /*** the data...               ***/
            sleep(10);

            busy = FALSE;
            lptstatus.busy = FALSE;
            lptstatus.pid  = 0;
            check_prn_status(ONLINE, "");

        }

        childpid = waitpid(-1, NULL , WNOHANG);
    }

    //remark PRINT("waitpid -- childpid%d\n",childpid);

}
Esempio n. 3
0
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);                              
    
}
Esempio n. 4
0
int copy_stream(int fd,int f)
{
	int		nread,nwrite;
	char		buffer[8192];
	int 		timeout=20, wait;
	int 		busyflag=0;

	//PRINT("copy_stream\n");
	while ((nread = read(fd, buffer, sizeof(buffer))) > 0)
	{
		int index=0,countread;
		
		// nwrite=fwrite(buffer, sizeof(char), nread, f);    
                /* Lisa:fwrite => write */
 		check_prn_status("Printing", clientaddr);  //Add by Lisa
          
		while	(index < nread )
		{
			countread=nread-index;
			nwrite=write(f, &buffer[index],countread);

			if (nwrite<0)
			{					
	     			logmessage("lpd", "write error : %d\n", errno);
				check_prn_status("Busy or Error", clientaddr);
				return(nread);
			}
#ifdef REMOVE
			else if (nwrite==0)
			{
	     			syslog(LOG_NOTICE, "write error 4: %d\n", nwrite);
				check_prn_status("Busy or Error",clientaddr);
				busyflag=1;
			}
#endif
			else if ((wait=waitsock(f,timeout,0))==0)
			{
	     			//logmessage("lpd", "write error %d\n", errno);
				check_prn_status("Busy or Error",clientaddr);
				busyflag=1;
			}
			else if(wait<0) 
			{
	     			logmessage("lpd", "can not write : %d\n", errno);
				check_prn_status("Busy or Error",clientaddr);
				return(nread);
			}
			else
			{
				index+=nwrite;

				if (busyflag==1)
				{
					busyflag = 0;
					check_prn_status("Printing",clientaddr);
				}
			}
		}			                
	}
	//(void)fflush(f);
        check_prn_status(ONLINE,""); //Add by Lisa
	return (nread);
}
Esempio n. 5
0
int copy_stream(int fd,int f)
{
	int		nread,nwrite;
	char		buffer[8192];
	int 		timeout=20, wait;
	int 		busyflag=0;

	//PRINT("copy_stream\n");
	while ((nread = read(fd, buffer, sizeof(buffer))) > 0)
	{
		int index=0,countread;
		
		// nwrite=fwrite(buffer, sizeof(char), nread, f);    
                /* Lisa:fwrite => write */
 		check_prn_status("Printing", clientaddr);  //Add by Lisa
          
		while	(index < nread )
		{
			countread=nread-index;
			nwrite=write(f, &buffer[index],countread);

			if (nwrite<0)
			{					
	     			logmessage("lpd", "write error : %d\n", errno);
				check_prn_status("Busy or Error", clientaddr);
				return(nread);
			}
#ifdef REMOVE
			else if (nwrite==0)
			{
	     			syslog(LOG_NOTICE, "write error 4: %d\n", nwrite);
				check_prn_status("Busy or Error",clientaddr);
				busyflag=1;
			}
#endif
			else if ((wait=waitsock(f,timeout,0))==0)
			{
	     			//logmessage("lpd", "write error %d\n", errno);
				check_prn_status("Busy or Error",clientaddr);
				busyflag=1;
			}
			else if(wait<0) 
			{
	     			logmessage("lpd", "can not write : %d\n", errno);
				check_prn_status("Busy or Error",clientaddr);
				return(nread);
			}
			else
			{
				index+=nwrite;

				if (busyflag==1)
				{
					busyflag = 0;
					check_prn_status("Printing",clientaddr);
				}
			}
		}			                
	}
	(void)fflush(f);// (STS)
	{ // (STS) wait 30 seconds for completion
	  fd_set rfds;
	  struct timeval tv;
	  FD_ZERO(&rfds);
	  FD_SET(f, &rfds);
	  tv.tv_sec = 30;
	  tv.tv_usec = 0;
	  select(1, NULL, &rfds, NULL, &tv);
	}
        check_prn_status(ONLINE,""); //Add by Lisa
	return (nread);
}