1 | /* |
2 | * CAN common CAN bus emulation support |
3 | * |
4 | * Copyright (c) 2013-2014 Jin Yang |
5 | * Copyright (c) 2014-2018 Pavel Pisa |
6 | * |
7 | * Initial development supported by Google GSoC 2013 from RTEMS project slot |
8 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | * of this software and associated documentation files (the "Software"), to deal |
11 | * in the Software without restriction, including without limitation the rights |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | * copies of the Software, and to permit persons to whom the Software is |
14 | * furnished to do so, subject to the following conditions: |
15 | * |
16 | * The above copyright notice and this permission notice shall be included in |
17 | * all copies or substantial portions of the Software. |
18 | * |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | * THE SOFTWARE. |
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 | struct CanBusState { |
37 | Object object; |
38 | |
39 | QTAILQ_HEAD(, CanBusClientState) clients; |
40 | }; |
41 | |
42 | static void can_bus_instance_init(Object *object) |
43 | { |
44 | CanBusState *bus = (CanBusState *)object; |
45 | |
46 | QTAILQ_INIT(&bus->clients); |
47 | } |
48 | |
49 | int can_bus_insert_client(CanBusState *bus, CanBusClientState *client) |
50 | { |
51 | client->bus = bus; |
52 | QTAILQ_INSERT_TAIL(&bus->clients, client, next); |
53 | return 0; |
54 | } |
55 | |
56 | int can_bus_remove_client(CanBusClientState *client) |
57 | { |
58 | CanBusState *bus = client->bus; |
59 | if (bus == NULL) { |
60 | return 0; |
61 | } |
62 | |
63 | QTAILQ_REMOVE(&bus->clients, client, next); |
64 | client->bus = NULL; |
65 | return 1; |
66 | } |
67 | |
68 | ssize_t can_bus_client_send(CanBusClientState *client, |
69 | const struct qemu_can_frame *frames, size_t frames_cnt) |
70 | { |
71 | int ret = 0; |
72 | CanBusState *bus = client->bus; |
73 | CanBusClientState *peer; |
74 | if (bus == NULL) { |
75 | return -1; |
76 | } |
77 | |
78 | QTAILQ_FOREACH(peer, &bus->clients, next) { |
79 | if (peer->info->can_receive(peer)) { |
80 | if (peer == client) { |
81 | /* No loopback support for now */ |
82 | continue; |
83 | } |
84 | if (peer->info->receive(peer, frames, frames_cnt) > 0) { |
85 | ret = 1; |
86 | } |
87 | } |
88 | } |
89 | |
90 | return ret; |
91 | } |
92 | |
93 | int can_bus_filter_match(struct qemu_can_filter *filter, qemu_canid_t can_id) |
94 | { |
95 | int m; |
96 | if (((can_id | filter->can_mask) & QEMU_CAN_ERR_FLAG)) { |
97 | return (filter->can_mask & QEMU_CAN_ERR_FLAG) != 0; |
98 | } |
99 | m = (can_id & filter->can_mask) == (filter->can_id & filter->can_mask); |
100 | return filter->can_id & QEMU_CAN_INV_FILTER ? !m : m; |
101 | } |
102 | |
103 | int can_bus_client_set_filters(CanBusClientState *client, |
104 | const struct qemu_can_filter *filters, size_t filters_cnt) |
105 | { |
106 | return 0; |
107 | } |
108 | |
109 | |
110 | static bool can_bus_can_be_deleted(UserCreatable *uc) |
111 | { |
112 | return false; |
113 | } |
114 | |
115 | static void can_bus_class_init(ObjectClass *klass, |
116 | void *class_data G_GNUC_UNUSED) |
117 | { |
118 | UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass); |
119 | |
120 | uc_klass->can_be_deleted = can_bus_can_be_deleted; |
121 | } |
122 | |
123 | static const TypeInfo can_bus_info = { |
124 | .parent = TYPE_OBJECT, |
125 | .name = TYPE_CAN_BUS, |
126 | .instance_size = sizeof(CanBusState), |
127 | .instance_init = can_bus_instance_init, |
128 | .class_init = can_bus_class_init, |
129 | .interfaces = (InterfaceInfo[]) { |
130 | { TYPE_USER_CREATABLE }, |
131 | { } |
132 | } |
133 | }; |
134 | |
135 | static void can_bus_register_types(void) |
136 | { |
137 | type_register_static(&can_bus_info); |
138 | } |
139 | |
140 | type_init(can_bus_register_types); |
141 | |