-
Notifications
You must be signed in to change notification settings - Fork 0
/
readnoaaport.c
1243 lines (1089 loc) · 40 KB
/
readnoaaport.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2011, University Corporation for Atmospheric Research.
* See COPYRIGHT file for copying and redistribution conditions.
*/
/**
* @file readnoaaport.c
*
* This file contains the code for the \c readnoaaport(1) program. This
* program reads NOAAPORT data from a shared-memory FIFO or a file, creates
* LDM data-products, and writes the data-products into an LDM product-queue.
*/
#define _XOPEN_SOURCE 500
#define __EXTENSIONS__
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include "nport.h"
#include "shmfifo.h"
#include "ldm.h"
#undef PACKAGE
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#undef VERSION
#include "globals.h"
#include "md5.h"
#include "setenv.h"
#include "libpng/png.h"
#include "noaaportLog.h"
#include "dvbs.h"
#include "config.h"
#include "ldmProductQueue.h"
#ifndef HAVE_GET_QUEUE_PATH
#include "paths.h" /* pre LDM 6.9 style */
#endif
/*
* Function prototypes
*/
size_t prodalloc(long int nfrags, long int dbsize, char **heap);
void ds_init(int nfrags);
void ds_free();
datastore *ds_alloc();
void pngout_end();
void process_prod(prodstore prod, char *PROD_NAME,
char *memheap, size_t heapsize, MD5_CTX * md5try,
LdmProductQueue* lpq, psh_struct * psh, sbn_struct * sbn);
void pngwrite(char *memheap);
void png_set_memheap(char *memheap, MD5_CTX * md5ctxp);
void png_header(char *memheap, int length);
void pngout_init(int width, int height);
int png_get_prodlen();
int prod_isascii(char *pname, char *prod, size_t psize);
static LdmProductQueue* ldmProdQueue = NULL;
static unsigned long idle = 0;
static fd_set readfds;
static fd_set exceptfds;
static int DONE = 0;
static const char* FOS_TRAILER = "\015\015\012\003";
static struct shmhandle* shm = NULL;
/*
* Output statistics if requested
*/
static volatile sig_atomic_t logstats = 0;
static unsigned long nmissed = 0;
static void dump_stats(void)
{
nplNotice("----------------------------------------\0");
nplNotice("Ingestion Statistics:\0");
nplNotice(" Number of missed packets %lu\0", nmissed);
nplNotice("----------------------------------------\0");
}
static void cleanup(void)
{
nplNotice("Exiting...\0");
dump_stats();
(void)lpqClose(ldmProdQueue);
if (shm != NULL) {
shmfifo_detach(shm);
shmfifo_free(shm);
shm = NULL;
}
}
/*
* Called upon receipt of signals
*/
static void signal_handler(
int sig)
{
#ifdef SVR3SIGNALS
/*
* Some systems reset handler to SIG_DFL upon entry to handler.
* In that case, we reregister our handler.
*/
(void)signal(sig, signal_handler);
#endif
switch (sig) {
case SIGINT:
exit(0);
case SIGTERM:
exit(0);
case SIGPIPE:
return;
case SIGUSR1:
logstats = 1;
return;
case SIGUSR2:
rollulogpri();
return;
}
}
/*
* Register the signal_handler
*/
static void set_sigactions(void)
{
struct sigaction sigact;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
/* Ignore these */
sigact.sa_handler = SIG_IGN;
(void)sigaction(SIGHUP, &sigact, NULL);
(void)sigaction(SIGALRM, &sigact, NULL);
(void)sigaction(SIGCHLD, &sigact, NULL);
/* Handle these */
#ifdef SA_RESTART /* SVR4, 4.3+ BSD */
/* usually, restart system calls */
sigact.sa_flags |= SA_RESTART;
#endif
sigact.sa_handler = signal_handler;
(void)sigaction(SIGTERM, &sigact, NULL);
(void)sigaction(SIGUSR1, &sigact, NULL);
(void)sigaction(SIGUSR2, &sigact, NULL);
/* Don't restart after interrupt */
sigact.sa_flags = 0;
#ifdef SA_INTERRUPT /* SunOS 4.x */
sigact.sa_flags |= SA_INTERRUPT;
#endif
(void)sigaction(SIGINT, &sigact, NULL);
(void)sigaction(SIGPIPE, &sigact, NULL);
}
static void usage(
const char* const av0) /* id string */
{
(void)fprintf(stderr, "Usage: %s [options] feedname\t\nOptions:\n", av0);
(void)fprintf(stderr,
"\t-v Verbose, tell me about each product\n");
(void)fprintf(stderr, "\t-n Log notice messages\n");
(void)fprintf(stderr, "\t-x Log debug messages\n");
(void)fprintf(stderr, "\t-l logfile Default logs to syslogd\n");
(void)fprintf(stderr,
"\t-f type Claim to be feedtype \"type\", "
"one of \"hds\", \"ddplus\", ...\n");
#ifdef HAVE_GET_QUEUE_PATH
(void)fprintf(stderr, "\t-q queue default \"%s\"\n", getQueuePath());
#else
(void)fprintf(stderr, "\t-q queue default \"%s\"\n", DEFAULT_QUEUE);
#endif
(void)fprintf(stderr, "\t-u number default LOCAL0\n");
exit(1);
}
/*
* Reads data from the FIFO to a buffer.
*
* Arguments:
* buf Pointer to the buffer into which to put the data.
* want The amount of data to read in bytes.
* Returns:
* 0 Success.
* -1 Pre-condition failure. Error-message logged.
* -2 I/O error. Error-message logged.
*/
static int _shm_bufread(
char* const buf,
const int want)
{
int status; /* return status */
if (shm == NULL) {
nplError("_shm_bufread(): NULL shared-memory pointer");
DONE = 1;
status = -1; /* pre-condition failure */
}
else if (NULL == buf) {
nplError("_shm_bufread(): NULL buffer pointer");
status = -1; /* pre-condition failure */
}
else if (0 > want) {
nplError("_shm_bufread(): Negative number of bytes to read: %d", want);
status = -1; /* pre-condition failure */
}
else {
int got; /* count of "buf" bytes */
if (ulogIsDebug())
nplDebug("shm_bufread %d", want);
status = 0; /* success */
for (got = 0; got < want; ) {
static char msgbuf[10000]; /* I/O buffer */
static char* from; /* next "msgbuf" byte */
static int left = 0; /* remaining "msgbuf" bytes */
int ncopy; /* number of bytes to copy */
if (left == 0) {
if (shmfifo_get(shm, msgbuf, sizeof(msgbuf), &left) != 0) {
status = -2; /* I/O error */
break;
}
from = msgbuf;
}
ncopy = want - got;
if (ncopy > left) {
nplError("Can \"want\" exceed 1 packet?");
ncopy = left;
}
(void)memcpy(buf + got, from, ncopy);
left -= ncopy;
from += ncopy ;
got += ncopy;
} /* while more data needed */
} /* pre-conditions satisfied */
return status;
}
/*
* Reads data from a file descriptor.
*
* Arguments:
* fd The file descriptor from which to read data.
* buf Pointer to the buffer into which to put the data.
* want The amount of data to read in bytes.
* Returns:
* 0 Success.
* -2 I/O error. Error-message logged.
* -3 End-of-file.
*/
static int _fd_bufread(
const int fd,
char* const buf,
const int bsiz)
{
int width;
int ready;
struct timeval timeo;
size_t bread = 0;
static int TV_SEC = 30;
width = fd + 1;
while (bread < bsiz) {
timeo.tv_sec = TV_SEC;
timeo.tv_usec = 0;
FD_ZERO(&readfds);
FD_ZERO(&exceptfds);
FD_SET(fd, &readfds);
FD_SET(fd, &exceptfds);
ready = select(width, &readfds, 0, &exceptfds, &timeo);
/* timeo may be modified, don't rely on value now */
if (ready < 0) {
if (errno != EINTR)
nplSerror("select");
else
nplNotice("select received interupt\0");
errno = 0;
continue;
}
if (ready == 0) {
idle += TV_SEC;
if (idle > 600) {
if (ulogIsVerbose())
nplInfo("Idle for 600 seconds");
idle = 0;
}
else if (ulogIsDebug()) {
nplDebug("Idle for %d seconds", idle);
}
continue; /* was return(-1) */
}
if (FD_ISSET(fd, &readfds) || FD_ISSET(fd, &exceptfds)) {
size_t nread;
idle = 0;
nread = read(fd, buf + bread, (size_t) bsiz - bread);
bread = bread + nread;
if (nread == -1) {
nplSerror("_fd_bufread(): read() failure");
return -2;
}
if (nread == 0) {
if (ulogIsVerbose())
nplInfo("End of Input");
DONE = 1;
return -3;
}
if (bread < (size_t) bsiz) {
FD_CLR(fd, &readfds);
FD_CLR(fd, &exceptfds);
}
else {
return 0;
}
}
else {
nplError("_fd_bufread(): select() returned %d but fd not set", ready);
idle += TV_SEC;
return -2;
}
}
return 0;
}
/*
* Reads data.
*
* Arguments:
* fd The file descriptor from which to read data if "shm"
* is NULL.
* buf Pointer to the buffer into which to put the data.
* want The amount of data to read in bytes.
* Returns:
* 0 Success.
* -1 Pre-condition failure. Error-message logged.
* -2 I/O error. Error-message logged.
* -3 End-of-file.
*/
static int bufread(
const int fd,
char* const buf,
const int bsiz)
{
return (shm == NULL)
? _fd_bufread(fd, buf, bsiz)
: _shm_bufread(buf, bsiz);
}
/**
* Reads NOAAPORT data from a shared-memory FIFO or a file, creates LDM
* data-products, and inserts the data-products into an LDM product-queue.
*
* Usage:
*
* readnoaaport [-nvx] [-q <em>queue</em>] [-u <em>n</em>] [-m mcastAddr] [path]\n
*
* Where:
* <dl>
* <dt>-l <em>log</em></dt>
* <dd>Log to \e log. if \e log is "-", then logging occurs to the
* standard error stream; otherwise, \e log is the pathname of a file to
* which logging will occur. If not specified, then log messages will go
* to the system logging daemon. </dd>
*
* <dt>-m <em>mcastAddr</em></dt>
* <dd>Use the shared-memory FIFO associated with the UDP
* multicast address \e mcastAddr.</dd>
*
* <dt>-n</dt>
* <dd>Log messages of level NOTICE and higher priority.</dd>
*
* <dt>-q <em>queue</em></dt>
* <dd>Use \e queue as the pathname of the LDM product-queue. The default
* is to use the default LDM pathname of the product-queue.</dd>
*
* <dt>-u <em>n</em></dt>
* <dd>If logging is to the system logging daemon, then use facility
* <b>local</b><em>n</em>. The default is to use the LDM facility.
*
* <dt>-v</dt>
* <dd>Log messages of level INFO and higher priority. Each data-product
* will generate a log message.</dd>
*
* <dt>-x</dt>
* <dd>Log messages of level DEBUG and higher priority.</dd>
*
* <dt><em>path</em></dt>
* <dd>Pathname of the file from which to read data. The default is to use
* a shared-memory FIFO.</dd>
* </dl>
*
* @retval 0 if successful.
* @retval 1 if an error occurred. At least one error-message is logged.
*/
int main(
const int argc,
char* const argv[])
{
#ifdef HAVE_GET_QUEUE_PATH
const char* pqfname = getQueuePath();
#else
const char* pqfname = DEFAULT_QUEUE;
#endif
int fd;
char* prodmmap;
char* memheap = NULL;
size_t heapsize;
size_t heapcount;
unsigned char b1;
int cnt, dataoff, datalen, deflen;
int nscan;
long IOFF;
int NWSTG, GOES, PNGINIT = 0, PROD_COMPRESSED;
long last_sbn_seqno = (-1);
char PROD_NAME[1024];
int status;
prodstore prod;
sbn_struct* sbn;
pdh_struct* pdh;
psh_struct* psh;
ccb_struct* ccb;
pdb_struct* pdb;
datastore* pfrag;
extern int optind;
extern int opterr;
extern char* optarg;
int ch;
int logmask = LOG_MASK(LOG_ERR);
const char* logfname = NULL; /* use system logging daemon */
unsigned logOptions = LOG_CONS | LOG_PID;
unsigned logFacility = LOG_LDM; /* use default LDM facility */
const char* const progName = ubasename(argv[0]);
MD5_CTX* md5ctxp = NULL;
/*unsigned char *compr;
long comprLen = 10000 * sizeof (int);*/
int pid_channel = -1;
/*compr = (unsigned char *) calloc (comprLen, 1);*/
/* Initialize the logger. */
(void)setulogmask(logmask);
(void)openulog(progName, logOptions, logFacility, logfname);
opterr = 1;
while ((ch = getopt(argc, argv, "nvxl:q:u:m:")) != EOF) {
switch (ch) {
case 'v':
logmask |= LOG_MASK(LOG_INFO);
(void)setulogmask(logmask);
break;
case 'x':
logmask |= LOG_MASK(LOG_DEBUG);
(void)setulogmask(logmask);
break;
case 'n':
logmask |= LOG_MASK(LOG_NOTICE);
(void)setulogmask(logmask);
break;
case 'l':
if (optarg[0] == '-' && optarg[1] != 0) {
nplError("logfile \"%s\" ??\n", optarg);
usage(argv[0]);
}
/* else */
logfname = optarg;
(void)openulog(progName, logOptions, logFacility, logfname);
break;
case 'q':
pqfname = optarg;
break;
case 'u': {
int i = atoi(optarg);
if (0 <= i && 7 >= i) {
static int logFacilities[] = {LOG_LOCAL0, LOG_LOCAL1,
LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6,
LOG_LOCAL7};
logFacility = logFacilities[i];
(void)openulog(progName, logOptions, logFacility, logfname);
}
break;
}
case 'm':
sscanf(optarg, "%*d.%*d.%*d.%d", &pid_channel);
if ((pid_channel < 1) || (pid_channel > MAX_DVBS_PID)) {
pid_channel = -1;
}
else {
shm = shmfifo_new();
cnt = 0;
while (((status = shmfifo_shm_from_key(shm,
s_port[pid_channel - 1])) == -3) && (cnt < 30)) {
nplInfo("Trying to get shared-memory FIFO");
cnt++;
sleep(1);
}
if (0 != status) {
nplError("Couldn't get shared-memory FIFO. "
"Check associated dvbs_multicast(1) process.");
shmfifo_free(shm);
shm = NULL;
}
else {
nplInfo("Got shared-memory FIFO");
}
}
break;
case '?':
usage(argv[0]);
break;
}
}
if (argc - optind < 0)
usage(argv[0]);
nplNotice("Starting Up %s", PACKAGE_VERSION);
fd = ((argc - optind) == 0)
? fileno(stdin)
: open(argv[optind], O_RDONLY, 0);
if ((!shm) && (fd == -1)) {
nplError("could not open input file");
exit(0);
}
/*
* Set up signal handlers
*/
set_sigactions();
/*
* Register atexit routine
*/
if (atexit(cleanup) != 0) {
nplSerror("atexit");
exit(-1);
}
sbn = (sbn_struct*)malloc(sizeof(sbn_struct));
pdh = (pdh_struct*)malloc(sizeof(pdh_struct));
psh = (psh_struct*)malloc(sizeof(psh_struct));
ccb = (ccb_struct*)malloc(sizeof(ccb_struct));
pdb = (pdb_struct*)malloc(sizeof(pdb_struct));
prodmmap = (char*)malloc(10000);
if (prodmmap == NULL) {
nplError("could not allocate read buffer");
exit(-1);
}
md5ctxp = new_MD5_CTX();
prod.head = NULL;
prod.tail = NULL;
if (lpqGet(pqfname, &ldmProdQueue) != 0) {
NPL_ADD1("Couldn't open LDM product-queue \"%s\"", pqfname);
exit(1);
}
while (DONE == 0) {
/* See if any stats need to be logged */
if (logstats) {
logstats = 0;
dump_stats();
}
/* Look for first byte == 255 and a valid SBN checksum */
if ((status = bufread(fd, prodmmap, 1)) != 0) {
if (-3 == status)
break;
abort();
}
if ((b1 = (unsigned char)prodmmap[0]) != 255) {
if (ulogIsVerbose())
nplInfo("trying to resync %u", b1);
if (ulogIsDebug())
nplDebug("bufread loop");
continue;
}
if (bufread(fd, prodmmap + 1, 15) != 0) {
if (ulogIsDebug())
nplDebug("couldn't read 16 bytes for sbn");
continue;
}
while ((status = readsbn(prodmmap, sbn)) != 0) {
if (ulogIsDebug())
nplDebug("Not SBN start");
IOFF = 1;
while ((IOFF < 16) && ((b1 = (unsigned char) prodmmap[IOFF]) !=
255))
IOFF++;
if (IOFF > 15) {
break;
}
else {
for (ch = IOFF; ch < 16; ch++)
prodmmap[ch - IOFF] = prodmmap[ch];
if (bufread(fd, prodmmap + 16 - IOFF, IOFF) != 0) {
if (ulogIsDebug())
nplDebug("Couldn't read bytes for SBN, resync");
break;
}
}
}
if (status != 0) {
if (ulogIsDebug())
nplDebug("SBN status continue");
continue;
}
IOFF = 0;
if (bufread(fd, prodmmap + 16, 16) != 0) {
if (ulogIsDebug())
nplDebug("error reading Product Definition Header");
continue;
}
if (ulogIsDebug())
nplDebug("***********************************************");
if (last_sbn_seqno != -1) {
if (sbn->seqno != last_sbn_seqno + 1) {
nplNotice("Gap in SBN sequence number %ld to %ld [skipped %ld]",
last_sbn_seqno, sbn->seqno,
sbn->seqno - last_sbn_seqno - 1);
if ( sbn->seqno > last_sbn_seqno )
nmissed = nmissed +
(unsigned long)(sbn->seqno - last_sbn_seqno - 1);
}
}
last_sbn_seqno = sbn->seqno;
if (ulogIsVerbose())
nplInfo("SBN seqnumber %ld", sbn->seqno);
if (ulogIsVerbose())
nplInfo("SBN datastream %d command %d", sbn->datastream,
sbn->command);
if (ulogIsDebug())
nplDebug("SBN version %d length offset %d", sbn->version, sbn->len);
if (((sbn->command != 3) && (sbn->command != 5)) ||
(sbn->version != 1)) {
nplError("Unknown sbn command/version %d PUNT", sbn->command);
continue;
}
switch (sbn->datastream) {
case 7: /* test */
case 6: /* was reserved...now nwstg2 */
case 5:
NWSTG = 1;
GOES = 0;
break;
case 1:
case 2:
case 4:
NWSTG = 0;
GOES = 1;
break;
default:
nplError("Unknown NOAAport channel %d PUNT", sbn->datastream);
continue;
}
/* End of SBN version low 4 bits */
if (readpdh(prodmmap + IOFF + sbn->len, pdh) == -1) {
nplError("problem with pdh, PUNT");
continue;
}
if (pdh->len > 16) {
bufread(fd, prodmmap + sbn->len + 16, pdh->len - 16);
}
if (ulogIsDebug())
nplDebug("Product definition header version %d pdhlen %d",
pdh->version, pdh->len);
if (pdh->version != 1) {
nplError("Error: PDH transfer type %u, PUNT", pdh->transtype);
continue;
}
else if (ulogIsDebug()) {
nplDebug("PDH transfer type %u", pdh->transtype);
}
if ((pdh->transtype & 8) > 0)
nplError("Product transfer flag error %u", pdh->transtype);
if ((pdh->transtype & 32) > 0)
nplError("Product transfer flag error %u", pdh->transtype);
if ((pdh->transtype & 16) > 0) {
PROD_COMPRESSED = 1;
if (ulogIsDebug())
nplDebug("Product transfer flag compressed %u", pdh->transtype);
}
else {
PROD_COMPRESSED = 0;
}
if (ulogIsDebug())
nplDebug("header length %ld [pshlen = %d]", pdh->len + pdh->pshlen,
pdh->pshlen);
if (ulogIsDebug())
nplDebug("blocks per record %ld records per block %ld\n",
pdh->blocks_per_record, pdh->records_per_block);
if (ulogIsDebug())
nplDebug("product seqnumber %ld block number %ld data block size "
"%ld", pdh->seqno, pdh->dbno, pdh->dbsize);
/* Stop here if no psh */
if ((pdh->pshlen == 0) && (pdh->transtype == 0)) {
IOFF = IOFF + sbn->len + pdh->len;
continue;
}
if (pdh->pshlen != 0) {
if (bufread(fd, prodmmap + sbn->len + pdh->len, pdh->pshlen) != 0) {
nplError("problem reading psh");
continue;
}
else {
if (ulogIsDebug())
nplDebug("read psh %d", pdh->pshlen);
}
/* Timing block */
if (sbn->command == 5) {
if (ulogIsDebug())
nplDebug("Timing block recieved %ld %ld\0", psh->olen,
pdh->len);
/*
* Don't step on our psh of a product struct of prod in
* progress.
*/
continue;
}
if (readpsh(prodmmap + IOFF + sbn->len + pdh->len, psh) == -1) {
nplError("problem with readpsh");
continue;
}
if (psh->olen != pdh->pshlen) {
nplError("ERROR in calculation of psh len %ld %ld", psh->olen,
pdh->len);
continue;
}
if (ulogIsDebug())
nplDebug("len %ld", psh->olen);
if (ulogIsDebug())
nplDebug("product header flag %d, version %d", psh->hflag,
psh->version);
if (ulogIsDebug())
nplDebug("prodspecific data length %ld", psh->psdl);
if (ulogIsDebug())
nplDebug("bytes per record %ld", psh->bytes_per_record);
if (ulogIsDebug())
nplDebug("Fragments = %ld category %d ptype %d code %d",
psh->frags, psh->pcat, psh->ptype, psh->pcode);
if (psh->frags < 0)
nplError("check psh->frags %d", psh->frags);
if (psh->origrunid != 0)
nplError("original runid %d", psh->origrunid);
if (ulogIsDebug())
nplDebug("next header offset %ld", psh->nhoff);
if (ulogIsDebug())
nplDebug("original seq number %ld", psh->seqno);
if (ulogIsDebug())
nplDebug("receive time %ld", psh->rectime);
if (ulogIsDebug())
nplDebug("transmit time %ld", psh->transtime);
if (ulogIsDebug())
nplDebug("run ID %ld", psh->runid);
if (ulogIsDebug())
nplDebug("original run id %ld", psh->origrunid);
if (prod.head != NULL) {
nplError("OOPS, start of new product [%ld ] with unfinished "
"product %ld", pdh->seqno, prod.seqno);
ds_free();
prod.head = NULL;
prod.tail = NULL;
if (PNGINIT != 0) {
pngout_end();
PNGINIT = 0;
}
nplError("Product definition header version %d pdhlen %d",
pdh->version, pdh->len);
nplError("PDH transfer type %u", pdh->transtype);
if ((pdh->transtype & 8) > 0)
nplError("Product transfer flag error %u", pdh->transtype);
if ((pdh->transtype & 32) > 0)
nplError("Product transfer flag error %u", pdh->transtype);
nplError("header length %ld [pshlen = %d]",
pdh->len + pdh->pshlen, pdh->pshlen);
nplError("blocks per record %ld records per block %ld",
pdh->blocks_per_record, pdh->records_per_block);
nplError("product seqnumber %ld block number %ld data block "
"size %ld", pdh->seqno, pdh->dbno, pdh->dbsize);
nplError("product header flag %d", psh->hflag);
nplError("prodspecific data length %ld", psh->psdl);
nplError("bytes per record %ld", psh->bytes_per_record);
nplError("Fragments = %ld category %d", psh->frags, psh->pcat);
if (psh->frags < 0)
nplError("check psh->frags %d", psh->frags);
if (psh->origrunid != 0)
nplError("original runid %d", psh->origrunid);
nplError("next header offset %ld", psh->nhoff);
nplError("original seq number %ld", psh->seqno);
nplError("receive time %ld", psh->rectime);
nplError("transmit time %ld", psh->transtime);
nplError("run ID %ld", psh->runid);
nplError("original run id %ld", psh->origrunid);
}
prod.seqno = pdh->seqno;
prod.nfrag = psh->frags;
ds_init(prod.nfrag);
/* NWSTG CCB = dataoff, WMO = dataoff + 24 */
if (bufread(fd, prodmmap + sbn->len + pdh->len + pdh->pshlen,
pdh->dbsize) != 0) {
nplError("problem reading datablock");
continue;
}
if (sbn->datastream == 4) {
if (psh->pcat != 3) {
GOES = 0;
NWSTG = 1;
}
}
heapcount = 0;
MD5Init(md5ctxp);
if (GOES == 1) {
if (readpdb(prodmmap + IOFF + sbn->len + pdh->len + pdh->pshlen,
psh, pdb, PROD_COMPRESSED, pdh->dbsize) == -1) {
nplError("Error reading pdb, punt");
continue;
}
memcpy(PROD_NAME, psh->pname, sizeof(PROD_NAME));
if (ulogIsDebug())
nplDebug("Read GOES %d %d %d [%d] %d", sbn->len, pdh->len,
pdh->pshlen, sbn->len + pdh->len + pdh->pshlen,
pdb->len);
/* Data starts at first block after pdb */
ccb->len = 0;
heapsize = prodalloc(psh->frags, 5152, &memheap);
}
if (NWSTG == 1) {
memset(psh->pname, 0, sizeof(psh->pname));
if (readccb(prodmmap + IOFF + sbn->len + pdh->len + pdh->pshlen,
ccb, psh, pdh->dbsize) == -1)
nplError("Error reading ccb, using default name");
if (ulogIsDebug())
nplDebug("look at ccb start %d %d", ccb->b1, ccb->len);
/*
cnt = 0;
memset(psh->pname,0,sizeof(psh->pname));
while ((b1 = (unsigned char)prodmmap[
IOFF + sbn->len + pdh->len + pdh->pshlen + ccb->len +
cnt]) >= 32) {
psh->pname[cnt] = prodmmap[
IOFF + sbn->len + pdh->len + pdh->pshlen + ccb->len +
cnt];
cnt++;
}
if(cnt > 0)
*/
if (ulogIsVerbose())
nplInfo("%s", psh->pname);
memcpy(PROD_NAME, psh->pname, sizeof(PROD_NAME));
heapsize = prodalloc(psh->frags, 4000 + 15, &memheap);
/*
* We will only compute md5 checksum on the data, 11 FOS
* characters at start
*/
/*
* sprintf(memheap,"\001\015\015\012%04d\015\015\012",
* ((int)pdh->seqno)%10000);
*/
sprintf(memheap, "\001\015\015\012%03d\040\015\015\012",
((int) pdh->seqno) % 1000);
heapcount += 11;
if (psh->metaoff > 0)
psh->metaoff = psh->metaoff + 11;
}
}
else {
/* If a continuation record...don't let psh->pcat get missed */
if ((sbn->datastream == 4) && (psh->pcat != 3)) {
GOES = 0;
NWSTG = 1;
}
ccb->len = 0;
if (ulogIsDebug())
nplDebug("continuation record");
if ((pdh->transtype & 4) > 0) {
psh->frags = 0;
}
if (bufread(fd, prodmmap + sbn->len + pdh->len + pdh->pshlen,
pdh->dbsize) != 0) {
nplError("problem reading datablock (cont)");
continue;
}
if (prod.head == NULL) {
if (ulogIsVerbose())
nplInfo("found data block before header, "
"skipping sequence %d frag #%d", pdh->seqno, pdh->dbno);
continue;
}
}
/* Get the data */
dataoff = IOFF + sbn->len + pdh->len + pdh->pshlen + ccb->len;
datalen = pdh->dbsize - ccb->len;
if (ulogIsDebug())
nplDebug("look at datalen %d", datalen);
pfrag = ds_alloc();
pfrag->seqno = pdh->seqno;