00001 #ident "$Header: /home/cvsuser/cvsroot/JSOC/proj/lev0/apps/decode_hk_vcdu.c,v 1.12 2013/03/15 19:52:36 prodtest Exp $"
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdio.h>
00011 #include "decode_hk.h"
00012 #include "packets.h"
00013 #include "decode_hk_vcdu.h"
00014 #include "save_packet_to_dayfile.h"
00015 #include <sys/time.h>
00016 #include <time.h>
00017 #include "timeio.h"
00018 #include <dirent.h>
00019 #include "printk.h"
00020
00021
00022 static unsigned short *decode_im_pdu(unsigned short *w, IM_PDU_Packet_t *p);
00023 static unsigned short *decode_ccsds(unsigned short *w, CCSDS_Packet_t *p);
00024
00025
00026 int lookup_fsn(CCSDS_Packet_t **ptr, unsigned int *Fsn);
00027 int save_packet_to_dayfile(unsigned short *word_ptr, int apid, HK_Dayfile_Data_t **df_head);
00028 int write_packet_to_dayfile(HK_Dayfile_Data_t **df_head);
00029 int filter_out_non_isps(CCSDS_Packet_t **ptr);
00030 int write_hk_to_drms(DRMS_Record_t *record, CCSDS_Packet_t **ccsds_pkt);
00031 int get_status( int lz_status[100], int status_count);
00032
00033
00034 extern int free_dayfile_data( HK_Dayfile_Data_t **df_head);
00035 extern int free_dayfile_pkt_data( HK_Dayfile_Data_t **df_head);
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 int decode_next_hk_vcdu(unsigned short vcdu[PACKETWORDS], CCSDS_Packet_t **hk_packets, unsigned int *Fsn)
00103 {
00104
00105 CCSDS_Packet_t ccsds, *p, *p_hk;
00106 IM_PDU_Packet_t im_pdu;
00107 int spfd;
00108 int i, j, k;
00109 unsigned char *p1,*p2;
00110 static unsigned short buffer[PACKETWORDS];
00111 unsigned short *w, *hkstart;
00112 int lz_status[100];
00113 int hk_status = ERROR_NODATA;
00114 int wdf_status;
00115 int wd_status;
00116 int foni_status;
00117 int decode_status;
00118 int lev0_status;
00119 int fsn_status=1;
00120 DRMS_Record_t *record=NULL;
00121 static short writeFlag= HK_INIT_WRITE_FLAG;
00122 static HK_Dayfile_Data_t *dfd=NULL;
00123
00124
00125 j=0;
00126 lev0_status=ERROR_NODATA;
00127
00128
00129 for (k=0;k<100;k++) lz_status[k]=99;
00130
00131
00132
00133
00134
00135
00136 #if __BYTE_ORDER == __LITTLE_ENDIAN
00137 p1 = (unsigned char *)vcdu;
00138 p2 = (unsigned char *)buffer;
00139 for (i=0; i<PACKETWORDS; i++)
00140 {
00141 *(p2+1) = *p1;
00142 *p2 = *(p1+1);
00143 p1 += 2;
00144 p2 += 2;
00145 }
00146 #else
00147 memcpy(buffer, vcdu, PACKETBYTES);
00148 #endif
00149
00150
00151 w = decode_im_pdu(buffer, &im_pdu);
00152
00153
00154 *hk_packets = p_hk = NULL;
00155 do
00156 {
00157
00158 hkstart = vcdu + (w-buffer);
00159 w = decode_ccsds(w, &ccsds);
00160
00161 #ifdef DEBUG_DECODE_HK_VCDU
00162 printkerr("DEBUG Message at %s, line %d: Looping thru vcdu for packet."
00163 "\n",__FILE__, __LINE__, ccsds.apid);
00164 #else
00165 #endif
00166
00167
00168 switch(ccsds.apid)
00169 {
00170
00171
00172
00173 case 0:
00174
00175 if (hk_status != ERROR_NODATA)
00176 {
00177 hk_status=HK_SUCCESS_REACHED_END_VCDU;
00178 }
00179 lz_status[j++] = hk_status;
00180 break;
00181
00182
00183
00184
00185 case APID_IRIS_SCIENCE:
00186 case APID_HMI_SCIENCE_1:
00187 case APID_HMI_SCIENCE_2:
00188 case APID_AIA_SCIENCE_1:
00189 case APID_AIA_SCIENCE_2:
00190 hk_status=HK_SUCCESS_SKIP_IMAGE;
00191 lz_status[j++] = hk_status;
00192 break;
00193
00194
00195
00196
00197 case APID_IRIS_TIME:
00198 case APID_HMI_TIME_1:
00199 case APID_HMI_TIME_2:
00200 case APID_AIA_TIME_1:
00201 case APID_AIA_TIME_2:
00202
00203 hk_status=HK_SUCCESS_HKTIME;
00204 lz_status[j++] = hk_status;
00205 break;
00206
00207
00208
00209
00210 case APID_HMI_IMSTAT_1:
00211 case APID_HMI_IMSTAT_2:
00212 case APID_AIA_IMSTAT_1:
00213 case APID_AIA_IMSTAT_2:
00214 case APID_HMI_SEQ_1:
00215 case APID_HMI_SEQ_2:
00216 case APID_AIA_SEQ_1:
00217 case APID_AIA_SEQ_2:
00218 case APID_IRIS_ISP:
00219
00220 #ifdef DEBUG_DECODE_HK_VCDU
00221 printkerr("DEBUG Message at %s, line %d: Processing hk packet for apid <%d>."
00222 "\n",__FILE__, __LINE__, ccsds.apid);
00223 #else
00224 #endif
00225
00226 decode_status = decode_hk_keywords(hkstart, ccsds.apid, &ccsds.keywords);
00227
00228
00229 if (decode_status != 0)
00230 {
00231
00232 hk_status=decode_status;
00233 }
00234 else
00235 {
00236
00237 hk_status=HK_SUCCESS_DECODING;
00238 }
00239 lz_status[j++] = hk_status;
00240
00241
00242 if (hk_status==HK_SUCCESS_DECODING)
00243 {
00244
00245 p = malloc(sizeof(CCSDS_Packet_t));
00246 if(!p)
00247 {
00248 printkerr("ERROR at %s, line %d: Cannot malloc space!\n",__FILE__, __LINE__);
00249 }
00250
00251 memcpy(p, &ccsds, sizeof(CCSDS_Packet_t));
00252
00253
00254 if (*hk_packets == NULL)
00255 {
00256 *hk_packets = p_hk = p;
00257 }
00258 else
00259 {
00260 p_hk->next = p;
00261 p_hk = p_hk->next;
00262 }
00263 p_hk->next = NULL;
00264 }
00265
00266 spfd = save_packet_to_dayfile(hkstart ,ccsds.apid, &dfd) ;
00267 hk_status=spfd;
00268 if (hk_status == ERROR_HK_ENVIRONMENT_VARS_NOT_SET)
00269 {
00270
00271 lev0_status= ERROR_HK_ENVIRONMENT_VARS_NOT_SET;
00272 lz_status[j++] = hk_status;
00273 return (lev0_status);
00274 }
00275
00276
00277
00278 lz_status[j++] = hk_status;
00279 break;
00280
00281
00282
00283 case APID_HMI_OBT_1:
00284 case APID_HMI_OBT_2:
00285 case APID_AIA_OBT_1:
00286 case APID_AIA_OBT_2:
00287 if (hk_status == HK_SUCCESS_HKTIME ||
00288 hk_status == HK_SUCCESS_DECODING ||
00289 hk_status == HK_SUCCESS_WRITE_DAYFILE)
00290 {
00291
00292
00293 spfd = save_packet_to_dayfile(hkstart ,ccsds.apid, &dfd) ;
00294 hk_status=spfd;
00295 if (hk_status == ERROR_HK_ENVIRONMENT_VARS_NOT_SET)
00296 {
00297
00298 lev0_status= ERROR_HK_ENVIRONMENT_VARS_NOT_SET;
00299 lz_status[j++] = hk_status;
00300 return (lev0_status);
00301 }
00302 }
00303
00304
00305
00306 lz_status[j++] = hk_status;
00307 break;
00308
00309
00310
00311
00312 default:
00313
00314 hk_status=HK_SUCCESS_SKIP_PROCESSING_APID;
00315 lz_status[j++] = hk_status;
00316 break;
00317 }
00318 #ifdef DEBUG_DECODE_HK_VCDU
00319 printkerr("DEBUG Message at %s, line %d: Processing packet for apid <%d>."
00320 "\n",__FILE__, __LINE__, ccsds.apid);
00321 #else
00322 #endif
00323 w += ccsds.length/2;
00324 } while ( ccsds.apid>0 && (int)(w-buffer) < PACKETWORDS);
00325
00326
00327 #ifdef DEBUG_DECODE_HK_VCDU
00328
00329
00330
00331
00332
00333 printkerr("DEBUG Message at %s, line %d: Level O by APID processing type"
00334 " number when processing packets for above apids", __FILE__, __LINE__);
00335 for (i=0; i < j ;i++)
00336 {
00337 printkerr(":%d:",lz_status[i]);
00338 }
00339 printkerr("\n");
00340 #else
00341 #endif
00342
00343
00344
00345
00346 if(dfd && hk_status != HK_SUCCESS_SKIP_IMAGE )
00347 {
00348 if (writeFlag == HK_WRITE_AFTER_VCDU_COUNT)
00349 {
00350 wdf_status = write_packet_to_dayfile( &dfd);
00351 if (HK_SUCCESS_WRITE_DAYFILE != wdf_status )
00352 {
00353 printkerr("ERROR at %s, line %d: Return status failed for "
00354 "write_packet_to_dayfile. Probably can't open dayfile.\n",
00355 __FILE__, __LINE__);
00356 return (ERROR_HK_FAILED_OPEN_DAYFILE);
00357 }
00358 wdf_status=free_dayfile_pkt_data( &dfd);
00359 if (HK_SUCCESS_WRITE_DAYFILE != wdf_status)
00360 {
00361 printkerr("Warning at %s, line %d: Return status failed for "
00362 "free_dayfile_pkt_data\n", __FILE__, __LINE__);
00363 }
00364 writeFlag= HK_INIT_WRITE_FLAG;
00365 }
00366 else writeFlag++;
00367 }
00368 if(*hk_packets && hk_status != HK_SUCCESS_SKIP_IMAGE)
00369 {
00370
00371
00372 wd_status = write_hk_to_drms(record, hk_packets);
00373
00374
00375
00376 if (wd_status == HK_SUCCESS_WROTE_TO_DRMS)
00377 {
00378
00379 lev0_status= SUCCESS_HK_NEED_TO_CTD;
00380 }
00381 else if (wd_status == SUCCESS_HK_SKIP_WTD_REC_EXISTS)
00382 {
00383
00384 lev0_status= SUCCESS_HK_SKIP_WTD_REC_EXISTS;
00385
00386 }
00387 else if (wd_status == SUCCESS_HK_SKIP_WTD_REC_TIME_OOR)
00388 {
00389
00390
00391 lev0_status= SUCCESS_HK_SKIP_WTD_REC_TIME_OOR;
00392 }
00393 else if (wd_status == ERROR_HK_ENVIRONMENT_VARS_NOT_SET)
00394 {
00395
00396 lev0_status= ERROR_HK_ENVIRONMENT_VARS_NOT_SET;
00397 }
00398 else if (wd_status == ERROR_HK_FAILED_TO_FIND_TIMECODES)
00399 {
00400
00401 lev0_status= ERROR_HK_FAILED_TO_FIND_TIMECODES;
00402 }
00403 else if (wd_status == ERROR_HK_FAILED_CLOSE_DRMS_RECORD)
00404 {
00405 lev0_status= ERROR_HK_FAILED_CLOSE_DRMS_RECORD;
00406 }
00407 else if (wd_status == ERROR_HK_FAILED_OPEN_DRMS_RECORD)
00408 {
00409 lev0_status= ERROR_HK_FAILED_OPEN_DRMS_RECORD;
00410 }
00411 else if (wd_status == ERROR_HK_FAILED_UNKNOWNSERIES)
00412 {
00413
00414
00415 printkerr("ERROR at %s, line %d: Return status code <%d> "
00416 "for write_hk_to_drms function. Unknown data series "
00417 "when writing to Level0 by APID series. Create Series.\n"
00418 , __FILE__, __LINE__, wd_status);
00419 }
00420 else
00421 {
00422 printkerr("Warning at %s, line %d: Return status <%d> unexpected "
00423 "for write_hk_to_drms. Check status code in "
00424 "drms_statuscodes.h.\n", __FILE__, __LINE__, wd_status);
00425 }
00426 }
00427
00428 #ifdef DEBUG_DECODE_HK_VCDU
00429 if (get_status( lz_status, j) != 4)
00430 {
00431 printkerr("DEBUG Message at %s, line %d: Overall hk by apid processing "
00432 "status for saving packets, decoding packets <%d> and writing "
00433 "to drms lev0 hk series <%d>\n",
00434 __FILE__, __LINE__, get_status( lz_status, j), lev0_status);
00435 }
00436 #else
00437 #endif
00438
00439
00440
00441
00442
00443
00444 if(hk_packets && hk_status != HK_SUCCESS_SKIP_IMAGE && lev0_status >= ERROR_NODATA)
00445 {
00446
00447 foni_status = filter_out_non_isps(hk_packets);
00448
00449 if(foni_status == 1)
00450 {
00451
00452
00453
00454 fsn_status= lookup_fsn(hk_packets, Fsn);
00455 if(fsn_status)
00456 {
00457 lev0_status = ERROR_HK_FAILED_GETTING_FSN;
00458 }
00459 else
00460 {
00461
00462 lev0_status = SUCCESS_HK_NEED_TO_WTD_CTD;
00463 }
00464 }
00465 }
00466
00467
00468
00469 return (lev0_status);
00470 }
00471
00472
00473
00474
00475
00476 static unsigned short *decode_im_pdu(unsigned short *w, IM_PDU_Packet_t *p)
00477 {
00478
00479 p->im_pdu_count = ((long long) (*w & 0x3ff)) << 32;
00480 p->im_pdu_id = (unsigned char)(*w++ >> 10);
00481
00482 p->im_pdu_count |= *w++ << 16;
00483
00484 p->im_pdu_count |= *w++;
00485
00486 p->fhp = (unsigned short)((*w++) & 0x7ff);
00487 return w;
00488 }
00489
00490
00491 static unsigned short *decode_ccsds(unsigned short *w, CCSDS_Packet_t *p)
00492 {
00493
00494 p->version = (unsigned char)((*w >> 13) & 0x7);
00495 p->type = (unsigned char)((*w >> 12) & 1);
00496 p->shf = (unsigned char)((*w >> 11) & 1);
00497 p->apid = (unsigned short)((*w++) & 0x7ff);
00498
00499 p->sf = (unsigned char)((*w >> 14) & 0x3);
00500 p->ssc = (unsigned short)((*w++) & 0x3fff);
00501
00502
00503
00504
00505 p->length = (unsigned short)(*w++ + 1);
00506 return w;
00507 }
00508
00509
00510 void hk_ccsds_free(CCSDS_Packet_t **ptr)
00511 {
00512 CCSDS_Packet_t *tmp, *p;
00513 p= *ptr;
00514 while(p)
00515 {
00516 if (p->keywords)
00517 {
00518 deallocate_hk_keyword(p->keywords);
00519 }
00520 tmp = p->next;
00521 free(p);
00522 p = tmp;
00523 }
00524 }
00525
00526
00527
00528 int filter_out_non_isps(CCSDS_Packet_t **ptr)
00529 {
00530
00531 CCSDS_Packet_t *tmp, *p, *prev;
00532 int isp_count=0;
00533
00534
00535 p= *ptr;
00536 prev=p;
00537
00538
00539 while(p)
00540 {
00541
00542 if (p->apid != APID_HMI_IMSTAT_1 && p->apid != APID_HMI_IMSTAT_2 &&
00543 p->apid != APID_AIA_IMSTAT_1 && p->apid != APID_AIA_IMSTAT_2 &&
00544 p->apid != APID_IRIS_ISP)
00545 {
00546 if (p->keywords)
00547 {
00548 deallocate_hk_keyword(p->keywords);
00549 }
00550 prev->next=p->next;
00551 tmp = p->next;
00552 free(p);
00553 p = tmp;
00554 }
00555 else
00556 {
00557 isp_count++;
00558 if(isp_count == 1)
00559 {
00560
00561 *ptr=p;
00562 }
00563 prev=p;
00564 p=p->next;
00565 }
00566 }
00567 return (isp_count);
00568 }
00569
00570
00571
00572 int lookup_fsn(CCSDS_Packet_t **ptr, unsigned int *Fsn)
00573 {
00574
00575 CCSDS_Packet_t *p;
00576 HK_Keyword_t *kw;
00577 unsigned int fsn=0;
00578 int status=1;
00579
00580
00581 p= *ptr;
00582 kw=p->keywords;
00583
00584
00585 while(kw)
00586 {
00587
00588 if (strstr(kw->name, "FILTERGRAM_SN") || strstr(kw->name, "FRAME_SN"))
00589 {
00590 fsn=kw->eng_value.uint32_val;
00591 status = 0;
00592 *Fsn=fsn;
00593 break;
00594 }
00595 else
00596 {
00597 kw=kw->next;
00598 }
00599 }
00600 return (status);
00601 }
00602
00603
00604
00605
00606 int get_status( int lz[], int jj)
00607 {
00608
00609 int k, count_errors, last_error, skip_apids;
00610
00611
00612
00613 for (k=0, count_errors=0,skip_apids=0, last_error=0; k < jj; k++)
00614 {
00615
00616 if (lz[k] < 0 )
00617 {
00618 count_errors+=1;
00619 last_error= lz[k];
00620 }
00621 else if (lz[k] == SUCCESS_SKIP_PROCESSING_APID)
00622 {
00623 skip_apids+=1;
00624 }
00625 }
00626
00627
00628
00629 if (!count_errors)
00630 {
00631 if (lz[0] == HK_SUCCESS_SKIP_IMAGE) return HK_SUCCESS_SKIP_IMAGE;
00632 else if (skip_apids == (jj - 2 ))
00633 {
00634 return HK_SUCCESS_SKIP_PROCESSING_APID;
00635 }
00636 else return HK_SUCCESS_HK_ALL;
00637 }
00638
00639
00640 else if ( count_errors == jj - 2)
00641 {
00642 return last_error;
00643 }
00644
00645 else if ( count_errors < jj - 2)
00646 {
00647 return HK_SUCCESS_HK_SOME;
00648 }
00649 else return lz[0];
00650 }