epics/trunk/epics_example/myexampleApp/src
--- epics/trunk/epics_example/myexampleApp/src/client_util.c (rev 0)
+++ epics/trunk/epics_example/myexampleApp/src/client_util.c 2014-06-25 22:50:57 UTC (rev 738)
@@ -0,0 +1,721 @@
+#include "client_util.h"
+
+const unsigned int BUFFER_SIZE = 256;
+const unsigned int MAX_BUFFERS = 1000;
+int client_util_debug = 0;
+
+void socket_error(const char *msg)
+{
+ perror(msg);
+ exit(0);
+}
+
+int open_socket(char* hostname, int portno) {
+ struct sockaddr_in serv_addr;
+ struct hostent *server;
+ int socketfd;
+ socketfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (socketfd < 0)
+ socket_error("ERROR opening socket");
+ server = gethostbyname(hostname);
+ if (server == NULL) {
+ fprintf(stderr,"ERROR, no such host\n");
+ exit(0);
+ }
+ 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 (connect(socketfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
+ socket_error("ERROR connecting");
+ return socketfd;
+
+}
+
+int close_socket(int socketfd) {
+ return close(socketfd);
+}
+
+
+void getSystemDOM(int* sockfd, xmlDoc** document, xmlNode** root){
+
+ //int debug = 0;
+ char* xml_string = (char*) read_system_xml_string(sockfd);
+ if(client_util_debug!=0) printf("Got %d long xml string\n",strlen(xml_string));
+ if(client_util_debug!=0) printf("create xml document\n");
+ *document = xmlReadMemory(xml_string,strlen(xml_string),"noname.xml",NULL,0);
+ if(client_util_debug!=0) printf("xml doc done %p\n",*document);
+ *root = xmlDocGetRootElement(*document);
+ if(client_util_debug!=0) printf("found root name %s\n",(*root)->name);
+ if(client_util_debug!=0) printf("free the string\n");
+ free(xml_string);
+}
+
+
+void writeCalDelay(int* sockfd,char value[],const unsigned int MAX) {
+
+}
+
+
+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();
+}
+
+
+
+
+
+xmlNode* retrieveElement(xmlDoc* doc, xmlNode* node, char* tag) {
+ xmlNode* cur_node = NULL;
+ xmlNode* found_it = NULL;
+ //int debug = 1;
+ for(cur_node = node; cur_node; cur_node = cur_node->next) {
+ if(found_it!=NULL) {
+ if(client_util_debug!=0) printf("stop at cur_node %s prev %s \n",cur_node->name,cur_node->prev->name);
+ break;
+ }
+ if(client_util_debug!=0) printf("Looking for %s and comparing to %s\n",tag,cur_node->name);
+ if( (!xmlStrcmp(cur_node->name,(const xmlChar*)tag)) ) {
+ if(client_util_debug!=0) printf("found an element of type %d\n",cur_node->type);
+ if (cur_node->type == XML_ELEMENT_NODE) {
+ if(client_util_debug!=0) printf("found it\n");
+ found_it = cur_node;
+ break;
+ }
+ }
+
+ if(found_it!=NULL) {
+ if(client_util_debug!=0) printf("found it at name %s \n",cur_node->name);
+ return found_it;
+ }
+
+ found_it = retrieveElement(doc,cur_node->children,tag);
+
+ }
+
+ return found_it;
+}
+
+
+
+
+char* read_system_xml_string(int* socketfd) {
+ int debug = 0;
+ int read_ii;
+ int read_i;
+ int read_s;
+ int read_n;
+ int read_cur_buff;
+ int read_offset;
+ const unsigned int READ_BUFFER_SIZE = 256;
+ const unsigned int READ_MAX_BUFFERS = 1000;
+ char buffer_read[READ_BUFFER_SIZE];
+ char buffer_read_overlap[READ_BUFFER_SIZE*2];
+ char* buffers_read[READ_MAX_BUFFERS];
+
+ read_i = 0;
+ read_ii = 0;
+ read_s = 0;
+ read_n = 0;
+ read_cur_buff = 0;
+ read_offset = 0;
+
+ if(debug!=0) printf("reset buffers\n");
+ for(read_i=0;read_i<READ_MAX_BUFFERS;++read_i) {
+ // if(buffers_read[read_i] != NULL) {
+ //if(debug!=0) printf("reset buffers\n");
+ //free(buffers_read[read_i]);
+ //}
+ buffers_read[read_i] = NULL;
+ }
+
+ while(read_ii<10000) { // some large number to avoid problems
+ ++read_ii;
+ //strcpy(buffer_read_overlap,buffer_read);//,READ_BUFFER_SIZE);
+ bzero(buffer_read,READ_BUFFER_SIZE);
+ read_n = read((*socketfd),buffer_read,(READ_BUFFER_SIZE-1));
+ if (read_n < 0)
+ error("ERROR reading from socket");
+ //printf("buffer_read_overlap after clearing the old buffer:\n%s\nbuffer below:\n",buffer_read_overlap);
+ if(debug!=0) printf("\nRead %d\n%s\n",read_ii,buffer_read);
+ read_cur_buff = read_ii-1;
+ buffers_read[read_cur_buff] = (char*) malloc(sizeof(char)*strlen(buffer_read)); //READ_BUFFER_SIZE-1);
+ if(debug!=0) printf("malloc %d of length %d at %p\n",read_cur_buff,sizeof(char)*strlen(buffer_read),buffers_read[read_cur_buff]);
+ strcpy(buffers_read[read_cur_buff],buffer_read);
+
+ // first find start and then end to make sure to see the whole xml string
+ if(read_s==0) {
+ if(strstr(buffer_read,"<system>")!=0) {
+ read_s=1;
+ if(debug!=0) printf("\n found start of xml string in buffer (read_ii=%d)\n",read_ii);
+ bzero(buffer_read_overlap,2*READ_BUFFER_SIZE);
+ continue;
+ }
+ // check if we cut the element by adding prev buffer
+ strcpy(buffer_read_overlap+READ_BUFFER_SIZE-1,buffer_read);//avoid last char that ends string
+ if(strstr(buffer_read_overlap,"<system>")!=0) {
+ if(debug!=0) printf("\n found start of xml string in buffer_read_overlap (read_ii=%d)\n",read_ii);
+ continue;
+ }
+ }
+ else {
+ if(strstr(buffer_read,"</system>")!=0) {
+ if(debug!=0) printf("\n found end of xml string in buffer (read_ii=%d)\n",read_ii);
+ break;
+ }
+ // check if we cut the element by adding prev buffer
+ strcpy(buffer_read_overlap+READ_BUFFER_SIZE-1,buffer_read);//avoid last char that ends string
+ //printf("\nbuffer_read_overlap (strlen=%d) after adding prev and new buffer to dest:\n%s\n",strlen(buffer_read_overlap),buffer_read_overlap);
+ if(strstr(buffer_read_overlap,"</system>")!=0) {
+ if(debug!=0) printf("\n found end of xml string in buffer_read_overlap (read_ii=%d)\n",read_ii);
+ break;
+ }
+ if(debug!=0) printf("\n found NO end of xml string in buffer (read_ii=%d)\n",read_ii);
+ strcpy(buffer_read_overlap,buffer_read);//,READ_BUFFER_SIZE);
+ }
+ }
+
+ if(read_ii==(10000-1)) {
+ printf("\n ERROR couldn't find the complete xml string\n");
+ return NULL;
+ }
+
+ char* xml_string = (char*)malloc(sizeof(char)*(read_cur_buff+1)*READ_BUFFER_SIZE);
+ read_offset = 0;
+ if(debug!=0) {
+ printf("\nallocating xml_string size %d\n",sizeof(char)*(read_cur_buff+1)*READ_BUFFER_SIZE);
+ printf("read_cur_buff = %d\n",read_cur_buff);
+ }
+ read_i=0;
+ for(read_i=0;read_i<READ_MAX_BUFFERS;++read_i) {
+ if(buffers_read[read_i]!=NULL) {
+ strcpy(xml_string+read_offset,buffers_read[read_i]);
+ read_offset += strlen(buffers_read[read_i]);
+ if(debug!=0) printf("copying i%d to %p with offset %d and strlen %d:\n",read_i,(xml_string+read_offset),read_offset,strlen(buffers_read[read_i]));
+ //printf("%s",buffers_read[i]);
+ //printf("\nfreeing %d at %p:\n%s\n",i,buffers_read[i],buffers_read[i]);
+ free(buffers_read[read_i]);
+
+ }
+ }
+ if(debug!=0) {
+ printf("after copying to xml_string offset is %d\n",read_offset);
+ printf("\n\nxml_string is %d chars\n",strlen(xml_string));
+ //printf("%s\n",xml_string);
+ }
+ // remove stuff outside the xml tags
+ // monkey around with null character to terminate strings correctly
+ char* p_start_system = strstr(xml_string,"<system>");
+ char* p_end_system = strstr(xml_string,"</system>");
+ int len = p_end_system+sizeof(char)*strlen("</system>")-p_start_system;
+ if(debug!=0) printf("found start %p and end %p which is of length %d (strlen(</system>)=%d)\n",p_start_system,p_end_system,len,strlen("</system>"));
+ char* xml_string_pruned =(char*) malloc(sizeof(char)*len+1);
+ memcpy(xml_string_pruned, p_start_system,sizeof(char)*len);
+ // add termination to string
+ xml_string_pruned[sizeof(char)*len] = '\0';
+
+ //printf("free xml_string\n");
+ free(xml_string);
+ //printf("after free xml_string\n");
+
+ if(debug!=0) printf("xml_string_pruned at %p below\n%s\n",xml_string_pruned,xml_string_pruned);
+
+ return xml_string_pruned;
+
+ }
+
+
+
+
+void retrieveValue(xmlDoc* doc, xmlNode* node, char* tags, char value[], const unsigned int MAX) {
+ int debug = 0;
+ xmlChar* value_str = NULL;
+ char* pch;
+ char* pch_prev=NULL;
+ xmlNode* cur_node = NULL;
+ xmlNode* prev_node = node;
+ //use a copy since it modifies the original string
+ //char* tags = (char*)malloc(strlen(tags)*sizeof(char));
+ //strcpy(tags,tags);
+ if(debug!=0) printf("Splitting strings \"%s\" into tokens\n",tags);
+ if(strlen(tags)>0) {
+ pch = strtok(tags," :,.-");
+ while(pch!=NULL) {
+ if(debug!=0) printf("Find element %s \n",pch);
+ if(pch_prev!=NULL) {
+ if(debug!=0) printf("Find element %s from children of prev element at %p\n",pch,prev_node->name);
+ cur_node = retrieveElement(doc,prev_node->children,pch);
+ }
+ else {
+ if(debug!=0) printf("Find element %s from element %s\n",pch,prev_node->name);
+ cur_node = retrieveElement(doc,prev_node,pch);
+ }
+
+ // check that we found it
+ if(cur_node != NULL) {
+ if(debug!=0) printf("found cur_node name %s\n",cur_node->name);
+ } else {
+ if(debug!=0) printf("couldn't find cur_node\n");
+ break;
+ }
+
+ pch_prev = pch;
+ prev_node = cur_node;
+ pch = strtok(NULL," :,.");
+ }
+
+ //if there is a node at the end it means we should get it's list of strings
+ // if it's not the lowest level it might returns some garbage -> FIX THIS!
+ if(cur_node !=NULL) {
+ value_str = xmlNodeListGetString(doc,cur_node->children,0);
+ if(value_str!=NULL) {
+ if(debug!=0) printf("Found value %s\n",value_str);
+ if(strlen((char*) value_str)>=MAX) {
+ if(debug!=0) printf("the value for tags=%s is %d i.e. larger than MAX=%d, return no value!\n",tags,strlen((char*)value_str),MAX);
+ value_str = NULL;
+ } else {
+ if(debug!=0) printf("copy the value_str=\"%s\" (%d) to %p\n",value_str,strlen((char*)value_str),value);
+ strcpy((char*)value,(char*)value_str);
+ }
+ } else {
+ value_str = NULL;
+ if(debug!=0) printf("Found no value for tags %s\n",tags);
+ }
+ } else {
+ value_str = NULL;
+ if(debug!=0) printf("cur_node is null so no value found for tags=%s\n",tags);
+ }
+ }
+ if(value_str!=NULL) {
+ if(debug!=0) printf("free value_str\n");
+ xmlFree(value_str);
+ }
+}
+
+
+/*
+
+char* retrieveTagTagValue(xmlDoc* doc, xmlNode* node, char* tag, char* tag_mother) {
+ xmlNode* cur_node = NULL;
+ char* temp_str = NULL;
+ xmlChar* key;
+
+ for(cur_node = node; cur_node; cur_node = cur_node->next) {
+ if(temp_str!=NULL) {
+ //printf("stop at cur_node %s prev %s parent %s \n",cur_node->name,cur_node->prev->name,cur_node->parent->name);
+ break;
+ }
+ //printf("comparing %s with tag %s\n",cur_node->name,tag);
+ if( (!xmlStrcmp(cur_node->name,(const xmlChar*)tag)) ) {
+ //if( strstr(cur_node->name,tag)!=0 ) {
+ //printf("getting temp_str\n");
+ xmlNode* mother_node = cur_node->parent;
+ if(mother_node!=NULL) {
+ //printf("comparing mother %s with tag %s\n",mother_node->name,tag_mother);
+ if( (!xmlStrcmp(mother_node->name,(const xmlChar*)tag_mother)) ) {
+ key = xmlNodeListGetString(doc,cur_node->xmlChildrenNode,1);
+ if(key!=NULL) {
+ //printf("key %s\n",key);
+ temp_str = (char*) malloc(strlen((char*)key)*sizeof(char));
+ strcpy(temp_str,(char*)key);
+ xmlFree(key);
+ //printf("temp_str %s at %p\n",temp_str,temp_str);
+ }
+ }
+ }
+
+ if(temp_str!=NULL) {
+ //printf("found it at name %s \n",cur_node->name);
+ return temp_str;
+ }
+
+ }
+ temp_str = retrieveTagTagValue(doc,cur_node->children,tag,tag_mother);
+ }
+ return temp_str;
+ }
+
+
+
+char* retrieveTemp(xmlDoc* doc, xmlNode* node, int rce, int hyb) {
+ xmlNode* cur_node = NULL;
+ char* temp_str = NULL;
+
+ for(cur_node = node; cur_node; cur_node = cur_node->next) {
+ if(temp_str!=NULL) {
+ //printf("stop before processing name %s \n",node->name);
+ break;
+ }
+ //printf("looking at %s\n",cur_node->name);
+ if( !xmlStrcmp(cur_node->name,(const xmlChar*)"cntrlFpga") ) {
+ //printf("getting temp_str\n");
+ temp_str = getTemp(doc,cur_node,rce,hyb);
+ //printf("temp_str %s at %p\n",temp_str,temp_str);
+ }
+
+ if(temp_str!=NULL) {
+ //printf("found it at name %s \n",cur_node->name);
+ return temp_str;
+ }
+
+ temp_str = retrieveTemp(doc,cur_node->children,rce,hyb);
+
+ }
+
+ //printf("returning %s at %p from retrieveTemp\n",temp_str,temp_str);
+
+ return temp_str;
+ }
+
+
+
+
+
+
+
+char* getTemp(xmlDoc* doc, xmlNode* cur, int rce, int hyb) {
+
+ xmlChar* key;
+ char* key_copy = NULL;
+ xmlChar str[20];
+ sprintf(str,"Temp_%d_%d",rce,hyb);
+ //printf("looking for %s in children to %s \n",str,cur->name);
+ cur = cur->xmlChildrenNode;
+ while( cur != NULL) {
+ if( !xmlStrcmp(cur->name,(const xmlChar*)str) ) {
+ key = xmlNodeListGetString(doc,cur->xmlChildrenNode,1);
+ //printf("key at %p\n",key);
+ if( key != NULL) {
+ //printf("Hybrid %s: %s\n",str,key);
+ key_copy = (char*) malloc(strlen((char*)key)*sizeof(char));
+ strcpy(key_copy,(char*)key);
+ xmlFree(key);
+ //printf("return key_copy %s at %p\n",key_copy,key_copy);
+ return key_copy;
+ } else {
+ //printf("no valid key\n");
+ }
+ xmlFree(key);
+ }
+ cur = cur->next;
+ }
+ return NULL;
+}
+
+
+char* retrieveTagValue(xmlDoc* doc, xmlNode* node, char* tag) {
+ xmlNode* cur_node = NULL;
+ char* temp_str = NULL;
+ xmlChar* key;
+
+ for(cur_node = node; cur_node; cur_node = cur_node->next) {
+ if(temp_str!=NULL) {
+ //printf("stop at cur_node %s prev %s \n",cur_node->name,cur_node->prev->name);
+ break;
+ }
+ //printf("comparing %s with tag %s\n",cur_node->name,tag);
+ if( (!xmlStrcmp(cur_node->name,(const xmlChar*)tag)) ) {
+ //printf("getting temp_str\n");
+ key = xmlNodeListGetString(doc,cur_node->xmlChildrenNode,1);
+ if(key!=NULL) {
+ //printf("key (%d) \"%s\"\n",strlen((char*)key),key);
+ temp_str = (char*) malloc(strlen((char*)key)*sizeof(char));
+ strcpy(temp_str,key);
+ xmlFree(key);
+ //printf("temp_str %s at %p\n",temp_str,temp_str);
+ }
+ }
+
+ if(temp_str!=NULL) {
+ //printf("found it at name %s \n",cur_node->name);
+ return temp_str;
+ }
+
+ temp_str = retrieveTagValue(doc,cur_node->children,tag);
+
+ }
+
+ return temp_str;
+}
+
+
+
+
+char* retrieveTemp(xmlDoc* doc, xmlNode* node, int rce, int hyb) {
+ xmlNode* cur_node = NULL;
+ char* temp_str = NULL;
+
+ for(cur_node = node; cur_node; cur_node = cur_node->next) {
+ if(temp_str!=NULL) {
+ //printf("stop before processing name %s \n",node->name);
+ break;
+ }
+ //printf("looking at %s\n",cur_node->name);
+ if( !xmlStrcmp(cur_node->name,(const xmlChar*)"cntrlFpga") ) {
+ //printf("getting temp_str\n");
+ temp_str = getTemp(doc,cur_node,rce,hyb);
+ //printf("temp_str %s at %p\n",temp_str,temp_str);
+ }
+
+ if(temp_str!=NULL) {
+ //printf("found it at name %s \n",cur_node->name);
+ return temp_str;
+ }
+
+ temp_str = retrieveTemp(doc,cur_node->children,rce,hyb);
+
+ }
+
+ //printf("returning %s at %p from retrieveTemp\n",temp_str,temp_str);
+
+ return temp_str;
+ }
+
+
+
+
+
+
+char* copy_substr(char* buf_src, int* nread, const char* str_start, const char* str_end) {
+
+ char* p_start = strstr(buf_src,str_start);
+
+ if(p_start == NULL) {
+ printf("start string \"%s\" not in string\n",str_start);
+ return 0;
+ }
+
+ char* p_end = strstr(buf_src,str_end);
+
+ if(p_end == NULL) {
+ printf("end string \"%s\" not in string\n",str_end);
+ char str_end2[sizeof(str_end)+2];
+ sprintf(str_end2,"%s\n",str_end);
+ p_end = strstr(buf_src,str_end2);
+ if(p_end == NULL) {
+ printf("end string \"%s\" not in string\n",str_end2);
+ return 0;
+ }
+ }
+
+ *nread = p_end-p_start;
+
+ char* buf_dest = (char*) malloc(*nread);
+
+ strncpy(buf_dest, p_start, *nread);
+ return buf_dest;
+}
+
+
+
+char* copy_to_array(int n, int len, char* xml_buffer[]) {
+ const unsigned int BUFFER_SIZE = n*len;
+ char* buffer = (char*) malloc(BUFFER_SIZE);
+ int i;
+ for(i=0;i<n;i++) {
+ int offset = i*len;
+ memcpy(buffer+offset, xml_buffer[i], len);
+ }
+ return buffer;
+}
+
+
+char* read_xml(int *sockfd, int* len) {
+
+ // loop over input until I see a full cycle of the system
+ // then put into a character array
+
+ const unsigned int BUFFER_SIZE = 1024;
+ const unsigned int XML_BUFFERS = 20;
+ xmlChar buffer[BUFFER_SIZE];
+ xmlChar* xml_buffer[XML_BUFFERS];
+ int j;
+ int n;
+ unsigned int i = 0;
+ unsigned int open_system = 0;
+ int buffer_offset= 0;
+
+
+ for(j=0;j<XML_BUFFERS;++j) {
+ xml_buffer[j] = NULL;
+ }
+
+
+ while(i<100000) { // stupid
+ bzero(buffer,BUFFER_SIZE);
+ n = read(*sockfd,buffer,BUFFER_SIZE-1);
+ if (n < 0) {
+ error("ERROR reading from socket");
+ }
+
+ if(open_system == 0) {
+ if(strstr(buffer,"<system>") != NULL) {
+ open_system = 1;
+ buffer_offset= 0;
+ xml_buffer[buffer_offset] = (char*) malloc(BUFFER_SIZE);
+ memcpy(xml_buffer[buffer_offset],buffer,BUFFER_SIZE);
+ }
+ continue;
+ }
+
+ if(open_system == 1) {
+ buffer_offset++;
+ xml_buffer[buffer_offset] = (char*) malloc(BUFFER_SIZE);
+ memcpy(xml_buffer[buffer_offset],buffer,BUFFER_SIZE);
+ if(strstr(buffer,"</system>") != NULL) {
+ open_system = 0;
+ break;
+ }
+ }
+ ++i;
+ }
+
+ // Now take this 2D array and put into a long 1D array
+ // this feels stupid.
+ char* buffer_long = copy_to_array(buffer_offset+1,BUFFER_SIZE,xml_buffer);
+
+ // I think I should free up some of the buffers used in the 2D array?
+ for(j=0;j<XML_BUFFERS;++j) {
+ if(xml_buffer[j] != NULL) {
+ free(xml_buffer[j]);
+ }
+ }
+
+ // set the length as a returned parameter
+ *len = (buffer_offset+1)*BUFFER_SIZE;
+
+ // return the long array
+ return buffer_long;
+
+
+
+ }
+
+
+
+
+ void print_element_names(xmlNode * a_node)
+{
+ //printf("print_element_names starting with name %s\n", a_node->name);
+ xmlNode *cur_node = NULL;
+
+ for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
+ if (cur_node->type == XML_ELEMENT_NODE) {
+ printf("node type: Element, name: %s\n, content: \%s\n", cur_node->name, cur_node->content);
+ }
+
+ print_element_names(cur_node->children);
+ }
+}
+
+
+
+
+char* get_xml_substr(char* xml_buf, int n, int* n_read, const char* p_start_str, const char* p_end_str) {
+
+ // ok, here I'm not sure why strstr doesn't work?
+ // so this is a stupid way of removing evereyting except within some
+ // xml tags
+
+ //char* p_start_str = "<system>";
+ char* p_start = NULL;
+ //char* p_end_str = "</system>";
+ char* p_end = NULL;
+ int i,j;
+ for(i=0;i<n;++i) {
+ j = memcmp(xml_buf+i,p_start_str,strlen(p_start_str));
+ if(j==0 && p_start==NULL) {
+ //printf("found %s at %d\n",p_start_str,i);
+ p_start = xml_buf+i;
+ }
+ j = memcmp(xml_buf+i,p_end_str,strlen(p_end_str));
+ if(j==0 && p_end==NULL) {
+ //printf("found %s at %d\n",p_end_str,i);
+ p_end = xml_buf+i + strlen(p_end_str);
+ }
+ }
+
+ if(p_start==NULL || p_end==NULL) {
+ *n_read = 0;
+ return NULL;
+ }
+
+ // then copy out that range
+
+ char* xml_buf_clean = (char*) malloc(p_end-p_start);
+ xml_buf_clean = memcpy(xml_buf_clean,p_start,p_end-p_start);
+ *n_read = (p_end-p_start);
+ return xml_buf_clean;
+ }
+
+
+void findTemp(char* xml_buf, int n) {
+
+ // xml stuff
+ xmlDoc *document;
+ xmlNode *root, *first_child, *node;
+ char *filename;
+
+ // ok, here I'm not sure why strstr doesn't work?
+ // so this is a stupid way of removing evereyting except within some
+ // xml tags
+
+ char* xml_buf_clean;
+ int n_clean;
+ xml_buf_clean = get_xml_substr(xml_buf,n,&n_clean,"<system>","</system>");
+ printf("read %d of %d\n",n_clean,n);
+ // I should turn this into a DOM
+ // but I get a bunch of annoying characters that the parser compains about!?
+ // be stupid here
+
+ int rce;
+ int hyb;
+ //char* t_p_start = NULL;
+ //char* t_p_end = NULL;
+ char* temp;
+ char t_str_start[256];
+ char t_str_end[256];
+ int n_temp;
+ for(rce=0;rce<3;++rce) {
+ for(hyb=0;hyb<4;++hyb) {
+ sprintf(t_str_start,"<Temp_%d_%d>",rce,hyb);
+ sprintf(t_str_end,"</Temp_%d_%d>",rce,hyb);
+ //printf("%s -> %s\n",t_str_start,t_str_end);
+ temp = get_xml_substr(xml_buf_clean,n_clean,&n_temp,t_str_start,t_str_end);
+ if(temp == NULL) {
+ printf(" couldn't find %s\n",temp);
+ } else {
+ printf("found %s\n",temp);
+ }
+ free(temp);
+
+ }
+
+ }
+
+ //free(xml_buf_clean);
+
+}
+
+*/
epics/trunk/epics_example/myexampleApp/src
--- epics/trunk/epics_example/myexampleApp/src/dbSubExample.c 2014-06-25 22:42:54 UTC (rev 737)
+++ epics/trunk/epics_example/myexampleApp/src/dbSubExample.c 2014-06-25 22:50:57 UTC (rev 738)
@@ -6,15 +6,15 @@
#include <aSubRecord.h>
#include <epicsExport.h>
#include <tcpipExample.h>
+#include <client_util.h>
int mySubDebug = 1;
-
+int sockfd = -1;
+int counter = 0;
static long mySubInit(subRecord *precord)
{
if (mySubDebug) {
printf("Record %s called mySubInit(%p)\n", precord->name, (void*) precord);
- int status = init_socket();
- printf("TCP/IP status = %d\n",status);
}
return 0;
}
@@ -24,13 +24,65 @@
if (mySubDebug) {
printf("Record %s called mySubProcess(%p)\n",
precord->name, (void*) precord);
- int status = tcpip_poll();
- printf("poll status = %d\n",status);
-
- }
+ }
+ precord->val++;
+ counter++;
return 0;
}
+static long mySubTempInit(subRecord* precord)
+{
+ if(mySubDebug) {
+ printf("Record %s called mySubTempInit(%p)\n",precord->name,(void*) precord);
+ }
+ return 0;
+}
+
+double extractTempValFromString(char value[]) {
+ double t = 0.0;
+ char* p_start = strstr(value,"C (");
+ if(p_start != NULL) {
+ char* t_str = (char*) malloc(p_start-value); //strlen(value));
+ memcpy(t_str,value,p_start-value);//,p_start-value);
+ t = atof(t_str);
+ //if(mySubDebug) {
+ // printf("convert %s to float %f\n",t_str,t);
+ //}
+ free(t_str);
+ }
+ return t;
+}
+
+static long mySubTempProcess(subRecord* precord)
+{
+ if(mySubDebug) {
+ printf("Record %s called mySubTempProcess(%p)\n",precord->name,(void*) precord);
+ }
+ sockfd = open_socket("134.79.229.141",8090);
+ 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 myAsubInit(aSubRecord *precord)
{
if (mySubDebug)
@@ -52,5 +104,7 @@
epicsExportAddress(int, mySubDebug);
epicsRegisterFunction(mySubInit);
epicsRegisterFunction(mySubProcess);
+epicsRegisterFunction(mySubTempInit);
+epicsRegisterFunction(mySubTempProcess);
epicsRegisterFunction(myAsubInit);
epicsRegisterFunction(myAsubProcess);