s390x/pci: Fix harmless mistake in zpci's property fid's setter
[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 void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp)
389 {
390 int i;
391
392 if (!props) {
393 return;
394 }
395
396 for (i = 0; i < props->len; i++) {
397 GlobalProperty *p = g_ptr_array_index(props, i);
398 Error *err = NULL;
399
400 if (object_dynamic_cast(obj, p->driver) == NULL) {
401 continue;
402 }
403 if (p->optional && !object_property_find(obj, p->property, NULL)) {
404 continue;
405 }
406 p->used = true;
407 object_property_parse(obj, p->value, p->property, &err);
408 if (err != NULL) {
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;
419 } else {
420 warn_report_err(err);
421 }
422 }
423 }
424 }
425
426 /*
427 * Global property defaults
428 * Slot 0: accelerator's global property defaults
429 * Slot 1: machine's global property defaults
430 * Slot 2: global properties from legacy command line option
431 * Each is a GPtrArray of of GlobalProperty.
432 * Applied in order, later entries override earlier ones.
433 */
434 static GPtrArray *object_compat_props[3];
435
436 /*
437 * Retrieve @GPtrArray for global property defined with options
438 * other than "-global". These are generally used for syntactic
439 * sugar and legacy command line options.
440 */
441 void object_register_sugar_prop(const char *driver, const char *prop, const char *value)
442 {
443 GlobalProperty *g;
444 if (!object_compat_props[2]) {
445 object_compat_props[2] = g_ptr_array_new();
446 }
447 g = g_new0(GlobalProperty, 1);
448 g->driver = g_strdup(driver);
449 g->property = g_strdup(prop);
450 g->value = g_strdup(value);
451 g_ptr_array_add(object_compat_props[2], g);
452 }
453
454 /*
455 * Set machine's global property defaults to @compat_props.
456 * May be called at most once.
457 */
458 void object_set_machine_compat_props(GPtrArray *compat_props)
459 {
460 assert(!object_compat_props[1]);
461 object_compat_props[1] = compat_props;
462 }
463
464 /*
465 * Set accelerator's global property defaults to @compat_props.
466 * May be called at most once.
467 */
468 void object_set_accelerator_compat_props(GPtrArray *compat_props)
469 {
470 assert(!object_compat_props[0]);
471 object_compat_props[0] = compat_props;
472 }
473
474 void object_apply_compat_props(Object *obj)
475 {
476 int i;
477
478 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
479 object_apply_global_props(obj, object_compat_props[i],
480 i == 2 ? &error_fatal : &error_abort);
481 }
482 }
483
484 static void object_class_property_init_all(Object *obj)
485 {
486 ObjectPropertyIterator iter;
487 ObjectProperty *prop;
488
489 object_class_property_iter_init(&iter, object_get_class(obj));
490 while ((prop = object_property_iter_next(&iter))) {
491 if (prop->init) {
492 prop->init(obj, prop);
493 }
494 }
495 }
496
497 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
498 {
499 type_initialize(type);
500
501 g_assert(type->instance_size >= sizeof(Object));
502 g_assert(type->abstract == false);
503 g_assert(size >= type->instance_size);
504
505 memset(obj, 0, type->instance_size);
506 obj->class = type->class;
507 object_ref(obj);
508 object_class_property_init_all(obj);
509 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
510 NULL, object_property_free);
511 object_init_with_type(obj, type);
512 object_post_init_with_type(obj, type);
513 }
514
515 void object_initialize(void *data, size_t size, const char *typename)
516 {
517 TypeImpl *type = type_get_by_name(typename);
518
519 if (!type) {
520 error_report("missing object type '%s'", typename);
521 abort();
522 }
523
524 object_initialize_with_type(data, size, type);
525 }
526
527 void object_initialize_child_with_props(Object *parentobj,
528 const char *propname,
529 void *childobj, size_t size, const char *type,
530 Error **errp, ...)
531 {
532 va_list vargs;
533
534 va_start(vargs, errp);
535 object_initialize_child_with_propsv(parentobj, propname,
536 childobj, size, type, errp, vargs);
537 va_end(vargs);
538 }
539
540 void object_initialize_child_with_propsv(Object *parentobj,
541 const char *propname,
542 void *childobj, size_t size, const char *type,
543 Error **errp, va_list vargs)
544 {
545 Error *local_err = NULL;
546 Object *obj;
547 UserCreatable *uc;
548
549 object_initialize(childobj, size, type);
550 obj = OBJECT(childobj);
551
552 object_set_propv(obj, &local_err, vargs);
553 if (local_err) {
554 goto out;
555 }
556
557 object_property_add_child(parentobj, propname, obj);
558
559 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
560 if (uc) {
561 user_creatable_complete(uc, &local_err);
562 if (local_err) {
563 object_unparent(obj);
564 goto out;
565 }
566 }
567
568 out:
569 /*
570 * We want @obj's reference to be 1 on success, 0 on failure.
571 * On success, it's 2: one taken by object_initialize(), and one
572 * by object_property_add_child().
573 * On failure in object_initialize() or earlier, it's 1.
574 * On failure afterwards, it's also 1: object_unparent() releases
575 * the reference taken by object_property_add_child().
576 */
577 object_unref(obj);
578
579 error_propagate(errp, local_err);
580 }
581
582 void object_initialize_child_internal(Object *parent,
583 const char *propname,
584 void *child, size_t size,
585 const char *type)
586 {
587 object_initialize_child_with_props(parent, propname, child, size, type,
588 &error_abort, NULL);
589 }
590
591 static inline bool object_property_is_child(ObjectProperty *prop)
592 {
593 return strstart(prop->type, "child<", NULL);
594 }
595
596 static void object_property_del_all(Object *obj)
597 {
598 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
599 ObjectProperty *prop;
600 ObjectPropertyIterator iter;
601 bool released;
602
603 do {
604 released = false;
605 object_property_iter_init(&iter, obj);
606 while ((prop = object_property_iter_next(&iter)) != NULL) {
607 if (g_hash_table_add(done, prop)) {
608 if (prop->release) {
609 prop->release(obj, prop->name, prop->opaque);
610 released = true;
611 break;
612 }
613 }
614 }
615 } while (released);
616
617 g_hash_table_unref(obj->properties);
618 }
619
620 static void object_property_del_child(Object *obj, Object *child)
621 {
622 ObjectProperty *prop;
623 GHashTableIter iter;
624 gpointer key, value;
625
626 g_hash_table_iter_init(&iter, obj->properties);
627 while (g_hash_table_iter_next(&iter, &key, &value)) {
628 prop = value;
629 if (object_property_is_child(prop) && prop->opaque == child) {
630 if (prop->release) {
631 prop->release(obj, prop->name, prop->opaque);
632 prop->release = NULL;
633 }
634 break;
635 }
636 }
637 g_hash_table_iter_init(&iter, obj->properties);
638 while (g_hash_table_iter_next(&iter, &key, &value)) {
639 prop = value;
640 if (object_property_is_child(prop) && prop->opaque == child) {
641 g_hash_table_iter_remove(&iter);
642 break;
643 }
644 }
645 }
646
647 void object_unparent(Object *obj)
648 {
649 if (obj->parent) {
650 object_property_del_child(obj->parent, obj);
651 }
652 }
653
654 static void object_deinit(Object *obj, TypeImpl *type)
655 {
656 if (type->instance_finalize) {
657 type->instance_finalize(obj);
658 }
659
660 if (type_has_parent(type)) {
661 object_deinit(obj, type_get_parent(type));
662 }
663 }
664
665 static void object_finalize(void *data)
666 {
667 Object *obj = data;
668 TypeImpl *ti = obj->class->type;
669
670 object_property_del_all(obj);
671 object_deinit(obj, ti);
672
673 g_assert(obj->ref == 0);
674 if (obj->free) {
675 obj->free(obj);
676 }
677 }
678
679 static Object *object_new_with_type(Type type)
680 {
681 Object *obj;
682
683 g_assert(type != NULL);
684 type_initialize(type);
685
686 obj = g_malloc(type->instance_size);
687 object_initialize_with_type(obj, type->instance_size, type);
688 obj->free = g_free;
689
690 return obj;
691 }
692
693 Object *object_new_with_class(ObjectClass *klass)
694 {
695 return object_new_with_type(klass->type);
696 }
697
698 Object *object_new(const char *typename)
699 {
700 TypeImpl *ti = type_get_by_name(typename);
701
702 return object_new_with_type(ti);
703 }
704
705
706 Object *object_new_with_props(const char *typename,
707 Object *parent,
708 const char *id,
709 Error **errp,
710 ...)
711 {
712 va_list vargs;
713 Object *obj;
714
715 va_start(vargs, errp);
716 obj = object_new_with_propv(typename, parent, id, errp, vargs);
717 va_end(vargs);
718
719 return obj;
720 }
721
722
723 Object *object_new_with_propv(const char *typename,
724 Object *parent,
725 const char *id,
726 Error **errp,
727 va_list vargs)
728 {
729 Object *obj;
730 ObjectClass *klass;
731 Error *local_err = NULL;
732 UserCreatable *uc;
733
734 klass = object_class_by_name(typename);
735 if (!klass) {
736 error_setg(errp, "invalid object type: %s", typename);
737 return NULL;
738 }
739
740 if (object_class_is_abstract(klass)) {
741 error_setg(errp, "object type '%s' is abstract", typename);
742 return NULL;
743 }
744 obj = object_new_with_type(klass->type);
745
746 if (object_set_propv(obj, &local_err, vargs) < 0) {
747 goto error;
748 }
749
750 if (id != NULL) {
751 object_property_add_child(parent, id, obj);
752 }
753
754 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
755 if (uc) {
756 user_creatable_complete(uc, &local_err);
757 if (local_err) {
758 if (id != NULL) {
759 object_unparent(obj);
760 }
761 goto error;
762 }
763 }
764
765 object_unref(obj);
766 return obj;
767
768 error:
769 error_propagate(errp, local_err);
770 object_unref(obj);
771 return NULL;
772 }
773
774
775 int object_set_props(Object *obj,
776 Error **errp,
777 ...)
778 {
779 va_list vargs;
780 int ret;
781
782 va_start(vargs, errp);
783 ret = object_set_propv(obj, errp, vargs);
784 va_end(vargs);
785
786 return ret;
787 }
788
789
790 int object_set_propv(Object *obj,
791 Error **errp,
792 va_list vargs)
793 {
794 const char *propname;
795 Error *local_err = NULL;
796
797 propname = va_arg(vargs, char *);
798 while (propname != NULL) {
799 const char *value = va_arg(vargs, char *);
800
801 g_assert(value != NULL);
802 object_property_parse(obj, value, propname, &local_err);
803 if (local_err) {
804 error_propagate(errp, local_err);
805 return -1;
806 }
807 propname = va_arg(vargs, char *);
808 }
809
810 return 0;
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 void object_property_get(Object *obj, Visitor *v, const char *name,
1317 Error **errp)
1318 {
1319 ObjectProperty *prop = object_property_find(obj, name, errp);
1320 if (prop == NULL) {
1321 return;
1322 }
1323
1324 if (!prop->get) {
1325 error_setg(errp, QERR_PERMISSION_DENIED);
1326 } else {
1327 prop->get(obj, v, name, prop->opaque, errp);
1328 }
1329 }
1330
1331 void object_property_set(Object *obj, Visitor *v, const char *name,
1332 Error **errp)
1333 {
1334 ObjectProperty *prop = object_property_find(obj, name, errp);
1335 if (prop == NULL) {
1336 return;
1337 }
1338
1339 if (!prop->set) {
1340 error_setg(errp, QERR_PERMISSION_DENIED);
1341 } else {
1342 prop->set(obj, v, name, prop->opaque, errp);
1343 }
1344 }
1345
1346 void object_property_set_str(Object *obj, const char *value,
1347 const char *name, Error **errp)
1348 {
1349 QString *qstr = qstring_from_str(value);
1350 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1351
1352 qobject_unref(qstr);
1353 }
1354
1355 char *object_property_get_str(Object *obj, const char *name,
1356 Error **errp)
1357 {
1358 QObject *ret = object_property_get_qobject(obj, name, errp);
1359 char *retval;
1360
1361 if (!ret) {
1362 return NULL;
1363 }
1364
1365 retval = g_strdup(qobject_get_try_str(ret));
1366 if (!retval) {
1367 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1368 }
1369
1370 qobject_unref(ret);
1371 return retval;
1372 }
1373
1374 void object_property_set_link(Object *obj, Object *value,
1375 const char *name, Error **errp)
1376 {
1377 if (value) {
1378 char *path = object_get_canonical_path(value);
1379 object_property_set_str(obj, path, name, errp);
1380 g_free(path);
1381 } else {
1382 object_property_set_str(obj, "", name, errp);
1383 }
1384 }
1385
1386 Object *object_property_get_link(Object *obj, const char *name,
1387 Error **errp)
1388 {
1389 char *str = object_property_get_str(obj, name, errp);
1390 Object *target = NULL;
1391
1392 if (str && *str) {
1393 target = object_resolve_path(str, NULL);
1394 if (!target) {
1395 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1396 "Device '%s' not found", str);
1397 }
1398 }
1399
1400 g_free(str);
1401 return target;
1402 }
1403
1404 void object_property_set_bool(Object *obj, bool value,
1405 const char *name, Error **errp)
1406 {
1407 QBool *qbool = qbool_from_bool(value);
1408 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1409
1410 qobject_unref(qbool);
1411 }
1412
1413 bool object_property_get_bool(Object *obj, const char *name,
1414 Error **errp)
1415 {
1416 QObject *ret = object_property_get_qobject(obj, name, errp);
1417 QBool *qbool;
1418 bool retval;
1419
1420 if (!ret) {
1421 return false;
1422 }
1423 qbool = qobject_to(QBool, ret);
1424 if (!qbool) {
1425 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1426 retval = false;
1427 } else {
1428 retval = qbool_get_bool(qbool);
1429 }
1430
1431 qobject_unref(ret);
1432 return retval;
1433 }
1434
1435 void object_property_set_int(Object *obj, int64_t value,
1436 const char *name, Error **errp)
1437 {
1438 QNum *qnum = qnum_from_int(value);
1439 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1440
1441 qobject_unref(qnum);
1442 }
1443
1444 int64_t object_property_get_int(Object *obj, const char *name,
1445 Error **errp)
1446 {
1447 QObject *ret = object_property_get_qobject(obj, name, errp);
1448 QNum *qnum;
1449 int64_t retval;
1450
1451 if (!ret) {
1452 return -1;
1453 }
1454
1455 qnum = qobject_to(QNum, ret);
1456 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1457 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1458 retval = -1;
1459 }
1460
1461 qobject_unref(ret);
1462 return retval;
1463 }
1464
1465 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1466 {
1467 Visitor *v = qobject_input_visitor_new(prop->defval);
1468
1469 assert(prop->set != NULL);
1470 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1471
1472 visit_free(v);
1473 }
1474
1475 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1476 {
1477 assert(!prop->defval);
1478 assert(!prop->init);
1479
1480 prop->defval = defval;
1481 prop->init = object_property_init_defval;
1482 }
1483
1484 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1485 {
1486 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1487 }
1488
1489 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1490 {
1491 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1492 }
1493
1494 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1495 {
1496 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1497 }
1498
1499 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1500 {
1501 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1502 }
1503
1504 void object_property_set_uint(Object *obj, uint64_t value,
1505 const char *name, Error **errp)
1506 {
1507 QNum *qnum = qnum_from_uint(value);
1508
1509 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1510 qobject_unref(qnum);
1511 }
1512
1513 uint64_t object_property_get_uint(Object *obj, const char *name,
1514 Error **errp)
1515 {
1516 QObject *ret = object_property_get_qobject(obj, name, errp);
1517 QNum *qnum;
1518 uint64_t retval;
1519
1520 if (!ret) {
1521 return 0;
1522 }
1523 qnum = qobject_to(QNum, ret);
1524 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1525 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1526 retval = 0;
1527 }
1528
1529 qobject_unref(ret);
1530 return retval;
1531 }
1532
1533 typedef struct EnumProperty {
1534 const QEnumLookup *lookup;
1535 int (*get)(Object *, Error **);
1536 void (*set)(Object *, int, Error **);
1537 } EnumProperty;
1538
1539 int object_property_get_enum(Object *obj, const char *name,
1540 const char *typename, Error **errp)
1541 {
1542 char *str;
1543 int ret;
1544 ObjectProperty *prop = object_property_find(obj, name, errp);
1545 EnumProperty *enumprop;
1546
1547 if (prop == NULL) {
1548 return 0;
1549 }
1550
1551 if (!g_str_equal(prop->type, typename)) {
1552 error_setg(errp, "Property %s on %s is not '%s' enum type",
1553 name, object_class_get_name(
1554 object_get_class(obj)), typename);
1555 return 0;
1556 }
1557
1558 enumprop = prop->opaque;
1559
1560 str = object_property_get_str(obj, name, errp);
1561 if (!str) {
1562 return 0;
1563 }
1564
1565 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1566 g_free(str);
1567
1568 return ret;
1569 }
1570
1571 void object_property_parse(Object *obj, const char *string,
1572 const char *name, Error **errp)
1573 {
1574 Visitor *v = string_input_visitor_new(string);
1575 object_property_set(obj, v, name, errp);
1576 visit_free(v);
1577 }
1578
1579 char *object_property_print(Object *obj, const char *name, bool human,
1580 Error **errp)
1581 {
1582 Visitor *v;
1583 char *string = NULL;
1584 Error *local_err = NULL;
1585
1586 v = string_output_visitor_new(human, &string);
1587 object_property_get(obj, v, name, &local_err);
1588 if (local_err) {
1589 error_propagate(errp, local_err);
1590 goto out;
1591 }
1592
1593 visit_complete(v, &string);
1594
1595 out:
1596 visit_free(v);
1597 return string;
1598 }
1599
1600 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1601 {
1602 ObjectProperty *prop = object_property_find(obj, name, errp);
1603 if (prop == NULL) {
1604 return NULL;
1605 }
1606
1607 return prop->type;
1608 }
1609
1610 Object *object_get_root(void)
1611 {
1612 static Object *root;
1613
1614 if (!root) {
1615 root = object_new("container");
1616 }
1617
1618 return root;
1619 }
1620
1621 Object *object_get_objects_root(void)
1622 {
1623 return container_get(object_get_root(), "/objects");
1624 }
1625
1626 Object *object_get_internal_root(void)
1627 {
1628 static Object *internal_root;
1629
1630 if (!internal_root) {
1631 internal_root = object_new("container");
1632 }
1633
1634 return internal_root;
1635 }
1636
1637 static void object_get_child_property(Object *obj, Visitor *v,
1638 const char *name, void *opaque,
1639 Error **errp)
1640 {
1641 Object *child = opaque;
1642 char *path;
1643
1644 path = object_get_canonical_path(child);
1645 visit_type_str(v, name, &path, errp);
1646 g_free(path);
1647 }
1648
1649 static Object *object_resolve_child_property(Object *parent, void *opaque,
1650 const char *part)
1651 {
1652 return opaque;
1653 }
1654
1655 static void object_finalize_child_property(Object *obj, const char *name,
1656 void *opaque)
1657 {
1658 Object *child = opaque;
1659
1660 if (child->class->unparent) {
1661 (child->class->unparent)(child);
1662 }
1663 child->parent = NULL;
1664 object_unref(child);
1665 }
1666
1667 ObjectProperty *
1668 object_property_add_child(Object *obj, const char *name,
1669 Object *child)
1670 {
1671 g_autofree char *type = NULL;
1672 ObjectProperty *op;
1673
1674 assert(!child->parent);
1675
1676 type = g_strdup_printf("child<%s>", object_get_typename(child));
1677
1678 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1679 object_finalize_child_property, child);
1680 op->resolve = object_resolve_child_property;
1681 object_ref(child);
1682 child->parent = obj;
1683 return op;
1684 }
1685
1686 void object_property_allow_set_link(const Object *obj, const char *name,
1687 Object *val, Error **errp)
1688 {
1689 /* Allow the link to be set, always */
1690 }
1691
1692 typedef struct {
1693 union {
1694 Object **targetp;
1695 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1696 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1697 };
1698 void (*check)(const Object *, const char *, Object *, Error **);
1699 ObjectPropertyLinkFlags flags;
1700 } LinkProperty;
1701
1702 static Object **
1703 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1704 {
1705 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1706 return &lprop->target;
1707 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1708 return (void *)obj + lprop->offset;
1709 } else {
1710 return lprop->targetp;
1711 }
1712 }
1713
1714 static void object_get_link_property(Object *obj, Visitor *v,
1715 const char *name, void *opaque,
1716 Error **errp)
1717 {
1718 LinkProperty *lprop = opaque;
1719 Object **targetp = object_link_get_targetp(obj, lprop);
1720 char *path;
1721
1722 if (*targetp) {
1723 path = object_get_canonical_path(*targetp);
1724 visit_type_str(v, name, &path, errp);
1725 g_free(path);
1726 } else {
1727 path = (char *)"";
1728 visit_type_str(v, name, &path, errp);
1729 }
1730 }
1731
1732 /*
1733 * object_resolve_link:
1734 *
1735 * Lookup an object and ensure its type matches the link property type. This
1736 * is similar to object_resolve_path() except type verification against the
1737 * link property is performed.
1738 *
1739 * Returns: The matched object or NULL on path lookup failures.
1740 */
1741 static Object *object_resolve_link(Object *obj, const char *name,
1742 const char *path, Error **errp)
1743 {
1744 const char *type;
1745 char *target_type;
1746 bool ambiguous = false;
1747 Object *target;
1748
1749 /* Go from link<FOO> to FOO. */
1750 type = object_property_get_type(obj, name, NULL);
1751 target_type = g_strndup(&type[5], strlen(type) - 6);
1752 target = object_resolve_path_type(path, target_type, &ambiguous);
1753
1754 if (ambiguous) {
1755 error_setg(errp, "Path '%s' does not uniquely identify an object",
1756 path);
1757 } else if (!target) {
1758 target = object_resolve_path(path, &ambiguous);
1759 if (target || ambiguous) {
1760 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1761 } else {
1762 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1763 "Device '%s' not found", path);
1764 }
1765 target = NULL;
1766 }
1767 g_free(target_type);
1768
1769 return target;
1770 }
1771
1772 static void object_set_link_property(Object *obj, Visitor *v,
1773 const char *name, void *opaque,
1774 Error **errp)
1775 {
1776 Error *local_err = NULL;
1777 LinkProperty *prop = opaque;
1778 Object **targetp = object_link_get_targetp(obj, prop);
1779 Object *old_target = *targetp;
1780 Object *new_target = NULL;
1781 char *path = NULL;
1782
1783 visit_type_str(v, name, &path, &local_err);
1784
1785 if (!local_err && strcmp(path, "") != 0) {
1786 new_target = object_resolve_link(obj, name, path, &local_err);
1787 }
1788
1789 g_free(path);
1790 if (local_err) {
1791 error_propagate(errp, local_err);
1792 return;
1793 }
1794
1795 prop->check(obj, name, new_target, &local_err);
1796 if (local_err) {
1797 error_propagate(errp, local_err);
1798 return;
1799 }
1800
1801 *targetp = new_target;
1802 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1803 object_ref(new_target);
1804 object_unref(old_target);
1805 }
1806 }
1807
1808 static Object *object_resolve_link_property(Object *parent, void *opaque,
1809 const char *part)
1810 {
1811 LinkProperty *lprop = opaque;
1812
1813 return *object_link_get_targetp(parent, lprop);
1814 }
1815
1816 static void object_release_link_property(Object *obj, const char *name,
1817 void *opaque)
1818 {
1819 LinkProperty *prop = opaque;
1820 Object **targetp = object_link_get_targetp(obj, prop);
1821
1822 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1823 object_unref(*targetp);
1824 }
1825 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1826 g_free(prop);
1827 }
1828 }
1829
1830 static ObjectProperty *
1831 object_add_link_prop(Object *obj, const char *name,
1832 const char *type, void *ptr,
1833 void (*check)(const Object *, const char *,
1834 Object *, Error **),
1835 ObjectPropertyLinkFlags flags)
1836 {
1837 LinkProperty *prop = g_malloc(sizeof(*prop));
1838 g_autofree char *full_type = NULL;
1839 ObjectProperty *op;
1840
1841 if (flags & OBJ_PROP_LINK_DIRECT) {
1842 prop->target = ptr;
1843 } else {
1844 prop->targetp = ptr;
1845 }
1846 prop->check = check;
1847 prop->flags = flags;
1848
1849 full_type = g_strdup_printf("link<%s>", type);
1850
1851 op = object_property_add(obj, name, full_type,
1852 object_get_link_property,
1853 check ? object_set_link_property : NULL,
1854 object_release_link_property,
1855 prop);
1856 op->resolve = object_resolve_link_property;
1857 return op;
1858 }
1859
1860 ObjectProperty *
1861 object_property_add_link(Object *obj, const char *name,
1862 const char *type, Object **targetp,
1863 void (*check)(const Object *, const char *,
1864 Object *, Error **),
1865 ObjectPropertyLinkFlags flags)
1866 {
1867 return object_add_link_prop(obj, name, type, targetp, check, flags);
1868 }
1869
1870 ObjectProperty *
1871 object_class_property_add_link(ObjectClass *oc,
1872 const char *name,
1873 const char *type, ptrdiff_t offset,
1874 void (*check)(const Object *obj, const char *name,
1875 Object *val, Error **errp),
1876 ObjectPropertyLinkFlags flags)
1877 {
1878 LinkProperty *prop = g_new0(LinkProperty, 1);
1879 char *full_type;
1880 ObjectProperty *op;
1881
1882 prop->offset = offset;
1883 prop->check = check;
1884 prop->flags = flags | OBJ_PROP_LINK_CLASS;
1885
1886 full_type = g_strdup_printf("link<%s>", type);
1887
1888 op = object_class_property_add(oc, name, full_type,
1889 object_get_link_property,
1890 check ? object_set_link_property : NULL,
1891 object_release_link_property,
1892 prop);
1893
1894 op->resolve = object_resolve_link_property;
1895
1896 g_free(full_type);
1897 return op;
1898 }
1899
1900 ObjectProperty *
1901 object_property_add_const_link(Object *obj, const char *name,
1902 Object *target)
1903 {
1904 return object_add_link_prop(obj, name,
1905 object_get_typename(target), target,
1906 NULL, OBJ_PROP_LINK_DIRECT);
1907 }
1908
1909 char *object_get_canonical_path_component(const Object *obj)
1910 {
1911 ObjectProperty *prop = NULL;
1912 GHashTableIter iter;
1913
1914 if (obj->parent == NULL) {
1915 return NULL;
1916 }
1917
1918 g_hash_table_iter_init(&iter, obj->parent->properties);
1919 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1920 if (!object_property_is_child(prop)) {
1921 continue;
1922 }
1923
1924 if (prop->opaque == obj) {
1925 return g_strdup(prop->name);
1926 }
1927 }
1928
1929 /* obj had a parent but was not a child, should never happen */
1930 g_assert_not_reached();
1931 return NULL;
1932 }
1933
1934 char *object_get_canonical_path(const Object *obj)
1935 {
1936 Object *root = object_get_root();
1937 char *newpath, *path = NULL;
1938
1939 if (obj == root) {
1940 return g_strdup("/");
1941 }
1942
1943 do {
1944 char *component = object_get_canonical_path_component(obj);
1945
1946 if (!component) {
1947 /* A canonical path must be complete, so discard what was
1948 * collected so far.
1949 */
1950 g_free(path);
1951 return NULL;
1952 }
1953
1954 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1955 g_free(path);
1956 g_free(component);
1957 path = newpath;
1958 obj = obj->parent;
1959 } while (obj != root);
1960
1961 return path;
1962 }
1963
1964 Object *object_resolve_path_component(Object *parent, const char *part)
1965 {
1966 ObjectProperty *prop = object_property_find(parent, part, NULL);
1967 if (prop == NULL) {
1968 return NULL;
1969 }
1970
1971 if (prop->resolve) {
1972 return prop->resolve(parent, prop->opaque, part);
1973 } else {
1974 return NULL;
1975 }
1976 }
1977
1978 static Object *object_resolve_abs_path(Object *parent,
1979 char **parts,
1980 const char *typename)
1981 {
1982 Object *child;
1983
1984 if (*parts == NULL) {
1985 return object_dynamic_cast(parent, typename);
1986 }
1987
1988 if (strcmp(*parts, "") == 0) {
1989 return object_resolve_abs_path(parent, parts + 1, typename);
1990 }
1991
1992 child = object_resolve_path_component(parent, *parts);
1993 if (!child) {
1994 return NULL;
1995 }
1996
1997 return object_resolve_abs_path(child, parts + 1, typename);
1998 }
1999
2000 static Object *object_resolve_partial_path(Object *parent,
2001 char **parts,
2002 const char *typename,
2003 bool *ambiguous)
2004 {
2005 Object *obj;
2006 GHashTableIter iter;
2007 ObjectProperty *prop;
2008
2009 obj = object_resolve_abs_path(parent, parts, typename);
2010
2011 g_hash_table_iter_init(&iter, parent->properties);
2012 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2013 Object *found;
2014
2015 if (!object_property_is_child(prop)) {
2016 continue;
2017 }
2018
2019 found = object_resolve_partial_path(prop->opaque, parts,
2020 typename, ambiguous);
2021 if (found) {
2022 if (obj) {
2023 *ambiguous = true;
2024 return NULL;
2025 }
2026 obj = found;
2027 }
2028
2029 if (*ambiguous) {
2030 return NULL;
2031 }
2032 }
2033
2034 return obj;
2035 }
2036
2037 Object *object_resolve_path_type(const char *path, const char *typename,
2038 bool *ambiguousp)
2039 {
2040 Object *obj;
2041 char **parts;
2042
2043 parts = g_strsplit(path, "/", 0);
2044 assert(parts);
2045
2046 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2047 bool ambiguous = false;
2048 obj = object_resolve_partial_path(object_get_root(), parts,
2049 typename, &ambiguous);
2050 if (ambiguousp) {
2051 *ambiguousp = ambiguous;
2052 }
2053 } else {
2054 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2055 }
2056
2057 g_strfreev(parts);
2058
2059 return obj;
2060 }
2061
2062 Object *object_resolve_path(const char *path, bool *ambiguous)
2063 {
2064 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2065 }
2066
2067 typedef struct StringProperty
2068 {
2069 char *(*get)(Object *, Error **);
2070 void (*set)(Object *, const char *, Error **);
2071 } StringProperty;
2072
2073 static void property_get_str(Object *obj, Visitor *v, const char *name,
2074 void *opaque, Error **errp)
2075 {
2076 StringProperty *prop = opaque;
2077 char *value;
2078 Error *err = NULL;
2079
2080 value = prop->get(obj, &err);
2081 if (err) {
2082 error_propagate(errp, err);
2083 return;
2084 }
2085
2086 visit_type_str(v, name, &value, errp);
2087 g_free(value);
2088 }
2089
2090 static void property_set_str(Object *obj, Visitor *v, const char *name,
2091 void *opaque, Error **errp)
2092 {
2093 StringProperty *prop = opaque;
2094 char *value;
2095 Error *local_err = NULL;
2096
2097 if (!visit_type_str(v, name, &value, &local_err)) {
2098 error_propagate(errp, local_err);
2099 return;
2100 }
2101
2102 prop->set(obj, value, errp);
2103 g_free(value);
2104 }
2105
2106 static void property_release_str(Object *obj, const char *name,
2107 void *opaque)
2108 {
2109 StringProperty *prop = opaque;
2110 g_free(prop);
2111 }
2112
2113 ObjectProperty *
2114 object_property_add_str(Object *obj, const char *name,
2115 char *(*get)(Object *, Error **),
2116 void (*set)(Object *, const char *, Error **))
2117 {
2118 StringProperty *prop = g_malloc0(sizeof(*prop));
2119
2120 prop->get = get;
2121 prop->set = set;
2122
2123 return object_property_add(obj, name, "string",
2124 get ? property_get_str : NULL,
2125 set ? property_set_str : NULL,
2126 property_release_str,
2127 prop);
2128 }
2129
2130 ObjectProperty *
2131 object_class_property_add_str(ObjectClass *klass, const char *name,
2132 char *(*get)(Object *, Error **),
2133 void (*set)(Object *, const char *,
2134 Error **))
2135 {
2136 StringProperty *prop = g_malloc0(sizeof(*prop));
2137
2138 prop->get = get;
2139 prop->set = set;
2140
2141 return object_class_property_add(klass, name, "string",
2142 get ? property_get_str : NULL,
2143 set ? property_set_str : NULL,
2144 NULL,
2145 prop);
2146 }
2147
2148 typedef struct BoolProperty
2149 {
2150 bool (*get)(Object *, Error **);
2151 void (*set)(Object *, bool, Error **);
2152 } BoolProperty;
2153
2154 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2155 void *opaque, Error **errp)
2156 {
2157 BoolProperty *prop = opaque;
2158 bool value;
2159 Error *err = NULL;
2160
2161 value = prop->get(obj, &err);
2162 if (err) {
2163 error_propagate(errp, err);
2164 return;
2165 }
2166
2167 visit_type_bool(v, name, &value, errp);
2168 }
2169
2170 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2171 void *opaque, Error **errp)
2172 {
2173 BoolProperty *prop = opaque;
2174 bool value;
2175 Error *local_err = NULL;
2176
2177 if (!visit_type_bool(v, name, &value, &local_err)) {
2178 error_propagate(errp, local_err);
2179 return;
2180 }
2181
2182 prop->set(obj, value, errp);
2183 }
2184
2185 static void property_release_bool(Object *obj, const char *name,
2186 void *opaque)
2187 {
2188 BoolProperty *prop = opaque;
2189 g_free(prop);
2190 }
2191
2192 ObjectProperty *
2193 object_property_add_bool(Object *obj, const char *name,
2194 bool (*get)(Object *, Error **),
2195 void (*set)(Object *, bool, Error **))
2196 {
2197 BoolProperty *prop = g_malloc0(sizeof(*prop));
2198
2199 prop->get = get;
2200 prop->set = set;
2201
2202 return object_property_add(obj, name, "bool",
2203 get ? property_get_bool : NULL,
2204 set ? property_set_bool : NULL,
2205 property_release_bool,
2206 prop);
2207 }
2208
2209 ObjectProperty *
2210 object_class_property_add_bool(ObjectClass *klass, const char *name,
2211 bool (*get)(Object *, Error **),
2212 void (*set)(Object *, bool, Error **))
2213 {
2214 BoolProperty *prop = g_malloc0(sizeof(*prop));
2215
2216 prop->get = get;
2217 prop->set = set;
2218
2219 return object_class_property_add(klass, name, "bool",
2220 get ? property_get_bool : NULL,
2221 set ? property_set_bool : NULL,
2222 NULL,
2223 prop);
2224 }
2225
2226 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2227 void *opaque, Error **errp)
2228 {
2229 EnumProperty *prop = opaque;
2230 int value;
2231 Error *err = NULL;
2232
2233 value = prop->get(obj, &err);
2234 if (err) {
2235 error_propagate(errp, err);
2236 return;
2237 }
2238
2239 visit_type_enum(v, name, &value, prop->lookup, errp);
2240 }
2241
2242 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2243 void *opaque, Error **errp)
2244 {
2245 EnumProperty *prop = opaque;
2246 int value;
2247 Error *err = NULL;
2248
2249 if (!visit_type_enum(v, name, &value, prop->lookup, &err)) {
2250 error_propagate(errp, err);
2251 return;
2252 }
2253 prop->set(obj, value, errp);
2254 }
2255
2256 static void property_release_enum(Object *obj, const char *name,
2257 void *opaque)
2258 {
2259 EnumProperty *prop = opaque;
2260 g_free(prop);
2261 }
2262
2263 ObjectProperty *
2264 object_property_add_enum(Object *obj, const char *name,
2265 const char *typename,
2266 const QEnumLookup *lookup,
2267 int (*get)(Object *, Error **),
2268 void (*set)(Object *, int, Error **))
2269 {
2270 EnumProperty *prop = g_malloc(sizeof(*prop));
2271
2272 prop->lookup = lookup;
2273 prop->get = get;
2274 prop->set = set;
2275
2276 return object_property_add(obj, name, typename,
2277 get ? property_get_enum : NULL,
2278 set ? property_set_enum : NULL,
2279 property_release_enum,
2280 prop);
2281 }
2282
2283 ObjectProperty *
2284 object_class_property_add_enum(ObjectClass *klass, const char *name,
2285 const char *typename,
2286 const QEnumLookup *lookup,
2287 int (*get)(Object *, Error **),
2288 void (*set)(Object *, int, Error **))
2289 {
2290 EnumProperty *prop = g_malloc(sizeof(*prop));
2291
2292 prop->lookup = lookup;
2293 prop->get = get;
2294 prop->set = set;
2295
2296 return object_class_property_add(klass, name, typename,
2297 get ? property_get_enum : NULL,
2298 set ? property_set_enum : NULL,
2299 NULL,
2300 prop);
2301 }
2302
2303 typedef struct TMProperty {
2304 void (*get)(Object *, struct tm *, Error **);
2305 } TMProperty;
2306
2307 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2308 void *opaque, Error **errp)
2309 {
2310 TMProperty *prop = opaque;
2311 Error *err = NULL;
2312 struct tm value;
2313
2314 prop->get(obj, &value, &err);
2315 if (err) {
2316 goto out;
2317 }
2318
2319 if (!visit_start_struct(v, name, NULL, 0, &err)) {
2320 goto out;
2321 }
2322 if (!visit_type_int32(v, "tm_year", &value.tm_year, &err)) {
2323 goto out_end;
2324 }
2325 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, &err)) {
2326 goto out_end;
2327 }
2328 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, &err)) {
2329 goto out_end;
2330 }
2331 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, &err)) {
2332 goto out_end;
2333 }
2334 if (!visit_type_int32(v, "tm_min", &value.tm_min, &err)) {
2335 goto out_end;
2336 }
2337 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, &err)) {
2338 goto out_end;
2339 }
2340 visit_check_struct(v, &err);
2341 out_end:
2342 visit_end_struct(v, NULL);
2343 out:
2344 error_propagate(errp, err);
2345
2346 }
2347
2348 static void property_release_tm(Object *obj, const char *name,
2349 void *opaque)
2350 {
2351 TMProperty *prop = opaque;
2352 g_free(prop);
2353 }
2354
2355 ObjectProperty *
2356 object_property_add_tm(Object *obj, const char *name,
2357 void (*get)(Object *, struct tm *, Error **))
2358 {
2359 TMProperty *prop = g_malloc0(sizeof(*prop));
2360
2361 prop->get = get;
2362
2363 return object_property_add(obj, name, "struct tm",
2364 get ? property_get_tm : NULL, NULL,
2365 property_release_tm,
2366 prop);
2367 }
2368
2369 ObjectProperty *
2370 object_class_property_add_tm(ObjectClass *klass, const char *name,
2371 void (*get)(Object *, struct tm *, Error **))
2372 {
2373 TMProperty *prop = g_malloc0(sizeof(*prop));
2374
2375 prop->get = get;
2376
2377 return object_class_property_add(klass, name, "struct tm",
2378 get ? property_get_tm : NULL,
2379 NULL, NULL, prop);
2380 }
2381
2382 static char *qdev_get_type(Object *obj, Error **errp)
2383 {
2384 return g_strdup(object_get_typename(obj));
2385 }
2386
2387 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2388 void *opaque, Error **errp)
2389 {
2390 uint8_t value = *(uint8_t *)opaque;
2391 visit_type_uint8(v, name, &value, errp);
2392 }
2393
2394 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2395 void *opaque, Error **errp)
2396 {
2397 uint8_t *field = opaque;
2398 uint8_t value;
2399 Error *local_err = NULL;
2400
2401 if (!visit_type_uint8(v, name, &value, &local_err)) {
2402 error_propagate(errp, local_err);
2403 return;
2404 }
2405
2406 *field = value;
2407 }
2408
2409 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2410 void *opaque, Error **errp)
2411 {
2412 uint16_t value = *(uint16_t *)opaque;
2413 visit_type_uint16(v, name, &value, errp);
2414 }
2415
2416 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2417 void *opaque, Error **errp)
2418 {
2419 uint16_t *field = opaque;
2420 uint16_t value;
2421 Error *local_err = NULL;
2422
2423 if (!visit_type_uint16(v, name, &value, &local_err)) {
2424 error_propagate(errp, local_err);
2425 return;
2426 }
2427
2428 *field = value;
2429 }
2430
2431 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2432 void *opaque, Error **errp)
2433 {
2434 uint32_t value = *(uint32_t *)opaque;
2435 visit_type_uint32(v, name, &value, errp);
2436 }
2437
2438 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2439 void *opaque, Error **errp)
2440 {
2441 uint32_t *field = opaque;
2442 uint32_t value;
2443 Error *local_err = NULL;
2444
2445 if (!visit_type_uint32(v, name, &value, &local_err)) {
2446 error_propagate(errp, local_err);
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 Error *local_err = NULL;
2466
2467 if (!visit_type_uint64(v, name, &value, &local_err)) {
2468 error_propagate(errp, local_err);
2469 return;
2470 }
2471
2472 *field = value;
2473 }
2474
2475 ObjectProperty *
2476 object_property_add_uint8_ptr(Object *obj, const char *name,
2477 const uint8_t *v,
2478 ObjectPropertyFlags flags)
2479 {
2480 ObjectPropertyAccessor *getter = NULL;
2481 ObjectPropertyAccessor *setter = NULL;
2482
2483 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2484 getter = property_get_uint8_ptr;
2485 }
2486
2487 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2488 setter = property_set_uint8_ptr;
2489 }
2490
2491 return object_property_add(obj, name, "uint8",
2492 getter, setter, NULL, (void *)v);
2493 }
2494
2495 ObjectProperty *
2496 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2497 const uint8_t *v,
2498 ObjectPropertyFlags flags)
2499 {
2500 ObjectPropertyAccessor *getter = NULL;
2501 ObjectPropertyAccessor *setter = NULL;
2502
2503 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2504 getter = property_get_uint8_ptr;
2505 }
2506
2507 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2508 setter = property_set_uint8_ptr;
2509 }
2510
2511 return object_class_property_add(klass, name, "uint8",
2512 getter, setter, NULL, (void *)v);
2513 }
2514
2515 ObjectProperty *
2516 object_property_add_uint16_ptr(Object *obj, const char *name,
2517 const uint16_t *v,
2518 ObjectPropertyFlags flags)
2519 {
2520 ObjectPropertyAccessor *getter = NULL;
2521 ObjectPropertyAccessor *setter = NULL;
2522
2523 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2524 getter = property_get_uint16_ptr;
2525 }
2526
2527 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2528 setter = property_set_uint16_ptr;
2529 }
2530
2531 return object_property_add(obj, name, "uint16",
2532 getter, setter, NULL, (void *)v);
2533 }
2534
2535 ObjectProperty *
2536 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2537 const uint16_t *v,
2538 ObjectPropertyFlags flags)
2539 {
2540 ObjectPropertyAccessor *getter = NULL;
2541 ObjectPropertyAccessor *setter = NULL;
2542
2543 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2544 getter = property_get_uint16_ptr;
2545 }
2546
2547 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2548 setter = property_set_uint16_ptr;
2549 }
2550
2551 return object_class_property_add(klass, name, "uint16",
2552 getter, setter, NULL, (void *)v);
2553 }
2554
2555 ObjectProperty *
2556 object_property_add_uint32_ptr(Object *obj, const char *name,
2557 const uint32_t *v,
2558 ObjectPropertyFlags flags)
2559 {
2560 ObjectPropertyAccessor *getter = NULL;
2561 ObjectPropertyAccessor *setter = NULL;
2562
2563 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2564 getter = property_get_uint32_ptr;
2565 }
2566
2567 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2568 setter = property_set_uint32_ptr;
2569 }
2570
2571 return object_property_add(obj, name, "uint32",
2572 getter, setter, NULL, (void *)v);
2573 }
2574
2575 ObjectProperty *
2576 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2577 const uint32_t *v,
2578 ObjectPropertyFlags flags)
2579 {
2580 ObjectPropertyAccessor *getter = NULL;
2581 ObjectPropertyAccessor *setter = NULL;
2582
2583 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2584 getter = property_get_uint32_ptr;
2585 }
2586
2587 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2588 setter = property_set_uint32_ptr;
2589 }
2590
2591 return object_class_property_add(klass, name, "uint32",
2592 getter, setter, NULL, (void *)v);
2593 }
2594
2595 ObjectProperty *
2596 object_property_add_uint64_ptr(Object *obj, const char *name,
2597 const uint64_t *v,
2598 ObjectPropertyFlags flags)
2599 {
2600 ObjectPropertyAccessor *getter = NULL;
2601 ObjectPropertyAccessor *setter = NULL;
2602
2603 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2604 getter = property_get_uint64_ptr;
2605 }
2606
2607 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2608 setter = property_set_uint64_ptr;
2609 }
2610
2611 return object_property_add(obj, name, "uint64",
2612 getter, setter, NULL, (void *)v);
2613 }
2614
2615 ObjectProperty *
2616 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2617 const uint64_t *v,
2618 ObjectPropertyFlags flags)
2619 {
2620 ObjectPropertyAccessor *getter = NULL;
2621 ObjectPropertyAccessor *setter = NULL;
2622
2623 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2624 getter = property_get_uint64_ptr;
2625 }
2626
2627 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2628 setter = property_set_uint64_ptr;
2629 }
2630
2631 return object_class_property_add(klass, name, "uint64",
2632 getter, setter, NULL, (void *)v);
2633 }
2634
2635 typedef struct {
2636 Object *target_obj;
2637 char *target_name;
2638 } AliasProperty;
2639
2640 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2641 void *opaque, Error **errp)
2642 {
2643 AliasProperty *prop = opaque;
2644
2645 object_property_get(prop->target_obj, v, prop->target_name, errp);
2646 }
2647
2648 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2649 void *opaque, Error **errp)
2650 {
2651 AliasProperty *prop = opaque;
2652
2653 object_property_set(prop->target_obj, v, prop->target_name, errp);
2654 }
2655
2656 static Object *property_resolve_alias(Object *obj, void *opaque,
2657 const char *part)
2658 {
2659 AliasProperty *prop = opaque;
2660
2661 return object_resolve_path_component(prop->target_obj, prop->target_name);
2662 }
2663
2664 static void property_release_alias(Object *obj, const char *name, void *opaque)
2665 {
2666 AliasProperty *prop = opaque;
2667
2668 g_free(prop->target_name);
2669 g_free(prop);
2670 }
2671
2672 ObjectProperty *
2673 object_property_add_alias(Object *obj, const char *name,
2674 Object *target_obj, const char *target_name)
2675 {
2676 AliasProperty *prop;
2677 ObjectProperty *op;
2678 ObjectProperty *target_prop;
2679 g_autofree char *prop_type = NULL;
2680
2681 target_prop = object_property_find(target_obj, target_name,
2682 &error_abort);
2683
2684 if (object_property_is_child(target_prop)) {
2685 prop_type = g_strdup_printf("link%s",
2686 target_prop->type + strlen("child"));
2687 } else {
2688 prop_type = g_strdup(target_prop->type);
2689 }
2690
2691 prop = g_malloc(sizeof(*prop));
2692 prop->target_obj = target_obj;
2693 prop->target_name = g_strdup(target_name);
2694
2695 op = object_property_add(obj, name, prop_type,
2696 property_get_alias,
2697 property_set_alias,
2698 property_release_alias,
2699 prop);
2700 op->resolve = property_resolve_alias;
2701 if (target_prop->defval) {
2702 op->defval = qobject_ref(target_prop->defval);
2703 }
2704
2705 object_property_set_description(obj, op->name,
2706 target_prop->description);
2707 return op;
2708 }
2709
2710 void object_property_set_description(Object *obj, const char *name,
2711 const char *description)
2712 {
2713 ObjectProperty *op;
2714
2715 op = object_property_find(obj, name, &error_abort);
2716 g_free(op->description);
2717 op->description = g_strdup(description);
2718 }
2719
2720 void object_class_property_set_description(ObjectClass *klass,
2721 const char *name,
2722 const char *description)
2723 {
2724 ObjectProperty *op;
2725
2726 op = g_hash_table_lookup(klass->properties, name);
2727 g_free(op->description);
2728 op->description = g_strdup(description);
2729 }
2730
2731 static void object_class_init(ObjectClass *klass, void *data)
2732 {
2733 object_class_property_add_str(klass, "type", qdev_get_type,
2734 NULL);
2735 }
2736
2737 static void register_types(void)
2738 {
2739 static TypeInfo interface_info = {
2740 .name = TYPE_INTERFACE,
2741 .class_size = sizeof(InterfaceClass),
2742 .abstract = true,
2743 };
2744
2745 static TypeInfo object_info = {
2746 .name = TYPE_OBJECT,
2747 .instance_size = sizeof(Object),
2748 .class_init = object_class_init,
2749 .abstract = true,
2750 };
2751
2752 type_interface = type_register_internal(&interface_info);
2753 type_register_internal(&object_info);
2754 }
2755
2756 type_init(register_types)