VLCB SDK
An opinionated SDK for VLCB protocol
Loading...
Searching...
No Matches
iface.c
Go to the documentation of this file.
1#include "vlcb/net/iface.h"
2
3#include <assert.h>
4#include <stddef.h>
5#include <stdint.h>
6
7#include "iface_can.h"
8#include "iface_vlcb.h"
9#include "vlcb/net/adapter.h"
10#include "vlcb/net/socket.h"
11#include "vlcb/platform/log.h"
12
13VlcbNetIface vlcb_net_iface_New(IVlcbNetAdpt *const adpt,
14 VlcbNetSocketList *const sockets) {
15 assert(adpt != NULL && sockets != NULL);
16
17 VlcbNetIface iface = {.adpt = adpt, .sockets = sockets};
18
19 return iface;
20}
21
22bool IngressPackets(VlcbNetIface *const iface) {
23 bool processed_any = false;
24
25 IVlcbNetAdpt *adpt = iface->adpt;
26 const VlcbNetAdptCaps caps = _INTERFACE_PTR_CALL(adpt, Caps);
27
28 do {
29 VlcbNetAdptPkt *phy_packet;
30 VlcbNetAdptErr dev_err = _INTERFACE_PTR_CALL(adpt, RecvPkt, phy_packet);
31
32 // device has nothing in buffer and would block execution while waiting
33 // for new packets
34 if (dev_err == VLCB_NET_ADPT_ERR_WOULD_BLOCK) {
35 break;
36 }
37
38 if (dev_err != VLCB_NET_ADPT_ERR_OK) {
40 }
41
42 switch (caps.medium) {
43 case VLCB_MEDIUM_CAN:
44 ProcessCanPacket(iface, phy_packet);
45 default:
46 assert(false /* unsupported medium */);
47 }
48
49 processed_any = true;
50 } while (1);
51
52 return processed_any;
53}
54
55bool EgressPackets(VlcbNetIface *const iface) {
56 bool emitted_any = false;
57
58 const VlcbNetAdptCaps caps = _INTERFACE_PTR_CALL(iface->adpt, Caps);
60
61 while (vlcb_net_sock_list_iter_HasNext(&iter)) {
63
64 VlcbNetPacket packet; // TODO: ensure clean packet
65
66 const bool emitted_packet =
67 _INTERFACE_PTR_CALL(sock, DispatchPacket, &packet);
68 // TODO: reject invalid packets -> for example when the packet wasn't filled
69 emitted_any |= emitted_packet;
70 if (emitted_packet) {
71 const VlcbNetAdptErr err = DispatchVlcbPacket(iface, caps, &packet);
73 // todo: handle this somehow
74 }
75
76 if (err != VLCB_NET_ADPT_ERR_OK) {
78 }
79 }
80 }
81
82 return emitted_any;
83}
84
86 assert(iface != NULL && iface->adpt != NULL /* iface, sockets need to be valid pointers and device needs to be initialized */);
87
88 bool readiness_may_have_changed = false;
89
90 do {
91 bool did_something = false;
92
93 did_something = IngressPackets(iface);
94 did_something = EgressPackets(iface);
95
96 if (did_something) {
97 readiness_may_have_changed = true;
98 } else {
99 break;
100 }
101 } while (1);
102
103 return (VlcbNetIfacePollResult){
104 .readiness_may_have_changed = readiness_may_have_changed,
105 };
106}
107
109 VlcbNetSocketHandle sock) {
110 assert(iface != NULL && sock != NULL);
111 int res = vlcb_net_sock_list_Insert(iface->sockets, sock);
112 if (res == -1) {
113 return false;
114 }
115 return true;
116}
vlcb_error vlcb_net_adpt_ErrToStr(VlcbNetAdptErr err)
Definition adapter.c:7
VlcbNetAdptErr
Definition adapter.h:39
@ VLCB_NET_ADPT_ERR_OK
Definition adapter.h:40
@ VLCB_NET_ADPT_ERR_WOULD_BLOCK
Definition adapter.h:52
@ VLCB_MEDIUM_CAN
Definition addr.h:6
bool vlcb_net_iface_BindSock(VlcbNetIface *const iface, VlcbNetSocketHandle sock)
Definition iface.c:108
bool IngressPackets(VlcbNetIface *const iface)
Definition iface.c:22
bool EgressPackets(VlcbNetIface *const iface)
Definition iface.c:55
VlcbNetIfacePollResult vlcb_net_iface_Poll(VlcbNetIface *const iface)
Definition iface.c:85
VlcbNetIface vlcb_net_iface_New(IVlcbNetAdpt *const adpt, VlcbNetSocketList *const sockets)
Definition iface.c:13
void ProcessCanPacket(VlcbNetIface *const iface, VlcbNetAdptPkt *pkt)
Definition iface_can.c:11
VlcbNetAdptErr DispatchVlcbPacket(VlcbNetIface *const iface, const VlcbNetAdptCaps caps, const VlcbNetPacket *const packet)
Definition iface_vlcb.c:27
#define _INTERFACE_PTR_CALL(o, m,...)
Definition interface.h:28
#define VLCBLOG_ERROR(msg)
Definition log.h:31
VlcbNetSocketDispatchErr DispatchPacket(const VlcbNetSocketDatagram *const self, VlcbNetPacket *const packet)
int vlcb_net_sock_list_Insert(VlcbNetSocketList *const list, VlcbNetSocketHandle sock)
Definition socket.c:17
bool vlcb_net_sock_list_iter_HasNext(VlcbNetSocketListIter *const iter)
Definition socket.c:43
VlcbNetSocketHandle vlcb_net_sock_list_iter_Next(VlcbNetSocketListIter *const iter)
Definition socket.c:50
VlcbNetSocketListIter vlcb_net_sock_list_GetIterator(const VlcbNetSocketList *const list)
Definition socket.c:30
IVlcbNetSocket * VlcbNetSocketHandle
Definition socket.h:41
VlcbNetMedium medium
Definition adapter.h:36
IVlcbNetAdpt *const adpt
Definition iface.h:11
VlcbNetSocketList *const sockets
Definition iface.h:12