]>
Commit | Line | Data |
---|---|---|
2f28d2ff AL |
1 | /* |
2 | * QEMU Object Model | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include "qemu/object.h" | |
14 | #include "qemu-common.h" | |
57c9fafe AL |
15 | #include "qapi/qapi-visit-core.h" |
16 | #include "hw/qdev.h" | |
17 | // FIXME remove above | |
2f28d2ff AL |
18 | |
19 | #define MAX_INTERFACES 32 | |
20 | ||
21 | typedef struct InterfaceImpl InterfaceImpl; | |
22 | typedef struct TypeImpl TypeImpl; | |
23 | ||
24 | struct InterfaceImpl | |
25 | { | |
26 | const char *parent; | |
27 | void (*interface_initfn)(ObjectClass *class, void *data); | |
28 | TypeImpl *type; | |
29 | }; | |
30 | ||
31 | struct TypeImpl | |
32 | { | |
33 | const char *name; | |
34 | ||
35 | size_t class_size; | |
36 | ||
37 | size_t instance_size; | |
38 | ||
39 | void (*class_init)(ObjectClass *klass, void *data); | |
40 | void (*class_finalize)(ObjectClass *klass, void *data); | |
41 | ||
42 | void *class_data; | |
43 | ||
44 | void (*instance_init)(Object *obj); | |
45 | void (*instance_finalize)(Object *obj); | |
46 | ||
47 | bool abstract; | |
48 | ||
49 | const char *parent; | |
50 | TypeImpl *parent_type; | |
51 | ||
52 | ObjectClass *class; | |
53 | ||
54 | int num_interfaces; | |
55 | InterfaceImpl interfaces[MAX_INTERFACES]; | |
56 | }; | |
57 | ||
58 | typedef struct Interface | |
59 | { | |
60 | Object parent; | |
61 | Object *obj; | |
62 | } Interface; | |
63 | ||
64 | #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE) | |
65 | ||
66 | static GHashTable *type_table_get(void) | |
67 | { | |
68 | static GHashTable *type_table; | |
69 | ||
70 | if (type_table == NULL) { | |
71 | type_table = g_hash_table_new(g_str_hash, g_str_equal); | |
72 | } | |
73 | ||
74 | return type_table; | |
75 | } | |
76 | ||
77 | static void type_table_add(TypeImpl *ti) | |
78 | { | |
79 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); | |
80 | } | |
81 | ||
82 | static TypeImpl *type_table_lookup(const char *name) | |
83 | { | |
84 | return g_hash_table_lookup(type_table_get(), name); | |
85 | } | |
86 | ||
87 | TypeImpl *type_register(const TypeInfo *info) | |
88 | { | |
89 | TypeImpl *ti = g_malloc0(sizeof(*ti)); | |
90 | ||
91 | g_assert(info->name != NULL); | |
92 | ||
73093354 AL |
93 | if (type_table_lookup(info->name) != NULL) { |
94 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); | |
95 | abort(); | |
96 | } | |
97 | ||
2f28d2ff AL |
98 | ti->name = g_strdup(info->name); |
99 | ti->parent = g_strdup(info->parent); | |
100 | ||
101 | ti->class_size = info->class_size; | |
102 | ti->instance_size = info->instance_size; | |
103 | ||
104 | ti->class_init = info->class_init; | |
105 | ti->class_finalize = info->class_finalize; | |
106 | ti->class_data = info->class_data; | |
107 | ||
108 | ti->instance_init = info->instance_init; | |
109 | ti->instance_finalize = info->instance_finalize; | |
110 | ||
111 | ti->abstract = info->abstract; | |
112 | ||
113 | if (info->interfaces) { | |
114 | int i; | |
115 | ||
116 | for (i = 0; info->interfaces[i].type; i++) { | |
117 | ti->interfaces[i].parent = info->interfaces[i].type; | |
118 | ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn; | |
119 | ti->num_interfaces++; | |
120 | } | |
121 | } | |
122 | ||
123 | type_table_add(ti); | |
124 | ||
125 | return ti; | |
126 | } | |
127 | ||
128 | TypeImpl *type_register_static(const TypeInfo *info) | |
129 | { | |
130 | return type_register(info); | |
131 | } | |
132 | ||
133 | static TypeImpl *type_get_by_name(const char *name) | |
134 | { | |
135 | if (name == NULL) { | |
136 | return NULL; | |
137 | } | |
138 | ||
139 | return type_table_lookup(name); | |
140 | } | |
141 | ||
142 | static TypeImpl *type_get_parent(TypeImpl *type) | |
143 | { | |
144 | if (!type->parent_type && type->parent) { | |
145 | type->parent_type = type_get_by_name(type->parent); | |
146 | g_assert(type->parent_type != NULL); | |
147 | } | |
148 | ||
149 | return type->parent_type; | |
150 | } | |
151 | ||
152 | static bool type_has_parent(TypeImpl *type) | |
153 | { | |
154 | return (type->parent != NULL); | |
155 | } | |
156 | ||
157 | static size_t type_class_get_size(TypeImpl *ti) | |
158 | { | |
159 | if (ti->class_size) { | |
160 | return ti->class_size; | |
161 | } | |
162 | ||
163 | if (type_has_parent(ti)) { | |
164 | return type_class_get_size(type_get_parent(ti)); | |
165 | } | |
166 | ||
167 | return sizeof(ObjectClass); | |
168 | } | |
169 | ||
170 | static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface) | |
171 | { | |
172 | TypeInfo info = { | |
173 | .instance_size = sizeof(Interface), | |
174 | .parent = iface->parent, | |
175 | .class_size = sizeof(InterfaceClass), | |
176 | .class_init = iface->interface_initfn, | |
177 | .abstract = true, | |
178 | }; | |
179 | char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent); | |
180 | ||
181 | info.name = name; | |
182 | iface->type = type_register(&info); | |
183 | g_free(name); | |
184 | } | |
185 | ||
186 | static void type_class_init(TypeImpl *ti) | |
187 | { | |
188 | size_t class_size = sizeof(ObjectClass); | |
189 | int i; | |
190 | ||
191 | if (ti->class) { | |
192 | return; | |
193 | } | |
194 | ||
195 | ti->class_size = type_class_get_size(ti); | |
196 | ||
197 | ti->class = g_malloc0(ti->class_size); | |
198 | ti->class->type = ti; | |
199 | ||
200 | if (type_has_parent(ti)) { | |
201 | TypeImpl *parent = type_get_parent(ti); | |
202 | ||
203 | type_class_init(parent); | |
204 | ||
205 | class_size = parent->class_size; | |
206 | g_assert(parent->class_size <= ti->class_size); | |
207 | ||
208 | memcpy((void *)ti->class + sizeof(ObjectClass), | |
209 | (void *)parent->class + sizeof(ObjectClass), | |
210 | parent->class_size - sizeof(ObjectClass)); | |
211 | } | |
212 | ||
213 | memset((void *)ti->class + class_size, 0, ti->class_size - class_size); | |
214 | ||
215 | for (i = 0; i < ti->num_interfaces; i++) { | |
216 | type_class_interface_init(ti, &ti->interfaces[i]); | |
217 | } | |
218 | ||
219 | if (ti->class_init) { | |
220 | ti->class_init(ti->class, ti->class_data); | |
221 | } | |
222 | } | |
223 | ||
224 | static void object_interface_init(Object *obj, InterfaceImpl *iface) | |
225 | { | |
226 | TypeImpl *ti = iface->type; | |
227 | Interface *iface_obj; | |
228 | ||
229 | iface_obj = INTERFACE(object_new(ti->name)); | |
230 | iface_obj->obj = obj; | |
231 | ||
232 | obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj); | |
233 | } | |
234 | ||
235 | static void object_init_with_type(Object *obj, TypeImpl *ti) | |
236 | { | |
237 | int i; | |
238 | ||
239 | if (type_has_parent(ti)) { | |
240 | object_init_with_type(obj, type_get_parent(ti)); | |
241 | } | |
242 | ||
243 | for (i = 0; i < ti->num_interfaces; i++) { | |
244 | object_interface_init(obj, &ti->interfaces[i]); | |
245 | } | |
246 | ||
247 | if (ti->instance_init) { | |
248 | ti->instance_init(obj); | |
249 | } | |
250 | } | |
251 | ||
252 | void object_initialize_with_type(void *data, TypeImpl *type) | |
253 | { | |
254 | Object *obj = data; | |
255 | ||
256 | g_assert(type != NULL); | |
257 | g_assert(type->instance_size >= sizeof(ObjectClass)); | |
258 | ||
259 | type_class_init(type); | |
260 | g_assert(type->abstract == false); | |
261 | ||
262 | memset(obj, 0, type->instance_size); | |
263 | obj->class = type->class; | |
57c9fafe | 264 | QTAILQ_INIT(&obj->properties); |
2f28d2ff AL |
265 | object_init_with_type(obj, type); |
266 | } | |
267 | ||
268 | void object_initialize(void *data, const char *typename) | |
269 | { | |
270 | TypeImpl *type = type_get_by_name(typename); | |
271 | ||
272 | object_initialize_with_type(data, type); | |
273 | } | |
274 | ||
57c9fafe AL |
275 | static void object_property_del_all(Object *obj) |
276 | { | |
277 | while (!QTAILQ_EMPTY(&obj->properties)) { | |
278 | ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); | |
279 | ||
280 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
281 | ||
282 | if (prop->release) { | |
283 | prop->release(obj, prop->name, prop->opaque); | |
284 | } | |
285 | ||
286 | g_free(prop->name); | |
287 | g_free(prop->type); | |
288 | g_free(prop); | |
289 | } | |
290 | } | |
291 | ||
292 | static void object_property_del_child(Object *obj, Object *child, Error **errp) | |
293 | { | |
294 | ObjectProperty *prop; | |
295 | ||
296 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
297 | if (!strstart(prop->type, "child<", NULL)) { | |
298 | continue; | |
299 | } | |
300 | ||
301 | if (prop->opaque == child) { | |
302 | object_property_del(obj, prop->name, errp); | |
303 | } | |
304 | } | |
305 | } | |
306 | ||
307 | void object_unparent(Object *obj) | |
308 | { | |
309 | if (obj->parent) { | |
310 | object_property_del_child(obj->parent, obj, NULL); | |
311 | } | |
312 | } | |
313 | ||
2f28d2ff AL |
314 | static void object_deinit(Object *obj, TypeImpl *type) |
315 | { | |
316 | if (type->instance_finalize) { | |
317 | type->instance_finalize(obj); | |
318 | } | |
319 | ||
320 | while (obj->interfaces) { | |
321 | Interface *iface_obj = obj->interfaces->data; | |
322 | obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces); | |
323 | object_delete(OBJECT(iface_obj)); | |
324 | } | |
325 | ||
326 | if (type_has_parent(type)) { | |
327 | object_deinit(obj, type_get_parent(type)); | |
328 | } | |
57c9fafe AL |
329 | |
330 | object_unparent(obj); | |
2f28d2ff AL |
331 | } |
332 | ||
333 | void object_finalize(void *data) | |
334 | { | |
335 | Object *obj = data; | |
336 | TypeImpl *ti = obj->class->type; | |
337 | ||
338 | object_deinit(obj, ti); | |
57c9fafe | 339 | object_property_del_all(obj); |
db85b575 AL |
340 | |
341 | g_assert(obj->ref == 0); | |
2f28d2ff AL |
342 | } |
343 | ||
344 | Object *object_new_with_type(Type type) | |
345 | { | |
346 | Object *obj; | |
347 | ||
348 | g_assert(type != NULL); | |
349 | ||
350 | obj = g_malloc(type->instance_size); | |
351 | object_initialize_with_type(obj, type); | |
db85b575 | 352 | object_ref(obj); |
2f28d2ff AL |
353 | |
354 | return obj; | |
355 | } | |
356 | ||
357 | Object *object_new(const char *typename) | |
358 | { | |
359 | TypeImpl *ti = type_get_by_name(typename); | |
360 | ||
361 | return object_new_with_type(ti); | |
362 | } | |
363 | ||
364 | void object_delete(Object *obj) | |
365 | { | |
db85b575 AL |
366 | object_unref(obj); |
367 | g_assert(obj->ref == 0); | |
2f28d2ff AL |
368 | g_free(obj); |
369 | } | |
370 | ||
371 | static bool object_is_type(Object *obj, const char *typename) | |
372 | { | |
373 | TypeImpl *target_type = type_get_by_name(typename); | |
374 | TypeImpl *type = obj->class->type; | |
375 | GSList *i; | |
376 | ||
377 | /* Check if typename is a direct ancestor of type */ | |
378 | while (type) { | |
379 | if (type == target_type) { | |
380 | return true; | |
381 | } | |
382 | ||
383 | type = type_get_parent(type); | |
384 | } | |
385 | ||
386 | /* Check if obj has an interface of typename */ | |
387 | for (i = obj->interfaces; i; i = i->next) { | |
388 | Interface *iface = i->data; | |
389 | ||
390 | if (object_is_type(OBJECT(iface), typename)) { | |
391 | return true; | |
392 | } | |
393 | } | |
394 | ||
395 | return false; | |
396 | } | |
397 | ||
398 | Object *object_dynamic_cast(Object *obj, const char *typename) | |
399 | { | |
400 | GSList *i; | |
401 | ||
402 | /* Check if typename is a direct ancestor */ | |
403 | if (object_is_type(obj, typename)) { | |
404 | return obj; | |
405 | } | |
406 | ||
407 | /* Check if obj has an interface of typename */ | |
408 | for (i = obj->interfaces; i; i = i->next) { | |
409 | Interface *iface = i->data; | |
410 | ||
411 | if (object_is_type(OBJECT(iface), typename)) { | |
412 | return OBJECT(iface); | |
413 | } | |
414 | } | |
415 | ||
416 | /* Check if obj is an interface and its containing object is a direct | |
417 | * ancestor of typename */ | |
418 | if (object_is_type(obj, TYPE_INTERFACE)) { | |
419 | Interface *iface = INTERFACE(obj); | |
420 | ||
421 | if (object_is_type(iface->obj, typename)) { | |
422 | return iface->obj; | |
423 | } | |
424 | } | |
425 | ||
426 | return NULL; | |
427 | } | |
428 | ||
429 | ||
430 | static void register_interface(void) | |
431 | { | |
432 | static TypeInfo interface_info = { | |
433 | .name = TYPE_INTERFACE, | |
434 | .instance_size = sizeof(Interface), | |
435 | .abstract = true, | |
436 | }; | |
437 | ||
438 | type_register_static(&interface_info); | |
439 | } | |
440 | ||
441 | device_init(register_interface); | |
442 | ||
443 | Object *object_dynamic_cast_assert(Object *obj, const char *typename) | |
444 | { | |
445 | Object *inst; | |
446 | ||
447 | inst = object_dynamic_cast(obj, typename); | |
448 | ||
449 | if (!inst) { | |
450 | fprintf(stderr, "Object %p is not an instance of type %s\n", | |
451 | obj, typename); | |
452 | abort(); | |
453 | } | |
454 | ||
455 | return inst; | |
456 | } | |
457 | ||
458 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
459 | const char *typename) | |
460 | { | |
461 | TypeImpl *target_type = type_get_by_name(typename); | |
462 | TypeImpl *type = class->type; | |
463 | ||
464 | while (type) { | |
465 | if (type == target_type) { | |
466 | return class; | |
467 | } | |
468 | ||
469 | type = type_get_parent(type); | |
470 | } | |
471 | ||
472 | return NULL; | |
473 | } | |
474 | ||
475 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
476 | const char *typename) | |
477 | { | |
478 | ObjectClass *ret = object_class_dynamic_cast(class, typename); | |
479 | ||
480 | if (!ret) { | |
481 | fprintf(stderr, "Object %p is not an instance of type %s\n", | |
482 | class, typename); | |
483 | abort(); | |
484 | } | |
485 | ||
486 | return ret; | |
487 | } | |
488 | ||
489 | const char *object_get_typename(Object *obj) | |
490 | { | |
491 | return obj->class->type->name; | |
492 | } | |
493 | ||
494 | ObjectClass *object_get_class(Object *obj) | |
495 | { | |
496 | return obj->class; | |
497 | } | |
498 | ||
499 | const char *object_class_get_name(ObjectClass *klass) | |
500 | { | |
501 | return klass->type->name; | |
502 | } | |
503 | ||
504 | ObjectClass *object_class_by_name(const char *typename) | |
505 | { | |
506 | TypeImpl *type = type_get_by_name(typename); | |
507 | ||
508 | if (!type) { | |
509 | return NULL; | |
510 | } | |
511 | ||
512 | type_class_init(type); | |
513 | ||
514 | return type->class; | |
515 | } | |
516 | ||
517 | typedef struct OCFData | |
518 | { | |
519 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
520 | const char *implements_type; |
521 | bool include_abstract; | |
2f28d2ff AL |
522 | void *opaque; |
523 | } OCFData; | |
524 | ||
525 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
526 | gpointer opaque) | |
527 | { | |
528 | OCFData *data = opaque; | |
529 | TypeImpl *type = value; | |
93c511a1 | 530 | ObjectClass *k; |
2f28d2ff AL |
531 | |
532 | type_class_init(type); | |
93c511a1 | 533 | k = type->class; |
2f28d2ff | 534 | |
93c511a1 AL |
535 | if (!data->include_abstract && type->abstract) { |
536 | return; | |
537 | } | |
538 | ||
539 | if (data->implements_type && | |
540 | !object_class_dynamic_cast(k, data->implements_type)) { | |
541 | return; | |
542 | } | |
543 | ||
544 | data->fn(k, data->opaque); | |
2f28d2ff AL |
545 | } |
546 | ||
547 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 548 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
549 | void *opaque) |
550 | { | |
93c511a1 | 551 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff AL |
552 | |
553 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); | |
554 | } | |
57c9fafe AL |
555 | |
556 | void object_ref(Object *obj) | |
557 | { | |
558 | obj->ref++; | |
559 | } | |
560 | ||
561 | void object_unref(Object *obj) | |
562 | { | |
563 | g_assert(obj->ref > 0); | |
564 | obj->ref--; | |
565 | ||
566 | /* parent always holds a reference to its children */ | |
567 | if (obj->ref == 0) { | |
568 | object_finalize(obj); | |
569 | } | |
570 | } | |
571 | ||
572 | void object_property_add(Object *obj, const char *name, const char *type, | |
573 | ObjectPropertyAccessor *get, | |
574 | ObjectPropertyAccessor *set, | |
575 | ObjectPropertyRelease *release, | |
576 | void *opaque, Error **errp) | |
577 | { | |
578 | ObjectProperty *prop = g_malloc0(sizeof(*prop)); | |
579 | ||
580 | prop->name = g_strdup(name); | |
581 | prop->type = g_strdup(type); | |
582 | ||
583 | prop->get = get; | |
584 | prop->set = set; | |
585 | prop->release = release; | |
586 | prop->opaque = opaque; | |
587 | ||
588 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); | |
589 | } | |
590 | ||
591 | static ObjectProperty *object_property_find(Object *obj, const char *name) | |
592 | { | |
593 | ObjectProperty *prop; | |
594 | ||
595 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
596 | if (strcmp(prop->name, name) == 0) { | |
597 | return prop; | |
598 | } | |
599 | } | |
600 | ||
601 | return NULL; | |
602 | } | |
603 | ||
604 | void object_property_del(Object *obj, const char *name, Error **errp) | |
605 | { | |
606 | ObjectProperty *prop = object_property_find(obj, name); | |
607 | ||
608 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
609 | ||
610 | prop->release(obj, prop->name, prop->opaque); | |
611 | ||
612 | g_free(prop->name); | |
613 | g_free(prop->type); | |
614 | g_free(prop); | |
615 | } | |
616 | ||
617 | void object_property_get(Object *obj, Visitor *v, const char *name, | |
618 | Error **errp) | |
619 | { | |
620 | ObjectProperty *prop = object_property_find(obj, name); | |
621 | ||
622 | if (prop == NULL) { | |
623 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); | |
624 | return; | |
625 | } | |
626 | ||
627 | if (!prop->get) { | |
628 | error_set(errp, QERR_PERMISSION_DENIED); | |
629 | } else { | |
630 | prop->get(obj, v, prop->opaque, name, errp); | |
631 | } | |
632 | } | |
633 | ||
634 | void object_property_set(Object *obj, Visitor *v, const char *name, | |
635 | Error **errp) | |
636 | { | |
637 | ObjectProperty *prop = object_property_find(obj, name); | |
638 | ||
639 | if (prop == NULL) { | |
640 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); | |
641 | return; | |
642 | } | |
643 | ||
644 | if (!prop->set) { | |
645 | error_set(errp, QERR_PERMISSION_DENIED); | |
646 | } else { | |
647 | prop->set(obj, v, prop->opaque, name, errp); | |
648 | } | |
649 | } | |
650 | ||
651 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) | |
652 | { | |
653 | ObjectProperty *prop = object_property_find(obj, name); | |
654 | ||
655 | if (prop == NULL) { | |
656 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); | |
657 | return NULL; | |
658 | } | |
659 | ||
660 | return prop->type; | |
661 | } | |
662 | ||
663 | Object *object_get_root(void) | |
664 | { | |
8b45d447 | 665 | static Object *root; |
57c9fafe | 666 | |
8b45d447 AL |
667 | if (!root) { |
668 | root = object_new("container"); | |
57c9fafe AL |
669 | } |
670 | ||
8b45d447 | 671 | return root; |
57c9fafe AL |
672 | } |
673 | ||
674 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, | |
675 | const char *name, Error **errp) | |
676 | { | |
677 | Object *child = opaque; | |
678 | gchar *path; | |
679 | ||
680 | path = object_get_canonical_path(child); | |
681 | visit_type_str(v, &path, name, errp); | |
682 | g_free(path); | |
683 | } | |
684 | ||
db85b575 AL |
685 | static void object_finalize_child_property(Object *obj, const char *name, |
686 | void *opaque) | |
687 | { | |
688 | Object *child = opaque; | |
689 | ||
690 | object_unref(child); | |
691 | } | |
692 | ||
57c9fafe AL |
693 | void object_property_add_child(Object *obj, const char *name, |
694 | Object *child, Error **errp) | |
695 | { | |
696 | gchar *type; | |
697 | ||
698 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); | |
699 | ||
700 | object_property_add(obj, name, type, object_get_child_property, | |
db85b575 | 701 | NULL, object_finalize_child_property, child, errp); |
57c9fafe AL |
702 | |
703 | object_ref(child); | |
704 | g_assert(child->parent == NULL); | |
705 | child->parent = obj; | |
706 | ||
707 | g_free(type); | |
708 | } | |
709 | ||
710 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, | |
711 | const char *name, Error **errp) | |
712 | { | |
713 | Object **child = opaque; | |
714 | gchar *path; | |
715 | ||
716 | if (*child) { | |
717 | path = object_get_canonical_path(*child); | |
718 | visit_type_str(v, &path, name, errp); | |
719 | g_free(path); | |
720 | } else { | |
721 | path = (gchar *)""; | |
722 | visit_type_str(v, &path, name, errp); | |
723 | } | |
724 | } | |
725 | ||
726 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, | |
727 | const char *name, Error **errp) | |
728 | { | |
729 | Object **child = opaque; | |
730 | bool ambiguous = false; | |
731 | const char *type; | |
732 | char *path; | |
733 | ||
734 | type = object_property_get_type(obj, name, NULL); | |
735 | ||
736 | visit_type_str(v, &path, name, errp); | |
737 | ||
738 | if (*child) { | |
739 | object_unref(*child); | |
740 | } | |
741 | ||
742 | if (strcmp(path, "") != 0) { | |
743 | Object *target; | |
744 | ||
745 | target = object_resolve_path(path, &ambiguous); | |
746 | if (target) { | |
747 | gchar *target_type; | |
748 | ||
fe40e627 AL |
749 | target_type = g_strdup(&type[5]); |
750 | target_type[strlen(target_type) - 2] = 0; | |
751 | ||
752 | if (object_dynamic_cast(target, target_type)) { | |
57c9fafe | 753 | object_ref(target); |
fe40e627 | 754 | *child = target; |
57c9fafe AL |
755 | } else { |
756 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, type); | |
757 | } | |
758 | ||
759 | g_free(target_type); | |
760 | } else { | |
761 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); | |
762 | } | |
763 | } else { | |
764 | *child = NULL; | |
765 | } | |
766 | ||
767 | g_free(path); | |
768 | } | |
769 | ||
770 | void object_property_add_link(Object *obj, const char *name, | |
771 | const char *type, Object **child, | |
772 | Error **errp) | |
773 | { | |
774 | gchar *full_type; | |
775 | ||
776 | full_type = g_strdup_printf("link<%s>", type); | |
777 | ||
778 | object_property_add(obj, name, full_type, | |
779 | object_get_link_property, | |
780 | object_set_link_property, | |
781 | NULL, child, errp); | |
782 | ||
783 | g_free(full_type); | |
784 | } | |
785 | ||
786 | gchar *object_get_canonical_path(Object *obj) | |
787 | { | |
788 | Object *root = object_get_root(); | |
789 | char *newpath = NULL, *path = NULL; | |
790 | ||
791 | while (obj != root) { | |
792 | ObjectProperty *prop = NULL; | |
793 | ||
794 | g_assert(obj->parent != NULL); | |
795 | ||
796 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { | |
797 | if (!strstart(prop->type, "child<", NULL)) { | |
798 | continue; | |
799 | } | |
800 | ||
801 | if (prop->opaque == obj) { | |
802 | if (path) { | |
803 | newpath = g_strdup_printf("%s/%s", prop->name, path); | |
804 | g_free(path); | |
805 | path = newpath; | |
806 | } else { | |
807 | path = g_strdup(prop->name); | |
808 | } | |
809 | break; | |
810 | } | |
811 | } | |
812 | ||
813 | g_assert(prop != NULL); | |
814 | ||
815 | obj = obj->parent; | |
816 | } | |
817 | ||
818 | newpath = g_strdup_printf("/%s", path); | |
819 | g_free(path); | |
820 | ||
821 | return newpath; | |
822 | } | |
823 | ||
824 | static Object *object_resolve_abs_path(Object *parent, | |
825 | gchar **parts, | |
826 | int index) | |
827 | { | |
828 | ObjectProperty *prop; | |
829 | Object *child; | |
830 | ||
831 | if (parts[index] == NULL) { | |
832 | return parent; | |
833 | } | |
834 | ||
835 | if (strcmp(parts[index], "") == 0) { | |
836 | return object_resolve_abs_path(parent, parts, index + 1); | |
837 | } | |
838 | ||
839 | prop = object_property_find(parent, parts[index]); | |
840 | if (prop == NULL) { | |
841 | return NULL; | |
842 | } | |
843 | ||
844 | child = NULL; | |
845 | if (strstart(prop->type, "link<", NULL)) { | |
846 | Object **pchild = prop->opaque; | |
847 | if (*pchild) { | |
848 | child = *pchild; | |
849 | } | |
850 | } else if (strstart(prop->type, "child<", NULL)) { | |
851 | child = prop->opaque; | |
852 | } | |
853 | ||
854 | if (!child) { | |
855 | return NULL; | |
856 | } | |
857 | ||
858 | return object_resolve_abs_path(child, parts, index + 1); | |
859 | } | |
860 | ||
861 | static Object *object_resolve_partial_path(Object *parent, | |
862 | gchar **parts, | |
863 | bool *ambiguous) | |
864 | { | |
865 | Object *obj; | |
866 | ObjectProperty *prop; | |
867 | ||
868 | obj = object_resolve_abs_path(parent, parts, 0); | |
869 | ||
870 | QTAILQ_FOREACH(prop, &parent->properties, node) { | |
871 | Object *found; | |
872 | ||
873 | if (!strstart(prop->type, "child<", NULL)) { | |
874 | continue; | |
875 | } | |
876 | ||
877 | found = object_resolve_partial_path(prop->opaque, parts, ambiguous); | |
878 | if (found) { | |
879 | if (obj) { | |
880 | if (ambiguous) { | |
881 | *ambiguous = true; | |
882 | } | |
883 | return NULL; | |
884 | } | |
885 | obj = found; | |
886 | } | |
887 | ||
888 | if (ambiguous && *ambiguous) { | |
889 | return NULL; | |
890 | } | |
891 | } | |
892 | ||
893 | return obj; | |
894 | } | |
895 | ||
896 | Object *object_resolve_path(const char *path, bool *ambiguous) | |
897 | { | |
898 | bool partial_path = true; | |
899 | Object *obj; | |
900 | gchar **parts; | |
901 | ||
902 | parts = g_strsplit(path, "/", 0); | |
903 | if (parts == NULL || parts[0] == NULL) { | |
904 | g_strfreev(parts); | |
905 | return object_get_root(); | |
906 | } | |
907 | ||
908 | if (strcmp(parts[0], "") == 0) { | |
909 | partial_path = false; | |
910 | } | |
911 | ||
912 | if (partial_path) { | |
913 | if (ambiguous) { | |
914 | *ambiguous = false; | |
915 | } | |
916 | obj = object_resolve_partial_path(object_get_root(), parts, ambiguous); | |
917 | } else { | |
918 | obj = object_resolve_abs_path(object_get_root(), parts, 1); | |
919 | } | |
920 | ||
921 | g_strfreev(parts); | |
922 | ||
923 | return obj; | |
924 | } | |
925 | ||
926 | typedef struct StringProperty | |
927 | { | |
928 | char *(*get)(Object *, Error **); | |
929 | void (*set)(Object *, const char *, Error **); | |
930 | } StringProperty; | |
931 | ||
932 | static void object_property_get_str(Object *obj, Visitor *v, void *opaque, | |
933 | const char *name, Error **errp) | |
934 | { | |
935 | StringProperty *prop = opaque; | |
936 | char *value; | |
937 | ||
938 | value = prop->get(obj, errp); | |
939 | if (value) { | |
940 | visit_type_str(v, &value, name, errp); | |
941 | g_free(value); | |
942 | } | |
943 | } | |
944 | ||
945 | static void object_property_set_str(Object *obj, Visitor *v, void *opaque, | |
946 | const char *name, Error **errp) | |
947 | { | |
948 | StringProperty *prop = opaque; | |
949 | char *value; | |
950 | Error *local_err = NULL; | |
951 | ||
952 | visit_type_str(v, &value, name, &local_err); | |
953 | if (local_err) { | |
954 | error_propagate(errp, local_err); | |
955 | return; | |
956 | } | |
957 | ||
958 | prop->set(obj, value, errp); | |
959 | g_free(value); | |
960 | } | |
961 | ||
962 | static void object_property_release_str(Object *obj, const char *name, | |
963 | void *opaque) | |
964 | { | |
965 | StringProperty *prop = opaque; | |
966 | g_free(prop); | |
967 | } | |
968 | ||
969 | void object_property_add_str(Object *obj, const char *name, | |
970 | char *(*get)(Object *, Error **), | |
971 | void (*set)(Object *, const char *, Error **), | |
972 | Error **errp) | |
973 | { | |
974 | StringProperty *prop = g_malloc0(sizeof(*prop)); | |
975 | ||
976 | prop->get = get; | |
977 | prop->set = set; | |
978 | ||
979 | object_property_add(obj, name, "string", | |
980 | get ? object_property_get_str : NULL, | |
981 | set ? object_property_set_str : NULL, | |
982 | object_property_release_str, | |
983 | prop, errp); | |
984 | } |