-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan-posix.c
264 lines (211 loc) · 4.98 KB
/
can-posix.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
#include <libmicro/can.h>
#include <libmicro/can-encap.h>
#include <libmicro/can-tcp.h>
#include <libmicro/can-uart.h>
/***************************************************************************
* Implement can.h for posix systems
*
* Uses cann.[hc] for cand or can-uart.[hc] according to first call to
* can_init( ... );
*/
static cann_conn_t *conn;
/************************************************************************+**
* Management
*/
void can_init_posix(cann_conn_t *aconn) {
conn = aconn;
}
void can_setmode(can_mode_t mode)
{
rs232can_msg rs232_msg;
rs232_msg.len = 1;
rs232_msg.cmd = RS232CAN_SETMODE;
rs232_msg.data[0] = (char)mode;
if (conn)
cann_transmit(conn, &rs232_msg);
else
canu_transmit(&rs232_msg);
}
void can_setfilter() {
// XXX
}
/****************************************************************************
* Sending
*/
//returns pointer to the next can TX buffer
can_message* can_buffer_get()
{
can_message *cmsg = malloc(sizeof(can_message));
if (cmsg == NULL)
{
printf("ERROR: can_buffer_get malloc fail!\n");
exit(EXIT_FAILURE);
}
else
{
memset(cmsg, 0 , sizeof(can_message));
return cmsg;
}
}
can_message_raw* can_buffer_get_raw()
{
can_message_raw *cmsg = malloc(sizeof(can_message_raw));
if (cmsg == NULL)
{
printf("ERROR: can_buffer_get_raw malloc fail!\n");
exit(EXIT_FAILURE);
}
else
return cmsg;
}
//transmit a raw rs232can message
void can_transmit_raw_gateway_message(rs232can_msg *rmsg)
{
if (rmsg == NULL)
{
printf("ERROR: can_transmit_raw_gateway_message got NULL!\n");
exit(EXIT_FAILURE);
}
if (conn)
cann_transmit(conn, rmsg);
else
canu_transmit(rmsg);
}
//transmit a can message
void can_transmit_raw(can_message_raw *cmsg)
{
rs232can_msg rs232_msg;
if (cmsg == NULL)
{
printf("ERROR: can_transmit_raw got NULL!\n");
exit(EXIT_FAILURE);
}
rs232can_msg_from_can_message_raw(&rs232_msg, cmsg);
can_transmit_raw_gateway_message(&rs232_msg);
}
void can_transmit(can_message *cmsg)
{
if (cmsg == NULL)
{
printf("ERROR: cmsg is NULL!\n");
exit(EXIT_FAILURE);
}
can_message_raw raw_msg;
can_message_raw_from_can_message(&raw_msg, cmsg);
can_transmit_raw(&raw_msg);
}
/****************************************************************************
* reciving
*/
rs232can_msg * can_get_raw_gateway_message_nb()
{
if (conn)
return cann_get_nb(conn);
else
return canu_get_nb();
}
// XXX must free messages
//returns next can message
can_message * can_get_nb()
{
rs232can_msg *rs232_msg;
can_message_raw raw_msg;
can_message *can_msg;
if (conn)
rs232_msg = cann_get(conn);
else
rs232_msg = canu_get();
if (!rs232_msg || rs232_msg->cmd != RS232CAN_PKT)
{
free(rs232_msg);
return 0;
}
can_msg = malloc(sizeof(can_message));
if (can_msg == NULL)
{
printf("ERROR: Could not allocate retmsg buffer!\n");
exit(EXIT_FAILURE);
}
can_message_raw_from_rs232can_msg(&raw_msg, rs232_msg);
can_message_from_can_message_raw(can_msg, &raw_msg);
free(rs232_msg);
return can_msg;
}
can_message *can_get()
{
return can_get_nb();
}
//returns next can message, or 0 if no Message was received.
can_message_raw * can_get_raw_nb() {
rs232can_msg *rs232_msg;
can_message_raw *cmsg;
if (conn)
rs232_msg = cann_get_nb(conn);
else
rs232_msg = canu_get_nb();
if (!rs232_msg) return 0;
cmsg = can_buffer_get_raw();
can_message_raw_from_rs232can_msg(cmsg, rs232_msg);
return cmsg;
}
can_message_raw* can_v2_to_raw(can_message_v2 *in_message)
{
can_message_raw *retmsg;
retmsg = malloc(sizeof(can_message_raw));
if (retmsg == NULL)
{
printf("ERROR: Could not allocate retmsg buffer!\n");
exit(EXIT_FAILURE);
}
retmsg->id = 0x0000;
retmsg->id |= ((uint32_t) in_message->channel << 20);
retmsg->id |= ((uint32_t)in_message->subchannel << 18);
retmsg->id |= ((uint16_t) in_message->addr_src << 8);
retmsg->id |= in_message->addr_dst;
retmsg->dlc = in_message->dlc;
memcpy(retmsg->data, in_message->data, 8);
return retmsg;
}
void can_transmit_v2(can_message_v2 *in_msg)
{
can_message_raw *txmsg;
txmsg = can_v2_to_raw (in_msg);
can_transmit_raw (txmsg);
}
can_message_v2 * can_get_v2_nb()
{
rs232can_msg *rs232_msg;
can_message_raw *cmsg;
can_message_v2 *retmsg;
if (conn)
rs232_msg = cann_get_nb(conn);
else
rs232_msg = canu_get_nb();
if (!rs232_msg) return 0;
retmsg = malloc(sizeof(can_message_v2));
if (retmsg == NULL)
{
printf("ERROR: Could not allocate retmsg buffer!\n");
exit(EXIT_FAILURE);
}
cmsg = can_buffer_get_raw();
can_message_raw_from_rs232can_msg(cmsg, rs232_msg);
retmsg->addr_src = (uint8_t) (cmsg->id >> 8);
retmsg->addr_dst = (uint8_t) (cmsg->id);
retmsg->channel = (uint16_t) ((cmsg->id >> 20) & 0x1ff);
retmsg->subchannel = (uint8_t)((cmsg->id >> 18 ) & 0x03);
retmsg->dlc = cmsg->dlc;
memcpy(retmsg->data, cmsg->data, 8);
free (cmsg);
return retmsg;
}
void can_free(can_message *msg){
free(msg);
}
void can_free_v2(can_message_v2 *msg){
free(msg);
}