int msgHandler(int sock) { // Header struct serbeep_header header; if (readHeader(sock, &header) != 0) return 1; if (header.magic != MAGIC) { fprintf(stderr, "Invalid magic\n"); return 1; } // clientHello if ((header.cmd & 0x2) == 0x2) { // serverHello header.cmd |= 0x1; if (writeStruct(sock, &header, sizeof(header)) != 1) return 1; return 0; } // musicScore if ((header.cmd & 0x4) == 0x4) { if (pthread_mutex_trylock(&global_score_mutex) != 0) return 1; if (readStruct(sock, &global_score_header, sizeof(global_score_header)) != 1) { pthread_mutex_unlock(&global_score_mutex); return 1; } global_score_header.length = ntohs(global_score_header.length); size_t size = sizeof(struct serbeep_score_note) * global_score_header.length; global_score_notes = (struct serbeep_score_note *)malloc(size); if (global_score_notes == NULL) { fprintf(stderr, "fail...malloc(%zd bytes)\n", size); pthread_mutex_unlock(&global_score_mutex); return 1; } if (readStruct(sock, global_score_notes, size) != 1) { freeNull(global_score_notes); pthread_mutex_unlock(&global_score_mutex); return 1; } // musicAck header.cmd |= 0x1; if (writeStruct(sock, &header, sizeof(header)) != 1) { freeNull(global_score_notes); pthread_mutex_unlock(&global_score_mutex); return 1; } pthread_mutex_unlock(&global_score_mutex); return 0; } return 0; // Success }
//=================================================================================================== Bool tests_readStruct(Bool details) { Bool testPassed = TRUE; Integ *myInteg, *newInteg; myInteg = malloc(sizeof(Integ)); newInteg = malloc(sizeof(Integ)); FILE* file = fopen("testStruct", "wb+"); if(file == NULL) { perror("copyFile fopen file"); return FALSE; } myInteg->a = 1; myInteg->b = 'a'; printf("%d%c", myInteg->a, myInteg->b); if(!writeStruct(file, &myInteg, sizeof(Integ))) testPassed = FALSE; fseek(file, 0, SEEK_SET); if(!readStruct(file, &newInteg, sizeof(Integ))) testPassed = FALSE; printf("\nNew int : %d and %c", newInteg->a, newInteg->b); free(myInteg); free(newInteg); fclose(file); return testPassed; }
void encodeAndWriteEvent(AnitaEventHeader_t *hdPtr, PedSubbedEventBody_t *psbPtr, int pri) { memset(outputBuffer,0,MAX_WAVE_BUFFER); EncodeControlStruct_t telemEncCntl; int surf,chan; int retVal,numBytes; for(surf=0;surf<ACTIVE_SURFS;surf++) { for(chan=0;chan<CHANNELS_PER_SURF;chan++) { telemEncCntl.encTypes[surf][chan]=ENCODE_LOSSLESS_BINFIB_COMBO; if(chan==8) telemEncCntl.encTypes[surf][chan]=ENCODE_LOSSY_MULAW_6BIT; } } retVal=packPedSubbedEvent(psbPtr,&telemEncCntl,outputBuffer,&numBytes); printf("retVal %d, numBytes %d\n",retVal,numBytes); char headName[FILENAME_MAX]; char bodyName[FILENAME_MAX]; sprintf(bodyName,"%s/ev_%u.dat",eventTelemDirs[pri],hdPtr->eventNumber); sprintf(headName,"%s/hd_%u.dat",eventTelemDirs[pri],hdPtr->eventNumber); retVal=normalSingleWrite((unsigned char*)outputBuffer,bodyName,numBytes); if(retVal<0) { printf("Something wrong while writing %s\n",bodyName); } else { writeStruct(hdPtr,headName,sizeof(AnitaEventHeader_t)); makeLink(headName,eventTelemLinkDirs[pri]); fprintf(stderr,"Think I did it\n"); } }
static void writeChunk(std::ofstream & out, ArxProfilerChunkType type, size_t dataSize, size_t & pos) { SavedProfilerChunkHeader chunk; chunk.type = type; chunk.size = dataSize; std::memset(chunk.padding, 0, sizeof(chunk.padding)); writeStruct(out, chunk, pos); LogDebug("Writing chunk at offset " << pos << " type " << chunk.type << " size " << chunk.size); }
void writeEventAndMakeLink(const char *theEventDir, const char *theLinkDir, AnitaEventFull_t *theEventPtr) { char theFilename[FILENAME_MAX]; // int retVal; AnitaEventHeader_t *theHeader=&(theEventPtr->header); AnitaEventBody_t *theBody=&(theEventPtr->body); sprintf(theFilename,"%s/ev_%d.dat",theEventDir, theEventPtr->header.eventNumber); writeStruct(theBody,theFilename,sizeof(AnitaEventBody_t)); sprintf(theFilename,"%s/hd_%d.dat",theEventDir, theEventPtr->header.eventNumber); writeStruct(theHeader,theFilename,sizeof(AnitaEventHeader_t)); /* Make links, not sure what to do with return value here */ makeLink(theFilename,theLinkDir); }
void writeStatus() /* Write the current calibration state */ { // int retVal; char filename[FILENAME_MAX]; CalibStruct_t theStatus; time_t unixTime; time(&unixTime); theStatus.unixTime=unixTime; theStatus.status=0; if(stateAmplite1) theStatus.status|=AMPLITE1_MASK; if(stateAmplite2) theStatus.status|=AMPLITE2_MASK; if(stateBZAmpa1) theStatus.status|=BZAMPA1_MASK; if(stateBZAmpa2) theStatus.status|=BZAMPA2_MASK; if(stateNTUAmpa) theStatus.status|=NTUAMPA_MASK; if(stateSB) theStatus.status|=SB_MASK; if(stateNTUSSD5V) theStatus.status|=NTU_SSD_5V_MASK; if(stateNTUSSD12V) theStatus.status|=NTU_SSD_12V_MASK; if(stateNTUSSDShutdown) theStatus.status|=NTU_SSD_SHUTDOWN_MASK; sprintf(filename,"%s/calib_%u.dat",CALIBD_STATUS_DIR,theStatus.unixTime); writeStruct(&theStatus,filename,sizeof(CalibStruct_t)); makeLink(filename,CALIBD_STATUS_LINK_DIR); cleverHkWrite((unsigned char*)&theStatus,sizeof(CalibStruct_t), theStatus.unixTime,&calibWriter); }
int writeFileAndLink(GpuPhiSectorPowerSpectrumStruct_t* payloadPowSpec, int phi) { char theFilename[FILENAME_MAX]; int retVal=0; fillGenericHeader(payloadPowSpec,PACKET_GPU_AVE_POW_SPEC,sizeof(GpuPhiSectorPowerSpectrumStruct_t)); sprintf(theFilename,"%s/gpuPowSpec_%u_phi%d.dat", GPU_TELEM_DIR,payloadPowSpec->unixTimeFirstEvent,phi); retVal=writeStruct(payloadPowSpec,theFilename,sizeof(GpuPhiSectorPowerSpectrumStruct_t)); retVal=makeLink(theFilename,GPU_TELEM_LINK_DIR); retVal=cleverHkWrite((unsigned char*)payloadPowSpec,sizeof(GpuPhiSectorPowerSpectrumStruct_t), payloadPowSpec->unixTimeFirstEvent,&gpuWriter); printf("cleverHkWrite for fileName %s returned %d\n", theFilename, retVal); if(retVal<0) { //Had an error } return retVal; }
void write(const Object& obj) { writeStruct(obj); }
void Profiler::writeProfileLog() { std::string filename = util::getDateTimeString() + ".arxprof"; LogInfo << "Writing profiler log to: " << filename; fs::ofstream out(fs::path(filename), std::ios::binary | std::ios::out); size_t pos = 0; int fileVersion = 1; LogDebug("Writing Header"); SavedProfilerHeader header; std::strncpy(header.magic, profilerMagic, 8); header.version = fileVersion; std::memset(header.padding, 0, sizeof(header.padding)); writeStruct(out, header, pos); LogDebug("Building string table"); ProfilerStringTable stringTable; std::vector<SavedProfilerThread> threadsData; std::vector<SavedProfilerSample> samplesData; for(ThreadInfos::const_iterator it = m_threads.begin(); it != m_threads.end(); ++it) { const ProfilerThread & thread = it->second; u32 stringIndex = stringTable.add(thread.threadName); SavedProfilerThread saved; saved.stringIndex = stringIndex; saved.threadId = thread.threadId; saved.startTime = thread.startTime; saved.endTime = thread.endTime; threadsData.push_back(saved); } // Profile points u32 index = 0; u32 numItems = m_writeIndex; if(numItems >= NB_SAMPLES) { index = numItems; numItems = NB_SAMPLES; } for(u32 i = 0; i < numItems; ++i, ++index) { ProfilerSample & sample = m_samples[index % NB_SAMPLES]; u32 stringIndex = stringTable.add(sample.tag); SavedProfilerSample saved; saved.stringIndex = stringIndex; saved.threadId = sample.threadId; saved.startTime = sample.startTime; saved.endTime = sample.endTime; samplesData.push_back(saved); } LogInfo << "Writing data: " " Strings " << stringTable.entries() << ", Threads " << threadsData.size() << ", Points " << samplesData.size(); { std::string stringsData = stringTable.data(); int dataSize = stringsData.size() + 1; // termination writeChunk(out, ArxProfilerChunkType_Strings, dataSize, pos); out.write(stringsData.c_str(), dataSize); pos += dataSize; } { int dataSize = threadsData.size() * sizeof(SavedProfilerThread); writeChunk(out, ArxProfilerChunkType_Threads, dataSize, pos); out.write((const char*) threadsData.data(), dataSize); pos += dataSize; } { int dataSize = samplesData.size() * sizeof(SavedProfilerSample); writeChunk(out, ArxProfilerChunkType_Samples, dataSize, pos); out.write((const char*) samplesData.data(), dataSize); pos += dataSize; } out.close(); }
void write(CObjRef obj) { writeStruct(obj); }
int outputData(AnalogueCode_t code) { int retVal; char theFilename[FILENAME_MAX]; char fullFilename[FILENAME_MAX]; SSHkDataStruct_t theHkData; AnitaHkWriterStruct_t *wrPtr; static int telemCount=0; struct timeval timeStruct; gettimeofday(&timeStruct,NULL); theHkData.unixTime=timeStruct.tv_sec;; theHkData.unixTimeUs=timeStruct.tv_usec;; switch(code) { case (IP320_RAW): sprintf(theFilename,"sshk_%d_%d.raw.dat",theHkData.unixTime, theHkData.unixTimeUs); theHkData.ip320=rawDataStruct; wrPtr=&hkRawWriter; break; case(IP320_CAL): sprintf(theFilename,"sshk_%d_%d.cal.dat",theHkData.unixTime, theHkData.unixTimeUs); theHkData.ip320=calDataStruct; wrPtr=&hkCalWriter; break; case(IP320_AVZ): sprintf(theFilename,"sshk_%d_%d.avz.dat",theHkData.unixTime, theHkData.unixTimeUs); theHkData.ip320=autoZeroStruct; // printf("First two %d %d\n",theHkData.ip320.board[0].data[0]&0xfff, // theHkData.ip320.board[0].data[0]&0xfff); wrPtr=&hkCalWriter; break; default: syslog(LOG_WARNING,"Unknown HK data code: %d",code); if(printToScreen) fprintf(stderr,"Unknown HK data code: %d\n",code); return -1; } fillGenericHeader(&theHkData,PACKET_HKD_SS,sizeof(SSHkDataStruct_t)); if(code==IP320_RAW) { int chan=0; printf("Raw data: "); for(chan=0;chan<40;chan++) { printf("%d ",rawDataStruct.board.data[chan]); } printf("\n"); } telemCount++; if(telemCount>=telemEvery) { //Write file and make link for SIPd if(printToScreen) printf("%s\n",theFilename); sprintf(fullFilename,"%s/%s",HK_TELEM_DIR,theFilename); retVal=writeStruct(&theHkData,fullFilename,sizeof(SSHkDataStruct_t)); retVal+=makeLink(fullFilename,HK_TELEM_LINK_DIR); telemCount=0; } //Write file to main disk retVal=cleverHkWrite((unsigned char*)&theHkData,sizeof(SSHkDataStruct_t), theHkData.unixTime,wrPtr); if(retVal<0) { //Had an error //Do something } return retVal; }