Example #1
0
int create_packets_send() {
    int i, timeout = 0;

    printf("llwrite: sending cp1!\n");
    while (timeout < ll.numTransmissions) {
        strcpy(ll.frame,"");
        strcpy(ctrData.frame,"");
        createCtrlPackets(0);
        if(llwrite(2,ctrData.frame,ctrData.sequenceNumber) != 0)
            printf("llwrite: error sending cp1! Try number %d\n",timeout+1);
        else
            break;
        timeout++;
    }

    if(timeout == 3) {
        printf("llwrite: timed out cp1!\n");
        return -1;
    }
    for(i = 1; i <= fileData.numSeg; i++) {
        strcpy(ll.frame,"");
        strcpy(fileData.frame,"");
        createDataPacket(i);
        timeout=0;
        while (timeout < ll.numTransmissions) {
            if(llwrite(2,fileData.frame, fileData.sequenceNumber) != 0)
                printf("llwrite: error sending dataFrame! Try number %d\n",timeout+1);
            else
                break;
            timeout++;
        }
        if(timeout == 3) {
            printf("llwrite: timed out data sending!\n");
            return -1;
        }
    }

    printf("llwrite: sending cp2!\n");
    timeout=0;
    while (timeout < ll.numTransmissions) {
        strcpy(ctrData.frame,"");
        strcpy(ll.frame,"");
        createCtrlPackets(1);
        if(llwrite(2, ctrData.frame, ctrData.sequenceNumber) != 0)
            printf("llwrite: error sending cp2! Try number %d\n",timeout+1);
        else
            break;
        timeout++;

    }
    if(timeout == 3) {
        printf("llwrite: timed out cp2!\n");
        return -1;
    }

    return 0;

}
Example #2
0
int app_send_control(Packet_Control control, Packet_Type type, int file_size) {

	int size = 3 * sizeof(char) + sizeof(int); // type + size + int
	
	unsigned char* buf = malloc(size);
	
	int fsize = file_size;
	unsigned char * tmp;
	tmp  = convert_int32_to_byte(fsize);
	
	buf[0] = control;
	buf[1] = type;
	buf[2] = 4;
	buf[3] = tmp[0];
	buf[4] = tmp[1];
	buf[5] = tmp[2];
	buf[6] = tmp[3];
	
	if(llwrite(buf, size) != OK) {
		printf("Could not send control packet\n");
		return ERROR;
	}
	
	return OK;
}
Example #3
0
int sendControl(int c, int fileSize) {

    int size = 3 * sizeof(char) + sizeof(int);

    unsigned char * control = malloc(size);
    unsigned char * temp;

    temp  = int32_to_byte(fileSize);

    control[0] = c;
    control[1] = 0;
    control[2] = 4;
    control[3] = temp[0];
    control[4] = temp[1];
    control[5] = temp[2];
    control[6] = temp[3];

    int i;
    for(i=0; i < size; i++ ) {
        toSend.buf[i] = control[i];
    }
    toSend.length = size;
    llwrite(toSend);
    return 0;
}
Example #4
0
int sendData(int dataLength, char* sendData) {
    data toSend;
    int totalLength = dataLength + 4;
    unsigned char n = (unsigned char)appDef.seqNr;

    char l2 = (char)(dataLength / 256);
    char l1 =  (char)(dataLength % 256);

    toSend.buf[0] = DATA_PAC;
    toSend.buf[1] = n;
    toSend.buf[2] = l2;
    toSend.buf[3] = l1;

    int i;
    for(i = 0; i < dataLength; i++) {
        toSend.buf[i + 4] = sendData[i];
    }


    toSend.length = totalLength;

    if( llwrite(toSend) == -1) {
        return -1;
    }

    return 0;
}
Example #5
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;
}
Example #6
0
//****** cnwrite *********************
size_t cnwrite(uint8_t* buf, size_t bytes, int16_t fd)
{
	fd_entry* fde = &fd_tbl[fd];
	check(fde->state == FD_WRITE, "File descriptor not in write mode");

	uint32_t required_size = fde->cursor + bytes;
	if(fde->inode.size < required_size)   //The data cache and block size may have to be expanded
	{
		//Compute new sizes
		uint32_t required_blocks = (required_size / BLOCK_SIZE) + 1;

		//Allocate new data cache
		check(realloc_cache(fde, required_blocks) == 0, "Unable to realloc cache");
		fde->inode.size = required_size;

		//Allocate fs blocks
		if(fde->inode.blocks < required_blocks)
		{
			check(realloc_fs_blocks(&fde->inode, required_blocks) == 0, "Could not allocate fs blocks");
		}
		fde->inode.modified = time(NULL);
		inode_write(fde->inode_id, &fde->inode);
	}

	uint8_t* data_ptr = ((uint8_t*)fde->data)+fde->cursor;
	memcpy(data_ptr, buf, bytes);
	fde->cursor += bytes;
	llwrite(&fde->inode, (block*)fde->data);

	return bytes;
error:
	return 0;
}
Example #7
0
int app_send_data(int sequence_nr, int data_field_size, char* data) {

	int final_size = data_field_size + 4;
	
	char* buf = malloc(final_size);

	char n = (char)((pack.sequence_nr) % 255);
	
	char l2 = (char)(data_field_size / 256);
	char l1 =  (char)(data_field_size % 256);
	
	buf[0] = PACKET_DATA;
	buf[1] = n;
	buf[2] = l2;
	buf[3] = l1;
	
	int i;
	for(i = 0; i < data_field_size; i++) {
		buf[i + 4] = data[i];
	}
	
	if( llwrite(buf, final_size) != OK) {
		printf("> Could not write data packet nr %c\n", n);
		return ERROR;
	}
	
	return OK;
}
Example #8
0
int main(int argc, char** argv)
{
    int fd, res;
    struct termios oldtio,newtio;
	unsigned char * buffer;
	unsigned char UA[UALENGT]; 

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

      
    fd = open(argv[1], O_RDWR | O_NOCTTY );
    if (fd <0) {perror(argv[1]); exit(-1); }

    tcgetattr(fd,&oldtio); /* save current port settings */

    bzero(&newtio, sizeof(newtio));
    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR;
    newtio.c_oflag = 0;

    /* set input mode (non-canonical, no echo,...) */
    newtio.c_lflag = 0;

    newtio.c_cc[VTIME]    = 0.1;   /* inter-character timer unused */
    newtio.c_cc[VMIN]     = 0;   /* blocking read until 1 chars received */

    tcflush(fd, TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);

    printf("New termios structure set\n");

	res = llread(fd, buffer);
	
	if(res < 0)
	{
		printf("Error recieving msg!");
		exit(1);
	}

	UA[0] = F;
	UA[1] = A;
	UA[2] = 0x08;
	UA[3] = F;
	UA[4] = A;
	UA[5] = Cr;
	UA[6] = A^Cr;
	UA[7] = F;	

	res = llwrite(fd,UA, UALENGT);

    tcsetattr(fd,TCSANOW,&oldtio);
    close(fd);
    return 0;
}
Example #9
0
//******** rmdir ********************
int8_t cnrmdir(const char* name)
{
	char parent_name[512];
	char entry_name[256];
	dir_entry* entry;
	inode dir_inode;
	memset(&dir_inode, 0, sizeof(inode));

	strcpy(parent_name, name);
	strcat(parent_name, "/..");

	dir_ptr* parent = cnopendir(parent_name);
	check(parent != NULL, "Cannot open parent directory");

	//Copy the entire directory minus the entry
	dir_ptr* new_parent = calloc(1,sizeof(dir_ptr));
	new_parent->data = calloc(parent->inode_st.blocks, sizeof(block));
	dir_entry* new_dir_entry = (dir_entry*)new_parent->data;
	new_parent->inode_st = parent->inode_st;
	new_parent->inode_id = parent->inode_id;
	new_parent->inode_st.size = 0;
	new_parent->index = 0;

	while((entry = cnreaddir(parent)))
	{
		memcpy(entry_name, entry->name, entry->name_len);
		entry_name[entry->name_len] = 0;
		if(strcmp(entry_name, name) == 0)  //If this is the directory we want
		{
			inode_read(entry->inode, &dir_inode);
			check(dir_inode.size == 24, "Directory is not empty");
			release_block(dir_inode.data0[0]);  //Release target directory block
			release_inode(entry->inode);        //Release target inode
			continue;
		}
		new_dir_entry->entry_len = entry->entry_len;
		new_dir_entry->name_len = entry->name_len;
		new_dir_entry->file_type = entry->file_type;
		new_dir_entry->inode = entry->inode;
		memcpy(new_dir_entry->name, entry->name, entry->name_len);
		new_parent->inode_st.size += entry->entry_len;
		new_parent->index += entry->entry_len;
		new_dir_entry = (dir_entry*)((uint8_t*)new_parent->data+new_parent->index);
	}

	inode_write(new_parent->inode_id, &new_parent->inode_st);
	llwrite(&new_parent->inode_st, new_parent->data);

	free(new_parent->data);
	free(new_parent);
	cnclosedir(parent);
	return 0;
error:
	if(new_parent != NULL && new_parent->data != NULL) free(new_parent->data);
	if(new_parent != NULL) free(new_parent);
	if(parent != NULL) cnclosedir(parent);
	return -1;
}
Example #10
0
int send_rr_app(int equalize, int segmentNumber, char* buf) {
    int i = 0;

    if(equalize == 0) {
        for(i = 0; i < ll.sequenceNumber; i++)
            ll.compFrame[i] = buf[i];
        return llwrite(equalize,buf,0);
    }
    else if (equalize == 1) {
        if(checkFrames(buf) == 0) {
            if(segmentNumber > 0)
                saveChunk(buf,ll.sequenceNumber);
        }
        return llwrite(equalize,buf,0);
    }

    return -1;
}
Example #11
0
int OFile::printf(const char*fmt,...){
  va_list arg;
  va_start(arg, fmt);
  int r=std::vsnprintf(&buffer[actual_buffer_length],buflen-actual_buffer_length,fmt,arg);
  va_end(arg);
  if(r>=buflen-actual_buffer_length){
    int newlen=buflen;
    while(newlen<=r+actual_buffer_length) newlen*=2;
    char* newbuf=new char [newlen];
    memmove(newbuf,buffer,buflen);
    for(int k=buflen;k<newlen;k++) newbuf[k]=0;
    delete [] buffer;
    buffer=newbuf;
    buflen=newlen;
    va_list arg;
    va_start(arg, fmt);
    r=std::vsnprintf(&buffer[actual_buffer_length],buflen-actual_buffer_length,fmt,arg);
    va_end(arg);
  }
  plumed_massert(r>-1 && r<buflen-actual_buffer_length,"error using fmt string " + std::string(fmt));

// Line is buffered until newline, then written with a PLUMED: prefix
  char*p1=buffer;
  char*p2;
// newline is only searched in the just added portion:
  char*psearch=p1+actual_buffer_length;
  actual_buffer_length+=r;
  while((p2=strchr(psearch,'\n'))){
    if(linePrefix.length()>0) llwrite(linePrefix.c_str(),linePrefix.length());
    llwrite(p1,p2-p1+1);
    actual_buffer_length-=(p2-p1)+1;
    p1=p2+1;
    psearch=p1;
  };
  if(buffer!=p1) memmove(buffer,p1,actual_buffer_length);
  return r;
}
//Escreve o RR de resposta ao I, devolvendo o numero de bytes escritos
int writeRR(int fd, int cnr){
	char buffer[5];
	int res;
	buffer[0]=FLAG;
	buffer[1]=A;
	buffer[2]=cnr;
	buffer[3]=A^cnr;
	buffer[4]=FLAG;

	res = llwrite(fd,buffer,5);
	if(res>0)
		printf("Resposta enviada (RR)\n");
	else
		exit(1);
	return res;
}
//Escreve o UA de resposta para o SET, devolvendo o numero de bytes escritos
int writeUA(int fd){
	char buffer[5];
	int res;
	buffer[0]=FLAG;
	buffer[1]=A;
	buffer[2]=CUA;
	buffer[3]=A^CUA;
	buffer[4]=FLAG;

	res = llwrite(fd,buffer,5);
	if(res>0)
		printf("Resposta enviada (UA)\n");
	else
		exit(1);
	return res;
}
//Escreve o DISC de resposta, devolvendo o numero de bytes escritos
int writeDISC(int fd){
	char buffer[5];
	int res;
	buffer[0]=FLAG;
	buffer[1]=A2;
	buffer[2]=CDISC;
	buffer[3]=(A2^CDISC);
	buffer[4]=FLAG;

	res = llwrite(fd,buffer,5);
	if(res>0)
		printf("Comando enviado (DISC)\n");
	else
		exit(1);
	return res;
}
Example #15
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;
}
Example #16
0
int llwrite(int fd, unsigned char* buffer, int length){

//printf("entrei no llwrite\n");
	char *bufferStuffed;
	char header[] = { FLAG, 0x03, I(ll.sequenceNumber), header[1]^header[2] };
	char dataBCC = generateBCC((char*)buffer, length);
	char* toStuff = malloc(length+1);
	memcpy(toStuff, buffer, length);
	toStuff[length] = dataBCC;


	int n= byteStuffing(toStuff,  length+1, &bufferStuffed);

	//int k;
	//puts("");
	//for (k=0;k<n;k++){
		//printf("\n%d\n", bufferStuffed[k]);
	//}
	//	puts("");
	char* message = (char*)  malloc(n+6);

	memcpy(message, header, 4);
	memcpy(message+4, bufferStuffed, n);
	message[n+4] = FLAG;
	int wrote = 0;
	while(!wrote)
		wrote = write(fd, message, n+5);
	stats.dataFramesTransmitted++;



//puts("message sent\n");

	free(message);
	free(bufferStuffed);
	alarm(ll.timeOut);
	
	Command command = receiveCommand(fd);
	
	//was asked for new frame
	if (command.command == RR(!ll.sequenceNumber)){
		ll.sequenceNumber = (!ll.sequenceNumber);
		retries = 0;
	//puts("fui confirmado");
		alarm(0);
		return length;
	}
	//frame was rejected, resend
	if (command.command == REJ(ll.sequenceNumber) || (command.command == NONE && ll.numTransmissions > retries)){
		if (command.command == NONE){
			
			stats.timeouts++;
			//printf("\nNumber of retries: %d\n", retries);
			alarm(0);
		}
		//puts("byte was rejected,resending or no response\n");
		stats.rejs++;
		return llwrite(fd, buffer, length);
	}
	else{
		//puts("eu esperei mas o reader nao me quis responder :(");
		return -1;
	}

	return length;
}
Example #17
0
int sendControlPacket(int fd, char *controlPacket, int sizeControlPacket) {
	unsigned char try = 0;
	while (try < MAX_TRY) {
		if (llwrite(fd, controlPacket, sizeControlPacket) > 0) return OK;
		try++;
	}
	return -1;
}

int sendInfoPacket(int fd, char *infoPacket, int sizeInfoPacket) {
	unsigned char try = 0;
	while (try < MAX_TRY) {
		if (llwrite(fd, infoPacket, sizeInfoPacket) > 0) return OK;
		try++;
	}
	return -1;
}

int sendFile(int fd, unsigned char fileNameSize, const char *fileName, unsigned int image_bytes_length, const char *image_bytes, unsigned  int* out_already_sent_bytes) {
	char controlPacket[MAX_CTRL_P];
	int sizeControlPacket;
	char infoPacket[MAX_INFO_P];
	int sizeInfoPacket;
	char info[L2 * 256 + L1]; 		// maximum size of info
	unsigned int infoSize;
	unsigned int maxInfoSize = L2 * 256 + L1;
	unsigned char N = 0;
	/*struct stat st;
	stat(fileName, &st);
	unsigned int fileSize = st.st_size;		// get file size from file statistics
	printf("File size: %d\n", fileSize);
	FILE *file;
	file = fopen(fileName, "r");
	sizeControlPacket = getControlPacket(START, fileSize, fileNameSize, fileName, controlPacket); // START control packet
	if (sendControlPacket(fd, controlPacket, sizeControlPacket) == OK) {
	int fileChar;
	while (1) {
	infoSize = 0;
	while ((fileChar = fgetc(file)) != EOF && infoSize < maxInfoSize) {
	info[infoSize] = (char) fileChar;
	infoSize++;
	}
	if (infoSize == 0) break;
	sizeInfoPacket = getInfoPacket(N, infoSize, info, infoPacket);
	if (sendInfoPacket(fd, infoPacket, sizeInfoPacket) == OK) N++;
	else {
	fclose(file);
	return -1;
	}
	}*/

	//(unsigned int)image_bytes_length: this casting is not the ideal solution but works for now
	sizeControlPacket = getControlPacket(START, (unsigned int)image_bytes_length, fileNameSize, fileName, controlPacket); // START control packet
	if (sendControlPacket(fd, controlPacket, sizeControlPacket) != OK)
		return -1;

	//printf("\n--fileName;%s\nimglength:%l\n", fileName, image_bytes_length);
	long i = 0;
	while (1) {
		infoSize = 0;
		while (i < image_bytes_length && infoSize < maxInfoSize) {
			info[infoSize] = image_bytes[i];
			infoSize++;
			i++;
		}
		if (infoSize == 0) break;
		sizeInfoPacket = getInfoPacket(N, infoSize, info, infoPacket);
		if (sendInfoPacket(fd, infoPacket, sizeInfoPacket) == OK)
		{
			++out_already_sent_bytes;
			N++;
		}
		else {
			//fclose(file);
			return -1;
		}
	}
	controlPacket[0] = CE; // END control packet
	if (sendControlPacket(fd, controlPacket, sizeControlPacket) == OK)
		return OK;

	return -1;
	/*
	controlPacket[0] = CE; // END control packet
	if (sendControlPacket(fd, controlPacket, sizeControlPacket) == OK) {
	fclose(file);
	return OK;
	}
	else {
	fclose(file);
	return -1;
	}
	}
	else return -1;	*/
}