-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_can_bridge.cpp
116 lines (90 loc) · 2.55 KB
/
c_can_bridge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "c_can_bridge.h"
//only for testing purposes
//#include <stdio.h>
msgReceived callbacks[MAX_CAN_INTERFACES];
pthread_t threads[MAX_CAN_INTERFACES];
int res;
void initLibrary() {
canPortLibraryInit();
}
int openPort(int port, int bitrate, msgReceived function) {
res = CANOPEN_ERROR;
res = canPortOpen(port);
if (res!=CANOPEN_OK)
return res;
res = canPortBitrateSet( port, bitrate );
if (res!=CANOPEN_OK)
return res;
res = canPortGoBusOn(port);
if (res!=CANOPEN_OK)
return res;
callbacks[port] = function;
long tmp_port = (long)port;
long *__port = (long *) malloc(sizeof(long));
__port = (long*)tmp_port;
res = pthread_create(&threads[port], NULL, readMsgFunction, (void*)__port);
if (res!=CANOPEN_OK)
return res;
return CANOPEN_OK;
}
int closePort(int port) {
res = CANOPEN_ERROR;
res = pthread_cancel(threads[port]);
//if (res!=CANOPEN_OK)
// return res;
res = canPortGoBusOff(port);
//if (res!=CANOPEN_OK)
// return res;
res = canPortClose(port);
//if (res!=CANOPEN_OK)
// return res;
canSinglePortReset(port);
return res;
}
int echoPort(int port, bool enable) {
return canPortEcho(port, enable );
}
int writeMsg ( int port,
long long id,
int dlc,
int flags,
void *msg
) {
return canPortWrite (port,
id,
msg,
(unsigned int)dlc,
(unsigned int)flags);
}
long msg_id;
char msg_content[8];
unsigned int msg_size;
unsigned int msg_flags;
int read_port;
void *readMsgFunction (void *__port) {
/*int*/ read_port = (long) __port;
//int ret;
while(1)
{
/*
long msg_id;
char msg_content[8];
unsigned int msg_size;
unsigned int msg_flags;
*/
res =
canPortRead( read_port,
&msg_id,
(void*)msg_content,
&msg_size,
&msg_flags);
if (res == CANOPEN_OK) {
(*callbacks[read_port])( read_port,
msg_id,
(int)msg_size,
(int)msg_flags,
msg_content);
}
}
return NULL;
}