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