00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <ctype.h>
00035 #include <signal.h>
00036 #include <strings.h>
00037 #include <errno.h>
00038 #include <sum_rpc.h>
00039 #include <sys/types.h>
00040 #include <sys/time.h>
00041 #include <sys/stat.h>
00042 #include <dirent.h>
00043 #include <unistd.h>
00044 #include <printk.h>
00045 #include "egsehmicomp.h"
00046
00047
00048 #define LEV0FILEON "/usr/local/logs/soc/LEV0FILEON" //touch to turnon lev0
00049 #define H0LOGFILE "/tmp/h0.%s.%d.log"
00050 #define DIRDDS "/egse/ssim2soc"
00051 #define IMAGEDIR "/tmp/jim"
00052 #define IMAGEDIRSMALL "/tmp/jim/small"
00053 #define IMAGEDIRCNT 100
00054 #define SEC1970TO2004 1072828800
00055 #define PKTSZ 1788
00056 #define DEFAULTDB "jsocdc"
00057
00058 #define MAXFILES 8192
00059 #define NUMTIMERS 10
00060 #define TESTAPPID 0x199
00061 #define TESTVALUE 0xc0b
00062
00063 FILE *h0logfp;
00064
00065 static char datestr[32];
00066 static struct timeval first[NUMTIMERS], second[NUMTIMERS];
00067 static float tsum[NUMTIMERS];
00068
00069
00070 static void open_sum(void);
00071
00072 static time_t call_time(void);
00073 static void now_do_alrm_sig();
00074
00075 extern int numcontexts;
00076 extern Decompress_Context_t *Context[];
00077 extern int errno;
00078
00079 unsigned int fsn = 0;
00080 unsigned int fsn_prev = 0;
00081 unsigned int fid = 0;
00082 SUM_t *sum;
00083 SUMID_t uid = 0;
00084 char **cptr;
00085 uint64_t *dsixpt;
00086 uint64_t alloc_index;
00087 char alloc_wd[64];
00088
00089 long long vcdu_seq_num;
00090 long long vcdu_seq_num_next;
00091 long long total_missing_im_pdu;
00092 unsigned int vcdu_24_cnt, vcdu_24_cnt_next;
00093 int verbose;
00094 int lev0_on_flag;
00095 double reqbytes;
00096 double dsize;
00097 double bytes_used;
00098 int total_tlm_vcdu;
00099 int total_missing_vcdu;
00100 int dsds_tid;
00101
00102 int abort_active;
00103 int sigalrmflg = 0;
00104 int sigtermflg = 0;
00105 int tlmactive = 0;
00106 int pflg = 0;
00107 int imagedircnt = 0;
00108 int ALRMSEC = 60;
00109 int dbxflg;
00110 int debugflg;
00111
00112 char database[MAX_STR];
00113 char pdshost[MAX_STR];
00114 char pchan[8];
00115 char rchan[8];
00116 char prependfits[8];
00117 char *dbname = DEFAULTDB;
00118 char *username;
00119 char *tlmdir;
00120 char *pipedir;
00121 char *frompipedir;
00122 char *logfile;
00123 struct p_r_chans {
00124 char *pchan;
00125 char *rchan;
00126 };
00127 typedef struct p_r_chans P_R_CHANS;
00128
00129 P_R_CHANS p_r_chan_pairs[] = {
00130 {"VC01", "VC09"},
00131 {"VC04", "VC12"},
00132 {"VC02", "VC10"},
00133 {"VC05", "VC13"},
00134 {"n/a", "n/a"}
00135 };
00136
00137 struct namesort {
00138 char *name;
00139 };
00140 typedef struct namesort NAMESORT;
00141
00142
00143 struct openimg {
00144 struct openimg *next;
00145 time_t sec;
00146 unsigned int fsn;
00147 };
00148 typedef struct openimg OPENIMG;
00149 OPENIMG *openimg_hdr = NULL;
00150 OPENIMG *openimg_ptr;
00151
00152
00153
00154 void setopenimg(OPENIMG **list, time_t sec, unsigned int fsn)
00155 {
00156 OPENIMG *newone;
00157
00158 newone = (OPENIMG *)malloc(sizeof(OPENIMG));
00159 newone->next = *list;
00160 newone->sec = sec;
00161 newone->fsn = fsn;
00162 *list = newone;
00163 }
00164
00165
00166 void remopenimg(OPENIMG **list, unsigned int fsn)
00167 {
00168 OPENIMG *walk = *list;
00169 OPENIMG *trail = NULL;
00170
00171 while(walk) {
00172 if(walk->fsn != fsn) {
00173 trail = walk;
00174 walk = walk->next;
00175 }
00176 else {
00177 if(trail)
00178 trail->next = walk->next;
00179 else
00180 *list = walk->next;
00181 free(walk);
00182 walk = NULL;
00183 }
00184 }
00185 }
00186
00187
00188 OPENIMG *getopenimg(OPENIMG *list, unsigned int fsn)
00189 {
00190 OPENIMG *walk = list;
00191
00192 while(walk) {
00193 if(walk->fsn != fsn)
00194 walk = walk->next;
00195 else
00196 return walk;
00197 }
00198 return walk;
00199 }
00200
00201
00202 void get_date()
00203 {
00204 time_t tval;
00205 struct tm *t_ptr;
00206
00207 tval = time(NULL);
00208 t_ptr = localtime(&tval);
00209 sprintf(datestr, "%d.%02d.%02d_%02d:%02d:%02d",
00210 (t_ptr->tm_year+1900), (t_ptr->tm_mon+1),
00211 t_ptr->tm_mday, t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec);
00212 }
00213
00214
00215 void StartTimer(int n)
00216 {
00217 gettimeofday (&first[n], NULL);
00218 }
00219
00220 float StopTimer(int n)
00221 {
00222 gettimeofday (&second[n], NULL);
00223 if (first[n].tv_usec > second[n].tv_usec) {
00224 second[n].tv_usec += 1000000;
00225 second[n].tv_sec--;
00226 }
00227 return (float) (second[n].tv_sec-first[n].tv_sec) +
00228 (float) (second[n].tv_usec-first[n].tv_usec)/1000000.0;
00229 }
00230
00231
00232
00233 int msg(char *fmt, ...)
00234 {
00235 va_list args;
00236 char string[32768];
00237
00238 va_start(args, fmt);
00239 vsprintf(string, fmt, args);
00240 printf(string);
00241 fflush(stdout);
00242 va_end(args);
00243 return(0);
00244 }
00245
00246
00247
00248
00249 void open_h0log(char *filename, char *type)
00250 {
00251 if((h0logfp=fopen(filename, type)) == NULL)
00252 fprintf(stderr, "**Can't open the log file %s\n", filename);
00253 }
00254
00255
00256
00257 int h0log(const char *fmt, ...)
00258 {
00259 va_list args;
00260 char string[32768];
00261
00262 va_start(args, fmt);
00263 vsprintf(string, fmt, args);
00264 if(h0logfp) {
00265 fprintf(h0logfp, string);
00266 fflush(h0logfp);
00267 } else {
00268 printf(string);
00269 fflush(stdout);
00270 }
00271 va_end(args);
00272 return(0);
00273 }
00274
00275 void decompress_next_vcdu_err_str(int status, char *text)
00276 {
00277 if(status == ERROR_BADOFFSET) sprintf(text, "ERROR_BADOFFSET");
00278 else if(status == ERROR_CORRUPTDATA) sprintf(text, "ERROR_CORRUPTDATA");
00279 else if(status == ERROR_BADHEADER) sprintf(text, "ERROR_BADHEADER");
00280 else if(status == ERROR_CTXOVERFLOW) sprintf(text, "ERROR_CTXOVERFLOW");
00281 else if(status == ERROR_INVALIDID) sprintf(text, "ERROR_INVALIDID");
00282 else if(status == ERROR_TOOMANYPIXELS) sprintf(text, "ERROR_TOOMANYPIXELS");
00283 else if(status == ERROR_NOSUCHIMAGE) sprintf(text, "ERROR_NOSUCHIMAGE");
00284 else if(status == ERROR_PARTIALOVERWRITE) sprintf(text,
00285 "ERROR_PARTIALOVERWRITE");
00286 else if(status == ERROR_WRONGPACKET) sprintf(text, "ERROR_WRONGPACKET");
00287 else if(status == ERROR_MISSING_FSN) sprintf(text, "ERROR_MISSING_FSN");
00288 else if(status == ERROR_MISSING_FID) sprintf(text, "ERROR_MISSING_FID");
00289 else if(status == ERROR_INVALIDCROPID) sprintf(text, "ERROR_INVALIDCROPID");
00290 else if(status == ERROR_NODATA) sprintf(text, "ERROR_NODATA");
00291 else if(status == ERROR_HK_UNKNOWN_APID) sprintf(text,
00292 "ERROR_HK_UNKNOWN_APID");
00293 else if(status == ERROR_HK_CANNOT_FIND_VER_NUM) sprintf(text,
00294 "ERROR_HK_CANNOT_FIND_VER_NUM");
00295 else if(status == ERROR_HK_CANNOT_LOAD_HK_VALUES) sprintf(text,
00296 "ERROR_HK_CANNOT_LOAD_HK_VALUES");
00297 else if(status == ERROR_HK_CANNOT_LOAD_ENGR_VALUES) sprintf(text,
00298 "ERROR_HK_CANNOT_LOAD_ENGR_VALUES");
00299 else if(status == ERROR_HK_INVALID_BITFIELD_LENGTH) sprintf(text,
00300 "ERROR_HK_INVALID_BITFIELD_LENGTH");
00301 else if(status == ERROR_HK_UNHANDLED_TYPE) sprintf(text,
00302 "ERROR_HK_UNHANDLED_TYPE");
00303 }
00304
00305
00306
00307
00308 void abortit(int stat)
00309 {
00310 printk("***Abort in progress ...\n");
00311 if(uid ) {
00312 SUM_close(sum, h0log);
00313 }
00314 printk("**Exit ingest_tlm w/ status = %d\n", stat);
00315 msg("Exit ingest_tlm w/ status = %d\n\n", stat);
00316 if (h0logfp) fclose(h0logfp);
00317 exit(stat);
00318 }
00319
00320
00321
00322
00323
00324
00325 void alrm_sig(int sig)
00326 {
00327 signal(SIGALRM, alrm_sig);
00328 if(!lev0_on_flag) return;
00329 if(!tlmactive)
00330 now_do_alrm_sig();
00331 else
00332 sigalrmflg = 1;
00333 return;
00334 }
00335
00336 void now_do_alrm_sig()
00337 {
00338 Image_t *image;
00339 Decompress_Stat_t *decomp_stat;
00340 int nx, i, status;
00341 char imgfile[128], cmd[128], bcmd[192];
00342
00343
00344 nx = decompress_status_all(&decomp_stat);
00345 for(i=0; i < nx; i++) {
00346 fsn = ID2FSN(decomp_stat[i].ID);
00347 fsn = fsn & 0x3fffffff;
00348 fid = ID2FID(decomp_stat[i].ID);
00349 openimg_ptr = (OPENIMG *)getopenimg(openimg_hdr, fsn);
00350 if(openimg_ptr != NULL) {
00351 h0log("#Timeout: Found an open image fsn=%u sec=%d call_time=%d\n",
00352 openimg_ptr->fsn, openimg_ptr->sec, call_time());
00353 if((openimg_ptr->sec + ALRMSEC) <= call_time()) {
00354 h0log("decmpress_print_status() says:\n");
00355 decompress_print_status(&decomp_stat[i]);
00356 h0log("Write partial image for timed out image.\n");
00357 status = decompress_flush_image(fsn, fid, &image);
00358 if(status == SUCCESS) {
00359 h0log("*SUCCESS FLUSH of partial image fsn=%u\n", fsn);
00360 sprintf(imgfile, "%s/%s_%09u.%d.fits",
00361 IMAGEDIR, prependfits, fsn, imagedircnt);
00362 if(decompress_writefitsimage(imgfile, image, 0)) {
00363 h0log("Error on output of %s\n", imgfile);
00364 }
00365 else {
00366 sprintf(bcmd, "/usr/local/bin/bin256 %s %s/%s_%09u.%d.fits", imgfile, IMAGEDIRSMALL, prependfits, fsn, imagedircnt);
00367 system(bcmd);
00368 }
00369 imagedircnt++;
00370 decompress_free_images(image);
00371
00372 if(imagedircnt > IMAGEDIRCNT) {
00373 sprintf(cmd,"/bin/rm -f %s/%s_*.%d.fits",
00374 IMAGEDIR,prependfits,imagedircnt-IMAGEDIRCNT);
00375 h0log("%s\n", cmd);
00376 system(cmd);
00377 }
00378 h0log("*SUCCESS FLUSH of partial image %s for fsn=%u\n",
00379 imgfile, fsn);
00380 }
00381 else {
00382 h0log("Can't flush image for fsn = %u\n", fsn);
00383 decompress_free_images(image);
00384 }
00385 remopenimg(&openimg_hdr, fsn);
00386 }
00387 }
00388 else {
00389 setopenimg(&openimg_hdr, call_time(), fsn);
00390 }
00391 }
00392 free(decomp_stat);
00393 sigalrmflg = 0;
00394 alarm(ALRMSEC);
00395 return;
00396 }
00397
00398 void sighandler(sig)
00399 int sig;
00400 {
00401 sigtermflg = 1;
00402 return;
00403 }
00404
00405 void now_do_term_sig()
00406 {
00407 Image_t *image;
00408 Decompress_Stat_t *decomp_stat;
00409 int nx, i, status;
00410 char imgfile[128], cmd[128], bcmd[192];
00411
00412 printk("\n***ingest_tlm received a termination signal\n");
00413 msg("\n***ingest_tlm received a termination signal\n");
00414 nx = decompress_status_all(&decomp_stat);
00415 msg("# of currently opened images to be flushed = %d\n", nx);
00416 if(nx) msg("(see log for more details)\n");
00417 h0log("# of currently opened images to be flushed = %d\n", nx);
00418 for(i=0; i < nx; i++) {
00419 h0log("decmpress_print_status() says:\n");
00420 decompress_print_status(&decomp_stat[i]);
00421 h0log("Write partial image.\n");
00422 fsn = ID2FSN(decomp_stat[i].ID);
00423 fsn = fsn & 0x3fffffff;
00424 fid = ID2FID(decomp_stat[i].ID);
00425 status = decompress_flush_image(fsn, fid, &image);
00426 if(status == SUCCESS) {
00427 sprintf(imgfile, "%s/%s_%09u.%d.fits",
00428 IMAGEDIR,prependfits,fsn,imagedircnt);
00429 if(decompress_writefitsimage(imgfile, image, 0)) {
00430 h0log("Error on output of %s\n", imgfile);
00431 }
00432 else {
00433 sprintf(bcmd, "/usr/local/bin/bin256 %s %s/%s_%09u.%d.fits", imgfile, IMAGEDIRSMALL, prependfits, fsn, imagedircnt);
00434 system(bcmd);
00435 }
00436 imagedircnt++;
00437 decompress_free_images(image);
00438
00439 if(imagedircnt >= IMAGEDIRCNT) {
00440 sprintf(cmd,"/bin/rm -f %s/%s_*.%d.fits",
00441 IMAGEDIR,prependfits,imagedircnt-IMAGEDIRCNT);
00442 h0log("%s\n", cmd);
00443 system(cmd);
00444 }
00445 h0log("*SUCCESS FLUSH of partial image %s for fsn=%u\n", imgfile, fsn);
00446 }
00447 else {
00448 h0log("Can't flush image for fsn = %u. Status=%d\n", fsn, status);
00449 decompress_free_images(image);
00450 }
00451 }
00452 free(decomp_stat);
00453 abortit(2);
00454 }
00455
00456
00457
00458 void usage()
00459 {
00460 msg("Usage:\ningest_tlm [-v] -pVCnn dir_in dir_out [log_file]\n");
00461 msg("where: -v = verbose\n");
00462 msg("where: -p = primary channel to listen to e.g. VC02\n");
00463 msg(" dir_in = directory containing the files to ingest.\n");
00464 msg(" dir_out = directory to move the files to after the ingest.\n");
00465 msg(" log_file = optional log file name. Will create one if not given.\n");
00466 abortit(1);
00467 }
00468
00469
00470
00471
00472 void get_cmd(int argc, char *argv[])
00473 {
00474 char *cptr;
00475 int c, i;
00476
00477 while(--argc > 0 && ((*++argv)[0] == '-')) {
00478 while((c = *++argv[0]))
00479 switch(c) {
00480 case 'd':
00481 debugflg=1;
00482 break;
00483 case 'v':
00484 verbose=1;
00485 break;
00486 case 'p':
00487 if(*++argv[0] != NULL) {
00488 cptr = argv[0];
00489 strcpy(pchan, cptr);
00490 for(i=0; ; i++) {
00491 if(!strcmp(p_r_chan_pairs[i].pchan, pchan)) {
00492 strcpy(rchan, p_r_chan_pairs[i].rchan);
00493 break;
00494 }
00495 if(!strcmp(p_r_chan_pairs[i].pchan, "n/a")) {
00496 printk("!!ERROR: Invalid VCid (%s) specified\n", pchan);
00497 usage();
00498 }
00499 }
00500 pflg = 1;
00501 }
00502 while(*++argv[0] != NULL);
00503 --argv[0];
00504 break;
00505 default:
00506 usage();
00507 break;
00508 }
00509 }
00510 if(!pflg) usage();
00511 if(argc != 4) usage();
00512 tlmdir = argv[0];
00513 pipedir = argv[1];
00514 frompipedir = argv[2];
00515 logfile = argv[3];
00516 gethostname(pdshost, MAX_STR);
00517 if((cptr = index(pdshost, '.')))
00518 *cptr = 0;
00519 }
00520
00521 time_t call_time()
00522 {
00523 time_t tsec;
00524
00525 tsec = time(NULL);
00526 return(tsec - (time_t)SEC1970TO2004);
00527 }
00528
00529
00530
00531
00532 void open_sum()
00533 {
00534 if((sum = SUM_open(NULL, NULL, h0log)) == 0) {
00535 printk("***Failed on SUM_open()\n");
00536 abortit(3);
00537 }
00538 uid=sum->uid;
00539 printk("*Opened with sum_svc as uid=%ld\n", uid);
00540 }
00541
00542 int compare_names(const void *a, const void *b)
00543 {
00544 NAMESORT *x=(NAMESORT *)a, *y=(NAMESORT *)b;
00545 return(strcmp(x->name+4, y->name+4));
00546 }
00547
00548 unsigned short MDI_getshort (unsigned char *c)
00549 {
00550 unsigned short s = 0;
00551
00552 s = (unsigned short) *c++ << 8;
00553 s |= (unsigned short) *c;
00554 return s;
00555 }
00556
00557
00558
00559
00560 int get_tlm(char *file)
00561 {
00562 FILE *fpin;
00563 Image_t *images, *sav_images, *im, *partimg;
00564 CCSDS_Packet_t *hk_packets;
00565 unsigned char cbuf[PKTSZ];
00566 char errtxt[128], imgfile[128], cmd[128], bcmd[192];
00567 long long gap_42_cnt;
00568 int status, rstatus, fpkt_cnt, i, j, sync_bad_cnt, nx;
00569 int imagecnt, appid, datval, eflg, first;
00570 unsigned int cnt1, cnt2, cnt3, fsnx, fidx, gap_24_cnt;
00571 int zero_pn;
00572 unsigned short pksync1, pksync2;
00573 float ftmp;
00574
00575 StartTimer(1);
00576 if(!(fpin = fopen(file, "r"))) {
00577 h0log("*Can't open tlm file %s\n", file);
00578 return(1);
00579 }
00580 get_date();
00581 h0log("%s\n", datestr);
00582 h0log("*Processing tlm file %s\n", file);
00583 fpkt_cnt = sync_bad_cnt = imagecnt = eflg = 0;
00584 zero_pn = gap_24_cnt = gap_42_cnt = 0;
00585 first = 1;
00586
00587
00588 while((status = fread(cbuf,sizeof(char),PKTSZ,fpin) ) == PKTSZ) {
00589 pksync1 = MDI_getshort(cbuf);
00590 pksync2 = MDI_getshort(cbuf+2);
00591 if((pksync1 == 0) && (pksync2 == 0)) {
00592 if(!zero_pn) {
00593 h0log("*0 PN code at pkt# %d\n", fpkt_cnt);
00594 h0log("*Subsequent ones will be ignored until non-0 again\n");
00595 zero_pn = 1;
00596 }
00597 fpkt_cnt++;
00598 continue;
00599 }
00600 if((pksync1 != 0x1acf) || (pksync2 != 0xfc1d)) {
00601 h0log("*Lost sync at VCDU pkt# %d. pksync1=%x pksync2=%x\n",
00602 fpkt_cnt, pksync1, pksync2);
00603 fpkt_cnt++;
00604 eflg++;
00605 if(sync_bad_cnt++ > 4) {
00606 h0log("**Too many out of sync packets.\n");
00607 return(1);
00608 }
00609 h0log(" Will attempt to press on...\n");
00610 zero_pn = 0;
00611 continue;
00612 }
00613 if(first) {
00614 h0log("*VCDU pkt# %d sync = %x %x\n", fpkt_cnt, pksync1, pksync2);
00615 }
00616 fpkt_cnt++;
00617
00618 cnt1 = MDI_getshort(cbuf+6);
00619 cnt2 = MDI_getshort(cbuf+8);
00620 cnt2 = (cnt2 >> 8)& 0xFF;
00621 cnt2 = ((cnt1 << 8)& 0xFF00) + cnt2;
00622 cnt1 = (cnt1 >> 8)& 0xFF;
00623 vcdu_24_cnt = (cnt1*65536) + cnt2;
00624 if(vcdu_24_cnt_next != vcdu_24_cnt) {
00625 h0log("*VCDU 24bit seq num out of sequence. exp: %u rec: %u\n",
00626 vcdu_24_cnt_next, vcdu_24_cnt);
00627 if(vcdu_24_cnt_next > vcdu_24_cnt) {
00628 h0log("*NOTE: VCDU 24 bit counter retarded\n");
00629 h0log("*NOTE: gap report will be inaccurate (tbd)\n");
00630 }
00631 if(!first) {
00632 gap_24_cnt += vcdu_24_cnt - vcdu_24_cnt_next;
00633 }
00634 }
00635 vcdu_24_cnt_next = vcdu_24_cnt + 1;
00636
00637 cnt1 = MDI_getshort(cbuf+10);
00638 cnt2 = MDI_getshort(cbuf+12);
00639 cnt3 = MDI_getshort(cbuf+14);
00640 cnt1 = cnt1 & 0x03ff;
00641 vcdu_seq_num = (cnt1*4294967296) + (cnt2*65536) + cnt3;
00642
00643 if(vcdu_seq_num_next != vcdu_seq_num) {
00644 h0log("*IM_PDU seq num out of sequence. exp: %lld rec: %lld\n",
00645 vcdu_seq_num_next, vcdu_seq_num);
00646 if(vcdu_seq_num_next > vcdu_seq_num) {
00647 h0log("*NOTE: IM_PDU 42 bit counter retarded\n");
00648 h0log("*NOTE: gap report will be inaccurate\n");
00649 }
00650 if(!first) {
00651 gap_42_cnt += vcdu_seq_num - vcdu_seq_num_next;
00652 }
00653 eflg++;
00654 }
00655 first = 0;
00656 vcdu_seq_num_next = vcdu_seq_num + 1;
00657
00658 appid = MDI_getshort(cbuf+18);
00659 appid = appid & 0x07ff;
00660 if(appid == TESTAPPID) {
00661
00662
00663
00664 h0log("*Test ApID of %0x found for IM_PDU Cntr = %lld\n",
00665 TESTAPPID, vcdu_seq_num);
00666 for(i=0, j=TESTVALUE; i < 877; i=i+2, j++) {
00667 datval = MDI_getshort(cbuf+32+i);
00668 if(datval != j) {
00669 h0log("*Test data value=%0x, expected=%0x for IM_PDU Cntr=%lld\n",
00670 datval, j, vcdu_seq_num);
00671 eflg++;
00672 break;
00673 }
00674 }
00675 continue;
00676 }
00677
00678
00679 if(lev0_on_flag) {
00680 rstatus = decompress_next_vcdu((unsigned short *)(cbuf+10),
00681 &images, &hk_packets);
00682 }
00683 else {
00684 rstatus = SUCCESS;
00685 goto BYPASS;
00686 }
00687
00688
00689 switch(rstatus) {
00690 case SUCCESS:
00691
00692
00693
00694
00695 cnt1 = MDI_getshort(cbuf+32);
00696 cnt2 = MDI_getshort(cbuf+34);
00697 fsnx = (unsigned int)(cnt1<<16)+(unsigned int)(cnt2);
00698 fsnx = fsnx & 0x3fffffff;
00699 if(fsnx == 0) {
00700 h0log("Found fsn=0. Ignore.\n");
00701 continue;
00702 }
00703 if(fsnx != fsn_prev) {
00704 h0log("*FSN has changed from %u to %u\n", fsn_prev, fsnx);
00705
00706 if(fsn_prev != 0) {
00707 for(nx=0; nx < numcontexts; nx++) {
00708 fsn = ID2FSN(Context[nx]->ID);
00709 fsn = fsn & 0x3fffffff;
00710 if(fsn == fsn_prev) {
00711 im = Context[nx]->image;
00712 if(im->keywords != NULL) {
00713 h0log("*New fsn. ISP in for prev fsn. Flush its image\n");
00714 }
00715 else {
00716 h0log("*New fsn. ISP NOT in for prev fsn. Flush its image\n");
00717 }
00718 fid = ID2FID(Context[nx]->ID);
00719 status = decompress_flush_image(fsn, fid, &partimg);
00720 if(status == SUCCESS) {
00721 h0log("*SUCCESS FLUSH of partial image fsn=%u\n", fsn);
00722 imagecnt++;
00723 sprintf(imgfile, "%s/%s_%09u.%d.fits",
00724 IMAGEDIR, prependfits, fsn, imagedircnt);
00725 if(decompress_writefitsimage(imgfile, partimg, 0)) {
00726 h0log("Error on output of %s\n", imgfile);
00727 }
00728 else {
00729 sprintf(bcmd, "/usr/local/bin/bin256 %s %s/%s_%09u.%d.fits", imgfile, IMAGEDIRSMALL, prependfits, fsn, imagedircnt);
00730 system(bcmd);
00731 }
00732 imagedircnt++;
00733 decompress_free_images(partimg);
00734
00735 if(imagedircnt >= IMAGEDIRCNT) {
00736 sprintf(cmd,"/bin/rm -f %s/%s_*.%d.fits",
00737 IMAGEDIR,prependfits,imagedircnt-IMAGEDIRCNT);
00738 h0log("%s\n", cmd);
00739 system(cmd);
00740 }
00741 }
00742 else {
00743 h0log("*FAILURE to flush prev fsn=%u\n", fsn);
00744 decompress_free_images(partimg);
00745 }
00746 remopenimg(&openimg_hdr, fsn);
00747 }
00748 }
00749 }
00750 fsn_prev = fsnx;
00751 }
00752 break;
00753 case SUCCESS_IMAGECOMPLETE: case SUCCESS_HKCOMPLETE:
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767 sav_images = images;
00768 while(images) {
00769 imagecnt++;
00770 fsn = IMAGE_FSN(images);
00771 fsn = fsn & 0x3fffffff;
00772 fid = IMAGE_FID(images);
00773
00774
00775
00776
00777
00778
00779 sprintf(imgfile, "%s/%s_%09u.%d.fits",
00780 IMAGEDIR,prependfits,fsn,imagedircnt);
00781 if(decompress_writefitsimage(imgfile, images, 0)) {
00782 h0log("Error on output of %s\n", imgfile);
00783 }
00784 else {
00785 sprintf(bcmd, "/usr/local/bin/bin256 %s %s/%s_%09u.%d.fits", imgfile, IMAGEDIRSMALL, prependfits, fsn, imagedircnt);
00786 system(bcmd);
00787 }
00788 imagedircnt++;
00789
00790 if(imagedircnt >= IMAGEDIRCNT) {
00791 sprintf(cmd, "/bin/rm -f %s/%s_*.%d.fits",
00792 IMAGEDIR,prependfits,imagedircnt-IMAGEDIRCNT);
00793 h0log("%s\n", cmd);
00794 system(cmd);
00795 }
00796 h0log("*SUCCESS_IMAGECOMPLETE %s\n", imgfile);
00797
00798 images = images->next;
00799 }
00800 decompress_free_images(sav_images);
00801 alarm(ALRMSEC);
00802 break;
00803 case SUCCESS_HK:
00804
00805
00806
00807
00808
00809 break;
00810 default:
00811
00812
00813
00814 h0log("*decompress_next_vcdu() returns err status = %d:\n", rstatus);
00815 decompress_next_vcdu_err_str(rstatus, errtxt);
00816 h0log("%s\n\n", errtxt);
00817 break;
00818 }
00819 if(rstatus > 1) {
00820 decompress_free_hk(hk_packets);
00821 }
00822 BYPASS:
00823 if(sigtermflg) { now_do_term_sig(); }
00824 if(sigalrmflg) { now_do_alrm_sig(); }
00825 }
00826 if(!eflg) {
00827 h0log("*No errors in tlm file\n");
00828 }
00829
00830 fclose(fpin);
00831 ftmp = StopTimer(1);
00832 h0log("**Processed %s with\n**complete images %d and %d VCDUs in %f sec\n\n",
00833 file, imagecnt, fpkt_cnt, ftmp);
00834 if(fpkt_cnt != total_tlm_vcdu) {
00835 h0log("**WARNING: Found #vcdu=%d; expected=%d\n", fpkt_cnt, total_tlm_vcdu);
00836 }
00837 if(gap_24_cnt != total_missing_vcdu) {
00838 h0log("**WARNING: VCDU 24bit cntr gaps=%d; expected=%d\n",
00839 gap_24_cnt, total_missing_vcdu);
00840 }
00841 if(gap_42_cnt != total_missing_im_pdu) {
00842 h0log("**WARNING: IM_PDU 42bit cntr gaps=%lld; expected=%lld\n",
00843 gap_42_cnt, total_missing_im_pdu);
00844 }
00845 return(0);
00846 }
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868 void do_pipe2soc() {
00869 DIR *dfd;
00870 struct dirent *dp;
00871 FILE *fp;
00872 int ptape_fn, complete;
00873 char line[128], fname[128], cmd[128];
00874 char *su_name, *ptape_id, *ptape_date;
00875
00876
00877 if(strcmp(pchan, "VC01") && strcmp(pchan, "VC02")) { return; }
00878
00879 if(DS_ConnectDB_Q(dbname)) {
00880 printk("**Can't connect to DB %s\n", dbname);
00881 abortit(3);
00882 }
00883
00884 if((dfd=opendir(frompipedir)) == NULL) {
00885 printk("**Can't opendir(%s) to find files\n", frompipedir);
00886 abortit(3);
00887 }
00888 while((dp=readdir(dfd)) != NULL) {
00889 if(strstr(dp->d_name, ".parc")) {
00890 sprintf(fname, "%s/%s", frompipedir, dp->d_name);
00891 if(!(fp=fopen(fname, "r"))) {
00892 printk("***Can't open %s\n", fname);
00893 continue;
00894 }
00895 printk("Found parc file: %s\n", fname);
00896 complete = 1;
00897 while(fgets(line, 128, fp)) {
00898 if(line[0] == '#' || line[0] == '\n') continue;
00899 printk("%s", line);
00900 su_name = (char *)strtok(line, " ");
00901 ptape_id = (char *)strtok(NULL, " ");
00902 ptape_fn = atoi((char *)strtok(NULL, " "));
00903 ptape_date = (char *)strtok(NULL, "\n");
00904 if(SUMLIB_SafeTapeUpdate(su_name,ptape_id,ptape_fn,ptape_date)) {
00905 printk("**ERROR in SUMLIB_SafeTapeUpdate(%s...)\n", su_name);
00906 complete = 0;
00907 }
00908 }
00909 fclose(fp);
00910 if(complete) {
00911 sprintf(cmd, "/bin/rm -f %s", fname);
00912 }
00913 else {
00914 sprintf(cmd, "/bin/mv -f %s %s/err/", fname, frompipedir);
00915 }
00916 printk("%s\n", cmd);
00917 system(cmd);
00918 }
00919 }
00920 closedir(dfd);
00921 DS_DisConnectDB_Q();
00922 }
00923
00924
00925
00926
00927 void do_ingest()
00928 {
00929 FILE *fp;
00930 FILE *dolev0fp;
00931 DIR *dfd;
00932 NAMESORT *nameptr;
00933 struct dirent *dp;
00934 float ttmp;
00935 int found, i, j, status;
00936 char name[128], line[128], mvname[128], tlmfile[128], tlmname[96];
00937 char cmd[128], xxname[128], tlmsize[80];
00938 char *token;
00939
00940
00941 for(i=0; i < NUMTIMERS; i++) {
00942 tsum[i] = 0.0;
00943 }
00944 if((dfd=opendir(tlmdir)) == NULL) {
00945 printk("**Can't opendir(%s) to find files\n", tlmdir);
00946 abortit(3);
00947 }
00948 found = 0; i = 0;
00949 if((nameptr = (NAMESORT *)malloc(MAXFILES * sizeof(NAMESORT))) == NULL) {
00950 printk("***Can't alloc memory for file name sort\n");
00951 abortit(3);
00952 }
00953
00954 while((dp=readdir(dfd)) != NULL) {
00955
00956
00957 if(strstr(dp->d_name, pchan) || strstr(dp->d_name, rchan) || strstr(dp->d_name, ".dsf")) {
00958 nameptr[i++].name = strdup(dp->d_name);
00959 if(i >= MAXFILES) {
00960 printk("***Fatal error. Too many (%d) files in %s\n", MAXFILES, tlmdir);
00961 abortit(3);
00962 }
00963 }
00964 }
00965 closedir(dfd);
00966 qsort(nameptr, i, sizeof(NAMESORT), &compare_names);
00967
00968 for(j=0; j < i; j++) {
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981 if(!strstr(nameptr[j].name, ".qac")) {
00982 free(nameptr[j].name);
00983 continue;
00984 }
00985 if(strstr(nameptr[j].name, pchan)) strcpy(prependfits, pchan);
00986 if(strstr(nameptr[j].name, rchan)) strcpy(prependfits, rchan);
00987 StartTimer(NUMTIMERS-1);
00988 sprintf(name, "%s/%s", tlmdir, nameptr[j].name);
00989 printk("\n*Found qac file:\n* %s\n", name);
00990 if(!(fp=fopen(name, "r"))) {
00991 printk("***Can't open %s\n", name);
00992 free(nameptr[j].name);
00993 continue;
00994 }
00995 found = 1;
00996
00997 while(fgets(line, 256, fp)) {
00998 if(line[0] == '#' || line[0] == '\n') continue;
00999 if(strstr(line, "TLM_FILE_NAME=")) {
01000 token = (char *)strtok(line, "=");
01001 token = (char *)strtok(NULL, "\n");
01002 printk("tlm file is %s\n", token);
01003 sprintf(tlmfile, "%s/%s", tlmdir, token);
01004 sprintf(tlmname, "%s", token);
01005 }
01006 else if(strstr(line, "TLM_FILE_SIZE=")) {
01007 token = (char *)strtok(line, "=");
01008 token = (char *)strtok(NULL, "=");
01009 printk("*tlm file size is %s", token);
01010 sprintf(tlmsize, "%s", token);
01011 tlmsize[strlen(token)-1] = 0;
01012 reqbytes = (double)atol(token);
01013
01014 }
01015 else if(strstr(line, "TOTAL_TLM_IM_PDU=")) {
01016 token = (char *)strtok(line, "=");
01017 token = (char *)strtok(NULL, "\n");
01018 total_tlm_vcdu = atoi(token);
01019 }
01020 else if(strstr(line, "TOTAL_MISSING_VCDU=")) {
01021 token = (char *)strtok(line, "=");
01022 token = (char *)strtok(NULL, "\n");
01023 total_missing_vcdu = atoi(token);
01024 }
01025 else if(strstr(line, "TOTAL_MISSING_IM_PDU=")) {
01026 token = (char *)strtok(line, "=");
01027 token = (char *)strtok(NULL, "\n");
01028 total_missing_im_pdu = atol(token);
01029 break;
01030 }
01031 }
01032 fclose(fp);
01033
01034
01035 sum->bytes = reqbytes;
01036 sum->reqcnt = 1;
01037 if(status = SUM_alloc(sum, h0log)) {
01038 printk("***Can't allocate %g bytes in SUM_alloc. Error code = %d\n",
01039 reqbytes, status);
01040 abortit(3);
01041 }
01042 cptr = sum->wd;
01043 dsixpt = sum->dsix_ptr;
01044 alloc_index = *dsixpt;
01045 strcpy(alloc_wd, *cptr);
01046 printk("*Alloc %g bytes at %s dsindex=%ld\n",
01047 sum->bytes, *cptr, alloc_index);
01048
01049
01050 sprintf(mvname, "%s/%s", alloc_wd, nameptr[j].name);
01051 free(nameptr[j].name);
01052
01053
01054
01055 sprintf(cmd, "cp -p %s %s", name, alloc_wd);
01056 printk("*cp qac to %s\n", alloc_wd);
01057 printk("%s\n", cmd);
01058 if(status = system(cmd)) {
01059 printk("***Error %d on: %s\n", status, cmd);
01060 printk("errno = %d\n", errno);
01061
01062 continue;
01063 }
01064 sprintf(cmd, "cp -p %s %s", tlmfile, alloc_wd);
01065 printk("*cp tlm to %s\n", alloc_wd);
01066 printk("%s\n", cmd);
01067 if(system(cmd)) {
01068 printk("***Error on: %s\n", cmd);
01069 printk("errno = %d\n", errno);
01070
01071 continue;
01072 }
01073
01074
01075
01076 sum->mode = ARCH;
01077 token = (char *)rindex(tlmname, '.');
01078 *token = 0;
01079 sum->dsname = tlmname;
01080 sum->group = 1;
01081 sum->storeset = 0;
01082 sum->reqcnt = 1;
01083 if(SUM_put(sum, h0log)) {
01084 printk("**Error: on SUM_put()\n");
01085 }
01086 else {
01087 printk("*SUM_put() successfull for wd = %s\n", *sum->wd);
01088 printk("Marked for archive data unit ds_index=%ld\n", *dsixpt);
01089 }
01090
01091
01092
01093 sprintf(cmd, "/bin/mv -f %s %s", tlmfile, pipedir);
01094 printk("*mv tlm file to %s\n", pipedir);
01095 printk("%s\n", cmd);
01096 if(system(cmd)) {
01097 printk("***Error on: %s\n", cmd);
01098 }
01099 sprintf(cmd, "/bin/mv -f %s %s", name, pipedir);
01100 printk("*mv qac file to %s\n", pipedir);
01101 printk("%s\n", cmd);
01102 if(system(cmd)) {
01103 printk("***Error on: %s\n", cmd);
01104 }
01105
01106
01107
01108
01109 sprintf(xxname, "%s/%s.tlm", alloc_wd, tlmname);
01110 tlmactive = 1;
01111 if(get_tlm(xxname)) {
01112 h0log("***Error in lev0 extraction for %s\n", xxname);
01113 }
01114 tlmactive = 0;
01115 ttmp = StopTimer(NUMTIMERS-1);
01116 printk("Rate tlm %s bytes in %f sec\n", tlmsize, ttmp);
01117
01118 if((dolev0fp=fopen(LEV0FILEON, "r")) != NULL) {
01119 if(!lev0_on_flag) {
01120 printk("Found file: %s. Lev0 processing now active.\n", LEV0FILEON);
01121 lev0_on_flag = 1;
01122 }
01123 fclose(dolev0fp);
01124 }
01125 else {
01126 if(lev0_on_flag) {
01127 printk("Not Found: %s. Lev0 processing not active.\n", LEV0FILEON);
01128 lev0_on_flag = 0;
01129 }
01130 }
01131 }
01132 free(nameptr);
01133
01134 }
01135
01136
01137
01138 void setup(int argc, char *argv[])
01139 {
01140 int i;
01141 char logname[128], string[128], cwdbuf[128], idstr[256];
01142
01143 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
01144 signal(SIGINT, sighandler);
01145 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
01146 signal(SIGTERM, sighandler);
01147 signal(SIGALRM, alrm_sig);
01148
01149 get_date();
01150 if(!(username = (char *)getenv("USER"))) username = "nouser";
01151 if(!logfile) {
01152 sprintf(logname, H0LOGFILE, username, getpid());
01153 open_h0log(logname, "w");
01154 } else {
01155 open_h0log(logfile, "a");
01156 }
01157 printk_set(h0log, h0log);
01158 printk("%s\n", datestr);
01159 getcwd(cwdbuf, 126);
01160 sprintf(idstr, "Cwd: %s\nCall: ", cwdbuf);
01161 for(i=0; i < argc; i++) {
01162 sprintf(string, "%s%s", argv[i], (i < argc-1) ? " " : "");
01163 strcat(idstr, string);
01164 }
01165 strcat(idstr, "\n");
01166 sprintf(string, "ingest_tlm started as pid=%d user=%s\n", getpid(), username);
01167 strcat(idstr, string);
01168 printk("*%s", idstr);
01169
01170
01171 umask(002);
01172 open_sum();
01173 }
01174
01175 int main(int argc, char *argv[])
01176 {
01177 get_cmd(argc, argv);
01178 setup(argc, argv);
01179 alarm(ALRMSEC);
01180 while(1) {
01181 do_ingest();
01182 do_pipe2soc();
01183 sleep(4);
01184 if(sigtermflg) { now_do_term_sig(); }
01185 }
01186 }
01187