Statistics
| Branch: | Tag: | Revision:

amiro-blt / Target / Source / gateway.c @ 1446566f

History | View | Annotate | Download (13.027 KB)

1
/************************************************************************************//**
2
* \file         Source\gateway.c
3
* \brief        Bootloader gateway interface source file. Needs the communication module!
4
* \ingroup      Core
5
* \internal
6
*----------------------------------------------------------------------------------------
7
*                          C O P Y R I G H T
8
*----------------------------------------------------------------------------------------
9
*   Copyright (c) 2011  by Feaser    http://www.feaser.com    All rights reserved
10
*
11
*----------------------------------------------------------------------------------------
12
*                            L I C E N S E
13
*----------------------------------------------------------------------------------------
14
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
15
* modify it under the terms of the GNU General Public License as published by the Free
16
* Software Foundation, either version 3 of the License, or (at your option) any later
17
* version.
18
*
19
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21
* PURPOSE. See the GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License along with OpenBLT.
24
* If not, see <http://www.gnu.org/licenses/>.
25
*
26
* A special exception to the GPL is included to allow you to distribute a combined work 
27
* that includes OpenBLT without being obliged to provide the source code for any 
28
* proprietary components. The exception text is included at the bottom of the license
29
* file <license.html>.
30
* 
31
* \endinternal
32
****************************************************************************************/
33

    
34
/****************************************************************************************
35
* Include files
36
****************************************************************************************/
37
#include "boot.h"                                /* bootloader generic header          */
38
#if (BOOT_GATE_CAN_ENABLE > 0)
39
  #include "can.h"                                    /* can driver module             */
40
#endif
41
#if (BOOT_GATE_UART_ENABLE > 0)
42
  #include "uart.h"                                   /* uart driver module            */
43
#endif
44
#if (BOOT_GATE_USB_ENABLE > 0)
45
  #include "usb.h"                                    /* usb driver module             */
46
#endif
47
#if (BOOT_GATE_NET_ENABLE > 0)
48
  #include "net.h"                                    /* tcp/ip driver module          */
49
#endif
50

    
51

    
52
#if (BOOT_GATE_ENABLE > 0)
53
/****************************************************************************************
54
* Local data declarations
55
****************************************************************************************/
56
/** \brief Holds the gateway interface of the currently active interface. */
57
static tGateInterfaceId gateActiveInterface = GATE_IF_OTHER;
58
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
59
static void InitDebugData(void);
60
#endif
61

    
62
blt_bool forwarded;
63

    
64
/************************************************************************************//**
65
** \brief     Initializes the gateway module. The hardware needed for the
66
**            communication should be initialized in the communication module!
67
** \return    none
68
**
69
****************************************************************************************/
70
void GateInit(void)
71
{
72
  forwarded = BLT_FALSE;
73
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
74
  InitDebugData();
75
#endif
76
#if (BOOT_GATE_CAN_ENABLE > 0)
77
  #if (BOOT_COM_CAN_ENABLE <= 0)
78
  /* initialize the CAN controller */
79
  CanInit();
80
  #endif
81
  /* set it as active */
82
  gateActiveInterface = GATE_IF_CAN;
83
#endif
84
#if (BOOT_GATE_UART_ENABLE > 0)
85
  #if (BOOT_COM_UART_ENABLE <= 0)
86
  /* initialize the UART interface */
87
  UartInit();
88
  #endif
89
  /* set it as active */
90
  gateActiveInterface = GATE_IF_UART;
91
#endif
92
#if (BOOT_GATE_USB_ENABLE > 0)
93
  #if (BOOT_COM_USB_ENABLE <= 0)
94
  /* initialize the USB interface */
95
  UsbInit();
96
  #endif
97
  /* set it as active */
98
  gateActiveInterface = GATE_IF_USB;
99
#endif
100
#if (BOOT_GATE_NET_ENABLE > 0)
101
  #if (BOOT_COM_NET_ENABLE <= 0)
102
  /* initialize the TCP/IP interface */
103
  NetInit();
104
  #endif
105
  /* set it as active */
106
  gateActiveInterface = GATE_IF_NET;
107
#endif
108
} /*** end of GateInit ***/
109

    
110
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
111
/************************************************************************************//**
112
** \brief     Initializes the debugging data.
113
** \return    none
114
**
115
****************************************************************************************/
116
static void InitDebugData(void) {
117
  debugDataRCan[0] = 'r';
118
  debugDataRCan[1] = 'C';
119
  debugDataRCan[2] = 'A';
120
  debugDataRCan[3] = 'N';
121
  debugDataRCan[4] = ' ';
122
  debugDataRCan[5] = ':';
123
  debugDataRCan[6] = ' ';
124
  debugDataRCan[7] = 10;
125
  debugDataTCan[0] = 't';
126
  debugDataTCan[1] = 'C';
127
  debugDataTCan[2] = 'A';
128
  debugDataTCan[3] = 'N';
129
  debugDataTCan[4] = ' ';
130
  debugDataTCan[5] = ':';
131
  debugDataTCan[6] = ' ';
132
  debugDataTCan[7] = 10;
133
} /*** end of InitDebugData ***/
134
#endif
135

    
136

    
137
/************************************************************************************//**
138
** \brief     Updates the gateway module by checking if new data was received 
139
**            and submitting the request to process newly received data.
140
** \return    none
141
**
142
****************************************************************************************/
143
void GateTask(void)
144
{
145
  blt_int16s messageLength;
146
  /* make xcpCtoReqPacket static for runtime efficiency */
147
  static unsigned char xcpCtoReqPacket[BOOT_COM_RX_MAX_DATA];
148
 
149
#if (BOOT_GATE_CAN_ENABLE > 0)
150
  messageLength = (blt_int16s)CanReceivePacket(&xcpCtoReqPacket[0]);
151
  if (messageLength > 0)
152
  {
153
    /* make this the active interface */
154
    gateActiveInterface = GATE_IF_CAN;
155
    /* process packet */
156
    if (forwarded == BLT_TRUE) {
157
      XcpGatewayPacketReceived(&xcpCtoReqPacket[0], messageLength);
158
    } else {
159
      XcpPacketReceived(&xcpCtoReqPacket[0], messageLength, BLT_TRUE);
160
    }
161
    forwarded = BLT_FALSE;
162
  }
163
#endif
164
#if (BOOT_GATE_UART_ENABLE > 0)
165
  messageLength = (blt_int16s)UartReceivePacket(&xcpCtoReqPacket[0]);
166
  if (messageLength > 0)
167
  {
168
    /* make this the active interface */
169
    gateActiveInterface = GATE_IF_UART;
170
    /* process packet */
171
    if (forwarded == BLT_TRUE) {
172
      XcpGatewayPacketReceived(&xcpCtoReqPacket[0], messageLength);
173
    } else {
174
      XcpPacketReceived(&xcpCtoReqPacket[0], messageLength, BLT_TRUE);
175
    }
176
    forwarded = BLT_FALSE;
177
  }
178

    
179
#endif
180
#if (BOOT_GATE_USB_ENABLE > 0)
181
  messageLength = (blt_int16s)UsbReceivePacket(&xcpCtoReqPacket[0]);
182
  if (messageLength > 0)
183
  {
184
    /* make this the active interface */
185
    gateActiveInterface = GATE_IF_USB;
186
    /* process packet */
187
    if (forwarded == BLT_TRUE) {
188
      XcpGatewayPacketReceived(&xcpCtoReqPacket[0], messageLength);
189
    } else {
190
      XcpPacketReceived(&xcpCtoReqPacket[0], messageLength, BLT_TRUE);
191
    }
192
    forwarded = BLT_FALSE;
193
  }
194
#endif
195
} /*** end of GateTask ***/
196

    
197

    
198
/************************************************************************************//**
199
** \brief     Releases the gateway module.
200
** \return    none
201
**
202
****************************************************************************************/
203
void GateFree(void)
204
{
205
#if (BOOT_GATE_USB_ENABLE > 0)
206
  /* disconnect the usb device from the usb host */
207
  UsbFree();
208
#endif
209
} /*** end of GateFree ***/
210

    
211

    
212
/************************************************************************************//**
213
** \brief     Transmits the packet using the xcp transport layer.
214
** \param     data Pointer to the byte buffer with packet data.
215
** \param     len  Number of data bytes that need to be transmitted.
216
** \return    none
217
**
218
****************************************************************************************/
219
void GateTransmitPacket(blt_int8u *data, blt_int16u len)
220
{
221
#if (BOOT_GATE_CAN_ENABLE > 0)
222
  /* transmit the packet. note that len is limited to 8 in the plausibility check,
223
   * so cast is okay.
224
   */
225
  if (gateActiveInterface == GATE_IF_CAN)
226
  {
227
    CanTransmitPacket(data, (blt_int8u)len, 0);
228
  }
229
#endif
230
#if (BOOT_GATE_UART_ENABLE > 0)
231
  /* transmit the packet. note that len is limited to 255 in the plausibility check,
232
   * so cast is okay.
233
   */
234
  if (gateActiveInterface == GATE_IF_UART)
235
  {
236
    UartTransmitPacket(data, (blt_int8u)len);
237
  }
238
#endif
239
#if (BOOT_GATE_USB_ENABLE > 0)
240
  /* transmit the packet */
241
  if (gateActiveInterface == GATE_IF_USB)
242
  {
243
    UsbTransmitPacket(data, len);
244
  }
245
#endif
246
#if (BOOT_GATE_NET_ENABLE > 0)
247
  if (gateActiveInterface == GATE_IF_NET)
248
  {
249
    /* transmit the packet */
250
    NetTransmitPacket(data, len);
251
  }
252
#endif
253

    
254
  /* send signal that the packet was transmitted */
255
  XcpPacketTransmitted();
256
} /*** end of GateTransmitPacket ***/
257

    
258

    
259
/************************************************************************************//**
260
** \brief     Obtains the maximum number of bytes that can be received on the specified
261
**            gateway interface.
262
** \return    Maximum number of bytes that can be received.
263
**
264
****************************************************************************************/
265
blt_int16u GateGetActiveInterfaceMaxRxLen(void)
266
{
267
  blt_int16u result;
268
  
269
  /* filter on gateway interface identifier */
270
  switch (gateActiveInterface)
271
  {
272
    case GATE_IF_UART:
273
      result = BOOT_COM_UART_RX_MAX_DATA;
274
      break;
275

    
276
    case GATE_IF_CAN:
277
      result = BOOT_COM_CAN_RX_MAX_DATA;
278
      break;
279

    
280
    case GATE_IF_USB:
281
      result = BOOT_COM_USB_RX_MAX_DATA;
282
      break;
283

    
284
    case GATE_IF_NET:
285
      result = BOOT_COM_NET_RX_MAX_DATA;
286
      break;
287
      
288
    default:
289
      result = BOOT_COM_RX_MAX_DATA;
290
      break;
291
  }
292
  
293
  return result;
294
} /*** end of GateGetActiveInterfaceMaxRxLen ***/
295

    
296

    
297
/************************************************************************************//**
298
** \brief     Obtains the maximum number of bytes that can be transmitted on the 
299
**            specified gateway interface.
300
** \return    Maximum number of bytes that can be received.
301
**
302
****************************************************************************************/
303
blt_int16u GateGetActiveInterfaceMaxTxLen(void)
304
{
305
  blt_int16u result;
306
  
307
  /* filter on gateway interface identifier */
308
  switch (gateActiveInterface)
309
  {
310
    case GATE_IF_UART:
311
      result = BOOT_COM_UART_TX_MAX_DATA;
312
      break;
313

    
314
    case GATE_IF_CAN:
315
      result = BOOT_COM_CAN_TX_MAX_DATA;
316
      break;
317

    
318
    case GATE_IF_USB:
319
      result = BOOT_COM_USB_TX_MAX_DATA;
320
      break;
321

    
322
    case GATE_IF_NET:
323
      result = BOOT_COM_NET_TX_MAX_DATA;
324
      break;
325
      
326
    default:
327
      result = BOOT_COM_TX_MAX_DATA;
328
      break;
329
  }
330
  
331
  return result;
332
} /*** end of GateGetActiveInterfaceMaxTxLen ***/
333

    
334

    
335
/************************************************************************************//**
336
** \brief     This function obtains the XCP connection state.
337
** \return    BLT_TRUE when an XCP connection is established, BLT_FALSE otherwise.
338
**
339
****************************************************************************************/
340
blt_bool GateIsConnected(void)
341
{
342
  return XcpIsConnected();
343
} /*** end of GateIsConnected ***/
344

    
345

    
346
#endif /* BOOT_GATE_ENABLE > 0 */
347

    
348

    
349

    
350

    
351

    
352
#if (BOOT_GATE_CAN_ENABLE > 0)
353
/************************************************************************************//**
354
** \brief     This function transmitts the packet direct with CAN.
355
** \param     data     Pointer to the byte buffer with packet data.
356
** \param     len      Number of data bytes that need to be transmitted.
357
** \param     deviceID ID of the device the data has to be sent to.
358
** \return    none.
359
**
360
****************************************************************************************/
361
void GateTransmitPacketDirect(blt_int8u *data, blt_int8u len, blt_int8u deviceID) {
362
  forwarded = BLT_TRUE;
363
  CanTransmitPacket(data, len, deviceID);
364
  XcpPacketTransmitted();
365
} /*** end of GateTransmitPacketDirect ***/
366

    
367
#if (BOOT_DEBUGGING_UART2_ENABLE > 0)
368
/************************************************************************************//**
369
** \brief     This function transmitts debugging data with UART2.
370
** \param     debugData Pointer to the byte buffer with additional debugging data.
371
** \param     data      Pointer to the byte buffer with packet data.
372
** \param     len       Number of packet data bytes that need to be transmitted.
373
** \return    none.
374
**
375
****************************************************************************************/
376
void BuildData(blt_int8u *debugData, blt_int8u *data, blt_int8u len) {
377
  UartSendDebuggingPacket(debugData, 7);
378
  UartSendDebuggingPacket(data, len);
379
  UartSendDebuggingPacket(&debugData[7], 1);
380
} /*** end of BuildData ***/
381

    
382
#endif /* BOOT_DEBUGGING_UART2_ENABLE > 0 */
383

    
384
#endif /* BOOT_GATE_CAN_ENABLE > 0 */
385

    
386
/*********************************** end of gateway.c **************************************/