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