summaryrefslogtreecommitdiff
path: root/src/lib/eldbus/eldbus_message_from_eina_value.c
diff options
context:
space:
mode:
authorLucas De Marchi <lucas.demarchi@profusion.mobi>2013-04-23 11:40:05 -0300
committerLucas De Marchi <lucas.demarchi@profusion.mobi>2013-04-23 12:36:29 -0300
commit4e3804041f8e865e007b2a6a0d45a05f39500c3b (patch)
treea493b9c8f8a610d2667651f3ba81819ed16fbe8c /src/lib/eldbus/eldbus_message_from_eina_value.c
parent4cf68bf728b5a858ce1c0efac25ded1a49ba8d9d (diff)
Rename edbus->eldbus
git grep -l edbus2 | while read f; do sed -i 's/edbus2/eldbus/g' "$f"; done find . -name '*edbus2*' -exec rename edbus2 eldbus {} \; git grep -l "EDBUS" | while read f; do sed -i 's/EDBUS/ELDBUS/g' "$f"; done git grep -l "EDBus" | while read f; do sed -i 's/EDBus/Eldbus/g' "$f"; done git grep -l "edbus (v2)" | while read f; do sed -i 's/edbus (v2)/eldbus/g' "$f"; done git grep -l "Edbus" | while read f; do sed -i 's/Edbus/Eldbus/g' "$f"; done git grep -l "edbus" | while read f; do sed -i 's/edbus/eldbus/g' "$f"; done find . -name '*edbus*' -exec rename edbus eldbus {} \; find . -name '*EDBus*' -exec rename EDBus Eldbus {} \;
Diffstat (limited to 'src/lib/eldbus/eldbus_message_from_eina_value.c')
-rw-r--r--src/lib/eldbus/eldbus_message_from_eina_value.c370
1 files changed, 370 insertions, 0 deletions
diff --git a/src/lib/eldbus/eldbus_message_from_eina_value.c b/src/lib/eldbus/eldbus_message_from_eina_value.c
new file mode 100644
index 0000000000..f94ca42846
--- /dev/null
+++ b/src/lib/eldbus/eldbus_message_from_eina_value.c
@@ -0,0 +1,370 @@
1#include "eldbus_private.h"
2#include "eldbus_private_types.h"
3
4static Eina_Bool
5_compatible_type(int dbus_type, const Eina_Value_Type *value_type)
6{
7 switch (dbus_type)
8 {
9 case 'i':
10 case 'h':
11 return value_type == EINA_VALUE_TYPE_INT;
12 case 's':
13 case 'o':
14 case 'g':
15 return value_type == EINA_VALUE_TYPE_STRING;
16 case 'b':
17 case 'y':
18 return value_type == EINA_VALUE_TYPE_UCHAR;
19 case 'n':
20 return value_type == EINA_VALUE_TYPE_SHORT;
21 case 'q':
22 return value_type == EINA_VALUE_TYPE_USHORT;
23 case 'u':
24 return value_type == EINA_VALUE_TYPE_UINT;
25 case 'x':
26 return value_type == EINA_VALUE_TYPE_INT64;
27 case 't':
28 return value_type == EINA_VALUE_TYPE_UINT64;
29 case 'd':
30 return value_type == EINA_VALUE_TYPE_DOUBLE;
31 case 'a':
32 return value_type == EINA_VALUE_TYPE_ARRAY;
33 case '(':
34 case '{':
35 case 'e':
36 case 'r':
37 return value_type == EINA_VALUE_TYPE_STRUCT;
38 default:
39 ERR("Unknown type %c", dbus_type);
40 return EINA_FALSE;
41 }
42}
43
44static Eina_Bool
45_array_append(const char *type, const Eina_Value *value_array, Eldbus_Message_Iter *iter)
46{
47 Eldbus_Message_Iter *array;
48
49 EINA_SAFETY_ON_FALSE_RETURN_VAL(
50 eldbus_message_iter_arguments_append(iter, type, &array), EINA_FALSE);
51 DBG("array of type %c", type[1]);
52 switch (type[1])
53 {
54 case '{':
55 case '(':
56 {
57 unsigned i = strlen(type+2);//remove 'a()' of len a(sv)
58 char *entry_sig = malloc(sizeof(char) * i);
59 memcpy(entry_sig, type+2, i);
60 entry_sig[i-1] = 0;
61
62 for (i = 0; i < eina_value_array_count(value_array); i++)
63 {
64 Eina_Value st;
65 Eldbus_Message_Iter *entry;
66 eina_value_array_value_get(value_array, i, &st);
67 eldbus_message_iter_arguments_append(array, type+1, &entry);
68 _message_iter_from_eina_value_struct(entry_sig, entry, &st);
69 eldbus_message_iter_container_close(array, entry);
70 eina_value_flush(&st);
71 }
72 free(entry_sig);
73 break;
74 }
75 case 'a':
76 {
77 unsigned i;
78 for (i = 0; i < eina_value_array_count(value_array); i++)
79 {
80 Eina_Value inner_array;
81 Eldbus_Message_Iter *sub_array;
82 eina_value_array_value_get(value_array, i, &inner_array);
83 eldbus_message_iter_arguments_append(array, type+1, &sub_array);
84 _array_append(type+1, &inner_array, sub_array);
85 eldbus_message_iter_container_close(array, sub_array);
86 eina_value_flush(&inner_array);
87 }
88 break;
89 }
90 case 'v':
91 {
92 ERR("Variant not supported.");
93 return EINA_FALSE;
94 }
95 case 'i':
96 case 'h'://fd
97 {
98 int32_t z;
99 unsigned i;
100 for (i = 0; i < eina_value_array_count(value_array); i++)
101 {
102 eina_value_array_get(value_array, i, &z);
103 eldbus_message_iter_basic_append(array, type[1], z);
104 }
105 break;
106 }
107 case 's':
108 case 'o'://object path
109 case 'g'://signature
110 {
111 const char *txt;
112 unsigned i;
113 for (i = 0; i < eina_value_array_count(value_array); i++)
114 {
115 eina_value_array_get(value_array, i, &txt);
116 eldbus_message_iter_basic_append(array, type[1], txt);
117 }
118 break;
119 }
120 case 'b'://boolean
121 case 'y'://byte
122 {
123 unsigned char z;
124 unsigned i;
125 for (i = 0; i < eina_value_array_count(value_array); i++)
126 {
127 eina_value_array_get(value_array, i, &z);
128 eldbus_message_iter_basic_append(array, type[1], z);
129 }
130 break;
131 }
132 case 'n'://int16
133 {
134 int16_t z;
135 unsigned i;
136 for (i = 0; i < eina_value_array_count(value_array); i++)
137 {
138 eina_value_array_get(value_array, i, &z);
139 eldbus_message_iter_basic_append(array, type[1], z);
140 }
141 break;
142 }
143 case 'q'://uint16
144 {
145 uint16_t z;
146 unsigned i;
147 for (i = 0; i < eina_value_array_count(value_array); i++)
148 {
149 eina_value_array_get(value_array, i, &z);
150 eldbus_message_iter_basic_append(array, type[1], z);
151 }
152 break;
153 }
154 case 'u'://uint32
155 {
156 uint32_t z;
157 unsigned i;
158 for (i = 0; i < eina_value_array_count(value_array); i++)
159 {
160 eina_value_array_get(value_array, i, &z);
161 eldbus_message_iter_basic_append(array, type[1], z);
162 }
163 break;
164 }
165 case 'x'://int64
166 {
167 int64_t z;
168 unsigned i;
169 for (i = 0; i < eina_value_array_count(value_array); i++)
170 {
171 eina_value_array_get(value_array, i, &z);
172 eldbus_message_iter_basic_append(array, type[1], z);
173 }
174 break;
175 }
176 case 't'://uint64
177 {
178 uint64_t z;
179 unsigned i;
180 for (i = 0; i < eina_value_array_count(value_array); i++)
181 {
182 eina_value_array_get(value_array, i, &z);
183 eldbus_message_iter_basic_append(array, type[1], z);
184 }
185 break;
186 }
187 case 'd'://double
188 {
189 double z;
190 unsigned i;
191 for (i = 0; i < eina_value_array_count(value_array); i++)
192 {
193 eina_value_array_get(value_array, i, &z);
194 eldbus_message_iter_basic_append(array, type[1], z);
195 }
196 break;
197 }
198 default:
199 {
200 ERR("Unknown type %c", type[1]);
201 return EINA_FALSE;
202 }
203 }
204 eldbus_message_iter_container_close(iter, array);
205 return EINA_TRUE;
206}
207
208static Eina_Bool
209_basic_append(char type, const Eina_Value *value, const Eina_Value_Struct_Desc *desc, unsigned idx, Eldbus_Message_Iter *iter)
210{
211 EINA_SAFETY_ON_FALSE_RETURN_VAL(
212 _compatible_type(type, desc->members[idx].type), EINA_FALSE);
213 switch (type)
214 {
215 case 'i'://int
216 case 'h'://fd
217 {
218 int32_t i;
219 eina_value_struct_get(value, desc->members[idx].name, &i);
220 eldbus_message_iter_basic_append(iter, type, i);
221 break;
222 }
223 case 's':
224 case 'o'://object path
225 case 'g'://signature
226 {
227 const char *txt;
228 eina_value_struct_get(value, desc->members[idx].name, &txt);
229 eldbus_message_iter_basic_append(iter, type, txt);
230 break;
231 }
232 case 'b'://boolean
233 case 'y'://byte
234 {
235 unsigned char byte;
236 eina_value_struct_get(value, desc->members[idx].name, &byte);
237 eldbus_message_iter_basic_append(iter, type, byte);
238 break;
239 }
240 case 'n'://int16
241 {
242 int16_t i;
243 eina_value_struct_get(value, desc->members[idx].name, &i);
244 eldbus_message_iter_basic_append(iter, type, i);
245 break;
246 }
247 case 'q'://uint16
248 {
249 uint16_t i;
250 eina_value_struct_get(value, desc->members[idx].name, &i);
251 eldbus_message_iter_basic_append(iter, type, i);
252 break;
253 }
254 case 'u'://uint32
255 {
256 uint32_t i;
257 eina_value_struct_get(value, desc->members[idx].name, &i);
258 eldbus_message_iter_basic_append(iter, type, i);
259 break;
260 }
261 case 'x'://int64
262 {
263 int64_t i;
264 eina_value_struct_get(value, desc->members[idx].name, &i);
265 eldbus_message_iter_basic_append(iter, type, i);
266 break;
267 }
268 case 't'://uint64
269 {
270 uint64_t i;
271 eina_value_struct_get(value, desc->members[idx].name, &i);
272 eldbus_message_iter_basic_append(iter, type, i);
273 break;
274 }
275 case 'd'://double
276 {
277 double d;
278 eina_value_struct_get(value, desc->members[idx].name, &d);
279 eldbus_message_iter_basic_append(iter, type, d);
280 break;
281 }
282 default:
283 ERR("Unexpected type %c", type);
284 return EINA_FALSE;
285 }
286 return EINA_TRUE;
287}
288
289Eina_Bool
290_message_iter_from_eina_value_struct(const char *signature, Eldbus_Message_Iter *iter, const Eina_Value *value)
291{
292 unsigned i;
293 DBusSignatureIter signature_iter;
294 Eina_Bool r = EINA_TRUE;
295 char *type;
296 Eina_Value_Struct st;
297
298 EINA_SAFETY_ON_FALSE_RETURN_VAL(
299 eina_value_type_get(value) == EINA_VALUE_TYPE_STRUCT, EINA_FALSE);
300 eina_value_pget(value, &st);
301
302 dbus_signature_iter_init(&signature_iter, signature);
303 i = 0;
304 while ((type = dbus_signature_iter_get_signature(&signature_iter)))
305 {
306 DBG("type: %s", type);
307 if (type[0] != 'v' && !type[1])
308 r = _basic_append(type[0], value, st.desc, i, iter);
309 else if (type[0] == 'a')
310 {
311 Eina_Value value_array;
312
313 EINA_SAFETY_ON_FALSE_RETURN_VAL(
314 _compatible_type(type[0], st.desc->members[i].type),
315 EINA_FALSE);
316 eina_value_struct_value_get(value, st.desc->members[i].name,
317 &value_array);
318 r = _array_append(type, &value_array, iter);
319 eina_value_flush(&value_array);
320 }
321 else if (type[0] == '(')
322 {
323 Eina_Value inner_st;
324 Eldbus_Message_Iter *sub_iter;
325 char *sub_sig;
326 unsigned len = strlen(type+1) -1;
327 sub_sig = malloc(sizeof(char) * len);
328 memcpy(sub_sig, type+1, len);
329 sub_sig[len-1] = 0;
330 EINA_SAFETY_ON_FALSE_RETURN_VAL(
331 _compatible_type(type[0], st.desc->members[i].type),
332 EINA_FALSE);
333 eina_value_struct_value_get(value, st.desc->members[i].name, &inner_st);
334 eldbus_message_iter_arguments_append(iter, type, &sub_iter);
335 r = _message_iter_from_eina_value_struct(sub_sig, sub_iter,
336 &inner_st);
337 eldbus_message_iter_container_close(iter, sub_iter);
338 free(sub_sig);
339 }
340 else if (type[0] == 'v')
341 {
342 ERR("Variant not supported");
343 r = EINA_FALSE;
344 }
345 else
346 {
347 ERR("Unknown type %c", type[0]);
348 r = EINA_FALSE;
349 }
350 i++;
351 dbus_free(type);
352 if (!r || !dbus_signature_iter_next(&signature_iter)) break;
353 }
354 return r;
355}
356
357EAPI Eina_Bool
358eldbus_message_from_eina_value(const char *signature, Eldbus_Message *msg, const Eina_Value *value)
359{
360 Eldbus_Message_Iter *iter;
361 EINA_SAFETY_ON_NULL_RETURN_VAL(signature, EINA_FALSE);
362 EINA_SAFETY_ON_NULL_RETURN_VAL(msg, EINA_FALSE);
363 EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE);
364
365 iter = eldbus_message_iter_get(msg);
366 EINA_SAFETY_ON_NULL_RETURN_VAL(iter, EINA_FALSE);
367 EINA_SAFETY_ON_FALSE_RETURN_VAL(iter->writable, EINA_FALSE);
368
369 return _message_iter_from_eina_value_struct(signature, iter, value);
370}