-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserialize.c
207 lines (173 loc) · 3.72 KB
/
serialize.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
#include <string.h>
#include "joynet.h"
#include <stdint.h>
/* Integer types larger than 1 byte are assumed to be transmitted as little
endian. Big endian platforms should convert to little endian before
transmitting. */
//! Byte swap unsigned short
static uint16_t swap_uint16( uint16_t val )
{
return (val << 8) | (val >> 8 );
}
//! Byte swap short
static int16_t swap_int16( int16_t val )
{
return (val << 8) | ((val >> 8) & 0xFF);
}
//! Byte swap unsigned int
static uint32_t swap_uint32( uint32_t val )
{
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}
//! Byte swap int
static int32_t swap_int32( int32_t val )
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
return (val << 16) | ((val >> 16) & 0xFFFF);
}
static int16_t swap_w(int16_t val)
{
if(joynet_swap_endianess)
{
return swap_int16(val);
}
return val;
}
static uint16_t swap_uw(uint16_t val)
{
if(joynet_swap_endianess)
{
return swap_uint16(val);
}
return val;
}
static int32_t swap_l(int32_t val)
{
if(joynet_swap_endianess)
{
return swap_int32(val);
}
return val;
}
static uint32_t swap_ul(uint32_t val)
{
if(joynet_swap_endianess)
{
return swap_uint32(val);
}
return val;
}
JOYNET_SERIAL_DATA * joynet_create_serial_data(void)
{
JOYNET_SERIAL_DATA * dp = NULL;
dp = malloc(sizeof(JOYNET_SERIAL_DATA));
if(dp)
{
dp->pointer = NULL;
dp->size = 0;
}
return dp;
}
void joynet_destroy_serial_data(JOYNET_SERIAL_DATA * dp)
{
free(dp);
}
void joynet_serialize(JOYNET_SERIAL_DATA * dp, char * ptr)
{
dp->data = ptr;
dp->pointer = ptr;
dp->size = 0;
}
void joynet_read(JOYNET_SERIAL_DATA * dp, char * dest, int num)
{
memcpy(dest, dp->pointer, num);
dp->pointer += num;
dp->size += num;
}
void joynet_write(JOYNET_SERIAL_DATA * dp, char * dest, int num)
{
memcpy(dp->pointer, dest, num);
dp->pointer += num;
dp->size += num;
}
void joynet_getc(JOYNET_SERIAL_DATA * dp, char * c)
{
*c = dp->pointer[0];
dp->pointer++;
dp->size++;
}
void joynet_putc(JOYNET_SERIAL_DATA * dp, char c)
{
dp->pointer[0] = c;
dp->pointer++;
dp->size++;
}
void joynet_getw(JOYNET_SERIAL_DATA * dp, int16_t * c)
{
int16_t d;
memcpy(&d, dp->pointer, sizeof(int16_t));
*c = swap_w(d);
dp->pointer += sizeof(int16_t);
dp->size += sizeof(int16_t);
}
void joynet_putw(JOYNET_SERIAL_DATA * dp, int16_t c)
{
int16_t d;
d = swap_w(c);
memcpy(dp->pointer, &d, sizeof(int16_t));
dp->pointer += sizeof(int16_t);
dp->size += sizeof(int16_t);
}
void joynet_getuw(JOYNET_SERIAL_DATA * dp, uint16_t * c)
{
uint16_t d;
memcpy(&d, dp->pointer, sizeof(uint16_t));
*c = swap_uw(d);
dp->pointer += sizeof(uint16_t);
dp->size += sizeof(uint16_t);
}
void joynet_putuw(JOYNET_SERIAL_DATA * dp, uint16_t c)
{
uint16_t d;
d = swap_uw(c);
memcpy(dp->pointer, &d, sizeof(uint16_t));
dp->pointer += sizeof(uint16_t);
dp->size += sizeof(uint16_t);
}
void joynet_getl(JOYNET_SERIAL_DATA * dp, int32_t * c)
{
int32_t d;
memcpy(&d, dp->pointer, sizeof(int32_t));
*c = swap_l(d);
dp->pointer += sizeof(int32_t);
dp->size += sizeof(int32_t);
}
void joynet_putl(JOYNET_SERIAL_DATA * dp, int32_t c)
{
int32_t d;
d = swap_l(c);
memcpy(dp->pointer, &d, sizeof(int32_t));
dp->pointer += sizeof(int32_t);
dp->size += sizeof(int32_t);
}
void joynet_getul(JOYNET_SERIAL_DATA * dp, uint32_t * c)
{
uint32_t d;
memcpy(&d, dp->pointer, sizeof(uint32_t));
*c = swap_ul(d);
dp->pointer += sizeof(uint32_t);
dp->size += sizeof(uint32_t);
}
void joynet_putul(JOYNET_SERIAL_DATA * dp, uint32_t c)
{
uint32_t d;
d = swap_ul(c);
memcpy(dp->pointer, &d, sizeof(uint32_t));
dp->pointer += sizeof(uint32_t);
dp->size += sizeof(uint32_t);
}
int joynet_get_serial_size(JOYNET_SERIAL_DATA * dp)
{
return dp->size;
}