]> git.proxmox.com Git - mirror_qemu.git/blob - qom/object.c
Merge remote-tracking branch 'remotes/aperard/tags/pull-xen-20200710' into staging
[mirror_qemu.git] / qom / object.c
1 /*
2 * QEMU Object Model
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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/osdep.h"
14 #include "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/qapi-builtin-visit.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qmp/qjson.h"
26 #include "trace.h"
27
28 /* TODO: replace QObject with a simpler visitor to avoid a dependency
29 * of the QOM core on QObject? */
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qemu/error-report.h"
35
36 #define MAX_INTERFACES 32
37
38 typedef struct InterfaceImpl InterfaceImpl;
39 typedef struct TypeImpl TypeImpl;
40
41 struct InterfaceImpl
42 {
43 const char *typename;
44 };
45
46 struct TypeImpl
47 {
48 const char *name;
49
50 size_t class_size;
51
52 size_t instance_size;
53
54 void (*class_init)(ObjectClass *klass, void *data);
55 void (*class_base_init)(ObjectClass *klass, void *data);
56
57 void *class_data;
58
59 void (*instance_init)(Object *obj);
60 void (*instance_post_init)(Object *obj);
61 void (*instance_finalize)(Object *obj);
62
63 bool abstract;
64
65 const char *parent;
66 TypeImpl *parent_type;
67
68 ObjectClass *class;
69
70 int num_interfaces;
71 InterfaceImpl interfaces[MAX_INTERFACES];
72 };
73
74 static Type type_interface;
75
76 static GHashTable *type_table_get(void)
77 {
78 static GHashTable *type_table;
79
80 if (type_table == NULL) {
81 type_table = g_hash_table_new(g_str_hash, g_str_equal);
82 }
83
84 return type_table;
85 }
86
87 static bool enumerating_types;
88
89 static void type_table_add(TypeImpl *ti)
90 {
91 assert(!enumerating_types);
92 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
93 }
94
95 static TypeImpl *type_table_lookup(const char *name)
96 {
97 return g_hash_table_lookup(type_table_get(), name);
98 }
99
100 static TypeImpl *type_new(const TypeInfo *info)
101 {
102 TypeImpl *ti = g_malloc0(sizeof(*ti));
103 int i;
104
105 g_assert(info->name != NULL);
106
107 if (type_table_lookup(info->name) != NULL) {
108 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
109 abort();
110 }
111
112 ti->name = g_strdup(info->name);
113 ti->parent = g_strdup(info->parent);
114
115 ti->class_size = info->class_size;
116 ti->instance_size = info->instance_size;
117
118 ti->class_init = info->class_init;
119 ti->class_base_init = info->class_base_init;
120 ti->class_data = info->class_data;
121
122 ti->instance_init = info->instance_init;
123 ti->instance_post_init = info->instance_post_init;
124 ti->instance_finalize = info->instance_finalize;
125
126 ti->abstract = info->abstract;
127
128 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
129 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
130 }
131 ti->num_interfaces = i;
132
133 return ti;
134 }
135
136 static TypeImpl *type_register_internal(const TypeInfo *info)
137 {
138 TypeImpl *ti;
139 ti = type_new(info);
140
141 type_table_add(ti);
142 return ti;
143 }
144
145 TypeImpl *type_register(const TypeInfo *info)
146 {
147 assert(info->parent);
148 return type_register_internal(info);
149 }
150
151 TypeImpl *type_register_static(const TypeInfo *info)
152 {
153 return type_register(info);
154 }
155
156 void type_register_static_array(const TypeInfo *infos, int nr_infos)
157 {
158 int i;
159
160 for (i = 0; i < nr_infos; i++) {
161 type_register_static(&infos[i]);
162 }
163 }
164
165 static TypeImpl *type_get_by_name(const char *name)
166 {
167 if (name == NULL) {
168 return NULL;
169 }
170
171 return type_table_lookup(name);
172 }
173
174 static TypeImpl *type_get_parent(TypeImpl *type)
175 {
176 if (!type->parent_type && type->parent) {
177 type->parent_type = type_get_by_name(type->parent);
178 if (!type->parent_type) {
179 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
180 type->name, type->parent);
181 abort();
182 }
183 }
184
185 return type->parent_type;
186 }
187
188 static bool type_has_parent(TypeImpl *type)
189 {
190 return (type->parent != NULL);
191 }
192
193 static size_t type_class_get_size(TypeImpl *ti)
194 {
195 if (ti->class_size) {
196 return ti->class_size;
197 }
198
199 if (type_has_parent(ti)) {
200 return type_class_get_size(type_get_parent(ti));
201 }
202
203 return sizeof(ObjectClass);
204 }
205
206 static size_t type_object_get_size(TypeImpl *ti)
207 {
208 if (ti->instance_size) {
209 return ti->instance_size;
210 }
211
212 if (type_has_parent(ti)) {
213 return type_object_get_size(type_get_parent(ti));
214 }
215
216 return 0;
217 }
218
219 size_t object_type_get_instance_size(const char *typename)
220 {
221 TypeImpl *type = type_get_by_name(typename);
222
223 g_assert(type != NULL);
224 return type_object_get_size(type);
225 }
226
227 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
228 {
229 assert(target_type);
230
231 /* Check if target_type is a direct ancestor of type */
232 while (type) {
233 if (type == target_type) {
234 return true;
235 }
236
237 type = type_get_parent(type);
238 }
239
240 return false;
241 }
242
243 static void type_initialize(TypeImpl *ti);
244
245 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
246 TypeImpl *parent_type)
247 {
248 InterfaceClass *new_iface;
249 TypeInfo info = { };
250 TypeImpl *iface_impl;
251
252 info.parent = parent_type->name;
253 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
254 info.abstract = true;
255
256 iface_impl = type_new(&info);
257 iface_impl->parent_type = parent_type;
258 type_initialize(iface_impl);
259 g_free((char *)info.name);
260
261 new_iface = (InterfaceClass *)iface_impl->class;
262 new_iface->concrete_class = ti->class;
263 new_iface->interface_type = interface_type;
264
265 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
266 }
267
268 static void object_property_free(gpointer data)
269 {
270 ObjectProperty *prop = data;
271
272 if (prop->defval) {
273 qobject_unref(prop->defval);
274 prop->defval = NULL;
275 }
276 g_free(prop->name);
277 g_free(prop->type);
278 g_free(prop->description);
279 g_free(prop);
280 }
281
282 static void type_initialize(TypeImpl *ti)
283 {
284 TypeImpl *parent;
285
286 if (ti->class) {
287 return;
288 }
289
290 ti->class_size = type_class_get_size(ti);
291 ti->instance_size = type_object_get_size(ti);
292 /* Any type with zero instance_size is implicitly abstract.
293 * This means interface types are all abstract.
294 */
295 if (ti->instance_size == 0) {
296 ti->abstract = true;
297 }
298 if (type_is_ancestor(ti, type_interface)) {
299 assert(ti->instance_size == 0);
300 assert(ti->abstract);
301 assert(!ti->instance_init);
302 assert(!ti->instance_post_init);
303 assert(!ti->instance_finalize);
304 assert(!ti->num_interfaces);
305 }
306 ti->class = g_malloc0(ti->class_size);
307
308 parent = type_get_parent(ti);
309 if (parent) {
310 type_initialize(parent);
311 GSList *e;
312 int i;
313
314 g_assert(parent->class_size <= ti->class_size);
315 g_assert(parent->instance_size <= ti->instance_size);
316 memcpy(ti->class, parent->class, parent->class_size);
317 ti->class->interfaces = NULL;
318
319 for (e = parent->class->interfaces; e; e = e->next) {
320 InterfaceClass *iface = e->data;
321 ObjectClass *klass = OBJECT_CLASS(iface);
322
323 type_initialize_interface(ti, iface->interface_type, klass->type);
324 }
325
326 for (i = 0; i < ti->num_interfaces; i++) {
327 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
328 if (!t) {
329 error_report("missing interface '%s' for object '%s'",
330 ti->interfaces[i].typename, parent->name);
331 abort();
332 }
333 for (e = ti->class->interfaces; e; e = e->next) {
334 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
335
336 if (type_is_ancestor(target_type, t)) {
337 break;
338 }
339 }
340
341 if (e) {
342 continue;
343 }
344
345 type_initialize_interface(ti, t, t);
346 }
347 }
348
349 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
350 object_property_free);
351
352 ti->class->type = ti;
353
354 while (parent) {
355 if (parent->class_base_init) {
356 parent->class_base_init(ti->class, ti->class_data);
357 }
358 parent = type_get_parent(parent);
359 }
360
361 if (ti->class_init) {
362 ti->class_init(ti->class, ti->class_data);
363 }
364 }
365
366 static void object_init_with_type(Object *obj, TypeImpl *ti)
367 {
368 if (type_has_parent(ti)) {
369 object_init_with_type(obj, type_get_parent(ti));
370 }
371
372 if (ti->instance_init) {
373 ti->instance_init(obj);
374 }
375 }
376
377 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
378 {
379 if (ti->instance_post_init) {
380 ti->instance_post_init(obj);
381 }
382
383 if (type_has_parent(ti)) {
384 object_post_init_with_type(obj, type_get_parent(ti));
385 }
386 }
387
388 bool object_apply_global_props(Object *obj, const GPtrArray *props,
389 Error **errp)
390 {
391 int i;
392
393 if (!props) {
394 return true;
395 }
396
397 for (i = 0; i < props->len; i++) {
398 GlobalProperty *p = g_ptr_array_index(props, i);
399 Error *err = NULL;
400
401 if (object_dynamic_cast(obj, p->driver) == NULL) {
402 continue;
403 }
404 if (p->optional && !object_property_find(obj, p->property, NULL)) {
405 continue;
406 }
407 p->used = true;
408 if (!object_property_parse(obj, p->property, p->value, &err)) {
409 error_prepend(&err, "can't apply global %s.%s=%s: ",
410 p->driver, p->property, p->value);
411 /*
412 * If errp != NULL, propagate error and return.
413 * If errp == NULL, report a warning, but keep going
414 * with the remaining globals.
415 */
416 if (errp) {
417 error_propagate(errp, err);
418 return false;
419 } else {
420 warn_report_err(err);
421 }
422 }
423 }
424
425 return true;
426 }
427
428 /*
429 * Global property defaults
430 * Slot 0: accelerator's global property defaults
431 * Slot 1: machine's global property defaults
432 * Slot 2: global properties from legacy command line option
433 * Each is a GPtrArray of of GlobalProperty.
434 * Applied in order, later entries override earlier ones.
435 */
436 static GPtrArray *object_compat_props[3];
437
438 /*
439 * Retrieve @GPtrArray for global property defined with options
440 * other than "-global". These are generally used for syntactic
441 * sugar and legacy command line options.
442 */
443 void object_register_sugar_prop(const char *driver, const char *prop, const char *value)
444 {
445 GlobalProperty *g;
446 if (!object_compat_props[2]) {
447 object_compat_props[2] = g_ptr_array_new();
448 }
449 g = g_new0(GlobalProperty, 1);
450 g->driver = g_strdup(driver);
451 g->property = g_strdup(prop);
452 g->value = g_strdup(value);
453 g_ptr_array_add(object_compat_props[2], g);
454 }
455
456 /*
457 * Set machine's global property defaults to @compat_props.
458 * May be called at most once.
459 */
460 void object_set_machine_compat_props(GPtrArray *compat_props)
461 {
462 assert(!object_compat_props[1]);
463 object_compat_props[1] = compat_props;
464 }
465
466 /*
467 * Set accelerator's global property defaults to @compat_props.
468 * May be called at most once.
469 */
470 void object_set_accelerator_compat_props(GPtrArray *compat_props)
471 {
472 assert(!object_compat_props[0]);
473 object_compat_props[0] = compat_props;
474 }
475
476 void object_apply_compat_props(Object *obj)
477 {
478 int i;
479
480 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
481 object_apply_global_props(obj, object_compat_props[i],
482 i == 2 ? &error_fatal : &error_abort);
483 }
484 }
485
486 static void object_class_property_init_all(Object *obj)
487 {
488 ObjectPropertyIterator iter;
489 ObjectProperty *prop;
490
491 object_class_property_iter_init(&iter, object_get_class(obj));
492 while ((prop = object_property_iter_next(&iter))) {
493 if (prop->init) {
494 prop->init(obj, prop);
495 }
496 }
497 }
498
499 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
500 {
501 type_initialize(type);
502
503 g_assert(type->instance_size >= sizeof(Object));
504 g_assert(type->abstract == false);
505 g_assert(size >= type->instance_size);
506
507 memset(obj, 0, type->instance_size);
508 obj->class = type->class;
509 object_ref(obj);
510 object_class_property_init_all(obj);
511 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
512 NULL, object_property_free);
513 object_init_with_type(obj, type);
514 object_post_init_with_type(obj, type);
515 }
516
517 void object_initialize(void *data, size_t size, const char *typename)
518 {
519 TypeImpl *type = type_get_by_name(typename);
520
521 if (!type) {
522 error_report("missing object type '%s'", typename);
523 abort();
524 }
525
526 object_initialize_with_type(data, size, type);
527 }
528
529 bool object_initialize_child_with_props(Object *parentobj,
530 const char *propname,
531 void *childobj, size_t size,
532 const char *type,
533 Error **errp, ...)
534 {
535 va_list vargs;
536 bool ok;
537
538 va_start(vargs, errp);
539 ok = object_initialize_child_with_propsv(parentobj, propname,
540 childobj, size, type, errp,
541 vargs);
542 va_end(vargs);
543 return ok;
544 }
545
546 bool object_initialize_child_with_propsv(Object *parentobj,
547 const char *propname,
548 void *childobj, size_t size,
549 const char *type,
550 Error **errp, va_list vargs)
551 {
552 bool ok = false;
553 Object *obj;
554 UserCreatable *uc;
555
556 object_initialize(childobj, size, type);
557 obj = OBJECT(childobj);
558
559 if (!object_set_propv(obj, errp, vargs)) {
560 goto out;
561 }
562
563 object_property_add_child(parentobj, propname, obj);
564
565 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
566 if (uc) {
567 if (!user_creatable_complete(uc, errp)) {
568 object_unparent(obj);
569 goto out;
570 }
571 }
572
573 ok = true;
574
575 out:
576 /*
577 * We want @obj's reference to be 1 on success, 0 on failure.
578 * On success, it's 2: one taken by object_initialize(), and one
579 * by object_property_add_child().
580 * On failure in object_initialize() or earlier, it's 1.
581 * On failure afterwards, it's also 1: object_unparent() releases
582 * the reference taken by object_property_add_child().
583 */
584 object_unref(obj);
585 return ok;
586 }
587
588 void object_initialize_child_internal(Object *parent,
589 const char *propname,
590 void *child, size_t size,
591 const char *type)
592 {
593 object_initialize_child_with_props(parent, propname, child, size, type,
594 &error_abort, NULL);
595 }
596
597 static inline bool object_property_is_child(ObjectProperty *prop)
598 {
599 return strstart(prop->type, "child<", NULL);
600 }
601
602 static void object_property_del_all(Object *obj)
603 {
604 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
605 ObjectProperty *prop;
606 ObjectPropertyIterator iter;
607 bool released;
608
609 do {
610 released = false;
611 object_property_iter_init(&iter, obj);
612 while ((prop = object_property_iter_next(&iter)) != NULL) {
613 if (g_hash_table_add(done, prop)) {
614 if (prop->release) {
615 prop->release(obj, prop->name, prop->opaque);
616 released = true;
617 break;
618 }
619 }
620 }
621 } while (released);
622
623 g_hash_table_unref(obj->properties);
624 }
625
626 static void object_property_del_child(Object *obj, Object *child)
627 {
628 ObjectProperty *prop;
629 GHashTableIter iter;
630 gpointer key, value;
631
632 g_hash_table_iter_init(&iter, obj->properties);
633 while (g_hash_table_iter_next(&iter, &key, &value)) {
634 prop = value;
635 if (object_property_is_child(prop) && prop->opaque == child) {
636 if (prop->release) {
637 prop->release(obj, prop->name, prop->opaque);
638 prop->release = NULL;
639 }
640 break;
641 }
642 }
643 g_hash_table_iter_init(&iter, obj->properties);
644 while (g_hash_table_iter_next(&iter, &key, &value)) {
645 prop = value;
646 if (object_property_is_child(prop) && prop->opaque == child) {
647 g_hash_table_iter_remove(&iter);
648 break;
649 }
650 }
651 }
652
653 void object_unparent(Object *obj)
654 {
655 if (obj->parent) {
656 object_property_del_child(obj->parent, obj);
657 }
658 }
659
660 static void object_deinit(Object *obj, TypeImpl *type)
661 {
662 if (type->instance_finalize) {
663 type->instance_finalize(obj);
664 }
665
666 if (type_has_parent(type)) {
667 object_deinit(obj, type_get_parent(type));
668 }
669 }
670
671 static void object_finalize(void *data)
672 {
673 Object *obj = data;
674 TypeImpl *ti = obj->class->type;
675
676 object_property_del_all(obj);
677 object_deinit(obj, ti);
678
679 g_assert(obj->ref == 0);
680 if (obj->free) {
681 obj->free(obj);
682 }
683 }
684
685 static Object *object_new_with_type(Type type)
686 {
687 Object *obj;
688
689 g_assert(type != NULL);
690 type_initialize(type);
691
692 obj = g_malloc(type->instance_size);
693 object_initialize_with_type(obj, type->instance_size, type);
694 obj->free = g_free;
695
696 return obj;
697 }
698
699 Object *object_new_with_class(ObjectClass *klass)
700 {
701 return object_new_with_type(klass->type);
702 }
703
704 Object *object_new(const char *typename)
705 {
706 TypeImpl *ti = type_get_by_name(typename);
707
708 return object_new_with_type(ti);
709 }
710
711
712 Object *object_new_with_props(const char *typename,
713 Object *parent,
714 const char *id,
715 Error **errp,
716 ...)
717 {
718 va_list vargs;
719 Object *obj;
720
721 va_start(vargs, errp);
722 obj = object_new_with_propv(typename, parent, id, errp, vargs);
723 va_end(vargs);
724
725 return obj;
726 }
727
728
729 Object *object_new_with_propv(const char *typename,
730 Object *parent,
731 const char *id,
732 Error **errp,
733 va_list vargs)
734 {
735 Object *obj;
736 ObjectClass *klass;
737 UserCreatable *uc;
738
739 klass = object_class_by_name(typename);
740 if (!klass) {
741 error_setg(errp, "invalid object type: %s", typename);
742 return NULL;
743 }
744
745 if (object_class_is_abstract(klass)) {
746 error_setg(errp, "object type '%s' is abstract", typename);
747 return NULL;
748 }
749 obj = object_new_with_type(klass->type);
750
751 if (!object_set_propv(obj, errp, vargs)) {
752 goto error;
753 }
754
755 if (id != NULL) {
756 object_property_add_child(parent, id, obj);
757 }
758
759 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
760 if (uc) {
761 if (!user_creatable_complete(uc, errp)) {
762 if (id != NULL) {
763 object_unparent(obj);
764 }
765 goto error;
766 }
767 }
768
769 object_unref(obj);
770 return obj;
771
772 error:
773 object_unref(obj);
774 return NULL;
775 }
776
777
778 bool object_set_props(Object *obj,
779 Error **errp,
780 ...)
781 {
782 va_list vargs;
783 bool ret;
784
785 va_start(vargs, errp);
786 ret = object_set_propv(obj, errp, vargs);
787 va_end(vargs);
788
789 return ret;
790 }
791
792
793 bool object_set_propv(Object *obj,
794 Error **errp,
795 va_list vargs)
796 {
797 const char *propname;
798
799 propname = va_arg(vargs, char *);
800 while (propname != NULL) {
801 const char *value = va_arg(vargs, char *);
802
803 g_assert(value != NULL);
804 if (!object_property_parse(obj, propname, value, errp)) {
805 return false;
806 }
807 propname = va_arg(vargs, char *);
808 }
809
810 return true;
811 }
812
813
814 Object *object_dynamic_cast(Object *obj, const char *typename)
815 {
816 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
817 return obj;
818 }
819
820 return NULL;
821 }
822
823 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
824 const char *file, int line, const char *func)
825 {
826 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
827 typename, file, line, func);
828
829 #ifdef CONFIG_QOM_CAST_DEBUG
830 int i;
831 Object *inst;
832
833 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
834 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
835 goto out;
836 }
837 }
838
839 inst = object_dynamic_cast(obj, typename);
840
841 if (!inst && obj) {
842 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
843 file, line, func, obj, typename);
844 abort();
845 }
846
847 assert(obj == inst);
848
849 if (obj && obj == inst) {
850 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
851 atomic_set(&obj->class->object_cast_cache[i - 1],
852 atomic_read(&obj->class->object_cast_cache[i]));
853 }
854 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
855 }
856
857 out:
858 #endif
859 return obj;
860 }
861
862 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
863 const char *typename)
864 {
865 ObjectClass *ret = NULL;
866 TypeImpl *target_type;
867 TypeImpl *type;
868
869 if (!class) {
870 return NULL;
871 }
872
873 /* A simple fast path that can trigger a lot for leaf classes. */
874 type = class->type;
875 if (type->name == typename) {
876 return class;
877 }
878
879 target_type = type_get_by_name(typename);
880 if (!target_type) {
881 /* target class type unknown, so fail the cast */
882 return NULL;
883 }
884
885 if (type->class->interfaces &&
886 type_is_ancestor(target_type, type_interface)) {
887 int found = 0;
888 GSList *i;
889
890 for (i = class->interfaces; i; i = i->next) {
891 ObjectClass *target_class = i->data;
892
893 if (type_is_ancestor(target_class->type, target_type)) {
894 ret = target_class;
895 found++;
896 }
897 }
898
899 /* The match was ambiguous, don't allow a cast */
900 if (found > 1) {
901 ret = NULL;
902 }
903 } else if (type_is_ancestor(type, target_type)) {
904 ret = class;
905 }
906
907 return ret;
908 }
909
910 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
911 const char *typename,
912 const char *file, int line,
913 const char *func)
914 {
915 ObjectClass *ret;
916
917 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
918 typename, file, line, func);
919
920 #ifdef CONFIG_QOM_CAST_DEBUG
921 int i;
922
923 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
924 if (atomic_read(&class->class_cast_cache[i]) == typename) {
925 ret = class;
926 goto out;
927 }
928 }
929 #else
930 if (!class || !class->interfaces) {
931 return class;
932 }
933 #endif
934
935 ret = object_class_dynamic_cast(class, typename);
936 if (!ret && class) {
937 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
938 file, line, func, class, typename);
939 abort();
940 }
941
942 #ifdef CONFIG_QOM_CAST_DEBUG
943 if (class && ret == class) {
944 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
945 atomic_set(&class->class_cast_cache[i - 1],
946 atomic_read(&class->class_cast_cache[i]));
947 }
948 atomic_set(&class->class_cast_cache[i - 1], typename);
949 }
950 out:
951 #endif
952 return ret;
953 }
954
955 const char *object_get_typename(const Object *obj)
956 {
957 return obj->class->type->name;
958 }
959
960 ObjectClass *object_get_class(Object *obj)
961 {
962 return obj->class;
963 }
964
965 bool object_class_is_abstract(ObjectClass *klass)
966 {
967 return klass->type->abstract;
968 }
969
970 const char *object_class_get_name(ObjectClass *klass)
971 {
972 return klass->type->name;
973 }
974
975 ObjectClass *object_class_by_name(const char *typename)
976 {
977 TypeImpl *type = type_get_by_name(typename);
978
979 if (!type) {
980 return NULL;
981 }
982
983 type_initialize(type);
984
985 return type->class;
986 }
987
988 ObjectClass *module_object_class_by_name(const char *typename)
989 {
990 ObjectClass *oc;
991
992 oc = object_class_by_name(typename);
993 #ifdef CONFIG_MODULES
994 if (!oc) {
995 module_load_qom_one(typename);
996 oc = object_class_by_name(typename);
997 }
998 #endif
999 return oc;
1000 }
1001
1002 ObjectClass *object_class_get_parent(ObjectClass *class)
1003 {
1004 TypeImpl *type = type_get_parent(class->type);
1005
1006 if (!type) {
1007 return NULL;
1008 }
1009
1010 type_initialize(type);
1011
1012 return type->class;
1013 }
1014
1015 typedef struct OCFData
1016 {
1017 void (*fn)(ObjectClass *klass, void *opaque);
1018 const char *implements_type;
1019 bool include_abstract;
1020 void *opaque;
1021 } OCFData;
1022
1023 static void object_class_foreach_tramp(gpointer key, gpointer value,
1024 gpointer opaque)
1025 {
1026 OCFData *data = opaque;
1027 TypeImpl *type = value;
1028 ObjectClass *k;
1029
1030 type_initialize(type);
1031 k = type->class;
1032
1033 if (!data->include_abstract && type->abstract) {
1034 return;
1035 }
1036
1037 if (data->implements_type &&
1038 !object_class_dynamic_cast(k, data->implements_type)) {
1039 return;
1040 }
1041
1042 data->fn(k, data->opaque);
1043 }
1044
1045 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1046 const char *implements_type, bool include_abstract,
1047 void *opaque)
1048 {
1049 OCFData data = { fn, implements_type, include_abstract, opaque };
1050
1051 enumerating_types = true;
1052 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1053 enumerating_types = false;
1054 }
1055
1056 static int do_object_child_foreach(Object *obj,
1057 int (*fn)(Object *child, void *opaque),
1058 void *opaque, bool recurse)
1059 {
1060 GHashTableIter iter;
1061 ObjectProperty *prop;
1062 int ret = 0;
1063
1064 g_hash_table_iter_init(&iter, obj->properties);
1065 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1066 if (object_property_is_child(prop)) {
1067 Object *child = prop->opaque;
1068
1069 ret = fn(child, opaque);
1070 if (ret != 0) {
1071 break;
1072 }
1073 if (recurse) {
1074 ret = do_object_child_foreach(child, fn, opaque, true);
1075 if (ret != 0) {
1076 break;
1077 }
1078 }
1079 }
1080 }
1081 return ret;
1082 }
1083
1084 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1085 void *opaque)
1086 {
1087 return do_object_child_foreach(obj, fn, opaque, false);
1088 }
1089
1090 int object_child_foreach_recursive(Object *obj,
1091 int (*fn)(Object *child, void *opaque),
1092 void *opaque)
1093 {
1094 return do_object_child_foreach(obj, fn, opaque, true);
1095 }
1096
1097 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1098 {
1099 GSList **list = opaque;
1100
1101 *list = g_slist_prepend(*list, klass);
1102 }
1103
1104 GSList *object_class_get_list(const char *implements_type,
1105 bool include_abstract)
1106 {
1107 GSList *list = NULL;
1108
1109 object_class_foreach(object_class_get_list_tramp,
1110 implements_type, include_abstract, &list);
1111 return list;
1112 }
1113
1114 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1115 {
1116 return strcasecmp(object_class_get_name((ObjectClass *)a),
1117 object_class_get_name((ObjectClass *)b));
1118 }
1119
1120 GSList *object_class_get_list_sorted(const char *implements_type,
1121 bool include_abstract)
1122 {
1123 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1124 object_class_cmp);
1125 }
1126
1127 Object *object_ref(Object *obj)
1128 {
1129 if (!obj) {
1130 return NULL;
1131 }
1132 atomic_inc(&obj->ref);
1133 return obj;
1134 }
1135
1136 void object_unref(Object *obj)
1137 {
1138 if (!obj) {
1139 return;
1140 }
1141 g_assert(obj->ref > 0);
1142
1143 /* parent always holds a reference to its children */
1144 if (atomic_fetch_dec(&obj->ref) == 1) {
1145 object_finalize(obj);
1146 }
1147 }
1148
1149 static ObjectProperty *
1150 object_property_try_add(Object *obj, const char *name, const char *type,
1151 ObjectPropertyAccessor *get,
1152 ObjectPropertyAccessor *set,
1153 ObjectPropertyRelease *release,
1154 void *opaque, Error **errp)
1155 {
1156 ObjectProperty *prop;
1157 size_t name_len = strlen(name);
1158
1159 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1160 int i;
1161 ObjectProperty *ret;
1162 char *name_no_array = g_strdup(name);
1163
1164 name_no_array[name_len - 3] = '\0';
1165 for (i = 0; ; ++i) {
1166 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1167
1168 ret = object_property_try_add(obj, full_name, type, get, set,
1169 release, opaque, NULL);
1170 g_free(full_name);
1171 if (ret) {
1172 break;
1173 }
1174 }
1175 g_free(name_no_array);
1176 return ret;
1177 }
1178
1179 if (object_property_find(obj, name, NULL) != NULL) {
1180 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1181 name, object_get_typename(obj));
1182 return NULL;
1183 }
1184
1185 prop = g_malloc0(sizeof(*prop));
1186
1187 prop->name = g_strdup(name);
1188 prop->type = g_strdup(type);
1189
1190 prop->get = get;
1191 prop->set = set;
1192 prop->release = release;
1193 prop->opaque = opaque;
1194
1195 g_hash_table_insert(obj->properties, prop->name, prop);
1196 return prop;
1197 }
1198
1199 ObjectProperty *
1200 object_property_add(Object *obj, const char *name, const char *type,
1201 ObjectPropertyAccessor *get,
1202 ObjectPropertyAccessor *set,
1203 ObjectPropertyRelease *release,
1204 void *opaque)
1205 {
1206 return object_property_try_add(obj, name, type, get, set, release,
1207 opaque, &error_abort);
1208 }
1209
1210 ObjectProperty *
1211 object_class_property_add(ObjectClass *klass,
1212 const char *name,
1213 const char *type,
1214 ObjectPropertyAccessor *get,
1215 ObjectPropertyAccessor *set,
1216 ObjectPropertyRelease *release,
1217 void *opaque)
1218 {
1219 ObjectProperty *prop;
1220
1221 assert(!object_class_property_find(klass, name, NULL));
1222
1223 prop = g_malloc0(sizeof(*prop));
1224
1225 prop->name = g_strdup(name);
1226 prop->type = g_strdup(type);
1227
1228 prop->get = get;
1229 prop->set = set;
1230 prop->release = release;
1231 prop->opaque = opaque;
1232
1233 g_hash_table_insert(klass->properties, prop->name, prop);
1234
1235 return prop;
1236 }
1237
1238 ObjectProperty *object_property_find(Object *obj, const char *name,
1239 Error **errp)
1240 {
1241 ObjectProperty *prop;
1242 ObjectClass *klass = object_get_class(obj);
1243
1244 prop = object_class_property_find(klass, name, NULL);
1245 if (prop) {
1246 return prop;
1247 }
1248
1249 prop = g_hash_table_lookup(obj->properties, name);
1250 if (prop) {
1251 return prop;
1252 }
1253
1254 error_setg(errp, "Property '.%s' not found", name);
1255 return NULL;
1256 }
1257
1258 void object_property_iter_init(ObjectPropertyIterator *iter,
1259 Object *obj)
1260 {
1261 g_hash_table_iter_init(&iter->iter, obj->properties);
1262 iter->nextclass = object_get_class(obj);
1263 }
1264
1265 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1266 {
1267 gpointer key, val;
1268 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1269 if (!iter->nextclass) {
1270 return NULL;
1271 }
1272 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1273 iter->nextclass = object_class_get_parent(iter->nextclass);
1274 }
1275 return val;
1276 }
1277
1278 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1279 ObjectClass *klass)
1280 {
1281 g_hash_table_iter_init(&iter->iter, klass->properties);
1282 iter->nextclass = object_class_get_parent(klass);
1283 }
1284
1285 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1286 Error **errp)
1287 {
1288 ObjectProperty *prop;
1289 ObjectClass *parent_klass;
1290
1291 parent_klass = object_class_get_parent(klass);
1292 if (parent_klass) {
1293 prop = object_class_property_find(parent_klass, name, NULL);
1294 if (prop) {
1295 return prop;
1296 }
1297 }
1298
1299 prop = g_hash_table_lookup(klass->properties, name);
1300 if (!prop) {
1301 error_setg(errp, "Property '.%s' not found", name);
1302 }
1303 return prop;
1304 }
1305
1306 void object_property_del(Object *obj, const char *name)
1307 {
1308 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1309
1310 if (prop->release) {
1311 prop->release(obj, name, prop->opaque);
1312 }
1313 g_hash_table_remove(obj->properties, name);
1314 }
1315
1316 bool object_property_get(Object *obj, const char *name, Visitor *v,
1317 Error **errp)
1318 {
1319 Error *err = NULL;
1320 ObjectProperty *prop = object_property_find(obj, name, errp);
1321
1322 if (prop == NULL) {
1323 return false;
1324 }
1325
1326 if (!prop->get) {
1327 error_setg(errp, QERR_PERMISSION_DENIED);
1328 return false;
1329 }
1330 prop->get(obj, v, name, prop->opaque, &err);
1331 error_propagate(errp, err);
1332 return !err;
1333 }
1334
1335 bool object_property_set(Object *obj, const char *name, Visitor *v,
1336 Error **errp)
1337 {
1338 Error *err = NULL;
1339 ObjectProperty *prop = object_property_find(obj, name, errp);
1340
1341 if (prop == NULL) {
1342 return false;
1343 }
1344
1345 if (!prop->set) {
1346 error_setg(errp, QERR_PERMISSION_DENIED);
1347 return false;
1348 }
1349 prop->set(obj, v, name, prop->opaque, &err);
1350 error_propagate(errp, err);
1351 return !err;
1352 }
1353
1354 bool object_property_set_str(Object *obj, const char *name,
1355 const char *value, Error **errp)
1356 {
1357 QString *qstr = qstring_from_str(value);
1358 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
1359
1360 qobject_unref(qstr);
1361 return ok;
1362 }
1363
1364 char *object_property_get_str(Object *obj, const char *name,
1365 Error **errp)
1366 {
1367 QObject *ret = object_property_get_qobject(obj, name, errp);
1368 char *retval;
1369
1370 if (!ret) {
1371 return NULL;
1372 }
1373
1374 retval = g_strdup(qobject_get_try_str(ret));
1375 if (!retval) {
1376 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1377 }
1378
1379 qobject_unref(ret);
1380 return retval;
1381 }
1382
1383 bool object_property_set_link(Object *obj, const char *name,
1384 Object *value, Error **errp)
1385 {
1386 g_autofree char *path = NULL;
1387
1388 if (value) {
1389 path = object_get_canonical_path(value);
1390 }
1391 return object_property_set_str(obj, name, path ?: "", errp);
1392 }
1393
1394 Object *object_property_get_link(Object *obj, const char *name,
1395 Error **errp)
1396 {
1397 char *str = object_property_get_str(obj, name, errp);
1398 Object *target = NULL;
1399
1400 if (str && *str) {
1401 target = object_resolve_path(str, NULL);
1402 if (!target) {
1403 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1404 "Device '%s' not found", str);
1405 }
1406 }
1407
1408 g_free(str);
1409 return target;
1410 }
1411
1412 bool object_property_set_bool(Object *obj, const char *name,
1413 bool value, Error **errp)
1414 {
1415 QBool *qbool = qbool_from_bool(value);
1416 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
1417
1418 qobject_unref(qbool);
1419 return ok;
1420 }
1421
1422 bool object_property_get_bool(Object *obj, const char *name,
1423 Error **errp)
1424 {
1425 QObject *ret = object_property_get_qobject(obj, name, errp);
1426 QBool *qbool;
1427 bool retval;
1428
1429 if (!ret) {
1430 return false;
1431 }
1432 qbool = qobject_to(QBool, ret);
1433 if (!qbool) {
1434 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1435 retval = false;
1436 } else {
1437 retval = qbool_get_bool(qbool);
1438 }
1439
1440 qobject_unref(ret);
1441 return retval;
1442 }
1443
1444 bool object_property_set_int(Object *obj, const char *name,
1445 int64_t value, Error **errp)
1446 {
1447 QNum *qnum = qnum_from_int(value);
1448 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1449
1450 qobject_unref(qnum);
1451 return ok;
1452 }
1453
1454 int64_t object_property_get_int(Object *obj, const char *name,
1455 Error **errp)
1456 {
1457 QObject *ret = object_property_get_qobject(obj, name, errp);
1458 QNum *qnum;
1459 int64_t retval;
1460
1461 if (!ret) {
1462 return -1;
1463 }
1464
1465 qnum = qobject_to(QNum, ret);
1466 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1467 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1468 retval = -1;
1469 }
1470
1471 qobject_unref(ret);
1472 return retval;
1473 }
1474
1475 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1476 {
1477 Visitor *v = qobject_input_visitor_new(prop->defval);
1478
1479 assert(prop->set != NULL);
1480 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1481
1482 visit_free(v);
1483 }
1484
1485 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1486 {
1487 assert(!prop->defval);
1488 assert(!prop->init);
1489
1490 prop->defval = defval;
1491 prop->init = object_property_init_defval;
1492 }
1493
1494 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1495 {
1496 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1497 }
1498
1499 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1500 {
1501 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1502 }
1503
1504 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1505 {
1506 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1507 }
1508
1509 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1510 {
1511 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1512 }
1513
1514 bool object_property_set_uint(Object *obj, const char *name,
1515 uint64_t value, Error **errp)
1516 {
1517 QNum *qnum = qnum_from_uint(value);
1518 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1519
1520 qobject_unref(qnum);
1521 return ok;
1522 }
1523
1524 uint64_t object_property_get_uint(Object *obj, const char *name,
1525 Error **errp)
1526 {
1527 QObject *ret = object_property_get_qobject(obj, name, errp);
1528 QNum *qnum;
1529 uint64_t retval;
1530
1531 if (!ret) {
1532 return 0;
1533 }
1534 qnum = qobject_to(QNum, ret);
1535 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1536 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1537 retval = 0;
1538 }
1539
1540 qobject_unref(ret);
1541 return retval;
1542 }
1543
1544 typedef struct EnumProperty {
1545 const QEnumLookup *lookup;
1546 int (*get)(Object *, Error **);
1547 void (*set)(Object *, int, Error **);
1548 } EnumProperty;
1549
1550 int object_property_get_enum(Object *obj, const char *name,
1551 const char *typename, Error **errp)
1552 {
1553 char *str;
1554 int ret;
1555 ObjectProperty *prop = object_property_find(obj, name, errp);
1556 EnumProperty *enumprop;
1557
1558 if (prop == NULL) {
1559 return 0;
1560 }
1561
1562 if (!g_str_equal(prop->type, typename)) {
1563 error_setg(errp, "Property %s on %s is not '%s' enum type",
1564 name, object_class_get_name(
1565 object_get_class(obj)), typename);
1566 return 0;
1567 }
1568
1569 enumprop = prop->opaque;
1570
1571 str = object_property_get_str(obj, name, errp);
1572 if (!str) {
1573 return 0;
1574 }
1575
1576 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1577 g_free(str);
1578
1579 return ret;
1580 }
1581
1582 bool object_property_parse(Object *obj, const char *name,
1583 const char *string, Error **errp)
1584 {
1585 Visitor *v = string_input_visitor_new(string);
1586 bool ok = object_property_set(obj, name, v, errp);
1587
1588 visit_free(v);
1589 return ok;
1590 }
1591
1592 char *object_property_print(Object *obj, const char *name, bool human,
1593 Error **errp)
1594 {
1595 Visitor *v;
1596 char *string = NULL;
1597
1598 v = string_output_visitor_new(human, &string);
1599 if (!object_property_get(obj, name, v, errp)) {
1600 goto out;
1601 }
1602
1603 visit_complete(v, &string);
1604
1605 out:
1606 visit_free(v);
1607 return string;
1608 }
1609
1610 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1611 {
1612 ObjectProperty *prop = object_property_find(obj, name, errp);
1613 if (prop == NULL) {
1614 return NULL;
1615 }
1616
1617 return prop->type;
1618 }
1619
1620 Object *object_get_root(void)
1621 {
1622 static Object *root;
1623
1624 if (!root) {
1625 root = object_new("container");
1626 }
1627
1628 return root;
1629 }
1630
1631 Object *object_get_objects_root(void)
1632 {
1633 return container_get(object_get_root(), "/objects");
1634 }
1635
1636 Object *object_get_internal_root(void)
1637 {
1638 static Object *internal_root;
1639
1640 if (!internal_root) {
1641 internal_root = object_new("container");
1642 }
1643
1644 return internal_root;
1645 }
1646
1647 static void object_get_child_property(Object *obj, Visitor *v,
1648 const char *name, void *opaque,
1649 Error **errp)
1650 {
1651 Object *child = opaque;
1652 char *path;
1653
1654 path = object_get_canonical_path(child);
1655 visit_type_str(v, name, &path, errp);
1656 g_free(path);
1657 }
1658
1659 static Object *object_resolve_child_property(Object *parent, void *opaque,
1660 const char *part)
1661 {
1662 return opaque;
1663 }
1664
1665 static void object_finalize_child_property(Object *obj, const char *name,
1666 void *opaque)
1667 {
1668 Object *child = opaque;
1669
1670 if (child->class->unparent) {
1671 (child->class->unparent)(child);
1672 }
1673 child->parent = NULL;
1674 object_unref(child);
1675 }
1676
1677 ObjectProperty *
1678 object_property_add_child(Object *obj, const char *name,
1679 Object *child)
1680 {
1681 g_autofree char *type = NULL;
1682 ObjectProperty *op;
1683
1684 assert(!child->parent);
1685
1686 type = g_strdup_printf("child<%s>", object_get_typename(child));
1687
1688 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1689 object_finalize_child_property, child);
1690 op->resolve = object_resolve_child_property;
1691 object_ref(child);
1692 child->parent = obj;
1693 return op;
1694 }
1695
1696 void object_property_allow_set_link(const Object *obj, const char *name,
1697 Object *val, Error **errp)
1698 {
1699 /* Allow the link to be set, always */
1700 }
1701
1702 typedef struct {
1703 union {
1704 Object **targetp;
1705 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1706 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1707 };
1708 void (*check)(const Object *, const char *, Object *, Error **);
1709 ObjectPropertyLinkFlags flags;
1710 } LinkProperty;
1711
1712 static Object **
1713 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1714 {
1715 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1716 return &lprop->target;
1717 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1718 return (void *)obj + lprop->offset;
1719 } else {
1720 return lprop->targetp;
1721 }
1722 }
1723
1724 static void object_get_link_property(Object *obj, Visitor *v,
1725 const char *name, void *opaque,
1726 Error **errp)
1727 {
1728 LinkProperty *lprop = opaque;
1729 Object **targetp = object_link_get_targetp(obj, lprop);
1730 char *path;
1731
1732 if (*targetp) {
1733 path = object_get_canonical_path(*targetp);
1734 visit_type_str(v, name, &path, errp);
1735 g_free(path);
1736 } else {
1737 path = (char *)"";
1738 visit_type_str(v, name, &path, errp);
1739 }
1740 }
1741
1742 /*
1743 * object_resolve_link:
1744 *
1745 * Lookup an object and ensure its type matches the link property type. This
1746 * is similar to object_resolve_path() except type verification against the
1747 * link property is performed.
1748 *
1749 * Returns: The matched object or NULL on path lookup failures.
1750 */
1751 static Object *object_resolve_link(Object *obj, const char *name,
1752 const char *path, Error **errp)
1753 {
1754 const char *type;
1755 char *target_type;
1756 bool ambiguous = false;
1757 Object *target;
1758
1759 /* Go from link<FOO> to FOO. */
1760 type = object_property_get_type(obj, name, NULL);
1761 target_type = g_strndup(&type[5], strlen(type) - 6);
1762 target = object_resolve_path_type(path, target_type, &ambiguous);
1763
1764 if (ambiguous) {
1765 error_setg(errp, "Path '%s' does not uniquely identify an object",
1766 path);
1767 } else if (!target) {
1768 target = object_resolve_path(path, &ambiguous);
1769 if (target || ambiguous) {
1770 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1771 } else {
1772 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1773 "Device '%s' not found", path);
1774 }
1775 target = NULL;
1776 }
1777 g_free(target_type);
1778
1779 return target;
1780 }
1781
1782 static void object_set_link_property(Object *obj, Visitor *v,
1783 const char *name, void *opaque,
1784 Error **errp)
1785 {
1786 Error *local_err = NULL;
1787 LinkProperty *prop = opaque;
1788 Object **targetp = object_link_get_targetp(obj, prop);
1789 Object *old_target = *targetp;
1790 Object *new_target;
1791 char *path = NULL;
1792
1793 if (!visit_type_str(v, name, &path, errp)) {
1794 return;
1795 }
1796
1797 if (*path) {
1798 new_target = object_resolve_link(obj, name, path, errp);
1799 if (!new_target) {
1800 g_free(path);
1801 return;
1802 }
1803 } else {
1804 new_target = NULL;
1805 }
1806
1807 g_free(path);
1808
1809 prop->check(obj, name, new_target, &local_err);
1810 if (local_err) {
1811 error_propagate(errp, local_err);
1812 return;
1813 }
1814
1815 *targetp = new_target;
1816 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1817 object_ref(new_target);
1818 object_unref(old_target);
1819 }
1820 }
1821
1822 static Object *object_resolve_link_property(Object *parent, void *opaque,
1823 const char *part)
1824 {
1825 LinkProperty *lprop = opaque;
1826
1827 return *object_link_get_targetp(parent, lprop);
1828 }
1829
1830 static void object_release_link_property(Object *obj, const char *name,
1831 void *opaque)
1832 {
1833 LinkProperty *prop = opaque;
1834 Object **targetp = object_link_get_targetp(obj, prop);
1835
1836 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1837 object_unref(*targetp);
1838 }
1839 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1840 g_free(prop);
1841 }
1842 }
1843
1844 static ObjectProperty *
1845 object_add_link_prop(Object *obj, const char *name,
1846 const char *type, void *ptr,
1847 void (*check)(const Object *, const char *,
1848 Object *, Error **),
1849 ObjectPropertyLinkFlags flags)
1850 {
1851 LinkProperty *prop = g_malloc(sizeof(*prop));
1852 g_autofree char *full_type = NULL;
1853 ObjectProperty *op;
1854
1855 if (flags & OBJ_PROP_LINK_DIRECT) {
1856 prop->target = ptr;
1857 } else {
1858 prop->targetp = ptr;
1859 }
1860 prop->check = check;
1861 prop->flags = flags;
1862
1863 full_type = g_strdup_printf("link<%s>", type);
1864
1865 op = object_property_add(obj, name, full_type,
1866 object_get_link_property,
1867 check ? object_set_link_property : NULL,
1868 object_release_link_property,
1869 prop);
1870 op->resolve = object_resolve_link_property;
1871 return op;
1872 }
1873
1874 ObjectProperty *
1875 object_property_add_link(Object *obj, const char *name,
1876 const char *type, Object **targetp,
1877 void (*check)(const Object *, const char *,
1878 Object *, Error **),
1879 ObjectPropertyLinkFlags flags)
1880 {
1881 return object_add_link_prop(obj, name, type, targetp, check, flags);
1882 }
1883
1884 ObjectProperty *
1885 object_class_property_add_link(ObjectClass *oc,
1886 const char *name,
1887 const char *type, ptrdiff_t offset,
1888 void (*check)(const Object *obj, const char *name,
1889 Object *val, Error **errp),
1890 ObjectPropertyLinkFlags flags)
1891 {
1892 LinkProperty *prop = g_new0(LinkProperty, 1);
1893 char *full_type;
1894 ObjectProperty *op;
1895
1896 prop->offset = offset;
1897 prop->check = check;
1898 prop->flags = flags | OBJ_PROP_LINK_CLASS;
1899
1900 full_type = g_strdup_printf("link<%s>", type);
1901
1902 op = object_class_property_add(oc, name, full_type,
1903 object_get_link_property,
1904 check ? object_set_link_property : NULL,
1905 object_release_link_property,
1906 prop);
1907
1908 op->resolve = object_resolve_link_property;
1909
1910 g_free(full_type);
1911 return op;
1912 }
1913
1914 ObjectProperty *
1915 object_property_add_const_link(Object *obj, const char *name,
1916 Object *target)
1917 {
1918 return object_add_link_prop(obj, name,
1919 object_get_typename(target), target,
1920 NULL, OBJ_PROP_LINK_DIRECT);
1921 }
1922
1923 char *object_get_canonical_path_component(const Object *obj)
1924 {
1925 ObjectProperty *prop = NULL;
1926 GHashTableIter iter;
1927
1928 if (obj->parent == NULL) {
1929 return NULL;
1930 }
1931
1932 g_hash_table_iter_init(&iter, obj->parent->properties);
1933 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1934 if (!object_property_is_child(prop)) {
1935 continue;
1936 }
1937
1938 if (prop->opaque == obj) {
1939 return g_strdup(prop->name);
1940 }
1941 }
1942
1943 /* obj had a parent but was not a child, should never happen */
1944 g_assert_not_reached();
1945 return NULL;
1946 }
1947
1948 char *object_get_canonical_path(const Object *obj)
1949 {
1950 Object *root = object_get_root();
1951 char *newpath, *path = NULL;
1952
1953 if (obj == root) {
1954 return g_strdup("/");
1955 }
1956
1957 do {
1958 char *component = object_get_canonical_path_component(obj);
1959
1960 if (!component) {
1961 /* A canonical path must be complete, so discard what was
1962 * collected so far.
1963 */
1964 g_free(path);
1965 return NULL;
1966 }
1967
1968 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1969 g_free(path);
1970 g_free(component);
1971 path = newpath;
1972 obj = obj->parent;
1973 } while (obj != root);
1974
1975 return path;
1976 }
1977
1978 Object *object_resolve_path_component(Object *parent, const char *part)
1979 {
1980 ObjectProperty *prop = object_property_find(parent, part, NULL);
1981 if (prop == NULL) {
1982 return NULL;
1983 }
1984
1985 if (prop->resolve) {
1986 return prop->resolve(parent, prop->opaque, part);
1987 } else {
1988 return NULL;
1989 }
1990 }
1991
1992 static Object *object_resolve_abs_path(Object *parent,
1993 char **parts,
1994 const char *typename)
1995 {
1996 Object *child;
1997
1998 if (*parts == NULL) {
1999 return object_dynamic_cast(parent, typename);
2000 }
2001
2002 if (strcmp(*parts, "") == 0) {
2003 return object_resolve_abs_path(parent, parts + 1, typename);
2004 }
2005
2006 child = object_resolve_path_component(parent, *parts);
2007 if (!child) {
2008 return NULL;
2009 }
2010
2011 return object_resolve_abs_path(child, parts + 1, typename);
2012 }
2013
2014 static Object *object_resolve_partial_path(Object *parent,
2015 char **parts,
2016 const char *typename,
2017 bool *ambiguous)
2018 {
2019 Object *obj;
2020 GHashTableIter iter;
2021 ObjectProperty *prop;
2022
2023 obj = object_resolve_abs_path(parent, parts, typename);
2024
2025 g_hash_table_iter_init(&iter, parent->properties);
2026 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2027 Object *found;
2028
2029 if (!object_property_is_child(prop)) {
2030 continue;
2031 }
2032
2033 found = object_resolve_partial_path(prop->opaque, parts,
2034 typename, ambiguous);
2035 if (found) {
2036 if (obj) {
2037 *ambiguous = true;
2038 return NULL;
2039 }
2040 obj = found;
2041 }
2042
2043 if (*ambiguous) {
2044 return NULL;
2045 }
2046 }
2047
2048 return obj;
2049 }
2050
2051 Object *object_resolve_path_type(const char *path, const char *typename,
2052 bool *ambiguousp)
2053 {
2054 Object *obj;
2055 char **parts;
2056
2057 parts = g_strsplit(path, "/", 0);
2058 assert(parts);
2059
2060 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2061 bool ambiguous = false;
2062 obj = object_resolve_partial_path(object_get_root(), parts,
2063 typename, &ambiguous);
2064 if (ambiguousp) {
2065 *ambiguousp = ambiguous;
2066 }
2067 } else {
2068 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2069 }
2070
2071 g_strfreev(parts);
2072
2073 return obj;
2074 }
2075
2076 Object *object_resolve_path(const char *path, bool *ambiguous)
2077 {
2078 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2079 }
2080
2081 typedef struct StringProperty
2082 {
2083 char *(*get)(Object *, Error **);
2084 void (*set)(Object *, const char *, Error **);
2085 } StringProperty;
2086
2087 static void property_get_str(Object *obj, Visitor *v, const char *name,
2088 void *opaque, Error **errp)
2089 {
2090 StringProperty *prop = opaque;
2091 char *value;
2092 Error *err = NULL;
2093
2094 value = prop->get(obj, &err);
2095 if (err) {
2096 error_propagate(errp, err);
2097 return;
2098 }
2099
2100 visit_type_str(v, name, &value, errp);
2101 g_free(value);
2102 }
2103
2104 static void property_set_str(Object *obj, Visitor *v, const char *name,
2105 void *opaque, Error **errp)
2106 {
2107 StringProperty *prop = opaque;
2108 char *value;
2109
2110 if (!visit_type_str(v, name, &value, errp)) {
2111 return;
2112 }
2113
2114 prop->set(obj, value, errp);
2115 g_free(value);
2116 }
2117
2118 static void property_release_str(Object *obj, const char *name,
2119 void *opaque)
2120 {
2121 StringProperty *prop = opaque;
2122 g_free(prop);
2123 }
2124
2125 ObjectProperty *
2126 object_property_add_str(Object *obj, const char *name,
2127 char *(*get)(Object *, Error **),
2128 void (*set)(Object *, const char *, Error **))
2129 {
2130 StringProperty *prop = g_malloc0(sizeof(*prop));
2131
2132 prop->get = get;
2133 prop->set = set;
2134
2135 return object_property_add(obj, name, "string",
2136 get ? property_get_str : NULL,
2137 set ? property_set_str : NULL,
2138 property_release_str,
2139 prop);
2140 }
2141
2142 ObjectProperty *
2143 object_class_property_add_str(ObjectClass *klass, const char *name,
2144 char *(*get)(Object *, Error **),
2145 void (*set)(Object *, const char *,
2146 Error **))
2147 {
2148 StringProperty *prop = g_malloc0(sizeof(*prop));
2149
2150 prop->get = get;
2151 prop->set = set;
2152
2153 return object_class_property_add(klass, name, "string",
2154 get ? property_get_str : NULL,
2155 set ? property_set_str : NULL,
2156 NULL,
2157 prop);
2158 }
2159
2160 typedef struct BoolProperty
2161 {
2162 bool (*get)(Object *, Error **);
2163 void (*set)(Object *, bool, Error **);
2164 } BoolProperty;
2165
2166 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2167 void *opaque, Error **errp)
2168 {
2169 BoolProperty *prop = opaque;
2170 bool value;
2171 Error *err = NULL;
2172
2173 value = prop->get(obj, &err);
2174 if (err) {
2175 error_propagate(errp, err);
2176 return;
2177 }
2178
2179 visit_type_bool(v, name, &value, errp);
2180 }
2181
2182 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2183 void *opaque, Error **errp)
2184 {
2185 BoolProperty *prop = opaque;
2186 bool value;
2187
2188 if (!visit_type_bool(v, name, &value, errp)) {
2189 return;
2190 }
2191
2192 prop->set(obj, value, errp);
2193 }
2194
2195 static void property_release_bool(Object *obj, const char *name,
2196 void *opaque)
2197 {
2198 BoolProperty *prop = opaque;
2199 g_free(prop);
2200 }
2201
2202 ObjectProperty *
2203 object_property_add_bool(Object *obj, const char *name,
2204 bool (*get)(Object *, Error **),
2205 void (*set)(Object *, bool, Error **))
2206 {
2207 BoolProperty *prop = g_malloc0(sizeof(*prop));
2208
2209 prop->get = get;
2210 prop->set = set;
2211
2212 return object_property_add(obj, name, "bool",
2213 get ? property_get_bool : NULL,
2214 set ? property_set_bool : NULL,
2215 property_release_bool,
2216 prop);
2217 }
2218
2219 ObjectProperty *
2220 object_class_property_add_bool(ObjectClass *klass, const char *name,
2221 bool (*get)(Object *, Error **),
2222 void (*set)(Object *, bool, Error **))
2223 {
2224 BoolProperty *prop = g_malloc0(sizeof(*prop));
2225
2226 prop->get = get;
2227 prop->set = set;
2228
2229 return object_class_property_add(klass, name, "bool",
2230 get ? property_get_bool : NULL,
2231 set ? property_set_bool : NULL,
2232 NULL,
2233 prop);
2234 }
2235
2236 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2237 void *opaque, Error **errp)
2238 {
2239 EnumProperty *prop = opaque;
2240 int value;
2241 Error *err = NULL;
2242
2243 value = prop->get(obj, &err);
2244 if (err) {
2245 error_propagate(errp, err);
2246 return;
2247 }
2248
2249 visit_type_enum(v, name, &value, prop->lookup, errp);
2250 }
2251
2252 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2253 void *opaque, Error **errp)
2254 {
2255 EnumProperty *prop = opaque;
2256 int value;
2257
2258 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
2259 return;
2260 }
2261 prop->set(obj, value, errp);
2262 }
2263
2264 static void property_release_enum(Object *obj, const char *name,
2265 void *opaque)
2266 {
2267 EnumProperty *prop = opaque;
2268 g_free(prop);
2269 }
2270
2271 ObjectProperty *
2272 object_property_add_enum(Object *obj, const char *name,
2273 const char *typename,
2274 const QEnumLookup *lookup,
2275 int (*get)(Object *, Error **),
2276 void (*set)(Object *, int, Error **))
2277 {
2278 EnumProperty *prop = g_malloc(sizeof(*prop));
2279
2280 prop->lookup = lookup;
2281 prop->get = get;
2282 prop->set = set;
2283
2284 return object_property_add(obj, name, typename,
2285 get ? property_get_enum : NULL,
2286 set ? property_set_enum : NULL,
2287 property_release_enum,
2288 prop);
2289 }
2290
2291 ObjectProperty *
2292 object_class_property_add_enum(ObjectClass *klass, const char *name,
2293 const char *typename,
2294 const QEnumLookup *lookup,
2295 int (*get)(Object *, Error **),
2296 void (*set)(Object *, int, Error **))
2297 {
2298 EnumProperty *prop = g_malloc(sizeof(*prop));
2299
2300 prop->lookup = lookup;
2301 prop->get = get;
2302 prop->set = set;
2303
2304 return object_class_property_add(klass, name, typename,
2305 get ? property_get_enum : NULL,
2306 set ? property_set_enum : NULL,
2307 NULL,
2308 prop);
2309 }
2310
2311 typedef struct TMProperty {
2312 void (*get)(Object *, struct tm *, Error **);
2313 } TMProperty;
2314
2315 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2316 void *opaque, Error **errp)
2317 {
2318 TMProperty *prop = opaque;
2319 Error *err = NULL;
2320 struct tm value;
2321
2322 prop->get(obj, &value, &err);
2323 if (err) {
2324 error_propagate(errp, err);
2325 return;
2326 }
2327
2328 if (!visit_start_struct(v, name, NULL, 0, errp)) {
2329 return;
2330 }
2331 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
2332 goto out_end;
2333 }
2334 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
2335 goto out_end;
2336 }
2337 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
2338 goto out_end;
2339 }
2340 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
2341 goto out_end;
2342 }
2343 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
2344 goto out_end;
2345 }
2346 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
2347 goto out_end;
2348 }
2349 visit_check_struct(v, errp);
2350 out_end:
2351 visit_end_struct(v, NULL);
2352 }
2353
2354 static void property_release_tm(Object *obj, const char *name,
2355 void *opaque)
2356 {
2357 TMProperty *prop = opaque;
2358 g_free(prop);
2359 }
2360
2361 ObjectProperty *
2362 object_property_add_tm(Object *obj, const char *name,
2363 void (*get)(Object *, struct tm *, Error **))
2364 {
2365 TMProperty *prop = g_malloc0(sizeof(*prop));
2366
2367 prop->get = get;
2368
2369 return object_property_add(obj, name, "struct tm",
2370 get ? property_get_tm : NULL, NULL,
2371 property_release_tm,
2372 prop);
2373 }
2374
2375 ObjectProperty *
2376 object_class_property_add_tm(ObjectClass *klass, const char *name,
2377 void (*get)(Object *, struct tm *, Error **))
2378 {
2379 TMProperty *prop = g_malloc0(sizeof(*prop));
2380
2381 prop->get = get;
2382
2383 return object_class_property_add(klass, name, "struct tm",
2384 get ? property_get_tm : NULL,
2385 NULL, NULL, prop);
2386 }
2387
2388 static char *object_get_type(Object *obj, Error **errp)
2389 {
2390 return g_strdup(object_get_typename(obj));
2391 }
2392
2393 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2394 void *opaque, Error **errp)
2395 {
2396 uint8_t value = *(uint8_t *)opaque;
2397 visit_type_uint8(v, name, &value, errp);
2398 }
2399
2400 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2401 void *opaque, Error **errp)
2402 {
2403 uint8_t *field = opaque;
2404 uint8_t value;
2405
2406 if (!visit_type_uint8(v, name, &value, errp)) {
2407 return;
2408 }
2409
2410 *field = value;
2411 }
2412
2413 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2414 void *opaque, Error **errp)
2415 {
2416 uint16_t value = *(uint16_t *)opaque;
2417 visit_type_uint16(v, name, &value, errp);
2418 }
2419
2420 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2421 void *opaque, Error **errp)
2422 {
2423 uint16_t *field = opaque;
2424 uint16_t value;
2425
2426 if (!visit_type_uint16(v, name, &value, errp)) {
2427 return;
2428 }
2429
2430 *field = value;
2431 }
2432
2433 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2434 void *opaque, Error **errp)
2435 {
2436 uint32_t value = *(uint32_t *)opaque;
2437 visit_type_uint32(v, name, &value, errp);
2438 }
2439
2440 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2441 void *opaque, Error **errp)
2442 {
2443 uint32_t *field = opaque;
2444 uint32_t value;
2445
2446 if (!visit_type_uint32(v, name, &value, errp)) {
2447 return;
2448 }
2449
2450 *field = value;
2451 }
2452
2453 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2454 void *opaque, Error **errp)
2455 {
2456 uint64_t value = *(uint64_t *)opaque;
2457 visit_type_uint64(v, name, &value, errp);
2458 }
2459
2460 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2461 void *opaque, Error **errp)
2462 {
2463 uint64_t *field = opaque;
2464 uint64_t value;
2465
2466 if (!visit_type_uint64(v, name, &value, errp)) {
2467 return;
2468 }
2469
2470 *field = value;
2471 }
2472
2473 ObjectProperty *
2474 object_property_add_uint8_ptr(Object *obj, const char *name,
2475 const uint8_t *v,
2476 ObjectPropertyFlags flags)
2477 {
2478 ObjectPropertyAccessor *getter = NULL;
2479 ObjectPropertyAccessor *setter = NULL;
2480
2481 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2482 getter = property_get_uint8_ptr;
2483 }
2484
2485 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2486 setter = property_set_uint8_ptr;
2487 }
2488
2489 return object_property_add(obj, name, "uint8",
2490 getter, setter, NULL, (void *)v);
2491 }
2492
2493 ObjectProperty *
2494 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2495 const uint8_t *v,
2496 ObjectPropertyFlags flags)
2497 {
2498 ObjectPropertyAccessor *getter = NULL;
2499 ObjectPropertyAccessor *setter = NULL;
2500
2501 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2502 getter = property_get_uint8_ptr;
2503 }
2504
2505 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2506 setter = property_set_uint8_ptr;
2507 }
2508
2509 return object_class_property_add(klass, name, "uint8",
2510 getter, setter, NULL, (void *)v);
2511 }
2512
2513 ObjectProperty *
2514 object_property_add_uint16_ptr(Object *obj, const char *name,
2515 const uint16_t *v,
2516 ObjectPropertyFlags flags)
2517 {
2518 ObjectPropertyAccessor *getter = NULL;
2519 ObjectPropertyAccessor *setter = NULL;
2520
2521 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2522 getter = property_get_uint16_ptr;
2523 }
2524
2525 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2526 setter = property_set_uint16_ptr;
2527 }
2528
2529 return object_property_add(obj, name, "uint16",
2530 getter, setter, NULL, (void *)v);
2531 }
2532
2533 ObjectProperty *
2534 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2535 const uint16_t *v,
2536 ObjectPropertyFlags flags)
2537 {
2538 ObjectPropertyAccessor *getter = NULL;
2539 ObjectPropertyAccessor *setter = NULL;
2540
2541 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2542 getter = property_get_uint16_ptr;
2543 }
2544
2545 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2546 setter = property_set_uint16_ptr;
2547 }
2548
2549 return object_class_property_add(klass, name, "uint16",
2550 getter, setter, NULL, (void *)v);
2551 }
2552
2553 ObjectProperty *
2554 object_property_add_uint32_ptr(Object *obj, const char *name,
2555 const uint32_t *v,
2556 ObjectPropertyFlags flags)
2557 {
2558 ObjectPropertyAccessor *getter = NULL;
2559 ObjectPropertyAccessor *setter = NULL;
2560
2561 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2562 getter = property_get_uint32_ptr;
2563 }
2564
2565 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2566 setter = property_set_uint32_ptr;
2567 }
2568
2569 return object_property_add(obj, name, "uint32",
2570 getter, setter, NULL, (void *)v);
2571 }
2572
2573 ObjectProperty *
2574 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2575 const uint32_t *v,
2576 ObjectPropertyFlags flags)
2577 {
2578 ObjectPropertyAccessor *getter = NULL;
2579 ObjectPropertyAccessor *setter = NULL;
2580
2581 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2582 getter = property_get_uint32_ptr;
2583 }
2584
2585 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2586 setter = property_set_uint32_ptr;
2587 }
2588
2589 return object_class_property_add(klass, name, "uint32",
2590 getter, setter, NULL, (void *)v);
2591 }
2592
2593 ObjectProperty *
2594 object_property_add_uint64_ptr(Object *obj, const char *name,
2595 const uint64_t *v,
2596 ObjectPropertyFlags flags)
2597 {
2598 ObjectPropertyAccessor *getter = NULL;
2599 ObjectPropertyAccessor *setter = NULL;
2600
2601 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2602 getter = property_get_uint64_ptr;
2603 }
2604
2605 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2606 setter = property_set_uint64_ptr;
2607 }
2608
2609 return object_property_add(obj, name, "uint64",
2610 getter, setter, NULL, (void *)v);
2611 }
2612
2613 ObjectProperty *
2614 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2615 const uint64_t *v,
2616 ObjectPropertyFlags flags)
2617 {
2618 ObjectPropertyAccessor *getter = NULL;
2619 ObjectPropertyAccessor *setter = NULL;
2620
2621 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2622 getter = property_get_uint64_ptr;
2623 }
2624
2625 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2626 setter = property_set_uint64_ptr;
2627 }
2628
2629 return object_class_property_add(klass, name, "uint64",
2630 getter, setter, NULL, (void *)v);
2631 }
2632
2633 typedef struct {
2634 Object *target_obj;
2635 char *target_name;
2636 } AliasProperty;
2637
2638 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2639 void *opaque, Error **errp)
2640 {
2641 AliasProperty *prop = opaque;
2642
2643 object_property_get(prop->target_obj, prop->target_name, v, errp);
2644 }
2645
2646 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2647 void *opaque, Error **errp)
2648 {
2649 AliasProperty *prop = opaque;
2650
2651 object_property_set(prop->target_obj, prop->target_name, v, errp);
2652 }
2653
2654 static Object *property_resolve_alias(Object *obj, void *opaque,
2655 const char *part)
2656 {
2657 AliasProperty *prop = opaque;
2658
2659 return object_resolve_path_component(prop->target_obj, prop->target_name);
2660 }
2661
2662 static void property_release_alias(Object *obj, const char *name, void *opaque)
2663 {
2664 AliasProperty *prop = opaque;
2665
2666 g_free(prop->target_name);
2667 g_free(prop);
2668 }
2669
2670 ObjectProperty *
2671 object_property_add_alias(Object *obj, const char *name,
2672 Object *target_obj, const char *target_name)
2673 {
2674 AliasProperty *prop;
2675 ObjectProperty *op;
2676 ObjectProperty *target_prop;
2677 g_autofree char *prop_type = NULL;
2678
2679 target_prop = object_property_find(target_obj, target_name,
2680 &error_abort);
2681
2682 if (object_property_is_child(target_prop)) {
2683 prop_type = g_strdup_printf("link%s",
2684 target_prop->type + strlen("child"));
2685 } else {
2686 prop_type = g_strdup(target_prop->type);
2687 }
2688
2689 prop = g_malloc(sizeof(*prop));
2690 prop->target_obj = target_obj;
2691 prop->target_name = g_strdup(target_name);
2692
2693 op = object_property_add(obj, name, prop_type,
2694 property_get_alias,
2695 property_set_alias,
2696 property_release_alias,
2697 prop);
2698 op->resolve = property_resolve_alias;
2699 if (target_prop->defval) {
2700 op->defval = qobject_ref(target_prop->defval);
2701 }
2702
2703 object_property_set_description(obj, op->name,
2704 target_prop->description);
2705 return op;
2706 }
2707
2708 void object_property_set_description(Object *obj, const char *name,
2709 const char *description)
2710 {
2711 ObjectProperty *op;
2712
2713 op = object_property_find(obj, name, &error_abort);
2714 g_free(op->description);
2715 op->description = g_strdup(description);
2716 }
2717
2718 void object_class_property_set_description(ObjectClass *klass,
2719 const char *name,
2720 const char *description)
2721 {
2722 ObjectProperty *op;
2723
2724 op = g_hash_table_lookup(klass->properties, name);
2725 g_free(op->description);
2726 op->description = g_strdup(description);
2727 }
2728
2729 static void object_class_init(ObjectClass *klass, void *data)
2730 {
2731 object_class_property_add_str(klass, "type", object_get_type,
2732 NULL);
2733 }
2734
2735 static void register_types(void)
2736 {
2737 static TypeInfo interface_info = {
2738 .name = TYPE_INTERFACE,
2739 .class_size = sizeof(InterfaceClass),
2740 .abstract = true,
2741 };
2742
2743 static TypeInfo object_info = {
2744 .name = TYPE_OBJECT,
2745 .instance_size = sizeof(Object),
2746 .class_init = object_class_init,
2747 .abstract = true,
2748 };
2749
2750 type_interface = type_register_internal(&interface_info);
2751 type_register_internal(&object_info);
2752 }
2753
2754 type_init(register_types)