/* rtprDump.c */ #include #include #include "/usr/src/rtlinux-2.2/drivers/mbuff/mbuff.h" #include "rtpr.h" volatile char *shmData, *shmControl; int main(int argc, char *argv[]) { int outFile; int wPtr = 0; int rPtr = 0; int crPtr = 1; int ret; int pCount = 0; int *c0, *c1; if (argc != 2) { fprintf(stderr,"Usage: %s \n",argv[0]); return 0; } outFile = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC); if ( outFile < 0 ) { fprintf(stderr,"Error opening file: %s\n",argv[1]); return -1; } // Shared memory alloc shmData = (volatile char*) mbuff_alloc("recData",MAX_COUNT*sizeof(long long)); if( shmData == NULL ) { fprintf(stderr,"mbuff_alloc failed\n"); return -1; } shmControl = (volatile char*) mbuff_alloc("recControl",1024); if( shmControl == NULL) { fprintf(stderr,"mbuff_alloc failed\n"); mbuff_free("recData",(void*)shmData); return -1; } c0 = (int*)&shmControl[0]; c1 = (int*)&shmControl[8]; c1[0] = wPtr; while(c0[0] != FINISH_MSG) { usleep(90000); rPtr = c0[0]; crPtr = rPtr + 1; if (crPtr == MAX_COUNT) crPtr = 0; if ((wPtr != crPtr) && (rPtr>=0)) { //fprintf(stderr,"w:%i r%i\n",wPtr,rPtr); if (wPtr <= rPtr) { // write from wPtr position up to rPtr, including rPtr ret = write(outFile, (void*) &shmData[wPtr*(sizeof(long long))], (rPtr - wPtr +1)*sizeof(long long)); if (ret != (rPtr - wPtr +1)*sizeof(long long)) { fprintf(stderr,"Error writing file:%s\n",argv[1]); } pCount += rPtr -wPtr +1; wPtr = rPtr+1; if (wPtr == MAX_COUNT) wPtr = 0; } else { // write from wPtr to the end ret = write(outFile, (void*) &shmData[wPtr*(sizeof(long long))], (MAX_COUNT - wPtr +1)*sizeof(long long)); if (ret != (MAX_COUNT - wPtr +1)*sizeof(long long)) { fprintf(stderr,"Error writing file:%s\n",argv[1]); } pCount += MAX_COUNT -wPtr +1; // write from the beginning to rPtr ret = write(outFile, (void*) &shmData[0], (rPtr + 1)*sizeof(long long)); if (ret != (rPtr + 1)*sizeof(long long)) { fprintf(stderr,"Error writing file:%s\n",argv[1]); } pCount += rPtr +1; wPtr = rPtr+1; if (wPtr == MAX_COUNT) wPtr = 0; } c1[0] = wPtr; } } fprintf(stderr,"%i written\n",pCount); close(outFile); // Free shared memory mbuff_free("recData",(void*)shmData); mbuff_free("recControl",(void*)shmControl); return 0; }