From nobody Mon Feb 9 18:46:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1518524062630972.1006113660134; Tue, 13 Feb 2018 04:14:22 -0800 (PST) Received: from localhost ([::1]:50535 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1elZTZ-0005Zr-9o for importer@patchew.org; Tue, 13 Feb 2018 07:14:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41630) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1elZHG-00041l-W8 for qemu-devel@nongnu.org; Tue, 13 Feb 2018 07:01:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1elZHA-0007WR-UE for qemu-devel@nongnu.org; Tue, 13 Feb 2018 07:01:39 -0500 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]:33441) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1elZHA-0007Vw-J3 for qemu-devel@nongnu.org; Tue, 13 Feb 2018 07:01:32 -0500 Received: by mail-wm0-x22f.google.com with SMTP id x4so12961957wmc.0 for ; Tue, 13 Feb 2018 04:01:32 -0800 (PST) Received: from 640k.lan ([82.84.102.245]) by smtp.gmail.com with ESMTPSA id f9sm6870298wmf.12.2018.02.13.04.01.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Feb 2018 04:01:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=amI+pF/YVqllD6MG0ITbDL+iI3svK0WHXrfLrUT9IWI=; b=Kf5SImw8N3fsi9xEaO4dbCaFyOaeOm4t3/ASgf7vcFZw7xtyKMZIF2OeQG/6WKsFJ9 6Teuf20SZkerzjimEa1pGHJy2efZOoGYppveIoaZrXhN7Sr9HOlpPQA2+PCe4XcaKw7K ohtL11YOkQMRXEiX6p2NxQPeiwIoH83wnVG1+Q9Af1PawIBO5caB01nh5oVtHANbFnhV NBKV1hi/I9N/ow08T+WRf3bqLzNGT3ZBIuE1jRrsWoOtxay8lXByTc7FWFgwZ8Yp4ldr G6Bax481dVFBe2nw2s6dJJNgtg39kC8/ANBYQ51l3jLpEDLXuE/oNBst+TgGU/ZdgBq8 hgow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=amI+pF/YVqllD6MG0ITbDL+iI3svK0WHXrfLrUT9IWI=; b=HoG/pxgz/PkZjOaquR9UFp0KPniy+YrG7uw5aIOzpjQjbflYWv5ZlPQVqgGMD34g8v YLLClwAVRdzu1FQURa42nqoAKn5Bd7J9M8Keltr84/XRTV/4pZz8qsDaMYw2YQ94GSL1 m40QbHArGpZpRTlX/LfrYmTxeAF3KKbOK68uNeuvPhMjx8JEOjkEJ6dIzpeaLFeGd9tz 7ITqZWrkZB7hU04OAa1K6FLxXv+RTqB7O8JP+9Z2MtPXIv0seSGYxK5xamoRci5RaZd8 +ausOY/nLH5aM+GgOe0cBXdYVLXxscSYzX7VVyDwhLkqszTayGoy8GVefRxL2RIYCJjZ i1/g== X-Gm-Message-State: APf1xPC4yb7UvTw0OPcrgNMdqZQMKWXx7Oi1HFIBBaMgVA1SpE8RTVa8 3PvKAJfQfT26IDc6n87WLH+8Smoz X-Google-Smtp-Source: AH8x226pB69P9QfmOYANSg/vmTeIXOXcWRCVt+f/MiflVeT5zRNHJ14rHXAnsucmnqU0FjfrAv8eQw== X-Received: by 10.28.239.3 with SMTP id n3mr1191416wmh.88.1518523290847; Tue, 13 Feb 2018 04:01:30 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 13 Feb 2018 13:00:08 +0100 Message-Id: <1518523252-49106-5-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1518523252-49106-1-git-send-email-pbonzini@redhat.com> References: <1518523252-49106-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::22f Subject: [Qemu-devel] [PULL 04/48] net/can: simple messages transport implementation for QEMU X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pavel Pisa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Pavel Pisa The CanBusState state structure is created for each emulated CAN channel. Individual clients/emulated CAN interfaces or host interface connection registers to the bus by CanBusClientState structure. The CAN core is prepared to support connection to the real host CAN bus network. The commit with such support for Linux SocketCAN follows. Implementation is as simple as possible. There is no state to be migrated, and messages prioritization and queuing are not considered for now. But it is intended to be extended when need arises. Development repository and more documentation at https://gitlab.fel.cvut.cz/canbus/qemu-canbus The work is based on Jin Yang GSoC 2013 work funded by Google and mentored in frame of RTEMS project GSoC slot donated to QEMU. Rewritten for QEMU-2.0+ versions and architecture cleanup by Pavel Pisa (Czech Technical University in Prague). Signed-off-by: Pavel Pisa Signed-off-by: Paolo Bonzini --- default-configs/pci.mak | 1 + hw/Makefile.objs | 1 + include/net/can_emu.h | 123 ++++++++++++++++++++++++++++++++++++++++++ include/net/can_host.h | 55 +++++++++++++++++++ net/Makefile.objs | 2 + net/can/Makefile.objs | 1 + net/can/can_core.c | 138 ++++++++++++++++++++++++++++++++++++++++++++= ++++ net/can/can_host.c | 112 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 433 insertions(+) create mode 100644 include/net/can_emu.h create mode 100644 include/net/can_host.h create mode 100644 net/can/Makefile.objs create mode 100644 net/can/can_core.c create mode 100644 net/can/can_host.c diff --git a/default-configs/pci.mak b/default-configs/pci.mak index 49a0f28..b87ae26 100644 --- a/default-configs/pci.mak +++ b/default-configs/pci.mak @@ -31,6 +31,7 @@ CONFIG_ESP_PCI=3Dy CONFIG_SERIAL=3Dy CONFIG_SERIAL_ISA=3Dy CONFIG_SERIAL_PCI=3Dy +CONFIG_CAN_BUS=3Dy CONFIG_IPACK=3Dy CONFIG_WDT_IB6300ESB=3Dy CONFIG_PCI_TESTDEV=3Dy diff --git a/hw/Makefile.objs b/hw/Makefile.objs index cf4cb20..9d84b8f 100644 --- a/hw/Makefile.objs +++ b/hw/Makefile.objs @@ -6,6 +6,7 @@ devices-dirs-$(CONFIG_SOFTMMU) +=3D block/ devices-dirs-$(CONFIG_SOFTMMU) +=3D bt/ devices-dirs-$(CONFIG_SOFTMMU) +=3D char/ devices-dirs-$(CONFIG_SOFTMMU) +=3D cpu/ +devices-dirs-$(CONFIG_SOFTMMU) +=3D can/ devices-dirs-$(CONFIG_SOFTMMU) +=3D display/ devices-dirs-$(CONFIG_SOFTMMU) +=3D dma/ devices-dirs-$(CONFIG_SOFTMMU) +=3D gpio/ diff --git a/include/net/can_emu.h b/include/net/can_emu.h new file mode 100644 index 0000000..1da4d01 --- /dev/null +++ b/include/net/can_emu.h @@ -0,0 +1,123 @@ +/* + * CAN common CAN bus emulation support + * + * Copyright (c) 2013-2014 Jin Yang + * Copyright (c) 2014-2018 Pavel Pisa + * + * Initial development supported by Google GSoC 2013 from RTEMS project sl= ot + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#ifndef NET_CAN_EMU_H +#define NET_CAN_EMU_H + +#include "qom/object.h" + +/* NOTE: the following two structures is copied from . */ + +/* + * Controller Area Network Identifier structure + * + * bit 0-28 : CAN identifier (11/29 bit) + * bit 29 : error frame flag (0 =3D data frame, 1 =3D error frame) + * bit 30 : remote transmission request flag (1 =3D rtr frame) + * bit 31 : frame format flag (0 =3D standard 11 bit, 1 =3D extended = 29 bit) + */ +typedef uint32_t qemu_canid_t; + +typedef struct qemu_can_frame { + qemu_canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ + uint8_t can_dlc; /* data length code: 0 .. 8 */ + uint8_t data[8] QEMU_ALIGNED(8); +} qemu_can_frame; + +/* Keep defines for QEMU separate from Linux ones for now */ + +#define QEMU_CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */ +#define QEMU_CAN_RTR_FLAG 0x40000000U /* remote transmission request */ +#define QEMU_CAN_ERR_FLAG 0x20000000U /* error message frame */ + +#define QEMU_CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */ +#define QEMU_CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */ + +/** + * struct qemu_can_filter - CAN ID based filter in can_register(). + * @can_id: relevant bits of CAN ID which are not masked out. + * @can_mask: CAN mask (see description) + * + * Description: + * A filter matches, when + * + * & mask =3D=3D can_id & mask + * + * The filter can be inverted (QEMU_CAN_INV_FILTER bit set in can_id) or i= t can + * filter for error message frames (QEMU_CAN_ERR_FLAG bit set in mask). + */ +typedef struct qemu_can_filter { + qemu_canid_t can_id; + qemu_canid_t can_mask; +} qemu_can_filter; + +/* QEMU_CAN_INV_FILTER can be set in qemu_can_filter.can_id */ +#define QEMU_CAN_INV_FILTER 0x20000000U + +typedef struct CanBusClientState CanBusClientState; +typedef struct CanBusState CanBusState; + +typedef struct CanBusClientInfo { + int (*can_receive)(CanBusClientState *); + ssize_t (*receive)(CanBusClientState *, + const struct qemu_can_frame *frames, size_t frames_cnt); +} CanBusClientInfo; + +struct CanBusClientState { + CanBusClientInfo *info; + CanBusState *bus; + int link_down; + QTAILQ_ENTRY(CanBusClientState) next; + CanBusClientState *peer; + char *model; + char *name; + void (*destructor)(CanBusClientState *); +}; + +#define TYPE_CAN_BUS "can-bus" +#define CAN_BUS_CLASS(klass) \ + OBJECT_CLASS_CHECK(CanBusClass, (klass), TYPE_CAN_BUS) +#define CAN_BUS_GET_CLASS(obj) \ + OBJECT_GET_CLASS(CanBusClass, (obj), TYPE_CAN_BUS) +#define CAN_BUS(obj) \ + OBJECT_CHECK(CanBusState, (obj), TYPE_CAN_BUS) + +int can_bus_filter_match(struct qemu_can_filter *filter, qemu_canid_t can_= id); + +int can_bus_insert_client(CanBusState *bus, CanBusClientState *client); + +int can_bus_remove_client(CanBusClientState *client); + +ssize_t can_bus_client_send(CanBusClientState *, + const struct qemu_can_frame *frames, + size_t frames_cnt); + +int can_bus_client_set_filters(CanBusClientState *, + const struct qemu_can_filter *filters, + size_t filters_cnt); + +#endif diff --git a/include/net/can_host.h b/include/net/can_host.h new file mode 100644 index 0000000..d796767 --- /dev/null +++ b/include/net/can_host.h @@ -0,0 +1,55 @@ +/* + * CAN common CAN bus emulation support + * + * Copyright (c) 2013-2014 Jin Yang + * Copyright (c) 2014-2018 Pavel Pisa + * + * Initial development supported by Google GSoC 2013 from RTEMS project sl= ot + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#ifndef NET_CAN_HOST_H +#define NET_CAN_HOST_H + +#include "net/can_emu.h" + +#define TYPE_CAN_HOST "can-host" +#define CAN_HOST_CLASS(klass) \ + OBJECT_CLASS_CHECK(CanHostClass, (klass), TYPE_CAN_HOST) +#define CAN_HOST_GET_CLASS(obj) \ + OBJECT_GET_CLASS(CanHostClass, (obj), TYPE_CAN_HOST) +#define CAN_HOST(obj) \ + OBJECT_CHECK(CanHostState, (obj), TYPE_CAN_HOST) + +typedef struct CanHostState { + ObjectClass oc; + + CanBusState *bus; + CanBusClientState bus_client; +} CanHostState; + +typedef struct CanHostClass { + ObjectClass oc; + + void (*connect)(CanHostState *ch, Error **errp); + void (*disconnect)(CanHostState *ch); +} CanHostClass; + +#endif diff --git a/net/Makefile.objs b/net/Makefile.objs index 64adf32..b2bf88a 100644 --- a/net/Makefile.objs +++ b/net/Makefile.objs @@ -23,3 +23,5 @@ common-obj-$(CONFIG_POSIX) +=3D tap.o $(tap-obj-y) common-obj-$(CONFIG_WIN32) +=3D tap-win32.o =20 vde.o-libs =3D $(VDE_LIBS) + +common-obj-$(CONFIG_CAN_BUS) +=3D can/ diff --git a/net/can/Makefile.objs b/net/can/Makefile.objs new file mode 100644 index 0000000..ef97fdb --- /dev/null +++ b/net/can/Makefile.objs @@ -0,0 +1 @@ +common-obj-y +=3D can_core.o can_host.o diff --git a/net/can/can_core.c b/net/can/can_core.c new file mode 100644 index 0000000..2a83cad --- /dev/null +++ b/net/can/can_core.c @@ -0,0 +1,138 @@ +/* + * CAN common CAN bus emulation support + * + * Copyright (c) 2013-2014 Jin Yang + * Copyright (c) 2014-2018 Pavel Pisa + * + * Initial development supported by Google GSoC 2013 from RTEMS project sl= ot + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ +#include "qemu/osdep.h" +#include "chardev/char.h" +#include "qemu/sockets.h" +#include "qapi/error.h" +#include "net/can_emu.h" +#include "qom/object_interfaces.h" + +struct CanBusState { + Object object; + + QTAILQ_HEAD(, CanBusClientState) clients; +}; + +static void can_bus_instance_init(Object *object) +{ + CanBusState *bus =3D (CanBusState *)object; + + QTAILQ_INIT(&bus->clients); +} + +int can_bus_insert_client(CanBusState *bus, CanBusClientState *client) +{ + client->bus =3D bus; + QTAILQ_INSERT_TAIL(&bus->clients, client, next); + return 0; +} + +int can_bus_remove_client(CanBusClientState *client) +{ + CanBusState *bus =3D client->bus; + if (bus =3D=3D NULL) { + return 0; + } + + QTAILQ_REMOVE(&bus->clients, client, next); + client->bus =3D NULL; + return 1; +} + +ssize_t can_bus_client_send(CanBusClientState *client, + const struct qemu_can_frame *frames, size_t frames_cnt) +{ + int ret =3D 0; + CanBusState *bus =3D client->bus; + CanBusClientState *peer; + if (bus =3D=3D NULL) { + return -1; + } + + QTAILQ_FOREACH(peer, &bus->clients, next) { + if (peer->info->can_receive(peer)) { + if (peer =3D=3D client) { + /* No loopback support for now */ + continue; + } + if (peer->info->receive(peer, frames, frames_cnt) > 0) { + ret =3D 1; + } + } + } + + return ret; +} + +int can_bus_filter_match(struct qemu_can_filter *filter, qemu_canid_t can_= id) +{ + int m; + if (((can_id | filter->can_mask) & QEMU_CAN_ERR_FLAG)) { + return (filter->can_mask & QEMU_CAN_ERR_FLAG) !=3D 0; + } + m =3D (can_id & filter->can_mask) =3D=3D (filter->can_id & filter->can= _mask); + return filter->can_id & QEMU_CAN_INV_FILTER ? !m : m; +} + +int can_bus_client_set_filters(CanBusClientState *client, + const struct qemu_can_filter *filters, size_t filters_cnt) +{ + return 0; +} + + +static bool can_bus_can_be_deleted(UserCreatable *uc) +{ + return false; +} + +static void can_bus_class_init(ObjectClass *klass, + void *class_data G_GNUC_UNUSED) +{ + UserCreatableClass *uc_klass =3D USER_CREATABLE_CLASS(klass); + + uc_klass->can_be_deleted =3D can_bus_can_be_deleted; +} + +static const TypeInfo can_bus_info =3D { + .parent =3D TYPE_OBJECT, + .name =3D TYPE_CAN_BUS, + .instance_size =3D sizeof(CanBusState), + .instance_init =3D can_bus_instance_init, + .class_init =3D can_bus_class_init, + .interfaces =3D (InterfaceInfo[]) { + { TYPE_USER_CREATABLE }, + { } + } +}; + +static void can_bus_register_types(void) +{ + type_register_static(&can_bus_info); +} + +type_init(can_bus_register_types); diff --git a/net/can/can_host.c b/net/can/can_host.c new file mode 100644 index 0000000..c3d2652 --- /dev/null +++ b/net/can/can_host.c @@ -0,0 +1,112 @@ +/* + * CAN generic CAN host connection support + * + * Copyright (c) 2013-2014 Jin Yang + * Copyright (c) 2014-2018 Pavel Pisa + * + * Initial development supported by Google GSoC 2013 from RTEMS project sl= ot + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ +#include "qemu/osdep.h" +#include "chardev/char.h" +#include "qemu/sockets.h" +#include "qapi/error.h" +#include "qom/object_interfaces.h" +#include "net/can_emu.h" +#include "net/can_host.h" + +struct CanBusState { + Object object; + + QTAILQ_HEAD(, CanBusClientState) clients; +}; + +static void can_host_disconnect(CanHostState *ch) +{ + CanHostClass *chc =3D CAN_HOST_GET_CLASS(ch); + + can_bus_remove_client(&ch->bus_client); + chc->disconnect(ch); +} + +static void can_host_connect(CanHostState *ch, Error **errp) +{ + CanHostClass *chc =3D CAN_HOST_GET_CLASS(ch); + Error *local_err =3D NULL; + + chc->connect(ch, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + can_bus_insert_client(ch->bus, &ch->bus_client); +} + +static void can_host_unparent(Object *obj) +{ + can_host_disconnect(CAN_HOST(obj)); +} + +static void can_host_complete(UserCreatable *uc, Error **errp) +{ + can_host_connect(CAN_HOST(uc), errp); +} + +static void can_host_instance_init(Object *obj) +{ + CanHostState *ch =3D CAN_HOST(obj); + + object_property_add_link(obj, "canbus", TYPE_CAN_BUS, + (Object **)&ch->bus, + object_property_allow_set_link, + OBJ_PROP_LINK_UNREF_ON_RELEASE, + &error_abort); +} + +static void can_host_class_init(ObjectClass *klass, + void *class_data G_GNUC_UNUSED) +{ + UserCreatableClass *uc_klass =3D USER_CREATABLE_CLASS(klass); + + klass->unparent =3D can_host_unparent; + uc_klass->complete =3D can_host_complete; +} + +static const TypeInfo can_host_info =3D { + .parent =3D TYPE_OBJECT, + .name =3D TYPE_CAN_HOST, + .instance_size =3D sizeof(CanHostState), + .class_size =3D sizeof(CanHostClass), + .abstract =3D true, + .instance_init =3D can_host_instance_init, + .class_init =3D can_host_class_init, + .interfaces =3D (InterfaceInfo[]) { + { TYPE_USER_CREATABLE }, + { } + } +}; + +static void can_host_register_types(void) +{ + type_register_static(&can_host_info); +} + +type_init(can_host_register_types); --=20 1.8.3.1