/// Agent文件
///
/// 指令处理模块、处理外部接收指令
/// @file    Agent.c
/// @author  LH 
/// @date    2022-04-22
/// @version v1.0

#include "includes.h"
#include "gd32f4xx.h"
#include "enet_to_udp.h"
#include "Lan8720.h"
#include "lwip/tcpip.h"
#include "lwip/raw.h"
#include "lwip/icmp.h"
#include "lwip/dhcp.h"
#include "lwip/netif.h"
#include "lwip/sys.h"
#include "lwip/timers.h"
#include "lwip/inet_chksum.h"
#include "lwip/init.h"
#include "netif/etharp.h"
#include "pdebug.h"

//网络
 BSP_OS_SEM udp_send_data_mutex;
 struct sockaddr_in ptz_from;//IP和端口
 struct sockaddr_in ptz_local;//本地IP和端口
 socklen_t ptz_fromlen;//长度
 int ptz_sock_fd;//句柄
 BSP_OS_SEM udp_mutex;//共享资源锁

BSP_OS_SEM sem_enet_isr_recv;

static void  task_create_recv_enet_isr (void);

extern struct netif fsl_netif0;

extern err_t ethernetif_input(struct netif *netif);

static void task_recv_enet_isr(void *p_arg)
{
	CPU_INT08U err;

	p_arg = p_arg;
	while(1) 
        {    
          OSSemPend(sem_enet_isr_recv,0,&err);
          ethernetif_input(&fsl_netif0);
          
	}
}

static OS_STK task_recv_enet_isr_stk[TASK_RECV_ENET_ISR_STK_SIZE];
static void  task_create_recv_enet_isr (void)
{ 

   CPU_INT08U  task_err;   
   CPU_INT08U  name_err;
   
   task_err = OSTaskCreateExt((void (*)(void *)) task_recv_enet_isr,           
		   (void           *) 0,
		   (OS_STK         *)&task_recv_enet_isr_stk[TASK_RECV_ENET_ISR_STK_SIZE - 1],
		   (INT8U           ) TASK_RECV_ENET_ISR_PRIO,                   
		   (INT16U          ) TASK_RECV_ENET_ISR_PRIO,
		   (OS_STK         *)&task_recv_enet_isr_stk[0],
		   (INT32U          ) TASK_RECV_ENET_ISR_STK_SIZE,
		   (void           *) 0,
		   (INT16U          )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));     
    #if (OS_TASK_NAME_EN > 0)
    OSTaskNameSet(TASK_RECV_ENET_ISR_PRIO, "task_recv_enet_isr", &name_err);
    #endif
    
    if ((task_err == OS_ERR_NONE) && (name_err == OS_ERR_NONE)) {
        pdebug(DEBUG_LEVEL_INFO,"create init_udp_module  success...\n\r");
	} else { 
        pdebug(DEBUG_LEVEL_FATAL,"create init_udp_module  failed...\n\r");
	}

}



//=====以太网接收中断初始化=====
void init_udp_module()
{
  BSP_OS_SemCreate(&udp_mutex,1u,"udp_mutex");
  BSP_OS_SemCreate(&sem_enet_isr_recv,0u,"sem_enet_isr_recv");
  Lan8720_Init();
  task_create_recv_enet_isr();
  term_printf("\n               Lwip Init begin        \r\n\r\n");
  Lwip_init();
}








