/**
  ******************************************************************************
  * @file    net_srv.c
  * @author  wangfq
  * @version V1.0
  * @date    2026-03-02
  * @brief   net message handle: unpack, exeute, response
  *
  ******************************************************************************
  */ 
#ifndef __NET_SRV_H__
#define __NET_SRV_H__

#include <stdint.h>

#define     MAX_COUNTER_NET_STATE_NORMAL        100
//#define     RECE_BUF_LEN                          512                               /* ?????????????��?? */
#define     MAX_REPORT_INTERVAL_MS                30000     //60000        //180??
#define     MIN_REPORT_INTERVAL_MS                6000          // 6??

#define     MAX_REPORT_TIMEOUT_MS                 4000           // 4??


#define     MAX_REPORT_INTERVAL         3
#define     MAX_CLEAR_COUNTER_INTERVAL  4900       // 6hour=6*3600

#define         PORT_TCP_DEFAULT            5550

#define     NET_REPORT_INTERVAL             300            // 300 second
#define     COM_LSSC_MESSAGE_UDP_PORT       5505            //ssc UDP message port
#define     COM_LSSC_TCP_PORT               5550
#define     COM_LSSC_UDP_PORT               5500
#define     COM_DEV_MESSAGE_PORT            4900              //udp dev local message port

#define     TOPIC_DEFAULT_SUBSCRIBE         "ptpc/display"
#define     TOPIC_DEFAULT_PUBLISH           "gtpc/display/Initialize"

#define     UART_BAUD_DEFAULT_PORT_1        115200       //  ��Loop MCU ͨ�ŵĲ����� TTL, g_storage_uart_baud_1
#define     UART_BAUD_DEFAULT_PORT_2        115200      // baud2 TTL g_storage_uart_baud_2



typedef enum
{
    IOT_Addr_IP_Mode = 0,    
    IOT_Addr_DN_Mode    // Domain Name Mode
}IOT_Host_Mode;

typedef struct _LOCAL_NET_CFG	                    
{		
  uint8_t mac[6];
  uint8_t lip[4];																							/*local IP��???IP???��*/
  uint8_t sub[4];																							/*��???????*/
  uint8_t gw[4];
  uint8_t dns[4];																							/*DNS��????��???��*/
  
  uint16_t port_ssc_tcp;
  uint16_t port_ssc_udp;
  uint16_t port_ssc_udp_message;  // use for interval of report
  uint16_t port_dev_tcp;  //use for server http port
  uint16_t port_dev_udp;
    
}Local_Net_Cfg; //32

extern Local_Net_Cfg local_net_cfg;

typedef struct _NET_CENTER_INFO_
{    
    uint8_t lssc_ip[4]; //Local Sotfware Service Center ��????��??��???????
    uint16_t msg_port;
    uint16_t tcp_port;   // use for server http port
    uint16_t udp_port;
    uint8_t sw_ver[2];
}NET_CENTER_INFO;

extern NET_CENTER_INFO net_center_info;

typedef struct _IOT_NET_INFO_
{
    uint8_t remote_addr[64];
    uint16_t mqtt_port;
    uint8_t client_id[64];  //?????������??????��??��?��????
    uint8_t username[64];
    uint8_t password[32];
    
    uint8_t mode;   //0xXX ??0???? 0 IP, 1 dns, ??1???? 0 disable, 1 enable
    //ip, iot_disable, 0x00:0000 0000, 
    //dns,iot_disable, 0x01:0000 0001, 
    //ip, iot_enable,  0x02:0000 0010, 
    //dns,iot_enable,  0x03:0000 0011
}IOT_NET_INFO;
extern IOT_NET_INFO iot_net_info;

#define     MAX_TOPIC_LENGTH            64

typedef struct _IOT_TOPIC_
{
    uint8_t clientid_enable;    // ?��????��?�㨹??��CClientID
    uint8_t topic_pub[MAX_TOPIC_LENGTH];
    uint8_t topic_sub[MAX_TOPIC_LENGTH];
} IOT_Topic;
extern IOT_Topic    g_iot_topic;

#define MQTT_KEEPALIVE_INTERVAL         9


typedef struct _NET_STATE_
{
    uint8_t flag;
    uint8_t intstat;
    uint32_t counter;    
} Net_State;
extern Net_State g_net_state;


void write_net_config(Local_Net_Cfg *local, NET_CENTER_INFO *center, IOT_NET_INFO *iotcfg, IOT_Topic *iottopic);



#define     DPG_API_KEY_METHOD          "\"Method\""
#define     DPG_API_KEY_CODE            "\"Code\""
#define     DPG_API_KEY_DATA            "\"Data\""
#define     DPG_API_KEY_IP              "\"Ip\""
#define     DPG_API_KEY_PORT            "\"Port\""
#define     DPG_API_KEY_PORT_MST        "\"PortMsg\""
#define     DPG_API_KEY_Mac             "\"Mac\""
#define     DPG_API_KEY_SubnetMask      "\"SubnetMask\""
#define     DPG_API_KEY_DNS             "\"DNS\""
#define     DPG_API_KEY_DEVICE_CODE     "\"Device_code\""
#define     DPG_API_KEY_DEVICE_ID       "\"Device_id\""
#define     DPG_API_KEY_EXTRA_ID        "\"Extra_id\""
#define     DPG_API_KEY_PARAMS          "\"Params\""
#define     DPG_API_KEY_AREA_NUM        "\"Area_num\""
#define     DPG_API_KEY_EXTRA_ID        "\"Extra_id\""
#define     DPG_API_KEY_AREA_AMOUNT     "\"Area_amount\""
#define     DPG_API_KEY_DEV_AMOUNT      "\"Dev_amount\""
#define     DPG_API_KEY_DEV_MODE        "\"Dev_mode\""
#define     DPG_API_KEY_TOTAL_AMOUNT    "\"Total_amount\""
#define     DPG_API_KEY_FREE_LOT        "\"Free_lot\""
#define     DPG_API_KEY_DEV_MODE        "\"Dev_mode\""
#define     DPG_API_KEY_DisplayLED      "\"DisplayLED\""
#define     DPG_API_KEY_LED_Addr        "\"LED_Addr\""
#define     DPG_API_KEY_Content         "\"_Content\""

#define		DPG_API_KEY_INFO_ENABLE		"\"Info_Enable\""
#define		DPG_API_KEY_Color			"\"Color\""
#define		DPG_API_KEY_Duration		"\"Duration\""
#define		DPG_API_KEY_Bus_Num		    "\"Bus_Num\""


#define     DPG_API_KEY_Sub_Amount      "\"Sub_Amount\""
#define     DPG_API_KEY_Sub_Dev         "\"Sub_Dev\""


#define     DPG_API_KEY_Dev_Ip          "\"Dev_Ip\""
#define     DPG_API_KEY_Gateway_Ip      "\"Gateway_Ip\""
#define     DPG_API_KEY_Gateway_Port    "\"Gateway_Port\""
#define     DPG_API_KEY_Server_Ip       "\"Server_Ip\""
#define     DPG_API_KEY_Server_Port     "\"Server_Port\""


#define		IOT_API_KEY_HOST			"\"Iot_Host\""
#define		IOT_API_KEY_PORT			"\"Iot_Port\""
#define		IOT_API_KEY_USERNAME		"\"UserName\""


//add by wfq 20190812
#define DPG_KEY_PARAMS_JSON              "\"Params\""
#define KEY_CODE_AccessReport        "AccessReport"
#define KEY_CODE_DPG_Addr            "\"Dpg_Addr\""
#define KEY_CODE_Access_Channel      "\"Access_Channel\""
#define KEY_CODE_State_InOut         "\"State_InOut\""
#define KEY_CODE_Time_Stamp          "\"TimeStamp\""

#define KEY_CODE_Time_Counter        "\"Time_Counter\""

#define KEY_CODE_Bus_Num			 "\"Bus_Num\""
#define KEY_CODE_Dus_Addr_Enable	 "\"Dus_Addr_Enable\""

//-------------------

#define  SSC_Code_Initialize           "\"Initialize\""
#define  SSC_Code_UnInitialize         "\"UnInitialize\""
#define  SSC_Code_Connect              "\"Connect\""
#define  SSC_Code_DisConnect           "\"DisConnect\""
#define  SSC_Code_SendCommand          "\"SendCommand\""
#define  SSC_Code_Speak                "\"Speak\""

#define  SSC_Code_Count_Off            "Count_Off"
#define  SSC_Code_Device_Info          "Device_Info"
#define	 SSC_Code_Devs_Info			   "Devs_Info"		//add by wfq 2020-07-14  ?????????????��??��??����????����?????
#define  SSC_Code_Global_Require       "Global_Get"
#define  SSC_Code_Global_Set           "Global_Set"
#define  SSC_Code_Collect_ALL          "Collect_All"    //?????��????????????
#define  SSC_Code_Collect_Single       "Collect_Single" //??????????????????
#define  SSC_Code_Config               "Config"
#define  SSC_Code_DisplayLED           "DisplayLED"
#define  SSC_Code_Device_Net_Set       "Device_Net_Set" //?????����?????????  
#define  SSC_Code_HeartBeat            "HeartBeat"
#define  SSC_Code_CDev_Subs            "CDev_Subs"      //????��??����????��????
#define  SSC_Code_Device_IOT_Set       "Dev_IOT_Set"    //?��??IOT???????? MQTT???��?????????????��??????


#define  SSC_Code_ACS_Collect_Area     "ACS_Collect_Area"
#define  SSC_Code_ACS_Collect_Counter  "ACS_Collect_Counter"	//?????????????????��??????  add 2021-01-04

#define  SSC_Code_Collect_NT_BUS	   "Collect_NT_BUS"		//??????????????��??????��????
#define  SSC_Code_CDetail_NT_BUS       "CDetail_NT_BUS"     //??????????????��????��?????��????   add 2022-04-19 

#define  SSC_Code_Radar_Reset           "Radar_Reset"       //?��??????   2021-04-23
#define  SSC_Code_SerialNet             "SerialNet"         //485????????  2021-04-23
#define  SSC_Code_Dev_Reset             "Dev_Reset"         // ?����??????? 2024-12-13


#define  SSC_Code_GetDeviceInfo        "\"GetDeviceInfo\""
#define  SSC_Code_SetDeviceInfo        "\"SetDeviceInfo\""
#define  SSC_Code_SetDeviceInfoEX      "\"SetDeviceInfoEX\""
#define  SSC_Code_StateChangedEvent    "\"StateChangedEventReg\""

#define KEY_IOT_HOST                 "\"Host\""
#define KEY_IOT_PORT                 "\"Port\""
#define KEY_IOT_USERNAME             "\"UserName\""
#define KEY_IOT_PASSWORD             "\"Password\""
#define KEY_IOT_MODE                 "\"Mode\""


//add by wfq 2019-08-14
#define  SSC_Code_ACS_Collect_Access    "ACS_Collect_Access"
#define  SSC_Code_TimeStamp             "TimeStamp"

#define	 SSC_Code_Dev_Time				"Dev_Time"
#define  SSC_Code_SSC_Time				"\"SSC_Time\""

//add by wfq 2020-7-11
#define  SSC_Code_ACS_NT_Event			"ACS_NT_Event"

#define  SSC_Code_GetDusCFG				"GetDusCFG"		//add by wfq 2020-7-23
#define  SSC_Code_SetDusCFG				"SetDusCFG"		//add by wfq 2020-7-23

#define  SSC_Code_ACS_Collect_CJQ    "ACS_Collect_CJQ"

//add by wangfq 2022-04-14
#define SSC_Code_Tran_Sub             "Tran_Sub"        // ??��??����??��??????
#define SSC_Code_Dev_Type             "\"Dev_Type\""        // ?����??��?? ��?��????? 20 DLD950
#define SSC_Code_Cmd                  "Cmd"       
#define SSC_Code_Detail               "Detail"


extern uint8_t g_flag_timestamp;
extern uint8_t RemoteIP[4];

#if NET_SSC_ENABLE
extern uint8_t SocketId_TCP;
extern uint8_t SocketId_UDP;
#endif


void manage_udp_message(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);

void unpack_ssc_count_off(uint8_t socket, uint8_t count_mode, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
void unpack_ssc_device_reset(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
void unpack_ssc_device_net_set(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
void unpack_dev_iot_config(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
void unpack_ssc_set_display_led(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
void unpack_ssc_tran_sub(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
void unpack_ssc_radar_reset(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);

void unpack_ssc_timestamp(uint8_t *buf, uint32_t len);
void unpack_ssc_collect_cjq_acs_response(uint8_t socket, uint8_t *buf, uint32_t len);

//void acs_dus_report(void);
//void rcv_acs_dus_report_reponse(void);

void dev_response_heartbeat(char *dat);

void dev_send_heartbeat(void);
void dev_get_timestamp_send(void);
void manage_tcp_message(uint8_t socket, uint8_t *buf, uint32_t len);
void manage_mqtt_recv_message(char * msg, int length);


void GetMacAddr(unsigned char *pMAC);
void mStopIfError(u8 iError);

void net_srv_init(void);
#if NET_SSC_ENABLE
void WCHNET_CreateTcpMqttSocket(void);
void WCHNET_CreateTcpSocket(void);
#endif
void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat);
int get_ipstr_to_array(char *src, uint8_t *dst);
void WCHNET_HandleGlobalInt(void);



#endif
