Skip to content

Commit

Permalink
change interface SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfrej committed Nov 1, 2024
1 parent 22f7870 commit 2efe274
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 158 deletions.
17 changes: 8 additions & 9 deletions modules/module/inc/vlcb/module/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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););
87 changes: 45 additions & 42 deletions modules/net/inc/vlcb/net/adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#include <inttypes.h>
#include <stdbool.h>

#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

Expand Down Expand Up @@ -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}; \
// }
7 changes: 3 additions & 4 deletions modules/net/inc/vlcb/net/adapter/can/packet_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#include <stdint.h>

#include "packet.h"
#include "../adapter.h"
#include "../../adapter.h"

/**
* @brief CAN frame id used in transmission
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions modules/net/inc/vlcb/net/iface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 21 additions & 14 deletions modules/net/inc/vlcb/net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <stddef.h>

#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,
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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);
30 changes: 20 additions & 10 deletions modules/net/inc/vlcb/net/socket/datagram.h
Original file line number Diff line number Diff line change
@@ -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);
static inline IVlcbNetSocket *const
vlcb_net_sock_dgram_Upcast(VlcbNetSocketDatagram *const sock) {
return (IVlcbNetSocket *)sock;
}
13 changes: 7 additions & 6 deletions modules/net/src/adapter/can/packet_parse.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "../../../../net/adapter/can/packet_parse.h"
#include "vlcb/net/adapter/can/packet_parse.h"

#include <assert.h>
#include <stdbool.h>
#include <string.h>

#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,
Expand All @@ -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;
Expand All @@ -48,16 +48,17 @@ 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;
}

*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]);
}
Expand Down
36 changes: 17 additions & 19 deletions modules/net/src/iface/iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@
#include <stddef.h>
#include <stdint.h>

#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;
}

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 {
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 2efe274

Please sign in to comment.