Пример #1
0
int main(int argc, char** argv)
{
  struct applicationLayer app;
  app.status = 0;
  //char buf[256];

    if ( (argc < 2) || 
  	     ((strcmp("/dev/ttyS0", argv[1])!=0) && 
  	      (strcmp("/dev/ttyS4", argv[1])!=0) )) {
      printf("Usage:\tnserial SerialPort\n\tex: nserial /dev/ttyS0\n");
      exit(1);
    }

  /*
    Open serial port device for reading and writing and not as controlling tty
    because we don't want to get killed if linenoise sends CTRL-C.
  */
   
    set_basic_definitions(3, 3, argv[1], BAUDRATE);
    if(open_tio(&app.fileDescriptor,0,0)!=OK)
    {
      printf("\nERROR:Couldnot open terminal\n");
      exit(1);
    }
    
    llopen( app.fileDescriptor , APP_STATUS_TRANSMITTER);
    
    close_tio(app.fileDescriptor);


    return 0;
}
Пример #2
0
int main(int argc, char** argv)
{
	FILE *file;
	char data[150000];
	int fd, i;
	fd = llopen(0);
	int filesize;

	unsigned char firstData[10000];

	estabelecimento(fd, firstData);

	filesize = transfDados(fd, firstData, data, filesize);
	terminacao(fd);
	/*printf("filename: \n");
	for(i=0; i<strlen(filename); i++){
		printf("%c",filename[i]);
	}

	printf("Dados:\n ");
	for(i=0;i<filesize;i++){
		printf("%x ",data[i]);
	}*/
	file=open(filename+1, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
	write(file,data,filesize);
	close(file);

	llclose(fd);
	return 0;
}
Пример #3
0
int main (int argc, char** argv) {
	int txrx;
	
	
	linkLayer.fd = config(argv[1]);
	//write(linkLayer.fd, "ola", 3);
	printf("Reciver - 0\nTransmitter -1\n");
	scanf("%d", &txrx);
	
	printf("[MAIN] Opening llopen with %d\n", txrx);
	llopen(linkLayer.fd, txrx);
	printf("[MAIN] LLOPEN SUCCESFULL\n");
	
	if(txrx == TRANSMITTER)
	{
		unsigned char buffer[256] = "Eu Sou o jose delfim ribeiro valverdeasdasdasdasdasfasfsadfasdasdasdsafsdfasdfsadas ";
		llwrite(linkLayer.fd, buffer, 60);
	}
	else
	{
		char buffer[3];
		llread(fd,buffer);
		puts(buffer);
	}
	
	return 1;
}
Пример #4
0
int main(int argc, char** argv)
{
    
    if ( (argc < 2) || 
  	     ((strcmp("/dev/ttyS0", argv[1])!=0) && 
  	      (strcmp("/dev/ttyS4", argv[1])!=0) )) {
      printf("Usage:\tnserial SerialPort\n\tex: nserial /dev/ttyS1\n");
      exit(1);
    }

	int port;
	sscanf(argv[1],"/dev/ttyS%d",&port);

	int fd = llopen(port,TRANSMITTER);

	if(fd < 0){
		printf("error on llopen.\n");
		return -1;
	}
	
	//llwrite(fd,buffer,length);  
	
	if(llclose(fd,TRANSMITTER) < 0){
		printf("error on llclose.\n");
		return -1;
	}
	printf("acabou.\n");
	return 0;
    
}
Пример #5
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[] = "/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 ;
    app.fileDescriptor = llopen(deviceN, app.status);
    
    if ( app.fileDescriptor == -1 )
    {
        print("Erro :: llopen :: Não foi possível estabelecer o protocolo de ligação.\n");
        exit(1);
    }
    
    
    // llwrite(int id, char * buffer, int length);
    // llread(int porta, size_t flag);
    
    
    llclose(app.fileDescriptor);
    
    //send_nc(argv[1]);
    
    //receive_nc(argv[1]);
    
    return 0;
}
Пример #6
0
int appRead(char * port, char * filename) {
    printf("[appSend] Connecting to Sender...n");
    if(llopen(port, RECEIVER) < 0)
        return -1;

    char packet[256];
    pingu.size = 0;
    int psize = 0;
    int tsize = 0;



    printf("[appRead] CONNECTEDn");

    do {
        if(llread(packet)!=-1) {

            if(checkPacket(packet, &psize) != 0) {
                printf("[appRead] Unrecognized packet typen");
                return -1;
            }
            else {
                if(appDef.control == START_PAC) {
                    FILE *p = fopen(filename, "wb");
                    if( p < 0 ) {
                        printf("[appRead] Error opening output filen");
                        return -1;
                    }
                    pingu.p = p;
                }
                else if(appDef.control == DATA_PAC) {
                    fwrite(packet+4, sizeof(char), psize, pingu.p);
                    tsize += psize;
                }
                else if(appDef.control == END_PAC) {
                    //
                    break;
                }
            }

        }
    } while((pingu.size != tsize) || (appDef.control != END_PAC));

    printf("[appRead] Bytes read: [ %d / %d ]n", tsize, pingu.size);

    printf("[appRead] Closing connection...n");

    llclose(appDef.status);
    printf("[appSend] Exiting Application!n");

    return 0;
}
Пример #7
0
int connect()
{
      if(open_tio(&app.fd,0,0)!=OK)
    {
      printf("\nERROR: Could not open terminal\n");
      exit(1);
    }
  
  
	  if (llopen(app.fd, APP_STATUS_RECEIVER) < 0) return -1;

	  return 0;

}
Пример #8
0
int connect()
{
	if (open_tio(&app.fd, 0, 0) != OK)
	{
		printf("\nERROR: Could not open terminal\n");
		exit(1);
	}


	if (llopen(app.fd, app.status) < 0) return -1;

	return 0;

}
Пример #9
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;   
}
Пример #10
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");
}
Пример #11
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;
}
Пример #12
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;
}
Пример #13
0
//1- porta
//2- modo (SEND | RECEIVE)
//3- filepath
int main(int argc, char **argv){

	//int fdesc = llopen(argv[1], SEND);
	//exit(-1);
	setbuf(stdout, NULL);

	ll.timeOut = 3;
	ll.sequenceNumber = 0;
	ll.numTransmissions  = 3;
	int mode;

	if(argc == 2 && !strcmp("--help", argv[1])){
		printTutorial();
		exit(0);
	}

	if(argc >= 3){
		if(!strcmp(argv[2], "send"))
			mode = 0;
		else if(!strcmp(argv[2], "receive")){
			mode = 1;
		}
		else{
			printUsage(argv[0]);
			exit(-1);
		}
	}
	else 
		exit(-1);
	if( (argc < 4 && mode == SEND) || (argc<3 && mode == RECEIVE) || strncmp(argv[1], "/dev/ttyS", strlen("dev/ttyS"))) {
		printUsage(argv[0]);
		exit(-1);
	}

	if((argc > 4 && mode == SEND) || (argc > 3 && mode == RECEIVE)){
		int i;
		for(i = 0; i < argc-(mode==SEND?4:3); i++){
			if(parseParams(argv[i+(mode==SEND?4:3)]) == -1)
				return -1;
		}
	}	
	

	int fd;
	if (mode == SEND && (fd = open(argv[3], O_RDONLY)) == ENOENT){
		perror("");
		exit(-1);
	}

	int serialPort = llopen(argv[1], mode);
	if (serialPort < 0){
		perror("");
		exit(-1);
	}

	int result;



	if (mode == SEND){

		result = sendFile(serialPort, fd, argv[3]);
	}
	else if (mode == RECEIVE){
		result = readFile(serialPort);
	}

	if(result){
		perror("could not transmit file");
		return result;
	}
	printStatistics(visMode);
	return 0;
}
Пример #14
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;
}
Пример #15
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;
}