#include <aspoprotocol.h>
#include <aspoutil.h>

#include <signal.h>
#include <stdint.h>

/* include this file from your postproc lib source to make func decl match */
/* declarations for the functions from dlopen()'ed lib */

/* poll_modbus wants "HOST:[U][PORT]:STA:CNT" arguments, the three below copies thoose values */
/* (port number is not saved for posterity) */
struct proc_data_t {
  const char *host;
  uint16_t starting_address;
  uint16_t quantity; /* == size of what data points to */
  int fd; /* file descriptor to read from or write to, to communicateo to the HOST:PORT */
  uint16_t tid; /* modbus transaction id, to match req. and res. packets */
  uint16_t *data; /* the data we got */
};

int proc_init(const char *arg);
int proc_run (size_t sz, struct proc_data_t *proc_data, struct timespec *timeout);
int proc_fini(void);

struct options_t {
  struct timespec interval;
  unsigned count;
  int (*dl_init)(const char *arg);
  int (*dl_proc)(size_t sz, struct proc_data_t *proc_data, struct timespec *timeout);
  int (*dl_fini)(void);
};
extern struct options_t g_opt;

#define REQSZ     (MODBUS_HDRSZ_MBAP+PDUSZ_03_Read_Holding_Registers)
#define RESSZ(n)  (MODBUS_HDRSZ_MBAP+2+2*(n)) /* n = number of requested registers */

void sighandler(int sig, siginfo_t *si, void *context);
size_t summing(int sz, uint16_t *arr);
int run(int devsz, struct net_state *netstate, uint16_t *starting_address, uint16_t *quantity);
int run_read(int fd, int ressz, uint8_t *res, int to); /* to timeout in ms */

/* cnv_data = a1 + a2 * x / mx;  0xffff => cnv_data = -99 */
double cnv_data(uint16_t ival, double a1, double a2, int mx);
/* default postprocessing: printout all raw values */
int process_data_print(size_t sz, struct proc_data_t *proc_data, struct timespec *timeout);
