OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
read_packet.c
Go to the documentation of this file.
1 // Ported from IDL procedure to read a single CCSDS packet from a file.
2 
3 // Arguments
4 //
5 // Name Type I/O Description
6 // ---- ---- --- -----------
7 // infile FILE I Input file pointer
8 // packet byte(*) O Byte array containing packet
9 // len int O Length of packet in bytes
10 // endfile long int O File Length (bytes); value set to 0 at the end of file
11 // Liang Hong, July 22, 2015
12 // Liang Hong, Sept. 21, 2015: changed feof check to compare pointer location with file size
13 
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdint.h>
17 
18 int read_packet(FILE *infile, uint8_t packet[], int *len, long int *endfile) {
19  // Check for end of file
20  long int currpos = ftell(infile);
21  if (currpos >= *endfile) {
22  //Check to see if the pointer reaches end of file
23  *endfile = 0;
24  *len = 0;
25  //printf("End of file\n");
26  return 0;
27  }
28 
29  // Read packet header
30  uint8_t phead[6];
31  fread(phead, 1, 6, infile);
32 
33  // Get length of packet body
34  *len = phead[4]*256 + phead[5] + 1;
35  uint8_t pbod[*len];
36 
37  fread(pbod, 1, *len, infile);
38 
39  // Concatenate header and body
40  memcpy(packet, phead, 6);
41  memcpy(packet + 6, pbod, *len);
42 
43  *len = *len + 6;
44 
45  return 0;
46 }
int read_packet(FILE *infile, uint8_t packet[], int *len, long int *endfile)
Definition: read_packet.c:18