int llclose_transmitter(int fd) { info->tentativas = tentativas; while(info->tentativas > 0) { buildFrame(info->flag, "disc"); transmitirFrame(info->frameSend, info->frameSendLength); start_alarm(); info->frameTempLength = readFrame(info->frameTemp); char * type = malloc(5); type = verifyFrameType(info->frameTemp); if (verifyFrame(info->frameTemp, info->frameTempLength, "disc")) { buildFrame(info->flag, "ua"); if(transmitirFrame(info->frameSend, info->frameSendLength)) break; } } if (info->tentativas == 0) { printf("Número de tentativas chegou ao fim. \n"); exit(-1); } sleep(1); if ( tcsetattr(info->fd,TCSANOW,&info->oldtio) == -1) { perror("tcsetattr"); return -1; } close(fd); printf("fechou transmissor\n"); return 1; }
Assets::Md2Model::FrameList Md2Parser::buildFrames(const Md2FrameList& frames, const Md2MeshList& meshes) { Assets::Md2Model::FrameList modelFrames; modelFrames.reserve(frames.size()); Md2FrameList::const_iterator it, end; for (it = frames.begin(), end = frames.end(); it != end; ++it) { const Md2Frame& frame = *it; Assets::Md2Model::Frame* modelFrame = buildFrame(frame, meshes); modelFrames.push_back(modelFrame); } return modelFrames; }
int llclose_receiver(int fd) { info->tentativas = tentativas; while(1) { info->frameTempLength = readFrame(info->frameTemp); char * type = malloc(5); type = verifyFrameType(info->frameTemp); if (type == "I0" || type == "I1") { if (verifyFrame(info->frameTemp, info->frameTempLength, type)) { char * typeRR = malloc(5); sprintf(typeRR, "rr%d", !info->sequenceNumber); //printf("criar frame de %s \n", typeRR); buildFrame(info->flag, typeRR); transmitirFrame(info->frameSend, info->frameSendLength); free(typeRR); int j; //printf("dados recebidos: "); for(j = 0; j < (info->frameTempLength-6); j++) { info->dados[j] = info->frameTemp[4+j]; //printf(" %x ", info->dados[j]); } //printf("\n"); info->lengthDados = j; continue; } else { char * typeREJ = malloc(5); sprintf(typeREJ, "rej%d", !info->sequenceNumber); //printf("criar frame de %s \n", typeREJ); buildFrame(info->flag, typeREJ); transmitirFrame(info->frameSend, info->frameSendLength); free(typeREJ); continue; } } else if (verifyFrame(info->frameTemp, info->frameTempLength, "disc")) { buildFrame(info->flag, "disc"); transmitirFrame(info->frameSend, info->frameSendLength); start_alarm(); info->frameTempLength = readFrame(info->frameTemp); type = verifyFrameType(info->frameTemp); if (verifyFrame(info->frameTemp, info->frameTempLength, "ua")) { break; } } else { printf("llclose_receiver não recebeu nem I nem disc \n"); } } if ( tcsetattr(info->fd,TCSANOW,&info->oldtio) == -1) { perror("tcsetattr"); return 0; } close(fd); printf("fechou recetor\n"); return 1; }
int llread(int fd, char * buffer) { //printf("iniciar llread \n"); while(1) { info->frameTempLength = readFrame(info->frameTemp); char * type = NULL; type = verifyFrameType(info->frameTemp); if (type == "set") { buildFrame(info->flag, "ua"); transmitirFrame(info->frameSend, info->frameSendLength); continue; } else if (type == "I0" || type == "I1") { destuffing(info->frameTemp, &info->frameTempLength); //fprintf(stderr,"Destuffing "); /*int bb; for(bb = 0; bb < info->frameTempLength; bb++){ fprintf(stderr,"0x%x ", info->frameTemp[bb]); } fprintf(stderr,"\n");*/ if (verifyFrame(info->frameTemp, info->frameTempLength, type)) { if(type == "I0" && !info->sequenceNumber || type == "I1" && info->sequenceNumber) { //printf("recebeu a trama I correspondente aos sequenceNumber %d \n", info->sequenceNumber); char * typeRR = malloc(5); sprintf(typeRR, "rr%d", !info->sequenceNumber); //printf("criar frame de %s \n", typeRR); buildFrame(info->flag, typeRR); transmitirFrame(info->frameSend, info->frameSendLength); free(typeRR); info->sequenceNumber = !info->sequenceNumber; } else { char * typeRR = malloc(5); sprintf(typeRR, "rr%d", info->sequenceNumber); //printf("criar frame de %s \n", typeRR); buildFrame(info->flag, typeRR); transmitirFrame(info->frameSend, info->frameSendLength); free(typeRR); continue; } int j; //printf("frameTempLength: %d\n", info->frameTempLength); //printf("dados recebidos: "); for(j = 0; j < (info->frameTempLength-6); j++) { info->dados[j] = info->frameTemp[4+j]; //printf(" %x ", info->dados[j]); buffer[j] = info->dados[j]; //printf(" %x \n", info->dados[j]); } //printf("\n"); info->lengthDados = j; } else { char * typeREJ = malloc(5); sprintf(typeREJ, "rej%d", !info->sequenceNumber); //printf("criar frame de %s \n", typeREJ); buildFrame(info->flag, typeREJ); transmitirFrame(info->frameSend, info->frameSendLength); free(typeREJ); continue; } } break; } return 1; }
int llopen(char * porta, int flag) { printf("FLAG (TRANS/REC) = %d \n", flag); info = malloc(sizeof(struct Info)); info->dados = malloc(255); info->frameTemp = malloc(255); info->frameSend = malloc(255); info->timeout = timeOut; install_handler(atende, info->timeout); //printf("sequenceNumber: %d \n", info->sequenceNumber); info->tentativas = tentativas; info->flag = flag; info->endPorta = malloc(255); info->endPorta = porta; info->fd = open(info->endPorta, O_RDWR | O_NOCTTY); if (info->fd < 0) { perror(info->endPorta); exit(-1); } if ( tcgetattr(info->fd,&info->oldtio) == -1) { // save current port settings perror("tcgetattr"); return -1; } bzero(&info->newtio, sizeof(info->newtio)); info->newtio.c_cflag = BaudRate | CS8 | CLOCAL | CREAD; info->newtio.c_iflag = IGNPAR; info->newtio.c_oflag = OPOST; // set input mode (non-canonical, no echo,...) info->newtio.c_lflag = 0; info->newtio.c_cc[VTIME] = 0; // inter-character timer unused info->newtio.c_cc[VMIN] = 1; // blocking read until 5 chars received tcflush(info->fd, TCIFLUSH); if ( tcsetattr(info->fd,TCSANOW,&info->newtio) == -1) { perror("tcsetattr"); return -1; } if (flag == TRANSMITTER) { printf("llopen de transmissor \n"); buildFrame(flag, "set"); transmitirFrame(info->frameSend, info->frameSendLength); while(info->tentativas > 0) { //printf("tentativasOpen = %d \n", info->tentativas); start_alarm(); info->frameTempLength = readFrame(info->frameTemp); if (verifyFrame(info->frameTemp, info->frameTempLength, "ua")) { stop_alarm(); info->tentativas = tentativas; return 1; } } if (info->tentativas == 0) { printf("Número de tentativas chegou ao fim. \n"); exit(-1); } } else { printf("llopen de recetor \n"); info->frameTempLength = readFrame(info->frameTemp); if (verifyFrame(info->frameTemp, info->frameTempLength, "set")) { buildFrame(flag, "ua"); transmitirFrame(info->frameSend, info->frameSendLength); //printf("terminar llopen recetor \n"); return 1; } } return info->fd; }