Ejemplo n.º 1
0
int leconnect(bool reconnect)
{
	if (can_reconect && reconnect==FALSE)
	{
		image_already_bytes = 0;
		free(image_bytes);
	}

	if (app.status == APP_STATUS_TRANSMITTER && image_bytes_length <= 0)
	{
		printf("\nNO IMAGE SELECTED!");
	}
	else if (connect() == 0)
	{
		conection_open = TRUE;

		if (
			(app.status == APP_STATUS_TRANSMITTER ?
			sendImage(reconnect) : receiveImage(reconnect)) == 0)
		{
			show_display = NO;
			llclose(app.fd);

			//save file if receiver
			if (app.status == APP_STATUS_RECEIVER){
				if (save2File(image_bytes, image_bytes_length, image_name) != OK){

					printf("\nImage was not saved sucessfully.\n");
					free(image_bytes);
					image_bytes_length = 0;
					return -1;
				}
				free(image_bytes);
				image_bytes_length = 0;
				printf("\nImage was saved sucessfully.\n");
			}

		}
		close_tio(app.fd);
		conection_open = FALSE;

		show_display = NO;

	}

	return 0;
}
Ejemplo n.º 2
0
int llwrite(int fd, unsigned char* buffer, int length) {

	int CompleteFrames =  length / (MAX_SIZE);
	int remainingBytes =  length % (MAX_SIZE);
	int flag = 1;
	int i;
	linkLayer.alarm_inf = TRUE;
	linkLayer.sequenceNumber = 0;

	linkLayer.alarm_char = DATA;

	for(i = 0; i < CompleteFrames; i++) write_frame(fd, buffer, 0, &i , FALSE);

	if(remainingBytes > 0) write_frame(fd, buffer, remainingBytes, &i, TRUE);

	(void) signal(SIGALRM, Timeout);

	llclose(linkLayer.fd);

	return 0;

}
Ejemplo n.º 3
0
int main(int argc, char *argv[]) 
{
    // argv: tipo / port / tentativas / timeout / baundrate / [ficheiro / framesize]

    if(argc < 6) {
        printf("ARGUMENTS ERROR\n./rcom type port tries timeout baundrate [file framesize]\n");
        return -1;        
    }

    strcpy(linkData.port, argv[2]);
    linkData.numTransmissions = atoi(argv[3]);
    linkData.timeout = atoi(argv[4]);
    linkData.baudRate = atoi(argv[5]);

    int found=1;
    while(found==1)
    {
        switch(linkData.baudRate)
        {
            case 2400:found=0;break;
            case 4800:found=0;break;
            case 9600:found=0;break;
            case 19200:found=0;break;
            case 38400:found=0;break;
            case 57600:found=0;break;
            case 115200:found=0;break;
            case 31250:found=0;break;
            default:
            {
                printf("Error baundrate must be (2400,4800,9600,19200,38400,57600,115200 or 31250)\n");
                printf("New baundrate: ");
                scanf("%d",&linkData.baudRate);
                break;
            }

        }
    }
   


    if (strcmp(argv[1], "transmitter") == 0) {

        printf("*Transmitter*\n");
        
   
        if (argc == 8) {
            strcpy(appData.filename,argv[6]);
            appData.dataSize = atoi(argv[7]);
        } else if(argc != 6) {
            printf("ARGUMENTS ERROR\n./rcom type port tries timeout baundrate [file framesize]\n");
            return -1; 
        }

        transmitter();

    } else if(strcmp(argv[1], "receiver") == 0) {

        if(argc < 7) {
            printf("ARGUMENTS ERROR\nfilename is required\n");
            return -1; 
        }
        printf("*Receiver*\n");
        strcpy(appData.filename,argv[6]);
        if(argc > 7)
            appData.dataSize = atoi(argv[7]);
        receiver();

    } else {
        printf("ARGUMENTS ERROR\ntype should be 'receiver' or 'transmitter'\n");
        return -1; 
    }

    llclose();

	return 0;
}
Ejemplo n.º 4
0
int main(int argc, char** argv)
{
   int opcao;
   int port = 0;
   int tamanho = 1024;

   if(atoi(argv[1]) == 0)
   if(argc < 3)
   {
      printf("Too few arguments! Usage: msg << 1, 0 >> << file_path >> \nPress Enter to exit...");
      getchar();
      exit(1);
   }

   do{
      printf("0 - ");
      
      if(atoi(argv[1])== 0) printf("Enviar ");
      else if(atoi(argv[1]) == 1) printf("Receber ");

      printf("ficheiro (default: pinguim.gif)\n");
      
      printf("1 - Definir baudrate (default:B38400) \n");
      printf("2 - Definir timeout (default: 3)\n");
      printf("3 - Definir numero de tentativas de retransmissao (default: 3)\n");
      printf("4 - Definir porta (default: 0)\n");
      
      if(atoi(argv[1]) == 0)
         printf("5 - Definir tamanho de trama (default: 1024)\n");

      scanf("%d", &opcao);

      switch(opcao)
      {
         case 1:
         printf("\nBAUDRATE: ");
         scanf("%d", &baud);
         break;

         case 2:
         printf("\nTIMEOUT (s): ");
         scanf("%d", &timeout);
         break;

         case 3:
         printf("\nNUMERO DE TENTATIVAS: ");
         scanf("%d", &tries);
         break;

         case 4:
         printf("\nPORTA (0,1,2...): ");
         scanf("%d", &port);
         break;

         case 5:

         if(atoi(argv[1]) == 0)
         {
            printf("\nTAMANHO: ");
            scanf("%d", &tamanho);
            
         }

         break;
      }

   }while(opcao != 0);

   llopen(port,atoi(argv[1]));

   if ((atoi(argv[1])) == 0) appSender(argv[2], tamanho);
    
   if ((atoi(argv[1])) == 1) appReceiver();

   printf("\nTERMINATING COMMUNICATIONS\n");    

   if (llclose(atoi(argv[1])) == 0)
   {
      printf("Timed out %d times\n",nTimeouts);
      printf("Works!\n");
   }
   return 0;   
}
Ejemplo n.º 5
0
int appSend(char * port, const char * fpath) {

    printf("[appSend] Connecting to Receiver...n");
    if(llopen(port, SENDER) < 0) {
        printf("[appSend]ERROR: Couldn't reach the receiver!n");
        return -1;
    }


    printf("[appSend] CONNECTEDn");

    if( define_file(fpath) != 0) {
        printf("[appSend] !!Cannot open file!!n");
        return -1;
    }

    sendControl(START_PAC,pingu.size);

    appDef.seqNr = 0;

    int lastOne;
    char buf[256];
    if(pingu.size % PAC_SIZE > 0)
        lastOne = 1;
    else
        lastOne = 0;

    int totalPacs = (pingu.size / PAC_SIZE) + lastOne;
    int real_totalPacs=0,duplicados=0;
    float percent_lost;

    int n;
    while(1) {

        printf("[appSend] Sending packet %d of %dn", appDef.seqNr,totalPacs);

        int size = fread(buf, sizeof(char), PAC_SIZE, pingu.p);
        if (size <= 0) break;
        if( sendData(size, buf) == -1) {
            printf("[appSend] !!!Error sending packet %d!!!n", appDef.seqNr);
            while( sendData(size, buf) == -1) {
                printf("[appSend] Re-sending packet %d of %dn", appDef.seqNr,totalPacs);
                real_totalPacs++;
                duplicados++;
            }

        }
        appDef.seqNr = (appDef.seqNr + 1) % 256;
        real_totalPacs++;


    }

    printf("[appSend] Sending end packet...n");
    sendControl(END_PAC, pingu.size);
    printf("[appSend] Sent %d packets, of which %d duplicatesn",real_totalPacs,duplicados);
    percent_lost = (duplicados * 100)/real_totalPacs;
    printf("[appSend] %.2f %% of the packets sent were lostn",percent_lost);
    fclose(pingu.p);
    printf("[appSend] Closing connection...n");
    llclose(appDef.status);
    printf("[appSend] Exiting Application!n");
}
Ejemplo n.º 6
0
int app_read(char * port) {

	printf("> [APP] Connecting...\n");

	int fd = llopen(port, RECEIVER);
	
	if(fd < 0) {
		return ERROR;
	}
	
	char packet[MAX_FRAME_SIZE];
	fstatus.size = 0;
	fstatus.valid = FALSE;
	int fsize = 0;
	
	printf("> [APP] Connection established...\n");

	printf("> [APP] Receiving...\n");

	do{
		int size = llread(&packet[0]);
		
		if(size == -2) { // Disconnect (forced)
			break;
		}
		else if(size < 0) {
			printf("  > [APP] Some error ocurred reading a packet. Exiting...\n");
			return ERROR;
		}
		else {
			if(app_check_packet(packet, size) != OK){
					printf("  > [APP] Unrecognized packet type\n");
					return ERROR;
			}
			else {
				if(pack.ctrl == PACKET_START) {
					FILE *f = fopen(((fstatus.name == NULL) ? "out" : fstatus.name), "w");
					if( f < 0 ) {	
						printf("> [APP] Error opening output file\n");
						return ERROR;
					}
					fstatus.f = f;
					
					fstatus.valid = TRUE;
				}
				else if(fstatus.valid != TRUE) {
					printf("> [APP] No file info received\n");
					return ERROR;
				}
				else if(pack.ctrl == PACKET_DATA) {
					//writing data to file 
					fwrite(pack.data.buf, sizeof(char), pack.data.length, fstatus.f);
					fsize += pack.data.length;
				}
				else if(pack.ctrl == PACKET_END) {
					//
					break;
				}
			}
		}

	} while((fstatus.size != fsize) || (pack.ctrl != PACKET_END));
	
	printf("> [APP] Bytes read: [ %d / %d ]\n", fsize, fstatus.size);
	
	printf("> [APP] Closing connection...\n");
	
	llclose();

	return OK;
}
Ejemplo n.º 7
0
int app_send(char * port, const char * filepath) {

	printf("> [APP] Connecting...\n");

	// Getting file attributes
	if( read_file_size(filepath) != OK)
		return ERROR;
	
	// Open the file for reading and sending the data packets
	if( open_file(filepath, "r") != OK) {
		printf("> [APP] Error opening input file\n");
		return ERROR;
	}

	int fd = llopen(port, TRANSMITTER);

	if(fd < 0) {
		return ERROR;
	}
	
	printf("> [APP] Connection established...\n");
	
	printf("\n> [APP] Sending start control packet...\n");
	if( app_send_control(PACKET_START, PACKET_FILE_SIZE, fstatus.size) != OK) {
		return ERROR;
	}
	
	pack.sequence_nr = 0;
	int number_of_packets = (fstatus.size / MAX_PACKET_SIZE)
							+ (fstatus.size % MAX_PACKET_SIZE > 0 ? 1 : 0);
	
	char buf[MAX_PACKET_SIZE];
	int res = 0;
	int i;
	for(i = 0; i < number_of_packets; i++) {
	
		printf("\n> [APP] Sending data packet(%d)...\n", pack.sequence_nr);
		
		int read = fread(buf, sizeof(char), MAX_PACKET_SIZE, fstatus.f);
		
		res += read;
				
		if( app_send_data(pack.sequence_nr, read, buf) != OK) {
		
			printf("> [APP] Could not write packet(%d)\n", pack.sequence_nr);	
			fclose(fstatus.f);
			llclose();
			return ERROR;
		}
		
		pack.sequence_nr++;
	}
	printf("\n> [APP] Written bytes: [ %d / %d ]\n", res, fstatus.size);
	
	// Ending connection
	printf("\n> [APP] Sending end control packet...\n");
	if( app_send_control(PACKET_END, PACKET_FILE_SIZE, fstatus.size) != OK) {
		return ERROR;
	}
	
	printf("\n> [APP] Closing connection...\n");
	
	if(fclose(fstatus.f) < 0) {
		printf("  > [APP] Error closing the input file\n");	
		llclose();
		return ERROR;
	}
	
	llclose();

	return OK;
}
Ejemplo n.º 8
0
int sendFile(int port, int fd, char *filePath)
{
	int size = getSize(fd);
	unsigned char* buffer = (unsigned char*) mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
	if(buffer == MAP_FAILED){
		//printf("did not mmap\n");
		exit(-1);
	}
	unsigned char *bufferBckup = buffer;
	int length;

	unsigned char* packet = makeControlPacket(size , filePath, 1, &length);
	//printf("gonna send following packet of length %d: \n", length);
		/*int j;
		for(j = 0; j < length; j++){
			//printf("%d: 0x%02x - %c\n", j, packet[j], packet[j]);
		}*/
	if(llwrite(port, packet, length) < 0 )
		return -1;
	int i = 0;
	int acum = 0;
	float percentage;
	char *proBar = updateProgressBar(acum, size, &percentage);
	for(i = 0; i < (size/PACKET_SIZE); i++){
		/*//printf("gonna send following packet of length %d: \n", length);
		int j;
		for(j = 0; j < length; j++){
			//printf("%d: 0x%02x - %c\n", j, packet[j], packet[j]);
		}*/
		packet = makeDataPacket(PACKET_SIZE, buffer, &length);

		if(llwrite(port, packet, length) < 0 )
			return -1;
		buffer += PACKET_SIZE;

		acum += PACKET_SIZE;
		proBar = updateProgressBar(acum, size, &percentage);
		if(visMode != 0)
			printProgressBar(proBar, percentage);
		

	}

	if((size % PACKET_SIZE) != 0){
		packet = makeDataPacket((size % PACKET_SIZE), buffer, &length);
		/*//printf("gonna send following packet of length %d: \n", length);
		int j;
		for(j = 0; j < length; j++){
			//printf("%d: 0x%02x - %c\n", j, packet[j], packet[j]);
		}*/
		if(llwrite(port, packet, length) < 0 )
			return -1;
		acum += (size % PACKET_SIZE);
		
		proBar = updateProgressBar(acum, size, &percentage);
		if(visMode != 0)
			printProgressBar(proBar, percentage);
		
		
	}

	printf("\n");
	packet = makeControlPacket(size, filePath, 2, &length);
	llwrite(port, packet, length);


	llclose(port);

	if(munmap(bufferBckup, size) == -1)
		perror("failed to unmap the file");

		return 0;
}
Ejemplo n.º 9
0
int main (int argc, char ** argv) {
    ll.baudRate = 9600;
    ll.timeout = 1;
    ll.numTransmissions = 3;

    int port = 0;
    int flag = 0;
    int read_ret = 0;

    if (argc < 3) {
        printf("Usage:\t./app PortNumber flag\n\tex: ./app 5 1\n");
        exit(1);
    }

    port = atoi(argv[1]);
    flag = atoi(argv[2]);

    if(port > 5 || port < 0) {
        printf("Port number must be between 0 and 5!\n");
        exit(1);
    }

    if(flag != 0 && flag != 1) {
        printf("Flag must be 0 or 1!\n");
        exit(1);
    }

    if(flag==0) {
        printf("Write the name of the file you want to copy: ");
        strcpy(ctrData.filePath,"");
        gets(ctrData.filePath);
        ctrData.fpLength = strlen(ctrData.filePath);
    }

    char sPort[20];

    strcpy(sPort,"");
    sprintf(sPort, "/dev/ttyS%d",port);

    appLayer.status = flag;
    strcpy(ll.port,sPort);

    if(flag == 0)
        if(readData() != 0) {
            printf("\nError: File doesn't exist!\n\n");
            return -1;
        }

    if(llopen() == -1) {
        printf("Error llopen!\n");
        exit(1);
    }

    if(appLayer.status == 0) {
        if(create_packets_send() == -1) {
            printf("Error llwrite!\n");
            exit(1);
        }
    }
    else {
        int control = 0;
        while(TRUE) {
            read_ret = llread(control);

            if (read_ret == 1) {
                checkControl(read_ret-1);
                control = 1;
            }
            else if(read_ret == 2) {
                checkControl(read_ret-1);
            }
            else if(read_ret == 3) {
                if(llclose() != 0) {
                    printf("\n\nError: llclose()!\n\n");
                    exit(-1);
                }
            }
        }
    }

    llclose();

    return 0;
}
Ejemplo n.º 10
0
int main(int argc, const char * argv[])
{
  /*
    Verify if arguments are valid:
    - "/dev/ttyS0" is the server (computer on the right)
    - "/dev/ttyS4" is the client (computer on the left)
  */
  if ( (argc < 3) || ((strcmp("server", argv[1])!=0) && (strcmp("client"  , argv[1])!=0))
       || ((strcmp("send", argv[2])!=0) && (strcmp("get", argv[2])!=0))
       )
    {
      printf("4 ways to use:\n\texecutable server send\n\texecutable server get\n\texecutable client send\n\texecutable client send\n");
      exit(1);
    }
    
  char device[11]; 
  strcpy(device,"/dev/ttySX");
  int deviceN = 0;
    
  if ( strcmp(argv[1],"server")==0 )
    {
      device[9] = '0';
      deviceN   =  0;
    }
  else
    {
      device[9] = '4';
      deviceN   =  4;
    }
    
    
  initLinkLayer(&lnk);
  initAppLayer(&app);
    
  setLinkLayer(&lnk, device, BAUDRATE, 0, 0, 0, ""); // ver qual o valor para os outros valores a 0
    
  app.status = (strcmp(argv[2],"send")==0) ? TRANSMITTER : RECEIVER ;
  
  //--- llopen
  llopen(deviceN, app.status);
    
  if ( app.fileDescriptor == -1 ) // se o llopen falhar
  {
  		printf("Erro :: llopen :: Não foi possível estabelecer o protocolo de ligação.\n");
      exit(1);
  }
  
  int next =0;
  
  if (app.status == TRANSMITTER) 
  {  		
  		// constroi tramas de envio
  		
  		do 
  		{
  			// passa à proxima trama
  			if (next == 0) 
  			{
  				// trama++ 
  			}
  			
  			// llwrite
  			//// constroi a próxima trama para envio
  			//// envia trama com X tentativas e Y timeouts
  			//// 
  			//next = llwrite(app.fileDescriptor,trama,size_trama);
  			
  		} while (next >= 0);
  }
  else
  {
  		// constroi trama de recepcao
  		
  		do
  		{
  			// passa à proxima trama
  			if (next == 0) 
  			{
  				// trama++ 
  			}
  			
  			// llread
  			//next = llread(app.fileDescriptor,buffer);
  			
  		} while (next >= 0);
  }
	
  //--- llclose
  llclose(app.fileDescriptor);  
    
  return 0;
}