

#include "head.h"


//#include <linux/kernel.h>
//#include <linux/init.h>
//#include <linux/module.h>
//#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <linux/sched.h>
//#include <linux/interrupt.h>
//#include <linux/kthread.h>
//#include <linux/delay.h>
#include <linux/wait.h> 
//#include <asm/uaccess.h>
//#include <asm/io.h>
#include <linux/errno.h>
//#include <asm/ioctls.h>
#include <linux/ioctl.h>
//#include <linux/signal.h>
#include "rt_clock.h"
#include "rt.h"
#include "bsp.h"
#include "app_goose.h"

// 直接发送和接收时使用的设备编号
enum ENUM_GOOSE_DEV_IDX
{
	GOOSE_NET_DEVICE0 = 0,
	GOOSE_NET_DEVICE1,
};


static unsigned char g_goose_init_flag = 0;

unsigned char goose_get_init_flag(void)
{
	return g_goose_init_flag;
}


// sunxi 软中断进行goose数据发送
int _goose_send_soft_isr(int irq, void *dev_id)
{
	rt_sirq_clear(RT_SOFT_IRQ_GOOSE_SEND);

#ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下，启动不编译进模块里面。即goose功能的启动作为独立的模块
	if(!g_goose_init_flag)
		return 0;

	if(tFAg.bSend)
	{
		app_goose_pub_data_handle((char *)tFAg.outval, GOOSE_SEND_TBL_NUMBER);
		tFAg.bSend = false;
	}
#endif // __IEC61850_GOOSE_FUNC__
	return 0;
}

#ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下，启动不编译进模块里面。即goose功能的启动作为独立的模块
#define GOOSE_READ_BUF_MAX		1540
static unsigned char goose_recv_data_buf[GOOSE_READ_BUF_MAX] = {0x00};
extern int goose_direct_read(int devno, char *data, int len);
extern int goose_direct_write(int devno, const char *data, int len);
static unsigned char g_can_enter_flag = 1;
#endif // __IEC61850_GOOSE_FUNC__

// 接收goose数据软中断，每468us中断一次
int _goose_recv_soft_isr(int irq, void *dev_id)
{
#ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下，启动不编译进模块里面。即goose功能的启动作为独立的模块
	int len = 0;
	int readlen = 0;
#endif // __IEC61850_GOOSE_FUNC__

	rt_sirq_clear(RT_SOFT_IRQ_GOOSE_RECV);

#ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下，启动不编译进模块里面。即goose功能的启动作为独立的模块
	if(!g_can_enter_flag)
		return 0;
	g_can_enter_flag = 0;
	len = goose_direct_read(GOOSE_NET_DEVICE1, goose_recv_data_buf,  GOOSE_READ_BUF_MAX);

	// 没有初始化或者缓冲区没有数据
	if(!g_goose_init_flag || !len)
	{
		g_can_enter_flag = 1;
		return 0;
	}

	if(len >= GOOSE_READ_BUF_MAX)
		len = GOOSE_READ_BUF_MAX;

	readlen = (GOOSE_RECV_TBL_NUMBER) * sizeof(int);
	if(readlen == app_goose_recv_data_handle(0, goose_recv_data_buf, len, (char *)tFAg.inval))
		fa_g_unpack();   //对接收的数据进行解析

	rt_sirq_force(RT_SOFT_IRQ_GOOSE_HANDLE);
	g_can_enter_flag = 1;
#endif // __IEC61850_GOOSE_FUNC__
	return 0;
}

int _goose_handle_soft_isr(int irq, void *dev_id)
{
#ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下，启动不编译进模块里面。即goose功能的启动作为独立的模块
	static uint32_t us_old = 0;
	uint32_t us_new, us_diff;
#endif // __IEC61850_GOOSE_FUNC__

	rt_sirq_clear(RT_SOFT_IRQ_GOOSE_HANDLE);

#ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下，启动不编译进模块里面。即goose功能的启动作为独立的模块
	us_new = ustimer_get_origin();
	us_diff = us_new - us_old;

	if(!g_can_enter_flag)
		return 0;
	us_old = us_new;
	g_can_enter_flag = 0;
	app_goose_timer_data_handle(us_diff);
	g_can_enter_flag = 1;
#endif // __IEC61850_GOOSE_FUNC__

	return 0;
}

// sunxi 20190425 把原来在 468us中断的函数处理中移到 500us 中断中来处理
void goose_timer_handle(void)
{
	if(g_goose_init_flag)
	{
		rt_sirq_force(RT_SOFT_IRQ_GOOSE_HANDLE);
	}
}

/*********************************************************/

void goose_send_wakeup(void)
{
	rt_sirq_force(RT_SOFT_IRQ_GOOSE_SEND);
}

void goose_recv_wakeup(void)
{
	rt_sirq_force(RT_SOFT_IRQ_GOOSE_RECV);
}
//初始化
int goose_init(void)
{
#ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下，启动不编译进模块里面。即goose功能的启动作为独立的模块
	if(app_goose_init(goose_direct_write) == 0)
	{
		goose_pub_init((char *)tFAg.outval, GOOSE_SEND_TBL_NUMBER);

		// 登记主循环软中断
		rt_sirq_register(RT_SOFT_IRQ_GOOSE_RECV, _goose_recv_soft_isr, "_goose_recv_soft_isr");
		rt_sirq_register(RT_SOFT_IRQ_GOOSE_SEND, _goose_send_soft_isr, "_goose_send_soft_isr");
		rt_sirq_register(RT_SOFT_IRQ_GOOSE_HANDLE, _goose_handle_soft_isr, "_goose_handle_soft_isr");

		g_goose_init_flag = 1;

		rt_printf("user_goose_init ok.\n");
	}
	else // sunxi 20191204
	{
		rt_printf("user_goose_init failed.\n");
		return -1;
	}
#endif // __IEC61850_GOOSE_FUNC__

	return 0;
}

int goose_exit(void)
{
#ifdef __IEC61850_GOOSE_FUNC__ // sunxi 20191015 goose功能在没有需求的情况下，启动不编译进模块里面。即goose功能的启动作为独立的模块
	if(g_goose_init_flag)
	{
		g_goose_init_flag = 0;

		rt_sirq_unregister(RT_SOFT_IRQ_GOOSE_HANDLE);
		rt_sirq_unregister(RT_SOFT_IRQ_GOOSE_SEND);
		rt_sirq_unregister(RT_SOFT_IRQ_GOOSE_RECV);

		app_goose_exit();
	}
#endif // __IEC61850_GOOSE_FUNC__

	rt_printf("user_goose_exit.\n");
	return 0;		
}

