/*
 * @Author: ybzhou yibo.zhou@lnxall.com
 * @Date: 2026-07-31 14:30:20
 * @LastEditors: ybzhou yibo.zhou@lnxall.com
 * @LastEditTime: 2026-08-08 16:43:54
 * @FilePath: /release-v2.10.x/proto_forward/src/frpc_cut/frpc_cut.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef _FRPC_CUT_H
#define _FRPC_CUT_H

#include "ipc_session.h"
#include "lnxall_list.h"
#include "mqtt_session.h"
#include "dy_utils/uthash/uthash.h"
#include "dy_utils/dy_common.h"
#include "dy_utils/dy_db.h"
#include "dy_utils/dy_ipc.h"
#include "dy_utils/led.h"

#include "../mqtt_emms2/mqtt_emms2.h"

#define MAX_LINE 512

#define FRPC_SERVICE_FILE "/usr/lib/systemd/system/frpc.service"

#define FRPC_CFG_PATH "/app/config/frpc"

#define FRPC_CFG_FILE    "frpc_cfg.json"

#define FRPC_INI "/etc/frpc.ini"
// #define FRPC_INI  "/opt/lnxall_app/app/config/frpc.ini"
#define FRPC_TOML "/etc/frpc.toml"


#define DEF_FRPC_SERVER_ADDR  "edge.lnxall.com"
#define DEF_FRPC_SERVER_PORT  (57000)
#define DEF_FRPC_METHOD     "token"
#define DEF_FRPC_TOKEN    "lnxall123!@#"

#define FUNCTION_TYPE_GET      "Get"
#define FUNCTION_TYPE_GETRESP  "GetResp"
#define FUNCTION_TYPE_SET      "Set"
#define FUNCTION_TYPE_SETRESP  "SetResp"
#define FUNCTION_TYPE_POST     "LcPost"
#define FUNCTION_TYPE_POSTRESP "LcPostResp"

#define FUNCTION_ID_FPRCUPDATE  "FrpcUpdate"
#define FUNCTION_ID_FPRCRESULT  "FrpcResult"

#define MAX_BUF 1024

typedef enum
{
    FRPC_IN = 0,
    FRPC_OUT,
    FRPC_MAX,     
} n_region_t;

typedef enum
{
    CTL_NO = -1,
    CTL_UA = 0,
    CTL_MQTT,
    CTL_MAX,     
} n_ctrl_t;

typedef struct frpc_cfgi_s
{
    char addr[MAX_HOST_LEN];   //<域名
    int  port;                 //<端口
    char method[MAX_USER_LEN]; //<身份校验的方式
    char token[MAX_PASS_LEN];  //<口令值
} frpc_cfgi_t;

typedef struct frpc_fun
{
    int (*read_frpc_cfg)(frpc_cfgi_t *cfg, const char *path, int origin);
    int (*update_frpc_cfg)(frpc_cfgi_t *cfg, const char *path);
} frpc_fun_t;

typedef struct frpc_cut_t
{
    pthread_t  tid;

    int seq;
    _Atomic int mark;   // 更新标记，mqtt/opc_ua
    int  ctl;           // 0 opc_ua, 1 mqtt        
    
    int     origin;    // frps源，0 国内，1 国外
    int    is_toml;
    char     *file;
    frpc_fun_t *frpc_fun;

    frpc_cfgi_t frpc_cfg[FRPC_MAX];
    frpc_cfgi_t *now;  
    void *var;         // emms2云信息

    int result;
    char sen_buf[MAX_BUF];
    char rev_buf[MAX_BUF];
} frpc_cut_t;

frpc_cut_t *get_frpc_cut(void);

int mqtt_frpc_cut_cmd(mqtt_emms2_var_t* var, cJSON* root, int seq);
int ua_frpc_cut_cmd(int seq);

int frpc_cut_fun(void);

#endif

