#include "lan.h"
#include "tcpip.h"
#include "mytype.h"
#include "IEC92.h"
volatile unsigned int* LAN_MCS = (volatile unsigned int *) 0x2000;
volatile unsigned int* LAN_SBHE = (volatile unsigned int *) 0x2800;
volatile unsigned int* LAN_IOR = (volatile unsigned int *) 0x2980;
volatile unsigned int* LAN_IOW = (volatile unsigned int *) 0x2980;
unsigned int interrupt_request_flag;
unsigned int rxevent_data;
unsigned int txevent_data;
unsigned int bufevent_data;
unsigned int rxmiss_data;
unsigned int txcol_data;
unsigned int transmit_length;
unsigned int transmit_icmp_length;
unsigned int receive_frame_size;
#pragma DATA_SECTION(receive_buffer,"DataBuf");
unsigned short receive_buffer[SIZE_OF_FRAME];
unsigned int broadcase_buffer[SIZE_OF_FRAME];
unsigned int transmit_buffer[SIZE_OF_FRAME];
unsigned int transmit_icmp_buffer[SIZE_OF_FRAME];
//add by libenyin
unsigned int rx_udp_length = 0;
unsigned int rx_udp_data[200];
unsigned long verity_data;
unsigned long verity_transmit_data;
unsigned int icmp_data_length;
unsigned int individual_address[3] = {0x1234,0x5678,0x9ABC};
extern void asnshift(int * buffer,char * buffer2);
extern Uint32 rearrange(Uint32 data);
extern int rearrange16(int data);
extern UINT16 DM9000SendFrame(unsigned int *pBuffer, UINT32 length);
static void DataShift(Uint16 * buffer,Uint16 * buffer2);
//#define SMPRATE 256 //每个周期的采样点数
//#define SMP SMPRATE*50 //采样率
unsigned int receive_udp_flag;
UINT16 apdu[26]={0x3260,0x0180,0xa201,
0x302d,0x802b,0x780a,0x7878,0x4d78,0x6e55,0x306e,0x8231,
0x0d02,0x8320,0x0004,0x0000,0x8501,0x0001,0x1087,0x3412,
0x7856,0x0000,0x0000,0x4523,0x8967,0x0000,0x0000}; //apdu initial valuve
unsigned int apdutemp[100];
volatile int smpCnt=0; //sample counter
//extern int vSMP;
extern int vSMR;
//extern int sycW;
extern int IsDeg;
extern volatile int RATEDCURRENT;
extern volatile int IEC61850_APPID;
extern const float RATED_CURRENT_MEASURE_IEC61850;//IEC61850协议规定的测量的额定电流值
extern const float RATED_CURRENT_PROTECT_IEC61850;//IEC61850协议规定的保护的额定电流值
/***************************************************************************
funcname: void send_test( long * buffer,int length)
说明: 处理IEC61850-9-2协议 初始版本v0.10 通用编码程序,可测试多个输入:最多为九路信号
输入: buffer:需要传送的32位信号数组地址 length: 信号通道个数
输出: 无
****************************************************************************/
void send_test92(long * inputbuf,int length)
{
unsigned int* pt1;
unsigned int* pt2;
// unsigned int* apdusbase;
unsigned int i,temp;
unsigned int SystemStatus = 0;
// char tempbuff[4]={1,5,18,28};
Uint16 tempbuff[4]={1,5,19,28};//20:APDU除数据长度外的字节数除以2
long buffer1[16];
static long quality[16] = {0x00000000,0x00000001,0x00000001,0x00000001,0x00000000,0x00000001,0x00000001,0x00000001};
long * buffer;
long * qptr;
// long temp;
// tempbuff[3]=24+length*4;
tempbuff[3]=26+length*4;//9-2 by wky
//for(i = 0;i < length;i ++)
//{
// buffer1[i] = ((long)(inputbuf[i])) & 0x0000ffff;
//}
/*
buffer1[0] = (long)((float)(inputbuf[0]) / 0.000463); //保护电流
buffer1[1] = (long)inputbuf[1]; //标准电流
buffer1[2] = (long)((float)(inputbuf[4]) / 0.011585); //测量电流
buffer1[3] = (long)inputbuf[2];
buffer1[4] = (long)inputbuf[3];
buffer1[5] = (long)inputbuf[5];
buffer1[6] = (long)inputbuf[6];
buffer1[7] = (long)inputbuf[7];
*/
buffer1[0] = (long)(inputbuf[0]); //保护电流
// buffer1[0] = 1000;
buffer1[1] = (long)inputbuf[1]; //标准电流
buffer1[2] = (long)(inputbuf[2]);
buffer1[3] = (long)inputbuf[3];
buffer1[4] = (long)(inputbuf[4]); //测量电流以毫安的形式输出
buffer1[5] = (long)inputbuf[5];
buffer1[6] = (long)inputbuf[6];
buffer1[7] = (long)inputbuf[7];
/*
buffer1[0] = 0x12345678; //保护电流
buffer1[1] = 0x01020304; //标准电流
buffer1[2] = 0x23456789;
buffer1[3] = 0x01020304;
buffer1[4] = 0x12345678; //测量电流以毫安的形式输出
buffer1[5] = 0x01020304;
buffer1[6] = 0x23456789;
buffer1[7] = 0x01020304;
*/
/*
quality[0] = 0x00000000;
quality[1] = 0x00000001;
quality[2] = 0x00000001;
quality[3] = 0x00000001;
quality[4] = 0x00000000;
quality[5] = 0x00000001;
quality[6] = 0x00000001;
quality[7] = 0x00000001;
*/
SystemStatus = (unsigned int)inputbuf[2];
//同步丢失 apdus.smpSynch
apdus92.smpRate = vSMR;
if((SystemStatus & 0x2000) == 0x00)
{
apdus92.smpSynch = 0x01; //Have Sync Sig
}
else
{
apdus92.smpSynch = 0x00; //Have no Sync Sig
}
//保护数据无效
if((SystemStatus & 0x8000) == 0x0000 )
{
quality[0] = quality[0] & 0xFFFFFFFE;
}
else
{
quality[0] = quality[0] | 0x00000001;
}
//测量数据无效
if((SystemStatus & 0x0080) == 0x0000)
{
quality[4] = quality[4] & 0xFFFFFFFE;
}
else
{
quality[4] = quality[4] | 0x00000001;
}
//保护故障报警
if(((SystemStatus & 0x8000) == 0x0000) )
{
quality[0] = quality[0] & (~(0x00000001 << 6));//故障标记
}
else
{
quality[0] = quality[0] | 0x00000001 << 6;//故障标记
}
//测量故障报警
if((SystemStatus & 0x0080) == 0x0000 )
{
quality[4] = quality[4] & (~(0x00000001 << 6));//故障标记
}
else
{
quality[4] = quality[4] | 0x00000001 << 6;//故障标记
}
//超限标志
//测量超限
if((SystemStatus & 0x1000) == 0)
{
quality[4] = quality[4] & (~(0x00000001 << 3));
}
else
{
quality[4] = quality[4] | (0x00000001 << 3);
}
/*
// overflow flag
//Protect overflow flag
if((inputbuf[0] >= 32765) || (inputbuf[0] <= -32766))
{
quality[0] = quality[0] | (0x00000001 << 2);//
}
else
{
quality[0] = quality[0] & (~(0x00000001 << 2));
}
//Measure overflow flag
if((inputbuf[4] >= 32765) || (inputbuf[4] <= -32766))
{
quality[4] = quality[4] | (0x00000001 << 2);//
}
else
{
quality[4] = quality[4] & (~(0x00000001 << 2));
}
*/
//smpCnt++;
//if(smpCnt>SMP)
// smpCnt=0;
/*
quality[0] = 0xabcdef23;
quality[1] = 0xa0b1c2d3;
quality[2] = 0xa0b1c2d3;
quality[3] = 0xa0b1c2d3;
quality[4] = 0xa0b1c2d3;
quality[5] = 0xa0b1c2d3;
quality[6] = 0xa0b1c2d3;
quality[7] = 0xa0b1c2d3;
*/
buffer = buffer1;
qptr = quality;
apdus92.smpCntV= (Uint16)((inputbuf[15]>>8)&0xff)|((inputbuf[15]<<8)&0xff00);
for(i=0;i<length;i++)
{
apdus92.seDataV[2*i]=rearrange(*buffer++);
apdus92.seDataV[2*i+1]= rearrange(*qptr++);
}
apdus92.savPDUL=0x26+length*8;
apdus92.sASDUL=0x21+length*8;
apdus92.hsASDUL=0x1f+length*8;
apdus92.seDataL=length*8;
pt1=apdutemp;
pt2=(unsigned int*) &apdus92;
for(i=0;i<22+length*4;i++)
*pt1++=*pt2++;
// asnshift((int*)apdutemp,tempbuff);
DataShift((Uint16 *)apdutemp,tempbuff);
//transmit_length = 0;
//目的地址
transmit_buffer[0] = source_hardware_address[0];
transmit_buffer[1] = source_hardware_address[1];
transmit_buffer[2] = source_hardware_address[2];
//吹刂�
transmit_buffer[3] = local_hardware_address[0];
transmit_buffer[4] = local_hardware_address[1];
transmit_buffer[5] = local_hardware_address[2];
transmit_buffer[6] = 0x0081;
transmit_buffer[7] = 0x0080;
// transmit_buffer[8] = 0x0081;
// transmit_buffer[9] = 0xc980;
transmit_buffer[8] = 0xba88;
transmit_buffer[9] = rearrange16(IEC61850_APPID); //appid
temp=0x30+8*length; //原来为2c 多出来的可能为填充字
transmit_buffer[10]=((temp>>8)&0xff)|((temp<<8)&0xff00); //PDU length
transmit_buffer[11] = 0x0000;
transmit_buffer[12] = 0x0000;
//apdu[11]=(apdu[11]&0x00ff)|((smpCnt)&0xff00);
//apdu[12]=(apdu[12]&0xff00)|((smpCnt)&0x00ff);
pt1=transmit_buffer+13;
pt2=apdutemp;
for(i=0;i<20+4*length;i++) //20:除数据长度外APDU的长度
*pt1++=*pt2++;
transmit_length=13+20+4*length;//1