2 * CAN common CAN bus emulation support
4 * Copyright (c) 2013-2014 Jin Yang
5 * Copyright (c) 2014-2018 Pavel Pisa
7 * Initial development supported by Google GSoC 2013 from RTEMS project slot
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
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"
39 QTAILQ_HEAD(, CanBusClientState
) clients
;
42 static void can_bus_instance_init(Object
*object
)
44 CanBusState
*bus
= (CanBusState
*)object
;
46 QTAILQ_INIT(&bus
->clients
);
49 int can_bus_insert_client(CanBusState
*bus
, CanBusClientState
*client
)
52 QTAILQ_INSERT_TAIL(&bus
->clients
, client
, next
);
56 int can_bus_remove_client(CanBusClientState
*client
)
58 CanBusState
*bus
= client
->bus
;
63 QTAILQ_REMOVE(&bus
->clients
, client
, next
);
68 ssize_t
can_bus_client_send(CanBusClientState
*client
,
69 const struct qemu_can_frame
*frames
, size_t frames_cnt
)
72 CanBusState
*bus
= client
->bus
;
73 CanBusClientState
*peer
;
78 QTAILQ_FOREACH(peer
, &bus
->clients
, next
) {
79 if (peer
->info
->can_receive(peer
)) {
81 /* No loopback support for now */
84 if (peer
->info
->receive(peer
, frames
, frames_cnt
) > 0) {
93 int can_bus_filter_match(struct qemu_can_filter
*filter
, qemu_canid_t can_id
)
96 if (((can_id
| filter
->can_mask
) & QEMU_CAN_ERR_FLAG
)) {
97 return (filter
->can_mask
& QEMU_CAN_ERR_FLAG
) != 0;
99 m
= (can_id
& filter
->can_mask
) == (filter
->can_id
& filter
->can_mask
);
100 return filter
->can_id
& QEMU_CAN_INV_FILTER
? !m
: m
;
103 int can_bus_client_set_filters(CanBusClientState
*client
,
104 const struct qemu_can_filter
*filters
, size_t filters_cnt
)
110 static bool can_bus_can_be_deleted(UserCreatable
*uc
)
115 static void can_bus_class_init(ObjectClass
*klass
,
116 void *class_data G_GNUC_UNUSED
)
118 UserCreatableClass
*uc_klass
= USER_CREATABLE_CLASS(klass
);
120 uc_klass
->can_be_deleted
= can_bus_can_be_deleted
;
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
},
135 static void can_bus_register_types(void)
137 type_register_static(&can_bus_info
);
140 type_init(can_bus_register_types
);