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 "net/can_emu.h"
34#include "qom/object_interfaces.h"
35
36
37
38static const uint8_t dlc2len[] = {
39 0, 1, 2, 3, 4, 5, 6, 7,
40 8, 12, 16, 20, 24, 32, 48, 64
41};
42
43
44uint8_t can_dlc2len(uint8_t can_dlc)
45{
46 return dlc2len[can_dlc & 0x0F];
47}
48
49static const uint8_t len2dlc[] = {
50 0, 1, 2, 3, 4, 5, 6, 7, 8,
51 9, 9, 9, 9,
52 10, 10, 10, 10,
53 11, 11, 11, 11,
54 12, 12, 12, 12,
55 13, 13, 13, 13, 13, 13, 13, 13,
56 14, 14, 14, 14, 14, 14, 14, 14,
57 14, 14, 14, 14, 14, 14, 14, 14,
58 15, 15, 15, 15, 15, 15, 15, 15,
59 15, 15, 15, 15, 15, 15, 15, 15
60};
61
62
63uint8_t can_len2dlc(uint8_t len)
64{
65 if (unlikely(len > 64)) {
66 return 0xF;
67 }
68
69 return len2dlc[len];
70}
71
72struct CanBusState {
73 Object object;
74
75 QTAILQ_HEAD(, CanBusClientState) clients;
76};
77
78static void can_bus_instance_init(Object *object)
79{
80 CanBusState *bus = (CanBusState *)object;
81
82 QTAILQ_INIT(&bus->clients);
83}
84
85int can_bus_insert_client(CanBusState *bus, CanBusClientState *client)
86{
87 client->bus = bus;
88 QTAILQ_INSERT_TAIL(&bus->clients, client, next);
89 return 0;
90}
91
92int can_bus_remove_client(CanBusClientState *client)
93{
94 CanBusState *bus = client->bus;
95 if (bus == NULL) {
96 return 0;
97 }
98
99 QTAILQ_REMOVE(&bus->clients, client, next);
100 client->bus = NULL;
101 return 1;
102}
103
104ssize_t can_bus_client_send(CanBusClientState *client,
105 const struct qemu_can_frame *frames, size_t frames_cnt)
106{
107 int ret = 0;
108 CanBusState *bus = client->bus;
109 CanBusClientState *peer;
110 if (bus == NULL) {
111 return -1;
112 }
113
114 QTAILQ_FOREACH(peer, &bus->clients, next) {
115 if (peer->info->can_receive(peer)) {
116 if (peer == client) {
117
118 continue;
119 }
120 if (peer->info->receive(peer, frames, frames_cnt) > 0) {
121 ret = 1;
122 }
123 }
124 }
125
126 return ret;
127}
128
129int can_bus_filter_match(struct qemu_can_filter *filter, qemu_canid_t can_id)
130{
131 int m;
132 if (((can_id | filter->can_mask) & QEMU_CAN_ERR_FLAG)) {
133 return (filter->can_mask & QEMU_CAN_ERR_FLAG) != 0;
134 }
135 m = (can_id & filter->can_mask) == (filter->can_id & filter->can_mask);
136 return filter->can_id & QEMU_CAN_INV_FILTER ? !m : m;
137}
138
139int can_bus_client_set_filters(CanBusClientState *client,
140 const struct qemu_can_filter *filters, size_t filters_cnt)
141{
142 return 0;
143}
144
145
146static bool can_bus_can_be_deleted(UserCreatable *uc)
147{
148 return false;
149}
150
151static void can_bus_class_init(ObjectClass *klass,
152 void *class_data G_GNUC_UNUSED)
153{
154 UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass);
155
156 uc_klass->can_be_deleted = can_bus_can_be_deleted;
157}
158
159static const TypeInfo can_bus_info = {
160 .parent = TYPE_OBJECT,
161 .name = TYPE_CAN_BUS,
162 .instance_size = sizeof(CanBusState),
163 .instance_init = can_bus_instance_init,
164 .class_init = can_bus_class_init,
165 .interfaces = (InterfaceInfo[]) {
166 { TYPE_USER_CREATABLE },
167 { }
168 }
169};
170
171static void can_bus_register_types(void)
172{
173 type_register_static(&can_bus_info);
174}
175
176type_init(can_bus_register_types);
177