#include "CM_Types.h"
#include "CFM_Net.h"
#include "PFM_Cfg.h"
#include "CM_Memory.h"
#include "CM_UDP.h"
#include "CM_MultiThread_Priority.h"
#include "CM_MultiThread.h"
#include "Area_2oo2.h"
#include "CM_Socket.h"
#include "PFM_Cfg.h"
#include "MSCP_Define.h"
#include "PFM_Log.h"

/*
 * Area_2oo2.c
 *
 *  Created on: 2025年12月2日
 *      Author: adan
 */


#define Area_2oo2_MSG_NUM_MAX (200u)
#define Area_INNET_RAW_MSG_SIZE  (2048u)


ENUM_CM_RETURN Area_2oo2_Send(IN const CM_UINT8* p_data, IN CM_INT32 len);
CM_INT32 Area_2oo2_Recv(OUT CM_UINT8* p_data, IN CM_UINT32 BufSize);



 STR_Area_Net_Cfg g_Area_2oo2_Cfg;
 /**
 * @brief 2oo2初始化
 * 
 */

void Area_2oo2_Init()
{
    CM_UINT32 val= 0;
    ENUM_CM_RETURN rt = ENUM_CM_FALSE;
    CM_BOOL rtCheck = 0;
    /** 2oo2网络结构体初始化 */
    g_Area_2oo2_Cfg.Net_2oo2_Ctl.SN = 0;
    g_Area_2oo2_Cfg.Net_2oo2_Ctl.DataSend = Area_2oo2_Send;
    g_Area_2oo2_Cfg.Net_2oo2_Ctl.DataRecv = Area_2oo2_Recv;
    /** 2oo2网络初始化 */
    CM_Memcpy(g_Area_2oo2_Cfg.IP_Local, 32 , g_PFM_Cfg.Local_Cfg.IP_2oo2 , 32);
    CM_Memcpy(g_Area_2oo2_Cfg.IP_Pear,  32 , g_PFM_Cfg.Peer_Cfg.IP_2oo2, 32);
    g_Area_2oo2_Cfg.Port = g_PFM_Cfg.Local_Cfg.Port_2oo2_Area;
     /*- 创建socket */
    rt = CM_UDP_Socket_Create(&g_Area_2oo2_Cfg.Socket, CM_FALSE, (CM_UINT32)inet_addr(g_Area_2oo2_Cfg.IP_Local), g_Area_2oo2_Cfg.Port, 0u, (CM_UINT8)1u);
    CM_ASSERT(ENUM_CM_TRUE == rt, NET_MODE, DARK_FAULT_CODE_FUN_ORDER_2, TSCP_DARK_ASSERT_1);
        /*- 配置socket */
    val = (CM_INT32)NET_UDP_BUF_MAX;
    (void)setsockopt(g_Area_2oo2_Cfg.Socket, SOL_SOCKET, SO_RCVBUF, (void*)(&val), (unsigned int)sizeof(val));

    /** 2oo2接收消息队列初始化 */
    rt = CM_MQ_Init(&g_Area_2oo2_Cfg.MsgQ, (CM_UINT16)Area_2oo2_MSG_NUM_MAX, (CM_UINT16)Area_INNET_RAW_MSG_SIZE);
    CM_ASSERT(ENUM_CM_TRUE == rt, NET_MODE, DARK_FAULT_CODE_FUN_ORDER_2, TSCP_DARK_ASSERT_1);

	/*- 启动2oo2接收线程 */
    rtCheck = CM_Thread_Create((CM_THREAD_T*)NULL, (CM_THREAD_FUNC_T)t_Area_2oo2_UdpRecvThread, "Area_2oo2_Rx", 0, CM_TASK_PRIORITY_73, (CM_Thread_Arg_Struct_T*)&g_Area_2oo2_Cfg);
	CM_NM_ASSERT(CM_TRUE == rtCheck, NET_MODE,DARK_FAULT_CODE_FUN_ORDER_1, TSCP_DARK_ASSERT_24);



}

/**
 * @brief 2oo2发送数据
 * 
 * @param p_data 指向要发送数据的指针
 * @param len 要发送数据的长度
 * @return ENUM_CM_RETURN 发送状态
 */
ENUM_CM_RETURN Area_2oo2_Send(IN const CM_UINT8* p_data, IN CM_INT32 len)
{
    ENUM_CM_RETURN rt = ENUM_CM_FALSE;
    if(NULL != p_data)
    {
        rt = CFM_2oo2_Send_Currency(g_Area_2oo2_Cfg.Socket, p_data, len, g_Area_2oo2_Cfg.Port);
        CM_ASSERT(ENUM_CM_TRUE == rt, NET_MODE, DARK_FAULT_CODE_FUN_ORDER_2, TSCP_DARK_ASSERT_1);
    }
    else 
    {
        CM_ASSERT(ENUM_CM_TRUE == rt, NET_MODE, DARK_FAULT_CODE_FUN_ORDER_2, TSCP_DARK_ASSERT_1);
    }
    return rt;
}
/**
 * @brief 2oo2接收数据
 * 
 * @param p_data 指向接收数据存储的指针
 * @param BufSize 接收数据存储缓冲区大小
 * @return CM_INT32 实际接收数据长度
 */
CM_INT32 Area_2oo2_Recv(OUT CM_UINT8* p_data, IN CM_UINT32 BufSize)
{
    CM_UINT16 rcvlen = 1024;
    ENUM_CM_RETURN rt = ENUM_CM_FALSE;
    CM_UINT8 buf[1024] = {0};
    if(NULL != p_data)
    {
    	//MSCP_LOG_MSG("2oo2 Qlen %d \r\n", CM_MQ_Get_Msg_Num(&g_Area_2oo2_Cfg.MsgQ));
        rt = CM_MQ_Read(&g_Area_2oo2_Cfg.MsgQ, buf, &rcvlen);
        	/*- 是否获取成功 */
        if ((ENUM_CM_TRUE == rt) && (0u < rcvlen))
        {
            /*- 输出接收数据 */
            rt = CM_Memcpy((void*)p_data, (CM_SIZE_T)BufSize, (void*)buf, (CM_SIZE_T)rcvlen);
            //CM_RE_ASSERT(ENUM_CM_TRUE == rt, NET_MODE, DARK_FAULT_CODE_FUN_ORDER_39, TSCP_DARK_ASSERT_2);
        }
        else
        {
        	rcvlen = 0u;
        }

    }
    else 
    {
        CM_ASSERT(ENUM_CM_TRUE == rt, NET_MODE, DARK_FAULT_CODE_FUN_ORDER_2, TSCP_DARK_ASSERT_1);
    }
    return rcvlen;
}




CM_INT32 Area_2oo2_Currency_NoSplit(IN const CM_UINT8 * pMyData, IN CM_INT32 DataLen, OUT CM_UINT8 * pPeerData, IN CM_UINT32 DataSize)
{
    return CFM_2oo2_Currency_NoSplit(&g_Area_2oo2_Cfg.Net_2oo2_Ctl, pMyData, DataLen, pPeerData, DataSize);
}

