/*
 * Copyright (C) 2022 EXOLIGENT (www.exoligent.com)
 * Author: Maxime Coroyer <maxime.coroyer@exoligent.com>
 */

#ifndef __MBUS_H__
#define __MBUS_H__

#include "modbus/modbus.h"

#define MBUS_CLIENT_COUNT_MAX 32

struct gw_mbus_var {
    int addr; /* register start address */
    int nb; /* registers number */
};

#define MBUS_VAR_COUNT_MAX 500
struct gw_modbus {
    int mode; /* 0: rtu, 1: tcp */
    /**
     * TCP settings
     */
    char ip_str[16];
    int port;
    int socket; /* mbus server socket */
    /**
     * RTU settings
     */
    unsigned long baudrate; /* */
    int parity; /* 0: none, 1: odd, 2: even */
    int data_bit; /* default: 8bits */
    int stop_bit; /* default: 1bits (1 or 2) */
    /**
     * Common settings
     */
    pthread_t thr_task;
    volatile int task_run;
    modbus_t *ctx;
    modbus_mapping_t *mapping;
    uint8_t *query;
    int mapping_locked;
    /* input registers attached to fip periodic cons var */
    struct gw_mbus_var cons_varlist[MBUS_VAR_COUNT_MAX];
    int cons_varcnt;
    /* holding registers attached to fip periodic prod var */
    struct gw_mbus_var prod_varlist[MBUS_VAR_COUNT_MAX];
    int prod_varcnt;
    /**
     * Statistics settings
     */
    int nb_co;
    /* request stats */
    uint32_t req_ok;
    uint32_t req_nok;
    /* response stats */
    uint32_t res_ok;
    uint32_t res_nok;
};

#endif
