#include "iec103.h"
char myprocessname[NAME_SIZE+1];
char mynodename[NAME_SIZE+1];
char strategy[NAME_SIZE+1];
char hostname[NAME_SIZE+1];
char com_tagname[NAME_SIZE+1];
char diag_tag[NAME_SIZE+1];
char IEC_TAGNAME[NAME_SIZE+1];
audio_msg Audio;
SENDDEF SendDef;
RCVDEF RcvDef;
MSG_BUF SendMsg;
MSG_BUF RcvMsg;
char recv_data[ BUFSIZE ];
IODEF *pIODEF;
IECDEF *pIECDEF[MaxIECNum],*pIEC;
char *pStr;
int IECCount,INTCOUNT;
int Display,Enqflag;
int GRP[MaxGRPNum];
unsigned port,RcvTimeOut,IONumber,IECInitMark;
short AddrIEC;
SHM_BH_DEF *pshm_bh_buf_w;
SHM_BH_DEF1 *pshm_bh_buf_r;
extern int fd;
extern char *rGetSharedMemory();
extern SysDef *pSysDef;
extern PORT_DEF port_def[];
extern void RcvTimeOut_hnd();
extern void alarm_handler();
extern void ralarm_handler();
extern void exit_exe();
struct timeb NowTime,EnqTime,ClkTime,FltTest,ValTime;
main(argc,argv)
int argc;
char *argv[];
{
char *pSHM;
int i;
if ( (argc!=3) && (argc!=4) ) {
printf("Command Syntax ----- drv103 com<11-48> TAGNAME [on|off]\n");
printf("Invalid argc ------- %s\n",argv[0]);
exit(0);
}
port=1;
while (strcmp(port_def[port].alias,"")!=0)
{
if (strcmp(port_def[port].alias,argv[1])==0) break;
else port++;
}
if (strcmp(port_def[port].alias,"")==0) {
printf("Command Syntax ----- drv103 com<11-48> TAGNAME [on|off]\n");
printf("Invalid argument --- %s\n",argv[1]);
exit(0);
}
strcpy(IEC_TAGNAME,argv[2]);
sprintf(myprocessname,"%s_%s",argv[0],argv[2]);
get_local_name(hostname);
if (InitSharedMemory()==-1) {
printf("InitSharedMemory Failurei --- %s_%s\n",argv[0],argv[1]);
exit(0);
}
if (get_local_aliasname(mynodename)==-1) {
printf("get_local_aliasname Failure---%s_%s\n",argv[0],argv[1]);
exit_exe();
}
if (get_local_strategy(strategy)==-1) {
printf("get_local_strategy Failure---%s_%s\n",argv[0],argv[1]);
exit_exe();
}
if (msg_init(myprocessname)==-1) {
printf("Msg_init failure---%s\n",myprocessname);
exit_exe();
}
get_diag_tag(hostname,diag_tag);
init_sighandler();
open_device();
init_device();
rLoadDataBase();
BH_ATTACH_SHM_BUF();
if (argc==3) pIEC->Display=0;
else
if (strcmp(argv[3],"on")==0) pIEC->Display=1;
if (pIEC->Display != 0) {
printf("\n *==================================================*");
printf("\n | |");
printf("\n | NARI IEC870-5-103 Relay Driver Interface |");
printf("\n | |");
printf("\n *==================================================*\n");
}
for(i=0;i<MaxIECNum;i++){
if(pIECDEF[i] == (IECDEF *)0)continue;
pIECDEF[i]->FCV = LPCI_FCV;
pIECDEF[i]->ACD = NO;
pIECDEF[i]->Status = NODE_ON;
pIECDEF[i]->PAD1 = 0;
pIECDEF[i]->PAD2 = 0;
if( pIEC->Display == 1 ) pIECDEF[i]->Display = 1;
else pIECDEF[i]->Display = 0;
}
pIEC->ClkSet = YES;
ftime(&EnqTime);
ftime(&ValTime);
ftime(&ClkTime);
ftime(&FltTest);
setsid();
main_loop();
}
main_loop()
{
int init_flag,rtn,i,pending;
init_flag = 0;
while (1)
{
Enqflag = 0;
if (get_redundant_flag(hostname) == 1) {
if (init_flag == 0) {
for(i=0;i<MaxIECNum;i++){
if(pIECDEF[i] == (IECDEF *)0)continue;
M_INIT_Proc(i);
sleep(1);
}
if( pshm_bh_buf_r != (SHM_BH_DEF1 *)-1 ){
pshm_bh_buf_r->rptr = pshm_bh_buf_r->wptr;
pshm_bh_buf_r->oflag = 0;
}
init_flag=1;
}
ReadBhShmProc();
dbmIEC();
ms_sleep(500);
}
else{
ioctl(fd,TCFLSH,0);
RcvMsg.mtype = MESG_ALL;
RcvMsg.msg_size = BUFSIZE;
RcvMsg.msg_data = recv_data;
pending = msg_recv( &RcvMsg, 0 );
if (pending>=0) IEC_Status_Proc();
for(i=0;i<MaxIECNum;i++){
if(pIECDEF[i] == (IECDEF *)0) continue;
pIECDEF[i]->ENQFLAG = 0;
}
if( pshm_bh_buf_r != (SHM_BH_DEF1 *)-1 ){
pshm_bh_buf_r->rptr = pshm_bh_buf_r->wptr;
pshm_bh_buf_r->oflag = 0;
}
sleep(2);
}
}
}
SendStatusToPeer(code,addr)
unsigned char code,addr;
{
MSG_BUF sbuf;
if (code == NODE_ON)
sbuf.function_code = MSG_IEC_STATUS_ON;
else sbuf.function_code = MSG_IEC_STATUS_OFF;
strcpy((char *)sbuf.dest_node_name,mynodename);
strcpy((char *)sbuf.dest_proc_name,myprocessname);
sbuf.status_flags = addr;
sbuf.msg_size = 0;
sbuf.msg_data = 0;
if(msg_send(&sbuf,0,8,1,0) < 0)
printf("%s send: Network Sending Error----.\n",myprocessname);
}
IEC_Status_Proc()
{
unsigned char relay_addr;
printf("recv msg from node=%s proc=%s\n",
RcvMsg.orig_node_name,RcvMsg.orig_proc_name);
relay_addr = RcvMsg.status_flags;
switch (RcvMsg.function_code)
{
case MSG_IEC_STATUS_ON:
pIECDEF[relay_addr]->Status = NODE_ON;
break;
case MSG_IEC_STATUS_OFF:
pIECDEF[relay_addr]->Status = NODE_OFF;
break;
default:
printf("Error Set Modify Command by Node=%s Proc=%s\n",
RcvMsg.orig_node_name,RcvMsg.orig_proc_name);
break;
}
}
ReadBhShmProc()
{
int addr,rptr,wait_times;
if( pshm_bh_buf_r == (SHM_BH_DEF1 *)-1 ) return;
if( pshm_bh_buf_r->oflag == 1 ||
pshm_bh_buf_r->rptr != pshm_bh_buf_r->wptr){
if( pshm_bh_buf_r->oflag == 1 ) {
pshm_bh_buf_r->rptr = pshm_bh_buf_r->wptr;
pshm_bh_buf_r->oflag = 0;
}
rptr = pshm_bh_buf_r->rptr;
if( pIEC->Display == 1 )
printf("BH read: instno=%d---rptr=%d---wptr=%d---\n",AddrIEC,rptr,pshm_bh_buf_r->wptr);
do{
addr = pshm_bh_buf_r->data[rptr].addr;
if(pIECDEF[addr] == (IECDEF *)0 || pIECDEF[addr]->Status == NODE_OFF){
rptr = ( rptr + 1 ) % BH_BUF_NUM;
ms_sleep(100);
continue;
}
memset(SendDef.Buf,0,MaxSendBuf);
memcpy(SendDef.Buf,pshm_bh_buf_r->data[rptr].buf,pshm_bh_buf_r->data[rptr].len);
SendDef.SendSize = pshm_bh_buf_r->data[rptr].len;
if( SendDef.Buf[0] == LPCI_SYN ){
SendDef.Buf[1] = SendDef.Buf[1] | pIECDEF[addr]->FCB | pIECDEF[addr]->FCV;
SendDef.Buf[3] = sum( &(SendDef.Buf[1]),2 );
}
else{
SendDef.Buf[4] = SendDef.Buf[4] | pIECDEF[addr]->FCB | pIECDEF[addr]->FCV;
SendDef.Buf[SendDef.SendSize-2] = sum( &(SendDef.Buf[4]),SendDef.Buf[1] );
}
TxDriver(SendDef.Buf,SendDef.SendSize);
rptr = ( rptr + 1 ) % BH_BUF_NUM;
ms_sleep(100);
if (RxDrvCheckProc(addr) < 0) break;
while( pIECDEF[addr]->ACD == NO && wait_times < MaxWaitTimes ){
wait_times ++ ;
C_PL2_NA_3(addr);
TxDriver(SendDef.Buf,SendDef.SendSize);
if (RxDrvCheckProc(addr) > 0) C_PL2_NA_Proc(addr);
else break;
sleep(1);
}
wait_times = 0;
while( pIECDEF[addr]->ACD == YES ){
C_PL1_NA_3(addr);
TxDriver(SendDef.Buf,SendDef.SendSize);
if (RxDrvCheckProc(addr) >