1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "qemu/osdep.h"
28#include "chardev/char.h"
29#include "qemu/sockets.h"
30#include "qapi/error.h"
31#include "qom/object_interfaces.h"
32#include "net/can_emu.h"
33#include "net/can_host.h"
34
35struct CanBusState {
36 Object object;
37
38 QTAILQ_HEAD(, CanBusClientState) clients;
39};
40
41static void can_host_disconnect(CanHostState *ch)
42{
43 CanHostClass *chc = CAN_HOST_GET_CLASS(ch);
44
45 can_bus_remove_client(&ch->bus_client);
46 chc->disconnect(ch);
47}
48
49static void can_host_connect(CanHostState *ch, Error **errp)
50{
51 CanHostClass *chc = CAN_HOST_GET_CLASS(ch);
52 Error *local_err = NULL;
53
54 chc->connect(ch, &local_err);
55 if (local_err) {
56 error_propagate(errp, local_err);
57 return;
58 }
59
60 can_bus_insert_client(ch->bus, &ch->bus_client);
61}
62
63static void can_host_unparent(Object *obj)
64{
65 can_host_disconnect(CAN_HOST(obj));
66}
67
68static void can_host_complete(UserCreatable *uc, Error **errp)
69{
70 can_host_connect(CAN_HOST(uc), errp);
71}
72
73static void can_host_instance_init(Object *obj)
74{
75 CanHostState *ch = CAN_HOST(obj);
76
77 object_property_add_link(obj, "canbus", TYPE_CAN_BUS,
78 (Object **)&ch->bus,
79 object_property_allow_set_link,
80 OBJ_PROP_LINK_STRONG,
81 &error_abort);
82}
83
84static void can_host_class_init(ObjectClass *klass,
85 void *class_data G_GNUC_UNUSED)
86{
87 UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass);
88
89 klass->unparent = can_host_unparent;
90 uc_klass->complete = can_host_complete;
91}
92
93static const TypeInfo can_host_info = {
94 .parent = TYPE_OBJECT,
95 .name = TYPE_CAN_HOST,
96 .instance_size = sizeof(CanHostState),
97 .class_size = sizeof(CanHostClass),
98 .abstract = true,
99 .instance_init = can_host_instance_init,
100 .class_init = can_host_class_init,
101 .interfaces = (InterfaceInfo[]) {
102 { TYPE_USER_CREATABLE },
103 { }
104 }
105};
106
107static void can_host_register_types(void)
108{
109 type_register_static(&can_host_info);
110}
111
112type_init(can_host_register_types);
113