/
MarshallerFull.c
291 lines (272 loc) · 8.7 KB
/
MarshallerFull.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* MarshallerFull.c
* All the serialisation and deserialisation functions. Grabbed
* from marshaller.c generated from insense_progs/unix/marshaller_types.isf
* and RadioPacket.c
*
* @author Andrew Bell
*
*/
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printIncludes
#include "InsenseRuntime.h"
#include "FunctionPair.h"
#include "BSTMap.h"
#include "GlobalObjects.h"
#include "cstring.h"
#include "MarshallerFull.h"
#ifndef DALSMALL
static char *file_name = "marshaller"; // used by DAL_error macro
#endif
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printSerializer
void* serialize_b( bool *p,int* size ) {
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.Code::genMallocAssign
void* pntr=( void* ) DAL_alloc( 128,false ) ;
if( pntr== NULL ) {
DAL_error(OUT_OF_MEMORY_ERROR);
return NULL;
}
void* result=pntr;
int used= 0;
used += 2;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( pntr,"b",2 ) ;
pntr =((char *)pntr)+2;
used += sizeof( int ) ;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( (char *)pntr,(char *)p,sizeof( int ) ) ;
*size=used;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printSerializer
void* serialize_i( int *p,int* size ) {
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.Code::genMallocAssign
void* pntr=( void* ) DAL_alloc( 128,false ) ;
if( pntr== NULL ) {
DAL_error(OUT_OF_MEMORY_ERROR);
return NULL;
}
void* result=pntr;
int used= 0;
used += 2;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( pntr,"i",2 ) ;
pntr =((char *)pntr)+2;
used += sizeof( int ) ;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( (char *)pntr,(char *)p,sizeof( int ) ) ;
*size=used;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printSerializer
void* serialize_u( unsigned *p,int* size ) {
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.Code::genMallocAssign
void* pntr=( void* ) DAL_alloc( 128,false ) ;
if( pntr== NULL ) {
DAL_error(OUT_OF_MEMORY_ERROR);
return NULL;
}
void* result=pntr;
int used= 0;
used += 2;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( pntr,"u",2 ) ;
pntr =((char *)pntr)+2;
used += sizeof( int ) ;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( (char *)pntr,(char *)p,sizeof( int ) ) ;
*size=used;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printSerializer
void* serialize_r( float *p,int* size ) {
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.Code::genMallocAssign
void* pntr=( void* ) DAL_alloc( 128,false ) ;
if( pntr== NULL ) {
DAL_error(OUT_OF_MEMORY_ERROR);
return NULL;
}
void* result=pntr;
int used= 0;
used += 2;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( pntr,"r",2 ) ;
pntr =((char *)pntr)+2;
used += sizeof( float ) ;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( (char *)pntr,(char *)p,sizeof( float ) ) ;
*size=used;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printSerializer
void* serialize_8( uint8_t *p,int* size ) {
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.Code::genMallocAssign
void* pntr=( void* ) DAL_alloc( 128,false ) ;
if( pntr== NULL ) {
DAL_error(OUT_OF_MEMORY_ERROR);
return NULL;
}
void* result=pntr;
int used= 0;
used += 2;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( pntr,"8",2 ) ;
pntr =((char *)pntr)+2;
used += sizeof( char ) ;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( (char *)pntr,(char *)p,sizeof( char ) ) ;
*size=used;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printSerializer
void* serialize_s( StringPNTR p,int* size ) {
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.Code::genMallocAssign
void* pntr=( void* ) DAL_alloc( 128,false ) ;
if( pntr== NULL ) {
DAL_error(OUT_OF_MEMORY_ERROR);
return NULL;
}
void* result=pntr;
int used= 0;
used += 2;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( pntr,"s",2 ) ;
pntr =((char *)pntr)+2;
used += p->length + 1;
if(used > 128){
DAL_error( SERIALISATION_ERROR ) ;
return NULL;
}
memncpy( (char *)pntr,p->data,p->length + 1 ) ;
*size=used;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_b( void* p ) {
void *result;
bool value;
memncpy( (char *)&value,(char *)p,sizeof( int ) ) ;
result = Construct_BoolAnyType0( value,"b" ) ;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_i( void* p ) {
void *result;
int value;
memncpy( (char *)&value,(char *)p,sizeof( int ) ) ;
result = Construct_IntAnyType0( value,"i" ) ;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_u( void* p ) {
void *result;
unsigned value;
memncpy( (char *)&value,(char *)p,sizeof( int ) ) ;
result = Construct_UnsignedIntAnyType0( value,"u" ) ;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_r( void* p ) {
void *result;
float value;
memncpy( (char *)&value,(char *)p,sizeof( float ) ) ;
result = Construct_RealAnyType0( value,"r" ) ;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_8( void* p ) {
void *result;
uint8_t value;
memncpy( (char *)&value,(char *)p,sizeof( char ) ) ;
result = Construct_ByteAnyType0( value,"8" ) ;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_s( void* p ) {
void *result;
StringPNTR value;
value=Construct_String1( p ) ;
result = Construct_PointerAnyType0( value,"s" ) ;
return result;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printInitializeSerializerFunctions
void initializeSerializerFunctions() {
mapPut( serialiserMap,"b",Construct_FunctionPair( (serialf_t)serialize_b,(deserialf_t)deserialize_b ) ) ;
mapPut( serialiserMap,"i",Construct_FunctionPair( (serialf_t)serialize_i,(deserialf_t)deserialize_i ) ) ;
mapPut( serialiserMap,"u",Construct_FunctionPair( (serialf_t)serialize_u,(deserialf_t)deserialize_u ) ) ;
mapPut( serialiserMap,"r",Construct_FunctionPair( (serialf_t)serialize_r,(deserialf_t)deserialize_r ) ) ;
mapPut( serialiserMap,"8",Construct_FunctionPair( (serialf_t)serialize_8,(deserialf_t)deserialize_8 ) ) ;
mapPut( serialiserMap,"s",Construct_FunctionPair( (serialf_t)serialize_s,(deserialf_t)deserialize_s ) ) ;
}
// function to serialise AnyType and return pointer to buffer
// containing data in serial form
void *serialiseAnyType(AnyTypePNTR data, int *size){
void *serial_buffer;
FunctionPairPNTR funcs = mapGet(serialiserMap, data->type);
if (funcs == NULL || funcs->ser == NULL){
DAL_error(SERIALISATION_ERROR);
PRINTF("serAny:%s \n", (char *) data->type);
return NULL;
}
//PRINTF(" serany: %s \n", data->type);
if(stringStartsWith(data->type, "i") || stringStartsWith(data->type, "e"))
serial_buffer = (*(funcs->ser))(&(data->value.int_value), size);
else if(stringStartsWith(data->type, "u"))
serial_buffer = (*(funcs->ser))(&(data->value.unsigned_value), size);
else if(stringStartsWith(data->type, "r"))
serial_buffer = (*(funcs->ser))(&(data->value.real_value), size);
else if(stringStartsWith(data->type, "b"))
serial_buffer = (*(funcs->ser))(&(data->value.bool_value), size);
else if(stringStartsWith(data->type, "8"))
serial_buffer = (*(funcs->ser))(&(data->value.byte_value), size);
else
serial_buffer = (*(funcs->ser))(data->value.pointer_value, size);
return serial_buffer;
}
// function to de-serialise data received from the radio to a new AnyType
AnyTypePNTR deserialiseToAnyType(void *buffer){
AnyTypePNTR newAny; void *data;
FunctionPairPNTR funcs = mapGet(serialiserMap, (char *) buffer);
if (funcs == NULL || funcs->des == NULL){
DAL_error(SERIALISATION_ERROR);
PRINTF("des2any:%s \n", (char *) buffer);
return NULL;
}
//PRINTF(" des2any:%s ", (char *) buffer);
data = ((char *) buffer) + strlen((char *)buffer) + 1; // 1 for \0
newAny = (*(funcs->des))(data);
//PRINTF("%p\n", newAny->value);
return newAny;
}