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 #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" 33 34 struct CanBusState { 35 Object object; 36 37 QTAILQ_HEAD(, CanBusClientState) clients; 38 }; 39 40 static void can_bus_instance_init(Object *object) 41 { 42 CanBusState *bus = (CanBusState *)object; 43 44 QTAILQ_INIT(&bus->clients); 45 } 46 47 int can_bus_insert_client(CanBusState *bus, CanBusClientState *client) 48 { 49 client->bus = bus; 50 QTAILQ_INSERT_TAIL(&bus->clients, client, next); 51 return 0; 52 } 53 54 int can_bus_remove_client(CanBusClientState *client) 55 { 56 CanBusState *bus = client->bus; 57 if (bus == NULL) { 58 return 0; 59 } 60 61 QTAILQ_REMOVE(&bus->clients, client, next); 62 client->bus = NULL; 63 return 1; 64 } 65 66 ssize_t can_bus_client_send(CanBusClientState *client, 67 const struct qemu_can_frame *frames, size_t frames_cnt) 68 { 69 int ret = 0; 70 CanBusState *bus = client->bus; 71 CanBusClientState *peer; 72 if (bus == NULL) { 73 return -1; 74 } 75 76 QTAILQ_FOREACH(peer, &bus->clients, next) { 77 if (peer->info->can_receive(peer)) { 78 if (peer == client) { 79 /* No loopback support for now */ 80 continue; 81 } 82 if (peer->info->receive(peer, frames, frames_cnt) > 0) { 83 ret = 1; 84 } 85 } 86 } 87 88 return ret; 89 } 90 91 int can_bus_filter_match(struct qemu_can_filter *filter, qemu_canid_t can_id) 92 { 93 int m; 94 if (((can_id | filter->can_mask) & QEMU_CAN_ERR_FLAG)) { 95 return (filter->can_mask & QEMU_CAN_ERR_FLAG) != 0; 96 } 97 m = (can_id & filter->can_mask) == (filter->can_id & filter->can_mask); 98 return filter->can_id & QEMU_CAN_INV_FILTER ? !m : m; 99 } 100 101 int can_bus_client_set_filters(CanBusClientState *client, 102 const struct qemu_can_filter *filters, size_t filters_cnt) 103 { 104 return 0; 105 } 106 107 108 static bool can_bus_can_be_deleted(UserCreatable *uc) 109 { 110 return false; 111 } 112 113 static void can_bus_class_init(ObjectClass *klass, 114 void *class_data G_GNUC_UNUSED) 115 { 116 UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass); 117 118 uc_klass->can_be_deleted = can_bus_can_be_deleted; 119 } 120 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 }, 129 { } 130 } 131 }; 132 133 static void can_bus_register_types(void) 134 { 135 type_register_static(&can_bus_info); 136 } 137 138 type_init(can_bus_register_types); 139