| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <cui-dbus.h> |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <stdio.h> |
| 27 |
|
| 28 |
static const char *map_basic_to_signature(int dbus_type) |
| 29 |
{ |
| 30 |
switch (dbus_type) { |
| 31 |
case DBUS_TYPE_BOOLEAN: |
| 32 |
return DBUS_TYPE_BOOLEAN_AS_STRING; |
| 33 |
case DBUS_TYPE_INT16: |
| 34 |
return DBUS_TYPE_INT16_AS_STRING; |
| 35 |
case DBUS_TYPE_UINT16: |
| 36 |
return DBUS_TYPE_UINT16_AS_STRING; |
| 37 |
case DBUS_TYPE_INT32: |
| 38 |
return DBUS_TYPE_INT32_AS_STRING; |
| 39 |
case DBUS_TYPE_UINT32: |
| 40 |
return DBUS_TYPE_UINT32_AS_STRING; |
| 41 |
case DBUS_TYPE_DOUBLE: |
| 42 |
return DBUS_TYPE_DOUBLE_AS_STRING; |
| 43 |
case DBUS_TYPE_STRING: |
| 44 |
return DBUS_TYPE_STRING_AS_STRING; |
| 45 |
case DBUS_TYPE_OBJECT_PATH: |
| 46 |
return DBUS_TYPE_OBJECT_PATH_AS_STRING; |
| 47 |
case DBUS_TYPE_SIGNATURE: |
| 48 |
return DBUS_TYPE_SIGNATURE_AS_STRING; |
| 49 |
case DBUS_TYPE_DICT_ENTRY: |
| 50 |
return DBUS_TYPE_DICT_ENTRY_AS_STRING; |
| 51 |
case DBUS_TYPE_BYTE: |
| 52 |
return DBUS_TYPE_BYTE_AS_STRING; |
| 53 |
default: |
| 54 |
break; |
| 55 |
} |
| 56 |
|
| 57 |
return DBUS_TYPE_VARIANT_AS_STRING; |
| 58 |
} |
| 59 |
|
| 60 |
static const char *map_array_to_signature(int dbus_type) |
| 61 |
{ |
| 62 |
switch (dbus_type) { |
| 63 |
case DBUS_TYPE_BOOLEAN: |
| 64 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BOOLEAN_AS_STRING; |
| 65 |
case DBUS_TYPE_INT16: |
| 66 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_INT16_AS_STRING; |
| 67 |
case DBUS_TYPE_UINT16: |
| 68 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_UINT16_AS_STRING; |
| 69 |
case DBUS_TYPE_INT32: |
| 70 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_INT32_AS_STRING; |
| 71 |
case DBUS_TYPE_UINT32: |
| 72 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_UINT32_AS_STRING; |
| 73 |
case DBUS_TYPE_DOUBLE: |
| 74 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_DOUBLE_AS_STRING; |
| 75 |
case DBUS_TYPE_STRING: |
| 76 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING; |
| 77 |
case DBUS_TYPE_OBJECT_PATH: |
| 78 |
return DBUS_TYPE_ARRAY_AS_STRING |
| 79 |
DBUS_TYPE_OBJECT_PATH_AS_STRING; |
| 80 |
case DBUS_TYPE_SIGNATURE: |
| 81 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_SIGNATURE_AS_STRING; |
| 82 |
case DBUS_TYPE_DICT_ENTRY: |
| 83 |
return DBUS_TYPE_ARRAY_AS_STRING |
| 84 |
DBUS_TYPE_DICT_ENTRY_AS_STRING; |
| 85 |
case DBUS_TYPE_BYTE: |
| 86 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_ARRAY_AS_STRING; |
| 87 |
default: |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
return DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_VARIANT_AS_STRING; |
| 92 |
} |
| 93 |
|
| 94 |
void cui_dbus_append_dict(DBusMessageIter *iter, |
| 95 |
const char *key_name, |
| 96 |
cui_dbus_property_f property_function, |
| 97 |
void *user_data) |
| 98 |
{ |
| 99 |
DBusMessageIter dict, variant, *vdict; |
| 100 |
|
| 101 |
if (key_name != NULL) { |
| 102 |
dbus_message_iter_append_basic(iter, |
| 103 |
DBUS_TYPE_STRING, &key_name); |
| 104 |
|
| 105 |
dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, |
| 106 |
DBUS_TYPE_ARRAY_AS_STRING |
| 107 |
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING |
| 108 |
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING |
| 109 |
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &variant); |
| 110 |
|
| 111 |
vdict = &variant; |
| 112 |
} else |
| 113 |
vdict = iter; |
| 114 |
|
| 115 |
dbus_message_iter_open_container(vdict, DBUS_TYPE_ARRAY, |
| 116 |
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING |
| 117 |
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING |
| 118 |
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict); |
| 119 |
|
| 120 |
if (property_function != NULL) |
| 121 |
property_function(&dict, user_data); |
| 122 |
|
| 123 |
dbus_message_iter_close_container(vdict, &dict); |
| 124 |
|
| 125 |
if (key_name != NULL) |
| 126 |
dbus_message_iter_close_container(iter, &variant); |
| 127 |
} |
| 128 |
|
| 129 |
void cui_dbus_append_basic(DBusMessageIter *iter, |
| 130 |
const char *key_name, |
| 131 |
int dbus_type, |
| 132 |
void *value) |
| 133 |
{ |
| 134 |
if (key_name != NULL) { |
| 135 |
DBusMessageIter basic; |
| 136 |
|
| 137 |
dbus_message_iter_append_basic(iter, |
| 138 |
DBUS_TYPE_STRING, &key_name); |
| 139 |
|
| 140 |
dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, |
| 141 |
map_basic_to_signature(dbus_type), &basic); |
| 142 |
|
| 143 |
dbus_message_iter_append_basic(&basic, dbus_type, value); |
| 144 |
|
| 145 |
dbus_message_iter_close_container(iter, &basic); |
| 146 |
} else |
| 147 |
dbus_message_iter_append_basic(iter, dbus_type, value); |
| 148 |
} |
| 149 |
|
| 150 |
void cui_dbus_append_array(DBusMessageIter *iter, |
| 151 |
const char *key_name, |
| 152 |
int dbus_type, |
| 153 |
cui_dbus_property_f property_function, |
| 154 |
void *user_data) |
| 155 |
{ |
| 156 |
DBusMessageIter variant, array, *varray; |
| 157 |
|
| 158 |
if (key_name != NULL) { |
| 159 |
dbus_message_iter_append_basic(iter, |
| 160 |
DBUS_TYPE_STRING, &key_name); |
| 161 |
|
| 162 |
dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, |
| 163 |
map_array_to_signature(dbus_type), &variant); |
| 164 |
|
| 165 |
varray = &variant; |
| 166 |
} else |
| 167 |
varray = iter; |
| 168 |
|
| 169 |
dbus_message_iter_open_container(varray, DBUS_TYPE_ARRAY, |
| 170 |
map_basic_to_signature(dbus_type), &array); |
| 171 |
|
| 172 |
if (property_function != NULL) |
| 173 |
property_function(&array, user_data); |
| 174 |
|
| 175 |
dbus_message_iter_close_container(varray, &array); |
| 176 |
|
| 177 |
if (key_name != NULL) |
| 178 |
dbus_message_iter_close_container(iter, &variant); |
| 179 |
} |
| 180 |
|
| 181 |
void cui_dbus_append_fixed_array(DBusMessageIter *iter, |
| 182 |
const char *key_name, |
| 183 |
int dbus_type, |
| 184 |
int length, |
| 185 |
void *value) |
| 186 |
{ |
| 187 |
DBusMessageIter variant, array, *varray; |
| 188 |
|
| 189 |
if (key_name != NULL) { |
| 190 |
dbus_message_iter_append_basic(iter, |
| 191 |
DBUS_TYPE_STRING, &key_name); |
| 192 |
|
| 193 |
dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, |
| 194 |
map_array_to_signature(DBUS_TYPE_BYTE), &variant); |
| 195 |
|
| 196 |
varray = &variant; |
| 197 |
} else |
| 198 |
varray = iter; |
| 199 |
|
| 200 |
dbus_message_iter_open_container(varray, DBUS_TYPE_ARRAY, |
| 201 |
map_basic_to_signature(DBUS_TYPE_BYTE), &array); |
| 202 |
|
| 203 |
dbus_message_iter_append_fixed_array(&array, dbus_type, value, length); |
| 204 |
|
| 205 |
dbus_message_iter_close_container(varray, &array); |
| 206 |
|
| 207 |
if (key_name != NULL) |
| 208 |
dbus_message_iter_close_container(iter, &variant); |
| 209 |
} |
| 210 |
|
| 211 |
void cui_dbus_append_dict_entry(DBusMessageIter *dict, |
| 212 |
const char *key_name, |
| 213 |
enum cui_dbus_entry entry_type, |
| 214 |
int dbus_type, |
| 215 |
int length, |
| 216 |
cui_dbus_property_f property_function, |
| 217 |
void *user_data) |
| 218 |
{ |
| 219 |
DBusMessageIter entry; |
| 220 |
|
| 221 |
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, |
| 222 |
NULL, &entry); |
| 223 |
|
| 224 |
switch (entry_type) { |
| 225 |
case CUI_DBUS_ENTRY_BASIC: |
| 226 |
cui_dbus_append_basic(&entry, key_name, |
| 227 |
dbus_type, user_data); |
| 228 |
break; |
| 229 |
case CUI_DBUS_ENTRY_ARRAY: |
| 230 |
cui_dbus_append_array(&entry, key_name, dbus_type, |
| 231 |
property_function, user_data); |
| 232 |
break; |
| 233 |
case CUI_DBUS_ENTRY_FIXED_ARRAY: |
| 234 |
cui_dbus_append_fixed_array(&entry, key_name, |
| 235 |
dbus_type, length, user_data); |
| 236 |
break; |
| 237 |
case CUI_DBUS_ENTRY_DICT: |
| 238 |
cui_dbus_append_dict(&entry, key_name, |
| 239 |
property_function, user_data); |
| 240 |
break; |
| 241 |
default: |
| 242 |
break; |
| 243 |
} |
| 244 |
|
| 245 |
dbus_message_iter_close_container(dict, &entry); |
| 246 |
} |
| 247 |
|
| 248 |
int cui_dbus_get_basic(DBusMessageIter *iter, |
| 249 |
int dbus_type, |
| 250 |
void *destination) |
| 251 |
{ |
| 252 |
if (destination == NULL) |
| 253 |
return -EINVAL; |
| 254 |
|
| 255 |
if (dbus_message_iter_get_arg_type(iter) != dbus_type) |
| 256 |
return -EINVAL; |
| 257 |
|
| 258 |
dbus_message_iter_get_basic(iter, destination); |
| 259 |
|
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
int cui_dbus_get_basic_variant(DBusMessageIter *iter, |
| 264 |
int dbus_type, |
| 265 |
void *destination) |
| 266 |
{ |
| 267 |
DBusMessageIter variant; |
| 268 |
|
| 269 |
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_VARIANT) |
| 270 |
return -EINVAL; |
| 271 |
|
| 272 |
dbus_message_iter_recurse(iter, &variant); |
| 273 |
|
| 274 |
if (dbus_message_iter_get_arg_type(&variant) != dbus_type) |
| 275 |
return -EINVAL; |
| 276 |
|
| 277 |
dbus_message_iter_get_basic(&variant, destination); |
| 278 |
|
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
int cui_dbus_get_array(DBusMessageIter *iter, |
| 283 |
int dbus_type, |
| 284 |
int *length, |
| 285 |
void *destination) |
| 286 |
{ |
| 287 |
DBusMessageIter variant, array; |
| 288 |
void **value_array = NULL; |
| 289 |
int iteration; |
| 290 |
int arg_type; |
| 291 |
size_t size; |
| 292 |
|
| 293 |
if (length == NULL || destination == NULL) |
| 294 |
return -EINVAL; |
| 295 |
|
| 296 |
switch (dbus_type) { |
| 297 |
case DBUS_TYPE_BOOLEAN: |
| 298 |
case DBUS_TYPE_INT16: |
| 299 |
case DBUS_TYPE_INT32: |
| 300 |
case DBUS_TYPE_UINT16: |
| 301 |
case DBUS_TYPE_UINT32: |
| 302 |
size = sizeof(int); |
| 303 |
break; |
| 304 |
case DBUS_TYPE_DOUBLE: |
| 305 |
size = sizeof(double); |
| 306 |
break; |
| 307 |
case DBUS_TYPE_STRING: |
| 308 |
case DBUS_TYPE_OBJECT_PATH: |
| 309 |
case DBUS_TYPE_SIGNATURE: |
| 310 |
size = sizeof(char *); |
| 311 |
break; |
| 312 |
default: |
| 313 |
return -EINVAL; |
| 314 |
} |
| 315 |
|
| 316 |
*length = 0; |
| 317 |
iteration = 0; |
| 318 |
*((void **) destination) = NULL; |
| 319 |
|
| 320 |
arg_type = dbus_message_iter_get_arg_type(iter); |
| 321 |
if (arg_type == DBUS_TYPE_VARIANT) { |
| 322 |
dbus_message_iter_recurse(iter, &variant); |
| 323 |
dbus_message_iter_recurse(&variant, &array); |
| 324 |
} else |
| 325 |
dbus_message_iter_recurse(iter, &array); |
| 326 |
|
| 327 |
arg_type = dbus_message_iter_get_arg_type(&array); |
| 328 |
while (arg_type != DBUS_TYPE_INVALID) { |
| 329 |
void **new_array; |
| 330 |
|
| 331 |
iteration++; |
| 332 |
|
| 333 |
new_array = realloc(value_array, size * (iteration + 1)); |
| 334 |
if (new_array == NULL) |
| 335 |
goto error; |
| 336 |
|
| 337 |
value_array = new_array; |
| 338 |
|
| 339 |
if (arg_type == DBUS_TYPE_VARIANT) { |
| 340 |
if (cui_dbus_get_basic_variant(&array, dbus_type, |
| 341 |
&value_array[iteration-1]) != 0) |
| 342 |
goto error; |
| 343 |
} else |
| 344 |
dbus_message_iter_get_basic(&array, |
| 345 |
&value_array[iteration-1]); |
| 346 |
|
| 347 |
dbus_message_iter_next(&array); |
| 348 |
|
| 349 |
arg_type = dbus_message_iter_get_arg_type(&array); |
| 350 |
} |
| 351 |
|
| 352 |
if (iteration > 0) { |
| 353 |
value_array[iteration] = NULL; |
| 354 |
*length = iteration - 1; |
| 355 |
} |
| 356 |
|
| 357 |
*((void **) destination) = value_array; |
| 358 |
|
| 359 |
return 0; |
| 360 |
|
| 361 |
error: |
| 362 |
if (value_array != NULL) |
| 363 |
free(value_array); |
| 364 |
|
| 365 |
return -ENOMEM; |
| 366 |
} |
| 367 |
|
| 368 |
int cui_dbus_get_fixed_array(DBusMessageIter *iter, |
| 369 |
int *length, |
| 370 |
void *destination) |
| 371 |
{ |
| 372 |
DBusMessageIter farray; |
| 373 |
|
| 374 |
if (length == NULL || destination == NULL) |
| 375 |
return -EINVAL; |
| 376 |
|
| 377 |
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) |
| 378 |
return -EINVAL; |
| 379 |
|
| 380 |
if (dbus_message_iter_get_element_type(iter) != DBUS_TYPE_BYTE) |
| 381 |
return -EINVAL; |
| 382 |
|
| 383 |
dbus_message_iter_recurse(iter, &farray); |
| 384 |
|
| 385 |
dbus_message_iter_get_fixed_array(&farray, destination, length); |
| 386 |
|
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
static int cui_dbus_get(DBusMessageIter *iter, |
| 391 |
enum cui_dbus_entry entry_type, |
| 392 |
int dbus_type, |
| 393 |
int *length, |
| 394 |
void *destination) |
| 395 |
{ |
| 396 |
switch (entry_type) { |
| 397 |
case CUI_DBUS_ENTRY_BASIC: |
| 398 |
return cui_dbus_get_basic(iter, |
| 399 |
dbus_type, destination); |
| 400 |
case CUI_DBUS_ENTRY_ARRAY: |
| 401 |
return cui_dbus_get_array(iter, |
| 402 |
dbus_type, length, destination); |
| 403 |
case CUI_DBUS_ENTRY_FIXED_ARRAY: |
| 404 |
return cui_dbus_get_fixed_array(iter, |
| 405 |
length, destination); |
| 406 |
case CUI_DBUS_ENTRY_DICT: |
| 407 |
if (destination == NULL) |
| 408 |
return -EINVAL; |
| 409 |
|
| 410 |
dbus_message_iter_recurse(iter, |
| 411 |
(DBusMessageIter *)destination); |
| 412 |
return 0; |
| 413 |
default: |
| 414 |
break; |
| 415 |
} |
| 416 |
|
| 417 |
return -EINVAL; |
| 418 |
} |
| 419 |
|
| 420 |
int cui_dbus_foreach_dict_entry(DBusMessageIter *iter, |
| 421 |
cui_dbus_foreach_callback_f callback, |
| 422 |
void *user_data) |
| 423 |
{ |
| 424 |
DBusMessageIter array, dict_entry; |
| 425 |
int arg_type; |
| 426 |
|
| 427 |
arg_type = dbus_message_iter_get_arg_type(iter); |
| 428 |
if (arg_type != DBUS_TYPE_ARRAY) |
| 429 |
return -EINVAL; |
| 430 |
|
| 431 |
if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) |
| 432 |
return -EINVAL; |
| 433 |
|
| 434 |
dbus_message_iter_recurse(iter, &array); |
| 435 |
|
| 436 |
arg_type = dbus_message_iter_get_arg_type(&array); |
| 437 |
while (arg_type != DBUS_TYPE_INVALID) { |
| 438 |
if (arg_type != DBUS_TYPE_DICT_ENTRY) |
| 439 |
return -EINVAL; |
| 440 |
|
| 441 |
dbus_message_iter_recurse(&array, &dict_entry); |
| 442 |
|
| 443 |
if (callback(&dict_entry, user_data) == TRUE) |
| 444 |
return 0; |
| 445 |
|
| 446 |
dbus_message_iter_next(&array); |
| 447 |
arg_type = dbus_message_iter_get_arg_type(&array); |
| 448 |
} |
| 449 |
|
| 450 |
return -EINVAL; |
| 451 |
} |
| 452 |
|
| 453 |
struct dict_entry_parameters { |
| 454 |
const char *key_name; |
| 455 |
enum cui_dbus_entry entry_type; |
| 456 |
int dbus_type; |
| 457 |
int *length; |
| 458 |
void *destination; |
| 459 |
}; |
| 460 |
|
| 461 |
static bool get_dict_entry_cb(DBusMessageIter *iter, void *user_data) |
| 462 |
{ |
| 463 |
struct dict_entry_parameters *param = user_data; |
| 464 |
DBusMessageIter dict_value; |
| 465 |
char *name; |
| 466 |
|
| 467 |
dbus_message_iter_get_basic(iter, &name); |
| 468 |
|
| 469 |
if (strncmp(param->key_name, name, strlen(param->key_name)) == 0) { |
| 470 |
dbus_message_iter_next(iter); |
| 471 |
dbus_message_iter_recurse(iter, &dict_value); |
| 472 |
|
| 473 |
cui_dbus_get(&dict_value, param->entry_type, |
| 474 |
param->dbus_type, param->length, param->destination); |
| 475 |
|
| 476 |
return TRUE; |
| 477 |
} |
| 478 |
|
| 479 |
return FALSE; |
| 480 |
} |
| 481 |
|
| 482 |
int cui_dbus_get_dict_entry(DBusMessageIter *iter, |
| 483 |
const char *key_name, |
| 484 |
enum cui_dbus_entry entry_type, |
| 485 |
int dbus_type, |
| 486 |
int *length, |
| 487 |
void *destination) |
| 488 |
{ |
| 489 |
struct dict_entry_parameters param; |
| 490 |
|
| 491 |
if (key_name == NULL) |
| 492 |
return -EINVAL; |
| 493 |
|
| 494 |
param.key_name = key_name; |
| 495 |
param.entry_type = entry_type; |
| 496 |
param.dbus_type = dbus_type; |
| 497 |
param.length = length; |
| 498 |
param.destination = destination; |
| 499 |
|
| 500 |
return cui_dbus_foreach_dict_entry(iter, get_dict_entry_cb, ¶m); |
| 501 |
} |
| 502 |
|
| 503 |
int cui_dbus_get_struct_entry(DBusMessageIter *iter, |
| 504 |
unsigned int position, |
| 505 |
enum cui_dbus_entry entry_type, |
| 506 |
int dbus_type, |
| 507 |
int *length, |
| 508 |
void *destination) |
| 509 |
{ |
| 510 |
DBusMessageIter struct_entry; |
| 511 |
unsigned int current = 1; |
| 512 |
int arg_type; |
| 513 |
|
| 514 |
arg_type = dbus_message_iter_get_arg_type(iter); |
| 515 |
if (arg_type != DBUS_TYPE_STRUCT) |
| 516 |
return -EINVAL; |
| 517 |
|
| 518 |
dbus_message_iter_recurse(iter, &struct_entry); |
| 519 |
|
| 520 |
arg_type = dbus_message_iter_get_arg_type(&struct_entry); |
| 521 |
|
| 522 |
while (arg_type != DBUS_TYPE_INVALID) { |
| 523 |
if (position != current) { |
| 524 |
dbus_message_iter_next(&struct_entry); |
| 525 |
arg_type = dbus_message_iter_get_arg_type(&struct_entry); |
| 526 |
|
| 527 |
current++; |
| 528 |
|
| 529 |
continue; |
| 530 |
} |
| 531 |
|
| 532 |
return cui_dbus_get(&struct_entry, entry_type, |
| 533 |
dbus_type, length, destination); |
| 534 |
} |
| 535 |
|
| 536 |
return -EINVAL; |
| 537 |
} |