summaryrefslogtreecommitdiff
path: root/source/utilities/Serialization.cpp
blob: b4daa212fd7a10ccf6e4e2dc5e04802bd61beacf (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/
/*
 *  @addtogroup ldr_utilities
 *  @{
 *    @addtogroup serialization
 *    @{
 */

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <cstdlib>
#include <cstring>
#include "Serialization.h"
#include "LCDriverMethods.h"
#include "command_ids.h"

ListDevice_t Devices[MAX_NO_OF_DEVICES];
uint32 DevicesNumber = 0;

Serialization::Serialization(): logger_(0)
{
}

Serialization::~Serialization()
{
}

/*******************************************************************************
 * Definition of external functions
 ******************************************************************************/

void Serialization::get_block(const void **data_pp, void *target_p, uint32 length)
{
    memcpy(target_p, *data_pp, length);
    *(const uint8 **)data_pp += length;
#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tget_block\tLength=%i", (void *)length);
#endif
}

SupportedCommand_t *Serialization::get_supportedcommands(const void **data_pp, uint32 CommandsCount)
{
    SupportedCommand_t *SupportedCommands = new SupportedCommand_t[CommandsCount];

    for (uint32 i = 0; i < CommandsCount; i++) {
        SupportedCommands[i].Group = get_uint32_le(data_pp);
        SupportedCommands[i].Command = get_uint32_le(data_pp);
        SupportedCommands[i].Permitted = get_uint32_le(data_pp);
    }

    return SupportedCommands;
}

ListDevice_t *Serialization::get_devices(const void **data_pp, uint32 DevicesCount)
{
    size_t size = 0;
    const char *str;

    DevicesNumber = DevicesCount;

    for (uint32 i = 0; i < DevicesCount; i++) {
        str = skip_str(data_pp);
        size = get_uint32_le((const void **)&str);
        memset(Devices[i].Path_p, 0, 256);
        memcpy(Devices[i].Path_p, str, size);
        Devices[i].Path_p[size] = '\0';

        str = skip_str(data_pp);
        size = get_uint32_le((const void **)&str);
        memset(Devices[i].Type_p, 0, 256);
        memcpy(Devices[i].Type_p, str, size);
        Devices[i].Type_p[size] = '\0';

        Devices[i].BlockSize = get_uint64_le(data_pp);
        Devices[i].Start = get_uint64_le(data_pp);
        Devices[i].Length = get_uint64_le(data_pp);
    }

    return Devices;
}

DirEntry_t *Serialization::get_direntries(const void **data_pp, uint32 DirectoriesCount)
{
    DirEntry_t *Direntries = new DirEntry_t[DirectoriesCount];
    size_t size = 0;
    const char *str;

    for (uint32 i = 0; i < DirectoriesCount; i++) {
        str = skip_str(data_pp);
        size = get_uint32_le((const void **)&str);
        Direntries[i].Name_p = new char[size + 1];
        memcpy(Direntries[i].Name_p, str, size);
        Direntries[i].Name_p[size] = '\0';

        Direntries[i].Size = get_uint64_le(data_pp);
        Direntries[i].Mode = get_uint32_le(data_pp);
        Direntries[i].Time = get_uint32_le(data_pp);
    }

    return Direntries;
}

uint8 Serialization::get_uint8(const void **data_pp)
{
    const uint8 *d = *(const uint8 **)data_pp;
    *(const uint8 **)data_pp += sizeof(uint8);
#ifdef _SERIALIZATIONDEBUG
    unsigned char v = d[0];
    PrintF("Serialization\tget_uint8\tReturned val=0x%x", (void *)v);
#endif
    return d[0];
}
uint16 Serialization::get_uint16_le(const void **data_pp)
{
    const uint8 *d = *(const uint8 **)data_pp;
    *(const uint8 **)data_pp += sizeof(uint16);
#ifdef _SERIALIZATIONDEBUG
    unsigned short v = (d[1] << 8) | d[0];
    PrintF("Serialization\tget_uint16_le\tReturned val=0x%x", (void *)v);
#endif
    return (d[1] << 8) | d[0];
}
uint16 Serialization::get_uint16_be(const void **data_pp)
{
    const uint8 *d = *(const uint8 **)data_pp;
    *(const uint8 **)data_pp += sizeof(uint16);
#ifdef _SERIALIZATIONDEBUG
    unsigned short v = (d[0] << 8) | d[1];
    PrintF("Serialization\tget_uint16_be\tReturned val=0x%x", (void *)v);
#endif
    return (d[0] << 8) | d[1];
}
uint32 Serialization::get_uint32_le(const void **data_pp)
{
    const uint8 *d = *(const uint8 **) data_pp;
    *(const uint8 **)data_pp += sizeof(uint32);
#ifdef _SERIALIZATIONDEBUG
    unsigned int v = ((d[3] << 8 | d[2]) << 8 | d[1]) << 8 | d[0];
    PrintF("Serialization\tget_uint32_le\tReturned val=0x%x", (void *)v);
#endif
    return ((d[3] << 8 | d[2]) << 8 | d[1]) << 8 | d[0];
}

uint64 Serialization::get_uint64_le(const void **data_pp)
{
    const uint8 *d = *(const uint8 **) data_pp;
    *(const uint8 **)data_pp += sizeof(uint64);
#ifdef _SERIALIZATIONDEBUG
    uint64 v = (((((((uint64)d[7] << 8 | d[6]) << 8 | d[5]) << 8 | d[4]) << 8 | d[3]) << 8 | d[2]) << 8 | d[1]) << 8 | d[0];
    PrintF("Serialization\tget_uint64_le\tReturned val=0x%x", (void *)v);
#endif
    return (((((((uint64)d[7] << 8 | d[6]) << 8 | d[5]) << 8 | d[4]) << 8 | d[3]) << 8 | d[2]) << 8 | d[1]) << 8 | d[0];
}

uint32 Serialization::get_uint32_be(const void **data_pp)
{
    const uint8 *d = *(const uint8 **) data_pp;
    *(const uint8 **)data_pp += sizeof(uint32);
#ifdef _SERIALIZATIONDEBUG
    unsigned int v = ((d[0] << 8 | d[1]) << 8 | d[2]) << 8 | d[3];
    PrintF("Serialization\tget_uint32_be\tReturned val=0x%x", (void *)v);
#endif
    return ((d[0] << 8 | d[1]) << 8 | d[2]) << 8 | d[3];
}

void Serialization::put_block(void **data_pp, const void *source_p, uint32 length)
{
    if (length > 0) {
        memcpy(*data_pp, source_p, length);
        *(const uint8 **)data_pp += length;
    }

#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tput_block\tLength=%i", (void *)length);
#endif
}

void Serialization::put_string(void **data_pp, const void *source_p, uint32 length)
{
    if (length > 0) {
        memcpy(*data_pp, &length, sizeof(uint32));
        *(const uint8 **)data_pp += sizeof(uint32);
        memcpy(*data_pp, source_p, length);
        *(const uint8 **)data_pp += length;
    }
}

void Serialization::put_uint8(void **data_pp, uint8 v)
{
    **(uint8 **) data_pp = v;
    *(uint8 **) data_pp += sizeof(uint8);
#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tput_uint8\tInput val=0x%x", (void *)v);
#endif
}

void Serialization::put_uint16_le(void **data_pp, uint16 v)
{
    uint8 *d = *(uint8 **) data_pp;
    d[0] = v & 0xff;
    d[1] = v >> 8;
    *(uint8 **)data_pp += sizeof(uint16);
#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tput_uint16_le\tInput val=0x%x", (void *)v);
#endif
}

void Serialization::put_uint16_be(void **data_pp, uint16 v)
{
    uint8 *d = *(uint8 **) data_pp;
    d[1] = v & 0xff;
    d[0] = v >> 8;
    *(uint8 **)data_pp += sizeof(uint16);
#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tput_uint16_be\tInput val=0x%x", (void *)v);
#endif
}

void Serialization::put_uint32_le(void **data_pp, uint32 v)
{
    uint8 *d = *(uint8 **) data_pp;
    uint8 *v_p = (uint8 *)&v;
    d[0] = v_p[0];
    d[1] = v_p[1];
    d[2] = v_p[2];
    d[3] = v_p[3];
    *(uint8 **)data_pp += sizeof(uint32);
#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tput_uint32_le\tInput val=0x%x", (void *)v);
#endif
}

void Serialization::put_uint32_be(void **data_pp, uint32 v)
{
    uint8 *d = *(uint8 **) data_pp;
    uint8 *v_p = (uint8 *)&v;
    d[3] = v_p[0];
    d[2] = v_p[1];
    d[1] = v_p[2];
    d[0] = v_p[3];
    *(uint8 **)data_pp += sizeof(uint32);
#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tput_uint32_be\tInput val=0x%x", (void *)v);
#endif
}

void Serialization::put_uint64_le(void **data_pp, uint64 v)
{
    uint8 *d = *(uint8 **) data_pp;
    uint8 *v_p = (uint8 *)&v;
    d[0] = v_p[0];
    d[1] = v_p[1];
    d[2] = v_p[2];
    d[3] = v_p[3];
    d[4] = v_p[4];
    d[5] = v_p[5];
    d[6] = v_p[6];
    d[7] = v_p[7];
    *(uint8 **)data_pp += sizeof(uint32);
    *(uint8 **)data_pp += sizeof(uint32);
#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tput_uint64_le\tInput val=0x%x", (void *)v);
#endif
}

void Serialization::put_uint64_be(void **data_pp, uint64 v)
{
    uint8 *d = *(uint8 **) data_pp;
    uint8 *v_p = (uint8 *)&v;
    d[7] = v_p[0];
    d[6] = v_p[1];
    d[5] = v_p[2];
    d[4] = v_p[3];
    d[3] = v_p[4];
    d[2] = v_p[5];
    d[1] = v_p[6];
    d[0] = v_p[7];
    *(uint8 **)data_pp += sizeof(uint32);
    *(uint8 **)data_pp += sizeof(uint32);
#ifdef _SERIALIZATIONDEBUG
    PrintF("Serialization\tput_uint64_be\tInput val=0x%x", (void *)v);
#endif
}

void Serialization::skip_uint8(const void **data_pp)
{
    *(const uint8 **)data_pp += sizeof(uint8);
}

void Serialization::skip_uint16(const void **data_pp)
{
    *(const uint8 **)data_pp += sizeof(uint16);
}

void Serialization::skip_uint32(const void **data_pp)
{
    *(const uint8 **)data_pp += sizeof(uint32);
}

void Serialization::skip_block(const void **data_pp, uint32 length)
{
    *(const uint8 **)data_pp += length;
}

char *Serialization::skip_str(const void **data_pp)
{
    char *Result_p = NULL;
    uint32 StrLength;

    Result_p = *(char **)data_pp;
    StrLength = get_uint32_le(data_pp);
    *(char **)data_pp += StrLength;
    return Result_p;
}

void Serialization::PrintF(const char *text, void *pVoid)
{
    if (NULL != logger_) {
        logger_->log(text, pVoid);
    }
}