00001 #ident "$Header: /home/cvsuser/cvsroot/JSOC/proj/lev0/apps/decode_hk.c,v 1.16 2013/03/07 23:25:58 arta Exp $"
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <math.h>
00013 #include <time.h>
00014 #include <sys/types.h>
00015 #include <sys/stat.h>
00016 #include <sys/vfs.h>
00017 #include <fcntl.h>
00018 #include <unistd.h>
00019 #include <libgen.h>
00020 #include "decode_hk.h"
00021 #include "printk.h"
00022 #include "write_hk_to_drms.h"
00023 #include "egsehmicomp.h"
00024
00025
00026
00027 #define DHK_ERRMSG(__msg__) printkerr("ERROR at %s, line %d: " #__msg__"\n",__FILE__,__LINE__);
00028
00029
00030
00031 extern APID_Pointer_HK_Configs *global_apid_configs;
00032 extern GTCIDS_Version_Number* global_gtcids_vn;
00033 extern SHCIDS_Version_Number* global_shcids_vn;
00034
00035 static void get_version_number(unsigned short *wd_ptr, char *ptr_vn);
00036 HK_Keywords_Format * load_hk_configs(HK_Config_Files *found_config);
00037 static int load_hk_values(unsigned short *word_ptr, HK_Keywords_Format *hk_keywords);
00038 static int load_engr_values(HK_Keywords_Format *hk_keywords, HK_Keyword_t **kw_head);
00039 static void deallocate_hk_keywords_format(HK_Keywords_Format *head);
00040 float translateint2float(int ival);
00041 double translateint2double(int64_t ival);
00042 double get_packet_time(unsigned short *word_ptr);
00043
00044 extern void sprint_time (char *at, TIME t, char *zone, int precision);
00045 extern int check_for_sdo_apid(int apid);
00046 extern char * find_fvn_from_shcids(SHCIDS_Version_Number *top,char pkt_date[MAX_SIZE_PKT_DATE], int apid);
00047 extern char * make_strupr (char *in_string);
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 int decode_hk_keywords(unsigned short *word_ptr, int apid, HK_Keyword_t **kw_head)
00058 {
00059
00060 APID_Pointer_HK_Configs *apid_configs;
00061 HK_Config_Files *config_files;
00062 HK_Config_Files *matching_config;
00063 HK_Keywords_Format *ptr_hk_keywords;
00064 char pkt_date[MAX_SIZE_PKT_DATE];
00065 char *ptr_fvn;
00066 char version_number[MAX_CHAR_VERSION_NUMBER];
00067 int status;
00068 char apid_str[10];
00069 int curr_pvn_wn, curr_pvn_dn;
00070 char pkt_name[10];
00071
00072
00073 matching_config= (HK_Config_Files *)NULL;
00074
00075
00076
00077 if (check_for_sdo_apid(apid))
00078 {
00079
00080 (void)sprint_time (pkt_date, get_packet_time(word_ptr), "TAI", 0);
00081 strcat(version_number,"\0");
00082
00083
00084 curr_pvn_wn= HK_LEV0_START_MERGED_PVNW;
00085 curr_pvn_dn= HK_LEV0_START_MERGED_PVND;
00086 }
00087 else
00088 {
00089 get_version_number(word_ptr, version_number);
00090 strcat(pkt_date,"\0");
00091
00092
00093 sscanf(version_number,"%3d.%3d",&curr_pvn_wn, &curr_pvn_dn);
00094 }
00095
00096
00097 if (!global_apid_configs )
00098 {
00099 load_all_apids_hk_configs(apid, version_number, pkt_date );
00100 }
00101
00102
00103
00104 if(curr_pvn_wn >= HK_LEV0_START_MERGED_PVNW && curr_pvn_dn >= HK_LEV0_START_MERGED_PVND)
00105 {
00106
00107
00108 #ifdef DEBUG_DECODE_HK
00109 printkerr("DEBUG:Message at %s, line %d: Doing Merge Data Series Name. PVN:<%d.%d> \n",
00110 __FILE__, __LINE__,curr_pvn_wn,curr_pvn_dn);
00111 #endif
00112
00113
00114 if (apid == HK_HSB_HMI_ISP_1 || apid == HK_HSB_HMI_ISP_2 || apid == HK_LR_HMI_ISP)
00115 {
00116
00117
00118 strcpy(pkt_name, HK_HMI_PKT_NAME_ISP);
00119
00120 strcpy(apid_str,make_strupr(pkt_name));
00121 }
00122 else if (apid == HK_HSB_AIA_ISP_1 || apid == HK_HSB_AIA_ISP_2 || apid == HK_LR_AIA_ISP)
00123 {
00124 strcpy(pkt_name, HK_AIA_PKT_NAME_ISP);
00125 strcpy(apid_str,make_strupr(pkt_name));
00126 }
00127 else if (apid == HK_HSB_HMI_SEQ_1 || apid == HK_HSB_HMI_SEQ_2 || apid == HK_LR_HMI_SEQ )
00128 {
00129 strcpy(pkt_name, HK_HMI_PKT_NAME_SEQ);
00130 strcpy(apid_str,make_strupr(pkt_name));
00131 }
00132 else if (apid == HK_HSB_AIA_SEQ_1 || apid == HK_HSB_AIA_SEQ_2 || apid == HK_LR_AIA_SEQ)
00133 {
00134 strcpy(pkt_name, HK_AIA_PKT_NAME_SEQ);
00135 strcpy(apid_str,make_strupr(pkt_name));
00136 }
00137 else if (apid == HK_HSB_HMI_OBT_1 || apid == HK_HSB_HMI_OBT_2 || apid == HK_LR_HMI_OBT )
00138 {
00139 strcpy(pkt_name, HK_HMI_PKT_NAME_OBT);
00140 strcpy(apid_str,make_strupr(pkt_name));
00141 }
00142 else if (apid == HK_HSB_AIA_OBT_1 || apid == HK_HSB_AIA_OBT_2 || apid == HK_LR_AIA_OBT)
00143 {
00144 strcpy(pkt_name, HK_AIA_PKT_NAME_OBT);
00145 strcpy(apid_str,make_strupr(pkt_name));
00146 }
00147 else if (apid == HK_LR_SDO_ASD)
00148 {
00149 strcpy(pkt_name, HK_SDO_PKT_NAME_ASD);
00150 strcpy(apid_str,make_strupr(pkt_name));
00151 }
00152 else
00153 {
00154
00155
00156 sprintf(apid_str,"0x%03.3x",apid);
00157 }
00158 }
00159 else
00160 {
00161 sprintf(apid_str,"0x%03.3x",apid);
00162
00163 #ifdef DEBUG_DECODE_HK
00164 printkerr("DEBUG:Message at %s, line %d: Doing NON-Merge Data Series Name. PVN:<%d.%d> \n",
00165 __FILE__, __LINE__,curr_pvn_wn,curr_pvn_dn);
00166 #endif
00167
00168 }
00169
00170
00171 apid_configs = global_apid_configs;
00172 while(apid_configs)
00173 {
00174
00175 if ((strcmp(apid_str, "ISP") && apid == apid_configs->apid) && (strcmp( apid_str, "SEQ") && apid == apid_configs->apid) && (strcmp( apid_str, "ASD") && apid == apid_configs->apid) && (strcmp(apid_str, "OBT") && apid == apid_configs->apid))
00176 {
00177
00178 #ifdef DEBUG_DECODE_HK
00179 printkerr("DEBUG:Message at %s, line %d: Found NON-Merge dec valued apid <%d> configuration data. \n",
00180 __FILE__, __LINE__, apid);
00181 #endif
00182 break;
00183 }
00184 else if( (!strcmp( apid_str, "ISP")) && (apid_configs->apid == HK_HSB_HMI_ISP_1 ) && (apid == HK_HSB_HMI_ISP_1 || apid == HK_HSB_HMI_ISP_2 || apid == HK_LR_HMI_ISP))
00185 {
00186
00187
00188
00189 #ifdef DEBUG_DECODE_HK
00190 printkerr("DEBUG:Message at %s, line %d: Found Merged HMI ISP dec valued apid <%d> configuration data. \n",
00191 __FILE__, __LINE__, apid);
00192 #endif
00193 break;
00194 }
00195 else if( (!strcmp( apid_str, "SEQ")) && ( apid_configs->apid == HK_HSB_HMI_SEQ_1 ) && (apid == HK_HSB_HMI_SEQ_2 || apid == HK_HSB_HMI_SEQ_1 || apid == HK_LR_HMI_SEQ))
00196 {
00197
00198
00199
00200 #ifdef DEBUG_DECODE_HK
00201 printkerr("DEBUG:Message at %s, line %d: Found Merged HMI SEQ dec valued apid <%d> configuration data. \n",
00202 __FILE__, __LINE__, apid);
00203 #endif
00204 break;
00205 }
00206 else if( (!strcmp( apid_str, "OBT")) && (apid_configs->apid == HK_HSB_HMI_OBT_1 ) && (apid == HK_HSB_HMI_OBT_1 || apid == HK_HSB_HMI_OBT_2 || apid == HK_LR_HMI_OBT ))
00207 {
00208
00209 #ifdef DEBUG_DECODE_HK
00210 printkerr("DEBUG:Message at %s, line %d: Found Merged HMI OBT dec valued apid <%d> configuration data. \n",
00211 __FILE__, __LINE__, apid);
00212 #endif
00213 break;
00214 }
00215 else if( (!strcmp( apid_str, "ISP")) && (apid_configs->apid == HK_HSB_AIA_ISP_1 ) && (apid == HK_HSB_AIA_ISP_1 || apid == HK_HSB_AIA_ISP_2 || apid == HK_LR_AIA_ISP))
00216 {
00217
00218 #ifdef DEBUG_DECODE_HK
00219 printkerr("DEBUG:Message at %s, line %d: Found Merged AIA ISP dec valued apid <%d> configuration data. \n",
00220 __FILE__, __LINE__, apid);
00221 #endif
00222 break;
00223 }
00224 else if( (!strcmp( apid_str, "SEQ")) && (apid_configs->apid == HK_HSB_AIA_SEQ_1 ) && (apid == HK_HSB_AIA_SEQ_1 || apid == HK_HSB_AIA_SEQ_2 || apid == HK_LR_AIA_SEQ ))
00225 {
00226
00227 #ifdef DEBUG_DECODE_HK
00228 printkerr("DEBUG:Message at %s, line %d: Found Merged AIA SEQ dec valued apid <%d> configuration data. \n",
00229 __FILE__, __LINE__, apid);
00230 #endif
00231 break;
00232 }
00233 else if( (!strcmp( apid_str, "OBT")) && (apid_configs->apid == HK_HSB_AIA_OBT_1 ) && (apid == HK_HSB_AIA_OBT_1 || apid == HK_HSB_AIA_OBT_2 || apid == HK_LR_AIA_OBT ))
00234 {
00235
00236 #ifdef DEBUG_DECODE_HK
00237 printkerr("DEBUG:Message at %s, line %d: Found Merged AIA OBT dec valued apid <%d> configuration data. \n",
00238 __FILE__, __LINE__, apid);
00239 #endif
00240 break;
00241 }
00242 else if( (!strcmp( apid_str, "ASD")) && (apid_configs->apid == HK_LR_SDO_ASD ) )
00243 {
00244
00245 #ifdef DEBUG_DECODE_HK
00246 printkerr("DEBUG:Message at %s, line %d: Found Merged SDO ASD dec valued apid <%d> configuration data. \n",
00247 __FILE__, __LINE__, apid);
00248 #endif
00249 break;
00250 }
00251 else
00252 {
00253
00254 #ifdef DEBUG_DECODE_HK
00255 printkerr("DEBUG:Message at %s, line %d: Keep checking for dec valued apid <%d> configuration data. \n",
00256 __FILE__, __LINE__, apid);
00257 #endif
00258 apid_configs = apid_configs->next;
00259 }
00260 }
00261
00262
00263 if ( apid_configs == NULL)
00264 {
00265 if(check_for_sdo_apid(apid))
00266 {
00267 printkerr("ERROR at %s, line %d: This apid <%x> does not have valid "
00268 "config data to decode packet. Check if sdo hk config files "
00269 "exists for file version number <%s>. "
00270 "If don't exist, run make_hkpdf.pl script to create files. "
00271 "If file version is set to <BAD PKT DATE> then check if "
00272 "packet date from packet time is a valid date. \n",
00273 __FILE__, __LINE__, apid, find_fvn_from_shcids(global_shcids_vn, pkt_date,apid));
00274 }
00275 else
00276 {
00277 printkerr("ERROR at %s, line %d: This apid <%x> does not have valid "
00278 "config data to decode packet. Check if hk config files exists "
00279 "for packet version number %s. "
00280 "If don't exist, run make_hkpdf.pl script to create files.\n",
00281 __FILE__, __LINE__, apid, version_number);
00282 }
00283 return HK_DECODER_ERROR_NO_CONFIG_DATA;
00284 }
00285
00286
00287 config_files = apid_configs->ptr_hk_configs;
00288
00289
00290
00291
00292
00293 if(check_for_sdo_apid(apid))
00294 {
00295
00296 ptr_fvn= find_fvn_from_shcids(global_shcids_vn, pkt_date,config_files->apid_number);
00297 matching_config = check_file_version_number(config_files, ptr_fvn);
00298 if ( matching_config == NULL )
00299 {
00300
00301 printkerr("WARNING at %s, line %d: For apid <%x> and file version number <%s> "
00302 "-Cannot find config data to decode packet!!Rereading data files.\n",
00303 __FILE__, __LINE__, apid, ptr_fvn, ptr_fvn);
00304 if( !strcmp(ptr_fvn,"BAD PKT DATE"))
00305 {
00306 printkerr("WARNING at %s, line %d: Skipping Rereading config file since have bad packet date<%s>\n", __FILE__, __LINE__,ptr_fvn);
00307 }
00308 else
00309 {
00310 config_files = reread_all_files(apid_configs, version_number, pkt_date);
00311 ptr_fvn= find_fvn_from_shcids(global_shcids_vn, pkt_date, config_files->apid_number);
00312 matching_config = check_file_version_number(config_files, ptr_fvn);
00313 }
00314 if ( matching_config != NULL )
00315 {
00316 printkerr("WARNING at %s, line %d: Found sdo hk config data for apid <%x> and file version number <%s>\n",
00317 __FILE__, __LINE__,apid, version_number);
00318 }
00319 }
00320 #ifdef DEBUG_DECODE_HK
00321 printkerr("DEBUG:Message at %s, line %d: Found sdo hk config for apid <%x> and file version number <%s>\n",
00322 __FILE__, __LINE__,apid, ptr_fvn);
00323 #endif
00324 }
00325 else
00326 {
00327
00328
00329
00330
00331
00332
00333 matching_config = check_packet_version_number(config_files,version_number);
00334 if ( matching_config == NULL )
00335 {
00336
00337 printkerr("WARNING at %s, line %d: For apid <%x> and packet version number <%s> "
00338 "-Cannot find config data to decode packet!!Rereading data files.\n",
00339 __FILE__, __LINE__, apid, version_number,version_number);
00340
00341 config_files = reread_all_files(apid_configs, version_number, pkt_date);
00342 matching_config = check_packet_version_number(config_files,version_number);
00343 if ( matching_config != NULL )
00344 {
00345 printkerr("WARNING at %s, line %d: Found config data for apid <%x> and packet version number <%s>\n",
00346 __FILE__, __LINE__,apid, version_number);
00347 }
00348 else
00349 {
00350 ;
00351 #ifdef DEBUG_DECODE_HK
00352 printkerr("WARNING at %s, line %d: After reread did not find config data for apid <%x> and packet version number <%s>\n",
00353 __FILE__, __LINE__,apid, version_number);
00354 #endif
00355 }
00356 }
00357 else
00358 {
00359 ;
00360 #ifdef DEBUG_DECODE_HK
00361 printkerr("DEBUG:Message at %s, line %d: Found config data for apid <%x> and packet version number <%s>\n",
00362 __FILE__, __LINE__,apid, version_number);
00363 #endif
00364 }
00365 }
00366
00367
00368 if ( matching_config == NULL )
00369 {
00370
00371 if(check_for_sdo_apid(apid))
00372 {
00373 DHK_ERRMSG("Could not find packet date value because of bad packet date or could not find even after re-reading sdo hk config files.");
00374 }
00375 else
00376 {
00377 DHK_ERRMSG("Could not find packet version number even after re-reading hk config files.");
00378 }
00379 return HK_DECODER_ERROR_CANNOT_FIND_VER_NUM;
00380 }
00381
00382 if (ptr_hk_keywords = load_hk_configs( matching_config ))
00383 {
00384
00385 if (status = load_hk_values( word_ptr, ptr_hk_keywords))
00386 return status;
00387
00388 if (!( status = load_engr_values(ptr_hk_keywords, kw_head)))
00389 {
00390
00391 deallocate_hk_keywords_format(ptr_hk_keywords);
00392 status = HK_DECODER_SUCCESSFUL;
00393 deallocate_apid_ptr_hk_config_nodes();
00394 return status;
00395 }
00396 }
00397 else
00398 {
00399 DHK_ERRMSG("Could not find config data for this packet version number");
00400 status = HK_DECODER_ERROR_CANNOT_LOAD_HK_VALUES;
00401 }
00402 return status;
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 static void get_version_number(unsigned short *wd_ptr, char *ptr_vn)
00414 {
00415
00416 unsigned short w;
00417 w = wd_ptr[7];
00418
00419 sprintf(ptr_vn, "%d.%d", w & 0x00FF, w >> 8 & 0x00FF );
00420 }
00421
00422
00423
00424
00425
00426
00427 static int load_engr_values(HK_Keywords_Format *hk_keywords, HK_Keyword_t **kw_head)
00428 {
00429
00430 HK_Keyword_t *kwt=NULL;
00431 DSC_Conversion *dsc;
00432 int i;
00433
00434
00435
00436 while(hk_keywords)
00437 {
00438 if (!kwt)
00439 *kw_head = kwt = (HK_Keyword_t*) malloc (sizeof (HK_Keyword_t));
00440 else
00441 {
00442 kwt->next = (HK_Keyword_t*) malloc (sizeof (HK_Keyword_t));
00443 kwt = kwt->next;
00444 }
00445 memset(kwt,0,sizeof(HK_Keyword_t));
00446 kwt->next = NULL;
00447
00448 strcpy(kwt->fitsname, hk_keywords->keyword_name);
00449 strcpy(kwt->name, hk_keywords->telemetry_mnemonic_name);
00450 strcat(kwt->name,"\0");
00451
00452
00453 kwt->raw_value = hk_keywords->keyword_value;
00454
00455
00456 if ( hk_keywords->conv_type == 'R')
00457 {
00458 if (!strcmp( hk_keywords->type , "UB"))
00459 {
00460 kwt->eng_type = KW_TYPE_UINT8;
00461 kwt->eng_value.uint8_val = (unsigned char)hk_keywords->keyword_value;
00462 }
00463 else if (!strcmp( hk_keywords->type , "SB"))
00464 {
00465 kwt->eng_type = KW_TYPE_INT8;
00466 kwt->eng_value.int8_val = (signed char)hk_keywords->keyword_value;
00467 }
00468 else if (!strcmp( hk_keywords->type , "IU1"))
00469 {
00470 kwt->eng_type = KW_TYPE_UINT16;
00471 kwt->eng_value.uint16_val = (unsigned short)hk_keywords->keyword_value;
00472
00473 }
00474 else if (!strcmp( hk_keywords->type , "IS1"))
00475 {
00476 kwt->eng_type = KW_TYPE_INT16;
00477 kwt->eng_value.int16_val = (short)hk_keywords->keyword_value;
00478 }
00479 else if (!strcmp( hk_keywords->type , "IL1"))
00480 {
00481 kwt->eng_type = KW_TYPE_INT32;
00482 kwt->eng_value.int32_val = hk_keywords->keyword_value;
00483 }
00484 else if (!strcmp( hk_keywords->type , "UL1"))
00485 {
00486 kwt->eng_type = KW_TYPE_UINT32;
00487 kwt->eng_value.uint32_val = hk_keywords->keyword_value;
00488 }
00489 else if (!strcmp( hk_keywords->type , "SFP"))
00490 {
00491 kwt->eng_type = KW_TYPE_FLOAT;
00492 kwt->eng_value.float_val = translateint2float(hk_keywords->keyword_value);
00493 }
00494 else if (!strcmp( hk_keywords->type , "DFP"))
00495 {
00496 kwt->eng_type = KW_TYPE_DOUBLE;
00497 kwt->eng_value.double_val = (double)translateint2double(hk_keywords->keyword_value);
00498 }
00499 else
00500 {
00501 printkerr("ERROR at %s, line %d: Type '%s' not handled.\n", __FILE__, __LINE__, hk_keywords->type);
00502 return ERROR_HK_UNHANDLED_TYPE;
00503 }
00504 }
00505 else if ( hk_keywords->conv_type == 'D')
00506 {
00507 if (!strcmp( hk_keywords->type,"UB") || !strcmp( hk_keywords->type ,"IU1") || !strcmp( hk_keywords->type,"UL1"))
00508 {
00509 kwt->eng_type = KW_TYPE_STRING;
00510 for (dsc = hk_keywords->dsc; dsc ; dsc= dsc->next)
00511 {
00512 if ( hk_keywords->keyword_value >= dsc->low_range &&
00513 hk_keywords->keyword_value <= dsc->high_range )
00514 {
00515 kwt->eng_value.string_val =(char *) malloc(sizeof(dsc->dsc_value) + 1);
00516 strcpy( kwt->eng_value.string_val , dsc->dsc_value);
00517 break;
00518 }
00519 }
00520 if ( !kwt->eng_value.string_val)
00521 {
00522 kwt->eng_value.string_val =(char *) malloc(sizeof("NO_VALUE") + 1);
00523 strcpy( kwt->eng_value.string_val , "NO_VALUE");
00524 printkerr("WARNING at %s, line %d: There are no DSC data lines to set digital type keyword '%s'.\n"
00525 "Setting value to NO_VALUE. Check config files and run script to update config files.\n",
00526 __FILE__, __LINE__, hk_keywords->keyword_name);
00527 }
00528 }
00529 else
00530 {
00531 printkerr("ERROR at %s, line %d: Type '%s' not handled.\n", __FILE__, __LINE__, hk_keywords->type);
00532 return ERROR_HK_UNHANDLED_TYPE;
00533 }
00534 }
00535 else if (hk_keywords->conv_type == 'A')
00536 {
00537
00538
00539
00540
00541
00542
00543 if (!strcmp( hk_keywords->type,"UB") || !strcmp( hk_keywords->type ,"IU1") || !strcmp( hk_keywords->type,"UL1"))
00544 {
00545 if(hk_keywords->alg)
00546 {
00547 kwt->eng_value.double_val=0.0;
00548 for (i=0; i < hk_keywords->alg->number_of_coeffs; i++)
00549 {
00550
00551 if( i == (MAX_NUMBER_COFFICIENTS))
00552 {
00553 printkerr("WARNING at %s, line %d: Cannot store all coefficients "
00554 "in array with total number of coefficients equal to <%d>. "
00555 "Can only store 6 coefficients in current array. Adjust array "
00556 "size,sscanf,and max number coefficients define in code "
00557 "if needed. Currently will scan in only 6 coefficients "
00558 "values. Skipping using other coefficient values to avoid "
00559 "overrunning array. This could effect analog value.\n",
00560 __FILE__, __LINE__, hk_keywords->alg->number_of_coeffs);
00561 }
00562 else
00563 {
00564
00565 kwt->eng_value.double_val += (double)hk_keywords->alg->coeff[i] * (double)powl((double)hk_keywords->keyword_value, (double)i);
00566
00567 }
00568 }
00569 }
00570 else
00571 {
00572 kwt->eng_value.double_val = 0;
00573 printkerr("WARNING at %s, line %d: Missing ACON line for keyword '%s'."
00574 " Default engr.value set to zero.\n"
00575 "Check config files for missing ACON lines for keyword.\n",
00576 __FILE__, __LINE__, hk_keywords->keyword_name);
00577 }
00578 kwt->eng_type = KW_TYPE_DOUBLE;
00579 }
00580 else
00581 {
00582 printkerr("ERROR at %s, line %d: Engr Type '%s' not handled.\n", __FILE__, __LINE__, hk_keywords->type);
00583 return ERROR_HK_UNHANDLED_TYPE;
00584 }
00585 }
00586 else
00587 {
00588 printkerr("ERROR at %c, line %d: Conversion Type '%s' not handled.\n", __FILE__, __LINE__, hk_keywords->conv_type);
00589 return ERROR_HK_UNHANDLED_TYPE;
00590 }
00591 hk_keywords = hk_keywords->next;
00592 }
00593 return HK_SUCCESSFUL;
00594 }
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 HK_Keywords_Format *load_hk_configs(HK_Config_Files *config)
00607 {
00608 HK_Keywords_Format *new_kw, *new_kw_head;
00609 Keyword_Parameter *config_kw;
00610 DSC_Conversion *config_dsc, *prev_new_dsc, *new_dsc;
00611 ALG_Conversion *config_alg, *new_alg;
00612 int i;
00613
00614 config_kw=config->keywords;
00615
00616 if (config_kw == NULL)
00617 {
00618 DHK_ERRMSG("Null pointer input.");
00619 return NULL;
00620 }
00621
00622 new_kw_head = new_kw = NULL;
00623 while (config_kw)
00624 {
00625 if (!new_kw)
00626 new_kw = new_kw_head = malloc(sizeof(HK_Keywords_Format));
00627 else
00628 {
00629 new_kw->next = malloc(sizeof(HK_Keywords_Format));
00630 new_kw = new_kw->next;
00631 }
00632 new_kw->next = NULL;
00633
00634 strcpy(new_kw->keyword_name, config_kw->keyword_name);
00635
00636 strcpy(new_kw->telemetry_mnemonic_name,
00637 config_kw->telemetry_mnemonic_name);
00638
00639 new_kw->start_byte = config_kw->start_byte;
00640 new_kw->start_bit = config_kw->start_bit_number;
00641
00642 strcpy( new_kw->type, config_kw->type);
00643
00644 new_kw->bit_length = config_kw->bit_length;
00645 new_kw->conv_type = config_kw->conv_type;
00646
00647 if( config_kw->conv_type == 'D')
00648 {
00649 new_kw->dsc = (DSC_Conversion*)NULL;
00650 config_dsc=config_kw->dsc_ptr;
00651 prev_new_dsc=NULL;
00652 while (config_dsc)
00653 {
00654
00655 new_dsc=(DSC_Conversion*) malloc(sizeof(DSC_Conversion));
00656 if (!prev_new_dsc)
00657 {
00658 new_kw->dsc= new_dsc;
00659 }
00660 new_dsc->low_range = config_dsc->low_range;
00661 new_dsc->high_range = config_dsc->high_range;
00662 strcpy(new_dsc->dsc_value,config_dsc->dsc_value);
00663 new_dsc->next = (DSC_Conversion *)NULL;
00664 if(prev_new_dsc)
00665 {
00666 prev_new_dsc->next = new_dsc;
00667 }
00668
00669 config_dsc= config_dsc->next;
00670 prev_new_dsc=new_dsc;
00671 new_dsc= new_kw->dsc->next;
00672 }
00673 }
00674 else
00675 {
00676 new_kw->dsc = (DSC_Conversion*)NULL;
00677 }
00678
00679 if( config_kw->conv_type == 'A')
00680 {
00681 config_alg=config_kw->alg_ptr;
00682 if (config_alg)
00683 {
00684
00685 new_alg = (ALG_Conversion*) malloc(sizeof(ALG_Conversion));
00686 new_kw->alg= new_alg;
00687 new_alg->number_of_coeffs = config_alg->number_of_coeffs;
00688 for(i=0; i < new_alg->number_of_coeffs; i++)
00689 {
00690 new_alg->coeff[i] = config_alg->coeff[i];
00691 }
00692 }
00693 else
00694 {
00695
00696 new_kw->alg= (ALG_Conversion*)NULL;
00697 }
00698
00699 }
00700 else
00701 {
00702 new_kw->alg = (ALG_Conversion*)NULL;
00703 }
00704
00705
00706
00707 config_kw = config_kw->next;
00708 }
00709 return new_kw_head;
00710 }
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720 static int load_hk_values(unsigned short *word_ptr, HK_Keywords_Format *ptr_hk_keywords)
00721 {
00722
00723 HK_Keywords_Format *top_hk_keywords;
00724 unsigned int *w;
00725 unsigned char *byte_ptr;
00726 unsigned int signbit;
00727 unsigned int keep_bits;
00728
00729 uint32_t low_word, high_word;
00730 uint64_t dfp_int;
00731
00732
00733 top_hk_keywords = ptr_hk_keywords;
00734
00735
00736 byte_ptr= (unsigned char*)(word_ptr+3);
00737
00738
00739 while ( ptr_hk_keywords != NULL )
00740 {
00741 if(!strcmp(ptr_hk_keywords->type, "UB") || !strcmp(ptr_hk_keywords->type, "SB") )
00742 {
00743
00744 keep_bits=0;
00745
00746 w = (unsigned int*) &(byte_ptr[ptr_hk_keywords->start_byte]);
00747
00748 keep_bits = (unsigned int) (pow( 2, ptr_hk_keywords->bit_length) - 1) ;
00749
00750 ptr_hk_keywords->keyword_value = (unsigned char)(( *w >> (8 - (ptr_hk_keywords->start_bit + ptr_hk_keywords->bit_length))) & keep_bits);
00751
00752 if(!strcmp(ptr_hk_keywords->type, "SB"))
00753 {
00754
00755 signbit = (unsigned int) (pow( 2, (ptr_hk_keywords->bit_length - 1))) ;
00756
00757 if (signbit & ptr_hk_keywords->keyword_value)
00758 {
00759 ptr_hk_keywords->keyword_value= ptr_hk_keywords->keyword_value | (0xFFFFFFFF ^ keep_bits);
00760 }
00761 }
00762 }
00763 else if( (!strcmp(ptr_hk_keywords->type, "IS1") || !strcmp(ptr_hk_keywords->type, "IU1")) && ptr_hk_keywords->bit_length <= 16)
00764 {
00765
00766 keep_bits=0;
00767
00768 w = (unsigned int*) &(byte_ptr[ptr_hk_keywords->start_byte]);
00769
00770 keep_bits = (unsigned int) (pow( 2, ptr_hk_keywords->bit_length) - 1) ;
00771
00772 ptr_hk_keywords->keyword_value = (unsigned short int) (( (*w ) >> 8) & 0x00FF );
00773
00774 ptr_hk_keywords->keyword_value |= (unsigned short int) (( (*w ) << 8 ) & 0xFF00 );
00775
00776 ptr_hk_keywords->keyword_value = (unsigned int) (( ptr_hk_keywords->keyword_value >> (16 - (ptr_hk_keywords->start_bit + ptr_hk_keywords->bit_length))) & keep_bits);
00777
00778 if(!strcmp(ptr_hk_keywords->type, "IS1"))
00779 {
00780
00781 signbit = (unsigned int) (pow( 2, (ptr_hk_keywords->bit_length - 1))) ;
00782
00783 if (signbit & ptr_hk_keywords->keyword_value)
00784 {
00785 ptr_hk_keywords->keyword_value= ptr_hk_keywords->keyword_value | (0xFFFFFFFF ^ keep_bits);
00786 }
00787 }
00788 }
00789 else if (!strcmp(ptr_hk_keywords->type, "UL1") || !strcmp(ptr_hk_keywords->type, "IL1"))
00790 {
00791
00792 keep_bits=0;
00793
00794 keep_bits = (unsigned int) (pow( 2, ptr_hk_keywords->bit_length ) - 1 ) ;
00795
00796 w = (unsigned int*) &(byte_ptr[ptr_hk_keywords->start_byte]);
00797
00798 ptr_hk_keywords->keyword_value = (unsigned int) ( (*w) >> 24 & 0x000000FF);
00799
00800 ptr_hk_keywords->keyword_value |= (unsigned int) ( (*w) >> 8 & 0x0000FF00);
00801
00802 ptr_hk_keywords->keyword_value |= (unsigned int) ( (*w) << 24 & 0xFF000000);
00803
00804 ptr_hk_keywords->keyword_value |= (unsigned int) ( (*w) << 8 & 0x00FF0000);
00805
00806 ptr_hk_keywords->keyword_value = (ptr_hk_keywords->keyword_value >> (32 - (ptr_hk_keywords->start_bit + ptr_hk_keywords->bit_length ))) & keep_bits ;
00807
00808 if(!strcmp(ptr_hk_keywords->type, "IL1"))
00809 {
00810
00811 signbit = (unsigned int) (pow( 2, (ptr_hk_keywords->bit_length - 1))) ;
00812
00813 if (signbit & ptr_hk_keywords->keyword_value)
00814 {
00815 ptr_hk_keywords->keyword_value= ptr_hk_keywords->keyword_value | (0xFFFFFFFF ^ keep_bits);
00816 }
00817 }
00818 }
00819 else if (!strcmp(ptr_hk_keywords->type, "SFP"))
00820 {
00821
00822 keep_bits=0;
00823
00824 keep_bits = (unsigned int) (pow( 2, ptr_hk_keywords->bit_length ) - 1 ) ;
00825
00826 w = (unsigned int*) &(byte_ptr[ptr_hk_keywords->start_byte]);
00827
00828 ptr_hk_keywords->keyword_value = (unsigned int) ( (*w) >> 24 & 0x000000FF);
00829
00830 ptr_hk_keywords->keyword_value |= (unsigned int) ( (*w) >> 8 & 0x0000FF00);
00831
00832 ptr_hk_keywords->keyword_value |= ( unsigned int) ( (*w) << 24 & 0xFF000000);
00833
00834 ptr_hk_keywords->keyword_value |= (unsigned int) ( (*w) << 8 & 0x00FF0000);
00835
00836 ptr_hk_keywords->keyword_value = (ptr_hk_keywords->keyword_value >> (32 - (ptr_hk_keywords->start_bit + ptr_hk_keywords->bit_length ))) & keep_bits ;
00837
00838 }
00839 else if (!strcmp(ptr_hk_keywords->type, "DFP"))
00840 {
00841
00842 keep_bits=0;
00843
00844 keep_bits = (unsigned int) (pow( 2, ptr_hk_keywords->bit_length ) - 1 ) ;
00845
00846 w = (unsigned int *) &(byte_ptr[ptr_hk_keywords->start_byte]);
00847
00848 low_word = (unsigned int) ( (*w) >> 24 & 0x000000FF);
00849
00850 low_word |= (unsigned int) ( (*w) >> 8 & 0x0000FF00);
00851
00852 low_word |= (unsigned int) ( (*w) << 8 & 0x00FF0000);
00853
00854 low_word |= ( unsigned int) ( (*w) << 24 & 0xFF000000);
00855
00856 w = (unsigned int *) &(byte_ptr[ptr_hk_keywords->start_byte + 4]);
00857
00858 high_word |= (unsigned int) ( (*w) >> 24 & 0x000000FF);
00859
00860 high_word |= (unsigned int) ( (*w) >> 8 & 0x0000FF00);
00861
00862 high_word |= (unsigned int) ( (*w) << 8 & 0x00FF0000);
00863
00864 high_word |= ( unsigned int) ( (*w) << 24 & 0xFF000000);
00865
00866
00867 dfp_int = (uint64_t)high_word ;
00868
00869 dfp_int |= (uint64_t)(((uint64_t)low_word << 32) & (0xFFFFFFFF00000000));
00870 ptr_hk_keywords->keyword_value = (uint64_t)dfp_int;
00871 }
00872 else
00873 {
00874 printkerr("ERROR at %s, line %d: Did not find this bit length for keyword %s\n",
00875 __FILE__, __LINE__, ptr_hk_keywords->keyword_name );
00876
00877 }
00878 ptr_hk_keywords = ptr_hk_keywords->next;
00879 }
00880 ptr_hk_keywords= top_hk_keywords;
00881 return (HK_SUCCESSFUL);
00882 }
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892 static void deallocate_hk_keywords_format(HK_Keywords_Format *head)
00893 {
00894 HK_Keywords_Format *tmp;
00895 DSC_Conversion *d_tmp, *d_head;
00896
00897 while(head)
00898 {
00899
00900 d_head=head->dsc;
00901 while (d_head)
00902 {
00903 d_tmp=d_head->next;
00904 free(d_head);
00905 d_head= d_tmp;
00906 }
00907
00908
00909 if (head->alg)
00910 {
00911 free(head->alg);
00912 }
00913
00914
00915 tmp = head->next;
00916 free(head);
00917 head = tmp;
00918 }
00919 }
00920
00921
00922
00923
00924
00925
00926
00927
00928 void deallocate_hk_keyword(HK_Keyword_t *head)
00929 {
00930 HK_Keyword_t *tmp;
00931
00932 while(head)
00933 {
00934 tmp = head->next;
00935 if (head->eng_type == KW_TYPE_STRING && head->eng_value.string_val)
00936 {
00937 free(head->eng_value.string_val);
00938 }
00939 free(head);
00940 head = tmp;
00941 }
00942 }
00943
00944
00945
00946
00947
00948
00949
00950 HK_Keyword_t *copy_hk_keywords(HK_Keyword_t *head)
00951 {
00952 HK_Keyword_t *newhead, *p;
00953
00954 if (head)
00955 {
00956 newhead = malloc(sizeof(HK_Keyword_t));
00957 if(!newhead)
00958 {
00959 printkerr("ERROR at %s, line %d: malloc failed to allocate space\n",__FILE__, __LINE__);
00960 }
00961 p = newhead;
00962 memcpy(p, head, sizeof(HK_Keyword_t));
00963 if (head->eng_type == KW_TYPE_STRING)
00964 p->eng_value.string_val = strdup(head->eng_value.string_val);
00965 head = head->next;
00966 while(head)
00967 {
00968 p->next = malloc(sizeof(HK_Keyword_t));
00969 if(!p->next)
00970 {
00971 printkerr("ERROR at %s, line %d: malloc failed to allocate space\n",__FILE__, __LINE__);
00972 }
00973
00974 p = p->next;
00975 memcpy(p, head, sizeof(HK_Keyword_t));
00976 if (head->eng_type == KW_TYPE_STRING)
00977 p->eng_value.string_val = strdup(head->eng_value.string_val);
00978 head = head->next;
00979 }
00980 }
00981 else
00982 newhead = NULL;
00983
00984 return newhead;
00985 }
00986
00987
00988
00989
00990
00991 double translateint2double(int64_t ival)
00992 {
00993 #define HK_DPF_RADIX 2
00994 #define HK_DPF_BIAS 1023
00995
00996 int i, x;
00997 int radix;
00998 int bias;
00999 int sign_bit;
01000 int64_t imatissa;
01001 int iexp;
01002 int iexp_posinf;
01003 int iexp_neginf;
01004 double calexp=0.0;
01005 double cal_matissa=0.0;
01006 double sum=0.0;
01007 double d=0.0;
01008
01009
01010 sign_bit=ival >> 63 & 0x00000000000001;
01011
01012
01013 radix=HK_DPF_RADIX;
01014
01015
01016 bias=HK_DPF_BIAS;
01017
01018
01019
01020
01021
01022 iexp=(ival >> 52) & 0x000007FF;
01023
01024
01025 iexp_posinf=(ival >> 52) & 0x000007FF;
01026 iexp_neginf=(ival >> 52) & 0x00000FFF;
01027 if (iexp_posinf == 2047)
01028 {
01029
01030
01031 }
01032 else if (iexp_neginf == 4095)
01033 {
01034
01035
01036 }
01037
01038
01039 calexp=(double)(iexp - bias);
01040
01041
01042
01043
01044
01045
01046
01047 imatissa= (ival) & 0x000FFFFFFFFFFFFF;
01048
01049
01050
01051 for (i =0, x=52; i < 53; i++, x--)
01052 {
01053
01054
01055 if (imatissa & (int64_t)(powl((double)2, (double)x)))
01056 {
01057
01058
01059
01060
01061 sum += (double)(powl((double)2,(double)-i));
01062 }
01063 }
01064 cal_matissa=(double)(1.0 + sum);
01065
01066 d= (double)((pow(-1,sign_bit)) * (cal_matissa) * pow(radix, calexp));
01067 return(d);
01068
01069 }
01070
01071
01072
01073
01074 float translateint2float(int ival)
01075 {
01076 #define HK_RADIX 2
01077 #define HK_BIAS 127
01078
01079 int radix;
01080 int bias;
01081 int sign_bit;
01082 int imatissa;
01083 int iexp;
01084 int iexp_posinf;
01085 int iexp_neginf;
01086 float calexp;
01087 float cal_matissa;
01088 float f;
01089 int i,x;
01090 float sum=0.0;
01091
01092
01093 sign_bit=ival >> 31;
01094
01095
01096 radix=HK_RADIX;
01097
01098
01099 bias=HK_BIAS;
01100
01101
01102
01103
01104
01105 iexp=(ival >> 23) & 0x000000FF;
01106
01107
01108 iexp_posinf=(ival >> 23) & 0x0000007F;
01109 iexp_neginf=(ival >> 23) & 0x000000FF;
01110 if (iexp_posinf == 127)
01111 {
01112 ;
01113
01114
01115 }
01116 else if (iexp_neginf == 255)
01117 {
01118 ;
01119
01120
01121 }
01122
01123
01124 calexp=(float)(iexp - bias);
01125
01126
01127
01128
01129
01130
01131
01132 imatissa= (ival) & 0x007FFFFF;
01133
01134 for (i =1, x=22; i < 24; i++, x--)
01135 {
01136
01137 if (imatissa & (unsigned int)(pow(2, x)))
01138 {
01139
01140
01141
01142
01143 sum += (float)pow(2, -i);
01144 }
01145 }
01146 cal_matissa=1 + sum;
01147
01148
01149
01150 f= (float)((pow(-1,sign_bit)) * (cal_matissa) * pow(radix, calexp));
01151 return(f);
01152
01153
01154
01155
01156
01157 }