VLCB SDK
An opinionated SDK for VLCB protocol
Loading...
Searching...
No Matches
packet_parse.c
Go to the documentation of this file.
2
3#include <assert.h>
4#include <stdbool.h>
5#include <string.h>
6
7#include "vlcb/platform/log.h"
8
10 const VlcbCanFrameId id, const bool is_rtr, const uint8_t payload_len,
11 const VlcbNetAdptPayload *const payload, VlcbNetAdptPkt *const packet) {
12 assert(payload != NULL && packet != NULL);
13 assert(is_rtr == false ||
14 payload_len == 0 /* when rtr is set, no payload can be sent */);
15
16 if (payload_len > VLCB_NET_ADPT_MAX_PAYLOAD) {
18 }
19
20 if (payload_len > 0) {
21 memcpy(&packet->payload, payload, payload_len);
22 }
23
24 packet->payload_len = payload_len;
25 packet->medium = VLCB_MEDIUM_CAN;
26
27 {
28 CanId can_id;
29 const bool valid = vlcb_defs_NewCanId(((uint8_t)id) & 0x7f, &can_id);
30 if (valid == false) {
32 }
33 packet->src_addr = vlcb_net_NewCanIdHwAddr(can_id);
34 }
35
36 VlcbCanPriority prio;
37 const int err = vlcb_defs_CanPriorityFromByte(id >> 7, &prio);
38 if (err != 0) {
39 VLCBLOG_DEBUG("invalid priority received. aliasing to default");
41 }
42
43 packet->meta.can = (VlcbNetAdptCanPktMeta){
44 .is_rtr = is_rtr,
45 .prio = prio,
46 };
47
49}
50
51inline int
53 VlcbCanFrameId *const id) {
54 assert(packet != NULL && id != NULL);
56 return 1;
57 }
58
59 *id = packet->src_addr.can_id;
60
61 VlcbCanPriority prio = VLCB_CAN_PRIO_DEFAULT; // init with default priority
62 if (packet->payload > 0) {
64 }
65
66 *id |= prio << 7;
67
68 return 0;
69}
70
72 assert(packet != NULL);
81 return packet->medium == VLCB_MEDIUM_CAN &&
82 (packet->meta.can.is_rtr && packet->payload_len == 0 ||
86}
VlcbNetAdptErr
Definition adapter.h:39
@ VLCB_NET_ADPT_ERR_INVALID_CANID
Definition adapter.h:66
@ VLCB_NET_ADPT_ERR_OK
Definition adapter.h:40
@ VLCB_NET_ADPT_ERR_PAYLOAD_TOO_LARGE
Definition adapter.h:64
uint8_t VlcbNetAdptPayload[8]
Definition adapter.h:11
#define VLCB_NET_ADPT_MAX_PAYLOAD
Definition adapter.h:9
@ VLCB_MEDIUM_CAN
Definition addr.h:6
VlcbNetHwAddr vlcb_net_NewCanIdHwAddr(VlcbCanId id)
Definition addr.c:17
bool vlcb_defs_IsCanIdValid(VlcbCanId id)
int vlcb_defs_NewCanId(uint8_t value, VlcbCanId *const id)
Definition can_id_new.c:6
bool vlcb_defs_IsCanPriorityValid(VlcbCanPriority prio)
VlcbCanPriority vlcb_defs_CanPriorityFromOpcode(VlcbOpCode opc)
#define VLCB_CAN_PRIO_DEFAULT
int vlcb_defs_CanPriorityFromByte(uint8_t val, VlcbCanPriority *const prio)
#define VLCBLOG_DEBUG(msg)
Definition log.h:35
#define VLCB_NET_ADPT_CAN_MAX_PAYLOAD
Definition packet.h:15
bool vlcb_net_adpt_IsPacketValid(const VlcbNetAdptPkt *const packet)
int vlcb_net_adpt_NewCanFrameIdFromPkt(const VlcbNetAdptPkt *const packet, VlcbCanFrameId *const id)
Construct new CAN frame ID from a packet.
VlcbNetAdptErr vlcb_net_adpt_ParseRawCanData(const VlcbCanFrameId id, const bool is_rtr, const uint8_t payload_len, const VlcbNetAdptPayload *const payload, VlcbNetAdptPkt *const packet)
Parse the CAN packet data into the inner format.
Definition packet_parse.c:9
uint16_t VlcbCanFrameId
CAN frame id used in transmission.
VlcbCanPriority prio
Definition packet.h:12
VlcbNetAdptPktMeta meta
Definition adapter.h:30
VlcbNetMedium medium
Definition adapter.h:28
uint8_t payload_len
Definition adapter.h:31
VlcbNetHwAddr src_addr
Definition adapter.h:29
VlcbNetAdptPayload payload
Definition adapter.h:32
VlcbNetAdptCanPktMeta can
Definition adapter.h:17
VlcbCanId can_id
Definition addr.h:10