-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathJ1939_TL.c
364 lines (315 loc) · 12.1 KB
/
J1939_TL.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/////////////////////////////////////////////////////////////////////////////////////////
// File: J1939_TL.c
// Module: J1939 stack
// Description: J1939 stack Transport Layer
// Originator: MB
// Derived from: Freescale J1939 stack
// Date created: 09 June 2014
//---------------------------------------------------------------------------------------
// Revision Log:
// $Log: $
//
//---------------------------------------------------------------------------------------
// Notes:
//
//---------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
// Copyright (C)2014 by Timespace Technology Ltd. All rights reserved.
//
// This software is confidential. It is not to be copied or distributed in any form
// to any third party.
//
// This software is provided by Timespace Technology on an 'as is' basis, and Timespace
// Technology expressly disclaims any and all warranties, expressed or implied including,
// without limitation, warranties of merchantability and fitness for a particular
// purpose. In no event shall Timespace Technology be liable for any direct, indirect,
// incidental, punitive or consequential damages - of any kind whatsoever - with respect
// to the software.
/////////////////////////////////////////////////////////////////////////////////////////
#include "J1939_includes.h"
static J1939_RX_MESSAGE_T J1939_rx_message;
static J1939_RX_STATE_MACHINE_T J1939_rx_state_machine;
static J1939_RX_PDU_T J1939_rx_pdu;
//==========================================================================================
// Transport Layer Interface Functions
//==========================================================================================
void TL_init(void)
{
U08 i;
J1939_rx_message.PGN = 0;
J1939_rx_message.dest_addr = 0;
J1939_rx_message.source_addr = 0;
J1939_rx_message.byte_count = 0;
J1939_rx_pdu.PGN = 0;
J1939_rx_pdu.dest_addr = 0;
J1939_rx_pdu.source_addr = 0;
J1939_rx_pdu.byte_count = 0;
J1939_rx_state_machine.PGN = 0;
J1939_rx_state_machine.dest_addr = 0;
J1939_rx_state_machine.source_addr = 0;
J1939_rx_state_machine.packet_number = 0;
J1939_rx_state_machine.timer_counter = 0;
J1939_rx_state_machine.total_packet_number = 0;
J1939_rx_state_machine.byte_count = 0;
J1939_rx_state_machine.status = WAIT_FOR_MESSAGE;
J1939_rx_state_machine.TP = TP_NONE;
for (i = 0; i < NUMBER_TRANS_RX_BUFFERS; i++)
{
J1939_rx_message.data[i] = 0;
}
for (i = 0; i < NUMBER_PDU_BUFFERS; i++)
{
J1939_rx_pdu.data[i] = 0;
}
}
void TL_process(J1939_RX_PDU_T *pdu_ptr)
{
U08 i;
if ((pdu_ptr->PGN == TP_CM) || (pdu_ptr->PGN == TP_DT))
{
J1939_rx_pdu.PGN = pdu_ptr->PGN;
J1939_rx_pdu.dest_addr = pdu_ptr->dest_addr;
J1939_rx_pdu.source_addr = pdu_ptr->source_addr;
J1939_rx_pdu.byte_count = pdu_ptr->byte_count;
for (i = 0; i < NUMBER_PDU_BUFFERS; i++)
{
J1939_rx_pdu.data[i] = pdu_ptr->data[i];
}
}
else
{
J1939_RX_MESSAGE_T msg;
msg.PGN = pdu_ptr->PGN;
msg.dest_addr = pdu_ptr->dest_addr;
msg.source_addr = pdu_ptr->source_addr;
msg.byte_count = pdu_ptr->byte_count;
//for (i = 0; i < NUMBER_TRANS_RX_BUFFERS; i++)
for (i = 0; i < NUMBER_PDU_BUFFERS; i++)
{
msg.data[i] = pdu_ptr->data[i];
}
// send message up the stack to the Network Management Layer
NML_process(&msg);
}
return;
}
void TL_periodic(void)
{
U08 i;
U08 go_on;
go_on = TRUE;
J1939_rx_state_machine.timer_counter++;
while (go_on)
{
switch (J1939_rx_state_machine.status)
{
case WAIT_FOR_MESSAGE:
if (J1939_rx_pdu.PGN == TP_CM && J1939_rx_pdu.dest_addr == GLOBADDR || J1939_rx_pdu.dest_addr == NODEADDR)
{
if (J1939_rx_pdu.data[0] == TP_CM_RTS)
{
J1939_rx_state_machine.PGN = J1939_rx_pdu.data[6];
J1939_rx_state_machine.PGN <<= 8;
J1939_rx_state_machine.PGN += J1939_rx_pdu.data[5];
J1939_rx_state_machine.dest_addr = J1939_rx_pdu.dest_addr;
J1939_rx_state_machine.source_addr = J1939_rx_pdu.source_addr;
J1939_rx_state_machine.packet_number = 0;
J1939_rx_state_machine.timer_counter = 0;
J1939_rx_state_machine.total_packet_number = J1939_rx_pdu.data[3];
J1939_rx_state_machine.byte_count = J1939_rx_pdu.data[2];
J1939_rx_state_machine.byte_count <<= 8;
J1939_rx_state_machine.byte_count += J1939_rx_pdu.data[1];
J1939_rx_state_machine.status = INIT_REASSEMBLE_STRUCTURE;
J1939_rx_state_machine.TP = TP_CM_RTS;
}
else if (J1939_rx_pdu.data[0] == TP_CM_BAM)
{
J1939_rx_state_machine.PGN = J1939_rx_pdu.data[6];
J1939_rx_state_machine.PGN <<= 8;
J1939_rx_state_machine.PGN += J1939_rx_pdu.data[5];
J1939_rx_state_machine.dest_addr = J1939_rx_pdu.dest_addr;
J1939_rx_state_machine.source_addr = J1939_rx_pdu.source_addr;
J1939_rx_state_machine.packet_number = 0;
J1939_rx_state_machine.timer_counter = 0;
J1939_rx_state_machine.total_packet_number = J1939_rx_pdu.data[3];
J1939_rx_state_machine.byte_count = J1939_rx_pdu.data[2];
J1939_rx_state_machine.byte_count <<= 8;
J1939_rx_state_machine.byte_count += J1939_rx_pdu.data[1];
J1939_rx_state_machine.status = INIT_REASSEMBLE_STRUCTURE;
J1939_rx_state_machine.TP = TP_CM_BAM;
}
else
{
go_on = FALSE;//break;
}
}
else
{
go_on = FALSE;//break;
}
break;
case INIT_REASSEMBLE_STRUCTURE:
if (J1939_rx_state_machine.TP == TP_CM_RTS)
{
}
else if (J1939_rx_state_machine.TP == TP_CM_BAM)
{
if (J1939_rx_state_machine.byte_count < NUMBER_TRANS_RX_BUFFERS)
{
J1939_rx_message.PGN = J1939_rx_state_machine.PGN;
J1939_rx_message.dest_addr = J1939_rx_state_machine.dest_addr;
J1939_rx_message.source_addr = J1939_rx_state_machine.source_addr;
J1939_rx_message.byte_count = J1939_rx_state_machine.byte_count;
for (i = 0; i < NUMBER_TRANS_RX_BUFFERS; i++)
{
J1939_rx_message.data[i] = 0;
}
J1939_rx_state_machine.status = WAIT_FOR_DATA;
}
else
{
J1939_rx_state_machine.status= WAIT_FOR_MESSAGE;
J1939_rx_state_machine.timer_counter = 0;
go_on = FALSE;
//break;
}
}
else
{
J1939_rx_state_machine.status = WAIT_FOR_MESSAGE;
J1939_rx_state_machine.timer_counter = 0;
go_on = FALSE;
//break;
}
break;
case CHECK_PACKET:
break;
case SEND_ABORT:
break;
case SEND_CTS_WITH_COUNT:
break;
case WAIT_FOR_DATA:
if (J1939_rx_pdu.PGN == TP_DT)
{
J1939_rx_state_machine.status = CHECK_DATA_PACKET;
//break;
}
else
{
J1939_rx_state_machine.status = CHECK_TIMER;
//break;
}
break;
case CHECK_TIMER:
if (J1939_rx_state_machine.timer_counter > (750/TICK)) // 750ms
{
J1939_rx_state_machine.status = RESET_REASSEMBLY_STRUCTURE;
//J1939_rx_state_machine.timer_counter = 0;
//break;
}
else
{
J1939_rx_state_machine.status = WAIT_FOR_DATA;
go_on = FALSE;//break;
}
break;
case RESET_REASSEMBLY_STRUCTURE:
J1939_rx_state_machine.PGN = 0;
J1939_rx_state_machine.dest_addr = 0;
J1939_rx_state_machine.source_addr = 0;
J1939_rx_state_machine.packet_number = 0;
J1939_rx_state_machine.timer_counter = 0;
J1939_rx_state_machine.total_packet_number = 0;
J1939_rx_state_machine.byte_count = 0;
J1939_rx_message.PGN = 0;
J1939_rx_message.dest_addr = 0;
J1939_rx_message.source_addr = 0;
J1939_rx_message.byte_count = 0;
for (i = 0; i < NUMBER_TRANS_RX_BUFFERS; i++)
{
J1939_rx_message.data[i] = 0;
}
J1939_rx_state_machine.status = WAIT_FOR_MESSAGE;
go_on = FALSE;
break;
case CHECK_DATA_PACKET:
if (J1939_rx_state_machine.TP == TP_CM_BAM)
{
if (J1939_rx_pdu.source_addr == J1939_rx_state_machine.source_addr &&
J1939_rx_pdu.dest_addr == J1939_rx_state_machine.dest_addr)
{
if (J1939_rx_state_machine.packet_number == J1939_rx_pdu.data[0]-1)
{
J1939_rx_state_machine.status = SAVE_DATA;
J1939_rx_state_machine.timer_counter = 0;
J1939_rx_pdu.data[0] = 0;
}
else if(J1939_rx_pdu.data[0] != 0)
{
J1939_rx_state_machine.status = RESET_REASSEMBLY_STRUCTURE;
}
else
{
J1939_rx_state_machine.status = WAIT_FOR_DATA;
go_on = FALSE;
}
}
else
{
J1939_rx_state_machine.status = WAIT_FOR_DATA;
go_on = FALSE;
}
}
else if (J1939_rx_state_machine.TP == TP_CM_RTS)
{}
else
{}
break;
case SAVE_DATA:
i=0;
while ((i < 7) && ((J1939_rx_state_machine.packet_number*7+i) <= J1939_rx_state_machine.byte_count))
{
J1939_rx_message.data[J1939_rx_state_machine.packet_number*7+i] = J1939_rx_pdu.data[i+1];
J1939_rx_pdu.data[i+1] = 0;
i++;
}
J1939_rx_state_machine.packet_number++;
if (J1939_rx_state_machine.packet_number == J1939_rx_state_machine.total_packet_number)
{
J1939_rx_state_machine.status = FILL_USER_MESSAGE;
}
else
{
J1939_rx_state_machine.status = WAIT_FOR_DATA;
go_on = FALSE;
}
break;
case SEND_EOM:
case SEND_CTS:
case FILL_USER_MESSAGE:
J1939_rx_state_machine.status = RESET_REASSEMBLY_STRUCTURE;
J1939_rx_state_machine.timer_counter = 0;
// send message up the stack to the Network Management Layer
NML_process(&J1939_rx_message);
//go_on = FALSE;
break;
default:
go_on = FALSE;
break;
} // end switch
} // end while
}
U08 Transmit_J1939msg(J1939_TX_MESSAGE_T *msg_ptr)
{
if (msg_ptr->byte_count > 8)
{
#if NOT_YET
// Transport layer for transmission of J1939 messages > 8 bytes not implemented yet
#endif
}
else
{
Build_CANpkt(msg_ptr, 0);
}
return TRUE;
}