/* DDC control
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456*/
/*===================================-======================================*/
/*                         DDC STATE & DATA CONTROL                         */
/*-----------------------------------=--------------------------------------*/
/* AUTHORs:        S.G.Basiladze, Abdelouahab Abdesselam

   DESIGN ISSUEs:  This Script contains the subroutines for State & Data
                   DataPoints Control from DDC side.
                      The Send/Take-Stat functions may be also used for
                   sending positive integers to PVSS. The written value is
                   sending back in the Respond as acknowledge; if Respond
                   is negative it is an Error code.
                      The SendData function is sending float variables, the
                   Response in this case contains an Error code only.

   MODIF_HISTORY:  07.05.2003 - first version
/*===================================-======================================*/

main()
{ dpConnect("SendState","SCTdcs01:SendStat.trigger:_online.._value");
  dpConnect("TakeState","SCTdcs01:TakeStat.trigger:_online.._value");
  dpConnect("SendRequest","SCTdcs01:SendRequ.trigger:_online.._value");
}

/* SPLIT DDC CONTROL STRING ON DP_NAME & DP_STAT AND PUT STAT IN NAMED DP   */
/*                                                                          */
/*                      Text - Text string from DDC                         */
/*                      Flag - Flag (trigger - synchronization) from DDC    */ 
/*                                                                          */

 void SendState(string Text, bool Flag)
 { string     stri, mstr, estr;
   dyn_string info;
   string     name;
   int        stat, resp, len1, len2, eror;

     if(Flag)
   { resp=0; eror = 0;
     dpGet("SCTdcs01:SendStat.parameters:_online.._value", stri);
     info = strsplit(stri,"|");
     sscanf(info[1],"%s",name);         sscanf(info[2],"%d",stat);
     len1 = strlen(name);
     strreplace(name, "SCTdcs01", "dcs01");
     len2 = strlen(name);
       if(len2 < len1)
     { eror = dpSetWait("SCT"+name+":_original.._value", stat);
         if(eror < 0)
       { resp = eror;
/**/
         dpSet("SCTdcs01:Eror.Stat:_original.._value", 0x1);
         sprintf(estr,"\n DDC ERROR: SendStat dpSetWait() fault "); 
         Debug(estr);                                                     /**/
       } else
       { dpGet("SCT"+name+":_original.._value", resp);
       }
       dpSet("SCTdcs01:SendStat.response:_original.._value",resp,
             "SCTdcs01:SendStat.trigger:_original.._value",!Flag);
/**/
       sprintf(mstr,
       "\n: INFO: DDC SendStat Name=%s, Stat=%xh, Resp=%xh ",name,stat,resp);
       Debug(mstr);                                                       /**/
     } else
     { dpSet("SCTdcs01:Eror.Stat:_original.._value", 0x1);
       sprintf(estr,"\n DDC ERROR: Wrong subSystem number in SendStat "); 
       Debug(estr);                                                       /**/
     }
   }
 }

/* SPLIT DDC CONTROL STRING ON DP_NAME & DP_DATA AND PUT DATA IN NAMED DP   */
/*                                                                          */
/*                      Text - Text string from DDC                         */
/*                      Flag - Flag (trigger - synchronization) from DDC    */ 
/*                                                                          */

 void Send_Data(string Text, bool Flag)
 { string     stri, mstr, estr;
   dyn_string info;
   string     name;
   int        resp, len1, len2, eror;
   float      data;

     if(Flag)
   { resp=0; eror = 0;
     dpGet("SCTdcs01:SendData.parameters:_online.._value", stri);
     info = strsplit(stri,"|");
     sscanf(info[1],"%s",name);         sscanf(info[2],"%f",data);
     len1 = strlen(name);
     strreplace(name, "SCTdcs01", "dcs01");
     len2 = strlen(name);
       if(len2 < len1)
     { eror = dpSetWait("SCT"+name+":_original.._value", stat);
         if(eror < 0)
       { resp = eror;
/**/
         dpSet("SCTdcs01:Eror.Stat:_original.._value", 0x1);
         sprintf(estr,"\n DDC ERROR: SendStat dpSetWait() fault "); 
         Debug(estr);                                                     /**/
       } else
       { resp = 0;
       }
       dpSet("SCTdcs01:SendData.response:_original.._value",resp,
             "SCTdcs01:SendData.trigger:_original.._value",!Flag);
/**/
       sprintf(mstr,
       "\n: INFO: DDC Send_Data Name=%s, Data=%fh, Resp=%xh ",name,data,resp);
       Debug(mstr);                                                       /**/
     } else
     { dpSet("SCTdcs01:Eror.Stat:_original.._value", 0x1);
       sprintf(estr,"\n DDC ERROR: Wrong subSystem number in SendData "); 
       Debug(estr);                                                       /**/
     }
   }
 }

/* TAKE STATE (INTEGER) BASING ON DDC CONTROL STRING (DP_NAME | DP_STAT)    */
/*                                                                          */
/*                      Text - Text string from DDC                         */
/*                      Flag - Flag (trigger - synchronization) from DDC    */ 
/*                                                                          */

 void TakeState(string Text, bool Flag)
 { string     stri, mstr, estr;
   dyn_string info;
   string     name;
   int        stat, resp, len1, len2, eror;

     if(Flag)
   { resp=0; eror = 0;
     dpGet("SCTdcs01:TakeStat.parameters:_online.._value", stri);
     info = strsplit(stri,"|");
     sscanf(info[1],"%s",name);         sscanf(info[2],"%d",stat);
     len1 = strlen(name);
     strreplace(name, "SCTdcs01", "dcs01");
     len2 = strlen(name);
       if(len2 < len1)
     { dpGet("SCT"+name+":_original.._value", resp);
       dpSet("SCTdcs01:TakeStat.response:_original.._value",resp,
             "SCTdcs01:TakeStat.trigger:_original.._value",!Flag);
/**/
       sprintf(mstr,
       "\n: INFO: DDC TakeStat Name=%s, Stat=%xh, Resp=%xh ",name,stat,resp);
       Debug(mstr);                                                       /**/
     } else
     { dpSet("SCTdcs01:Eror.Stat:_original.._value", 0x1);
       sprintf(estr,"\n DDC ERROR: Wrong subSystem number in TakeStat "); 
       Debug(estr);                                                       /**/
     }
   }
 }

/* SPLIT DDC REQU_NUMBER STRING ON DP_NAME AND DP_REQU-NUMB                 */
/*                                                                          */
/*                      Text - Text string from DDC                         */
/*                      Flag - Flag (trigger - synchronization) from DDC    */ 
/*                                                                          */

 void SendRequest(string Text, bool Flag)
 { string     stri, mstr, estr;
   dyn_string info;
   string     name;
   int        numb, resp, len1, len2, eror;

     if(Flag)
   { resp=0; eror = 0;
     dpGet("SCTdcs01:SendRequ.parameters:_online.._value", stri);
     info = strsplit(stri,"|");
     sscanf(info[1],"%s",name);         sscanf(info[2],"%d",numb);
     len1 = strlen(name);
     strreplace(name, "SCTdcs01", "dcs01");
     len2 = strlen(name);
       if(len2 < len1)
     { eror = dpSetWait("SCT"+name+":_original.._value", numb);
         if(eror < 0)
       { resp = eror;
/**/
         dpSet("SCTdcs01:Eror.Stat:_original.._value", 0x1);
         sprintf(estr,"\n DDC ERROR: SendRequ dpSetWait() fault "); 
         Debug(estr);                                                     /**/
       } else
       { MakeArbitration("No", 0x1); 
         dpGet("SCTdcs01:SendRequ.Next:_original.._value", resp);
       }
       dpSet("SCTdcs01:SendRequ.response:_original.._value",resp,
             "SCTdcs01:SendRequ.trigger:_original.._value",!Flag);
/**/
       sprintf(mstr,
       "\n: INFO: DDC SendRequ Name=%s, Numb=%xh, Resp=%xh ",name,numb,resp);
       Debug(mstr);                                                       /**/
     } else
     { dpSet("SCTdcs01:Eror.Stat:_original.._value", 0x1);
       sprintf(estr,"\n DDC ERROR: Wrong subSystem number in SendRequ "); 
       Debug(estr);                                                       /**/
     }
   }
 }

/* MAKE ARBITRATION OF REQUESTS ON DELAYED HOLD->1                          */
/*                                                                          */
/*                      Dapo - DAta POint (element)                         */
/*                      Stat - DataPoint Hold state                         */ 
/*                                                                          */

 MakeArbitration(string Dapo, int Stat) 
 { int    req1, req2, req3, req4, next;
   string name, mstr;
   
     if(Stat > 0)
   { delay(1);                       /* Waiting for Requests & Arbitration  */
     dpGet("SCTdcs01:SendRequ.Req1:_original.._value", req1);
     dpGet("SCTdcs01:SendRequ.Req2:_original.._value", req2);
     dpGet("SCTdcs01:SendRequ.Req3:_original.._value", req3);
     dpGet("SCTdcs01:SendRequ.Req4:_original.._value", req4);
     next = 0;
       if((req1 > 0) && (req2 == 0) && (req3 == 0) && (req4 == 0))
     { next = 1;
     } if((req2 > 0) && (req3 == 0) && (req4 == 0))
     { next = 2;
     } if((req3 > 0) && (req4 == 0))
     { next = 3;
     } if(req4 > 0)
     { next = 4;
     } dpSet("SCTdcs01:SendRequ.Next:_original.._value", next);
/*
     sprintf(mstr,
             "\n: DDC INFO: MakeArbi r1=%d, r2=%d, r3=%d, r4=%d, Next=%d ",
                                     req1 , req2 , req3 , req4 , next);
     Debug(mstr);                                                         /**/
   }
 }

