OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
check_checksum.c
Go to the documentation of this file.
1 #include "PGS_SMF.h"
2 #include "PGS_MODIS_35005.h"
3 #include "hdfi.h"
4 #include "PH_pkt_hdr.h"
5 #include "PD_pkt_data.h"
6 #include "L1A_prototype.h"
7 
8 
9 PGSt_SMF_status check_checksum (PH_PACKET_HEADER_t pkt_header,
10  uint16 *pkt_contents)
11 
12 
13 /*
14 !C************************************************************************
15 
16 !Description: This function will evaluate the current packet's check
17  sum by calculating the check sum and comparing it to
18  the checksum value in the packet. If there is any
19  discrepancy, the function will return an error message.
20 
21 !Input Parameters:
22  uint16 *pkt_contents ** The current packet, with **
23  ** the 12-bit words already **
24  ** unpacked into 16-bit **
25  ** integers **
26 
27  PH_PACKET_HEADER_t pkt_header ** Buffer that contains pkt **
28  ** header info of the pckt **
29 
30 !Output Parameters:
31  None
32 
33 Return Values:
34  MODIS_S_SUCCESS (PGS_MODIS_35005.h)
35  MODIS_E_CHECKSUM_NOT_VALID (PGS_MODIS_35005.h)
36 
37 Externally Defined:
38  PGSt_SMF_status (PGS_SMF.h)
39  uint16 (hdfi.h)
40  PH_PACKET_HEADER_t (PH_pkt_hdt.h)
41  PH_NUM_12BIT_WORDS_IN_HEADER (PH_pkt_hdt.h)
42  PH_PRI_LONG_PKT_LENGTH (PH_pkt_hdt.h)
43  PH_PRI_SHORT_PKT_LENGTH (PH_pkt_hdt.h)
44  PD_NUM_ELMTS_IN_DATA_FIELD_DAY_PKT (PD_pkt_data.h)
45 
46 Called by:
47  load_eng_data
48  process_a_packet
49  process_a_scan
50 
51 Routines Called:
52  log_fmt_msg
53 
54 !Revision History:
55  Revision 2.0 1997/08/14 10:14 EDT
56  Timi Adelekan/GSC (adelekan@ltpmail.gsfc.nasa.gov)
57  Restructered module per version2 development.
58 
59  Revision 1.0 1997/06/18 16:40 EDT
60  Timi Adelekan/GSC (adelekan@ltpmail.gsfc.nasa.gov)
61  Baseline from Version 1.
62 
63 !Team-unique Header:
64  This software is developed by the MODIS Science
65  Data Support Team (SDST) for the National Aeronautics
66  and Space Administration (NASA), Goddard Space Flight
67  Center (GSFC), under contract NAS5-32373.
68 
69 !References and Credits
70  None
71 
72 !Design Notes: The Checksum Algorithm is provided by Santa Barbara
73  Remote Sensing (SBRS), and can be found in (TBS).
74 
75  The CODE below was developed in C language.
76 
77  The checksum Algorithm was produce by Joe Aucther for Sci
78  Test Pkts (6/06/96). It states as follows:
79 
80  Packet checksums
81  ----------------
82  FYI, The checksum is computed as follows:
83  1: Within each packet, the 12-bit science data (or test data)
84  is summed into a 16-bit register, with overflows ignored.
85  2: After all the data is summed, the 16-bit result is right
86  shifted four bits, with zeros inserted into the leading
87  four bits.
88  3: The 12 bits left in the lower end of the word becomes the
89  checksum.
90 
91 !END**************************************************************************
92 */
93 
94  {
95  /**************************************************************************/
96  /* */
97  /* Define and Initialize Local Variables */
98  /* */
99  /**************************************************************************/
100 
101  char *routine; /* Variable to hold routine name */
102  PGSt_SMF_status returnStatus; /* SMF-style message returned by function */
103  uint16 calc_checksum; /* The checksum calculated by this routine */
104  uint16 pkt_checksum; /* The checksum contained within the packet */
105  int begin_sci_data; /* Begining of data field in current packet */
106  int end_sci_data; /* End of data field in current packet */
107  int i; /* loop variable */
108  int num_16bit_words; /* number of 16-bit words in a packet */
109 
110 
111  routine = "check_checksum";
112  returnStatus = MODIS_S_SUCCESS;
113  calc_checksum = 0;
114  pkt_checksum = 0;
115  begin_sci_data = PH_NUM_12BIT_WORDS_IN_HEADER;
116  end_sci_data = 0;
117  num_16bit_words = 0;
118 
119 
120  /**************************************************************************/
121  /* */
122  /* IF pkt_header.pkt_length is equal to PH_PRI_LONG_PKT_LENGTH */
123  /* THEN */
124  /* set num_16bit_words equal to PD_NUM_ELMTS_IN_DATA_FIELD_DAY_PKT */
125  /* ELSE */
126  /* set num_16bit_words equal to PD_NUM_ELMTS_IN_DATA_FIELD_NIGHT_PKT */
127  /* ENDIF */
128  /* */
129  /* set end_science_data equal to PH_NUM_12BIT_WORDS_IN_HEADER + */
130  /* num_16bit_words */
131  /* */
132  /**************************************************************************/
133 
134  if (pkt_header.pkt_length == PH_PRI_LONG_PKT_LENGTH)
135  num_16bit_words = PD_NUM_ELMTS_IN_DATA_FIELD_DAY_PKT;
136 
137  else
138  num_16bit_words = PD_NUM_ELMTS_IN_DATA_FIELD_NIGHT_PKT;
139 
140  end_sci_data = PH_NUM_12BIT_WORDS_IN_HEADER + num_16bit_words;
141 
142 
143 
144  /**************************************************************************/
145  /* */
146  /* Calculate the checksum with the algorithm provided by Joe Aucther */
147  /* for Science Test Packets. Suggested loop: FOR LOOP */
148  /* */
149  /* FOR index equal to begin_of_science_data to end_of_science_data */
150  /* set calc_checksum equal to calc_checksum + pkt_contents[index] */
151  /* END_FOR */
152  /* */
153  /* shift right calc_checksum 4 times */
154  /* */
155  /**************************************************************************/
156 
157  for (i = begin_sci_data; i < end_sci_data; i++)
158  calc_checksum = calc_checksum + pkt_contents[i];
159 
160  calc_checksum >>= 4;
161 
162 
163  /**************************************************************************/
164  /* */
165  /* Extract the actual checksum value from the packet and compare it */
166  /* with the calculated value of checksum */
167  /* */
168  /* set pkt_checksum equal to */
169  /* pkt_contents[PH_NUM_12BIT_WORDS_IN_HEADER + num_16bit_words] */
170  /* */
171  /* IF calc_checksum is not equal to pkt_checksum */
172  /* THEN */
173  /* set returnStatus equal to MODIS_E_CHECKSUM_NOT_VALID */
174  /* set routine equal to "check_checksum" */
175  /* set msg equal to "checksum value of packet does not match */
176  /* calculated checksum" */
177  /* CALL log_fmt_msg to report descrepancy in checksum values */
178  /* INPUTS: returnStatus, routine, msg */
179  /* ENDIF */
180  /* */
181  /**************************************************************************/
182 
183  pkt_checksum = pkt_contents[PH_NUM_12BIT_WORDS_IN_HEADER + num_16bit_words];
184 
185  if (calc_checksum != pkt_checksum) {
186  returnStatus = MODIS_E_CHECKSUM_NOT_VALID;
187  log_fmt_msg (MODIS_E_CHECKSUM_NOT_VALID, routine, "%d does not match pkt checksum",
188  calc_checksum, pkt_checksum);
189  }
190 
191  return returnStatus;
192 
193  } /* End of routine check_checksum */
#define PD_NUM_ELMTS_IN_DATA_FIELD_DAY_PKT
Definition: PD_pkt_data.h:79
#define PD_NUM_ELMTS_IN_DATA_FIELD_NIGHT_PKT
Definition: PD_pkt_data.h:78
#define MODIS_E_CHECKSUM_NOT_VALID
#define PH_NUM_12BIT_WORDS_IN_HEADER
Definition: PH_pkt_hdr.h:78
void log_fmt_msg(PGSt_SMF_status code, const char *routine, const char *msg_fmt,...)
Definition: log_fmt_msg.c:6
PGSt_SMF_status check_checksum(PH_PACKET_HEADER_t pkt_header, uint16 *pkt_contents)
Definition: check_checksum.c:9
#define MODIS_S_SUCCESS
#define PH_PRI_LONG_PKT_LENGTH
Definition: PH_pkt_hdr.h:140
int i
Definition: decode_rs.h:71