int main(int argc, char** argv) { unsigned char bigBuffer[100000]; int numBytes=0,count=0,checkVal; GenericHeader_t *gHdr; if(argc!=2) { printf("Usage:\t%s <packetFile>\n",basename(argv[0])); return 0; } numBytes=genericReadOfFile(bigBuffer,argv[1],100000); printf("Read %d bytes from %s\n",numBytes,argv[1]); // for(count =0; count<numBytes;count++) { // printf("count %d -- %x\n",count,(bigBuffer[count]&0xff)); // } checkVal=checkPacket(bigBuffer); if(checkVal==0) { gHdr = (GenericHeader_t*) &bigBuffer[0]; printf("Got %s\n",packetCodeAsString(gHdr->code)); count+=gHdr->numBytes; } else { printf("Problem with packet -- checkVal==%d\n",checkVal); return 1; // count+=gHdr->numBytes; } return 0; }
int readAndSendEventRamdisk(char *headerLinkFilename) { static int errorCounter=0; AnitaEventHeader_t *theHeader; GenericHeader_t *gHdr; int retVal; char currentTouchname[FILENAME_MAX]; char currentLOSTouchname[FILENAME_MAX]; char waveFilename[FILENAME_MAX]; char headerFilename[FILENAME_MAX]; // char crapBuffer[FILENAME_MAX]; char justFile[FILENAME_MAX]; unsigned int thisEventNumber; //First up we test if the link still exists if(!checkFileExists(headerLinkFilename)) return 0; retVal=snprintf(justFile,sizeof(justFile),"%s",basename(headerLinkFilename)); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=sscanf(justFile,"hd_%u.dat",&thisEventNumber); if(retVal<=0) { syslog(LOG_ERR,"Error reading eventNumber from %s",justFile); thisEventNumber=0; } if(thisEventNumber==0) { printf("Why is this zero -- %s\n",headerLinkFilename); } retVal=snprintf(headerFilename,sizeof(headerFilename),"%s/hd_%d.dat",eventTelemDirs[currentPri],thisEventNumber); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(waveFilename,sizeof(waveFilename),"%s/hd_%d.dat",eventTelemDirs[currentPri], thisEventNumber); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(currentTouchname,sizeof(currentTouchname),"%s.sipd",headerFilename); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(currentLOSTouchname,sizeof(currentLOSTouchname),"%s.losd",headerFilename); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } if(checkFileExists(currentLOSTouchname)) return 0; touchFile(currentTouchname); // printf("%s\n",headerLinkFilename); removeFile(headerLinkFilename); // Next load header theHeader=(AnitaEventHeader_t*) &theBuffer[0]; retVal=fillHeader(theHeader,headerFilename); if(retVal<0) { // syslog(LOG_ERR,"Problem with %s",headerFilename); retVal=snprintf(headerFilename,sizeof(headerFilename),"%s/hd_%d.dat",eventTelemDirs[currentPri],thisEventNumber); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } unlink(currentTouchname); unlink(headerFilename); unlink(waveFilename); //Bollocks return 0; } theHeader->gHdr.packetNumber=getOpenportNumber(); retVal = openportWrite((unsigned char*)theHeader,sizeof(AnitaEventHeader_t),0); if(retVal<0) { //Problem sending data syslog(LOG_ERR,"Problem sending file %s over Openport\n",headerFilename); fprintf(stderr,"Problem sending file %s over Openport\n",headerFilename); } eventDataSent+=sizeof(AnitaEventHeader_t); thisEventNumber=theHeader->eventNumber; //Now get event file retVal=snprintf(headerFilename,sizeof(headerFilename),"%s/hd_%d.dat",eventTelemDirs[currentPri], thisEventNumber); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(waveFilename,sizeof(waveFilename),"%s/ev_%d.dat",eventTelemDirs[currentPri], thisEventNumber); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=genericReadOfFile((unsigned char*)theBuffer,waveFilename,MAX_EVENT_SIZE); if(retVal<0) { fprintf(stderr,"Problem reading %s\n",waveFilename); syslog(LOG_ERR,"Problem reading %s\n",waveFilename); // unlink(headerLinkFilename); unlink(headerFilename); unlink(waveFilename); unlink(currentTouchname); //Bollocks return 0; } //Okay so now the buffer either contains EncodedSurfPacketHeader_t or // it contains EncodedPedSubbedSurfPacketHeader_t gHdr = (GenericHeader_t*) &theBuffer[0]; switch(gHdr->code) { case PACKET_BD: if(sendWavePackets) retVal=sendRawWaveformPackets(retVal); else retVal=sendRawSurfPackets(retVal); break; case PACKET_PED_SUBBED_EVENT: if(sendWavePackets) retVal=sendPedSubbedWaveformPackets(retVal); else retVal=sendPedSubbedSurfPackets(retVal); break; case PACKET_ENC_EVENT_WRAPPER: retVal=sendEncodedSurfPackets(retVal); break; case PACKET_ENC_PEDSUB_EVENT_WRAPPER: if(!sendWavePackets) { retVal=sendEncodedPedSubbedSurfPackets(retVal); } else { retVal=sendEncodedPedSubbedWavePackets(retVal); } break; default: if(errorCounter<100) { syslog(LOG_ERR,"Don't know what to do with packet %d -- %s (Message %d of 100)\n",gHdr->code,packetCodeAsString(gHdr->code),errorCounter); errorCounter++; } fprintf(stderr,"Don't know what to do with packet %d -- %s\n",gHdr->code,packetCodeAsString(gHdr->code)); } if(printToScreen && verbosity>1) printf("Removing files %s\t%s\n%s\n",headerFilename,waveFilename, headerLinkFilename); if(!checkFileExists(currentLOSTouchname)) { // unlink(headerLinkFilename); unlink(headerFilename); unlink(waveFilename); unlink(currentTouchname); } else { sleep(1); // unlink(headerLinkFilename); unlink(headerFilename); unlink(waveFilename); unlink(currentTouchname); unlink(currentLOSTouchname); } return 1; }
int readHkAndOpenport(int wd,int maxCopy, char *telemDir, char *linkDir, int fileSize, int *numSent) /* Looks in the specified directroy and OPENPORT's up to maxCopy bytes of data */ /* fileSize is the maximum size of a packet in the directory */ { // fprintf(stderr,"readHkAndOpenport %s -- %d\n",linkDir,maxCopy); char currentFilename[FILENAME_MAX]; char currentTouchname[FILENAME_MAX]; char currentLOSTouchname[FILENAME_MAX]; char currentLinkname[FILENAME_MAX]; int retVal,numLinks,count,numBytes,totalBytes=0;//,checkVal=0; char *tempString; GenericHeader_t *gHdr; *numSent=0; numLinks=getNumLinks(wd); if(numLinks<=0) { return 0; } int counter=0; for(count=numLinks-1;count>=0;count--) { //Get last link name tempString=getLastLink(wd); retVal=snprintf(currentFilename,sizeof(currentFilename),"%s/%s",telemDir,tempString); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(currentTouchname,sizeof(currentTouchname),"%s.sipd",currentFilename); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(currentLOSTouchname,sizeof(currentLOSTouchname),"%s.losd",currentFilename); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(currentLinkname,sizeof(currentLinkname),"%s/%s",linkDir,tempString); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } if(!checkFileExists(currentLinkname)) continue; if(checkFileExists(currentLOSTouchname)) continue; touchFile(currentTouchname); if(checkFileExists(currentLOSTouchname)) { unlink(currentTouchname); continue; } retVal=genericReadOfFile((unsigned char*)theBuffer, currentFilename, MAX_EVENT_SIZE); syslog(LOG_DEBUG,"Trying %s",currentFilename); if(retVal<=0) { // syslog(LOG_ERR,"Error opening file, will delete: %s", // currentFilename); // fprintf(stderr,"Error reading file %s -- %d\n",currentFilename,retVal); unlink(currentFilename); unlink(currentLinkname); unlink(currentTouchname); continue; } numBytes=retVal; if(printToScreen && verbosity>=0) { printf("Read File: %s -- (%d bytes)\n",currentFilename,numBytes); } // printf("Read %d bytes from file\n",numBytes); // Maybe I'll add a packet check here gHdr = (GenericHeader_t*)theBuffer; // checkVal=checkPacket(gHdr); // if(checkVal!=0 ) { // printf("Bad packet %s == %d\n",currentFilename,checkVal); // } gHdr->packetNumber=getOpenportNumber(); printf("Openport number %d\n",gHdr->packetNumber); retVal = openportWrite(theBuffer, numBytes,1); if(retVal<0) { //Problem sending data syslog(LOG_ERR,"Problem sending Wake up Packet over OPENPORT\n"); fprintf(stderr,"Problem sending Wake up Packet over OPENPORT\n"); } totalBytes+=numBytes; if(!checkFileExists(currentLOSTouchname)) { unlink(currentLinkname); unlink(currentFilename); unlink(currentTouchname); } else { usleep(1); unlink(currentLinkname); unlink(currentFilename); unlink(currentTouchname); unlink(currentLOSTouchname); } (*numSent)++; // if((totalBytes+fileSize)>maxCopy) break; counter++; if(counter>=maxCopy) break; // break; } // fprintf(stderr,"readHkAndOpenport %s -- %d\n",linkDir,*numSent); return totalBytes; }
void readAndSendEventRamdisk(char *headerLinkFilename) { static int errorCounter=0; AnitaEventHeader_t *theHeader; GenericHeader_t *gHdr; int retVal; char waveFilename[FILENAME_MAX]; char headerFilename[FILENAME_MAX]; char currentTouchname[FILENAME_MAX]; char currentLOSTouchname[FILENAME_MAX]; char justFile[FILENAME_MAX]; unsigned int thisEventNumber; sprintf(justFile,"%s",basename(headerLinkFilename)); sscanf(justFile,"hd_%u.dat",&thisEventNumber); sprintf(headerFilename,"%s/hd_%d.dat",eventTelemDirs[currentPri], thisEventNumber); sprintf(waveFilename,"%s/psev_%d.dat",eventTelemDirs[currentPri], thisEventNumber); sprintf(currentTouchname,"%s.sipd",headerFilename); sprintf(currentLOSTouchname,"%s.losd",headerFilename); if(checkFileExists(currentTouchname)) return; touchFile(currentLOSTouchname); //Removing headerLinkFilename // fprintf(stderr,"Removing %s\n",headerLinkFilename); unlink(headerLinkFilename); //First check if there is room for the header if((LOS_MAX_BYTES-numBytesInBuffer)<sizeof(AnitaEventHeader_t)) doWrite(); // Next load header theHeader=(AnitaEventHeader_t*) &losBuffer[numBytesInBuffer]; retVal=fillHeader(theHeader,headerFilename); theHeader->gHdr.packetNumber=getLosNumber(); numBytesInBuffer+=sizeof(AnitaEventHeader_t); if(retVal<0) { unlink(headerFilename); unlink(waveFilename); unlink(currentLOSTouchname); //Bollocks return; } thisEventNumber=theHeader->eventNumber; //Now get event file sprintf(headerFilename,"%s/hd_%d.dat",eventTelemDirs[currentPri], thisEventNumber); sprintf(waveFilename,"%s/ev_%d.dat",eventTelemDirs[currentPri], thisEventNumber); retVal=genericReadOfFile(eventBuffer,waveFilename,MAX_EVENT_SIZE); if(retVal<0) { fprintf(stderr,"Problem reading %s\n",waveFilename); unlink(headerFilename); unlink(waveFilename); unlink(currentLOSTouchname); //Bollocks return; } //Okay so now the buffer either contains EncodedSurfPacketHeader_t or // it contains EncodedPedSubbedSurfPacketHeader_t gHdr = (GenericHeader_t*) &eventBuffer[0]; switch(gHdr->code) { case PACKET_BD: if(sendWavePackets) retVal=sendRawWaveformPackets(retVal); else retVal=sendRawSurfPackets(retVal); break; case PACKET_PED_SUBBED_EVENT: if(sendWavePackets) retVal=sendPedSubbedWaveformPackets(retVal); else retVal=sendPedSubbedSurfPackets(retVal); break; case PACKET_ENC_EVENT_WRAPPER: retVal=sendEncodedSurfPackets(retVal); break; case PACKET_ENC_PEDSUB_EVENT_WRAPPER: if(sendWavePackets) retVal=sendEncodedPedSubbedWavePackets(retVal); else retVal=sendEncodedPedSubbedSurfPackets(retVal); break; default: if(errorCounter<100) { syslog(LOG_ERR,"Don't know what to do with packet %d -- %s (Message %d of 100)\n",gHdr->code,packetCodeAsString(gHdr->code),errorCounter); errorCounter++; } fprintf(stderr,"Don't know what to do with packet %d -- %s (file %s, size %d)\n",gHdr->code,packetCodeAsString(gHdr->code),waveFilename,retVal); } if(printToScreen && verbosity>1) printf("Removing files %s\t%s\n",headerFilename,waveFilename); if(!checkFileExists(currentTouchname)) { unlink(headerFilename); unlink(waveFilename); unlink(currentLOSTouchname); } else { printf("Not removing %s because checkFileExists == %d\n", headerFilename,checkFileExists(currentTouchname)); } }
int addToTelemetryBuffer(int maxCopy, int wd, char *telemDir, char *linkDir, int fileSize,int numToLeave) /* Uses the inotify watch specified by wd to look */ /* in the specified directroy and fill buffer upto maxCopy. */ /* fileSize is the maximum size of a packet in the directory */ { char currentFilename[FILENAME_MAX]; char currentLinkname[FILENAME_MAX]; char currentTouchname[FILENAME_MAX]; char currentLOSTouchname[FILENAME_MAX]; int retVal,numLinks,count,numBytes,totalBytes=0;//,checkVal=0; GenericHeader_t *gHdr; char *tempString; // printf("%s %s == %d %d %d\n",telemDir,linkDir,fileSize,numBytesInBuffer,LOS_MAX_BYTES); //Both of these two checks should be uneccesary if((numBytesInBuffer+fileSize)>LOS_MAX_BYTES) return 0; numLinks=getNumLinks(wd); if(numLinks<=numToLeave) { return 0; } for(count=numLinks-1;count>=numToLeave;count--) { tempString=getLastLink(wd); sprintf(currentFilename,"%s/%s",telemDir,tempString); sprintf(currentTouchname,"%s.sipd",currentFilename); sprintf(currentLOSTouchname,"%s.losd",currentFilename); sprintf(currentLinkname,"%s/%s",linkDir,tempString); printf("RJN -- %s\n",currentFilename); if(!checkFileExists(currentLinkname)) { // printf("%s -- doesn't exist\n",currentLinkname); unlink(currentLinkname); continue; } if(checkFileExists(currentTouchname)) continue; touchFile(currentLOSTouchname); retVal=genericReadOfFile((unsigned char*)&(losBuffer[numBytesInBuffer]), currentFilename, LOS_MAX_BYTES-numBytesInBuffer); if(retVal<=0) { // syslog(LOG_ERR,"Error opening file, will delete: %s", // currentFilename); // fprintf(stderr,"Error reading file %s -- %d\n",currentFilename,retVal); unlink(currentFilename); unlink(currentLOSTouchname); unlink(currentLinkname); unlink(currentTouchname); continue; } numBytes=retVal; if(printToScreen && verbosity>1) { printf("Read File: %s -- (%d bytes)\n",currentFilename,numBytes); } // printf("Read %d bytes from file\n",numBytes); // Maybe I'll add a packet check here gHdr = (GenericHeader_t*) (&(losBuffer[numBytesInBuffer])); // checkVal=checkPacket(gHdr); // if(checkVal!=0 ) { // printf("Bad packet %s == %d\n",currentFilename,checkVal); // } gHdr->packetNumber=getLosNumber(); numBytesInBuffer+=numBytes; totalBytes+=numBytes; if(!checkFileExists(currentTouchname)) { unlink(currentLinkname); unlink(currentFilename); unlink(currentLOSTouchname); } else { printf("%s exists\n",currentTouchname); } if((totalBytes+fileSize)>maxCopy || (numBytesInBuffer+fileSize)>LOS_MAX_BYTES) break; } return totalBytes; }
int checkLinkDir(int maxCopy, char *telemDir, char *linkDir, int fileSize) /* Looks in the specified directroy and fill buffer upto maxCopy. */ /* fileSize is the maximum size of a packet in the directory */ { char currentFilename[FILENAME_MAX]; char currentLinkname[FILENAME_MAX]; char currentTouchname[FILENAME_MAX]; char currentLOSTouchname[FILENAME_MAX]; int retVal,numLinks,count,numBytes,totalBytes=0;//,checkVal=0; GenericHeader_t *gHdr; struct dirent **linkList; if((numBytesInBuffer+fileSize)>LOS_MAX_BYTES) return 0; numLinks=getListofLinks(linkDir,&linkList); if(numLinks<=1) { return 0; } for(count=numLinks-1;count>=1;count--) { sprintf(currentFilename,"%s/%s",telemDir, linkList[count]->d_name); sprintf(currentTouchname,"%s.sipd",currentFilename); sprintf(currentLOSTouchname,"%s.losd",currentFilename); sprintf(currentLinkname,"%s/%s", linkDir,linkList[count]->d_name); if(checkFileExists(currentTouchname)) continue; touchFile(currentLOSTouchname); retVal=genericReadOfFile((unsigned char*)&(losBuffer[numBytesInBuffer]), currentFilename, LOS_MAX_BYTES-numBytesInBuffer); if(retVal<=0) { // syslog(LOG_ERR,"Error opening file, will delete: %s", // currentFilename); // fprintf(stderr,"Error reading file %s -- %d\n",currentFilename,retVal); unlink(currentFilename); unlink(currentLOSTouchname); unlink(currentLinkname); unlink(currentTouchname); continue; } numBytes=retVal; if(printToScreen && verbosity>1) { printf("Read File: %s -- (%d bytes)\n",currentFilename,numBytes); } // printf("Read %d bytes from file\n",numBytes); // Maybe I'll add a packet check here gHdr = (GenericHeader_t*) (&(losBuffer[numBytesInBuffer])); // checkVal=checkPacket(gHdr); // if(checkVal!=0 ) { // printf("Bad packet %s == %d\n",currentFilename,checkVal); // } gHdr->packetNumber=getLosNumber(); numBytesInBuffer+=numBytes; totalBytes+=numBytes; if(!checkFileExists(currentTouchname)) { unlink(currentLinkname); unlink(currentFilename); unlink(currentLOSTouchname); } else { printf("%s exists\n",currentTouchname); } if((totalBytes+fileSize)>maxCopy || (numBytesInBuffer+fileSize)>LOS_MAX_BYTES) break; } for(count=0;count<numLinks;count++) free(linkList[count]); free(linkList); return totalBytes; }
int checkLinkDirAndOpenport(int maxCopy, char *telemDir, char *linkDir, int fileSize) /* Looks in the specified directroy and OPENPORT's up to maxCopy bytes of data */ /* fileSize is the maximum size of a packet in the directory */ { char currentFilename[FILENAME_MAX]; char currentTouchname[FILENAME_MAX]; char currentLOSTouchname[FILENAME_MAX]; char currentLinkname[FILENAME_MAX]; int retVal,numLinks,count,numBytes,totalBytes=0;//,checkVal=0; GenericHeader_t *gHdr; struct dirent **linkList; numLinks=getListofLinks(linkDir,&linkList); if(numLinks<=0) { return 0; } int counter=0; for(count=numLinks-1;count>=0;count--) { retVal=snprintf(currentFilename,sizeof(currentFilename),"%s/%s",telemDir, linkList[count]->d_name); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(currentTouchname,sizeof(currentTouchname),"%s.sipd",currentFilename); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(currentLOSTouchname,sizeof(currentLOSTouchname),"%s.losd",currentFilename); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } retVal=snprintf(currentLinkname,sizeof(currentLinkname),"%s/%s", linkDir,linkList[count]->d_name); if(retVal<0) { syslog(LOG_ERR,"Error using snprintf -- %s",strerror(errno)); } if(checkFileExists(currentLOSTouchname)) continue; touchFile(currentTouchname); retVal=genericReadOfFile((unsigned char*)theBuffer, currentFilename, MAX_EVENT_SIZE); syslog(LOG_DEBUG,"Trying %s",currentFilename); if(retVal<=0) { // syslog(LOG_ERR,"Error opening file, will delete: %s", // currentFilename); // fprintf(stderr,"Error reading file %s -- %d\n",currentFilename,retVal); removeFile(currentFilename); removeFile(currentLinkname); removeFile(currentTouchname); continue; } numBytes=retVal; if(printToScreen && verbosity>1) { printf("Read File: %s -- (%d bytes)\n",currentFilename,numBytes); } printf("Read %d bytes from file\n",numBytes); // Maybe I'll add a packet check here gHdr = (GenericHeader_t*)theBuffer; // checkVal=checkPacket(gHdr); // if(checkVal!=0 ) { // printf("Bad packet %s == %d\n",currentFilename,checkVal); // } gHdr->packetNumber=getOpenportNumber(); printf("Openport number %d\n",gHdr->packetNumber); retVal = openportWrite(theBuffer, numBytes,1); if(retVal<0) { //Problem sending data syslog(LOG_ERR,"Problem sending Wake up Packet over Openprt\n"); fprintf(stderr,"Problem sending Wake up Packet over Openport\n"); } totalBytes+=numBytes; if(!checkFileExists(currentLOSTouchname)) { removeFile(currentLinkname); removeFile(currentFilename); removeFile(currentTouchname); } else { sleep(1); removeFile(currentLinkname); removeFile(currentFilename); removeFile(currentTouchname); removeFile(currentLOSTouchname); } printf("Got code: %#x %#x\n",gHdr->code&BASE_PACKET_MASK,PACKET_GPS_ADU5_PAT); // if((totalBytes+fileSize)>maxCopy) break; counter++; if(counter>=maxCopy) break; // break; } for(count=0;count<numLinks;count++) free(linkList[count]); free(linkList); return totalBytes; }