23 #define EIB_HEADERSIZE 6 24 #define EIB_VERSION 0x10 27 #define EIB_CRICRDSIZE 4 28 #define TUNNEL_CONNECTION 4 29 #define REMLOG_CONNECTION 6 30 #define OBJSVR_CONNECTION 8 31 #define TUNNEL_LINKLAYER 2 33 #define E_NO_MORE_CONNECTIONS 0x24 34 #define E_SEQUENCE_NUMBER 0x04 36 #define L_Data_Req 0x11 37 #define L_Data_Con 0x2E 38 #define L_Data_Ind 0x29 64 int expected_recseq = 0;
76 ret = eib->
recv(&pdu,
sizeof(pdu));
80 switch (ntohs(pdu.servicetype))
87 if(eib->
debug) ::printf(
"eib_reader() CONNECTIONSTATE_RESPONSE\n");
91 ::printf(
"eib_reader() TUNNELING_REQUEST sequenzecounter=%d\n",recseq);
97 len = ntohs(pdu.totalsize) - 6;
104 pdu.data[2] = recseq;
106 if(recseq != expected_recseq)
109 ::printf(
"eib_reader() recseq=%d expected_recseq=%d\n",recseq,expected_recseq);
113 expected_recseq = (expected_recseq+1) & 0x0ff;
114 eib->rlUdpSocket::sendto(&pdu, ntohs(pdu.totalsize), eib->
server);
125 if(eib->
debug) ::printf(
"eib_reader() TUNNELLING_ACK typespecific=0x%x\n",pdu.data[3]);
130 if(eib->
debug) ::printf(
"eib_reader() TUNNELLING_NACK typespecific=0x%x\n",pdu.data[3]);
134 ::printf(
"eib_reader() unknown servicetype=0x%x\n",ntohs(pdu.servicetype));
141 if(eib->
debug) ::printf(
"send heartbeat\n");
148 eib->rlUdpSocket::sendto(&pdu, ntohs(pdu.totalsize), eib->
server);
181 if(num_signals <= 0)
return;
195 char *cptr = (
char *)
mem;
204 short unsigned int *sptr;
207 if(len <= 0 ||
mem == NULL)
return -1;
210 ::printf(
"rlEIBnetIP::storeBuffer() buf=[0x%x",buf[0]);
211 for(i=1; i<len; i++) ::
printf(
",0x%x",buf[i]);
219 sptr = (
unsigned short *) &buf[4];
220 tel.
saddr = ntohs(*sptr);
221 sptr = (
unsigned short *) &buf[6];
222 tel.
daddr = ntohs(*sptr);
225 tel.
val[0] = buf[10];
226 tel.
val[1] = buf[10+1];
227 tel.
val[2] = buf[10+2];
228 tel.
val[3] = buf[10+3];
229 for(i=0; i<(len-10); i++) tel.
val[i] = buf[10+i];
233 ::printf(
"rlEIBnetIP::storeBuffer() messagecode=0x%x L_Data_Req\n",tel.
mc);
240 ::printf(
"rlEIBnetIP::storeBuffer() unknown messagecode=0x%x L_Data_Con\n",tel.
mc);
245 ::printf(
"\nmc=0x%x addi1=0x%x ctrl1=0x%x ctrl2=0x%x saddr=0x%x daddr=0x%x acpi_length=0x%x apci=0x%x",
254 for(i=10; i<len; i++) ::
printf(
" val[%d]=0x%x",i-10,tel.
val[i-10]);
265 if(memptr[i].mc == 0)
269 memcpy(&memptr[i],&tel,
sizeof(tel));
274 else if(memptr[i].daddr == tel.
daddr)
278 memcpy(&memptr[i],&tel,
sizeof(tel));
289 int s1,s2,s3,d1,d2,d3,val,length;
291 s1 = tel->
saddr/(8*256);
292 s2 = (tel->
saddr/256) & 0x0ff;
293 s3 = tel->
saddr & 0x0ff;
294 d1 = tel->
daddr/(8*256);
295 d2 = (tel->
daddr/256) & 0x0ff;
296 d3 = tel->
daddr & 0x0ff;
305 char *cptr = (
char *) &tel->
val[0];
306 val = (((*cptr)*256)+tel->
val[1]);
310 char *cptr = (
char *) &tel->
val[0];
311 val = (((*cptr)*256)+tel->
val[1])*tel->
val[2];
315 char *cptr = (
char *) &tel->
val[0];
316 val = ((((*cptr)*256)+tel->
val[1])*(tel->
val[2]*256) * tel->
val[3]);
318 ::printf(
"src=/%d/%d/%03d\tdest=/%d/%d/%03d\tval=0x%x\tlen=%d\n",s1,s2,s3,d1,d2,d3,val,length);
324 if(
provider == NULL || tel == NULL)
return -1;
326 int d1,d2,d3,val,length;
327 char name[128],
value[128];
332 d1 = tel->
daddr/(8*256);
333 d2 = (tel->
daddr/256) & 0x0ff;
334 d3 = tel->
daddr & 0x0ff;
343 char *cptr = (
char *) &tel->
val[0];
344 val = (((*cptr)*256)+tel->
val[1]);
348 char *cptr = (
char *) &tel->
val[0];
349 val = (((*cptr)*256)+tel->
val[1])*tel->
val[2];
353 char *cptr = (
char *) &tel->
val[0];
354 val = ((((*cptr)*256)+tel->
val[1])*(tel->
val[2]*256) * tel->
val[3]);
357 sprintf(name,
"/%d/%d/%03d",d1,d2,d3);
358 sprintf(
value,
"%d",val);
366 if(fout == NULL)
return -1;
368 int s1,s2,s3,d1,d2,d3,val,length;
378 if(tel->
mc == 0)
break;
380 s1 = tel->
saddr/(8*256);
381 s2 = (tel->
saddr/256) & 0x0ff;
382 s3 = tel->
saddr & 0x0ff;
383 d1 = tel->
daddr/(8*256);
384 d2 = (tel->
daddr/256) & 0x0ff;
385 d3 = tel->
daddr & 0x0ff;
393 char *cptr = (
char *) &tel->
val[0];
394 val = (((*cptr)*256)+tel->
val[1]);
398 char *cptr = (
char *) &tel->
val[0];
399 val = (((*cptr)*256)+tel->
val[1])*tel->
val[2];
403 char *cptr = (
char *) &tel->
val[0];
404 val = ((((*cptr)*256)+tel->
val[1])*(tel->
val[2]*256) * tel->
val[3]);
406 fprintf(fout,
"/%d/%d/%d\t/%d/%d/%d\t0x%x\t%d\n",s1,s2,s3,d1,d2,d3,val,length);
416 char line[1024], *cptr;
417 int s1,s2,s3,d1,d2,d3,i,val,len;
420 if(filename == NULL)
return -1;
421 fin = fopen(filename,
"r");
424 ::printf(
"rlEIBnetIP::setValuesFromCSV(%s) could not open file\n",filename);
428 while(fgets(line,
sizeof(line)-1,fin) != NULL)
431 cptr = strchr(line,
'/');
432 if(cptr == NULL)
break;
433 sscanf(cptr,
"/%d/%d/%d",&s1,&s2,&s3);
435 while(line[i] !=
'\0')
437 if(line[i] ==
' ' || line[i] ==
'\t')
439 while(line[i] ==
' ' || line[i] ==
'\t') i++;
442 cptr = strstr(&line[i],
"0x");
445 sscanf(&line[i],
"/%d/%d/%d %d %d",&d1,&d2,&d3,&val,&len);
449 sscanf(&line[i],
"/%d/%d/%d %x %d",&d1,&d2,&d3,&val,&len);
457 cptr = strchr(line,
'/');
460 sprintf(line,
"/%d/%d/%d",d1,d2,d3);
463 memcpy(&uval,&val,
sizeof(uval));
483 ::printf(
"rlEIBnetIP::connect() already connected\n");
488 memset(&response,0,
sizeof(response));
513 ::printf(
"response.channelid = 0x%x\n",response.
data[0]);
524 ::printf(
"response == CONNECT_RESPONSE status=0x%x failed\n",response.
data[1]);
529 ::printf(
"response != CONNECT_RESPONSE\n");
555 if(
debug)
::printf(
"rlEIBnetIP()::sendDisconnectRequest()\n");
601 cptr = strchr(adr,
'/');
604 ::printf(
"USER_ERROR: rlEIBnetIP::setSourceAdr() wrong adr=%s\n", adr);
607 sscanf(cptr,
"/%d/%d/%d",&a1,&a2,&a3);
608 saddr = ((a1*8)+a2)*256+a3;
615 memcpy(&uval,&val,
sizeof(uval));
624 memcpy(&val,&uval,
sizeof(val));
630 unsigned int uval,
s,e,m;
632 if(length == -1 || length == 2)
635 if(val < 0.0f)
s = 0x80000000;
637 m = (
unsigned int) (val*100.0f);
645 fval = fabsf(val)/(float) m;
647 e = ((
unsigned int) eval) * 8* 256;
653 memcpy(&uval,&val,
sizeof(uval));
665 unsigned int uval,e,m;
667 if(uval & 0x080000000) sign = 1.0f;
669 uval = uval | 0x07fffffff;
672 val = sign*(0.01f*m)*(2^e);
681 memcpy(&val,&uval,
sizeof(
float));
688 unsigned int val,a1,a2,a3,length;
694 memset(&tel,0xff,
sizeof(
EIB_TEL));
695 cptr = strchr(name,
'/');
698 ::printf(
"USER_ERROR: rlEIBnetIP::value() wrong name=%s\n", name);
701 sscanf(cptr,
"/%d/%d/%d",&a1,&a2,&a3);
703 tel.
daddr = ((a1*8)+a2)*256+a3;
707 if(memptr[i].mc == 0)
break;
709 if(memptr[i].daddr == tel.
daddr)
716 val = memptr[i].
val[0];
720 char *cptr = (
char *) &memptr[i].val[0];
721 val = (((*cptr)*256)+memptr[i].val[1]);
725 char *cptr = (
char *) &memptr[i].val[0];
726 val = (((*cptr)*256)+memptr[i].val[1])*memptr[i].
val[2];
730 char *cptr = (
char *) &memptr[i].val[0];
731 val = ((((*cptr)*256)+memptr[i].val[1])*(memptr[i].
val[2]*256) * memptr[i].val[3]);
734 if(length < 1 || length > 4 || length == 3)
736 ::printf(
"rlEIBnetIP::value(0x%x) unknown length=%d\n",val,length);
748 unsigned int a1,a2,a3;
757 memset(&tel,0xff,
sizeof(
EIB_TEL));
758 cptr = strchr(name,
'/');
761 ::printf(
"USER_ERROR: rlEIBnetIP::getText() wrong name=%s\n", name);
764 sscanf(cptr,
"/%d/%d/%d",&a1,&a2,&a3);
766 tel.
daddr = ((a1*8)+a2)*256+a3;
770 if(memptr[i].mc == 0)
break;
772 if(memptr[i].daddr == tel.
daddr)
776 for(j=0; j<14; j++) buf[j] = memptr[i].val[j];
779 while(j<maxlen && j<14)
794 int length = strlen(text);
795 unsigned int a1,a2,a3,daddr;
802 if(length > 14) length = 14;
803 memset(buf,0,
sizeof(buf));
804 for(i=0; i<length; i++) buf[i] = text[i];
807 cptr = strchr(name,
'/');
810 ::printf(
"USER_ERROR: rlEIBnetIP::setText() wrong name=%s text=%s\n", name, text);
813 sscanf(cptr,
"/%d/%d/%d",&a1,&a2,&a3);
814 daddr = ((a1*8)+a2)*256+a3;
829 tel.
saddr = (
unsigned short) htons((
short)
saddr);
830 tel.
daddr = (
unsigned short) htons((
short) daddr);
833 for(i=0; i<14; i++) tel.
val[i] = buf[i];
855 if(
debug)
::printf(
"rlEIBnetIP::setText(%s) timeout\n",text);
861 ::printf(
"rlEIBnetIP::setText() connection lost\n");
872 if(memptr[i].mc == 0)
874 if(
debug)
::printf(
"rlEIBnetIP::setText() insert new value\n");
876 memcpy(&memptr[i],&tel,
sizeof(tel));
881 else if(memptr[i].daddr == tel.
daddr)
883 if(
debug)
::printf(
"rlEIBnetIP::setText() update existing value\n");
885 memcpy(&memptr[i],&tel,
sizeof(tel));
890 ::printf(
"ERROR rlEIBnetIP::setText() not enough signals specified max=%d\n",
maxvalues);
897 unsigned int i,a1,a2,a3,daddr;
905 cptr = strchr(name,
'/');
908 ::printf(
"USER_ERROR: rlEIBnetIP::setValue() wrong name=%s val=%d\n", name, val);
911 sscanf(cptr,
"/%d/%d/%d",&a1,&a2,&a3);
912 daddr = ((a1*8)+a2)*256+a3;
931 tel.
saddr = (
unsigned short) htons((
short)
saddr);
932 tel.
daddr = (
unsigned short) htons((
short) daddr);
936 if(addi1 != -1) tel.
addi1 = (
unsigned char) ((addi1 & 0x0ff));
939 tel.
ctrl1 = (
unsigned char) ((ctrl & 0x0ff00)/256);
940 tel.
ctrl2 = (
unsigned char) ((ctrl & 0x0ff));
942 if(length != -1) tel.
apci_length = (
unsigned char) length;
943 if(apci != -1) tel.
apci = (
unsigned char) apci;
945 if(length == -1 || length == 1)
947 tel.
val[0] = (
unsigned char) val;
948 tel.
val[1] = (
unsigned char) 0x0;
949 tel.
val[2] = (
unsigned char) 0x0;
950 tel.
val[3] = (
unsigned char) 0x0;
954 tel.
val[0] = (
unsigned char) ((val & 0x0ff00)/256);
955 tel.
val[1] = (
unsigned char) (val & 0x0ff);
956 tel.
val[2] = (
unsigned char) 0x0;
957 tel.
val[3] = (
unsigned char) 0x0;
961 tel.
val[0] = (
unsigned char) ((val & 0x0ff000000)/(256*256));
962 tel.
val[1] = (
unsigned char) (val & 0x0ff0000)/256;
963 tel.
val[2] = (
unsigned char) (val & 0x0ff00);
964 tel.
val[3] = (
unsigned char) (val & 0x0ff);
968 tel.
val[0] = (
unsigned char) ((val & 0x0ff000000)/(256*256*256));
969 tel.
val[1] = (
unsigned char) ((val & 0x0ff0000)/(256*256));
970 tel.
val[2] = (
unsigned char) ((val & 0x0ff00)/(256));
971 tel.
val[3] = (
unsigned char) (val & 0x0ff);
975 ::printf(
"rlEIBnetIP::setValue(0x%x) unknown length=%d\n",val,length);
982 ::printf(
"rlEIBnetIP::setValue() mc = 0x%x\n", tel.
mc);
983 ::printf(
"rlEIBnetIP::setValue() addi1 = 0x%x\n", tel.
addi1);
984 ::printf(
"rlEIBnetIP::setValue() ctrl1 = 0x%x\n", tel.
ctrl1);
985 ::printf(
"rlEIBnetIP::setValue() ctrl2 = 0x%x\n", tel.
ctrl2);
986 ::printf(
"rlEIBnetIP::setValue() saddr = 0x%x\n", ntohs(tel.
saddr));
987 ::printf(
"rlEIBnetIP::setValue() daddr = 0x%x\n", ntohs(tel.
daddr));
989 ::printf(
"rlEIBnetIP::setValue() apci = 0x%x\n", tel.
apci);
990 ::printf(
"rlEIBnetIP::setValue() val[0] = 0x%x\n", tel.
val[0]);
991 ::printf(
"rlEIBnetIP::setValue() val[1] = 0x%x\n", tel.
val[1]);
992 ::printf(
"rlEIBnetIP::setValue() val[2] = 0x%x\n", tel.
val[2]);
993 ::printf(
"rlEIBnetIP::setValue() val[3] = 0x%x\n", tel.
val[3]);
1010 if(
debug)
::printf(
"rlEIBnetIP::setValue(0x%x) NACK\n",val);
1015 if(
debug)
::printf(
"rlEIBnetIP::setValue(0x%x) timeout\n",val);
1021 ::printf(
"rlEIBnetIP::setValue() connection lost\n");
1033 for(i=0; i< (
unsigned int)
maxvalues; i++)
1035 if(memptr[i].mc == 0)
1037 if(
debug)
::printf(
"rlEIBnetIP::setValue(0x%x) insert new value\n",val);
1039 memcpy(&memptr[i],&tel,
sizeof(tel));
1044 else if(memptr[i].daddr == tel.
daddr)
1046 if(
debug)
::printf(
"rlEIBnetIP::setValue(0x%x) update existing value\n",val);
1048 memcpy(&memptr[i],&tel,
sizeof(tel));
1053 ::printf(
"ERROR rlEIBnetIP::setValue() not enough signals specified max=%d\n",
maxvalues);
1067 if(
debug)
::printf(
"rlEIBnetIP::setServer() found server\n");
1073 ::printf(
"rlEIBnetIP::setServer() could not find server\n");
1081 unsigned char murx1[8], murx2[8];
1082 memcpy(murx1,&_client->
address,
sizeof(murx1));
1083 murx2[0] = murx1[0];
1084 murx2[1] = murx1[1];
1085 murx2[6] = murx1[2];
1086 murx2[7] = murx1[3];
1087 murx2[2] = murx1[4];
1088 murx2[3] = murx1[5];
1089 murx2[4] = murx1[6];
1090 murx2[5] = murx1[7];
1101 if(ret < 0)
return ret;
1125 if(
debug)
::printf(
"rlEIBnetIP()::getDescription() timeout\n");
1130 if(
debug)
::printf(
"rlEIBnetIP()::getDescription() got description\n");
1150 ret = eib.setClient(&client);
1151 if(ret < 0)
return -1;
1152 ret = eib.setServer(&server);
1153 if(ret < 0)
return -1;
1161 if(line[0] ==
'x')
break;
1166 else if(line[0] ==
's')
1168 eib.setValuesFromCSV(
"test.csv");
1170 else if(line[0] ==
't')
1172 eib.setText(
"/0/1/000",
"test");
1176 sscanf(line,
"%x",&val);
1177 eib.setValue(
"/0/1/000", val);
1178 printf(
"eib.value(/0/1/000)=0x%x\n",eib.value(
"/0/1/000"));
1180 printf(
"x=exit d=dump s=set t=text value=\n");
static void * eib_reader(void *arg)
int setValue(const char *name, int val, int length=-1, int addi1=-1, int ctrl=-1, int apci=-1)
int printTelegram(EIB_TEL *tel)
int sendDisconnectRequest()
#define TUNNEL_CONNECTION
int setSourceAdr(const char *adr)
int storeInProvider(EIB_TEL *tel)
unsigned short servicetype
int value(const char *name)
unsigned int valueUnsigned(const char *name)
int recvfrom(void *buf, int maxlen, rlIpAdr *source, int timeout=-1)
int recv(void *buf, int maxlen)
float valueFloat2(const char *name)
int create(void *(*func)(void *), void *argument)
int getDescription(PDU *pdu)
int printf(rlIpAdr *dest, const char *format,...)
unsigned char data[128-6]
int getText(const char *name, char *text, int maxlen)
int setAdr(const char *adr, int port)
int setServer(rlIpAdr *server)
unsigned char apci_length
float valueFloat4(const char *name)
rlEIBnetIP(int num_signals=1000, int debug=0, rlDataAcquisitionProvider *provider=NULL)
int setValueFloat(const char *name, float val, int length=-1, int addi1=-1, int ctrl=-1, int apci=-1)
int storeBuffer(unsigned char *buf, int len)
int setClient(rlIpAdr *client)
int sendto(const void *buf, int len, rlIpAdr *dest)
int setValuesFromCSV(const char *filename)
int setText(const char *name, const char *text)
int setValueUnsigned(const char *name, unsigned int val, int length=-1, int addi1=-1, int ctrl=-1, int apci=-1)
int setStringValue(const char *variable, const char *value)
struct sockaddr_in address
rlDataAcquisitionProvider * provider