ARG_UINT8/16
[bcusdk.git] / eibd / client / eibclient-int.h
blob1700214c0e9cb6ec693725024aa9804f7c7ffd94
1 /*
2 EIBD client library - internals
3 Copyright (C) 2005-2007 Martin Koegler <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
28 #ifndef EIBCLIENT_INT_H
29 #define EIBCLIENT_INT_H
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
36 #include "eibtypes.h"
38 /** unsigned char */
39 typedef uint8_t uchar;
41 /** EIB Connection internal */
42 struct _EIBConnection
44 int (*complete) (EIBConnection *);
45 /** file descriptor */
46 int fd;
47 unsigned readlen;
48 /** buffer */
49 uchar *buf;
50 /** buffer size */
51 unsigned buflen;
52 /** used buffer */
53 unsigned size;
54 struct
56 int len;
57 uint8_t *buf;
58 int16_t *ptr1;
59 uint8_t *ptr2;
60 uint8_t *ptr3;
61 uint16_t *ptr4;
62 eibaddr_t *ptr5;
63 eibaddr_t *ptr6;
64 } req;
67 /** extracts TYPE code of an eibd packet */
68 #define EIBTYPE(con) (((con)->buf[0]<<8)|((con)->buf[1]))
69 /** sets TYPE code for an eibd packet*/
70 #define EIBSETTYPE(buf,type) do{(buf)[0]=(type>>8)&0xff;(buf)[1]=(type)&0xff;}while(0)
72 /** set EIB address */
73 #define EIBSETADDR(buf,type) do{(buf)[0]=(type>>8)&0xff;(buf)[1]=(type)&0xff;}while(0)
75 int _EIB_SendRequest (EIBConnection * con, unsigned int size, uchar * data);
76 int _EIB_CheckRequest (EIBConnection * con, int block);
77 int _EIB_GetRequest (EIBConnection * con);
79 #define EIBC_GETREQUEST \
80 int i; \
81 i = _EIB_GetRequest (con); \
82 if (i == -1) \
83 return -1;
85 #define EIBC_RETURNERROR(msg, error) \
86 if (EIBTYPE (con) == msg) \
87 { \
88 errno = error; \
89 return -1; \
92 #define EIBC_RETURNERROR_UINT16(offset, error) \
93 if (!con->buf[offset] && !con->buf[offset+1]) \
94 { \
95 errno = error; \
96 return -1; \
99 #define EIBC_RETURNERROR_SIZE(length, error) \
100 if (con->size <= length) \
102 errno = error; \
103 return -1; \
106 #define EIBC_CHECKRESULT(msg, msgsize) \
107 if (EIBTYPE (con) != msg || con->size < msgsize) \
109 errno = ECONNRESET; \
110 return -1; \
113 #define EIBC_RETURN_BUF(offset) \
114 i = con->size - offset; \
115 if (i > con->req.len) \
116 i = con->req.len; \
117 memcpy (con->req.buf, con->buf + offset, i); \
118 return i;
120 #define EIBC_RETURN_OK \
121 return 0;
123 #define EIBC_RETURN_LEN \
124 return con->req.len;
126 #define EIBC_RETURN_UINT8(offset) \
127 return con->buf[offset];
129 #define EIBC_RETURN_UINT16(offset) \
130 return (con->buf[offset] << 8) | (con->buf[offset+1]);
132 #define EIBC_RETURN_PTR1(offset) \
133 if (con->req.ptr1) \
134 *con->req.ptr1 = (con->buf[offset] << 8) | (con->buf[offset+1]);
136 #define EIBC_RETURN_PTR2(offset) \
137 if (con->req.ptr2) \
138 *con->req.ptr2 = con->buf[offset];
140 #define EIBC_RETURN_PTR3(offset) \
141 if (con->req.ptr3) \
142 *con->req.ptr3 = con->buf[offset];
144 #define EIBC_RETURN_PTR4(offset) \
145 if (con->req.ptr4) \
146 *con->req.ptr4 = (con->buf[offset] << 8) | (con->buf[offset+1]);
148 #define EIBC_RETURN_PTR5(offset) \
149 if (con->req.ptr5) \
150 *con->req.ptr5 = (con->buf[offset] << 8) | (con->buf[offset+1]);
152 #define EIBC_RETURN_PTR6(offset) \
153 if (con->req.ptr6) \
154 *con->req.ptr6 = (con->buf[offset] << 8) | (con->buf[offset+1]);
156 #define EIBC_COMPLETE(name, body) \
157 static int \
158 name ## _complete (EIBConnection * con) \
160 body \
163 #define EIBC_INIT_COMPLETE(name) \
164 con->complete = name ## _complete; \
165 return 0;
167 #define EIBC_INIT_SEND(length) \
168 uchar head[length]; \
169 uchar *ibuf = head; \
170 unsigned int ilen = length; \
171 int dyn = 0; \
172 int i; \
173 if (!con) \
175 errno = EINVAL; \
176 return -1; \
179 #define EIBC_SEND_BUF(name) EIBC_SEND_BUF_LEN (name, 0)
181 #define EIBC_SEND_BUF_LEN(name, length) \
182 if (!name || name ## _len < length) \
184 errno = EINVAL; \
185 return -1; \
187 con->req.len = name ## _len; \
188 dyn = 1; \
189 ibuf = (uchar *) malloc (ilen + name ## _len); \
190 if (!ibuf) \
192 errno = ENOMEM; \
193 return -1; \
195 memcpy (ibuf, head, ilen); \
196 memcpy (ibuf + ilen, name, name ## _len); \
197 ilen = ilen + name ## _len;
199 #define EIBC_SEND_LEN(name) (name ## _len)
201 #define EIBC_SEND(msg) \
202 EIBSETTYPE (ibuf, msg); \
203 i = _EIB_SendRequest (con, ilen, ibuf); \
204 if (dyn) \
205 free (ibuf); \
206 if (i == -1) \
207 return -1;
209 #define EIBC_READ_BUF(buffer) \
210 if (!buffer || buffer ## _maxlen < 0) \
212 if (dyn) \
213 free (ibuf); \
214 errno = EINVAL; \
215 return -1; \
217 con->req.buf = buffer; \
218 con->req.len = buffer ## _maxlen;
220 #define EIBC_READ_LEN(buffer) (buffer ## _maxlen)
222 #define EIBC_PTR1(name) \
223 con->req.ptr1 = name;
225 #define EIBC_PTR2(name) \
226 con->req.ptr2 = name;
228 #define EIBC_PTR3(name) \
229 con->req.ptr3 = name;
231 #define EIBC_PTR4(name) \
232 con->req.ptr4 = name;
234 #define EIBC_PTR5(name) \
235 con->req.ptr5 = name;
237 #define EIBC_PTR6(name) \
238 con->req.ptr6 = name;
240 #define EIBC_SETADDR(name, offset) \
241 EIBSETADDR (ibuf + offset, name);
243 #define EIBC_SETUINT8(value, offset) \
244 ibuf[offset] = value;
246 #define EIBC_SETUINT16(value, offset) \
247 ibuf[offset] = ((value) >> 8) & 0xff; \
248 ibuf[offset + 1] = ((value)) & 0xff;
250 #define EIBC_SETBOOL(value, offset) \
251 ibuf[offset] = ((value) ? 0xff : 0);
253 #define EIBC_SETKEY(value, offset) \
254 memcpy (ibuf + offset, value, 4);
256 #define EIBC_ASYNC(name, args, body) \
257 int \
258 name ##_async (EIBConnection * con AG##args) \
260 body \
263 int \
264 name (EIBConnection * con AG##args) \
266 if (name ## _async (con AL##args) == -1) \
267 return -1; \
268 return EIBComplete (con); \
271 #define AGARG_NONE
272 #define AGARG_BOOL(name, args) , int name AG##args
273 #define AGARG_UINT8(name, args) , uint8_t name AG##args
274 #define AGARG_UINT8a(name, args) , uint8_t name AG##args
275 #define AGARG_UINT8b(name, args) , uint8_t name AG##args
276 #define AGARG_UINT16(name, args) , uint16_t name AG##args
277 #define AGARG_ADDR(name, args) , eibaddr_t name AG##args
278 #define AGARG_INBUF(name, args) , int name##_len, const uint8_t *name AG##args
279 #define AGARG_OUTBUF(name, args) , int name##_maxlen, uint8_t *name AG##args
281 #define ALARG_NONE
282 #define ALARG_BOOL(name, args) , name AL##args
283 #define ALARG_UINT8(name, args) , name AL##args
284 #define ALARG_UINT8a(name, args) , name AL##args
285 #define ALARG_UINT8b(name, args) , name AL##args
286 #define ALARG_UINT16(name, args) , name AL##args
287 #define ALARG_ADDR(name, args) , name AL##args
288 #define ALARG_INBUF(name, args) , name##_len, name AL##args
289 #define ALARG_OUTBUF(name, args) , name##_maxlen, name AL##args
291 #endif