int main(int argc, char *argv[]) { int status = 0; int length = 0; networkAddress address = { { 0, 0, 0, 0, 0, 0 } }; networkFilter filter; unsigned char *buffer = NULL; int count, bytes; setlocale(LC_ALL, getenv(ENV_LANG)); textdomain("telnet"); if (argc != 2) { usage(argv[0]); return (status = ERR_ARGUMENTCOUNT); } // Parse the supplied network address into our networkAddress structure. length = strlen(argv[1]); // Replace dots ('.') with NULLS for (count = 0; count < length; count ++) if (argv[1][count] == '.') argv[1][count] = '\0'; // Get the decimal value of up to 4 numbers for (count = 0; count < 4; count ++) { status = atoi(argv[1]); if (status < 0) { usage(argv[0]); return (status = ERR_INVALID); } address.bytes[count] = status; argv[1] += (strlen(argv[1]) + 1); } buffer = malloc(NETWORK_PACKET_MAX_LENGTH); if (!buffer) { errno = ERR_MEMORY; perror(argv[0]); return (status); } // Clear out our filter and ask for the network the headers we want memset(&filter, 0, sizeof(networkFilter)); filter.transProtocol = NETWORK_TRANSPROTOCOL_TCP; filter.localPort = 12468; filter.remotePort = 23; printf("Telnet %d.%d.%d.%d\n", address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]); // Open a connection on the adapter connection = networkOpen(NETWORK_MODE_READWRITE, &address, &filter); if (!connection) { errno = ERR_IO; perror(argv[0]); free(buffer); return (status = errno); } // Set up the signal handler for catching CTRL-C interrupt if (signal(SIGINT, &interrupt) == SIG_ERR) { errno = ERR_NOTINITIALIZED; perror(argv[0]); networkClose(connection); free(buffer); return (status); } //if (0) { // Send some commands //sendCommand(TELNET_COMMAND_DO, TELNET_OPTION_SUPGA); //sendCommand(TELNET_COMMAND_WILL, TELNET_OPTION_TTYPE); while (!stop) { bytes = networkCount(connection); if (bytes < 0) break; if (bytes) { bytes = networkRead(connection, buffer, NETWORK_PACKET_MAX_LENGTH); if (bytes < 0) break; for (count = 0 ; count < bytes; count ++) { if ((unsigned char) buffer[count] == TELNET_COMMAND_IAC) { count ++; printCommand(buffer[count]); switch ((unsigned char) buffer[count]) { case TELNET_COMMAND_WILL: case TELNET_COMMAND_WONT: case TELNET_COMMAND_DO: case TELNET_COMMAND_DONT: count ++; printOption(buffer[count]); printCommand(TELNET_COMMAND_WONT); printOption(buffer[count]); sendCommand(TELNET_COMMAND_WONT, buffer[count]); break; default: break; } } else printf("%c", buffer[count]); } } multitaskerYield(); } } free(buffer); status = networkClose(connection); if (status < 0) { errno = status; perror(argv[0]); return (status); } return (status = 0); }
readClock(&timeCopy); timeCopy=sumClock(timeCopy,timeProt.offset);//add offset writeClock(timeCopy); if(timeProt.offset.sign==true){ sprintf(messageHMIOffset," update clock offset: +%lus,%ld[+%lus,%lu ms,~%lu us] t: %lu,%lu",timeProt.offset.second,(long unsigned int)timeProt.offset.halfmillis,timeProt.offset.second,timeProt.offset.halfmillis/32,(timeProt.offset.halfmillis%32)*31,timeCopy.second,timeCopy.halfmillis); } else{ sprintf(messageHMIOffset," update clock offset: -%lus,%ld[-%lus,%lums,~%lu us] t: %lu,%lu",timeProt.offset.second,(long unsigned int)timeProt.offset.halfmillis,timeProt.offset.second,timeProt.offset.halfmillis/32,(timeProt.offset.halfmillis%32)*31,timeCopy.second,timeCopy.halfmillis); } sendHMI(messageHMIOffset); } void receiver(){ #ifdef SLAVEMODE Clock t2={0,0,true}; Clock t3={0,0,true}; Clock t2subt1={0,0,true}; Clock t4subt3={0,0,true}; Clock delayL={0,0,true}; Clock timeMaster={0,0,true}; uint8_t i=0; #endif Data data; if(networkAvailable()){ vTaskDelay(100); if(networkRead()==HEADER){ data.id=networkRead(); data.type=(Type)networkRead(); switch (data.type){ #ifdef SLAVEMODE case SYNC: sendHMI(" sync receive"); data.length=networkRead(); for(i=0;i<data.length;i++){ data.data[i]=networkRead(); } data.crc=networkRead(); //networkFlush(); if(calculCRCID(data.type,data.length,data.data,data.id)!=data.crc){ sendHMI("erreur crc"); return; } else{ timeMaster.second=data.data[0]|(data.data[1]<<8); timeMaster.halfmillis=data.data[2]|(data.data[3]<<8); timeMaster=sumClock(timeMaster,timeProt.delay); timeProt.offset=subClock(timeMaster,timeProt.rxSync);//on calcul le temps de calcul updateClock(); sendHMI(" sync completed"); networkFlush(); } break; case DELAYRESPONSE: data.length=networkRead(); for(i=0;i<data.length;i++){ data.data[i]=networkRead(); } data.crc=networkRead(); //networkFlush(); if(calculCRCID(data.type,data.length,data.data,data.id)!=data.crc){ networkFlush(); sendHMI("erreur crc"); return; } sendHMI(" delay response receive"); if(data.data[0]==IDDEVICE){ //time receive master t2.second=data.data[1]|(data.data[2]<<8); t2.halfmillis=data.data[3]|(data.data[4]<<8); //time send master t3.second=data.data[5]|(data.data[6]<<8); t3.halfmillis=data.data[7]|(data.data[8]<<8); t2subt1=subClock(t2,timeProt.tx); t4subt3=subClock(timeProt.rxDelay,t3); /* if(timeProt.tx.sign!=true){ printf("neg "); } printf("t1: %lu s,%u\r\n",timeProt.tx.second,(unsigned int)timeProt.tx.halfmillis/2); if(t2.sign!=true){ printf("neg "); } printf("t2: %lu s,%u\r\n",t2.second,(unsigned int)t2.halfmillis/2); if(t3.sign!=true){ printf("neg "); } printf("t3: %lu s,%u\r\n",t3.second,(unsigned int)t3.halfmillis/2); if(timeProt.rxDelay.sign!=true){ printf("neg "); } printf("t4: %lu s,%u\r\n",timeProt.rxDelay.second,(unsigned int)timeProt.rxDelay.halfmillis/2); if(t4subt3.sign!=true){ printf("neg "); } printf("Tsubt3: %lu s,%u\r\n",t4subt3.second,(unsigned int)t4subt3.halfmillis/2); if(t2subt1.sign!=true){ printf("neg "); } printf("t2subt1: %lu s,%u\r\n",t2subt1.second,(unsigned int)t2subt1.halfmillis/2); */ delayL=sumClock(t2subt1,t4subt3); timeProt.delay.second=delayL.second/2; timeProt.delay.halfmillis=delayL.halfmillis/2; timeProt.offset=subClock(t2subt1,t4subt3); timeProt.offset.second=timeProt.offset.second/2; timeProt.offset.halfmillis=timeProt.offset.halfmillis/2; //updateClock(); if(timeProt.delay.sign==true){ sprintf(messageHMIDelay," update delay: %lu s,%u",timeProt.delay.second,(unsigned int)timeProt.delay.halfmillis/2); } else{ sprintf(messageHMIDelay," update delay: -%lu s,%u",timeProt.delay.second,(unsigned int)timeProt.delay.halfmillis/2); } sendHMI(messageHMIDelay); /*if(timeProt.delay.sign!=true){ printf("neg "); } printf("delay: %lu s,%u\r\n",timeProt.delay.second,(unsigned int)timeProt.delay.halfmillis/2); if(timeProt.offset.sign==true){ printf("delayReq offset: %lu s,%u\r\n",timeProt.offset.second,(unsigned int)timeProt.offset.halfmillis/2); } else{ printf("delayReq offset: -%lu s,%u\r\n",timeProt.offset.second,(unsigned int)timeProt.offset.halfmillis/2); }*/ networkFlush(); sendHMI(" delay response completed"); } else{ sendHMI(" delay response not for me"); } break; case ACK: break; case PING: break; #else case DELAYREQUEST: data.crc=networkRead(); //printf("Type:%d , id:%d, crc:%d.",data.type, data.id,data.crc ); if(calculCRCID(data.type,0,NULL,data.id)!=data.crc){ taskENTER_CRITICAL(); timeProt.saveTime[data.id].second=0; taskEXIT_CRITICAL(); networkFlush(); sendHMI(" Erreur crc"); return; } else{