3 Copyright (C) 2005-2007 Martin Kögler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 In addition to the permissions in the GNU General Public License,
11 you may link the compiled version of this file into combinations
12 with other programs, and distribute those combinations without any
13 restriction coming from the use of this file. (The General Public
14 License restrictions do apply in other respects; for example, they
15 cover modification of the file, and distribution when not linked into
16 a combine executable.)
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/socket.h>
32 #include <netinet/in.h>
38 #include "eibclient.h"
42 typedef uint8_t uchar
;
44 /** EIB Connection internal */
47 int (*complete
) (EIBConnection
*);
48 /** file descriptor */
68 /** extracts TYPE code of an eibd packet */
69 #define EIBTYPE(con) (((con)->buf[0]<<8)|((con)->buf[1]))
70 /** sets TYPE code for an eibd packet*/
71 #define EIBSETTYPE(buf,type) do{(buf)[0]=(type>>8)&0xff;(buf)[1]=(type)&0xff;}while(0)
73 /** set EIB address */
74 #define EIBSETADDR(buf,type) do{(buf)[0]=(type>>8)&0xff;(buf)[1]=(type)&0xff;}while(0)
76 /** resolve host name */
78 GetHostIP (struct sockaddr_in
*sock
, const char *Name
)
80 #ifdef HAVE_GETHOSTBYNAME_R
83 char *buf
= (char *) malloc (len
);
90 memset (sock
, 0, sizeof (*sock
));
91 #ifdef HAVE_GETHOSTBYNAME_R
94 res
= gethostbyname_r (Name
, &host
, buf
, len
, &h
, &err
);
98 buf
= (char *) realloc (buf
, len
);
103 while (res
== ERANGE
);
111 h
= gethostbyname (Name
);
115 sock
->sin_family
= h
->h_addrtype
;
116 sock
->sin_addr
.s_addr
= (*((unsigned long *) h
->h_addr_list
[0]));
117 #ifdef HAVE_GETHOSTBYNAME_R
125 EIBClose (EIBConnection
* con
)
140 EIBSocketLocal (const char *path
)
142 EIBConnection
*con
= (EIBConnection
*) malloc (sizeof (EIBConnection
));
143 struct sockaddr_un addr
;
149 addr
.sun_family
= AF_LOCAL
;
150 strncpy (addr
.sun_path
, path
, sizeof (addr
.sun_path
));
151 addr
.sun_path
[sizeof (addr
.sun_path
) - 1] = 0;
153 con
->fd
= socket (AF_LOCAL
, SOCK_STREAM
, 0);
160 if (connect (con
->fd
, (struct sockaddr
*) &addr
, sizeof (addr
)) == -1)
176 EIBSocketRemote (const char *host
, int port
)
178 EIBConnection
*con
= (EIBConnection
*) malloc (sizeof (EIBConnection
));
179 struct sockaddr_in addr
;
186 if (!GetHostIP (&addr
, host
))
189 errno
= ECONNREFUSED
;
192 addr
.sin_port
= htons (port
);
194 con
->fd
= socket (addr
.sin_family
, SOCK_STREAM
, 0);
201 if (connect (con
->fd
, (struct sockaddr
*) &addr
, sizeof (addr
)) == -1)
217 EIBSocketURL (const char *url
)
224 if (!strncmp (url
, "local:", 6))
226 return EIBSocketLocal (url
+ 6);
228 if (!strncmp (url
, "ip:", 3))
230 char *a
= strdup (url
+ 3);
249 c
= EIBSocketRemote (a
, port
);
257 /** send a request to eibd */
259 _EIB_SendRequest (EIBConnection
* con
, unsigned int size
, uchar
* data
)
264 if (size
> 0xffff || size
< 2)
269 head
[0] = (size
>> 8) & 0xff;
270 head
[1] = (size
) & 0xff;
273 i
= write (con
->fd
, &head
, 2);
274 if (i
== -1 && errno
== EINTR
)
285 i
= write (con
->fd
, data
+ start
, size
- start
);
286 if (i
== -1 && errno
== EINTR
)
302 _EIB_CheckRequest (EIBConnection
* con
, int block
)
313 FD_SET (con
->fd
, &readset
);
314 if (select (con
->fd
+ 1, &readset
, 0, 0, &tv
) == -1)
317 if (!FD_ISSET (con
->fd
, &readset
))
321 if (con
->readlen
< 2)
324 head
[0] = (con
->size
>> 8) & 0xff;
325 i
= read (con
->fd
, &head
+ con
->readlen
, 2 - con
->readlen
);
326 if (i
== -1 && errno
== EINTR
)
336 con
->size
= (head
[0] << 8) | (head
[1]);
343 if (con
->size
> con
->buflen
)
345 con
->buf
= (uchar
*) realloc (con
->buf
, con
->size
);
352 con
->buflen
= con
->size
;
357 if (con
->readlen
< con
->size
+ 2)
360 read (con
->fd
, con
->buf
+ (con
->readlen
- 2),
361 con
->size
- (con
->readlen
- 2));
362 if (i
== -1 && errno
== EINTR
)
376 /** receive packet from eibd */
378 _EIB_GetRequest (EIBConnection
* con
)
382 if (_EIB_CheckRequest (con
, 1) == -1)
385 while (con
->readlen
< 2
386 || (con
->readlen
>= 2 && con
->readlen
< con
->size
+ 2));
394 EIB_Poll_Complete (EIBConnection
* con
)
401 if (_EIB_CheckRequest (con
, 0) == -1)
403 return (con
->readlen
>= 2 && con
->readlen
>= con
->size
+ 2) ? 1 : 0;
407 EIB_Poll_FD (EIBConnection
* con
)
418 EIBComplete (EIBConnection
* con
)
425 return con
->complete (con
);
429 OpenBusmonitor_complete (EIBConnection
* con
)
432 i
= _EIB_GetRequest (con
);
436 if (EIBTYPE (con
) == EIB_CONNECTION_INUSE
)
441 if (EIBTYPE (con
) != EIB_OPEN_BUSMONITOR
)
450 EIBOpenBusmonitor_async (EIBConnection
* con
)
459 EIBSETTYPE (head
, EIB_OPEN_BUSMONITOR
);
460 i
= _EIB_SendRequest (con
, 2, head
);
464 con
->complete
= OpenBusmonitor_complete
;
469 EIBOpenBusmonitor (EIBConnection
* con
)
471 if (EIBOpenBusmonitor_async (con
) == -1)
473 return EIBComplete (con
);
477 OpenBusmonitorText_complete (EIBConnection
* con
)
480 i
= _EIB_GetRequest (con
);
484 if (EIBTYPE (con
) == EIB_CONNECTION_INUSE
)
489 if (EIBTYPE (con
) != EIB_OPEN_BUSMONITOR_TEXT
)
498 EIBOpenBusmonitorText_async (EIBConnection
* con
)
507 EIBSETTYPE (head
, EIB_OPEN_BUSMONITOR_TEXT
);
508 i
= _EIB_SendRequest (con
, 2, head
);
511 con
->complete
= OpenBusmonitorText_complete
;
516 EIBOpenBusmonitorText (EIBConnection
* con
)
518 if (EIBOpenBusmonitorText_async (con
) == -1)
520 return EIBComplete (con
);
524 OpenVBusmonitor_complete (EIBConnection
* con
)
527 i
= _EIB_GetRequest (con
);
531 if (EIBTYPE (con
) == EIB_CONNECTION_INUSE
)
536 if (EIBTYPE (con
) != EIB_OPEN_VBUSMONITOR
)
545 EIBOpenVBusmonitor_async (EIBConnection
* con
)
554 EIBSETTYPE (head
, EIB_OPEN_VBUSMONITOR
);
555 i
= _EIB_SendRequest (con
, 2, head
);
558 con
->complete
= OpenVBusmonitor_complete
;
563 EIBOpenVBusmonitor (EIBConnection
* con
)
565 if (EIBOpenVBusmonitor_async (con
) == -1)
567 return EIBComplete (con
);
571 OpenVBusmonitorText_complete (EIBConnection
* con
)
574 i
= _EIB_GetRequest (con
);
578 if (EIBTYPE (con
) == EIB_CONNECTION_INUSE
)
583 if (EIBTYPE (con
) != EIB_OPEN_VBUSMONITOR_TEXT
)
592 EIBOpenVBusmonitorText_async (EIBConnection
* con
)
601 EIBSETTYPE (head
, EIB_OPEN_VBUSMONITOR_TEXT
);
602 i
= _EIB_SendRequest (con
, 2, head
);
605 con
->complete
= OpenVBusmonitorText_complete
;
610 EIBOpenVBusmonitorText (EIBConnection
* con
)
612 if (EIBOpenVBusmonitorText_async (con
) == -1)
614 return EIBComplete (con
);
618 EIBGetBusmonitorPacket (EIBConnection
* con
, int maxlen
, uint8_t * buf
)
627 i
= _EIB_GetRequest (con
);
631 if (EIBTYPE (con
) != EIB_BUSMONITOR_PACKET
)
639 memcpy (buf
, con
->buf
+ 2, i
);
644 OpenT_Connection_complete (EIBConnection
* con
)
647 i
= _EIB_GetRequest (con
);
651 if (EIBTYPE (con
) != EIB_OPEN_T_CONNECTION
)
660 EIBOpenT_Connection_async (EIBConnection
* con
, eibaddr_t dest
)
669 EIBSETTYPE (head
, EIB_OPEN_T_CONNECTION
);
670 EIBSETADDR (head
+ 2, dest
);
671 i
= _EIB_SendRequest (con
, 5, head
);
674 con
->complete
= OpenT_Connection_complete
;
679 EIBOpenT_Connection (EIBConnection
* con
, eibaddr_t dest
)
681 if (EIBOpenT_Connection_async (con
, dest
) == -1)
683 return EIBComplete (con
);
687 OpenT_TPDU_complete (EIBConnection
* con
)
690 i
= _EIB_GetRequest (con
);
694 if (EIBTYPE (con
) != EIB_OPEN_T_TPDU
)
703 EIBOpenT_TPDU_async (EIBConnection
* con
, eibaddr_t src
)
712 EIBSETTYPE (head
, EIB_OPEN_T_TPDU
);
713 EIBSETADDR (head
+ 2, src
);
714 i
= _EIB_SendRequest (con
, 5, head
);
717 con
->complete
= OpenT_TPDU_complete
;
722 EIBOpenT_TPDU (EIBConnection
* con
, eibaddr_t src
)
724 if (EIBOpenT_TPDU_async (con
, src
) == -1)
726 return EIBComplete (con
);
730 OpenT_Individual_complete (EIBConnection
* con
)
733 i
= _EIB_GetRequest (con
);
737 if (EIBTYPE (con
) != EIB_OPEN_T_INDIVIDUAL
)
746 EIBOpenT_Individual_async (EIBConnection
* con
, eibaddr_t dest
,
756 EIBSETTYPE (head
, EIB_OPEN_T_INDIVIDUAL
);
757 EIBSETADDR (head
+ 2, dest
);
758 head
[4] = (write_only
? 0xff : 0);
759 i
= _EIB_SendRequest (con
, 5, head
);
762 con
->complete
= OpenT_Individual_complete
;
767 EIBOpenT_Individual (EIBConnection
* con
, eibaddr_t dest
, int write_only
)
769 if (EIBOpenT_Individual_async (con
, dest
, write_only
) == -1)
771 return EIBComplete (con
);
775 OpenT_Group_complete (EIBConnection
* con
)
778 i
= _EIB_GetRequest (con
);
782 if (EIBTYPE (con
) != EIB_OPEN_T_GROUP
)
791 EIBOpenT_Group_async (EIBConnection
* con
, eibaddr_t dest
, int write_only
)
800 EIBSETTYPE (head
, EIB_OPEN_T_GROUP
);
801 EIBSETADDR (head
+ 2, dest
);
802 head
[4] = (write_only
? 0xff : 0);
803 i
= _EIB_SendRequest (con
, 5, head
);
806 con
->complete
= OpenT_Group_complete
;
811 EIBOpenT_Group (EIBConnection
* con
, eibaddr_t dest
, int write_only
)
813 if (EIBOpenT_Group_async (con
, dest
, write_only
) == -1)
815 return EIBComplete (con
);
819 OpenT_Broadcast_complete (EIBConnection
* con
)
822 i
= _EIB_GetRequest (con
);
826 if (EIBTYPE (con
) != EIB_OPEN_T_BROADCAST
)
835 EIBOpenT_Broadcast_async (EIBConnection
* con
, int write_only
)
844 EIBSETTYPE (head
, EIB_OPEN_T_BROADCAST
);
845 head
[4] = (write_only
? 0xff : 0);
846 i
= _EIB_SendRequest (con
, 5, head
);
849 con
->complete
= OpenT_Broadcast_complete
;
854 EIBOpenT_Broadcast (EIBConnection
* con
, int write_only
)
856 if (EIBOpenT_Broadcast_async (con
, write_only
) == -1)
858 return EIBComplete (con
);
862 EIBSendTPDU (EIBConnection
* con
, eibaddr_t dest
, int len
, uint8_t * data
)
871 if (len
< 2 || !data
)
876 ibuf
= (uchar
*) malloc (len
+ 4);
882 EIBSETTYPE (ibuf
, EIB_APDU_PACKET
);
883 EIBSETADDR (ibuf
+ 2, dest
);
884 memcpy (ibuf
+ 4, data
, len
);
885 i
= _EIB_SendRequest (con
, len
+ 4, ibuf
);
891 EIBSendAPDU (EIBConnection
* con
, int len
, uint8_t * data
)
900 if (len
< 2 || !data
)
905 ibuf
= (uchar
*) malloc (len
+ 2);
911 EIBSETTYPE (ibuf
, EIB_APDU_PACKET
);
912 memcpy (ibuf
+ 2, data
, len
);
913 i
= _EIB_SendRequest (con
, len
+ 2, ibuf
);
919 EIBGetAPDU (EIBConnection
* con
, int maxlen
, uint8_t * buf
)
928 i
= _EIB_GetRequest (con
);
932 if (EIBTYPE (con
) != EIB_APDU_PACKET
)
940 memcpy (buf
, con
->buf
+ 2, i
);
945 EIBGetAPDU_Src (EIBConnection
* con
, int maxlen
, uint8_t * buf
,
955 i
= _EIB_GetRequest (con
);
959 if (EIBTYPE (con
) != EIB_APDU_PACKET
|| con
->size
< 4)
967 memcpy (buf
, con
->buf
+ 4, i
);
969 *src
= (con
->buf
[2] << 8) | (con
->buf
[3]);
974 Open_GroupSocket_complete (EIBConnection
* con
)
977 i
= _EIB_GetRequest (con
);
981 if (EIBTYPE (con
) != EIB_OPEN_GROUPCON
)
990 EIBOpen_GroupSocket_async (EIBConnection
* con
, int write_only
)
999 EIBSETTYPE (head
, EIB_OPEN_GROUPCON
);
1000 head
[4] = (write_only
? 0xff : 0);
1001 i
= _EIB_SendRequest (con
, 5, head
);
1004 con
->complete
= Open_GroupSocket_complete
;
1009 EIBOpen_GroupSocket (EIBConnection
* con
, int write_only
)
1011 if (EIBOpen_GroupSocket_async (con
, write_only
) == -1)
1013 return EIBComplete (con
);
1017 EIBGetGroup_Src (EIBConnection
* con
, int maxlen
, uint8_t * buf
,
1018 eibaddr_t
* src
, eibaddr_t
* dest
)
1027 i
= _EIB_GetRequest (con
);
1031 if (EIBTYPE (con
) != EIB_GROUP_PACKET
|| con
->size
< 6)
1039 memcpy (buf
, con
->buf
+ 6, i
);
1041 *src
= (con
->buf
[2] << 8) | (con
->buf
[3]);
1043 *dest
= (con
->buf
[4] << 8) | (con
->buf
[5]);
1048 EIBSendGroup (EIBConnection
* con
, eibaddr_t dest
, int len
, uint8_t * data
)
1057 if (len
< 2 || !data
)
1062 ibuf
= (uchar
*) malloc (len
+ 4);
1068 EIBSETTYPE (ibuf
, EIB_GROUP_PACKET
);
1069 EIBSETADDR (ibuf
+ 2, dest
);
1070 memcpy (ibuf
+ 4, data
, len
);
1071 i
= _EIB_SendRequest (con
, len
+ 4, ibuf
);
1077 M_ReadIndividualAddresses_complete (EIBConnection
* con
)
1080 i
= _EIB_GetRequest (con
);
1083 if (EIBTYPE (con
) != EIB_M_INDIVIDUAL_ADDRESS_READ
)
1089 if (i
> con
->req
.len
)
1091 memcpy (con
->req
.buf
, con
->buf
+ 2, i
);
1096 EIB_M_ReadIndividualAddresses_async (EIBConnection
* con
, int maxlen
,
1105 con
->req
.len
= maxlen
;
1107 EIBSETTYPE (head
, EIB_M_INDIVIDUAL_ADDRESS_READ
);
1108 if (_EIB_SendRequest (con
, 2, head
) == -1)
1110 con
->complete
= M_ReadIndividualAddresses_complete
;
1115 EIB_M_ReadIndividualAddresses (EIBConnection
* con
, int maxlen
, uint8_t * buf
)
1117 if (EIB_M_ReadIndividualAddresses_async (con
, maxlen
, buf
) == -1)
1119 return EIBComplete (con
);
1123 M_Progmode_On_complete (EIBConnection
* con
)
1126 i
= _EIB_GetRequest (con
);
1129 if (EIBTYPE (con
) != EIB_PROG_MODE
)
1138 EIB_M_Progmode_On_async (EIBConnection
* con
, eibaddr_t dest
)
1146 EIBSETTYPE (head
, EIB_PROG_MODE
);
1147 EIBSETADDR (head
+ 2, dest
);
1149 if (_EIB_SendRequest (con
, 5, head
) == -1)
1151 con
->complete
= M_Progmode_On_complete
;
1156 EIB_M_Progmode_On (EIBConnection
* con
, eibaddr_t dest
)
1158 if (EIB_M_Progmode_On_async (con
, dest
) == -1)
1160 return EIBComplete (con
);
1164 M_Progmode_Off_complete (EIBConnection
* con
)
1167 i
= _EIB_GetRequest (con
);
1170 if (EIBTYPE (con
) != EIB_PROG_MODE
)
1179 EIB_M_Progmode_Off_async (EIBConnection
* con
, eibaddr_t dest
)
1187 EIBSETTYPE (head
, EIB_PROG_MODE
);
1188 EIBSETADDR (head
+ 2, dest
);
1190 if (_EIB_SendRequest (con
, 5, head
) == -1)
1192 con
->complete
= M_Progmode_Off_complete
;
1197 EIB_M_Progmode_Off (EIBConnection
* con
, eibaddr_t dest
)
1199 if (EIB_M_Progmode_Off_async (con
, dest
) == -1)
1201 return EIBComplete (con
);
1205 M_Progmode_Toggle_complete (EIBConnection
* con
)
1208 i
= _EIB_GetRequest (con
);
1211 if (EIBTYPE (con
) != EIB_PROG_MODE
)
1220 EIB_M_Progmode_Toggle_async (EIBConnection
* con
, eibaddr_t dest
)
1228 EIBSETTYPE (head
, EIB_PROG_MODE
);
1229 EIBSETADDR (head
+ 2, dest
);
1231 if (_EIB_SendRequest (con
, 5, head
) == -1)
1233 con
->complete
= M_Progmode_Toggle_complete
;
1238 EIB_M_Progmode_Toggle (EIBConnection
* con
, eibaddr_t dest
)
1240 if (EIB_M_Progmode_Toggle (con
, dest
) == -1)
1242 return EIBComplete (con
);
1246 M_Progmode_Status_complete (EIBConnection
* con
)
1249 i
= _EIB_GetRequest (con
);
1252 if (EIBTYPE (con
) != EIB_PROG_MODE
|| con
->size
< 3)
1261 EIB_M_Progmode_Status_async (EIBConnection
* con
, eibaddr_t dest
)
1269 EIBSETTYPE (head
, EIB_PROG_MODE
);
1270 EIBSETADDR (head
+ 2, dest
);
1272 if (_EIB_SendRequest (con
, 5, head
) == -1)
1274 con
->complete
= M_Progmode_Status_complete
;
1279 EIB_M_Progmode_Status (EIBConnection
* con
, eibaddr_t dest
)
1281 if (EIB_M_Progmode_Status (con
, dest
) == -1)
1283 return EIBComplete (con
);
1287 M_GetMaskVersion_complete (EIBConnection
* con
)
1290 i
= _EIB_GetRequest (con
);
1293 if (EIBTYPE (con
) != EIB_MASK_VERSION
|| con
->size
< 4)
1298 return (con
->buf
[2] << 8) | (con
->buf
[3]);
1302 EIB_M_GetMaskVersion_async (EIBConnection
* con
, eibaddr_t dest
)
1310 EIBSETTYPE (head
, EIB_MASK_VERSION
);
1311 EIBSETADDR (head
+ 2, dest
);
1312 if (_EIB_SendRequest (con
, 4, head
) == -1)
1314 con
->complete
= M_GetMaskVersion_complete
;
1319 EIB_M_GetMaskVersion (EIBConnection
* con
, eibaddr_t dest
)
1321 if (EIB_M_GetMaskVersion_async (con
, dest
) == -1)
1323 return EIBComplete (con
);
1327 M_WriteIndividualAddress_complete (EIBConnection
* con
)
1330 i
= _EIB_GetRequest (con
);
1333 if (EIBTYPE (con
) == EIB_ERROR_ADDR_EXISTS
)
1338 if (EIBTYPE (con
) == EIB_M_INDIVIDUAL_ADDRESS_WRITE
)
1342 if (EIBTYPE (con
) == EIB_ERROR_TIMEOUT
)
1347 if (EIBTYPE (con
) == EIB_ERROR_MORE_DEVICE
)
1349 errno
= EADDRNOTAVAIL
;
1357 EIB_M_WriteIndividualAddress_async (EIBConnection
* con
, eibaddr_t dest
)
1365 EIBSETTYPE (head
, EIB_M_INDIVIDUAL_ADDRESS_WRITE
);
1366 EIBSETADDR (head
+ 2, dest
);
1367 if (_EIB_SendRequest (con
, 4, head
) == -1)
1369 con
->complete
= M_WriteIndividualAddress_complete
;
1374 EIB_M_WriteIndividualAddress (EIBConnection
* con
, eibaddr_t dest
)
1376 if (EIB_M_WriteIndividualAddress_async (con
, dest
) == -1)
1378 return EIBComplete (con
);
1382 MC_Connect_complete (EIBConnection
* con
)
1385 i
= _EIB_GetRequest (con
);
1389 if (EIBTYPE (con
) != EIB_MC_CONNECTION
)
1398 EIB_MC_Connect_async (EIBConnection
* con
, eibaddr_t dest
)
1407 EIBSETTYPE (head
, EIB_MC_CONNECTION
);
1408 EIBSETADDR (head
+ 2, dest
);
1409 i
= _EIB_SendRequest (con
, 4, head
);
1413 con
->complete
= MC_Connect_complete
;
1418 EIB_MC_Connect (EIBConnection
* con
, eibaddr_t dest
)
1420 if (EIB_MC_Connect_async (con
, dest
) == -1)
1422 return EIBComplete (con
);
1426 MC_Progmode_On_complete (EIBConnection
* con
)
1429 i
= _EIB_GetRequest (con
);
1432 if (EIBTYPE (con
) != EIB_MC_PROG_MODE
)
1441 EIB_MC_Progmode_On_async (EIBConnection
* con
)
1449 EIBSETTYPE (head
, EIB_MC_PROG_MODE
);
1451 if (_EIB_SendRequest (con
, 3, head
) == -1)
1453 con
->complete
= MC_Progmode_On_complete
;
1458 EIB_MC_Progmode_On (EIBConnection
* con
)
1460 if (EIB_MC_Progmode_On_async (con
) == -1)
1462 return EIBComplete (con
);
1466 MC_Progmode_Off_complete (EIBConnection
* con
)
1469 i
= _EIB_GetRequest (con
);
1472 if (EIBTYPE (con
) != EIB_MC_PROG_MODE
)
1481 EIB_MC_Progmode_Off_async (EIBConnection
* con
)
1489 EIBSETTYPE (head
, EIB_MC_PROG_MODE
);
1491 if (_EIB_SendRequest (con
, 3, head
) == -1)
1493 con
->complete
= MC_Progmode_Off_complete
;
1498 EIB_MC_Progmode_Off (EIBConnection
* con
)
1500 if (EIB_MC_Progmode_Off_async (con
) == -1)
1502 return EIBComplete (con
);
1506 MC_Progmode_Toggle_complete (EIBConnection
* con
)
1509 i
= _EIB_GetRequest (con
);
1512 if (EIBTYPE (con
) != EIB_MC_PROG_MODE
)
1521 EIB_MC_Progmode_Toggle_async (EIBConnection
* con
)
1529 EIBSETTYPE (head
, EIB_MC_PROG_MODE
);
1531 if (_EIB_SendRequest (con
, 3, head
) == -1)
1533 con
->complete
= MC_Progmode_Toggle_complete
;
1538 EIB_MC_Progmode_Toggle (EIBConnection
* con
)
1540 if (EIB_MC_Progmode_Toggle_async (con
) == -1)
1542 return EIBComplete (con
);
1546 MC_Progmode_Status_complete (EIBConnection
* con
)
1549 i
= _EIB_GetRequest (con
);
1552 if (EIBTYPE (con
) != EIB_MC_PROG_MODE
|| con
->size
< 3)
1561 EIB_MC_Progmode_Status_async (EIBConnection
* con
)
1569 EIBSETTYPE (head
, EIB_MC_PROG_MODE
);
1571 if (_EIB_SendRequest (con
, 3, head
) == -1)
1573 con
->complete
= MC_Progmode_Status_complete
;
1578 EIB_MC_Progmode_Status (EIBConnection
* con
)
1580 if (EIB_MC_Progmode_Status_async (con
) == -1)
1582 return EIBComplete (con
);
1586 MC_GetMaskVersion_complete (EIBConnection
* con
)
1589 i
= _EIB_GetRequest (con
);
1592 if (EIBTYPE (con
) != EIB_MC_MASK_VERSION
|| con
->size
< 4)
1597 return (con
->buf
[2] << 8) | (con
->buf
[3]);
1601 EIB_MC_GetMaskVersion_async (EIBConnection
* con
)
1609 EIBSETTYPE (head
, EIB_MC_MASK_VERSION
);
1610 if (_EIB_SendRequest (con
, 2, head
) == -1)
1612 con
->complete
= MC_GetMaskVersion_complete
;
1617 EIB_MC_GetMaskVersion (EIBConnection
* con
)
1619 if (EIB_MC_GetMaskVersion_async (con
) == -1)
1621 return EIBComplete (con
);
1625 MC_GetPEIType_complete (EIBConnection
* con
)
1628 i
= _EIB_GetRequest (con
);
1631 if (EIBTYPE (con
) != EIB_MC_PEI_TYPE
|| con
->size
< 4)
1636 return (con
->buf
[2] << 8) | (con
->buf
[3]);
1640 EIB_MC_GetPEIType_async (EIBConnection
* con
)
1648 EIBSETTYPE (head
, EIB_MC_PEI_TYPE
);
1649 if (_EIB_SendRequest (con
, 2, head
) == -1)
1651 con
->complete
= MC_GetPEIType_complete
;
1656 EIB_MC_GetPEIType (EIBConnection
* con
)
1658 if (EIB_MC_GetPEIType_async (con
) == -1)
1660 return EIBComplete (con
);
1664 MC_ReadADC_complete (EIBConnection
* con
)
1667 i
= _EIB_GetRequest (con
);
1670 if (EIBTYPE (con
) != EIB_MC_ADC_READ
|| con
->size
< 4)
1676 *con
->req
.ptr1
= (con
->buf
[2] << 8) | (con
->buf
[3]);
1681 EIB_MC_ReadADC_async (EIBConnection
* con
, uint8_t channel
, uint8_t count
,
1690 con
->req
.ptr1
= val
;
1691 EIBSETTYPE (head
, EIB_MC_ADC_READ
);
1694 if (_EIB_SendRequest (con
, 4, head
) == -1)
1696 con
->complete
= MC_ReadADC_complete
;
1701 EIB_MC_ReadADC (EIBConnection
* con
, uint8_t channel
, uint8_t count
,
1704 if (EIB_MC_ReadADC_async (con
, channel
, count
, val
) == -1)
1706 return EIBComplete (con
);
1710 MC_PropertyRead_complete (EIBConnection
* con
)
1713 i
= _EIB_GetRequest (con
);
1716 if (EIBTYPE (con
) != EIB_MC_PROP_READ
)
1722 if (i
> con
->req
.len
)
1724 memcpy (con
->req
.buf
, con
->buf
+ 2, i
);
1729 EIB_MC_PropertyRead_async (EIBConnection
* con
, uint8_t obj
, uint8_t property
,
1730 uint16_t start
, uint8_t nr_of_elem
, int max_len
,
1740 con
->req
.len
= max_len
;
1746 EIBSETTYPE (head
, EIB_MC_PROP_READ
);
1749 head
[4] = (start
>> 8) & 0xff;
1750 head
[5] = (start
) & 0xff;
1751 head
[6] = nr_of_elem
;
1752 if (_EIB_SendRequest (con
, 7, head
) == -1)
1754 con
->complete
= MC_PropertyRead_complete
;
1759 EIB_MC_PropertyRead (EIBConnection
* con
, uint8_t obj
, uint8_t property
,
1760 uint16_t start
, uint8_t nr_of_elem
, int max_len
,
1763 if (EIB_MC_PropertyRead_async
1764 (con
, obj
, property
, start
, nr_of_elem
, max_len
, buf
) == -1)
1766 return EIBComplete (con
);
1770 MC_Read_complete (EIBConnection
* con
)
1773 i
= _EIB_GetRequest (con
);
1776 if (EIBTYPE (con
) != EIB_MC_READ
)
1782 if (i
> con
->req
.len
)
1784 memcpy (con
->req
.buf
, con
->buf
+ 2, i
);
1789 EIB_MC_Read_async (EIBConnection
* con
, uint16_t addr
, int len
, uint8_t * buf
)
1804 EIBSETTYPE (head
, EIB_MC_READ
);
1805 head
[2] = (addr
>> 8) & 0xff;
1806 head
[3] = (addr
) & 0xff;
1807 head
[4] = (len
>> 8) & 0xff;
1808 head
[5] = (len
) & 0xff;
1809 if (_EIB_SendRequest (con
, 6, head
) == -1)
1811 con
->complete
= MC_Read_complete
;
1816 EIB_MC_Read (EIBConnection
* con
, uint16_t addr
, int len
, uint8_t * buf
)
1818 if (EIB_MC_Read_async (con
, addr
, len
, buf
) == -1)
1820 return EIBComplete (con
);
1824 MC_PropertyWrite_complete (EIBConnection
* con
)
1827 i
= _EIB_GetRequest (con
);
1830 if (EIBTYPE (con
) != EIB_MC_PROP_WRITE
)
1836 if (i
> con
->req
.len
)
1838 memcpy (con
->req
.buf
, con
->buf
+ 2, i
);
1843 EIB_MC_PropertyWrite_async (EIBConnection
* con
, uint8_t obj
,
1844 uint8_t property
, uint16_t start
,
1845 uint8_t nr_of_elem
, int len
, const uint8_t * buf
,
1846 int max_len
, uint8_t * res
)
1860 con
->req
.len
= max_len
;
1862 ibuf
= (uchar
*) malloc (len
+ 7);
1868 EIBSETTYPE (ibuf
, EIB_MC_PROP_WRITE
);
1871 ibuf
[4] = (start
>> 8) & 0xff;
1872 ibuf
[5] = (start
) & 0xff;
1873 ibuf
[6] = nr_of_elem
;
1874 memcpy (ibuf
+ 7, buf
, len
);
1875 i
= _EIB_SendRequest (con
, len
+ 7, ibuf
);
1879 con
->complete
= MC_PropertyWrite_complete
;
1884 EIB_MC_PropertyWrite (EIBConnection
* con
, uint8_t obj
, uint8_t property
,
1885 uint16_t start
, uint8_t nr_of_elem
, int len
,
1886 const uint8_t * buf
, int max_len
, uint8_t * res
)
1888 if (EIB_MC_PropertyWrite_async
1889 (con
, obj
, property
, start
, nr_of_elem
, len
, buf
, max_len
, res
) == -1)
1891 return EIBComplete (con
);
1894 MC_Write_complete (EIBConnection
* con
)
1897 i
= _EIB_GetRequest (con
);
1900 if (EIBTYPE (con
) == EIB_ERROR_VERIFY
)
1905 if (EIBTYPE (con
) != EIB_MC_WRITE
)
1910 return con
->req
.len
;
1914 EIB_MC_Write_async (EIBConnection
* con
, uint16_t addr
, int len
,
1915 const uint8_t * buf
)
1930 ibuf
= (uchar
*) malloc (len
+ 6);
1936 EIBSETTYPE (ibuf
, EIB_MC_WRITE
);
1937 ibuf
[2] = (addr
>> 8) & 0xff;
1938 ibuf
[3] = (addr
) & 0xff;
1939 ibuf
[4] = (len
>> 8) & 0xff;
1940 ibuf
[5] = (len
) & 0xff;
1941 memcpy (ibuf
+ 6, buf
, len
);
1942 i
= _EIB_SendRequest (con
, len
+ 6, ibuf
);
1946 con
->complete
= MC_Write_complete
;
1951 EIB_MC_Write (EIBConnection
* con
, uint16_t addr
, int len
,
1952 const uint8_t * buf
)
1954 if (EIB_MC_Write_async (con
, addr
, len
, buf
) == -1)
1956 return EIBComplete (con
);
1960 MC_PropertyDesc_complete (EIBConnection
* con
)
1963 i
= _EIB_GetRequest (con
);
1966 if (EIBTYPE (con
) != EIB_MC_PROP_DESC
|| con
->size
< 6)
1973 *con
->req
.ptr2
= con
->buf
[2];
1974 /* max_nr_of_elem */
1976 *con
->req
.ptr4
= (con
->buf
[3] << 8) | (con
->buf
[4]);
1979 *con
->req
.ptr3
= con
->buf
[5];
1984 EIB_MC_PropertyDesc_async (EIBConnection
* con
, uint8_t obj
, uint8_t property
,
1985 uint8_t * type
, uint16_t * max_nr_of_elem
,
1994 con
->req
.ptr2
= type
;
1995 con
->req
.ptr4
= max_nr_of_elem
;
1996 con
->req
.ptr3
= access
;
1997 EIBSETTYPE (head
, EIB_MC_PROP_DESC
);
2000 if (_EIB_SendRequest (con
, 4, head
) == -1)
2002 con
->complete
= MC_PropertyDesc_complete
;
2007 EIB_MC_PropertyDesc (EIBConnection
* con
, uint8_t obj
, uint8_t property
,
2008 uint8_t * type
, uint16_t * max_nr_of_elem
,
2011 if (EIB_MC_PropertyDesc_async
2012 (con
, obj
, property
, type
, max_nr_of_elem
, access
) == -1)
2014 return EIBComplete (con
);
2018 MC_Authorize_complete (EIBConnection
* con
)
2021 i
= _EIB_GetRequest (con
);
2024 if (EIBTYPE (con
) != EIB_MC_AUTHORIZE
|| con
->size
< 3)
2033 EIB_MC_Authorize_async (EIBConnection
* con
, uint8_t key
[4])
2041 EIBSETTYPE (head
, EIB_MC_AUTHORIZE
);
2042 memcpy (head
+ 2, key
, 4);
2043 if (_EIB_SendRequest (con
, 6, head
) == -1)
2045 con
->complete
= MC_Authorize_complete
;
2050 EIB_MC_Authorize (EIBConnection
* con
, uint8_t key
[4])
2052 if (EIB_MC_Authorize_async (con
, key
) == -1)
2054 return EIBComplete (con
);
2058 MC_SetKey_complete (EIBConnection
* con
)
2061 i
= _EIB_GetRequest (con
);
2064 if (EIBTYPE (con
) == EIB_PROCESSING_ERROR
)
2069 if (EIBTYPE (con
) != EIB_MC_KEY_WRITE
)
2078 EIB_MC_SetKey_async (EIBConnection
* con
, uint8_t key
[4], uint8_t level
)
2086 EIBSETTYPE (head
, EIB_MC_KEY_WRITE
);
2087 memcpy (head
+ 2, key
, 4);
2089 if (_EIB_SendRequest (con
, 7, head
) == -1)
2091 con
->complete
= MC_SetKey_complete
;
2096 EIB_MC_SetKey (EIBConnection
* con
, uint8_t key
[4], uint8_t level
)
2098 if (EIB_MC_SetKey_async (con
, key
, level
) == -1)
2100 return EIBComplete (con
);
2104 MC_PropertyScan_complete (EIBConnection
* con
)
2107 i
= _EIB_GetRequest (con
);
2110 if (EIBTYPE (con
) != EIB_MC_PROP_SCAN
)
2116 if (i
> con
->req
.len
)
2118 memcpy (con
->req
.buf
, con
->buf
+ 2, i
);
2123 EIB_MC_PropertyScan_async (EIBConnection
* con
, int maxlen
, uint8_t * buf
)
2131 con
->req
.len
= maxlen
;
2133 EIBSETTYPE (head
, EIB_MC_PROP_SCAN
);
2134 if (_EIB_SendRequest (con
, 2, head
) == -1)
2136 con
->complete
= MC_PropertyScan_complete
;
2141 EIB_MC_PropertyScan (EIBConnection
* con
, int maxlen
, uint8_t * buf
)
2143 if (EIB_MC_PropertyScan_async (con
, maxlen
, buf
) == -1)
2145 return EIBComplete (con
);
2149 LoadImage_complete (EIBConnection
* con
)
2152 i
= _EIB_GetRequest (con
);
2155 if (EIBTYPE (con
) != EIB_LOAD_IMAGE
|| con
->size
< 4)
2158 return IMG_UNKNOWN_ERROR
;
2160 return (con
->buf
[2] << 8) | con
->buf
[3];
2164 EIB_LoadImage_async (EIBConnection
* con
, const uint8_t * image
, int len
)
2178 ibuf
= (uchar
*) malloc (len
+ 2);
2184 EIBSETTYPE (ibuf
, EIB_LOAD_IMAGE
);
2185 memcpy (ibuf
+ 2, image
, len
);
2186 i
= _EIB_SendRequest (con
, len
+ 2, ibuf
);
2190 con
->complete
= LoadImage_complete
;
2195 EIB_LoadImage (EIBConnection
* con
, const uint8_t * image
, int len
)
2197 if (EIB_LoadImage_async (con
, image
, len
) == -1)
2199 return EIBComplete (con
);