summaryrefslogtreecommitdiff
path: root/lcmodule/source/cnh1605205_ldr_network_layer/include/t_r15_network_layer.h
blob: 5e78ddd209b0fd9c8964aceae9cd2658fdaa89bb (plain)
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
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/
#ifndef _T_R15_NETWORK_LAYER_H_
#define _T_R15_NETWORK_LAYER_H_
/**
 *  @addtogroup ldr_communication_serv
 *  @{
 *    @addtogroup r15_family
 *    @{
 *      @addtogroup ldr_r15_network_layer
 *      @{
 */
/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "t_communication_service.h"
#include "t_critical_section.h"
#include "t_r15_header.h"
#include "t_time_utilities.h"

/*******************************************************************************
 * Types, constants
 ******************************************************************************/

/** PROTROM protocol ID*/
#define PROTROM_PROTOCOL        (0xFB)
/** COMMAND protocol ID*/
#define COMMAND_PROTOCOL        (0xBC)
/** BULK protocol ID*/
#define BULK_PROTOCOL        (0xBD)
/** State: Command packet */
#define COMMAND_PACKAGE        (0x0000)
/** State: Acknowledge packet */
#define ACK_PACKAGE             (0x0001)
/** State: General response */
#define GENERAL_RESPONSE_PACKAGE (0x0002)
/** State: Acknowledge for general response */
#define ACK_GENERAL_RESPONSE_PACKAGE (0x0003)
/**
 *  Maximum numbers of used process in transport layer
 */
#define MAX_SIZE_RETRANSMISSION_LIST  32
/** Amount of resends of the same package (until it aborts) */
#define MAX_RESENDS             (0x03)


#ifndef  CFG_ENABLE_LOADER_TYPE
/** Acknowledge packet time(mS) out value */
#define ACK_TIMEOUT_IN_MS       10000
/** Default time(mS) out for receiving bulk command */
#define BULK_COMMAND_RECEIVING_TIMEOUT  10000
/** Default time(mS) out for receiving bulk data. This value is set for UART on 115200kbps! */
#define BULK_DATA_RECEIVING_TIMEOUT   120000

#else
/** Acknowledge packet time(mS) out value */
#define ACK_TIMEOUT_IN_MS       (5000)
/** Default time(mS) out for receiving bulk command */
#define BULK_COMMAND_RECEIVING_TIMEOUT  (5000)
/** Default time(mS) out for receiving bulk data. This value is set for UART on 115200kbps! */
#define BULK_DATA_RECEIVING_TIMEOUT   (120000)
#endif

/** Number of pre-allocated buffers */
#define COMMAND_BUFFER_COUNT 16
/** Number of pre-allocated buffers */
#define BULK_BUFFER_COUNT 16

/** Size of a buffer used for commands. */
#define COMMAND_BUFFER_SIZE 0x00010000

/** Size of a buffer used for bulk transfer,
 * must be biger than buffer for commands. */
#define BULK_BUFFER_SIZE 0x00100000


/** Mask for clearing the state bits in session/state field */
#define MASK_CLR_STATE    (0xFFFC)
/** Mask for selecting the state bits in session/state field */
#define MASK_SELECT_STATE    (0x0003)
/** Mask for selecting the session number in session/state field */
#define MASK_SELECT_SESSION_NUMBER  (0x3FFF)
/** Mask for selecting session bits */
#define SESSION_MASK 0xFFFC

/**
 * Defines all posible types of buffers that can be created (allocated).
 *
 * It is used to specified the type of the buffer
 * when allocating a new buffer. Also it is used when
 * requesting the number of buffers to specify the buffer type.
 */
typedef enum {
    UNDEFINED_BUFFER = 0, /** Buffer with no specified type.*/
    COMMAND_BUFFER   = 1, /** Command buffer type, used for commands.*/
    BULK_BUFFER      = 2 /** Bulk buffer type, used for bulk transfer.*/
} BuffersType_t;

/** The enum for buffer Flags values. */
TYPEDEF_ENUM {
    BUF_FREE            = 0x00000000,        /**< No buffer activity */
    BUF_ALLOCATED       = 0x00000001,        /**< The buffer is allocated. */
    BUF_TX_READY        = 0x00000002,        /**< The buffer is filled with data
                                                and is ready to be sent. */
    BUF_TX_SENDING      = 0x00000004,        /**< The buffer is sending. */
    BUF_TX_SENT         = 0x00000008,        /**< The buffer is sent and wait ACK. */
    BUF_TX_DONE         = 0x00000010,        /**< The buffer has been sent and can
                                                be deallocated. */
    BUF_TX_TIMEOUT      = 0x00000020,        /**< The timeout ocure when buffer is
                                                sending. */
    BUF_RX_READY        = 0x00000040,        /**< The buffer is filled with
                                                received data and is ready for
                                                processing. */
    BUF_HDR_CRC_OK      = 0x00000080,        /**< Set to true if the CRC has been
                                                calculated. */
    BUF_PAYLOAD_CRC_OK  = 0x00000100,        /**< Set to true if the CRC has been
                                                calculated and is correct. */
    BUF_ACK_READY       = 0x00000200,        /**< Set the buffer ready for
                                                acknowledge. */
    BUF_ACKNOWLEDGED           = 0x00000400, /**< Set if the buffer is
                                                acknowledged. */
    BUF_CRC_CALC_READY         = 0x00000800, /**< Set if the buffer is ready for
                                                payload CRC calculating */
    BUF_CRC_CALCULATING        = 0x00001000, /**< Set if the calculating of
                                                payload CRC is started */
    BUF_HEADER_CRC_CALCULATED  = 0x00002000, /**< Set if the header CRC is
                                                calculated */
    BUF_PAYLOAD_CRC_CALCULATED = 0x00004000, /**< Set if the payload CRC is
                                                calculated */
} ENUM32(PacketFlags_t);

/** Flags Masks **/
#define PACKET_ALLOCATION_STATE_MASK (BUF_FREE | BUF_ALLOCATED)
#define PACKET_TX_STATE_MASK         (BUF_TX_READY | BUF_TX_SENDING | BUF_TX_SENT | BUF_TX_DONE | BUF_TX_TIMEOUT)
#define PACKET_RX_STATE_MASK         (BUF_RX_READY | BUF_HDR_CRC_OK | BUF_PAYLOAD_CRC_OK | BUF_ACK_READY | BUF_ACKNOWLEDGED)
#define PACKET_CRC_STATE_MASK        (BUF_CRC_CALC_READY | BUF_CRC_CALCULATING | BUF_PAYLOAD_CRC_CALCULATED | BUF_HEADER_CRC_CALCULATED)

/** Macro for setting a packet state **/
#define SET_PACKET_FLAGS(packet, mask, flags) \
  do \
  { \
    (packet)->Flags &= ~(mask); \
    (packet)->Flags |= (mask & flags); \
  } while (0)

#define CHECK_PACKET_FLAGS(packet, flags) (((flags) == ((packet)->Flags & (flags))) ? TRUE : FALSE)

/** Defined state of the receiver */
typedef enum {
    RECEIVE_HEADER,          /**< State for receiving Header.*/
    RECEIVE_EXTENDED_HEADER, /**< State for receiving Extended Header.*/
    RECEIVE_PAYLOAD,         /**< State for receiving Payload.*/
    RECEIVE_ERROR,           /**< State for error handling.*/
    RECEIVE_IDLE             /**< State for receiver idle.*/
} R15_InboundState_t;

/** Defined state of the transmitter */
typedef enum {
    SEND_IDLE,       /**< Transmiter idle state.*/
    SEND_HEADER,     /**< Transmiter send header and extended header. */
    SENDING_HEADER,  /**< Transmiter is in process sending the header and extended header. */
    SEND_PAYLOAD,    /**< Transmiter send payload. */
    SENDING_PAYLOAD  /**< Transmiter is in process sending payload.*/
} R15_OutboundState_t;

/**
 * Command buffer used for sending and receiving commands.
 */
typedef struct {
    uint32 Id;                           /**< The buffer ID. NOTE: Currently is
                                            not used */
    uint32 Canary0;                      /**< A canary value. NOTE: Currently is
                                            not used */
    uint8  Payload[COMMAND_BUFFER_SIZE]; /**< The buffer containing the raw packet
                                            data. NOTE! The buffer includes all
                                            the data that is sent
                                            (header, data and CRC) */
    uint32 Canary1;                      /**< A canary value. NOTE: Currently is
                                            not used */
} CommandBuffer_t;

/** Structure for the bulk buffer type. */
typedef struct {
    uint32 Id;                          /**< The buffer ID. NOTE: Currently is
                                           not used */
    uint32 Canary0;                     /**< A canary value. NOTE: Currently is
                                           not used */
    uint8  Payload[BULK_BUFFER_SIZE];   /**< The buffer containing the raw packet
                                           data. NOTE! The buffer includes all
                                           the data that is sent
                                           (header, data and CRC) */
    uint32 Canary1;                     /**< A Canary value. NOTE: Currently is
                                           not used */
} BulkBuffer_t;

typedef void (*PacketCallBack_t)(Communication_t *Communication_p, const void *Data_p);

/** Structure for the packet meta data. */
typedef struct PacketMeta {
    PacketCallBack_t CallBack_p;                  /**< Cllback function used after
                                                   sending packet.*/
    uint32           Flags;                       /**< Field is a bit-field. Flags
                                                   for the Packet state. */
    int              BufferSize;                  /**< Field contains a Size of the
                                                   corresponding buffer used in
                                                   the packet. */
    R15_Header_t     Header;                      /**< Structure of the header. */
    uint32           Resend;                      /**< Resend counter. */
    Timer_t          *Timer_p;                    /**< Timer data used for
                                                   sending/reciving packet. */
    uint8            *ExtendedHeader_p;           /**< Pointer to the extended
                                                   header located in the packet. */
    uint8            *Payload_p;                  /**< Pointer to the payload data
                                                   located in the packet. */
    uint8            *Buffer_p;                   /**< Field contains a pointer to
                                                   the corresponding (type)
                                                   buffer. */
    uint32           Canary;                      /**< Field contains the master
                                                   Canary value. NOTE: Currently
                                                   is not used */
    Communication_t  *Communication_p;            /**< The communication over which
                                                   this packet has been/is to be
                                                   transferred over. */
    uint8            Hash[SHA_256_HASH_LENGTH];   /**< Field contain calculated hash
                                                   for payload. */
} PacketMeta_t;

/** Retransmission context. */
typedef struct {
    uint32       Timeout;   /**< Defined timeout for retransmission. */
    uint32       TimerKey;  /**< Timer identification number.*/
    uint32       Key;       /**< Generated unique key, used for marking packet for
                               retransmission or removing from
                              retransmission list. */
    PacketMeta_t *Packet_p; /**< Pointer to the packet for retransmission. */
} RetransmissionContext_t;


/** Structure for handling incoming R15 packets.*/
typedef struct {
    /**< State of the state machine for handling incoming R15 packets. */
    R15_InboundState_t State;
    /**< Number of requested data for receiving from communication device. */
    uint32             ReqData;
    /**< Number of receivied data from communication device. */
    uint32             RecData;
    /**< Number of receivied data from backup buffer used for switching the
     * protocol family. */
    uint32             RecBackupData;
    /**< Offset in the buffer for next data that should be received. */
    uint32             ReqBuffOffset;
    /**< Temporary pointer to buffer for handling received data.*/
    uint8              *Target_p;
    /**< Temporary buffer for receiving data. */
    uint8              Scratch[ALIGNED_HEADER_LENGTH + ALIGNED_BULK_EXTENDED_HEADER_LENGTH];
    /** Temporary structure for handling R15 packet.*/
    R15_Header_t       Header;
    /** Poiter to meta data for allocated buffer for handling R15 packet.*/
    PacketMeta_t       *Packet_p;
    /** Number of packets before receiver is stoped. */
    uint8               PacketsBeforeReceiverStop;
    /** Indicator for stopping the receiver. */
    boolean             StopTransfer;
    /** Error flag, purposed for error handling.*/
    ErrorCode_e         LCM_Error;
} R15_Inbound_t;


/** Structure for handling outgoing R15 packets.*/
typedef struct {
    /**< State of the state machine for handling outgoing R15 packets. */
    R15_OutboundState_t State;
    /** Temporary pointer for handling PROTROM packet.*/
    PacketMeta_t        *Packet_p;
    /**< Synchronization object to avoid parallel access in transmitter function. */
    CriticalSection_t   TxCriticalSection;
    /** Error flag, purposed for error handling.*/
    ErrorCode_e         LCM_Error;
} R15_Outbound_t;


/** R15 Network context */
typedef struct {
    PacketMeta_t            *MetaInfoList[COMMAND_BUFFER_COUNT+BULK_BUFFER_COUNT];
    /**< List of meta data for used packets. */
    RetransmissionContext_t *RetransmissionList[MAX_SIZE_RETRANSMISSION_LIST];
    /**< List of packet for retransmision */
    uint32                   RetransmissionListCount;
    /**< Counter for packets retransmision */
    R15_Inbound_t            Inbound;
    /**< Context for receiver. */
    R15_Outbound_t           Outbound;
    /**< Context for transmiter. */
} R15_NetworkContext_t;

/*
 * XVSZOAN @TODO:
 *
 * This structure is only for UART Process File fix.
 * When we start using ESB block for data verification
 * structure should be deleted!
 */
typedef struct {
    const uint8 *Data_Packet_p;
    uint32       Packet_Size;
    uint32       UART_Device;
    boolean      New_Packed_Arrived;
} DataBox_t;

/** @} */
/** @} */
/** @} */
#endif /*_T_R15_NETWORK_LAYER_H_*/