tizen 2.3 release
[external/buxton.git] / src / shared / serialize.h
1 /*
2  * This file is part of buxton.
3  *
4  * Copyright (C) 2013 Intel Corporation
5  *
6  * buxton is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1
9  * of the License, or (at your option) any later version.
10  */
11
12 /**
13  * \file serialize.h Internal header
14  * This file is used internally by buxton to provide serialization
15  * functionality
16  */
17 #pragma once
18
19 #ifdef HAVE_CONFIG_H
20         #include "config.h"
21 #endif
22
23 #include <stdint.h>
24
25 #include "buxton.h"
26 #include "buxtonarray.h"
27
28 /**
29  * Magic for Buxton messages
30  */
31 #define BUXTON_CONTROL_CODE 0x672
32
33 /**
34  * Location of size in serialized message data
35  */
36 #define BUXTON_LENGTH_OFFSET sizeof(uint32_t)
37
38 /**
39  * Minimum size of serialized BuxtonData
40  * 2 is the minimum number of characters in a valid SMACK label
41  * 0 is the mimimum number of characters in a valid value (NULL STRING)
42  */
43 #define BXT_MINIMUM_SIZE sizeof(BuxtonDataType) \
44         + (sizeof(uint32_t) * 2)                \
45         + 2
46
47 /**
48  * Length of valid message header
49  */
50 #define BUXTON_MESSAGE_HEADER_LENGTH sizeof(uint32_t)   \
51         + sizeof(uint32_t)
52 /**
53  * Maximum length of valid control message
54  */
55 #define BUXTON_MESSAGE_MAX_LENGTH 32768
56
57 /**
58  * Maximum length of valid control message
59  */
60 #define BUXTON_MESSAGE_MAX_PARAMS 16
61
62 /**
63  * Serialize data internally for backend consumption
64  * @param source Data to be serialized
65  * @param label Label to be serialized
66  * @param target Pointer to store serialized data in
67  * @return a size_t value, indicating the size of serialized data
68  */
69 size_t buxton_serialize(BuxtonData *source, BuxtonString *label,
70                         uint8_t **target)
71         __attribute__((warn_unused_result));
72
73 /**
74  * Deserialize internal data for client consumption
75  * @param source Serialized data pointer
76  * @param target A pointer where the deserialize data will be stored
77  * @param label A pointer where the deserialize label will be stored
78  */
79 void buxton_deserialize(uint8_t *source, BuxtonData *target,
80                         BuxtonString *label);
81
82 /**
83  * Serialize an internal buxton message for wire communication
84  * @param dest Pointer to store serialized message in
85  * @param message The type of message to be serialized
86  * @param msgid The message ID to be serialized
87  * @param list An array of BuxtonData's to be serialized
88  * @return a size_t, 0 indicates failure otherwise size of dest
89  */
90 size_t buxton_serialize_message(uint8_t **dest,
91                                 BuxtonControlMessage message,
92                                 uint32_t msgid,
93                                 BuxtonArray *list)
94         __attribute__((warn_unused_result));
95
96 /**
97  * Deserialize the given data into an array of BuxtonData structs
98  * @param data The source data to be deserialized
99  * @param r_message An empty pointer that will be set to the message type
100  * @param size The size of the data being deserialized
101  * @param r_msgid The message ID being deserialized
102  * @param list A pointer that will be filled out as an array of BuxtonData structs
103  * @return the length of the array, or -1 if deserialization failed
104  */
105 ssize_t buxton_deserialize_message(uint8_t *data,
106                                   BuxtonControlMessage *r_message,
107                                   size_t size, uint32_t *r_msgid,
108                                   BuxtonData **list)
109         __attribute__((warn_unused_result));
110
111 /**
112  * Get size of a buxton message data stream
113  * @param data The source data stream
114  * @param size The size of the data stream (from read)
115  * @return a size_t length of the complete message or 0
116  */
117 size_t buxton_get_message_size(uint8_t *data, size_t size)
118         __attribute__((warn_unused_result));
119
120 void include_serialize(void);
121
122 /*
123  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
124  *
125  * Local variables:
126  * c-basic-offset: 8
127  * tab-width: 8
128  * indent-tabs-mode: t
129  * End:
130  *
131  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
132  * :indentSize=8:tabSize=8:noTabs=false:
133  */