13 #include <sys/types.h>
24 #include <sys/timeb.h>
30 #include <sys/timeb.h>
36 #include <sys/timeb.h>
38 #define fgetpos64 fgetpos
40 #define fseeko64 fseek
41 #define fpos64_t fpos_t
43 #ifndef _HAS_CLOCK_REALTIME
45 #define CLOCK_REALTIME 1
46 int clock_gettime(
int clockid,
struct timespec* tp) {
return 0; }
54 #define fgetpos64 fgetpos
56 #define fseeko64 fseek
57 #define fpos64_t fpos_t
65 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
66 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
68 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
71 #define CLOCK_REALTIME 1
73 int clock_gettime(
int clockid,
struct timespec* tp)
76 unsigned __int64 tmpres = 0;
81 GetSystemTimeAsFileTime(&ft);
83 tmpres |= ft.dwHighDateTime;
85 tmpres |= ft.dwLowDateTime;
89 tmpres -= DELTA_EPOCH_IN_MICROSECS;
90 tp->tv_sec = (long)(tmpres / 1000000UL);
91 tp->tv_nsec = (long)(tmpres % 1000000UL) * 1000;
107 #define OFFSET__ENTRIES 250000
120 struct timespec clock;
134 clock_gettime(CLOCK_REALTIME, &clock);
140 size_t len = strlen(Filename);
141 if (len <
sizeof(pLmdControl->
cFile)) {
142 strncpy(pLmdControl->
cFile, Filename, len);
144 strncpy(pLmdControl->
cFile, Filename,
sizeof(pLmdControl->
cFile) - 1);
148 pLmdControl->
pBuffer = (int16_t*)malloc(iBytes);
155 if ((pLmdControl->
fFile = (FILE*)fopen64(Filename,
"r")) != NULL) {
156 printf(
"fLmdPutOpen: File exists: %s\n", Filename);
158 fclose(pLmdControl->
fFile);
163 if ((pLmdControl->
fFile = (FILE*)fopen64(Filename,
"w+")) == NULL) {
164 printf(
"fLmdPutOpen: Error open file %s\n", Filename);
180 pLmdControl->
iBytes += iReturn;
185 printf(
"fLmdPutOpen: %s. Bytes:%d over:%d table:%d large:%d.\n", Filename, iBytes, iOver, iUseOffset, iLargeFile);
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");
214 pLmdControl->
iBytes += iReturn;
215 if (iReturn != (pHeader->
iWords + 4) * 2) {
216 printf(
"fLmdPutElement: Write error \n");
230 pLmdControl->
iBytes += iReturn;
237 printf(
"fLmdPutElement: Element too big: %d words\n", pHeader->
iWords + 4);
243 iReturn = (pHeader->
iWords + 4) / 2;
244 for (i = 0; i < iReturn; i++) {
261 uint32_t Bytes = 0, TotalBytes = 0, i, elements;
262 int64_t fileleft, used;
268 elements = pLmdControl->
iElements + Items + 2;
269 used = pLmdControl->
iBytes / 4;
270 fileleft = 0xffffffff - used - (4 + elements);
271 for (i = 0; i < Items; i++) {
272 Bytes = (4 + pH->
iWords) * 2;
276 if ((int64_t)TotalBytes / 4 > fileleft) {
277 printf(
"fLmdPutElement: File size exceed\n");
284 for (i = 0; i < Items; i++) {
286 Bytes = (4 + pH->
iWords) * 2;
297 pLmdControl->
iBytes += iReturn;
298 if (iReturn != TotalBytes) {
315 pLmdControl->
iBytes += iReturn;
317 printf(
"fLmdPutClose: Error writing last buffer. Closing file.\n");
319 rewind(pLmdControl->
fFile);
331 rewind(pLmdControl->
fFile);
350 pLmdControl->
iPort = iPort;
356 printf(
"fLmdConnectMbs: Connect to transport server %s port %d\n", Nodename, pLmdControl->
iPort);
359 printf(
"fLmdConnectMbs: Connect to stream server %s port %d\n", Nodename, pLmdControl->
iPort);
363 printf(
"fLmdConnectMbs: Error connect to %s \n", Nodename);
369 printf(
"fLmdConnectMbs: Error read info from %s \n", Nodename);
374 pLmdControl->
iSwap = 1;
376 if (pLmdControl->
iSwap) {
380 printf(
"fLmdConnectMbs: Event spanning not supported!\n");
385 printf(
"fLmdConnectMbs: MBS not in DABC mode!\n");
389 size_t len = strlen(Nodename);
390 if (len <
sizeof(pLmdControl->
cFile)) {
391 strncpy(pLmdControl->
cFile, Nodename, len);
393 strncpy(pLmdControl->
cFile, Nodename,
sizeof(pLmdControl->
cFile) - 1);
417 printf(
"fLmdInitMbs: Event spanning not supported!\n");
421 printf(
"fLmdInitMbs: MBS not in DABC mode!\n");
424 pLmdControl->
iPort = iPort;
425 size_t len = strlen(Nodename);
426 if (len <
sizeof(pLmdControl->
cFile)) {
427 strncpy(pLmdControl->
cFile, Nodename, len);
429 strncpy(pLmdControl->
cFile, Nodename,
sizeof(pLmdControl->
cFile) - 1);
431 if (pLmdControl->
pBuffer == NULL) {
432 pLmdControl->
pBuffer = (int16_t*)malloc(iMaxBytes);
446 const char cClose[12] =
"CLOSE";
451 printf(
"fLmdCloseMbs: Error writing CLOSE with f_stc_write!\n");
457 pLmdControl->
pTCP = NULL;
493 uint32_t usedBytes = 0, leftBytes = 0;
495 const char cRequest[12] =
"GETEVT";
504 printf(
"fLmdGetMbsBuffer: Need buffer to read\n");
508 printf(
"fLmdGetMbsBuffer: %s buffer size %d too small for %lx bytes\n",
528 if (pLmdControl->
iSwap) {
532 printf(
"fLmdGetMbsBuffer: %s buffer size %d too small for %lx bytes\n",
539 if ((pBuf->
iType & 0xffff) == 100) {
540 iReturn =
f_stc_read((int32_t*)(pBuf + 1), usedBytes, pLmdControl->
iTCP, -1);
548 if (pLmdControl->
iSwap) {
551 if (iBytesUsed != NULL) {
554 if (iElements != NULL) {
557 pLmdControl->
iBytes += usedBytes;
588 size_t len = strlen(Filename);
589 if (len <
sizeof(pLmdControl->
cFile)) {
590 strncpy(pLmdControl->
cFile, Filename, len);
592 strncpy(pLmdControl->
cFile, Filename,
sizeof(pLmdControl->
cFile) - 1);
594 if ((pLmdControl->
fFile = (FILE*)fopen64(Filename,
"r")) == NULL) {
595 printf(
"fLmdGetOpen: File not found: %s\n", Filename);
603 printf(
"fLmdGetOpen: LMD format error: no LMD file: %s\n", Filename);
609 pLmdControl->
iSwap = 1;
611 if (pLmdControl->
iSwap) {
612 printf(
"do swap !!!\n");
617 printf(
"fLmdGetOpen: LMD format error: no LMD file: %s, type is %0x\n",
629 printf(
"fLmdGetOpen: Index format error: %s\n", Filename);
635 pLmdControl->
iBytes += iReturn;
643 printf(
"fLmdGetOpen: LMD format error: no LMD file: %s\n", Filename);
649 bufferBytes = iBytes;
650 if (bufferBytes < pLmdControl->pMbsFileHeader->iMaxWords * 2
655 pLmdControl->
pBuffer = (int16_t*)malloc(bufferBytes);
658 printf(
"fLmdGetOpen: %s words %u\n", Filename, pLmdControl->
iBufferWords);
673 uint32_t elem = 0, leftBytes = 0, used, elem_sz;
676 if (iBytes < pLmdControl->pMbsFileHeader->iMaxWords) {
677 printf(
"fLmdGetBuffer: %s buffer size %d too small for %d bytes\n",
683 if (pMbsHeader == NULL) {
684 printf(
"fLmdGetBuffer: Need buffer to read\n");
699 printf(
"fLmdGetBuffer: EOF: %s\n", pLmdControl->
cFile);
702 if (iReturn != used) {
703 printf(
"fLmdGetBuffer: LMD read error: unexpected EOF: %s %u %u\n", pLmdControl->
cFile, iReturn, used);
708 if (pLmdControl->
iSwap) {
711 pLmdControl->
iBytes += iReturn;
718 if (leftBytes > iBytes) {
719 printf(
"fLmdGetBuffer: stored piece of data (%u) larger than provided buffer (%u)\n", leftBytes, iBytes);
724 printf(
"fLmdGetBuffer: Internal error pMbsHeader==0\n");
728 memcpy(pMbsHeader, pLmdControl->
pMbsHeader, leftBytes);
730 iReturn =
fLmdReadBuffer(pLmdControl, (
char*)pMbsHeader + leftBytes, iBytes - leftBytes);
732 printf(
"fLmdGetBuffer: EOF: %s\n", pLmdControl->
cFile);
734 printf(
"fLmdGetBuffer: EOF while we have some rest data (%u)\n", leftBytes);
740 if (iReturn > (iBytes - leftBytes)) {
741 printf(
"fLmdGetBuffer: LMD read error %s - too many bytes read %u wants %u",
748 if (pLmdControl->
iSwap) {
751 pLmdControl->
iBytes += iReturn;
752 leftBytes += iReturn;
755 while (leftBytes >= 8) {
759 elem_sz = (pm->
iWords + 4) * 2;
760 if (elem_sz > leftBytes) {
764 *iBytesUsed += elem_sz;
768 leftBytes -= elem_sz;
774 printf(
"fLmdGetBuffer: ERROR: internal buffer overflow. Needed:%d available:%d\n",
779 memcpy(pLmdControl->
pBuffer, pm, leftBytes);
800 if (pLmdControl->
pBuffer == NULL) {
815 printf(
"fLmdGetElement: EOF\n");
819 if (pLmdControl->
iSwap) {
823 pLmdControl->
iBytes += iReturn;
842 printf(
"fLmdGetElement: EOF\n");
846 if (pLmdControl->
iSwap) {
850 pLmdControl->
iBytes += iReturn;
861 "fLmdGetElement: Error, full element %u does not fit in buffer %u", evsz, pLmdControl->
iLeftWords * 2);
878 int val = fseeko64(pLmdControl->
fFile,
fLmdOffsetGet(pLmdControl, iEvent - 1) * 4, SEEK_SET);
885 printf(
"fLmdGetElement: EOF\n");
888 if (iReturn != (i * 4)) {
889 printf(
"fLmdGetBuffer: LMD read error: unexpected EOF: %s\n", pLmdControl->
cFile);
892 if (pLmdControl->
iSwap) {
897 printf(
"fLmdGetElement: Error Event %d: size from table is %d, header %d\n",
903 pLmdControl->
iBytes += iReturn;
915 if (fclose(pLmdControl->
fFile) != 0) {
916 pLmdControl->
fFile = NULL;
919 pLmdControl->
fFile = NULL;
926 IObytes = (int32_t)fread(buffer, 1, bytes, pLmdControl->
fFile);
934 IObytes = (int32_t)fwrite(buffer, 1, bytes, pLmdControl->
fFile);
943 bytes = pLmdControl->
iBytes;
958 if (pLmdControl->
pTCP != NULL) {
959 free(pLmdControl->
pTCP);
961 if (pLmdControl->
cHeader != NULL) {
964 if (pLmdControl->
pOffset4 != NULL) {
967 if (pLmdControl->
pOffset8 != NULL) {
976 pLmdControl->
pTCP = NULL;
989 if (pLmdControl != NULL) {
990 return (pLmdControl->
iSwap);
1002 printf(
"fLmdSetWrittenEndian: No file header allocated!");
1012 printf(
"fLmdGetWrittenEndian: No file header allocated!");
1028 uint32_t elem = 0, i, *iff1, *iff2;
1033 for (i = pLmdControl->
iElements; i < pLmdControl->iOffsetEntries - 1; i++) {
1034 if ((*(iff1 + 1) - *iff2) > bytes / 4) {
1041 *used = (*iff1 - *iff2) * 4;
1046 for (i = pLmdControl->
iElements; i < pLmdControl->iOffsetEntries - 1; i++) {
1047 if ((*(off1 + 1) - *off2) > bytes / 4) {
1054 *used = (*off1 - *off2) * 4;
1072 if (iReturn != 16) {
1073 printf(
"fLmdGetBuffer: LMD read error: unexpected EOF: %s\n", pLmdControl->
cFile);
1077 if (pLmdControl->
iSwap) {
1082 "fLmdOffsetTable: LMD format error: no index table: %s, type %0x\n", pLmdControl->
cFile, pTableHead->
iType);
1093 printf(
"fLmdOffsetTable: LMD format error: no index table: %s\n", pLmdControl->
cFile);
1097 if (pLmdControl->
iSwap) {
1123 memset(pTableHead, 0, 16);
1128 iReturn = fgetpos64(pLmdControl->
fFile, (fpos64_t*)¤t);
1131 pbuf = (
char*)pLmdControl->
pOffset4;
1133 pbuf = (
char*)pLmdControl->
pOffset8;
1143 printf(
"Table offset mismatch: current:%lld calculated:%lld, cur-cal %lld\n",
1144 (
long long int)(current / 4),
1150 printf(
"Table write error \n");
1176 return (*(pLmdControl->
pOffset8 + index));
1192 memset(
new, 0, newEntries * pLmdControl->
iOffsetSize);
1193 if (oldEntries > 0) {
1221 *pLmdControl->
pOffset4 = firstValue;
1231 if (pMbsBufferHeader) {
1232 printf(
"BfHd: # %d, DataWords:%d Type:%08x Elements:%d sec:%d.%d MaxWords:%d\n",
1235 pMbsBufferHeader->
iType,
1247 if (pMbsFileHeader) {
1248 printf(
"FiHd: DataWords:%d Type:%d.%d Elements:%d sec:%d.%d MaxWords:%d Index: %llx[%d]\n",
1250 pMbsFileHeader->
iType & 0xffff,
1251 pMbsFileHeader->
iType >> 16,
1256 (
long long unsigned int)(pMbsFileHeader->
iTableOffset),
1266 printf(
"ElHd: words:%d type:%08x\n", pMbsHeader->
iWords, pMbsHeader->
iType);
1274 if (pMbsEventHeader) {
1275 printf(
"EvHd: words:%6d type:%08x trigger:%2d #:%4d\n",
1277 pMbsEventHeader->
iType,
1287 printf(
"Ctrl: file:%s words:%d left:%d bytes read:%lld elements:%d\n",
1291 (
long long int)(pLmdControl->
iBytes),
1302 for (i = 0; i < items; i++) {
1304 *pp = (*pp >> 24) + ((*pp >> 8) & 0x0000ff00) + ((*pp << 8) & 0x00ff0000) + (*pp << 24);
1315 for (i = 0; i < items; i++) {
1317 *pp = (*pp << 32) + (*pp >> 32);
uint32_t fLmdOffsetWrite(sLmdControl *)
#define LMD__STANDARD_HEADER
INTS4 f_stc_close(struct s_tcpcomm *ps_tcp)
int32_t fLmdWriteBuffer(sLmdControl *, char *, uint32_t)
#define LMD__ENDIAN_UNKNOWN
#define LMD__NO_OVERWRITE
#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)
INTS4 f_stc_write(INTS1 *p_buffer, INTS4 i_buflen, INTS4 i_channel)
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)
INTS4 f_stc_read(INTS1 *p_buffer, INTS4 i_buflen, INTS4 i_channel, INTS4 i_timeout)
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)