19 #include <sys/types.h>
31 #include <sys/timeb.h>
37 #include <sys/timeb.h>
43 #include <sys/timeb.h>
44 #define fgetpos64 fgetpos
46 #define fseeko64 fseek
47 #define fpos64_t fpos_t
50 #define CLOCK_REALTIME 1
51 int clock_gettime(
int clockid,
struct timespec *tp)
64 #define fgetpos64 fgetpos
66 #define fseeko64 fseek
67 #define fpos64_t fpos_t
74 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
75 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
77 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
80 #define CLOCK_REALTIME 1
82 int clock_gettime(
int clockid,
struct timespec *tp)
85 unsigned __int64 tmpres = 0;
90 GetSystemTimeAsFileTime(&ft);
92 tmpres |= ft.dwHighDateTime;
94 tmpres |= ft.dwLowDateTime;
98 tmpres -= DELTA_EPOCH_IN_MICROSECS;
99 tp->tv_sec = (long)(tmpres / 1000000UL);
100 tp->tv_nsec = (long)(tmpres % 1000000UL) * 1000;
116 #define OFFSET__ENTRIES 250000
128 struct timespec clock;
142 clock_gettime(CLOCK_REALTIME,&clock);
148 strcpy(pLmdControl->
cFile,Filename);
152 pLmdControl->
pBuffer = (int16_t*) malloc(iBytes);
159 if((pLmdControl->
fFile=(FILE *)fopen64(Filename,
"r") )!=NULL){
160 printf(
"fLmdPutOpen: File exists: %s\n",Filename);
162 fclose(pLmdControl->
fFile);
167 if((pLmdControl->
fFile=(FILE *)fopen64(Filename,
"w+") )== NULL){
168 printf(
"fLmdPutOpen: Error open file %s\n",Filename);
180 pLmdControl->
iBytes+=iReturn;
183 printf(
"fLmdPutOpen: %s. Bytes:%d over:%d table:%d large:%d.\n",
184 Filename,iBytes,iOver,iUseOffset,iLargeFile);
192 uint32_t *ps, *pd, i, elements;
193 int64_t fileleft,used;
199 used=pLmdControl->
iBytes/4;
200 fileleft=0xffffffff - used - (4+elements);
201 if((int64_t)(pHeader->
iWords/2+2) > fileleft){
202 printf(
"fLmdPutElement: File size exceed\n");
213 pLmdControl->
iBytes+=iReturn;
214 if(iReturn != (pHeader->
iWords+4)*2){
215 printf(
"fLmdPutElement: Write error \n");
227 pLmdControl->
iBytes+=iReturn;
232 printf(
"fLmdPutElement: Element too big: %d words\n",pHeader->
iWords+4);
236 ps=(uint32_t *)pHeader;
238 iReturn=(pHeader->
iWords+4)/2;
239 for(i=0;i<iReturn;i++) *pd++ = *ps++;
254 uint32_t Bytes=0,TotalBytes=0,i, elements;
255 int64_t fileleft,used;
262 used=pLmdControl->
iBytes/4;
263 fileleft=0xffffffff - used - (4+elements);
264 for(i=0;i<Items;i++) {
269 if((int64_t)TotalBytes/4 > fileleft){
270 printf(
"fLmdPutElement: File size exceed\n");
288 pLmdControl->
iBytes+=iReturn;
305 pLmdControl->
iBytes+=iReturn;
307 printf(
"fLmdPutClose: Error writing last buffer. Closing file.\n");
309 rewind(pLmdControl->
fFile);
321 rewind(pLmdControl->
fFile);
332 uint32_t *iBufferBytes)
339 else pLmdControl->
iPort=iPort;
344 printf(
"fLmdConnectMbs: Connect to transport server %s port %d\n",Nodename,pLmdControl->
iPort);
346 printf(
"fLmdConnectMbs: Connect to stream server %s port %d\n",Nodename,pLmdControl->
iPort);
349 printf (
"fLmdConnectMbs: Error connect to %s \n",Nodename);
355 printf (
"fLmdConnectMbs: Error read info from %s \n",Nodename);
362 printf(
"fLmdConnectMbs: Event spanning not supported!\n");
367 printf(
"fLmdConnectMbs: MBS not in DABC mode!\n");
371 strcpy(pLmdControl->
cFile,Nodename);
392 if(iBuffers > 1){printf(
"fLmdInitMbs: Event spanning not supported!\n");
return(
LMD__FAILURE);}
393 if(iStreams > 0){printf(
"fLmdInitMbs: MBS not in DABC mode!\n");
return(
LMD__FAILURE);}
394 pLmdControl->
iPort=iPort;
395 strcpy(pLmdControl->
cFile,Nodename);
396 if(pLmdControl->
pBuffer == NULL) pLmdControl->
pBuffer = (int16_t*) malloc(iMaxBytes);
411 memset(cClose,0,
sizeof(cClose));
412 strcpy(cClose,
"CLOSE");
450 uint32_t *iBytesUsed){
454 uint32_t *ps, *pd, i,ii, elem=0, size=0, usedBytes=0,leftBytes=0;
465 printf(
"fLmdGetMbsBuffer: Need buffer to read\n");
469 printf(
"fLmdGetMbsBuffer: %s buffer size %d too small for %d bytes\n",
475 memset(cRequest,0,
sizeof(cRequest));
476 strcpy(cRequest,
"GETEVT");
484 printf(
"fLmdGetMbsBuffer: %s buffer size %d too small for %d bytes\n",
489 if((pBuf->
iType&0xffff) == 100)
490 iReturn=
f_stc_read((int32_t *)(pBuf+1),usedBytes,pLmdControl->
iTCP,-1);
493 if(pLmdControl->
iSwap)
fLmdSwap4((uint32_t *)(pBuf+1),usedBytes/4);
495 if(iElements != NULL)*iElements =pBuf->
iElements;
496 pLmdControl->
iBytes += usedBytes;
512 uint32_t
l=0,i,bufferBytes=0,h[12];
526 strcpy(pLmdControl->
cFile,Filename);
527 if((pLmdControl->
fFile=(FILE *)fopen64(Filename,
"r"))== NULL)
529 printf(
"fLmdGetOpen: File not found: %d\n",Filename);
538 printf(
"fLmdGetOpen: LMD format error: no LMD file: %s\n",Filename);
544 if(pLmdControl->
iSwap){
545 printf(
"do swap !!!\n");
550 printf(
"fLmdGetOpen: LMD format error: no LMD file: %s, type is %0x\n",
561 printf(
"fLmdGetOpen: Index format error: %s\n",Filename);
567 pLmdControl->
iBytes+=iReturn;
577 printf(
"fLmdGetOpen: LMD format error: no LMD file: %s\n",Filename);
583 bufferBytes = iBytes;
584 if(bufferBytes < pLmdControl->pMbsFileHeader->iMaxWords*2)
587 pLmdControl->
pBuffer = (int16_t *)malloc(bufferBytes);
591 printf(
"fLmdGetOpen: %s words %u\n", Filename, pLmdControl->
iBufferWords);
603 uint32_t *iBytesUsed){
606 uint32_t *ps, *pd, i,ii, elem=0, size=0, leftBytes=0, used, elem_sz;
609 if(iBytes < pLmdControl->pMbsFileHeader->iMaxWords){
610 printf(
"fLmdGetBuffer: %s buffer size %d too small for %d bytes\n",
614 if(pMbsHeader == NULL){
615 printf(
"fLmdGetBuffer: Need buffer to read\n");
628 printf(
"fLmdGetBuffer: EOF: %s\n",pLmdControl->
cFile);
632 printf(
"fLmdGetBuffer: LMD read error: unexpected EOF: %s %u %u\n",
633 pLmdControl->
cFile, iReturn, used);
638 if(pLmdControl->
iSwap)
fLmdSwap4((uint32_t *)pMbsHeader,iReturn/4);
639 pLmdControl->
iBytes+=iReturn;
646 if (leftBytes > iBytes) {
647 printf(
"fLmdGetBuffer: stored piece of data (%u) larger than provided buffer (%u)\n",
653 printf(
"fLmdGetBuffer: Internal error pMbsHeader==0\n");
657 memcpy(pMbsHeader, pLmdControl->
pMbsHeader, leftBytes);
659 iReturn =
fLmdReadBuffer(pLmdControl,(
char *)pMbsHeader+leftBytes, iBytes-leftBytes);
661 printf(
"fLmdGetBuffer: EOF: %s\n",pLmdControl->
cFile);
663 printf(
"fLmdGetBuffer: EOF while we have some rest data (%u)\n", leftBytes);
668 if(iReturn > (iBytes-leftBytes)) {
669 printf(
"fLmdGetBuffer: LMD read error %s - too many bytes read %u wants %u",
670 pLmdControl->
cFile, iReturn, iBytes-leftBytes);
674 if(pLmdControl->
iSwap)
fLmdSwap4((uint32_t *)pMbsHeader+leftBytes/4,iReturn/4);
675 pLmdControl->
iBytes += iReturn;
676 leftBytes += iReturn;
679 while(leftBytes >=8){
681 elem_sz = (pm->
iWords+4)*2;
682 if(elem_sz > leftBytes)
break;
684 *iBytesUsed += elem_sz;
688 leftBytes -= elem_sz;
694 printf(
"fLmdGetBuffer: ERROR: internal buffer overflow. Needed:%d available:%d\n",
698 memcpy(pLmdControl->
pBuffer,pm,leftBytes);
712 uint32_t *ps, *pd, i, evsz;
736 if(iReturn <= 0) { printf(
"fLmdGetElement: EOF\n");
return(
GETLMD__EOFILE); }
740 pLmdControl->
iBytes += iReturn;
750 printf (
"fLmdGetElement: Error, full element %u does not fit in buffer %u",
769 if(iReturn <= 0) {printf(
"fLmdGetElement: EOF\n");
return(
GETLMD__EOFILE);}
771 printf(
"fLmdGetBuffer: LMD read error: unexpected EOF: %s\n",pLmdControl->
cFile);
777 printf(
"fLmdGetElement: Error Event %d: size from table is %d, header %d\n",
781 pLmdControl->
iBytes+=iReturn;
792 if(fclose(pLmdControl->
fFile)!=0) {
793 pLmdControl->
fFile=NULL;
796 pLmdControl->
fFile=NULL;
802 IObytes=(int32_t)fread(buffer,1,bytes,pLmdControl->
fFile);
809 IObytes=(int32_t)fwrite(buffer,1,bytes,pLmdControl->
fFile);
817 bytes=pLmdControl->
iBytes;
830 if(pLmdControl->
pTCP != NULL)free(pLmdControl->
pTCP);
838 pLmdControl->
pTCP=NULL;
852 if(pLmdControl != NULL)
853 return(pLmdControl->
iSwap);
861 else printf(
"fLmdSetWrittenEndian: No file header allocated!");
868 else printf(
"fLmdGetWrittenEndian: No file header allocated!");
884 uint32_t elem=0,i,*iff1,*iff2;
889 for(i=pLmdControl->
iElements;i<pLmdControl->iOffsetEntries-1;i++){
890 if((*(iff1+1)-*iff2)>bytes/4)
break;
895 *used=(*iff1-*iff2)*4;
901 for(i=pLmdControl->
iElements;i<pLmdControl->iOffsetEntries-1;i++){
902 if((*(off1+1)-*off2)>bytes/4)
break;
907 *used=(*off1-*off2)*4;
921 printf(
"fLmdGetBuffer: LMD read error: unexpected EOF: %s\n",pLmdControl->
cFile);
927 printf(
"fLmdOffsetTable: LMD format error: no index table: %s, type %0x\n",
940 printf(
"fLmdOffsetTable: LMD format error: no index table: %s\n",pLmdControl->
cFile);
945 if(pLmdControl->
iSwap){
967 memset(pTableHead,0,16);
972 iReturn=fgetpos64(pLmdControl->
fFile,(fpos64_t *) ¤t);
976 if(pbuf == NULL) pbuf=(
char *)pLmdControl->
pOffset8;
984 printf(
"Table offset mismatch: current:%lld calculated:%lld, cur-cal %lld\n",
990 printf(
"Table write error \n");
1013 return(*(pLmdControl->
pOffset8+index));
1021 uint32_t oldEntries,newEntries;
1026 memset(
new,0,newEntries*pLmdControl->
iOffsetSize);
1037 pLmdControl->
pOffset4=(uint32_t *)
new;
1047 pLmdControl->
pOffset4=(uint32_t *)
new;
1057 if(pMbsBufferHeader){
1058 printf(
"BfHd: # %d, DataWords:%d Type:%08x Elements:%d sec:%d.%d MaxWords:%d\n",
1061 pMbsBufferHeader->
iType,
1072 printf(
"FiHd: DataWords:%d Type:%d.%d Elements:%d sec:%d.%d MaxWords:%d Index: %llx[%d]\n",
1074 pMbsFileHeader->
iType&0xffff,
1075 pMbsFileHeader->
iType>>16,
1088 printf(
"ElHd: words:%d type:%08x\n",
1096 if(pMbsEventHeader){
1097 printf(
"EvHd: words:%6d type:%08x trigger:%2d #:%4d\n",
1099 pMbsEventHeader->
iType,
1107 printf(
"Ctrl: file:%s words:%d left:%d bytes read:%lld elements:%d\n",
1121 for(i=0;i<items;i++){
1125 ((*pp >> 8)&0x0000ff00) +
1126 ((*pp << 8)&0x00ff0000) +
1136 for(i=0;i<items;i++){
1138 *pp = (*pp << 32) + (*pp >> 32);
uint32_t fLmdOffsetWrite(sLmdControl *)
#define LMD__STANDARD_HEADER
INTS4 f_stc_close(struct s_tcpcomm *ps_tcp)
INTS4 f_stc_read(void *p_buffer, INTS4 i_buflen, INTS4 i_channel, INTS4 i_timeout)
int32_t fLmdWriteBuffer(sLmdControl *, char *, uint32_t)
#define LMD__ENDIAN_UNKNOWN
#define LMD__NO_OVERWRITE
INTS4 f_stc_write(void *p_buffer, INTS4 i_buflen, INTS4 i_channel)
#define PUTLMD__FILE_EXIST
uint32_t fLmdOffsetSet(sLmdControl *, uint32_t)
uint32_t fLmdCleanup(sLmdControl *)
void fLmdOffsetElements(sLmdControl *, uint32_t, uint32_t *, uint32_t *)
void fLmdPrintFileHeader(uint32_t iVerbose, sMbsFileHeader *pMbsFileHeader)
uint32_t fLmdGetMbsEvent(sLmdControl *pLmdControl, sMbsHeader **event)
uint32_t fLmdGetElement(sLmdControl *pLmdControl, uint32_t iEvent, sMbsHeader **event)
uint32_t fLmdInitMbs(sLmdControl *pLmdControl, char *Nodename, uint32_t iMaxBytes, uint32_t iBuffers, uint32_t iStreams, uint32_t iPort, uint32_t iTimeout)
uint32_t fLmdPutClose(sLmdControl *pLmdControl)
uint32_t fLmdGetClose(sLmdControl *pLmdControl)
INTS4 f_stc_connectserver(CHARS *c_node, INTS4 l_port, INTS4 *pi_channel, struct s_tcpcomm *ps_client)
#define LMD__TYPE_FILE_INDEX_101_2
void fLmdSwap4(uint32_t *array, uint32_t items)
uint64_t fLmdGetBytesWritten(sLmdControl *pLmdControl)
#define GETLMD__OUTOF_RANGE
void fLmdSetWrittenEndian(sLmdControl *pLmdControl, uint32_t iE)
uint32_t fLmdGetBuffer(sLmdControl *pLmdControl, sMbsHeader *pMbsHeader, uint32_t iBytes, uint32_t *iElements, uint32_t *iBytesUsed)
#define GETLMD__SIZE_ERROR
uint32_t fLmdGetWrittenEndian(sLmdControl *pLmdControl)
void fLmdPrintEvent(uint32_t iVerbose, sMbsEventHeader *pMbsEventHeader)
uint32_t fLmdGetOpen(sLmdControl *pLmdControl, char *Filename, sMbsFileHeader *pBuffHead, uint32_t iBytes, uint32_t iUseOffset)
void fLmdPrintHeader(uint32_t iVerbose, sMbsHeader *pMbsHeader)
int32_t fLmdReadBuffer(sLmdControl *pLmdControl, char *buffer, uint32_t bytes)
uint32_t fLmdCloseMbs(sLmdControl *pLmdControl)
#define GETLMD__NOLMDFILE
void fLmdPrintControl(uint32_t iVerbose, sLmdControl *pLmdControl)
lmdoff_t fLmdOffsetGet(sLmdControl *, uint32_t)
sLmdControl * fLmdAllocateControl()
uint32_t fLmdOffsetRead(sLmdControl *)
void fLmdOffsetResize(sLmdControl *, uint32_t)
uint32_t fLmdPutElement(sLmdControl *pLmdControl, sMbsHeader *pHeader)
#define LMD__TYPE_FILE_HEADER_101_1
uint32_t fLmdGetMbsBuffer(sLmdControl *pLmdControl, sMbsBufferHeader *pBuffer, uint32_t iBytes, uint32_t *iElements, uint32_t *iBytesUsed)
uint32_t fLmdPutOpen(sLmdControl *pLmdControl, char *Filename, sMbsFileHeader *pBuffHead, uint32_t iBytes, uint32_t iOver, uint32_t iUseOffset, uint32_t iLargeFile)
uint32_t fLmdGetSwap(sLmdControl *pLmdControl)
sMbsFileHeader * pMbsFileHeader
uint32_t fLmdConnectMbs(sLmdControl *pLmdControl, char *Nodename, uint32_t iPort, uint32_t *iBufferBytes)
#define LMD__INTERNAL_HEADER
uint32_t fLmdPutBuffer(sLmdControl *pLmdControl, sMbsHeader *pHeader, uint32_t Items)
void fLmdPrintBufferHeader(uint32_t iVerbose, sMbsBufferHeader *pMbsBufferHeader)
void fLmdSwap8(uint64_t *array, uint32_t items)