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
27 #include "qemu/osdep.h"
28 #include "chardev/char.h"
29 #include "qemu/sockets.h"
30 #include "qapi/error.h"
31 #include "net/can_emu.h"
32 #include "qom/object_interfaces.h"
37 QTAILQ_HEAD(, CanBusClientState
) clients
;
40 static void can_bus_instance_init(Object
*object
)
42 CanBusState
*bus
= (CanBusState
*)object
;
44 QTAILQ_INIT(&bus
->clients
);
47 int can_bus_insert_client(CanBusState
*bus
, CanBusClientState
*client
)
50 QTAILQ_INSERT_TAIL(&bus
->clients
, client
, next
);
54 int can_bus_remove_client(CanBusClientState
*client
)
56 CanBusState
*bus
= client
->bus
;
61 QTAILQ_REMOVE(&bus
->clients
, client
, next
);
66 ssize_t
can_bus_client_send(CanBusClientState
*client
,
67 const struct qemu_can_frame
*frames
, size_t frames_cnt
)
70 CanBusState
*bus
= client
->bus
;
71 CanBusClientState
*peer
;
76 QTAILQ_FOREACH(peer
, &bus
->clients
, next
) {
77 if (peer
->info
->can_receive(peer
)) {
79 /* No loopback support for now */
82 if (peer
->info
->receive(peer
, frames
, frames_cnt
) > 0) {
91 int can_bus_filter_match(struct qemu_can_filter
*filter
, qemu_canid_t can_id
)
94 if (((can_id
| filter
->can_mask
) & QEMU_CAN_ERR_FLAG
)) {
95 return (filter
->can_mask
& QEMU_CAN_ERR_FLAG
) != 0;
97 m
= (can_id
& filter
->can_mask
) == (filter
->can_id
& filter
->can_mask
);
98 return filter
->can_id
& QEMU_CAN_INV_FILTER
? !m
: m
;
101 int can_bus_client_set_filters(CanBusClientState
*client
,
102 const struct qemu_can_filter
*filters
, size_t filters_cnt
)
108 static bool can_bus_can_be_deleted(UserCreatable
*uc
)
113 static void can_bus_class_init(ObjectClass
*klass
,
114 void *class_data G_GNUC_UNUSED
)
116 UserCreatableClass
*uc_klass
= USER_CREATABLE_CLASS(klass
);
118 uc_klass
->can_be_deleted
= can_bus_can_be_deleted
;
121 static const TypeInfo can_bus_info
= {
122 .parent
= TYPE_OBJECT
,
123 .name
= TYPE_CAN_BUS
,
124 .instance_size
= sizeof(CanBusState
),
125 .instance_init
= can_bus_instance_init
,
126 .class_init
= can_bus_class_init
,
127 .interfaces
= (InterfaceInfo
[]) {
128 { TYPE_USER_CREATABLE
},
133 static void can_bus_register_types(void)
135 type_register_static(&can_bus_info
);
138 type_init(can_bus_register_types
);