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