From 2efe274886197f8cf9ca93b8f32e817102379801 Mon Sep 17 00:00:00 2001 From: Freja Date: Fri, 1 Nov 2024 23:10:21 +0100 Subject: [PATCH] change interface SPI --- modules/module/inc/vlcb/module/ui.h | 17 ++- modules/net/inc/vlcb/net/adapter.h | 87 +++++++-------- .../inc/vlcb/net/adapter/can/packet_parse.h | 7 +- modules/net/inc/vlcb/net/iface.h | 5 +- modules/net/inc/vlcb/net/socket.h | 35 +++--- modules/net/inc/vlcb/net/socket/datagram.h | 30 ++++-- modules/net/src/adapter/can/packet_parse.c | 13 +-- modules/net/src/iface/iface.c | 36 +++---- modules/net/src/iface/iface_can.c | 14 ++- modules/net/src/socket/datagram.c | 100 +++++++++++------- .../platform/inc/vlcb/platform/interface.h | 38 ++++++- 11 files changed, 224 insertions(+), 158 deletions(-) diff --git a/modules/module/inc/vlcb/module/ui.h b/modules/module/inc/vlcb/module/ui.h index b554486..c703d4b 100644 --- a/modules/module/inc/vlcb/module/ui.h +++ b/modules/module/inc/vlcb/module/ui.h @@ -4,12 +4,11 @@ #include "vlcb/platform/interface.h" #include "vlcb/platform/time.h" -_INTERFACE_DECLARE(VlcbModuleUi, - _INTERFACE_METHOD_DECLARE(void, IndicateState, - _INTERFACE_SELF_PTR_MUT, - const VlcbModuleState state) - _INTERFACE_METHOD_DECLARE(void, Poll, - _INTERFACE_SELF_PTR_MUT, - const clock_t now)) - -#define VLCB_MODULE_UI_NULL _INTERFACE_NULL(VlcbModuleUi) +_INTERFACE_DECLARE( + IVlcbModuleUi, + _INTERFACE_METHOD_DECLARE(void, IndicateState, + _INTERFACE_SELF_PTR_MUT(IVlcbModuleUi), + const VlcbModuleState state); + _INTERFACE_METHOD_DECLARE(void, Poll, + _INTERFACE_SELF_PTR_MUT(IVlcbModuleUi), + const clock_t now);); diff --git a/modules/net/inc/vlcb/net/adapter.h b/modules/net/inc/vlcb/net/adapter.h index 4743679..dd29beb 100644 --- a/modules/net/inc/vlcb/net/adapter.h +++ b/modules/net/inc/vlcb/net/adapter.h @@ -3,9 +3,8 @@ #include #include -#include "vlcb/common/can.h" -#include "vlcb/platform/interface.h" #include "vlcb/platform/error.h" +#include "vlcb/platform/interface.h" #define VLCB_NET_ADPT_MAX_PAYLOAD 8 @@ -76,43 +75,47 @@ typedef enum { vlcb_error vlcb_net_adpt_ErrToStr(VlcbNetAdptErr err); _INTERFACE_DECLARE( - VlcbNetAdpt, - - /** - * Receive a VLCB packet using this device - * - * Returns `VLCB_NET_DEV_ERR_WOULD_BLOCK` when there is nothing - * in the reception buffer. - */ - _INTERFACE_METHOD_DECLARE(VlcbNetAdptErr, RecvPkt, _INTERFACE_SELF_PTR_MUT, VlcbNetAdptPkt *const pkt) - - /** - * Transfer a VLCB packet using this device - */ - _INTERFACE_METHOD_DECLARE(VlcbNetAdptErr, SendPkt, _INTERFACE_SELF_PTR_MUT, const VlcbNetAdptPkt *const pkt) - - /** - * Get the device capabilities - */ - _INTERFACE_METHOD_DECLARE(VlcbNetAdptCaps, Caps, _INTERFACE_SELF_PTR) -) - -#define vlcb_net_adpt_impl(T, Name, send_f, receive_f, caps_f) \ - VlcbNetAdpt Name##_Upcast(T *x) { \ - _TYPE_UPCAST_METHOD_PTR_SIG(send_f, VlcbNetAdptErr, T *const, \ - const VlcbNetAdptPkt *const) \ - _TYPE_UPCAST_METHOD_PTR_SIG(receive_f, VlcbNetAdptErr, T *const, \ - VlcbNetAdptPkt *const) \ - _TYPE_UPCAST_METHOD_PTR_SIG(caps_f, VlcbNetAdptCaps, const T *const) \ - _TYPE_UPCAST_VTABLE_DEF( \ - tc, VlcbNetAdptTrait, \ - _TYPE_UPCAST_VTABLE_METHOD_ENTRY(RecvPkt, receive_f, VlcbNetAdptErr, \ - void *const, \ - VlcbNetAdptPkt *const), \ - _TYPE_UPCAST_VTABLE_METHOD_ENTRY(SendPkt, send_f, VlcbNetAdptErr, \ - void *const, \ - const VlcbNetAdptPkt *const), \ - _TYPE_UPCAST_VTABLE_METHOD_ENTRY(Caps, caps_f, VlcbNetAdptCaps, \ - const void *const)) \ - return (VlcbNetAdpt){.tc = &tc, .self = x}; \ - } + IVlcbNetAdpt, + + /** + * Receive a VLCB packet using this device + * + * Returns `VLCB_NET_DEV_ERR_WOULD_BLOCK` when there is nothing + * in the reception buffer. + */ + _INTERFACE_METHOD_DECLARE(VlcbNetAdptErr, RecvPkt, + _INTERFACE_SELF_PTR_MUT(IVlcbNetAdpt), + VlcbNetAdptPkt *const pkt); + + /** + * Transfer a VLCB packet using this device + */ + _INTERFACE_METHOD_DECLARE(VlcbNetAdptErr, SendPkt, + _INTERFACE_SELF_PTR_MUT(IVlcbNetAdpt), + const VlcbNetAdptPkt *const pkt); + + /** + * Get the device capabilities + */ + _INTERFACE_METHOD_DECLARE(VlcbNetAdptCaps, Caps, + _INTERFACE_SELF_PTR(IVlcbNetAdpt));); +; + +// #define vlcb_net_adpt_impl(T, Name, send_f, receive_f, caps_f) \ +// VlcbNetAdpt Name##_Upcast(T *x) { \ +// _TYPE_UPCAST_METHOD_PTR_SIG(send_f, VlcbNetAdptErr, T *const, \ +// const VlcbNetAdptPkt *const) \ +// _TYPE_UPCAST_METHOD_PTR_SIG(receive_f, VlcbNetAdptErr, T *const, \ +// VlcbNetAdptPkt *const) \ +// _TYPE_UPCAST_METHOD_PTR_SIG(caps_f, VlcbNetAdptCaps, const T *const) \ +// _TYPE_UPCAST_VTABLE_DEF( \ +// tc, VlcbNetAdptTrait, \ +// _TYPE_UPCAST_VTABLE_METHOD_ENTRY(RecvPkt, receive_f, VlcbNetAdptErr, \ +// void *const, VlcbNetAdptPkt *const), \ +// _TYPE_UPCAST_VTABLE_METHOD_ENTRY(SendPkt, send_f, VlcbNetAdptErr, \ +// void *const, \ +// const VlcbNetAdptPkt *const), \ +// _TYPE_UPCAST_VTABLE_METHOD_ENTRY(Caps, caps_f, VlcbNetAdptCaps, \ +// const void *const)) \ +// return (VlcbNetAdpt){.tc = &tc, .self = x}; \ +// } diff --git a/modules/net/inc/vlcb/net/adapter/can/packet_parse.h b/modules/net/inc/vlcb/net/adapter/can/packet_parse.h index 5207756..d25ce54 100644 --- a/modules/net/inc/vlcb/net/adapter/can/packet_parse.h +++ b/modules/net/inc/vlcb/net/adapter/can/packet_parse.h @@ -2,8 +2,7 @@ #include -#include "packet.h" -#include "../adapter.h" +#include "../../adapter.h" /** * @brief CAN frame id used in transmission @@ -56,8 +55,8 @@ VlcbNetAdptErr vlcb_net_adpt_ParseRawCanData( * @retval 0 The id is valid * @retval 1 The id is invalid */ -int vlcb_net_adpt_NewCanFrameIdFromPkt( - const VlcbNetAdptPkt *const packet, VlcbCanFrameId *const id); +int vlcb_net_adpt_NewCanFrameIdFromPkt(const VlcbNetAdptPkt *const packet, + VlcbCanFrameId *const id); /** * @brief Check if the packed passed in is correctly constructed diff --git a/modules/net/inc/vlcb/net/iface.h b/modules/net/inc/vlcb/net/iface.h index 5d14fda..1323951 100644 --- a/modules/net/inc/vlcb/net/iface.h +++ b/modules/net/inc/vlcb/net/iface.h @@ -8,11 +8,12 @@ typedef struct VlcbNetIface VlcbNetIface; struct VlcbNetIface { - VlcbNetAdpt *const adpt; + IVlcbNetAdpt *const adpt; VlcbNetSocketList *const sockets; }; -VlcbNetIface vlcb_net_iface_New(VlcbNetAdpt *const adpt, VlcbNetSocketList *const sockets); +VlcbNetIface vlcb_net_iface_New(IVlcbNetAdpt *const adpt, + VlcbNetSocketList *const sockets); typedef struct { bool readiness_may_have_changed; diff --git a/modules/net/inc/vlcb/net/socket.h b/modules/net/inc/vlcb/net/socket.h index 71a804d..b58b663 100644 --- a/modules/net/inc/vlcb/net/socket.h +++ b/modules/net/inc/vlcb/net/socket.h @@ -4,8 +4,8 @@ #include #include "packet/vlcb.h" -#include "vlcb/platform/interface.h" #include "vlcb/net/addr.h" +#include "vlcb/platform/interface.h" typedef enum { VLCB_NET_SOCK_ERR_OK = 0, @@ -20,14 +20,21 @@ typedef enum { vlcb_error vlcb_net_sock_ErrToStr(VlcbNetSocketErr err); -_INTERFACE_DECLARE(VlcbNetSocket, - _INTERFACE_METHOD_DECLARE(bool, SupportsProtocol, VlcbProtocol protocol) - _INTERFACE_METHOD_DECLARE(VlcbNetSocketErr, ProcessPacket, _INTERFACE_SELF_PTR_MUT, const VlcbPacket *const packet) - _INTERFACE_METHOD_DECLARE(bool, DispatchPacket, _INTERFACE_SELF_PTR_MUT, VlcbPacket *const packet) - _INTERFACE_METHOD_DECLARE(int, Bind, _INTERFACE_SELF_PTR_MUT, const VlcbNetHwAddr *const addr) -) +_INTERFACE_DECLARE( + IVlcbNetSocket, + _INTERFACE_METHOD_DECLARE(bool, SupportsProtocol, VlcbProtocol protocol); + _INTERFACE_METHOD_DECLARE(VlcbNetSocketErr, ProcessPacket, + _INTERFACE_SELF_PTR_MUT(IVlcbNetSocket), + const VlcbPacket *const packet); + _INTERFACE_METHOD_DECLARE(bool, DispatchPacket, + _INTERFACE_SELF_PTR_MUT(IVlcbNetSocket), + VlcbPacket *const packet); + _INTERFACE_METHOD_DECLARE(int, Bind, + _INTERFACE_SELF_PTR_MUT(IVlcbNetSocket), + const VlcbNetHwAddr *const addr);); +; -typedef VlcbNetSocket *VlcbNetSocketHandle; +typedef IVlcbNetSocket *VlcbNetSocketHandle; typedef struct { const size_t size; VlcbNetSocketHandle *const ptr; @@ -37,8 +44,8 @@ typedef struct { VlcbNetSocketList vlcb_net_sock_list_New(VlcbNetSocketHandle *const list, size_t size); -#define VLCB_NET_SOCK_LIST(name, size) \ - VlcbNetSocketHandle data_##name[size]; \ +#define VLCB_NET_SOCK_LIST(name, size) \ + VlcbNetSocketHandle data_##name[size]; \ VlcbNetSocketList name = vlcb_net_sock_list_New(data_##name, size); void vlcb_net_sock_list_Insert(VlcbNetSocketList *const list, @@ -49,10 +56,10 @@ typedef struct { const VlcbNetSocketList *const list; } VlcbNetSocketListIter; -VlcbNetSocketListIter vlcb_net_sock_list_GetIterator( - const VlcbNetSocketList *const list); +VlcbNetSocketListIter +vlcb_net_sock_list_GetIterator(const VlcbNetSocketList *const list); void vlcb_net_sock_list_iter_Reset(VlcbNetSocketListIter *const iter); bool vlcb_net_sock_list_iter_HasNext(VlcbNetSocketListIter *const iter); -VlcbNetSocketHandle vlcb_net_sock_list_iter_Next( - VlcbNetSocketListIter *const iter); +VlcbNetSocketHandle +vlcb_net_sock_list_iter_Next(VlcbNetSocketListIter *const iter); diff --git a/modules/net/inc/vlcb/net/socket/datagram.h b/modules/net/inc/vlcb/net/socket/datagram.h index 805d150..82cad42 100644 --- a/modules/net/inc/vlcb/net/socket/datagram.h +++ b/modules/net/inc/vlcb/net/socket/datagram.h @@ -1,25 +1,35 @@ #pragma once +#include "../addr.h" #include "../packet/datagram.h" -#include "../storage/packet_buf.h" #include "../socket.h" -#include "../addr.h" +#include "../storage/packet_buf.h" +#include "vlcb/platform/interface.h" -#define VLCB_SOCK_DGRAM_BUF(name, size) \ - unsigned char data_##name[sizeof(VlcbPacketDatagram) * size]; \ - VlcbPacketBuf name = \ +#define VLCB_SOCK_DGRAM_BUF(name, size) \ + unsigned char data_##name[sizeof(VlcbPacketDatagram) * size]; \ + VlcbPacketBuf name = \ vlcb_net_packetbuf_New(data_##name, size, sizeof(VlcbPacketDatagram)); typedef struct { + _INTERFACE_IMPLEMENT(IVlcbNetSocket); VlcbPacketBuf *const rxBuf; VlcbPacketBuf *const txBuf; const VlcbNetHwAddr *addr; } VlcbNetSocketDatagram; -VlcbNetSocket vlcb_net_sock_dgram_Upcast(VlcbNetSocketDatagram *const sock); +// VlcbNetSocket vlcb_net_sock_dgram_Upcast(VlcbNetSocketDatagram *const sock); + +VlcbNetSocketDatagram vlcb_net_sock_dgram_New(VlcbPacketBuf *const rxBuf, + VlcbPacketBuf *const txBuf); -VlcbNetSocketDatagram vlcb_net_sock_dgram_New( - VlcbPacketBuf *const rxBuf, VlcbPacketBuf *const txBuf); +VlcbNetSocketErr +vlcb_net_sock_dgram_Send(VlcbNetSocketDatagram *const sock, + const VlcbPacketDatagram *const packet); +VlcbNetSocketErr vlcb_net_sock_dgram_Recv(VlcbNetSocketDatagram *const sock, + VlcbPacketDatagram *const packet); -VlcbNetSocketErr vlcb_net_sock_dgram_Send(VlcbNetSocketDatagram *const sock, const VlcbPacketDatagram *const packet); -VlcbNetSocketErr vlcb_net_sock_dgram_Recv(VlcbNetSocketDatagram *const sock, VlcbPacketDatagram *const packet); \ No newline at end of file +static inline IVlcbNetSocket *const +vlcb_net_sock_dgram_Upcast(VlcbNetSocketDatagram *const sock) { + return (IVlcbNetSocket *)sock; +} diff --git a/modules/net/src/adapter/can/packet_parse.c b/modules/net/src/adapter/can/packet_parse.c index 530f1cd..1895b9c 100644 --- a/modules/net/src/adapter/can/packet_parse.c +++ b/modules/net/src/adapter/can/packet_parse.c @@ -1,10 +1,10 @@ -#include "../../../../net/adapter/can/packet_parse.h" +#include "vlcb/net/adapter/can/packet_parse.h" #include #include #include -#include "../../../../../shared/log.h" +#include "vlcb/platform/log.h" inline VlcbNetAdptErr vlcb_net_adpt_ParseRawCanData( const VlcbCanFrameId id, const bool is_rtr, const uint8_t payload_len, @@ -30,7 +30,7 @@ inline VlcbNetAdptErr vlcb_net_adpt_ParseRawCanData( if (valid == false) { return VLCB_NET_ADPT_ERR_INVALID_CANID; } - packet->src_addr = vlcb_net_adpt_NewCanIdHwAddr(can_id); + packet->src_addr = vlcb_net_NewCanIdHwAddr(can_id); } VlcbCanPriority prio; @@ -48,8 +48,9 @@ inline VlcbNetAdptErr vlcb_net_adpt_ParseRawCanData( return VLCB_NET_ADPT_ERR_OK; } -inline int vlcb_net_adpt_NewCanFrameIdFromPkt( - const VlcbNetAdptPkt *const packet, VlcbCanFrameId *const id) { +inline int +vlcb_net_adpt_NewCanFrameIdFromPkt(const VlcbNetAdptPkt *const packet, + VlcbCanFrameId *const id) { assert(packet != NULL && id != NULL); if (vlcb_defs_IsCanIdValid(packet->src_addr.can_id)) { return 1; @@ -57,7 +58,7 @@ inline int vlcb_net_adpt_NewCanFrameIdFromPkt( *id = packet->src_addr.can_id; - VlcbCanPriority prio = VLCB_CAN_PRIO_DEFAULT; // init with default priority + VlcbCanPriority prio = VLCB_CAN_PRIO_DEFAULT; // init with default priority if (packet->payload > 0) { prio = vlcb_defs_CanPriorityFromOpcode(packet->payload[0]); } diff --git a/modules/net/src/iface/iface.c b/modules/net/src/iface/iface.c index e655dfd..6adfe52 100644 --- a/modules/net/src/iface/iface.c +++ b/modules/net/src/iface/iface.c @@ -4,18 +4,16 @@ #include #include -#include "vlcb/platform/log.h" -#include "vlcb/net/adapter.h" #include "iface_can.h" #include "iface_vlcb.h" +#include "vlcb/net/adapter.h" +#include "vlcb/platform/log.h" -VlcbNetIface vlcb_net_iface_New(VlcbNetAdpt *const adpt, VlcbNetSocketList *const sockets) { +VlcbNetIface vlcb_net_iface_New(IVlcbNetAdpt *const adpt, + VlcbNetSocketList *const sockets) { assert(adpt != NULL && sockets != NULL); - VlcbNetIface iface = { - .adpt = adpt, - .sockets = sockets - }; + VlcbNetIface iface = {.adpt = adpt, .sockets = sockets}; return iface; } @@ -23,7 +21,7 @@ VlcbNetIface vlcb_net_iface_New(VlcbNetAdpt *const adpt, VlcbNetSocketList *cons bool IngressPackets(VlcbNetIface *const iface) { bool processed_any = false; - const VlcbNetAdpt *adpt = iface->adpt; + IVlcbNetAdpt *adpt = iface->adpt; const VlcbNetAdptCaps caps = _INTERFACE_PTR_CALL(adpt, Caps); do { @@ -41,10 +39,10 @@ bool IngressPackets(VlcbNetIface *const iface) { } switch (caps.medium) { - case VLCB_MEDIUM_CAN: - ProcessCanPacket(iface, phy_packet); - default: - assert(false /* unsupported medium */); + case VLCB_MEDIUM_CAN: + ProcessCanPacket(iface, phy_packet); + default: + assert(false /* unsupported medium */); } processed_any = true; @@ -62,16 +60,17 @@ bool EgressPackets(VlcbNetIface *const iface) { while (vlcb_net_sock_list_iter_HasNext(&iter)) { const VlcbNetSocketHandle sock = vlcb_net_sock_list_iter_Next(&iter); - VlcbPacket packet; // TODO: ensure clean packet + VlcbPacket packet; // TODO: ensure clean packet - const bool emitted_packet = _INTERFACE_PTR_CALL(sock, DispatchPacket, &packet); + const bool emitted_packet = + _INTERFACE_PTR_CALL(sock, DispatchPacket, &packet); // TODO: reject invalid packets -> for example when the packet wasn't filled emitted_any |= emitted_packet; if (emitted_packet) { const VlcbNetAdptErr err = DispatchVlcbPacket(iface, caps, &packet); - if (err == VLCB_NET_ADPT_ERR_WOULD_BLOCK) { - //todo: handle this somehow - } + if (err == VLCB_NET_ADPT_ERR_WOULD_BLOCK) { + // todo: handle this somehow + } if (err != VLCB_NET_ADPT_ERR_OK) { VLCBLOG_ERROR(vlcb_net_adpt_ErrToStr(err)); @@ -82,8 +81,7 @@ bool EgressPackets(VlcbNetIface *const iface) { return emitted_any; } -VlcbNetIfacePollResult vlcb_net_iface_Poll( - VlcbNetIface *const iface) { +VlcbNetIfacePollResult vlcb_net_iface_Poll(VlcbNetIface *const iface) { assert(iface != NULL && iface->adpt != NULL /* iface, sockets need to be valid pointers and device needs to be initialized */); bool readiness_may_have_changed = false; diff --git a/modules/net/src/iface/iface_can.c b/modules/net/src/iface/iface_can.c index f10dc46..49328c1 100644 --- a/modules/net/src/iface/iface_can.c +++ b/modules/net/src/iface/iface_can.c @@ -2,16 +2,14 @@ #include #include -#include "vlcb/common/opcode.h" +#include "iface_vlcb.h" #include "vlcb/net/adapter.h" #include "vlcb/net/iface.h" #include "vlcb/net/packet/vlcb.h" #include "vlcb/platform/log.h" -#include "iface_vlcb.h" -void ProcessCanPacket(VlcbNetIface* const iface, - VlcbNetAdptPkt* pkt) { - VlcbNetAdptPayload* payload = &pkt->payload; +void ProcessCanPacket(VlcbNetIface *const iface, VlcbNetAdptPkt *pkt) { + VlcbNetAdptPayload *payload = &pkt->payload; VlcbOpCode opc; { int err = vlcb_defs_OpcodeFromByte(*payload[0], &opc); @@ -24,7 +22,7 @@ void ProcessCanPacket(VlcbNetIface* const iface, VlcbPacket vlcb_pkt; { VlcbPacketConstructErr err; - err = vlcb_net_pkt_New(opc, pkt->payload_len, (VlcbPayload*)(payload + 1), + err = vlcb_net_pkt_New(opc, pkt->payload_len, (VlcbPayload *)(payload + 1), &vlcb_pkt); if (err != VLCB_PKT_CONSTRUCT_ERR_OK) { VLCBLOG_ERROR(vlcb_net_pkt_ConstructErrToStr(err)); @@ -35,8 +33,8 @@ void ProcessCanPacket(VlcbNetIface* const iface, ProcessVlcbPacket(iface, &vlcb_pkt); } -VlcbNetAdptErr DispatchCanPacket(VlcbNetIface* const iface, - const VlcbPacket* const packet) { +VlcbNetAdptErr DispatchCanPacket(VlcbNetIface *const iface, + const VlcbPacket *const packet) { VlcbNetAdptPkt adpt_pkt; adpt_pkt.medium = VLCB_MEDIUM_CAN; diff --git a/modules/net/src/socket/datagram.c b/modules/net/src/socket/datagram.c index 7503167..efa51c3 100644 --- a/modules/net/src/socket/datagram.c +++ b/modules/net/src/socket/datagram.c @@ -2,7 +2,11 @@ #include #include +#include +#include "vlcb/net/addr.h" +#include "vlcb/net/packet/vlcb.h" +#include "vlcb/net/socket.h" #include "vlcb/platform/interface.h" bool SupportsProtocol(VlcbProtocol proto) { @@ -39,8 +43,8 @@ bool DispatchPacket(const VlcbNetSocketDatagram *const self, } vlcb_net_pkt_NewUnchecked(VLCB_PROTO_DATAGRAM, datagram_packet.opc, - datagram_packet.payload_len, - &datagram_packet.payload, packet); + datagram_packet.payload_len, + &datagram_packet.payload, packet); return true; } @@ -54,45 +58,63 @@ int Bind(VlcbNetSocketDatagram *const sock, const VlcbNetHwAddr *const addr) { return 1; } -VlcbNetSocket vlcb_net_sock_dgram_Upcast(VlcbNetSocketDatagram *sock) { - _TYPE_UPCAST_METHOD_PTR_SIG(SupportsProtocol, bool, VlcbProtocol); - _TYPE_UPCAST_METHOD_PTR_SIG(ProcessPacket, VlcbNetSocketErr, - VlcbNetSocketDatagram *const, - const VlcbPacket *const); - _TYPE_UPCAST_METHOD_PTR_SIG(DispatchPacket, bool, - VlcbNetSocketDatagram *const, VlcbPacket *const); - _TYPE_UPCAST_METHOD_PTR_SIG(Bind, bool, - VlcbNetSocketDatagram *const, const VlcbNetHwAddr *const); - _TYPE_UPCAST_VTABLE_DEF( - tc, VlcbNetSocketTrait, - _TYPE_UPCAST_VTABLE_METHOD_ENTRY(SupportsProtocol, SupportsProtocol, bool, - VlcbProtocol), - _TYPE_UPCAST_VTABLE_METHOD_ENTRY(ProcessPacket, ProcessPacket, - VlcbNetSocketErr, void *const, - const VlcbPacket *const), - _TYPE_UPCAST_VTABLE_METHOD_ENTRY(DispatchPacket, DispatchPacket, bool, - void *const, VlcbPacket *const) - , - _TYPE_UPCAST_VTABLE_METHOD_ENTRY(Bind, Bind, int, - void *const, const VlcbNetHwAddr *const) - ); - return (VlcbNetSocket){.tc = &tc, .self = sock}; +_INTERFACE_VTABLE_DEFINE( + IVlcbNetSocket, + _INTERFACE_VTABLE_METHOD(SupportsProtocol, SupportsProtocol, bool, + VlcbProtocol), + _INTERFACE_VTABLE_METHOD(ProcessPacket, ProcessPacket, VlcbNetSocketErr, + _INTERFACE_SELF_PTR_MUT(IVlcbNetSocket), + const VlcbPacket *const), + _INTERFACE_VTABLE_METHOD(DispatchPacket, DispatchPacket, bool, + _INTERFACE_SELF_PTR_MUT(IVlcbNetSocket), + VlcbPacket *const), + _INTERFACE_VTABLE_METHOD(Bind, Bind, int, + _INTERFACE_SELF_PTR_MUT(IVlcbNetSocket), + const VlcbNetHwAddr *const)); + +// VlcbNetSocket vlcb_net_sock_dgram_Upcast(VlcbNetSocketDatagram *sock) { +// _TYPE_UPCAST_METHOD_PTR_SIG(SupportsProtocol, bool, VlcbProtocol); +// _TYPE_UPCAST_METHOD_PTR_SIG(ProcessPacket, VlcbNetSocketErr, +// VlcbNetSocketDatagram *const, +// const VlcbPacket *const); +// _TYPE_UPCAST_METHOD_PTR_SIG(DispatchPacket, bool, +// VlcbNetSocketDatagram *const, VlcbPacket +// *const); +// _TYPE_UPCAST_METHOD_PTR_SIG(Bind, bool, +// VlcbNetSocketDatagram *const, const +// VlcbNetHwAddr *const); +// _TYPE_UPCAST_VTABLE_DEF( +// tc, VlcbNetSocketTrait, +// _TYPE_UPCAST_VTABLE_METHOD_ENTRY(SupportsProtocol, SupportsProtocol, +// bool, +// VlcbProtocol), +// _TYPE_UPCAST_VTABLE_METHOD_ENTRY(ProcessPacket, ProcessPacket, +// VlcbNetSocketErr, void *const, +// const VlcbPacket *const), +// _TYPE_UPCAST_VTABLE_METHOD_ENTRY(DispatchPacket, DispatchPacket, bool, +// void *const, VlcbPacket *const) +// , +// _TYPE_UPCAST_VTABLE_METHOD_ENTRY(Bind, Bind, int, +// void *const, const VlcbNetHwAddr +// *const) +// ); +// return (VlcbNetSocket){.tc = &tc, .self = sock}; +// } + +VlcbNetSocketDatagram vlcb_net_sock_dgram_New(VlcbPacketBuf *const rxBuf, + VlcbPacketBuf *const txBuf) { + assert(rxBuf != NULL && txBuf != NULL); + return (VlcbNetSocketDatagram){_INTERFACE_ASSIGN_VTABLE(IVlcbNetSocket), + .rxBuf = rxBuf, .txBuf = txBuf, .addr = NULL}; } -VlcbNetSocketDatagram vlcb_net_sock_dgram_New( - VlcbPacketBuf *const rxBuf, VlcbPacketBuf *const txBuf) { - assert(rxBuf !=NULL && txBuf!=NULL); - return (VlcbNetSocketDatagram){.rxBuf = rxBuf, .txBuf = txBuf, .addr = NULL}; -} - -VlcbNetSocketErr vlcb_net_sock_dgram_Send(VlcbNetSocketDatagram *const sock, const VlcbPacketDatagram *const packet) { - assert(sock != NULL); - - +VlcbNetSocketErr +vlcb_net_sock_dgram_Send(VlcbNetSocketDatagram *const sock, + const VlcbPacketDatagram *const packet) { + assert(sock != NULL && packet != NULL); } -VlcbNetSocketErr vlcb_net_sock_dgram_Recv(VlcbNetSocketDatagram *const sock, VlcbPacketDatagram *const packet) { +VlcbNetSocketErr vlcb_net_sock_dgram_Recv(VlcbNetSocketDatagram *const sock, + VlcbPacketDatagram *const packet) { assert(sock != NULL && packet != NULL); - - -} \ No newline at end of file +} diff --git a/modules/platform/inc/vlcb/platform/interface.h b/modules/platform/inc/vlcb/platform/interface.h index df40265..a502551 100644 --- a/modules/platform/inc/vlcb/platform/interface.h +++ b/modules/platform/inc/vlcb/platform/interface.h @@ -3,15 +3,15 @@ #include // #define _INTERFACE_TRAIT_TYPEDEF_START(iT) typedef struct iT { -// #define _INTERFACE_TRAIT_METHOD(retT, name, ...) retT (*const name)(__VA_ARGS__); -// #define _INTERFACE_TRAIT_TYPEDEF_END(T) } T; +// #define _INTERFACE_TRAIT_METHOD(retT, name, ...) retT (*const +// name)(__VA_ARGS__); #define _INTERFACE_TRAIT_TYPEDEF_END(T) } T; // // #define _INTERFACE_TYPEDEF(T, trait) \ // typedef struct { \ // void *const self; \ // const trait *const tc; \ // } T; - +/* #define _INTERFACE_DECLARE(T, ...) \ typedef struct T##Trait { \ __VA_ARGS__ \ @@ -20,7 +20,8 @@ void *const self; \ const T##Trait *const tc; \ } T; -#define _INTERFACE_METHOD_DECLARE(retT, name, ...) retT (*const name)(__VA_ARGS__); +#define _INTERFACE_METHOD_DECLARE(retT, name, ...) retT (*const +name)(__VA_ARGS__); #define _INTERFACE_SELF_PTR const void *const self #define _INTERFACE_SELF_PTR_MUT void *const self @@ -43,4 +44,31 @@ #define _INTERFACE_PTR_STATIC_CALL(o, m, ...) o->tc->m(__VA_ARGS__) #define _INTERFACE_STATIC_CALL(o, m, ...) o.tc->m(__VA_ARGS__) #define _INTERFACE_PTR_CALL(o, m, ...) o->tc->m(o->self, ##__VA_ARGS__) -#define _INTERFACE_CALL(o, m, ...) o.tc->m(o.self, ##__VA_ARGS__) \ No newline at end of file +#define _INTERFACE_CALL(o, m, ...) o.tc->m(o.self, ##__VA_ARGS__) +*/ + +#define _INTERFACE_DECLARE(T, ...) \ + typedef struct T { \ + __VA_ARGS__ \ + } T + +#define _INTERFACE_METHOD_DECLARE(retT, name, ...) \ + retT (*const name)(__VA_ARGS__) + +#define _INTERFACE_SELF(T) const struct T self +#define _INTERFACE_SELF_MUT(T) struct T +#define _INTERFACE_SELF_PTR(T) const struct T *const self +#define _INTERFACE_SELF_PTR_MUT(T) struct T *const self + +#define _INTERFACE_IMPLEMENT(T) T _iface + +#define _INTERFACE_VTABLE_DEFINE(T, ...) static T iface##T = {__VA_ARGS__} +#define _INTERFACE_VTABLE_METHOD(m, ptr, retT, ...) \ + .m = (retT(*const)(__VA_ARGS__))(ptr) + +#define _INTERFACE_ASSIGN_VTABLE(T) ._iface = iface##T + +#define _INTERFACE_PTR_STATIC_CALL(o, m, ...) o->m(__VA_ARGS__) +#define _INTERFACE_STATIC_CALL(o, m, ...) o.m(__VA_ARGS__) +#define _INTERFACE_PTR_CALL(o, m, ...) o->m(o, ##__VA_ARGS__) +#define _INTERFACE_CALL(o, m, ...) o.m(&o, ##__VA_ARGS__)