Merge branch 'mesa_7_5_branch'
[mesa.git] / src / gallium / auxiliary / rbug / rbug_core.c
1 /*
2 * Copyright 2009 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * This file holds the function implementation for one of the rbug extensions.
27 * Prototypes and declerations of functions and structs is in the same folder
28 * in the header file matching this file's name.
29 *
30 * The functions starting rbug_send_* encodes a call to the write format and
31 * sends that to the supplied connection, while functions starting with
32 * rbug_demarshal_* demarshal data in the wire protocol.
33 *
34 * Functions ending with _reply are replies to requests.
35 */
36
37 #include "rbug_internal.h"
38 #include "rbug/rbug_core.h"
39
40 int rbug_send_noop(struct rbug_connection *__con,
41 uint32_t *__serial)
42 {
43 uint32_t __len = 0;
44 uint32_t __pos = 0;
45 uint8_t *__data = NULL;
46 int __ret = 0;
47
48 LEN(8); /* header */
49
50 /* align */
51 PAD(__len, 8);
52
53 __data = (uint8_t*)MALLOC(__len);
54 if (!__data)
55 return -ENOMEM;
56
57 WRITE(4, int32_t, ((int32_t)RBUG_OP_NOOP));
58 WRITE(4, uint32_t, ((uint32_t)(__len / 4)));
59
60 /* final pad */
61 PAD(__pos, 8);
62
63 if (__pos != __len) {
64 __ret = -EINVAL;
65 } else {
66 rbug_connection_send_start(__con, RBUG_OP_NOOP, __len);
67 rbug_connection_write(__con, __data, __len);
68 __ret = rbug_connection_send_finish(__con, __serial);
69 }
70
71 FREE(__data);
72 return __ret;
73 }
74
75 int rbug_send_ping(struct rbug_connection *__con,
76 uint32_t *__serial)
77 {
78 uint32_t __len = 0;
79 uint32_t __pos = 0;
80 uint8_t *__data = NULL;
81 int __ret = 0;
82
83 LEN(8); /* header */
84
85 /* align */
86 PAD(__len, 8);
87
88 __data = (uint8_t*)MALLOC(__len);
89 if (!__data)
90 return -ENOMEM;
91
92 WRITE(4, int32_t, ((int32_t)RBUG_OP_PING));
93 WRITE(4, uint32_t, ((uint32_t)(__len / 4)));
94
95 /* final pad */
96 PAD(__pos, 8);
97
98 if (__pos != __len) {
99 __ret = -EINVAL;
100 } else {
101 rbug_connection_send_start(__con, RBUG_OP_PING, __len);
102 rbug_connection_write(__con, __data, __len);
103 __ret = rbug_connection_send_finish(__con, __serial);
104 }
105
106 FREE(__data);
107 return __ret;
108 }
109
110 int rbug_send_error(struct rbug_connection *__con,
111 uint32_t error,
112 uint32_t *__serial)
113 {
114 uint32_t __len = 0;
115 uint32_t __pos = 0;
116 uint8_t *__data = NULL;
117 int __ret = 0;
118
119 LEN(8); /* header */
120 LEN(4); /* error */
121
122 /* align */
123 PAD(__len, 8);
124
125 __data = (uint8_t*)MALLOC(__len);
126 if (!__data)
127 return -ENOMEM;
128
129 WRITE(4, int32_t, ((int32_t)RBUG_OP_ERROR));
130 WRITE(4, uint32_t, ((uint32_t)(__len / 4)));
131 WRITE(4, uint32_t, error); /* error */
132
133 /* final pad */
134 PAD(__pos, 8);
135
136 if (__pos != __len) {
137 __ret = -EINVAL;
138 } else {
139 rbug_connection_send_start(__con, RBUG_OP_ERROR, __len);
140 rbug_connection_write(__con, __data, __len);
141 __ret = rbug_connection_send_finish(__con, __serial);
142 }
143
144 FREE(__data);
145 return __ret;
146 }
147
148 int rbug_send_ping_reply(struct rbug_connection *__con,
149 uint32_t serial,
150 uint32_t *__serial)
151 {
152 uint32_t __len = 0;
153 uint32_t __pos = 0;
154 uint8_t *__data = NULL;
155 int __ret = 0;
156
157 LEN(8); /* header */
158 LEN(4); /* serial */
159
160 /* align */
161 PAD(__len, 8);
162
163 __data = (uint8_t*)MALLOC(__len);
164 if (!__data)
165 return -ENOMEM;
166
167 WRITE(4, int32_t, ((int32_t)RBUG_OP_PING_REPLY));
168 WRITE(4, uint32_t, ((uint32_t)(__len / 4)));
169 WRITE(4, uint32_t, serial); /* serial */
170
171 /* final pad */
172 PAD(__pos, 8);
173
174 if (__pos != __len) {
175 __ret = -EINVAL;
176 } else {
177 rbug_connection_send_start(__con, RBUG_OP_PING_REPLY, __len);
178 rbug_connection_write(__con, __data, __len);
179 __ret = rbug_connection_send_finish(__con, __serial);
180 }
181
182 FREE(__data);
183 return __ret;
184 }
185
186 int rbug_send_error_reply(struct rbug_connection *__con,
187 uint32_t serial,
188 uint32_t error,
189 uint32_t *__serial)
190 {
191 uint32_t __len = 0;
192 uint32_t __pos = 0;
193 uint8_t *__data = NULL;
194 int __ret = 0;
195
196 LEN(8); /* header */
197 LEN(4); /* serial */
198 LEN(4); /* error */
199
200 /* align */
201 PAD(__len, 8);
202
203 __data = (uint8_t*)MALLOC(__len);
204 if (!__data)
205 return -ENOMEM;
206
207 WRITE(4, int32_t, ((int32_t)RBUG_OP_ERROR_REPLY));
208 WRITE(4, uint32_t, ((uint32_t)(__len / 4)));
209 WRITE(4, uint32_t, serial); /* serial */
210 WRITE(4, uint32_t, error); /* error */
211
212 /* final pad */
213 PAD(__pos, 8);
214
215 if (__pos != __len) {
216 __ret = -EINVAL;
217 } else {
218 rbug_connection_send_start(__con, RBUG_OP_ERROR_REPLY, __len);
219 rbug_connection_write(__con, __data, __len);
220 __ret = rbug_connection_send_finish(__con, __serial);
221 }
222
223 FREE(__data);
224 return __ret;
225 }
226
227 struct rbug_proto_noop * rbug_demarshal_noop(struct rbug_proto_header *header)
228 {
229 uint32_t len = 0;
230 uint32_t pos = 0;
231 uint8_t *data = NULL;
232 struct rbug_proto_noop *ret;
233
234 if (!header)
235 return NULL;
236 if (header->opcode != (int16_t)RBUG_OP_NOOP)
237 return NULL;
238
239 pos = 0;
240 len = header->length * 4;
241 data = (uint8_t*)&header[1];
242 ret = MALLOC(sizeof(*ret));
243 if (!ret)
244 return NULL;
245
246 ret->header.__message = header;
247 ret->header.opcode = header->opcode;
248
249
250 return ret;
251 }
252
253 struct rbug_proto_ping * rbug_demarshal_ping(struct rbug_proto_header *header)
254 {
255 uint32_t len = 0;
256 uint32_t pos = 0;
257 uint8_t *data = NULL;
258 struct rbug_proto_ping *ret;
259
260 if (!header)
261 return NULL;
262 if (header->opcode != (int16_t)RBUG_OP_PING)
263 return NULL;
264
265 pos = 0;
266 len = header->length * 4;
267 data = (uint8_t*)&header[1];
268 ret = MALLOC(sizeof(*ret));
269 if (!ret)
270 return NULL;
271
272 ret->header.__message = header;
273 ret->header.opcode = header->opcode;
274
275
276 return ret;
277 }
278
279 struct rbug_proto_error * rbug_demarshal_error(struct rbug_proto_header *header)
280 {
281 uint32_t len = 0;
282 uint32_t pos = 0;
283 uint8_t *data = NULL;
284 struct rbug_proto_error *ret;
285
286 if (!header)
287 return NULL;
288 if (header->opcode != (int16_t)RBUG_OP_ERROR)
289 return NULL;
290
291 pos = 0;
292 len = header->length * 4;
293 data = (uint8_t*)&header[1];
294 ret = MALLOC(sizeof(*ret));
295 if (!ret)
296 return NULL;
297
298 ret->header.__message = header;
299 ret->header.opcode = header->opcode;
300
301 READ(4, uint32_t, error); /* error */
302
303 return ret;
304 }
305
306 struct rbug_proto_ping_reply * rbug_demarshal_ping_reply(struct rbug_proto_header *header)
307 {
308 uint32_t len = 0;
309 uint32_t pos = 0;
310 uint8_t *data = NULL;
311 struct rbug_proto_ping_reply *ret;
312
313 if (!header)
314 return NULL;
315 if (header->opcode != (int16_t)RBUG_OP_PING_REPLY)
316 return NULL;
317
318 pos = 0;
319 len = header->length * 4;
320 data = (uint8_t*)&header[1];
321 ret = MALLOC(sizeof(*ret));
322 if (!ret)
323 return NULL;
324
325 ret->header.__message = header;
326 ret->header.opcode = header->opcode;
327
328 READ(4, uint32_t, serial); /* serial */
329
330 return ret;
331 }
332
333 struct rbug_proto_error_reply * rbug_demarshal_error_reply(struct rbug_proto_header *header)
334 {
335 uint32_t len = 0;
336 uint32_t pos = 0;
337 uint8_t *data = NULL;
338 struct rbug_proto_error_reply *ret;
339
340 if (!header)
341 return NULL;
342 if (header->opcode != (int16_t)RBUG_OP_ERROR_REPLY)
343 return NULL;
344
345 pos = 0;
346 len = header->length * 4;
347 data = (uint8_t*)&header[1];
348 ret = MALLOC(sizeof(*ret));
349 if (!ret)
350 return NULL;
351
352 ret->header.__message = header;
353 ret->header.opcode = header->opcode;
354
355 READ(4, uint32_t, serial); /* serial */
356 READ(4, uint32_t, error); /* error */
357
358 return ret;
359 }