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

#ifndef __GW_H__
#define __GW_H__

#include "common.h"
#include "fip.h"
#include "opc.h"

#define APP_VERSION "1.0.0"

//#define FIP_PING_PONG_TST_TRACE
//#define FIP_RW_OP_VIA_RT_APP_TASK
//#define OPC_RX_BLOCKING_SOCKET
#define OPC_RX_XDP_ENABLE

struct gw_measure {
    /* synchro callback measure */
    /* => fip writing operation */
    uint64_t nb_sync_w_op_cb;
    uint64_t cumul_sync_w_op_cb_duration;
    uint64_t min_sync_w_op_cb_duration;
    uint64_t max_sync_w_op_cb_duration;
    uint64_t min_sync_w_op_cb_last;
    uint64_t max_sync_w_op_cb_last;
    /* => fip reading operation */
    uint64_t nb_sync_r_op_cb;
    uint64_t cumul_sync_r_op_cb_duration;
    uint64_t min_sync_r_op_cb_duration;
    uint64_t max_sync_r_op_cb_duration;
    uint64_t min_sync_r_op_cb_last;
    uint64_t max_sync_r_op_cb_last;
    /* => opc pub operation */
    uint64_t nb_pub_op;
    /* => opc sub operation */
    uint64_t nb_sub_op;
    /* fip variables stats */
    uint64_t r_var_ok;
    uint64_t w_var_ok;
    uint64_t r_var_warn;
    uint64_t w_var_warn;
};

struct gw_var_cfg {
    /* powerfip var lib config */
    struct pwrfip_var_cfg pwrfip_cfg;
    /* extra config fields */
    char name[FIP_STR_BSZ_MAX];
    struct pwrfip_node *n; /* parent fip node */
    UA_NodeId v_node_id;   /* opc node id attached to fip variable (var.data)
                            * note: used to attached fip data to a pub/sub
                            * dataset
                            */
#if defined OPC_ENABLE_REALTIME_CAP
    UA_DataValue *rt_value; /* realtime data value */
    UA_ByteString rt_data;
#endif
};

struct gateway {
    /* gateway opc-ua part */
    struct gw_opc opc;
    /* gateway fip part */
    struct gw_fip fip;
    /* gateway measurement */
    struct gw_measure stats;
    /* gateway configuration */
    struct gw_var_cfg cfg_varlist[FIP_VAR_COUNT_MAX];
    int cfg_varcnt;
};

extern struct gateway gw;

/****************************************************************************************
 * GATEWAY FUNCTIONS
 ***************************************************************************************/
int gw_cfg_parse(const char *json_path, struct gateway *gw);
extern void gw_display_update(struct gateway *gw);
/****************************************************************************************
 * OPCUA FUNCTIONS
 ***************************************************************************************/
extern int opc_pub_start(struct gateway *gw);
extern int opc_pub_stop(struct gateway* gw);
extern void opc_display_update(struct gateway *gw);
/****************************************************************************************
 * FIP FUNCTIONS
 ***************************************************************************************/
extern int fip_start(struct gateway *gw);
extern int fip_stop(struct gateway *gw);
extern void fip_display_update(struct gateway *gw);
extern int fip_report(struct gateway *gw);
extern int fip_r_op(struct gateway *gw);
extern int fip_w_op(struct gateway *gw);

#endif
