Project

General

Profile

Feature #562 » se104sPscada15.cpp

yufeng wu, 05/19/2020 11:17 AM

 
1

    
2
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3
//????Լ?Ǹ???104??д?ġ?
4
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
#if defined(WIN32)
6
#include    <io.h>
7
#include "stdafx.h"
8
#endif
9
#include    <limits.h>
10
#include    <sys/stat.h>
11
#include	<sys/timeb.h>
12
#include	<time.h>
13

    
14
#include    "se104sPscada15.h"
15
#include	"se_log.h"
16

    
17

    
18
void CSE104S::Init( S_PROTOCOLCFG * pcfg )
19
{
20
	PRawCtrl	=	pcfg->PRawCtrl;
21
	pRxBuf		=	pcfg->pRxBuf;
22
	pTxBuf		=	pcfg->pTxBuf;
23
	pCmdMem		=	pcfg->pCmdMem;
24
	pRtu		=	pcfg->pRtu;
25
	pLink		=	pcfg->pLink;
26
	pTable		=	pcfg->pTable;
27
	pZfDataCtrl	=	pcfg->pZfDataCtrl;
28

    
29
	sint32 buflen=pRxBuf->GetReadableSize();   
30
	pRxBuf->Move(buflen);
31
	buflen=pTxBuf->GetReadableSize();
32
	pTxBuf->Move(buflen);
33

    
34
	InitStation(); 
35
	SendFrameNo=0;
36
	SendKwhFrameNo=0;
37

    
38
	Timeout2=10;
39
	Timeout3=30;
40
	W_IFrame = 8;
41
	K_IFrame = 12;
42
	m_rtuQX=1;
43
	m_staNum=50;
44
	memset(m_zone,0,16+1);
45
}
46
/********************************************************************************
47
*
48
*	????: ???մ???????.
49
*	????: ??.
50
*	????: ??
51
********************************************************************************/
52

    
53
sint32 CSE104S::GetNowSecond()
54
{
55
	CSeTime     SE_T;
56
	TCriterionTime tmptime;
57
	SE_T.GetNow(&tmptime);
58
	return (sint32)tmptime;
59
}
60

    
61
sint32	CSE104S::RxProc()
62
{
63
	uint8  buf[256];
64
	sint32 buflen=pRxBuf->GetReadableSize();
65
	if (buflen<6)		//??????
66
		return 1;
67
	sint32 datalen=0;
68
	while((buflen=pRxBuf->GetReadableSize())>=6)
69
	{
70
		if(buflen>256)
71
			buflen=256;
72
		datalen = pRxBuf->Read(buf,buflen,DEF_BUFF_NOMOVE);
73
		if(buf[0]!= 0x68)	
74
		{
75
			sint32 unuse = 0;
76
			while(unuse<datalen)
77
			{
78
				if(buf[unuse] != 0x68)
79
					unuse ++;
80
				else
81
				{
82
					break;
83
				}
84
			}
85
			pRxBuf->Move(unuse);
86
			continue;
87
		}
88

    
89
		if((buf[1]+2)>datalen)
90
			return 0;
91

    
92
		if(buf[1]<4)
93
		{
94
			pRxBuf->Move(datalen);
95
			return 0;
96
		}
97

    
98
		LastRxTime = GetNowSecond();				//?յ?֡??ˢ?½??ռ?ʱ??
99
		if(buf[2]& 0x01)	//U or S format
100
		{
101
			App_RxFixFrame(buf,datalen);
102
			break;
103
		}
104
		else	//I format
105
		{
106
			App_RxVarFrame(buf,datalen);
107
			break;
108
		}
109

    
110
	}
111
	return 1;
112

    
113
}
114

    
115
BOOL CSE104S::ProcZfMail()
116
{
117
	sint32 rtuno=pLink->GetRtuNo();
118
	if(pCmdMem->GetResultNum(rtuno)<=0)
119
		return 0;
120

    
121
	S_RAWCMD rawcmd;
122
	S_DO_CTL cmdinfo;
123
	while(pCmdMem->GetResultNum(rtuno)>0)
124
	{
125
		if(pCmdMem->GetAResult(rtuno,&rawcmd)==0)
126
				break;
127
		if(rawcmd.type!=DC_K_CMDRET_DOOPER
128
				||rawcmd.len!=sizeof(cmdinfo))
129
		{
130
			pCmdMem->DelAResult(rtuno);
131
			continue;
132
		}
133
		memcpy(&cmdinfo,rawcmd.data,sizeof(S_DO_CTL));
134

    
135
		ProcYkResponse(cmdinfo);
136
		pCmdMem->DelAResult(rtuno);
137

    
138
	}
139

    
140
	return 0;
141
}
142

    
143
sint32	CSE104S::ProcYkResponse(S_DO_CTL& cmdinfo)
144
{
145
	BYTE* ptr = senddata.databuf ;
146
	senddata.headP = senddata.trailP = 0;
147
	sint32 rtuno=pLink->GetRtuNo();
148
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
149
	/*///////////////////////////////////////////*/
150
	uint8	staNum=m_staNum;//20 Added by WYF20180601for????Ȩ??NPSCADA0-0??CIOS0-1??COCC1-0
151
	uint8	b1,b2;//b1-1occ,0C3;b2-1NPSCADA,0CIOS
152
	float32	z=0;
153
	uint16 zone=0,staZone=0,nTmp=1,i=0;
154
	sint32 n=rtuno%staNum;//վ??
155
	PRawCtrl->GetAYc(m_rtuQX,staNum+1,&z);//??λ????
156
	zone=(uint16)z;
157
	if(n>0 && n<staNum){
158
		PRawCtrl->GetAYx(m_rtuQX,n+staNum,&b1);	//b1??C3=0;StaOCC=1
159
		PRawCtrl->GetAYx(m_rtuQX,n,&b2);//b2??C3-CIOS=0;C3-NPSCADA=1
160
		staZone=0;
161
		for(i=0;i<17-1;i++)
162
		{
163
			if(n>m_zone[i] && n<=m_zone[i+1] ){
164
				staZone=1<<i;//??ǰ??վʼ???ĸ???λ
165
				break;
166
			}
167
		}
168
		if(zone>0 && (zone&staZone))//ѡ???󣬵?վ??ѡ??Ч
169
		{
170
			if(rtuno>3*staNum && rtuno<4*staNum)b1==0;b2==0;//C3-CIOS
171
			if(rtuno>2*staNum && rtuno<3*staNum)b1==0;b2==1;//C3-NPSCADA
172
			if(rtuno>staNum && rtuno<2*staNum)b1==1;//StaOCC
173
		}
174
	}
175
	/*///////////////////////////////////////////*/
176

    
177
	DC_D_DATAUNITID DataHead;
178
	DataHead.TypeID =C_DC_NA_1;
179
	DataHead.VSQ.SQ = 0;
180
	DataHead.VSQ.Num = 1;
181
	DataHead.CommonAddrLo = (rtuaddr)%256;
182
	DataHead.CommonAddrHi = (rtuaddr)/256;
183
	if(cmdinfo.retCode==1)//???ң?ط?У??ȷ//Added by WYF20180601for????Ȩ??NPSCADA??CIOS??COCC
184
	{
185
		DataHead.Cause =APP_COT_ACT_CON;
186
		/*///////////////////////////////////////////*/
187
		if((rtuno>3*staNum && rtuno<4*staNum && b1==0 && b2==0) || (rtuno>2*staNum && rtuno<3*staNum && b1==0 && b2==1) || (rtuno>staNum && rtuno<2*staNum && b1==1))
188
			DataHead.Cause =APP_COT_ACT_CON;
189
		else{
190
			DataHead.Cause = APP_COT_ACT_CON | 0x40;
191
			PrintLog(LOG_INFORMATION,">>>>> RTU%dȨ??ֵocc%d:Npscada%d?޿???Ȩ?ޣ?",rtuno,b1,b2);
192
		}
193
		/*///////////////////////////////////////////*/
194
	}else								//???ң?ط?У????
195
		DataHead.Cause = APP_COT_ACT_CON | 0x40;	//??ȷ??
196
	DataHead.SrcAddr = 0;
197

    
198
	*ptr++ = 0x68;
199
	*ptr++ = 4+DC_D_HEADLEN+4;
200
	*ptr++ = (SendSeqNo<<1)%256;
201
	*ptr++ = (SendSeqNo<<1)/256;
202
	*ptr++ = (RecvSeqNo<<1)%256;
203
	*ptr++ = (RecvSeqNo<<1)/256;
204
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
205
	ptr += sizeof(DC_D_DATAUNITID);
206
	*ptr++ = (cmdinfo.ptAddr+START_YK)%256;
207
	*ptr++ = (cmdinfo.ptAddr+START_YK)/256;
208
	*ptr++ = 0;
209

    
210
	uint8 DCO = 0;
211
	if(cmdinfo.ctlVal == 0)
212
		DCO |= 0x01;
213
	else if(cmdinfo.ctlVal == 1)
214
		DCO |= 0x02;
215

    
216
	if(cmdinfo.funcCode == DC_K_CTL_DOSEL)
217
		DCO |= 0x80;
218

    
219
	*ptr++ = DCO;
220
	senddata.trailP = ptr - senddata.databuf ;
221

    
222
	PrintLog(LOG_INFORMATION,">>>>> ????ң?ط?????Ϣ");
223
	AppToLink();
224

    
225
	SendSeqNo ++;		
226
	SendSeqNo %= 0x8000;		//Snd_seqnum Ϊ15λ??????????
227

    
228
	return 1;
229

    
230

    
231
}
232

    
233

    
234
/********************************************************************************
235
*
236
*	????: ????????????֡
237
*	????: ??.
238
*	????: ??.
239
*
240
********************************************************************************/
241
void CSE104S::App_RxFixFrame(uint8 *buf,sint32 buflen)
242
{
243
	if(buf[1]!= 4 )
244
	{
245
		pRxBuf->Move(1);
246
		return;
247
	}
248
	
249
	pLink->RegisterFrm( FRAME_RX_SUC );
250
	if( buf[2] & 0x02 )		//U format
251
	{	
252
		BYTE type = buf[2] & 0xfc;	
253
				
254
		switch( type )
255
		{
256
		case APP_UFORMAT_STARTACT:
257
			InitStation();     	//???³?ʼ??
258
			AppLayerStat= IEC104_APP_STATE_IDLE;
259
			PrintLog(LOG_INFORMATION,"?յ?????ִ??֡");
260
			App_SendAppUFormat( APP_UFORMAT_STARTCON);
261
			PrintLog(LOG_INFORMATION,"????????ȷ??֡");
262
			break;
263
		case APP_UFORMAT_STOPACT:
264
			InitStation();      	//???³?ʼ??
265
			PrintLog(LOG_INFORMATION,"?յ?ִֹͣ??֡");
266
			App_SendAppUFormat( APP_UFORMAT_STOPCON);
267
			PrintLog(LOG_INFORMATION,"????ֹͣȷ??֡");
268
			break;
269
		case APP_UFORMAT_TESTACT:
270
			PrintLog(LOG_INFORMATION,"?յ?????ִ??֡");
271
			if(AppLayerStat != IEC104_APP_STATE_UNRESET)//<xyanglee>û??STARTDT????????????
272
			{
273
				App_SendAppUFormat( APP_UFORMAT_TESTCON);
274
				PrintLog(LOG_INFORMATION,"???Ͳ???ȷ??֡");
275
			}
276
			break;
277
		case APP_UFORMAT_TESTCON:
278
			PrintLog(LOG_INFORMATION,"?յ?????ȷ??֡");
279
			break;
280
		}
281
	}
282
	else 		//S format  
283
	{	
284
		ConSeqNo = buf[4]/2 + buf[5]*128;
285
		PrintLog(LOG_INFORMATION,"?յ?S֡,???ȷ??֡ %d ???ͺ?:%d",ConSeqNo,SendSeqNo);
286
	}
287
	pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)buf,6);
288
	pRxBuf->Move(6);
289
}
290

    
291
/********************************************************************************
292
*
293
*	????: ?????䳤??Ϣ֡
294
*	????: apdu  ָ??Ӧ??Э?????ݵ?Ԫ(APDU) ??ָ??.
295
*	      size  Ӧ??Э?????ݵ?Ԫ(APDU) ?ij???
296
*	????: ??.
297
*
298
********************************************************************************/
299
sint32 CSE104S::App_RxVarFrame(uint8 *buf,sint32 buflen)
300
{
301
	sint32 DataLen=buf[1];
302

    
303
	if(AppLayerStat == IEC104_APP_STATE_UNRESET)//<xyanglee>û??STARTDT????????????
304
	{
305
		PrintLog(LOG_ERROR,"?ͻ???û?з??????????????????!");
306
		pRxBuf->Move(DataLen+2);
307
		return 1;
308
	}
309

    
310
	sint32 Seqnum = buf[2]/2+buf[3]*128;	//У????վ??Snd_seqnum
311
	uint8 initflag=0;
312
	//ÿ????һI֡????ReceiveSeqNo??1???÷???ȷ?ϱ?־SendACKFlag??	
313
	RecvSeqNo ++;						//??ŵ??ǵ?ǰ?????к?
314
	RecvSeqNo =RecvSeqNo%0x8000;		//ReceiveSeqNo Ϊ15λ??????????
315
	if( Seqnum >RecvSeqNo )	//??վ??I format ???ֶ?ʧ
316
	{
317
		RecvSeqNo=Seqnum;
318
		PrintLog(LOG_ERROR,"???ֶ?֡????  Ӧ???յ?֡??= %d, ʵ???յ?֡??= %d\n",RecvSeqNo, Seqnum);
319
		LostNum++;
320
		if(LostNum>6)
321
			initflag=1;
322
	}
323
	else 
324
		LostNum=0;
325
	
326
	//?Է?ȷ?????к?
327
	ConSeqNo = buf[4]/2 + buf[5]*128;
328

    
329
	memcpy(recvdata.databuf,&buf[6],DataLen-4);
330

    
331
	recvdata.trailP = DataLen -4;
332
	recvdata.headP = 0;
333

    
334
	switch(buf[6])
335
	{
336
	case C_IC_NA_1:		//????
337
		Proc_AllData();
338
		break;
339
	case C_CI_NA_1:		//??????????
340
		Proc_AllKwh();
341
		break;
342
	case C_RD_NA_1:		//??????
343
		break;
344
	case C_CS_NA_1:		//ʱ??ͬ??
345
		Proc_SyncTime();
346
		break;
347
	case C_RP_NA_1:		//??λ????????
348
		Proc_RstCmd();
349
		break;
350
	case C_SC_NA_1:		//????????
351
	case C_DC_NA_1:		//˫??????
352
		Proc_YkCmd();
353
		break;
354
	case C_RC_NA_1:		//??????????
355
		break;
356
	}
357

    
358
	pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)buf,DataLen+2);
359
	pRxBuf->Move(DataLen+2);
360
	SendACKFlag ++;
361
	if(initflag)
362
	{
363
		InitStation();   	//???³?ʼ??
364
	}
365
	
366

    
367
	return 1;
368
}
369

    
370
sint32 CSE104S::Proc_AllData()
371
{
372
	uint8 * ptr = recvdata.databuf+recvdata.headP;
373
	uint8 COT = ptr[2];	//cause
374
//	uint8 QOI = ptr[DC_D_HEADLEN+2+];
375
	uint8 QOI = ptr[DC_D_HEADLEN+DC_D_INFOADDRLEN];//WYF 20180725+DC_D_INFOADDRLEN
376
	uint8 groupno = QOI-COT_CALLALLDATA;
377

    
378
	if(COT == APP_COT_DEACT)// ֹͣ????
379
	{
380
		if((m_groupNo & 0x1F) == groupno)
381
		{
382
			App_AllData_Conf(APP_COT_DEACT_CON,QOI);
383

    
384
			m_flagCallReply &= 0xF8;
385
			m_groupNo &= 0xE0;
386
			SendFrameNo = 0;
387

    
388
			return 1;
389
		}
390
	}
391

    
392

    
393
	if(COT != APP_COT_ACT)//cause
394
		return 0;
395

    
396
	if(m_flagCallReply & 0x07)	//?ϴ??ٻ?û?н???
397
	{
398
		App_AllData_Conf(APP_COT_ACT | 0x40, QOI);	//?񶨻ش?
399
		return 1;
400
	}
401

    
402

    
403
	if(groupno>=1 && m_groupNo<=8)//YX
404
	{
405
		m_flagCallReply |= 0x05;
406
		m_groupNo |= groupno;
407
		SendFrameNo = 0;
408
	}
409
	else if(groupno>=9 && groupno<=14)//YC
410
	{
411
		m_flagCallReply |= 0x06;
412
		m_groupNo |= groupno;
413
		SendFrameNo = 0;
414
	}
415
	else if(groupno == 15)	//??λ??
416
	{
417
	}
418
	else if(groupno == 16)	//Զ???ն?״̬
419
	{
420

    
421
	}
422
	else if(groupno == 0)
423
	{
424
		m_flagCallReply |= 0x07;
425
		m_groupNo &= 0xE0;
426
		SendFrameNo = 0;
427
	}
428
	else
429
	{
430
		return 0;
431
	}
432

    
433

    
434
	App_AllData_Conf(APP_COT_ACT_CON,QOI);
435

    
436
	return 1;
437
}
438

    
439
sint32 CSE104S::Proc_AllKwh()
440
{
441

    
442
	uint8 * ptr = recvdata.databuf+recvdata.headP;
443

    
444
	uint8 COT = ptr[2];
445
	uint8 QCC = ptr[DC_D_HEADLEN+2];
446
	uint8 FRZ = (QCC & 0xC0)>>6;
447
	uint8 calltype = QCC & 0x3F;
448

    
449
	if(calltype<1 || calltype>5)
450
		return 0;
451

    
452
	if(COT == APP_COT_DEACT)
453
	{
454
		uint8 nowgroup = (m_groupNo & 0xE0)>>5;
455
		if(nowgroup ==0)
456
			nowgroup = 5;
457

    
458
		if(nowgroup == calltype)
459
		{
460
			App_AllKwh_Conf(APP_COT_DEACT_CON,QCC);
461
			m_flagCallReply &= 0xCF;
462
			m_groupNo &= 0x1F;
463
			SendKwhFrameNo = 0;
464

    
465
			return 1;
466
		}
467
		else
468
			return 0;
469
	}
470

    
471
	if(COT != APP_COT_ACT)
472
		return 0;
473

    
474
	if(m_flagCallReply & 0x30)	//?ϴ??ٻ?û?н???
475
	{
476
		App_AllKwh_Conf(APP_COT_ACT | 0x40, QCC);	//?񶨻ش?
477
		return 1;
478
	}
479

    
480

    
481
	if(FRZ == 1)	//ģʽC???ȶ???
482
	{
483
		//?˴?Ӧ?????ۼ???:???ȫ??
484

    
485
		App_AllKwh_Conf(APP_COT_ACT_CON,QCC);
486
		return 1;
487
	}
488

    
489
	if(FRZ == 0)	//ֱ?Ӷ?
490
	{
491
		if(QCC>0 && QCC<QCC_CALLALLKWH)
492
		{
493
			App_AllKwh_Conf(APP_COT_ACT_CON,QCC);
494
			m_groupNo |= (calltype<<5);
495
			m_flagCallReply |= 0x30;
496
			SendKwhFrameNo = 0;
497

    
498
			return 1;
499
		}
500
		else if(QCC == QCC_CALLALLKWH)
501
		{
502
			App_AllKwh_Conf(APP_COT_ACT_CON);
503
			m_flagCallReply |= 0x30;
504
			SendKwhFrameNo = 0;
505

    
506
			return 1;
507
		}
508
		else
509
		{
510
			return 0;
511
		}
512
	}
513
	
514

    
515
	return 0;
516
}
517

    
518
sint32 CSE104S::Proc_SyncTime()
519
{
520
	BYTE * Buff=recvdata.databuf ;
521
	CP56Time2a tm104,localtm104;
522

    
523
	//ȡ?õ?ǰʱ??
524
	SSE_CLOCK seClock;
525
	CSeTime     SE_T;
526
	SE_T.GetNow(&seClock);
527
	localtm104.YearMonth.Year = seClock.year - 2000 ;
528
	localtm104.YearMonth.Months = seClock.month;
529
	localtm104.DaysWeeks.Days = seClock.day;
530
	localtm104.Hours = seClock.hour;
531
	localtm104.Minutes = seClock.minute;
532
	localtm104.MilliSecHi = (seClock.second * 1000 + seClock.msecond)/256;
533
	localtm104.MilliSecLo = (seClock.second * 1000 + seClock.msecond)%256;
534

    
535

    
536
	//???ö?ʱ
537
	memcpy(&tm104,&recvdata.databuf[recvdata.headP+DC_D_HEADLEN+DC_D_INFOADDRLEN],sizeof(CP56Time2a));
538

    
539
	//<xyanglee>
540
	//tm104:Year ??ǰ?? - 2000
541
	//struct tm: tm_year: current year minus 1900
542
	//			 tm_mon: 0 -- 11
543

    
544
#ifdef WIN32
545
	SYSTEMTIME systemtm;
546
	systemtm.wYear = tm104.YearMonth.Year + 2000;
547
	systemtm.wMonth = tm104.YearMonth.Months;
548
	systemtm.wDay = tm104.DaysWeeks.Days;
549
	systemtm.wHour = tm104.Hours;
550
	systemtm.wMinute = tm104.Minutes;
551
	systemtm.wSecond = (tm104.MilliSecHi *256 +tm104.MilliSecLo)/1000;
552
	systemtm.wMilliseconds = (tm104.MilliSecHi *256 +tm104.MilliSecLo)%1000;
553

    
554
	SetLocalTime(&systemtm);
555
#else
556
	struct timeval tmval;
557
	TCriterionTime inttime;
558
	seClock.year = tm104.YearMonth.Year + 2000;
559
	seClock.month = tm104.YearMonth.Months;
560
	seClock.day = tm104.DaysWeeks.Days;
561
	seClock.hour = tm104.Hours;
562
	seClock.minute  =tm104.Minutes;
563
	seClock.second = (tm104.MilliSecHi *256 +tm104.MilliSecLo)/1000;
564
	seClock.msecond = (tm104.MilliSecHi *256 +tm104.MilliSecLo)%1000;
565
	
566
//	SE_T.ConvertTime(&seClock, & inttime);
567
	SE_T.CovertTime(inttime, &seClock);
568
	tmval.tv_sec = inttime;
569
	tmval.tv_usec = ((tm104.MilliSecHi *256 +tm104.MilliSecLo)%1000) * 1000;
570
	settimeofday(&tmval,NULL);
571
#endif
572

    
573
	App_Ack_SyncTime(&localtm104);
574

    
575
	return 1;
576
}
577

    
578
sint32 CSE104S::Proc_YkCmd()
579
{
580
	sint32 rtuno=pLink->GetRtuNo();
581

    
582
	uint8 * Buff=recvdata.databuf;
583

    
584
	DC_D_DATAUNITID DataHead;
585
	memcpy((BYTE *)&DataHead,Buff,DC_D_HEADLEN);
586
	
587
	Buff += DC_D_HEADLEN;
588
	sint32 YkNo;
589
	YkNo=Buff[0]+Buff[1]*256-START_YK;
590

    
591
	//??????ֱ??ѹ??????ң?ض???
592
	S_DO_CTL cmdinfo;
593
	S_RAWCMD  rawcmd;
594
	
595
	rawcmd.type=DC_K_CMD_DOOPER;
596
	rawcmd.origTime = 0;		//Temp
597
	rawcmd.len=sizeof(S_DO_CTL);
598

    
599
	memset(&(rawcmd.src),0,sizeof(ST_TRACK_ADDR));
600
	rawcmd.src.route[0] = rtuno;
601

    
602
	cmdinfo.ptAddr=YkNo;
603
	if(DataHead.Cause == APP_COT_ACT)
604
	{
605
		if(Buff[3] & 0x80)
606
			cmdinfo.funcCode = DC_K_CTL_DOSEL;
607
		else
608
			cmdinfo.funcCode = DC_K_CTL_DOEXE;
609
	}
610
	else if(DataHead.Cause == APP_COT_DEACT)
611
		cmdinfo.funcCode = DC_K_CTL_DODEL;
612

    
613
	if(DataHead.TypeID == C_DC_NA_1)
614
	{
615
		if((Buff[3] & 0x03) == 1)
616
			cmdinfo.ctlVal = 0;
617
		else if((Buff[3] & 0x03) == 2)
618
			cmdinfo.ctlVal = 1;
619
		else 
620
		{
621
			App_Ack_Yk(YkNo,APP_COT_ACT_CON | 0x40 ,Buff[3]);
622
			return 0;	//<xyanglee>?Ƿ?Ӧ?÷??ش?????Ϣ?
623
		}
624
	}
625
	else//C_SC_NA_1
626
	{
627
		if((Buff[3] & 0x01) == 1)
628
			cmdinfo.ctlVal = 1;
629
		else
630
			cmdinfo.ctlVal = 0;
631
	}
632

    
633
	if(cmdinfo.funcCode == DC_K_CTL_DODEL)
634
		App_Ack_Yk(YkNo,APP_COT_DEACT_CON,Buff[3]);
635
	if(cmdinfo.funcCode == DC_K_CTL_DOEXE)
636
		App_Ack_Yk(YkNo,APP_COT_ACT_CON,Buff[3]);
637

    
638

    
639
	memcpy(rawcmd.data,&cmdinfo,sizeof(S_DO_CTL));
640
	pCmdMem->RptAZfCmd(rtuno,rawcmd);
641

    
642

    
643
	return 1;
644
}
645

    
646
sint32 CSE104S::Proc_RstCmd()
647
{
648
	uint8 * Buff=recvdata.databuf;
649

    
650
	DC_D_DATAUNITID DataHead;
651
	memcpy((BYTE *)&DataHead,Buff,DC_D_HEADLEN);
652
	
653
	Buff += DC_D_HEADLEN;
654

    
655
	uint8 QRP = Buff[3];
656

    
657
	if(DataHead.Cause != APP_COT_ACT)
658
		return 0;
659

    
660
	App_Ack_Rst(QRP);
661

    
662
	if(QRP == 1)	//?ܸ?λ
663
	{
664
		//Ӧ???????ر?????
665
	}
666
	else if(QRP == 2)	//??λ?¼???????
667
	{
668

    
669
	}
670

    
671

    
672
	return 1;
673
}
674

    
675
//??Լ???͹???
676
sint32 CSE104S::TxProc()
677
{
678
	if(ProcZfMail())
679
		return 1;
680

    
681
	if(AppLayerStat == IEC104_APP_STATE_IDLE && (SendSeqNo - ConSeqNo)<K_IFrame)
682
	{
683
		if(App_Ack_ChangeYx())
684
			return 1;
685
		
686
		if(App_Ack_Soe())
687
			return 1;
688

    
689

    
690
		if(m_flagCallReply)
691
		{
692
			if(App_ReplyData())
693
				return 1;
694
		}
695

    
696
		if(App_Ack_ChangeYc())
697
			return 1;
698
	}
699
	
700
	if( SendACKFlag )				//Send S format
701
	{
702
		if( App_SendAppSFormat() == 1 )
703
		{
704
			PrintLog(LOG_INFORMATION,">>>>> ????S֡  ֡??:%d ",RecvSeqNo);
705
		}
706
	}
707
	return 1;
708
}
709

    
710
//?ش??????ٻ?
711
sint32 CSE104S::App_ReplyData()
712
{
713
	sint8 datagroup=m_groupNo & 0x1F;
714
	sint8 accgroup =(m_groupNo & 0xE0)>>5;
715

    
716
	if(m_flagCallReply & 0x01)
717
	{
718
		if(datagroup == 0)
719
		{
720
			if(App_Ack_AllYx())
721
				return 1;
722
		}
723
		else
724
		{
725
		}
726
	}
727

    
728
	if(m_flagCallReply & 0x02)
729
	{
730
		if(datagroup == 0)
731
		{
732
			if(App_Ack_AllYc())
733
				return 1;
734
		}
735
		else
736
		{
737
		}
738
	}
739

    
740
	if(m_flagCallReply & 0x04)
741
	{
742
		if(App_AllData_Conf(APP_COT_ACT_TERM,COT_CALLALLDATA+datagroup))
743
			return 1; 
744
	}
745

    
746
	if(m_flagCallReply & 0x10)
747
	{
748
		if(accgroup == 0)
749
		{
750
			if(App_Ack_Kwh())
751
				return 1;
752
		}
753
		else
754
		{
755
		}
756
	}
757

    
758
	if(m_flagCallReply & 0x20)
759
	{
760
		uint8 QCC = accgroup;
761
		if(QCC == 0)
762
			QCC = QCC_CALLALLKWH;
763
		if(App_AllKwh_Conf(APP_COT_ACT_TERM,QCC))
764
			return 1;
765
	}
766

    
767
	return 0;
768
}
769

    
770
/*******************************************************************************
771
*
772
*	??????????ң????Ӧ֡
773
*	????????
774
*	???أ?1				
775
*
776
********************************************************************************/
777
sint32 CSE104S::App_Ack_Yk(sint32 ykno,uint8 cause,uint8 dco)
778
{
779
		sint32 rtuno=pLink->GetRtuNo();
780
		unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
781

    
782
		BYTE* ptr = senddata.databuf ;
783
		senddata.headP = senddata.trailP = 0;
784
		DC_D_DATAUNITID DataHead;
785
		DataHead.TypeID =C_DC_NA_1;
786
		DataHead.VSQ.SQ = 0;
787
		DataHead.VSQ.Num =1;
788
		DataHead.CommonAddrLo = rtuaddr%256;
789
		DataHead.CommonAddrHi = rtuaddr/256;
790
		DataHead.Cause =cause;
791
		DataHead.SrcAddr = 0;
792

    
793

    
794
		*ptr++ = 0x68;
795
		*ptr++ = 4+DC_D_HEADLEN+4;
796
		*ptr++ = (SendSeqNo<<1)%256;
797
		*ptr++ = (SendSeqNo<<1)/256;
798
		*ptr++ = (RecvSeqNo<<1)%256;
799
		*ptr++ = (RecvSeqNo<<1)/256;
800
		memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
801
		ptr += DC_D_HEADLEN;
802
		*ptr++ = (ykno+START_YK )%256; // ң?غŴ?1??ʼ??
803
		*ptr++ = (ykno+START_YK )/256; // ң?غŴ?1??ʼ??
804
		*ptr++ = 0;
805
		*ptr++ = dco;
806

    
807
		senddata.trailP = ptr - senddata.databuf ;
808

    
809
		PrintLog(LOG_INFORMATION,"????ң?ط?У??Ӧ֡");
810
		AppToLink();
811

    
812
		SendSeqNo ++;		
813
		SendSeqNo %= 0x8000;		//Snd_seqnum Ϊ15λ??????????
814

    
815

    
816
	return 1;
817
}
818

    
819

    
820
/*******************************************************************************
821
*
822
*	??????????ͬ??ʱ????Ӧ֡
823
*	????????
824
*	???أ?1	???ͳɹ?
825
*	      0 ????ʧ??			
826
*
827
********************************************************************************/
828
sint32 CSE104S::App_Ack_SyncTime(CP56Time2a * ptime)
829
{
830
	BYTE* ptr = senddata.databuf ;
831
	senddata.headP = senddata.trailP = 0;
832
	sint32 rtuno=pLink->GetRtuNo();
833
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
834

    
835
	DC_D_DATAUNITID DataHead;
836
	DataHead.TypeID =C_CS_NA_1;
837
	DataHead.VSQ.SQ = 0;
838
	DataHead.VSQ.Num = 1;
839
	DataHead.CommonAddrLo = (rtuaddr)%256;
840
	DataHead.CommonAddrHi = (rtuaddr)/256;
841
	DataHead.Cause  = APP_COT_ACT_CON;
842
	DataHead.SrcAddr = 0;
843

    
844
	*ptr++ = 0x68;
845
	*ptr++ = 4+DC_D_HEADLEN+DC_D_INFOADDRLEN+sizeof(CP56Time2a);
846
	*ptr++ = (SendSeqNo<<1)%256;
847
	*ptr++ = (SendSeqNo<<1)/256;
848
	*ptr++ = (RecvSeqNo<<1)%256;
849
	*ptr++ = (RecvSeqNo<<1)/256;
850
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
851
	ptr += sizeof(DC_D_DATAUNITID);
852
	*ptr++ = 0;
853
	*ptr++ = 0;
854
	*ptr++ = 0;
855

    
856
	memcpy(ptr,ptime,sizeof(CP56Time2a));
857

    
858
	ptr += sizeof(CP56Time2a);
859

    
860
	senddata.trailP = ptr - senddata.databuf ;
861

    
862
	PrintLog(LOG_INFORMATION,">>>>> ????ʱ??ͬ????Ӧȷ??֡");
863
	AppToLink();
864

    
865
	SendSeqNo ++;		
866
	SendSeqNo %= 0x8000;		//Snd_seqnum Ϊ15λ??????????
867
	return 1;
868
}
869

    
870
/*******************************************************************************
871
*
872
*	?????????͸?λ??Ӧ֡
873
*	????????
874
*	???أ?1	???ͳɹ?
875
*	      0 ????ʧ??				
876
*
877
********************************************************************************/
878
sint32 CSE104S::App_Ack_Rst(uint8 QRP )
879
{
880
	BYTE* ptr = senddata.databuf ;
881
	senddata.headP = senddata.trailP = 0;
882
	sint32 rtuno=pLink->GetRtuNo();
883
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
884

    
885
	DC_D_DATAUNITID DataHead;
886

    
887
	DataHead.TypeID=C_RP_NA_1;
888
	DataHead.VSQ.SQ = 0;
889
	DataHead.VSQ.Num = 1;
890
	DataHead.CommonAddrLo = (rtuaddr)%256;
891
	DataHead.CommonAddrHi = (rtuaddr)/256;
892
	DataHead.Cause  = APP_COT_ACT_CON;
893
	DataHead.SrcAddr = 0;
894

    
895
	*ptr++ = 0x68;
896
	*ptr++ = 4+DC_D_HEADLEN+4;
897
	*ptr++ = (SendSeqNo<<1)%256;
898
	*ptr++ = (SendSeqNo<<1)/256;
899
	*ptr++ = (RecvSeqNo<<1)%256;
900
	*ptr++ = (RecvSeqNo<<1)/256;
901
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
902
	ptr += DC_D_HEADLEN;
903
	*ptr++ = 0;
904
	*ptr++ = 0;
905
	*ptr++ = 0;
906
	*ptr++ = QRP;//?޶???
907

    
908
	senddata.trailP = ptr - senddata.databuf ;
909
	PrintLog(LOG_INFORMATION,"???͸?λ??????Ӧ֡");
910
	AppToLink();
911

    
912

    
913
	SendSeqNo ++;		
914
	SendSeqNo %= 0x8000;		//Snd_seqnum Ϊ15λ??????????
915
	return 1;
916
}
917

    
918
/********************************************************************************
919
*
920
*	??????????ȫ?????Ӧȷ??֡
921
*	????????
922
*	???أ? 0  ????ʧ??
923
*	       1  ???ͳɹ?
924
*
925
*********************************************************************************/
926
sint32 CSE104S::App_AllKwh_Conf( uint8 cause,uint8 QCC)
927
{
928
	BYTE* ptr = senddata.databuf ;
929
	senddata.headP = senddata.trailP = 0;
930
	sint32 rtuno=pLink->GetRtuNo();
931
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
932

    
933
	DC_D_DATAUNITID DataHead;
934
	DataHead.TypeID =C_CI_NA_1;
935
	DataHead.VSQ.SQ = 0;
936
	DataHead.VSQ.Num = 1;
937
	DataHead.CommonAddrLo = (rtuaddr)%256;
938
	DataHead.CommonAddrHi = (rtuaddr)/256;
939
	DataHead.Cause  = cause;
940
	DataHead.SrcAddr = 0;
941
	*ptr++ = 0x68;
942
	*ptr++ = 4+DC_D_HEADLEN+4;
943
	*ptr++ = (SendSeqNo<<1)%256;
944
	*ptr++ = (SendSeqNo<<1)/256;
945
	*ptr++ = (RecvSeqNo<<1)%256;
946
	*ptr++ = (RecvSeqNo<<1)/256;
947
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
948
	ptr += DC_D_HEADLEN;
949
	*ptr++ = 0;
950
	*ptr++ = 0;
951
	*ptr++ = 0;
952
	*ptr++ = QCC;//?ٻ??޶???
953

    
954
	senddata.trailP = ptr - senddata.databuf ;
955

    
956
	PrintLog(LOG_INFORMATION,"???͵????Ӧ֡");
957
	AppToLink();
958

    
959
	if(cause == APP_COT_ACT_TERM)
960
	{
961
		m_flagCallReply &= 0xDF;
962
		m_groupNo &= 0x1F;
963
	}
964

    
965
	SendSeqNo ++;		
966
	SendSeqNo %= 0x8000;		//Snd_seqnum Ϊ15λ??????????
967
	return 1;
968
}
969

    
970
/********************************************************************************
971
*
972
*	??????????ȫ?????Ӧ????֡
973
*	????????
974
*	???أ? 0  ????δ?????????е??ֵ??????
975
*	       1  ???ͽ???
976
*
977
*********************************************************************************/
978
sint32 CSE104S::App_Ack_Kwh( )
979
{
980
	sint32 rtuno=pLink->GetRtuNo();
981
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
982
	sint32 numperframe,startno,sendnum,framelen=0;
983
	CEasyList *kwhlist=pZfDataCtrl->GetDdStructList();
984

    
985
	numperframe=(FRAME_MAXLEN-6-DC_D_HEADLEN-DC_D_INFOADDRLEN)/5;//ÿ֡?Ϊ255????ȥ6???ֽڵ?APCI,?ټ?ȥhlen???????ֽڵ?ң????ʼ??
986
//	numperframe = 31;
987

    
988
	startno=SendKwhFrameNo*numperframe;
989
	sint32 numKwh = kwhlist->GetListLen();
990
	S_RDB_ZFDd *kwhinfo;
991
	kwhinfo=(S_RDB_ZFDd *)((*kwhlist)[numKwh-1]);
992
	if(startno>kwhinfo->DdNo)
993
	{
994
		m_flagCallReply &= 0xEF;
995
		SendKwhFrameNo = 0;
996
		return 0;
997
	}
998

    
999
	sendnum=kwhinfo->DdNo - startno +1;
1000
	if(sendnum > numperframe)
1001
		sendnum = numperframe;
1002

    
1003

    
1004
	uint8 buf[256]={0};
1005

    
1006
	sint32 kwhval;
1007
	sint16 lsval,hsval;
1008
	sint32 i;
1009
	sint32 index=0;
1010
	sint32 order = 0;
1011
	for(i=0;i<numKwh ;i++)
1012
	{
1013
		kwhinfo=(S_RDB_ZFDd *)((*kwhlist)[i]);
1014
		if(kwhinfo->DdNo >= (sendnum + startno))
1015
			break;
1016

    
1017
		if(kwhinfo->DdNo < startno)
1018
			continue;
1019

    
1020

    
1021
		//ֵ
1022
		kwhval=kwhinfo->Val;
1023
		lsval=kwhval%(256*256);
1024
		hsval=kwhval/(256*256);
1025
		index = (kwhinfo->DdNo - startno)*5;
1026
		buf[index]=lsval%256; 
1027
		buf[index+1]=lsval/256; 
1028
		buf[index+2]=hsval%256; 
1029
		buf[index+3]=hsval/256; 
1030

    
1031
		buf[index+4] = order++;	//<xyanglee>temp ??????
1032

    
1033
		kwhinfo->OldVal=kwhinfo->Val;
1034
	}
1035
	
1036
	if(i>=numKwh)
1037
	{
1038
		m_flagCallReply &= 0xEF;
1039
		SendKwhFrameNo = 0;
1040
	}
1041

    
1042
	DC_D_DATAUNITID DataHead;
1043
	DataHead.TypeID = 0x0F;
1044
	DataHead.VSQ.SQ=1;
1045
	DataHead.VSQ.Num=sendnum;
1046
	DataHead.CommonAddrLo = (rtuaddr)%256;
1047
	DataHead.CommonAddrLo = (rtuaddr)/256;
1048
	DataHead.Cause  = COT_CALLALLKWH;	//??Ӧվ???ٻ?
1049
	DataHead.SrcAddr = 0;
1050
	framelen = DataHead.VSQ.Num * 5 + 4 + DC_D_HEADLEN +DC_D_INFOADDRLEN;
1051

    
1052
	BYTE *ptr = senddata.databuf ;
1053
	senddata.headP = senddata.trailP = 0;
1054
	*ptr++ = 0x68;
1055
	*ptr++ = (BYTE)framelen;
1056
	*ptr++ = (SendSeqNo<<1)%256;
1057
	*ptr++ = (SendSeqNo<<1)/256;
1058
	*ptr++ = (RecvSeqNo<<1)%256;
1059
	*ptr++ = (RecvSeqNo<<1)/256;
1060
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
1061
	ptr += DC_D_HEADLEN;
1062
	//??????ַ
1063
	*ptr++=(START_ACC+startno)%256; 
1064
	*ptr++=(START_ACC+startno)/256; 
1065
	*ptr++=0;
1066

    
1067
	memcpy(ptr,buf,sendnum*5);
1068
	ptr+=sendnum*5;
1069

    
1070
	senddata.trailP = ptr - senddata.databuf ;
1071
	PrintLog(LOG_INFORMATION,"????ȫ??????Ӧ???֡");
1072
	AppToLink();
1073

    
1074
	SendSeqNo = (SendSeqNo + 1)%0x8000;
1075
	SendKwhFrameNo ++;
1076

    
1077
	return 1;
1078
}
1079

    
1080
/********************************************************************************
1081
*
1082
*	??????????ȫ??????Ӧȷ??֡
1083
*	????????
1084
*	???أ? 0  ????ʧ??
1085
*	       1  ???ͳɹ?
1086
*
1087
*********************************************************************************/
1088
sint32 CSE104S::App_AllData_Conf( uint8 cause,uint8 QOI)
1089
{
1090
	BYTE* ptr = senddata.databuf ;
1091
	senddata.headP = senddata.trailP = 0;
1092
	sint32 rtuno=pLink->GetRtuNo();
1093
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
1094

    
1095
	DC_D_DATAUNITID DataHead;
1096

    
1097
	DataHead.TypeID=C_IC_NA_1;
1098
	DataHead.VSQ.SQ = 0;
1099
	DataHead.VSQ.Num = 1;
1100
	DataHead.CommonAddrLo = (rtuaddr)%256;
1101
	DataHead.CommonAddrHi = (rtuaddr)/256;
1102
	DataHead.Cause  = cause;
1103
	*ptr++ = 0x68;
1104
	*ptr++ = 4+DC_D_HEADLEN+4;
1105
	*ptr++ = (SendSeqNo<<1)%256;
1106
	*ptr++ = (SendSeqNo<<1)/256;
1107
	*ptr++ = (RecvSeqNo<<1)%256;
1108
	*ptr++ = (RecvSeqNo<<1)/256;
1109
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
1110
	ptr += DC_D_HEADLEN;
1111
	*ptr++ = 0;
1112
	*ptr++ = 0;
1113
	*ptr++ = 0;
1114
	*ptr++ = QOI;//?ٻ??޶???
1115

    
1116
	senddata.trailP = ptr - senddata.databuf ;
1117
	PrintLog(LOG_INFORMATION,"????ȫ??????Ӧ֡,ԭ??:%d",cause);
1118
	AppToLink();
1119

    
1120
	if(cause == APP_COT_ACT_TERM)
1121
	{
1122
		m_flagCallReply &= 0xFB;
1123
		m_groupNo &= 0xE0;
1124
	}
1125
	
1126
	SendSeqNo ++;		
1127
	SendSeqNo %= 0x8000;		//Snd_seqnum Ϊ15λ??????????
1128
	return 1;
1129
}
1130

    
1131
/********************************************************************************
1132
*
1133
*	??????????ȫ??????Ӧң??֡
1134
*	????????
1135
*	???أ? 0  ????δ??????????ң??ֵ??????
1136
*	       1  ???ͽ???
1137
*	???ͣ?20
1138
*********************************************************************************/
1139
sint32 CSE104S::App_Ack_AllYx( )
1140
{
1141
	sint32 rtuno=pLink->GetRtuNo();
1142
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
1143

    
1144
	sint32 numperframe=(FRAME_MAXLEN-6-DC_D_HEADLEN-DC_D_INFOADDRLEN)/5;//ÿ֡?Ϊ255????ȥ6???ֽڵ?APCI,?ټ?ȥhlen???????ֽڵ?ң????ʼ??
1145

    
1146
	CEasyList *yxlist=pZfDataCtrl->GetYxStructList();
1147
	sint32 startno,sendnum,framelen=0;
1148
	
1149
	startno=SendFrameNo*numperframe*16;	//ÿ??????ɰ???16??YX
1150

    
1151
	sint32 numYx = yxlist->GetListLen();
1152
	if(numYx <=0)
1153
	{
1154
		m_flagCallReply &= 0xFE;
1155
		SendFrameNo = 0;
1156
		return 0;
1157
	}
1158

    
1159
	S_RDB_ZFYx *yxinfo;
1160
	yxinfo = (S_RDB_ZFYx *)((*yxlist)[numYx-1]);
1161
	if(yxinfo->YxNo < startno)
1162
	{
1163
		m_flagCallReply &= 0xFE;
1164
		SendFrameNo = 0;
1165
		return 0;
1166
	}
1167

    
1168
	sendnum = yxinfo->YxNo - startno + 1;
1169
	if(sendnum >numperframe*16)
1170
		sendnum = numperframe*16;
1171

    
1172
	uint8 buf[256]={0};
1173

    
1174
	sint32 i;
1175
	sint32 wdIndex,btIndex;
1176
	for(i=0;i<numYx; i++)
1177
	{
1178
		yxinfo=(S_RDB_ZFYx *)((*yxlist)[i]);
1179
		if(yxinfo->YxNo>=(startno+sendnum))
1180
			break;
1181

    
1182
		if(yxinfo->YxNo<startno)
1183
			continue;
1184

    
1185
		wdIndex = (yxinfo->YxNo-startno)/16*5 + ((yxinfo->YxNo-startno)%16)/8;
1186
		btIndex = (yxinfo->YxNo-startno)%8;
1187
		if(yxinfo->Val==1)
1188
		{
1189
			buf[wdIndex] |= (1<<btIndex);
1190
		}
1191
		if(ISBITSET(yxinfo->qds/*chgFlag*/, PTMSK_DI_VALID))
1192
			buf[wdIndex+2] |= (1<<btIndex);
1193

    
1194

    
1195
		yxinfo->OldVal=yxinfo->Val;
1196
		UNSETBIT(yxinfo->qds/*chgFlag*/, PTMSK_DI_CHANGE);
1197
	}
1198
//	if(i>=numYx)
1199
//	{
1200
//		m_flagCallReply &= 0xFE;
1201
//		SendFrameNo = 0;
1202
//	}
1203

    
1204
	DC_D_DATAUNITID DataHead;
1205
	DataHead.TypeID  = M_PS_NA_1;
1206
	DataHead.VSQ.SQ = 1;
1207
	DataHead.VSQ.Num=(sendnum+15)/16;
1208
	DataHead.CommonAddrLo = (rtuaddr)%256;
1209
	DataHead.CommonAddrHi = (rtuaddr)/256;
1210
	DataHead.Cause  = COT_CALLALLDATA;
1211
	DataHead.SrcAddr = 0;
1212

    
1213
	framelen = DataHead.VSQ.Num * 5 + DC_D_INFOADDRLEN + DC_D_HEADLEN + 4; //ҥ??
1214

    
1215
	uint8 *ptr = senddata.databuf ;
1216
	senddata.headP = senddata.trailP = 0;
1217

    
1218
	*ptr++ = 0x68;
1219
	*ptr++ = (BYTE)framelen;
1220
	*ptr++ = (SendSeqNo<<1)%256;
1221
	*ptr++ = (SendSeqNo<<1)/256;
1222
	*ptr++ = (RecvSeqNo<<1)%256;
1223
	*ptr++ = (RecvSeqNo<<1)/256;
1224
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
1225
	ptr += DC_D_HEADLEN;
1226
	*ptr++ = (startno)%256;
1227
	*ptr++ = (startno)/256;
1228
	*ptr++ = 0;
1229

    
1230
	memcpy(ptr,buf,DataHead.VSQ.Num*5);
1231
	ptr += DataHead.VSQ.Num*5;
1232

    
1233
	senddata.trailP = ptr - senddata.databuf ;
1234
	PrintLog(LOG_INFORMATION,"????ȫ??????Ӧң??֡");
1235
	AppToLink();
1236

    
1237
	SendSeqNo = (SendSeqNo + 1)%0x8000;
1238
	SendFrameNo ++;
1239
	//WYF 20180726
1240
	if(i>=numYx)
1241
	{
1242
		m_flagCallReply &= 0xFE;
1243
		SendFrameNo = 0;
1244
	}//WYF 20180726
1245
	return 1;
1246
}
1247

    
1248
/********************************************************************************
1249
*
1250
*	??????????ȫ??????Ӧң??֡
1251
*	????????
1252
*	???أ? 0  ????δ??????????ң??ֵ??????
1253
*	       1  ???ͽ???
1254
*
1255
*********************************************************************************/
1256
sint32 CSE104S::App_Ack_AllYc()
1257
{
1258
	sint32 rtuno=pLink->GetRtuNo();
1259
	uint8 rtuaddr=pRtu->GetRtuAddr(rtuno);
1260

    
1261
	CEasyList *yclist=pZfDataCtrl->GetYcStructList();
1262
	sint32 numperframe,startno,sendnum,framelen=0;
1263

    
1264
	numperframe=(FRAME_MAXLEN-6-DC_D_HEADLEN-DC_D_INFOADDRLEN)/5;//ÿ֡?Ϊ255????ȥ6???ֽڵ?APCI,?ټ?ȥhlen???????ֽڵ?ң????ʼ??
1265

    
1266
	startno=SendFrameNo*numperframe;
1267
	startno++;//WYF 20180726
1268
	sint32 numYc=yclist->GetListLen();
1269
	if(numYc<=0)
1270
	{
1271
		m_flagCallReply &= 0xFD;
1272
		SendFrameNo = 0;
1273
		return 0;
1274
	}
1275

    
1276
	S_RDB_ZFYc *ycinfo;
1277
	ycinfo = (S_RDB_ZFYc *)((*yclist)[numYc-1]);
1278
	if(ycinfo->YcNo < startno)
1279
	{
1280
		m_flagCallReply &= 0xFD;
1281
		SendFrameNo = 0;
1282
		return 0;
1283
	}
1284

    
1285
	sendnum = ycinfo->YcNo - startno +1;
1286
	if(sendnum >numperframe)
1287
		sendnum = numperframe;
1288

    
1289
	uint8 buf[256]={0};
1290
	sint32  i;
1291
	for(i=0;i<numYc ;i++)
1292
	{
1293
	   ycinfo=(S_RDB_ZFYc *)((*yclist)[i]);
1294
	   if(ycinfo->YcNo>=(sendnum+startno))
1295
		   break;
1296

    
1297
	   if(ycinfo->YcNo < startno)
1298
			continue;
1299

    
1300
	   memcpy(&buf[(ycinfo->YcNo-startno)*5],&(ycinfo->Val),4);
1301

    
1302
	   //WYF 20180726
1303
	   if(ISBITSET(ycinfo->qds/*chgFlag*/,PTMSK_AI_VALID))
1304
	   {	buf[(ycinfo->YcNo-startno)*5+4]=0x00;
1305
	   if(ISBITSET(ycinfo->qds/*chgFlag*/,PTMSK_AI_CHANGE))
1306
		   buf[(ycinfo->YcNo-startno)*5+4] |= 0x20;
1307
	   else
1308
		   buf[(ycinfo->YcNo-startno)*5+4] &= 0xE0;
1309
	   }
1310
	   else
1311
	   {
1312
		   buf[(ycinfo->YcNo-startno)*5+4]=0xD0;
1313
	   }
1314
	   //WYF 20180726
1315
//	   if(ISBITSET(ycinfo->qds/*chgFlag*/,PTMSK_AI_VALID))
1316
//		   buf[(ycinfo->YcNo-startno)*5+4] = 1;
1317

    
1318
		ycinfo->OldVal=ycinfo->Val;
1319
		UNSETBIT(ycinfo->qds/*chgFlag*/ ,PTMSK_AI_CHANGE);
1320
	}
1321
	
1322
/*	if(i>=numYc)
1323
	{
1324
		m_flagCallReply &= 0xFD;
1325
		SendFrameNo = 0;
1326
	}
1327
*/	
1328

    
1329
	DC_D_DATAUNITID DataHead;
1330
	DataHead.TypeID = M_ME_NC_1;			
1331
	DataHead.VSQ.SQ = 1;
1332
	DataHead.VSQ.Num=sendnum;
1333
	DataHead.CommonAddrLo = (rtuaddr)%256;
1334
	DataHead.CommonAddrHi = (rtuaddr)/256;//WYF 20180726 CommonAddrLo??????CommonAddrHi
1335
	DataHead.Cause  = COT_CALLALLDATA;	//??Ӧվ???ٻ?
1336
	DataHead.SrcAddr = 0;
1337
	framelen = DataHead.VSQ.Num * 5 + DC_D_INFOADDRLEN + DC_D_HEADLEN + 4;
1338

    
1339
	BYTE *ptr= senddata.databuf ;
1340
	senddata.headP = senddata.trailP = 0;
1341
	*ptr++ = 0x68;
1342
	*ptr++ = (BYTE)framelen;
1343
	*ptr++ = (SendSeqNo<<1)%256;
1344
	*ptr++ = (SendSeqNo<<1)/256;
1345
	*ptr++ = (RecvSeqNo<<1)%256;
1346
	*ptr++ = (RecvSeqNo<<1)/256;
1347
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
1348
	ptr += DC_D_HEADLEN;
1349
	*ptr++ = (startno+START_YC)%256;
1350
	*ptr++ = (startno+START_YC)/256;
1351
	*ptr++ = 0;//WYF 20180726
1352
	memcpy(ptr,buf,sendnum*5);
1353
	ptr += sendnum*5;
1354

    
1355
	senddata.trailP = ptr - senddata.databuf ;
1356
	PrintLog(LOG_INFORMATION,"????ȫ??????Ӧң??֡");
1357
	AppToLink();
1358

    
1359
	SendSeqNo = (SendSeqNo + 1)%0x8000;
1360
	SendFrameNo ++;
1361
	//WYF 20180726
1362
	if(i>=numYc)
1363
	{
1364
		m_flagCallReply &= 0xFD;
1365
		SendFrameNo = 0;
1366
	}//WYF 20180726
1367
	return 1;
1368
}
1369

    
1370
/********************************************************************************
1371
*
1372
*	?????????ͱ仯ң??֡
1373
*	??????ack_type ????֡???ͣ?ACK_REQUESTED??ACK_UNSOLICITED??
1374
*	???أ?1	?仯ң?????ݻ??????????ݷ??????
1375
*	      0 ?仯ң?????ݻ??????л???????δ????			
1376
*
1377
*********************************************************************************/
1378
sint32 CSE104S::App_Ack_ChangeYx()
1379
{
1380
	sint32 rtuno=pLink->GetRtuNo();
1381
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
1382

    
1383
	sint32 numperframe;
1384
	numperframe=(FRAME_MAXLEN-6-DC_D_HEADLEN)/4;
1385
	if(numperframe<=0)
1386
		return 0;
1387
	if(numperframe>127) numperframe=127;
1388

    
1389
	BYTE* ptr;	
1390

    
1391
	senddata.headP = senddata.trailP = 0;
1392
	ptr = senddata.databuf + 6 + DC_D_HEADLEN;
1393
	sint32 changenum=0;
1394
	CEasyList *yxlist=pZfDataCtrl->GetYxStructList();
1395
	S_RDB_ZFYx *yxinfo;
1396
	for(sint32 i=0;i<yxlist->GetListLen();i++)
1397
	{
1398
		yxinfo=(S_RDB_ZFYx *)((*yxlist)[i]);
1399
//		if(yxinfo->qds/*chgFlag*/)
1400
		if(ISBITSET(yxinfo->qds, PTMSK_DI_CHANGE))
1401
		{
1402
			*ptr++ = (yxinfo->YxNo)%256;
1403
			*ptr++ = (yxinfo->YxNo)/256;
1404
			*ptr++ = 0;
1405
			if(ISBITSET(yxinfo->qds/*chgFlag*/, PTMSK_DI_VALID))
1406
			{
1407
				if(yxinfo->Val==1)
1408
					*ptr++ = 1;
1409
				else
1410
					*ptr++ = 0; 
1411
			}
1412
			else
1413
				*ptr++ = 0x80;
1414
				
1415
			changenum++;
1416
			yxinfo->OldVal=yxinfo->Val;
1417
			UNSETBIT(yxinfo->qds/*chgFlag*/, PTMSK_DI_CHANGE);
1418

    
1419
			if(changenum>=numperframe)
1420
				break;
1421
		}
1422
	}
1423

    
1424
	if(changenum<=0)
1425
		return 0;
1426

    
1427
	senddata.trailP = ptr - senddata.databuf ;
1428

    
1429
	DC_D_DATAUNITID  DataHead;
1430
	DataHead.TypeID = M_SP_NA_1;
1431
	DataHead.VSQ.SQ = 0;
1432
	DataHead.VSQ.Num = changenum;
1433
	DataHead.Cause  = APP_COT_SPONT;
1434
	DataHead.SrcAddr = 0;
1435
	DataHead.CommonAddrLo = (rtuaddr)%256;
1436
	DataHead.CommonAddrHi = (rtuaddr)/256;
1437

    
1438
	ptr = senddata.databuf;
1439
	*ptr++ = 0x68;
1440
	*ptr++ = senddata.trailP -2;
1441
	*ptr++ = (SendSeqNo<<1)%256;
1442
	*ptr++ = (SendSeqNo<<1)/256;
1443
	*ptr++ = (RecvSeqNo<<1)%256;
1444
	*ptr++ = (RecvSeqNo<<1)/256;
1445
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
1446

    
1447
	PrintLog(LOG_INFORMATION,"???ͱ仯ң??֡");
1448
	AppToLink();
1449
	SendSeqNo = (SendSeqNo + 1)%0x8000;
1450

    
1451
	return 1;
1452
}
1453

    
1454

    
1455
/********************************************************************************
1456
*
1457
*	??????????????????
1458
*	????????
1459
*	???أ?1	???????ݻ??????????ݷ??????
1460
*	      0 ???????ݻ??????л???????δ????			
1461
*	???ͣ?30
1462
********************************************************************************/
1463
sint32 CSE104S::App_Ack_Soe()
1464
{
1465
	sint32 rtuno=pLink->GetRtuNo();
1466
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
1467

    
1468
	sint32 nSoeNum=pZfDataCtrl->GetSoeNum();
1469
	if(nSoeNum<1) return 0;
1470

    
1471
	sint32 numperframe;
1472
	numperframe=(FRAME_MAXLEN-6-DC_D_HEADLEN)/11;
1473
	if(numperframe<=0)
1474
		return 0;
1475
	if(numperframe>127) numperframe=127;
1476
	
1477
	if(nSoeNum > numperframe)
1478
		nSoeNum = numperframe;
1479

    
1480
	senddata.headP = senddata.trailP = 0;
1481
	BYTE* ptr = senddata.databuf + 6 + DC_D_HEADLEN;
1482

    
1483
	S_RAWSOE psoe;	
1484

    
1485
	sint32 yxindex;
1486
	for(sint32 i=0; i<nSoeNum; i++)
1487
	{
1488
		memset(&psoe,0,sizeof(S_RAWSOE));
1489
		pZfDataCtrl->GetASoe(&psoe);
1490
		yxindex=psoe.Yxno;
1491
		*ptr++ = yxindex%256;
1492
		*ptr++ = yxindex/256;
1493
		*ptr++ = 0;
1494
		if(psoe.Val==1)
1495
			*ptr++=1;
1496
		else
1497
			*ptr++=0;
1498

    
1499
		*ptr++=(psoe.Ms+psoe.Second*1000)%256;//?????λ
1500
		*ptr++=(psoe.Ms+psoe.Second*1000)/256;//?????λ
1501
		*ptr++=(psoe.Minute)&0x3F;//????
1502
		*ptr++=(psoe.Hour)&0x1F;//Hour
1503
		*ptr++=(psoe.Day)&0x1F;//Day
1504
		*ptr++=(psoe.Month)&0x0F;//Month;
1505
		*ptr++=(psoe.Year-2000)&0x7F;//Year;//WYF 20180726 -2000
1506

    
1507
		pZfDataCtrl->DelASoe();
1508

    
1509
	}
1510

    
1511
	senddata.trailP = ptr - senddata.databuf ;
1512

    
1513
	DC_D_DATAUNITID DataHead;
1514
	DataHead.TypeID = M_SP_TB_1;
1515
	DataHead.VSQ.SQ = 0;
1516
	DataHead.VSQ.Num = nSoeNum;
1517
	DataHead.Cause  = APP_COT_SPONT;
1518
	DataHead.SrcAddr = 0;
1519
	DataHead.CommonAddrLo = (rtuaddr)%256;
1520
	DataHead.CommonAddrHi = (rtuaddr)/256;
1521

    
1522
	sint32 framelen = DataHead.VSQ.Num * 11 + 4 + DC_D_HEADLEN; //SOE
1523

    
1524
	ptr = senddata.databuf;
1525
	*ptr++ = 0x68;
1526
	*ptr++ = (BYTE)framelen;
1527
	*ptr++ = (SendSeqNo<<1)%256;
1528
	*ptr++ = (SendSeqNo<<1)/256;
1529
	*ptr++ = (RecvSeqNo<<1)%256;
1530
	*ptr++ = (RecvSeqNo<<1)/256;
1531
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
1532

    
1533
	PrintLog(LOG_INFORMATION,"????SOE֡");
1534
	AppToLink();
1535
	SendSeqNo = (SendSeqNo + 1)%0x8000;
1536

    
1537
	return 1;
1538
}
1539

    
1540
/********************************************************************************
1541
*
1542
*	?????????ͱ仯ң??????
1543
*	????????
1544
*	???أ?1	?仯ң?????ݻ??????е????ݷ??????
1545
*	      0 ?仯ң?????ݻ??????л???????δ????			
1546
*
1547
*********************************************************************************/
1548
sint32 CSE104S::App_Ack_ChangeYc()
1549
{
1550
	sint32 rtuno=pLink->GetRtuNo();
1551
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
1552

    
1553

    
1554
	sint32 numperframe=(FRAME_MAXLEN-6-DC_D_HEADLEN)/8;
1555
	if(numperframe<=0)
1556
		return 0;
1557
	if(numperframe>127) numperframe=127;
1558

    
1559
	BYTE* ptr;
1560

    
1561
	senddata.headP = senddata.trailP = 0;
1562
	ptr = senddata.databuf + 6 + DC_D_HEADLEN;
1563

    
1564
	sint32 i,changenum;
1565
	changenum=0;
1566

    
1567
	CEasyList *yclist=pZfDataCtrl->GetYcStructList();
1568
	S_RDB_ZFYc *ycinfo;
1569

    
1570
	for(i=0;i<yclist->GetListLen();i++)
1571
	{
1572
		ycinfo=(S_RDB_ZFYc *)((*yclist)[i]);
1573
//		if(ycinfo->qds/*chgFlag*/)
1574
		if(ISBITSET(ycinfo->qds, PTMSK_AI_CHANGE))
1575
		{
1576
			*ptr++ = (START_YC+ycinfo->YcNo)%256;
1577
			*ptr++ = (START_YC+ycinfo->YcNo)/256;
1578
			*ptr++ = 0;
1579
			
1580
			memcpy(ptr,&(ycinfo->Val),4);
1581
			ptr += 4;
1582
			//WYF 20180726
1583
			if(ISBITSET(ycinfo->qds/*chgFlag*/,PTMSK_AI_VALID))
1584
			{	*ptr=0x00;
1585
				if(ISBITSET(ycinfo->qds/*chgFlag*/,PTMSK_AI_CHANGE))
1586
					*ptr |= 0x20;
1587
				else
1588
					*ptr &= 0xE0;
1589
				*ptr++;
1590
			}
1591
			else
1592
			{
1593
				*ptr++=0xD0;
1594
			}
1595
			//WYF 20180726			
1596
			changenum++;
1597
			ycinfo->OldVal=ycinfo->Val;
1598
			UNSETBIT(ycinfo->qds/*chgFlag*/ ,PTMSK_AI_CHANGE);
1599

    
1600
			if(changenum>=numperframe)
1601
				break;
1602
		}
1603
	}
1604
		
1605
	if(changenum<=0)
1606
		return 0;
1607
		
1608
		
1609
	senddata.trailP = ptr - senddata.databuf ;
1610

    
1611
	DC_D_DATAUNITID  DataHead;
1612
	DataHead.TypeID = M_ME_NC_1;
1613
	DataHead.VSQ.SQ = 0;
1614
	DataHead.VSQ.Num = changenum;
1615
	DataHead.Cause  = APP_COT_SPONT;
1616
	DataHead.SrcAddr = 0;
1617
	DataHead.CommonAddrLo = (rtuaddr)%256;
1618
	DataHead.CommonAddrHi = (rtuaddr)/256;
1619

    
1620
	ptr = senddata.databuf;
1621
	*ptr++ = 0x68;
1622
	*ptr++ = senddata.trailP -2;
1623
	*ptr++ = (SendSeqNo<<1)%256;
1624
	*ptr++ = (SendSeqNo<<1)/256;
1625
	*ptr++ = (RecvSeqNo<<1)%256;
1626
	*ptr++ = (RecvSeqNo<<1)/256;
1627
	memcpy(ptr,(BYTE *)&DataHead,DC_D_HEADLEN);
1628

    
1629

    
1630
	PrintLog(LOG_INFORMATION,"???ͱ仯ң??????");
1631
	AppToLink();
1632
	SendSeqNo = (SendSeqNo + 1)%0x8000;
1633

    
1634
	return 1;
1635
}
1636

    
1637
/********************************************************************************
1638
*
1639
*	????: Ӧ?ò㷢??U format
1640
*	????: type  U֡??????
1641
*	????: 1  ???ͳɹ?.
1642
*	     -1  ????ʧ??.
1643
*
1644
*********************************************************************************/
1645
sint32 CSE104S::App_SendAppUFormat(sint32 type)
1646
{
1647
	BYTE* ptr = senddata.databuf ;
1648
	
1649
	*ptr++ = 0x68;
1650
	*ptr++ = 4;			
1651
	*ptr++ = type | 0x03;
1652
	*ptr++ =0;
1653
	*ptr++ =0;
1654
	*ptr++ =0;			
1655
	senddata.headP = senddata.trailP = 0;
1656
	senddata.trailP = ptr - senddata.databuf ;
1657
	AppToLink();	
1658
	
1659
	return 1;
1660
}
1661

    
1662
/********************************************************************************
1663
*
1664
*	????: Ӧ?ò㷢??S format
1665
*	????: type  U֡??????
1666
*	????: 1  ???ͳɹ?.
1667
*	     -1  ????ʧ??.
1668
*
1669
*********************************************************************************/
1670
sint32 CSE104S::App_SendAppSFormat()
1671
{
1672
	BYTE * ptr = senddata.databuf ;
1673
	senddata.headP = senddata.trailP = 0;
1674
	
1675
	sint32 second=GetNowSecond();
1676
	if(SendACKFlag < W_IFrame)
1677
	{
1678
		if( second < Timeout2 )
1679
			return -1;
1680
	}
1681

    
1682
	*ptr++ = 0x68;
1683
	*ptr++ = 4;			
1684
	*ptr++ = 1;
1685
	*ptr++ =0;
1686
	*ptr++ =RecvSeqNo%128<<1;
1687
	*ptr++ =RecvSeqNo/128;
1688
			
1689
	senddata.trailP = ptr - senddata.databuf ;
1690
	AppToLink();
1691

    
1692
	
1693
	//ÿ????һS֡????Snd_seqnum???䣬?巢??ȷ?ϱ?־SendACKFlag??
1694
	SendACKFlag = 0;
1695
	return 1;
1696
}
1697

    
1698
void CSE104S::AppToLink()
1699
{
1700
	BYTE * ptr = senddata.databuf ;
1701
	sint32 len =senddata.trailP ;
1702

    
1703
	
1704
	pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)ptr,len);
1705
	pTxBuf->Write(ptr,len);
1706

    
1707
	
1708
	sint32 curtime=GetNowSecond();
1709
	LastTxTime = curtime;
1710
	return;
1711
}
1712

    
1713
void CSE104S::InitStation()
1714
{
1715
	sint32 nowsecond=GetNowSecond();
1716
	LastTxTime = nowsecond;
1717
	AppLayerStat=IEC104_APP_STATE_UNRESET;
1718
	SendSeqNo=0;
1719
	RecvSeqNo=0;
1720
	ConSeqNo=0;
1721

    
1722
	m_flagCallReply = 0;
1723
	m_groupNo = 0;
1724
	SendACKFlag=0;
1725
	pLink->SetCommStatus(CMST_NORMAL);
1726
	LostNum=0;
1727
}
1728

    
1729
void	CSE104S::SetExtendedPara(uint16 no, sint32 val)
1730
{
1731
	if(no == 0)//statinoNums
1732
		m_staNum = val;
1733
	if(no == 100)//QX rtuno
1734
		m_rtuQX = val;
1735
	if(no >= 1 && no<=16)//1-15 ZoneRtuNoEnd
1736
		m_zone[no] = val;	
1737
	PrintLog(LOG_WARNING,"SetExtendedPara no=%d   val=%d\n",no,val);
1738
}
1739
#if defined(WIN32)
1740
		extern "C" __declspec(dllexport) 
1741
#else 
1742
	extern "C"
1743
#endif
1744
CProtocol	*CreateProtocol(char *defpara)
1745
{
1746
	return (CProtocol*)(new CSE104S());
1747
}
(2-2/4)