Skip to content

Commit 201630f

Browse files
committed
initial checkin
1 parent 744ce07 commit 201630f

12 files changed

+2041
-0
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "libcan-encode-decode"]
2+
path = libcan-encode-decode
3+
url = https://github.com/reinzor/libcan-encode-decode.git

README.md

+86
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
11
# SocketCandecodeSignals
22
filter to decode signals in combination with candump (socketcan tools)
3+
4+
5+
*Compile:*
6+
gcc -g -o socketcanDecodeSignal main.c datenbasis.c processFrame.c lib.c
7+
8+
*Test with vitual can*
9+
**prepare virtual can:**
10+
sudo modprobe vcan
11+
sudo ip link add type vcan
12+
sudo ifconfig vcan0 up
13+
14+
15+
**dump vcan0 with signal decoding:**
16+
candump -L vcan0 | ./socketcanDecodeSignal ccl_test.dbc testFrame1 testFrame2
17+
18+
**send some can frames (other terminal)**
19+
cansend vcan0 001#8d00100100820100
20+
cansend vcan0 002#0C00057003CD1F83
21+
22+
**will result:**
23+
```
24+
Trying to find: Frame: testFrame1
25+
-- testFrame1 (0x001)
26+
Trying to find: Frame: testFrame2
27+
-- testFrame2 (0x002)
28+
(1456687759.303127) vcan0 sig0: 0x01 01
29+
(1456687759.303127) vcan0 sig1: 0x23 35
30+
(1456687759.303127) vcan0 sig2: 0x00 00
31+
(1456687759.303127) vcan0 sig3: 0x800 2048
32+
(1456687759.303127) vcan0 sig4: 0x100 256
33+
(1456687759.303127) vcan0 sig5: 0x01 01
34+
(1456687759.303127) vcan0 sig6: 0x00 00
35+
(1456687759.303127) vcan0 sig7: 0x208 520
36+
(1456687759.303127) vcan0 sig8: 0x00 00
37+
(1456687759.303127) vcan0 sig9: 0x00 00
38+
(1456687759.303127) vcan0 sig10: 0x00 00
39+
(1456687763.319899) vcan0 secSig1: 0x00 00
40+
(1456687763.319899) vcan0 secSig2: 0x00 00
41+
(1456687763.319899) vcan0 secSig3: 0x00 00
42+
(1456687763.319899) vcan0 secSig4: 0x02 02
43+
(1456687763.319899) vcan0 secSig5: 0x00 00
44+
(1456687763.319899) vcan0 secSig6: 0x00 00
45+
(1456687763.319899) vcan0 secSig7: 0x00 00
46+
(1456687763.319899) vcan0 secSig8: 0x03 03
47+
(1456687763.319899) vcan0 secSig9: 0x01 01
48+
(1456687763.319899) vcan0 secSig10: 0x500 1280
49+
(1456687763.319899) vcan0 secSig11: 0xffffffffffffff70 -144
50+
(1456687763.319899) vcan0 secSig12: 0x0c 12
51+
```
52+
53+
54+
*Test without virtualcan:*
55+
echo "(0.0) vcan0 001#8d00100100820100" | ./socketcanDecodeSignal ccl_test.dbc testFrame1
56+
```
57+
Trying to find: Frame: testFrame1
58+
-- testFrame1 (0x001)
59+
(0000.000000) vcan0 sig0: 0x01 01
60+
(0000.000000) vcan0 sig1: 0x23 35
61+
(0000.000000) vcan0 sig2: 0x00 00
62+
(0000.000000) vcan0 sig3: 0x800 2048
63+
(0000.000000) vcan0 sig4: 0x100 256
64+
(0000.000000) vcan0 sig5: 0x01 01
65+
(0000.000000) vcan0 sig6: 0x00 00
66+
(0000.000000) vcan0 sig7: 0x208 520
67+
(0000.000000) vcan0 sig8: 0x00 00
68+
(0000.000000) vcan0 sig9: 0x00 00
69+
(0000.000000) vcan0 sig10: 0x00 00
70+
```
71+
72+
echo "(0.0) vcan0 001#8d00100100820100" | ./socketcanDecodeSignal ccl_test.dbc testFrame1
73+
```
74+
Trying to find: Frame: testFrame2
75+
-- testFrame2 (0x002)
76+
(0000.000001) vcan0 secSig1: 0x00 00
77+
(0000.000001) vcan0 secSig2: 0x00 00
78+
(0000.000001) vcan0 secSig3: 0x00 00
79+
(0000.000001) vcan0 secSig4: 0x02 02
80+
(0000.000001) vcan0 secSig5: 0x00 00
81+
(0000.000001) vcan0 secSig6: 0x00 00
82+
(0000.000001) vcan0 secSig7: 0x00 00
83+
(0000.000001) vcan0 secSig8: 0x03 03
84+
(0000.000001) vcan0 secSig9: 0x01 01
85+
(0000.000001) vcan0 secSig10: 0x500 1280
86+
(0000.000001) vcan0 secSig11: 0xffffffffffffff70 -144
87+
(0000.000001) vcan0 secSig12: 0x0c 12
88+
```

ccl_test.dbc

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
VERSION "created by canmatrix"
2+
3+
4+
NS_ :
5+
6+
BS_:
7+
8+
BU_:
9+
10+
11+
BO_ 1 testFrame1: 8 TEST_ECU
12+
SG_ sig0 : 1|2@0+ (1.0,0.0) [0|0] "" CCL_TEST
13+
SG_ sig1 : 7|6@0+ (1.0,0.0) [0|0] "" CCL_TEST
14+
SG_ sig2 : 15|11@0+ (1.0,0.0) [0|0] "" CCL_TEST
15+
SG_ sig3 : 20|12@0+ (1.0,0.0) [0|0] "" CCL_TEST
16+
SG_ sig4 : 24|9@0+ (1.0,0.0) [0|0] "" CCL_TEST
17+
SG_ sig5 : 50|3@0+ (1.0,0.0) [0|0] "" CCL_TEST
18+
SG_ sig6 : 53|3@0+ (1.0,0.0) [0|0] "" CCL_TEST
19+
SG_ sig7 : 47|10@0+ (1.0,0.0) [0|0] "" CCL_TEST
20+
SG_ sig8 : 58|3@0+ (1.0,0.0) [0|0] "" CCL_TEST
21+
SG_ sig9 : 61|3@0+ (1.0,0.0) [0|0] "" CCL_TEST
22+
SG_ sig10 : 63|2@0+ (1.0,0.0) [0|0] "" CCL_TEST
23+
24+
BO_ 2 testFrame2: 8 TEST_ECU
25+
SG_ secSig1 : 60|2@1+ (1.0,0.0) [0|0] "" CCL_TEST
26+
SG_ secSig2 : 55|1@1+ (1.0,0.0) [0|0] "" CCL_TEST
27+
SG_ secSig3 : 20|4@1+ (1.0,0.0) [0|0] "" CCL_TEST
28+
SG_ secSig4 : 62|2@1+ (1.0,0.0) [0|0] "" CCL_TEST
29+
SG_ secSig5 : 34|3@1+ (1.0,0.0) [0|0] "" CCL_TEST
30+
SG_ secSig6 : 37|3@1+ (1.0,0.0) [0|0] "" CCL_TEST
31+
SG_ secSig7 : 59|1@1- (1.0,0.0) [0|0] "" CCL_TEST
32+
SG_ secSig8 : 56|3@1+ (1.0,0.0) [0|0] "" CCL_TEST
33+
SG_ secSig9 : 52|3@1+ (1.0,0.0) [0|0] "" CCL_TEST
34+
SG_ secSig10 : 8|12@1+ (1.0,0.0) [0|0] "" CCL_TEST
35+
SG_ secSig11 : 24|10@1- (1.0,0.0) [0|0] "" CCL_TEST
36+
SG_ secSig12 : 0|8@1+ (1.0,0.0) [0|0] "" CCL_TEST
37+
38+
39+
40+
41+
42+
43+
44+
45+

datenbasis.c

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <stdio.h>
2+
#include "datenbasis.h"
3+
4+
5+
void add_frame(struct frame_struct **db, canid_t canID, char *frameName) {
6+
struct frame_struct *s;
7+
s = malloc(sizeof(struct frame_struct));
8+
s->canID = canID;
9+
s->signals = NULL;
10+
strcpy(s->name, frameName);
11+
HASH_ADD_INT( *db, canID, s );
12+
}
13+
14+
struct frame_struct *find_frame(struct frame_struct *db, int canID) {
15+
struct frame_struct *s;
16+
HASH_FIND_INT( db, &canID, s );
17+
return s;
18+
/* s: output pointer */
19+
}
20+
21+
struct frame_struct *find_frame_by_name(struct frame_struct *db, char *name) {
22+
struct frame_struct *s;
23+
24+
for(s=db; s != NULL; s=s->hh.next) {
25+
if(!strcmp(s->name, name)) return s;
26+
}
27+
28+
return 0;
29+
/* s: output pointer */
30+
}
31+
32+
struct signal_struct *find_signal_by_name(struct frame_struct *frame, char *name) {
33+
struct signal_struct *sig;
34+
35+
for(sig=frame->signals; sig != NULL; sig = sig->hh.next) {
36+
if(!strcmp(sig->name, name)) return sig;
37+
}
38+
39+
return 0;
40+
}
41+
42+
struct frame_struct *find_frame_by_signalname(struct frame_struct *db, char *name) {
43+
struct frame_struct *frame;
44+
struct signal_struct *sig;
45+
46+
for(frame=db; frame != NULL; frame=frame->hh.next) {
47+
for(sig=frame->signals; sig != NULL; sig = sig->hh.next) {
48+
if(!strcmp(sig->name, name)) return frame;
49+
}
50+
}
51+
52+
return 0;
53+
/* s: output pointer */
54+
}
55+
56+
void add_signal(struct frame_struct *db, int frameId, char *signalName, int startBit, int signalLength, int is_big_endian, int signedState, float factor, float offset, float min, float max, char *unit, char *receiverList)
57+
{
58+
struct frame_struct *frame;
59+
struct signal_struct *newSignal;
60+
61+
frame = find_frame(db, frameId);
62+
63+
newSignal = malloc(sizeof(struct signal_struct));
64+
strcpy(newSignal->name, signalName);
65+
newSignal->startBit = startBit;
66+
newSignal->signalLength = signalLength;
67+
newSignal->is_big_endian = is_big_endian;
68+
newSignal->is_signed = signedState;
69+
newSignal->factor = factor;
70+
newSignal->offset = offset;
71+
newSignal->min = min;
72+
newSignal->max = max;
73+
74+
strcpy(newSignal->unit, unit);
75+
strcpy(newSignal->receiverList, receiverList);
76+
77+
HASH_ADD_STR( frame->signals, name, newSignal );
78+
}
79+
80+
81+
82+
int readInDatabase(struct frame_struct **db, char *Filename)
83+
{
84+
char frameName[512], sender[512], line[512];
85+
char signalName[512],signedState, unit[512], receiverList[512];
86+
int startBit=0,signalLength=0,byteOrder=0;
87+
float factor=0., offset=0., min=0., max=0.;
88+
89+
90+
int frameId=0, len;
91+
92+
FILE *fp;
93+
if(!(fp = fopen(Filename,"r")))
94+
{
95+
fprintf(stderr, "Error opening %s\n", Filename);
96+
return 1;
97+
}
98+
99+
while(fgets(line,511,fp))
100+
{
101+
if(sscanf(line," BO_ %d %s %d %s",&frameId,frameName,&len,sender) == 4)
102+
{
103+
frameName[strlen(frameName)-1] = 0;
104+
add_frame(db, frameId, frameName);
105+
}
106+
else if(sscanf(line," SG_ %s : %d|%d@%d%c (%f,%f) [%f|%f] %s %s",signalName, &startBit, &signalLength,&byteOrder, &signedState, &factor, &offset, &min, &max, unit, receiverList ) > 5)
107+
{
108+
if (byteOrder == 0)
109+
{
110+
// following code is from https://github.com/julietkilo/CANBabel/blob/master/src/main/java/com/github/canbabel/canio/dbc/DbcReader.java:
111+
112+
int pos = 7 - (startBit % 8) + (signalLength - 1);
113+
if (pos < 8)
114+
{
115+
startBit = startBit - signalLength + 1;
116+
}
117+
else
118+
{
119+
int cpos = 7 - (pos % 8);
120+
int bytes = (int)(pos / 8);
121+
startBit = cpos + (bytes * 8) + (int)(startBit/8) * 8;
122+
}
123+
}
124+
125+
add_signal(*db, frameId, signalName, startBit, signalLength, byteOrder == 0, signedState == '-', factor, offset, min, max, unit, receiverList);
126+
}
127+
}
128+
return 0;
129+
}

datenbasis.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#if !defined _DATENBASIS_H_
2+
#define _DATENBASIS_H_
3+
4+
#include "uthash.h"
5+
6+
#include <net/if.h>
7+
#include <linux/can.h>
8+
#include "lib.h"
9+
10+
11+
struct signal_struct {
12+
char name[80];
13+
int startBit;
14+
int signalLength;
15+
int is_big_endian; // Intel = 0; Motorola (== BIG Endian) = 1
16+
int is_signed;
17+
float factor;
18+
float offset;
19+
float min;
20+
float max;
21+
char unit[80];
22+
char receiverList[512];
23+
24+
__u8 number;
25+
UT_hash_handle hh;
26+
};
27+
28+
struct frame_struct {
29+
canid_t canID;
30+
char name[80];
31+
struct signal_struct *signals;
32+
UT_hash_handle hh;
33+
};
34+
35+
36+
#endif
37+
38+
39+
void add_message(struct frame_struct **db, canid_t canID, char *messageName);
40+
struct frame_struct *find_frame(struct frame_struct *db, int canID);
41+
struct frame_struct *find_frame_by_name(struct frame_struct *db, char *name);
42+
struct signal_struct *find_signal_by_name(struct frame_struct *frame, char *name);
43+
struct frame_struct *find_frame_by_signalname(struct frame_struct *db, char *name);
44+
void add_signal(struct frame_struct *db, int frameId, char *signalName, int startBit, int signalLength, int byteOrder, int signedState, float factor, float offset, float min, float max, char *unit, char *receiverList);
45+
int readInDatabase(struct frame_struct **db, char *Filename);
46+

0 commit comments

Comments
 (0)