// $Id: pdinrs.h,v 1.4 2005/03/30 05:07:14 pdi Exp $
//*****************************************************************************/
// Copyright 1998-2022 Stonie R. Cooper, Licensed under the auspices of GPLv3,
// See top-level copyright disclaimer (COPYRIGHT) and license (LICENSE)
//                    Marietta, Georgia.
// File:            pdinrs.h
// Author: Stonie R. Cooper
// Documentation:   NA
// Revision Record: (as follows)
//         Rewritten: 20050114 by Stonie R. Cooper
//                  Established initial file for pdinrs.
//        Revision: 19980810.0000UTC by Stonie R. Cooper
//                  Added defines
//        Revision: 19980816.1417UTC by Stonie R. Cooper
//                  Added defines for bit masking for TransferType.
//        Revision: 19980819.0042UTC by Stonie R. Cooper
//                  Added FILE * to log_type.
// Dependencies:
//*****************************************************************************/

// PDINRS_H BEGIN

#ifndef PDINRS_H
#define PDINRS_H

// INCLUDE BEGIN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <getopt.h>
#include <regex.h>
#include <signal.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <pdi_logger.h>
#include <sys/time.h>

// INCLUDE END

// EXTERN BEGIN

// EXTERN END

// DEFINE BEGIN

#define MAXFRAMESIZE            5224
#define MAXLINESIZE              512
#ifndef FALSE
#define FALSE                      0
#endif
#ifndef TRUE
#define TRUE                       1
#endif

typedef unsigned char PDI_Byte;

// masking numbers for determining the transfer type in Product Definition H.

#define PDH_START_PRODUCT          1
#define PDH_TRANSFER_IN_PROGRESS   2
#define PDH_LAST_PACKET            4
#define PDH_PRODUCT_ERROR          8
#define PDH_PRODUCT_ABORT         32
#define PDH_OPTION_HEADER_INC     64
#define CCB_SIZE_BYTE_SHIFT        2
#define HE_SIZE                 8192

// And, well, errr, I guess we better do some debugging . . .

// PDINRS VERSION NUMBER

#define SLEEP_TIME                 2

// PDINRS CONFIG RETURN CODES

#define PDINRSCONFIG_FOPEN_ERROR  -2
#define PDINRSCONFIG_SUCCESS       0
#define PDINRSCONFIG_MAX_EXCEEDED  1

#ifndef HOSTNAME_SIZE
#define HOSTNAME_SIZE            128
#endif
#ifndef SHORTLINESIZE
#define SHORTLINESIZE             81 
#endif

#define UDP_ARRAY_SIZE             8

// UDP ERROR RETUNRS
#define UDP_PDI_LONGFRAME  	  -1
#define UDP_PDI_READERROR         -2
#define UDP_PDI_SHORTFRAME        -3
#define UDP_PDI_TIMEOUT           -4

// REPLAY SETTINGS
#define REPLAY_OFF                 0
#define REPLAY_RECORD              1
#define REPLAY_PLAYBACK            2

// REPLAY - WHEN TO START REPLAY
#define FROMBEGINNING              0
#define ABSOLUTEDATETIME           1

// DEFINE END

// CONST BEGIN

// CONST END

// TYPEDEF BEGIN

typedef struct
{
  unsigned char                    state;
  char                             archive_path[(MAXLINESIZE-2)];
  time_t                           duration;
  char                             start_seek;
  time_t                           start;
} replayType;

typedef struct
{
  unsigned int                seconds;
  unsigned int                microseconds;
  unsigned int                frame_size;
} replayDataSegmentType;

typedef struct
{
  unsigned int                seconds;
  unsigned int                microseconds;
  double                           vber;
  double                           ebno;
} replayDemodSegmentType;

typedef struct
{
  unsigned int                begin_time;
  unsigned int                end_time;
} pdinrsMainType;

typedef struct
{
  unsigned int                pdinrs_version;
  unsigned int                maximum_bytes;
  unsigned int                first_data_byte;
  unsigned int                next_write_byte;
  unsigned int                next_read_byte;
  signed int                  frame_count;
} buffer_header_type;

typedef struct
{
  pthread_mutex_t                  *maximum_bytes;
  pthread_mutex_t                  *first_data_byte;
  pthread_mutex_t                  *next_write_byte;
  pthread_mutex_t                  *next_read_byte;
  pthread_mutex_t                  *frame_count;
} buffer_header_mutex_type;

typedef struct
{
  unsigned char                    quiet; // turn all stdout/logging off.
  pthread_mutex_t                  *quiet_mutex;
  unsigned char                    reset; // will reset stats.
  pthread_mutex_t                  *reset_mutex;
//  unsigned char                    restart; // if stop had been set, start up.
//  pthread_mutex_t                  *restart_mutex;
  unsigned char                    exit; // exit all processing.
  pthread_mutex_t                  *exit_mutex;
} user_interface_type;

typedef struct
{
  unsigned char                    restart; // if stop had been set, start up.
  pthread_mutex_t                  *restart_mutex;
} restart_type;

typedef struct
{
  double                           rawBER; // raw BER report from receiver.
  double                           EBNZero; // EBN0 report from receiver.
  pthread_mutex_t                  *receiver_mutex;
} receiver_type;

typedef struct
{
  char                             label[81];
  replayType                       *replay;
  unsigned char                    array_number;
  user_interface_type              *ui;
  restart_type                     *restart;
  char                             config_file[256];
  log_type                         *error_file;
  buffer_header_mutex_type         *mutex;
  buffer_header_type               *header;
  pthread_cond_t                   *new_data_cond;
  pthread_mutex_t                  *new_data_mutex;
  unsigned char                    *new_data_flag;
  pthread_mutex_t                  *idr_mutex;
  pthread_mutex_t                  *odr_mutex;
  pthread_mutex_t                  *cm_mutex;
  pthread_mutex_t                  *cno_mutex;
  unsigned int                *idr;
  unsigned int                *odr;
  unsigned int                *product_size;
  char                             *cm;
  unsigned char                    *cno;
  time_t                           *rt; // Reset Time
  unsigned int                *ff; // FirstFrame (SBN_Sequence)
  unsigned int                *cf; // CurrentFrame (SBN_Sequence)
  unsigned int                *fr; // Frame Rollover (>999999999)
  unsigned int                *mt; // MissingTotal
  unsigned int                *mr; // MissingFrameRollOver (>999999999)
  unsigned int                *tb; // TotalBytes
  unsigned int                *tgb; // TotalGigaBytes
  char                             *LongNCFWaitWmo;
  unsigned int                *LongNCFWait;
  signed int                  *CurNCFWait;
  unsigned int                *AveNCFWait;
  unsigned int                *LongTransit;
  signed int                  *CurTransit;
  unsigned int                *AveTransit;
  receiver_type                    *demod; // report from live receiver
} buffer_type;

typedef buffer_type distribution_type;

typedef struct
{
  user_interface_type              *ui;
  restart_type                     *restart;
  char                             config_file[256];
  char                             label[81];
  unsigned char                    array_number;
  log_type                         *error;
  pthread_t                        channel_thread;
  unsigned char                    priority;
  replayType                       *replay;
} channel_type;

// TYPEDEF END

// PROTOTYPE BEGIN

void nrs_buffer(buffer_type *buffer_struct);

void nrs_distribution(distribution_type *distribution_struct);

void channel_stream(channel_type *channel_struct);

int create_ftp_script(char *p);

int create_list_script(char *p);

// PROTOTYPE END

#endif

// PDINRS_H END
