Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Functions | Variables
DBCParser.h File Reference
#include <stdint.h>
#include "Can.h"
Include dependency graph for DBCParser.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int parseDbcLine (CAN_MessageList *messages, char *line)
 Parses a single line of a DBC file and populates the CAN_MessageList structure. More...
 
int parseDbcFile (CAN_MessageList *messages, const unsigned char *dbc_contents)
 Parses a DBC (CAN database) file and populates the DBC structure. More...
 
void print_CAN_MessageList (const CAN_MessageList *messages)
 Prints the contents of a CAN message list (aka DBC file). More...
 

Variables

static const int MAX_LINE_LENGTH = 256
 

Function Documentation

◆ parseDbcFile()

int parseDbcFile ( CAN_MessageList messages,
const unsigned char *  dbc_contents 
)

Parses a DBC (CAN database) file and populates the DBC structure.

This function reads the specified DBC file and parses its content, populating the provided DBC structure with messages and signals.

Parameters
[out]CAN_MessageListPointer to the CAN_MessageList structure that will be populated with the parsed data.
[in]filenamePath to the DBC file to parse.
Returns
Returns 1 on success, or o on failure.
Note
The CAN_MessageList structure should be properly initialized before calling this function.

Definition at line 31 of file DBCParser.c.

31 {
32 // dbc contents is a string of the file
33 char* line = strtok(dbc_contents, "\n");
34 while (line != NULL) {
35 printf("Parsing line: %s\n", line);
36 if (!parseDbcLine(messages, line)) {
37 return 0;
38 }
39 line = strtok(NULL, "\n");
40 }
41 return 1;
42}
int parseDbcLine(CAN_MessageList *messages, char *line)
Parses a single line of a DBC file and populates the CAN_MessageList structure.
Definition: DBCParser.c:7
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseDbcLine()

int parseDbcLine ( CAN_MessageList messages,
char *  line 
)

Parses a single line of a DBC file and populates the CAN_MessageList structure.

This function parses a single line of a CAN_MessageList file and populates the provided CAN_MessageList structure with messages and signals.

Parameters
[out]messagesPointer to the CAN_MessageList structure that will be populated with the parsed data.
[in]lineLine of the DBC file to parse.
Returns
Returns 1 on success, or 0 on failure.

Definition at line 7 of file DBCParser.c.

7 {
8 // Check if the line is a message
9 if (strstr(line, "BO_") != NULL) {
10 // Parse the message
12 if (sscanf(line, "BO_ %d %s: %d %s", &msg.id, msg.name, &msg.dlc, msg.sender) != 4) {
13 return 0;
14 }
15 messages->messages[messages->num_messages] = msg;
16 messages->num_messages++;
17 } else if (strstr(line, "SG_") != NULL) {
18 // Parse the signal
20 char* signedness = 0;
21 if (sscanf(line, "SG_ %s %d|%d@%d%c(%f,%f) [%f|%f] \"%[^\"]\" %s", sig.name, &sig.start_bit, &sig.length, &sig.endian, signedness, &sig.scale, &sig.offset, &sig.min, &sig.max, sig.unit, sig.reciever) != 11) {
22 return 0;
23 }
24 sig.isSigned = signedness[0] == '-';
25 messages->messages[messages->num_messages - 1].signals[messages->messages[messages->num_messages - 1].signal_count] = sig;
26 messages->messages[messages->num_messages - 1].signal_count++;
27 }
28 return 1;
29}
CAN_Message_Template messages[MAX_MESSAGES]
Definition: Can.h:123
int num_messages
Definition: Can.h:124
char sender[MAX_NODE_NAME_LENGTH]
Definition: Can.h:78
int signal_count
Definition: Can.h:80
CAN_Signal_Template signals[MAX_SIGNALS]
Definition: Can.h:79
char name[MAX_MESSAGE_NAME_LENGTH]
Definition: Can.h:77
char unit[MAX_UNIT_NAME_LENGTH]
Definition: Can.h:29
char reciever[MAX_NODE_NAME_LENGTH]
Definition: Can.h:30
float offset
Definition: Can.h:26
float scale
Definition: Can.h:25
char name[MAX_SIGNAL_NAME_LENGTH]
Definition: Can.h:20
Here is the caller graph for this function:

◆ print_CAN_MessageList()

void print_CAN_MessageList ( const CAN_MessageList messages)

Prints the contents of a CAN message list (aka DBC file).

This function prints the contents of a CAN message list (aka DBC file) to the console.

Parameters
[in]messagesPointer to the CAN message list to print. return void

Definition at line 44 of file DBCParser.c.

44 {
45 printf("Printing CAN Message List with %d messages\n", messages->num_messages);
46 for (int i = 0; i < messages->num_messages; i++) {
47 const CAN_Message_Template *msg = &messages->messages[i];
48 printf(ANSI_COLOR_GREEN "Message" ANSI_COLOR_RESET ": %s (ID: %d, DLC: %d, Sender: %s, SIGs: %d)\n", msg->name, msg->id, msg->dlc, msg->sender, msg->signal_count);
49 for (int j = 0; j < msg->signal_count; j++) {
50 const CAN_Signal_Template *sig = &msg->signals[j];
51 printf("\t" ANSI_COLOR_BLUE "Signal" ANSI_COLOR_RESET ": %s (Start bit: %d, Length: %d, Endain: %d, Signed: %c,\n\t\tScale: %f, Offset: %f, Min: %f, Max: %f, \n\t\tUnit: %s, Reciever: %s)\n", sig->name, sig->start_bit, sig->length, sig->endian, sig->isSigned, sig->scale, sig->offset, sig->min, sig->max, sig->unit, sig->reciever);
52 }
53 printf("\n");
54 }
55}
#define ANSI_COLOR_RESET
Definition: PrintHelpers.h:7
#define ANSI_COLOR_GREEN
Definition: PrintHelpers.h:2
#define ANSI_COLOR_BLUE
Definition: PrintHelpers.h:4
Here is the caller graph for this function:

Variable Documentation

◆ MAX_LINE_LENGTH

const int MAX_LINE_LENGTH = 256
static

Definition at line 8 of file DBCParser.h.