epics/trunk/epics_example/myexampleApp/src
--- epics/trunk/epics_example/myexampleApp/src/client_util.c 2014-07-21 18:40:24 UTC (rev 807)
+++ epics/trunk/epics_example/myexampleApp/src/client_util.c 2014-07-22 00:04:39 UTC (rev 808)
@@ -11,6 +11,18 @@
xmlDoc* doc = NULL;
xmlNode* xml_root = NULL;
+void free_xml_string() {
+ if(client_util_debug>0) printf("free_xml_string(): at %p\n",xml_string_global);
+ if(xml_string_global!=NULL) {
+ free(xml_string_global);
+ xml_string_global = NULL;
+ if(client_util_debug>0) printf("free_xml_string(): done\n");
+ } else {
+ if(client_util_debug>0) printf("free_xml_string(): already nil\n");
+ }
+ if(client_util_debug>0) printf("free_xml_string(): now at %p\n",xml_string_global);
+}
+
char* strToUpper( char* s )
{
char* p = s;
@@ -118,26 +130,36 @@
struct sockaddr_in serv_addr;
struct hostent *server;
int socketfd;
+ if(client_util_debug>0) printf("open_socket 1: open socket \n");
socketfd = socket(AF_INET, SOCK_STREAM, 0);
+ if(client_util_debug) printf("open_socket 1: opened socket %d \n",socketfd);
if (socketfd < 0) {
socket_error("ERROR opening socket");
return socketfd;
}
+ if(client_util_debug) printf("open_socket 1: set server\n");
server = gethostbyname(hostname);
if (server == NULL) {
socket_error("ERROR, no such host");
return -1;
}
+ if(client_util_debug) printf("open_socket 1: got server at %p\n",server);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
+
+ if(client_util_debug) printf("open_socket 1: connect\n");
+
if (connect(socketfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
socket_error("ERROR connecting");
return -1;
}
+
+ if(client_util_debug) printf("open_socket 1: return socket %d\n",socketfd);
+
return socketfd;
}
@@ -151,12 +173,13 @@
void pollXmlStr(int sockfd) {
if(client_util_debug!=0) printf("pollXmlStr\n");
//clear old documents
- if(client_util_debug!=0) printf("free xml string\n");
- if(xml_string_global!=NULL) {
- free(xml_string_global);
- }
- if(client_util_debug!=0) printf("free xml string done\n");
- if(client_util_debug!=0) printf("free xml doc\n");
+ if(client_util_debug!=0) printf("pollXmlStr: free xml string %p\n",xml_string_global);
+ free_xml_string();
+ //if(xml_string_global!=NULL) {
+ // free(xml_string_global);
+ //}
+ if(client_util_debug!=0) printf("pollXmlStr: free xml string done %p\n",xml_string_global);
+ if(client_util_debug!=0) printf("pollXmlStr: free xml doc\n");
if(doc!=NULL) {
xmlFreeDoc(doc);
xmlCleanupParser();
@@ -192,7 +215,7 @@
if(client_util_debug!=0) printf("ERROR: xml_string has zero length!\n");
}
} else {
- if(client_util_debug!=0) printf("ERROR: xml_string is NULL!\n");
+ printf("WARNING: pollXmlStr: xml_string is NULL after reading from socket!\n");
}
}
@@ -247,7 +270,29 @@
}
+void writeHybridVoltageTrim(int sockid, int value, int hyb_id, char ch_name[]) {
+ char buffer[256];
+ char hyb_tag[256];
+ char ch_name_corr[256];
+ int n;
+ memset(buffer,0,256);
+ memset(hyb_tag,0,256);
+ memset(ch_name_corr,0,256);
+ // correct name
+ if(strcmp(ch_name,"dvdd")==0) strcpy(ch_name_corr,"Dvdd");
+ if(strcmp(ch_name,"avdd")==0) strcpy(ch_name_corr,"Avdd");
+ if(strcmp(ch_name,"v125")==0) strcpy(ch_name_corr,"V1_25");
+ sprintf(hyb_tag,"Hybrid%d%sTrim",hyb_id,ch_name_corr);
+ // sprintf(buffer,"<system><config><FrontEndTestFpga><FebCore><%s>0x%x</%s></FebCore></FrontEndTestFpga></config></system>\f",hyb_tag,value,hyb_tag);
+ sprintf(buffer,"<system><config><FrontEndTestFpga><FebCore><%s>%d</%s></FebCore></FrontEndTestFpga></config></system>\f",hyb_tag,value,hyb_tag);
+ if(client_util_debug > 0) printf("Executing:\"%s\"\n",buffer);
+ n = write(sockid,buffer,strlen(buffer));
+ if(n<0)
+ socket_error("ERROR writing to socket");
+ return;
+}
+
void writeReadConfig(int sockfd) {
int n;
//char buffer[] = "<system><command>ReadConfig</command></system>\f";
@@ -260,54 +305,9 @@
}
-void readTemp(int* sockfd,char value[],const unsigned int MAX) {
- //int debug=0;
- char tag[] = "system:status:cntrlFpga:Temp_0_0";
- xmlDoc* document = NULL;
- xmlNode* root = NULL;
- if(client_util_debug!=0) printf("readTemp: get DOM\n");
- getSystemDOM(sockfd,&document,&root);
- if(document==NULL) {
- printf("couldn't get xml document %p\n",document);
- return;
- }
- if(root==NULL) {
- printf("couldn't get root from document\n");
- document=NULL;
- return;
- }
- if(client_util_debug!=0) printf("retrieve value\n");
- retrieveValue(document,root,tag,value,MAX);
- if(client_util_debug!=0) printf("free the xml\n");
- xmlFreeDoc(document);
- xmlCleanupParser();
-}
-void readCalDelay(int* sockfd,char value[],const unsigned int MAX) {
- //int debug=0;
- char tag[] = "system:config:cntrlFpga:CalDelay";
- xmlDoc* document = NULL;
- xmlNode* root = NULL;
- if(client_util_debug!=0) printf("readCalDelay: get DOM\n");
- getSystemDOM(sockfd,&document,&root);
- if(document==NULL) {
- printf("couldn't get xml document %p\n",document);
- return;
- }
- if(root==NULL) {
- printf("couldn't get root from document\n");
- document=NULL;
- return;
- }
- if(client_util_debug!=0) printf("retrieve value\n");
- retrieveValue(document,root,tag,value,MAX);
- if(client_util_debug!=0) printf("free the xml\n");
- xmlFreeDoc(document);
- xmlCleanupParser();
-}
-
void getXMLValue(char tag[], char value[],const unsigned int MAX) {
if(client_util_debug!=0) printf("getXMLValue for tag: %s\n",tag);
if(doc==NULL) {
@@ -361,6 +361,7 @@
+
xmlNode* retrieveElement(xmlDoc* doc, xmlNode* node, char* tag) {
xmlNode* cur_node = NULL;
xmlNode* found_it = NULL;
@@ -580,23 +581,33 @@
int buf_len = 0;
int read_i = 0;
int read_n;
+ int nsleep;
+ int counter;
int debug = client_util_debug;
int n_endings = 0;
clock_t start_time;
clock_t diff;
char *pch;
- if(xml_string_global!=NULL) {
- if(debug!=0) printf("xml string is not NULL in read_system_xml_string4 so clear it!\n");
- //exit(1);
- }
+
+
+ if(debug!=0) printf("read_system_xml_string4: xml string at %p\n",xml_string_global);
+ free_xml_string();
+ // if(xml_string_global!=NULL) {
+ // free(xml_string_global);
+ // if(debug!=0) printf("read_system_xml_string4: xml string free done now at %p\n",xml_string_global);
+ // //exit(1);
+ //}
if(client_util_debug!=0) printf("xml_string at %p \n",xml_string_global);
start_time = clock();
diff = (start_time - start_time);
if(debug>0) printf("start_time at %f clocks\n",(float)start_time);
- while(((float)(diff/CLOCKS_PER_SEC))<5.0) {
- if(debug>0) printf("at read_i %d clocks %f time %f\n",read_i,(float)diff,(float)((float)diff)/CLOCKS_PER_SEC);
+ nsleep=0;
+ counter=0;
+ //while(((float)(nsdiff/CLOCKS_PER_SEC))<5.0) {
+ while(nsleep<6) {
+ if(debug>0) printf("at read_i %d clock time %f clocks %f time %f\n",read_i,(float)clock(),(float)diff,(float)((float)diff)/CLOCKS_PER_SEC);
//bzero(buf,READ_BUFFER_SIZE);
@@ -694,26 +705,63 @@
else {
if(debug>0) printf("Nothing to read from socket. Sleep a little\n");
//usleep(wait_interval_usec);
+ if(debug>0) {
+ printf("nsleep %d\n",nsleep);
+ }
sleep(1);
+ nsleep++;
}
+
+
if(n_endings>1) {
if(debug!=0) printf("\nfound %d endings at read_i %d with at len %d and strlen %ld\n",n_endings,read_i,buf_len,strlen(buf));
break;
}
-
-
+
+ if(debug>0) {
+ printf("update timing\n");
+ }
+
+
//update timing
diff = clock()-start_time;
+
+ counter++;
+ if(debug>0) {
+ printf("yepp\n");
+ }
+
+ if(counter>50) {
+ printf("Many (%d) iterations to get XML string. Something is strange\n",counter);
+ break;
+ }
+
+ if(debug>0) {
+ printf("yepp3\n");
+ }
+
}
- if(debug>0) printf("\nDone reading from socket. Found %d endings and a buf_len of %d and strlen of %ld\n",n_endings, buf_len, strlen(buf));
+ if(debug>0) {
+ printf("yepp2\n");
+ }
+ if(debug>0) {
+ printf("%d\n",buf_len);
+ printf("%p\n",buf);
+ }
+ if(debug>0) {
+ printf("\nDone reading from socket. Found %d endings and a buf_len of %d\n\n",n_endings, buf_len);
+ if(buf!=NULL) printf("strlen %ld\n", strlen(buf));
+ }
+
+
if(n_endings>=1) {
if(debug>0) printf("\nPick out config and status string between <system> and %d endings in string with strlen %ld and buf_len %d\n",n_endings,strlen(buf),buf_len);
if(debug>1) printf("\nbuf: \n%s\n",buf);
epics/trunk/epics_example/myexampleApp/src
--- epics/trunk/epics_example/myexampleApp/src/dbSubExample.c 2014-07-21 18:40:24 UTC (rev 807)
+++ epics/trunk/epics_example/myexampleApp/src/dbSubExample.c 2014-07-22 00:04:39 UTC (rev 808)
@@ -17,7 +17,7 @@
//char hostName[256] = "";// "134.79.229.141";
char* hostNameDef = "134.79.229.141";
char hostName[256];
-int portDef = 8090;
+int portDef = 8091;
int port;
int sockfd = 0;
int counter = 0;
@@ -50,40 +50,47 @@
printf("%d: Record %s called setupSocket(%p)\n", process_order, precord->name, (void*) precord);
}
- // look for host name and port in DB
- //initialize the string
- memset(hostName,0,BUF_SIZE);
- port=0;
- struct link* inpa;
- struct link* inpb;
- inpa = &precord->inpa;
- inpb = &precord->inpb;
- if(inpa && inpb) {
+ if(strcmp(precord->name,"SVT:poll_xml")==0) {
+
+ // look for host name and port in DB
+ //initialize the string
+ //memset(hostName,0,BUF_SIZE);
+ //port=0;
+ struct link* inpa;
+ struct link* inpb;
+ inpa = &precord->inpa;
+ inpb = &precord->inpb;
if(inpa && inpb) {
- //union value va = inpa->value;
- //union value vb = inpb->value;
- if (mySubDebug) {
- printf("inpa type %d value %p %s\n",inpa->type,inpa->value.constantStr,inpa->value.constantStr);
- printf("inpb type %d value %p %s\n",inpb->type,inpb->value.constantStr,inpb->value.constantStr);
- printf("inpa pv_link name %p\n",(inpa->value).pv_link.pvname);
- printf("inpa pv_link name %s\n",(inpa->value).pv_link.pvname);
+ if(inpa && inpb) {
+ //union value va = inpa->value;
+ //union value vb = inpb->value;
+ if (mySubDebug) {
+ printf("inpa type %d value %p %s\n",inpa->type,inpa->value.constantStr,inpa->value.constantStr);
+ printf("inpb type %d value %p %s\n",inpb->type,inpb->value.constantStr,inpb->value.constantStr);
+ printf("inpa pv_link name %p\n",(inpa->value).pv_link.pvname);
+ printf("inpa pv_link name %s\n",(inpa->value).pv_link.pvname);
+ }
+ if(inpa->value.pv_link.pvname && inpb->value.pv_link.pvname) {
+ //struct dbCommon* recA = ((inpa->value).pv_link).precord;
+ struct stringinRecord* recA = (stringinRecord*) ((inpa->value).pv_link).precord;
+ if (mySubDebug) printf("inpa pv_link record %p name %s\n",recA, recA->name);
+ char* stra = recA->val;
+ if (mySubDebug) printf("stra %p %s %c\n",stra,stra,stra[0]);
+ strcpy(hostName,stra);
+ if (mySubDebug) printf("got hostname \"%s\"\n",hostName);
+ struct longinRecord* recB = (longinRecord*) ((inpa->value).pv_link).precord;
+ if (mySubDebug) printf("inpb pv_link record %p name %s\n",recB, recB->name);
+ port = recB->val;
+ } else {
+ printf("inpa and inpb value are null when getting host and port\n");
+ }
+ } else {
+ printf("inpa and inpb strings are null when getting host and port\n");
}
- //struct dbCommon* recA = ((inpa->value).pv_link).precord;
- struct stringinRecord* recA = (stringinRecord*) ((inpa->value).pv_link).precord;
- if (mySubDebug) printf("inpa pv_link record %p name %s\n",recA, recA->name);
- char* stra = recA->val;
- if (mySubDebug) printf("stra %p %s %c\n",stra,stra,stra[0]);
- strcpy(hostName,stra);
- if (mySubDebug) printf("got hostname \"%s\"\n",hostName);
- struct longinRecord* recB = (longinRecord*) ((inpa->value).pv_link).precord;
- if (mySubDebug) printf("inpb pv_link record %p name %s\n",recB, recB->name);
- port = recB->val;
+
} else {
- printf("inpa and inpb strings are null when getting host and port\n");
+ printf("inpa and inpb are null when getting host and port\n");
}
-
- } else {
- printf("inpa and inpb are null when getting host and port\n");
}
if(strlen(hostName)==0) {
@@ -95,13 +102,15 @@
port = portDef;
}
- printf("hostName %s port %d\n",hostName,port);
-
if(strcmp(hostName,"")==0) {
printf("ERROR no valid hostname found\n");
return;
}
-
+
+ if (mySubDebug) {
+ printf("hostName %s port %d\n",hostName,port);
+ }
+
}
@@ -110,7 +119,7 @@
if (mySubDebug) {
printf("%d: Record %s called subPollInit(%p)\n", process_order, precord->name, (void*) precord);
}
- setupSocket(precord);
+ //setupSocket(precord);
return 0;
@@ -118,10 +127,67 @@
+ static void writeHybrid(subRecord* precord,char action[], int id, int feb_id, char ch_name[])
+{
+ if(mySubDebug) {
+ printf("Record %s called writeHybrid %s with val %f for feb_id= %d id=%d ch_name=%s\n", precord->name,action,precord->val,feb_id,id,ch_name);
+ }
+
+ if(sockfd<=0) {
+ setupSocket(precord);
+ if (mySubDebug) printf("Opening socket: host: %s:%d\n",hostName, port);
+ sockfd = open_socket(hostName,port);
+ if (mySubDebug) printf("Opened socket : %d\n",sockfd);
+
+ }
+
+ if(sockfd<=0) {
+ //if (mySubDebug)
+ printf("Failed to open socket in writeHybrid (host %s:%d) \n",hostName,sockfd);
+ return;
+ }
+ if(strcmp(action,"v_set_sub")==0) {
+ if(precord->val<255 && precord->val>0) {
+ writeHybridVoltageTrim(sockfd,(int)precord->val, id, ch_name);
+ if (mySubDebug) {
+ printf("Poll xml string after write\n");
+ }
+
+ pollXmlStr(sockfd);
+
+ if (mySubDebug) {
+ printf(" Poll XML done after write.\n");
+ }
+
+
+ } else {
+ printf("ERROR: voltage trim %f is not allowed!\n",precord->val);
+ }
+ }
+ else {
+ printf("ERROR: this action \"%s\" for writeHybrid is not defined!\n",action);
+ }
+
+ if (mySubDebug) {
+ printf(" Close socket\n");
+ }
+
+ sockfd = close_socket(sockfd);
+
+ if (mySubDebug) {
+ printf(" sleep after closing (%d)\n",sockfd);
+ sleep(1);
+ }
+
+}
+
+
+
+
static void readHybrid(subRecord* precord,char action[], int id, int feb_id, char ch_name[])
{
double constant;
@@ -158,10 +224,9 @@
else if(strcmp(action,"vf_sub")==0) {
readHybridV(feb_id, id, ch_name, "Far", value, BUF_SIZE);
}
- else if(strcmp(action,"v_set_sub")==0) {
- // need to move this to a write function?
- printf("ERROR: need to move the set voltage function!\n");
- exit(1);
+ else {
+ printf("wrong action for readHybrid \"%s\"\n",action);
+ return;
}
if (mySubDebug) {
printf("Got value=\"%s\"\n",value);
@@ -208,53 +273,15 @@
-static void writeHybridVoltage(subRecord* precord, char half[],char id[],char ch_name[])
-{
- if(mySubDebug) {
- printf("Record %s called writeHybridVoltage to val=%f for half=%s id=%s ch_name=%s\n",precord->name,precord->val,half,id,ch_name);
- }
-
-
- sockfd = open_socket(hostName,port);
- if(sockfd<0) {
- printf("ERROR could not open socket in readHybridVoltageNear\n");
- return;
- }
- if (mySubDebug) {
- printf("Opened TCP/IP socket %d\n",sockfd);
- }
- char value[BUF_SIZE];
- memset(value,0,BUF_SIZE);
- sprintf(value,"%d",(int)precord->val);
-
- if (mySubDebug) {
- printf("Write CalDelay\n");
- }
-
- writeCalDelay(&sockfd,value,BUF_SIZE);
-
- if (mySubDebug) {
- printf("Read CalDelay\n");
- }
- if (mySubDebug) {
- char value2[BUF_SIZE];
- memset(value2,0,BUF_SIZE);
- readCalDelay(&sockfd,value2,BUF_SIZE);
- printf("read directly after got value=\"%s\"\n",value);
- printf("close socket\n");
- }
- close_socket(sockfd);
-}
-
-
static long subLVProcess(subRecord *precord) {
process_order++;
if (mySubDebug) {
printf("%d: Record %s called subLVProcess(%p)\n",process_order, precord->name, (void*) precord);
}
//SVT:lv:hyb:bot:0:dvdd:vn_sub
+ //SVT:lv:hyb:bot:0:dvdd:v_set_sub
int feb_id;
char type[BUF_SIZE];
char board_type[BUF_SIZE];
@@ -300,12 +327,14 @@
printf("ERROR wrong option for hybrid ch: %s\n",ch_name);
return 0;
}
- if(strcmp(action,"vn_sub")!=0 && strcmp(action,"i_rd_sub")!=0 && strcmp(action,"v_set_sub")!=0) {
+ if(strcmp(action,"vn_sub")==0 || strcmp(action,"vf_sub")==0 || strcmp(action,"i_rd_sub")==0) {
+ readHybrid(precord,action,(int)li_id,feb_id,ch_name);
+ } else if(strcmp(action,"v_set_sub")==0) {
+ writeHybrid(precord,action,(int)li_id,feb_id,ch_name);
+ } else {
printf("ERROR this hybrid action type is not valid \"%s\"\n",action);
return 0;
- }
-
- readHybrid(precord,action,(int)li_id,feb_id,ch_name);
+ }
}
else {
printf("ERROR this board type is not valid \"%s\" for LV\n",board_type);
@@ -425,6 +454,7 @@
printf("%d: Record %s called subPollProcess(%p)\n",process_order, precord->name, (void*) precord);
printf("sockfd %d\n",sockfd);
}
+
if(sockfd<=0) {
setupSocket(precord);
printf("Re-opening socket: host: %s:%d\n",hostName, port);
@@ -470,102 +500,9 @@
return 0;
}
-static long mySubTempInit(subRecord* precord)
-{
- process_order++;
- if(mySubDebug) {
- printf("%d: Record %s called mySubTempInit(%p)\n",process_order, precord->name,(void*) precord);
- }
- return 0;
-}
-static long mySubCalDelayInit(subRecord* precord)
-{
- process_order++;
- if(mySubDebug) {
- printf("%d: Record %s called mySubCalDelayInit(%p)\n",process_order,precord->name,(void*) precord);
- }
- return 0;
-}
-static long mySubTempProcess(subRecord* precord)
-{
- process_order++;
- if(mySubDebug) {
- printf("%d: Record %s called mySubTempProcess(%p)\n", process_order, precord->name,(void*) precord);
- }
- sockfd = open_socket(hostName,port);
- if (mySubDebug) {
- printf("Opened TCP/IP socket %d\n",sockfd);
- }
- char value[256];
- memset(value,0,sizeof(value));
- readTemp(&sockfd,value,sizeof(value));
- if (mySubDebug) {
- printf("%s\n",value);
- //"23.44 C (0x0843)"
- }
- if (mySubDebug) {
- printf("close socket\n");
- }
- double val = extractTempValFromString(value);
- precord->val = val;
- //if (mySubDebug) {
- printf("mySubTempProcess got temp %f \n",precord->val);
- //}
- close_socket(sockfd);
-
- return 0;
-}
-
-
-
-
-
-static long mySubCalDelayProcess(subRecord* precord)
-{
- process_order++;
- if(mySubDebug) {
- printf("%d: Record %s called mySubCalDelayProcess(%p)\n",process_order,precord->name,(void*) precord);
- }
- sockfd = open_socket(hostName,port);
- if (mySubDebug) {
- printf("Opened TCP/IP socket %d\n",sockfd);
- }
- char value[256];
- memset(value,0,sizeof(value));
- //if(precord->a) {
- sprintf(value,"%d",(int)precord->a);
- if (mySubDebug) {
- printf("precord->a %f so write %s to record\n",precord->a,value);
- }
- writeCalDelay(&sockfd,value,sizeof(value));
- //}
- if (mySubDebug) {
- printf("Read CalDelay\n");
- }
- readCalDelay(&sockfd,value,sizeof(value));
- if (mySubDebug) {
- printf("Got value=\"%s\"\n",value);
- //"23.44 C (0x0843)"
- }
-
- if (mySubDebug) {
- printf("close socket\n");
- }
- double val = atof(value);
- precord->val = val;
- //if (mySubDebug) {
- printf("mySubCalDelayProcess got %f \n",precord->val);
- //}
- close_socket(sockfd);
-
- return 0;
-}
-
-
-
static long myAsubInit(aSubRecord *precord)
{
process_order++;
@@ -595,9 +532,5 @@
epicsRegisterFunction(subPollProcess);
epicsRegisterFunction(mySubInit);
epicsRegisterFunction(mySubProcess);
-epicsRegisterFunction(mySubTempInit);
-epicsRegisterFunction(mySubTempProcess);
-epicsRegisterFunction(mySubCalDelayInit);
-epicsRegisterFunction(mySubCalDelayProcess);
epicsRegisterFunction(myAsubInit);
epicsRegisterFunction(myAsubProcess);