Skip to content
Newer
Older
100644 256 lines (201 sloc) 8.62 KB
ed8d604 @ekarak first commit
authored May 17, 2012
1 /*
2 EIBD client library
3 Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
4
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.
9
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.)
17
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.
22
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27
28 #define EIBC_LICENSE(text)
29
30 #define STR(A) #A
31
32 /* #define SHORT2INT(h) "(((self.data[" #h "])<<8)|(self.data[" #h "+1]))" */
33 #define SHORT2INT(h) " bit.bor( bit.lshift(self.data[" #h "+1], 8) , (self.data[" #h "+2]))"
34
35 /* #define SHORT2LONG(h) "(((self.data[" #h "])<<24)|((self.data[" #h "+1])<<16)|((self.data[" #h "+2])<<8)|(self.data[" #h "+3]))" */
36 /*
37 (
38 ((self.data[" #h "])<<24) bit.blshift(self.data[" #h "], 24)
39 |
40 ((self.data[" #h "+1])<<16) bit.blshift(self.data[" #h "+1], 16)
41 |
42 ((self.data[" #h "+2])<<8) bit.blshift(self.data[" #h "+2], 8)
43 |
44 (self.data[" #h "+3])
45 )
46 */
47
48 #define SHORT2LONG(h) " bit.bor( bit.lshift(self.data[" #h "+1], 24), bit.bor( bit.lshift(self.data[" #h "+2], 16), bit_bor( bit.lshift(self.data[" #h "+3], 8), bit_bor(self.data[" #h "+4])"
49
50 #define EIBTYPE (SHORT2INT(0))
51
52 #define EIBC_GETREQUEST \
53 printf(" if EIBConnection.__EIB_GetRequest(self) == -1 then \n"); \
54 printf(" return -1;\n"); \
55 printf(" end\n");
56
57 #define EIBC_RETURNERROR(msg, error) \
58 printf(" if %s ~= %d then \n", EIBTYPE, msg); \
59 printf(" self.errno = \"%s\"\n", #error); \
60 printf(" return -1\n"); \
61 printf(" end\n");
62
63 #define EIBC_RETURNERROR_UINT16(offset, error) \
64 printf(" if %s == 0 then \n", SHORT2INT (offset)); \
65 printf(" self.errno = \"%s\"\n", #error); \
66 printf(" return -1\n"); \
67 printf(" end\n"); \
68
69 #define EIBC_RETURNERROR_SIZE(Length, error) \
70 printf(" if #(self.data) <= %d then \n", Length); \
71 printf(" self.errno = \"%s\"\n", #error); \
72 printf(" return -1\n"); \
73 printf(" end\n");
74
75 #define EIBC_CHECKRESULT(msg, msgsize) \
76 printf(" if (%s ~= %d) or (#(self.data) < %d) then \n", EIBTYPE, msg, msgsize); \
77 printf(" self.errno = \"ECONNRESET\"\n"); \
78 printf(" return -1\n"); \
79 printf(" end\n"); \
80
81 #define EIBC_RETURN_BUF(offset) \
82 printf(" self.buf.buffer = self.data:sub(%d)\n", offset); \
83 printf(" return #(self.buf.buffer)\n");
84
85 #define EIBC_RETURN_OK \
86 printf(" return 0\n");
87
88 #define EIBC_RETURN_LEN \
89 printf(" return self.sendlen\n");
90
91 #define EIBC_RETURN_UINT8(offset) \
92 printf(" return self.data[%d]\n", offset);
93
94 #define EIBC_RETURN_UINT16(offset) \
95 printf(" return %s\n", SHORT2INT (offset));
96
97 #define EIBC_RETURN_PTR1(offset) \
98 printf(" if self.ptr1 ~= nil then\n"); \
99 printf(" self.ptr1.data = %s\n", SHORT2INT (offset)); \
100 printf(" end\n");
101
102 #define EIBC_RETURN_PTR2(offset) \
103 printf(" if self.ptr2 ~= nil then\n"); \
104 printf(" self.ptr2.data = self.data[%d]\n", offset); \
105 printf(" end\n");
106
107 #define EIBC_RETURN_PTR3(offset) \
108 printf(" if self.ptr3 ~= nil then\n"); \
109 printf(" self.ptr3.data = self.data[%d]\n", offset); \
110 printf(" end\n");
111
112 #define EIBC_RETURN_PTR4(offset) \
113 printf(" if self.ptr4 ~= nil then\n"); \
114 printf(" self.ptr4.data = %s\n", SHORT2INT (offset)); \
115 printf(" end\n");
116
117 #define EIBC_RETURN_PTR5(offset) \
118 printf(" if self.ptr5 ~= nil then\n"); \
119 printf(" self.ptr5.data = %s\n", SHORT2INT (offset)); \
120 printf(" end\n"); \
121
122 #define EIBC_RETURN_PTR6(offset) \
123 printf(" if self.ptr6 ~= nil then\n"); \
124 printf(" self.ptr6.data = %s\n", SHORT2INT (offset)); \
125 printf(" end\n");
126
127 #define EIBC_RETURN_PTR7(offset) \
128 printf(" if self.ptr7 ~= nil then\n"); \
129 printf(" self.ptr7.data = %s\n", SHORT2LONG (offset)); \
130 printf(" end\n");
131
132 #define EIBC_COMPLETE(name, body) \
133 printf("function EIBConnection.__%s_Complete(self)\n", #name); \
134 printf(" if DEBUG then print(\"Entering EIBConnection.__%s_Complete()\") end\n", #name); \
135 printf(" self.__complete = nil;\n"); \
136 body \
137 printf("\nend\n\n");
138
139 #define EIBC_INIT_COMPLETE(name) \
140 printf(" self.__complete = self:__%s_Complete();\n", #name); \
141 printf(" return 0\n");
142
143 #define EIBC_INIT_SEND(length) \
144 printf(" ibuf = {}\n"); \
145 printf(" for i=1,%d do ibuf[i]=\"\" end\n", length);
146
147 #define EIBC_SEND_BUF(name) EIBC_SEND_BUF_LEN (name, 0)
148
149 #define EIBC_SEND_BUF_LEN(name, Length) \
150 printf(" if (#%s < %d) then \n", #name, Length); \
151 printf(" self.errno = \"EINVAL\"\n"); \
152 printf(" return -1\n"); \
153 printf(" end\n"); \
154 printf(" self.sendlen = #%s\n", #name); \
155 printf(" for i=1,#%s do table.insert(ibuf, %s[i]) end\n", #name, #name); \
156 printf(" --ibuf = ibuf .. %s\n", #name);
157
158 #define EIBC_SEND_LEN(name) STR(#name)
159
160 #define EIBC_SEND(msg) \
161 printf(" ibuf[%d] = %d\n", 1, ((msg>>8)&0xff)); \
162 printf(" ibuf[%d] = %d\n", 2, ((msg)&0xff)); \
163 printf(" if self:__EIB_SendRequest(ibuf) == -1 then \n"); \
164 printf(" return -1;\n"); \
165 printf(" end\n");
166
167 #define EIBC_READ_BUF(buffer) \
168 printf(" self.buf = %s\n", #buffer);
169
170
171 #define EIBC_READ_LEN(name) STR(name ## _len)
172
173 #define EIBC_PTR1(name) \
174 printf(" self.ptr1 = %s\n", #name);
175
176 #define EIBC_PTR2(name) \
177 printf(" self.ptr2 = %s\n", #name);
178
179 #define EIBC_PTR3(name) \
180 printf(" self.ptr3 = %s\n", #name);
181
182 #define EIBC_PTR4(name) \
183 printf(" self.ptr4 = %s\n", #name);
184
185 #define EIBC_PTR5(name) \
186 printf(" self.ptr5 = %s\n", #name);
187
188 #define EIBC_PTR6(name) \
189 printf(" self.ptr6 = %s\n", #name);
190
191 #define EIBC_PTR7(name) \
192 printf(" self.ptr7 = %s\n", #name);
193
194 #define EIBC_SETADDR(name, offset) \
195 printf(" ibuf[%d] = (bit.band(bit.rshift(%s,8), 0xff))\n", offset+1, #name); \
196 printf(" ibuf[%d] = (bit.band(%s, 0xff))\n", offset+2, #name);
197
198 #define EIBC_SETUINT8(name, offset) \
199 printf(" ibuf[%d] = (bit.band(%s, 0xff))\n", offset+1, #name);
200
201 #define EIBC_UINT8(name, offset) \
202 printf(" ibuf[%d] = (bit.band(%s, 0xff))\n", offset+1, #name);
203
204 #define EIBC_SETUINT16(name, offset) \
205 printf(" ibuf[%d] = (bit.band(bit.rshift(%s,8), 0xff))\n", offset+1, #name); \
206 printf(" ibuf[%d] = (bit.band(%s, 0xff))\n", offset+2, #name);
207
208 #define EIBC_SETLEN(name, offset) \
209 printf(" ibuf[%d] = (bit.band(bit.rshift(%s,8), 0xff))\n", offset+1, name); \
210 printf(" ibuf[%d] = (bit.band(%s, 0xff))\n", offset+2, name);
211
212 #define EIBC_SETBOOL(value, offset) \
213 printf(" if %s ~= 0 then \n", #value); \
214 printf(" ibuf[%d] = 0xff\n", offset+1); \
215 printf(" else\n", #value); \
216 printf(" ibuf[%d] = 0x00\n", offset+1); \
217 printf(" end\n");
218
219 #define EIBC_SETKEY(value, offset) \
220 printf(" if #%s ~= 4 then \n", #value); \
221 printf(" self.errno = \"EINVAL\"\n"); \
222 printf(" return -1\n"); \
223 printf(" end\n"); \
224 printf(" for i=1,4 do ibuf[%d+i] = %s[i] end\n", offset, #value); \
225 printf(" --UGLY HACK: was: ibuf[%d..%d] = %s\n", offset, offset+4, #value);
226
227 #define EIBC_ASYNC(name, args, body) \
228 printf("function EIBConnection.%s_async(self", #name); \
229 KAG ## args \
230 printf(")\n"); \
231 printf(" if DEBUG then print(\"Entering EIBConnection.%s_async()\") end\n", #name); \
232 body \
233 printf("end\n\n"); \
234 printf("function EIBConnection.%s(self", #name); \
235 KAG ## args \
236 printf(")\n"); \
237 printf(" local ibuf\n"); \
238 printf (" if DEBUG then print(\"Entering EIBConnection.%s()\") end\n", #name); \
239 printf (" if self:%s_async (", #name); \
240 AL ## args \
241 printf (") == -1 then \n"); \
242 printf (" return -1\n"); \
243 printf (" end\n"); \
244 printf (" return self:EIBComplete()\n"); \
245 printf(" end\n\n");
246
247 #define EIBC_SYNC(name, args, body) \
248 printf("function EIBConnection.%s(self", #name); \
249 KAG ## args \
250 printf(")\n"); \
251 printf(" local ibuf\n"); \
252 printf(" if DEBUG then print(\"Entering EIBConnection.%s()\") end\n", #name); \
253 body \
254 printf("end\n\n");
255
Something went wrong with that request. Please try again.