summaryrefslogtreecommitdiff
path: root/lcmodule/source/cnh1605205_ldr_network_layer/source/protrom_network.c
blob: 3dc19a48cef563ce1f1380025e78b02a52a28057 (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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/
/**
 *  @addtogroup ldr_communication_serv
 *  @{
 *    @addtogroup protrom_family
 *    @{
 *      @addtogroup ldr_network_layer
 *      @{
 */

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "c_system.h"
#include "t_basicdefinitions.h"
#include "r_protrom_family.h"
#include "r_protrom_transport.h"
#include "r_protrom_network.h"
#include "r_protrom_protocol.h"
#include "r_protrom_header.h"
#include "r_communication_service.h"
#include "r_debug.h"
#include "r_debug_macro.h"
#include "r_critical_section.h"

#ifdef  WIN32
#include <windows.h>
#endif

/*******************************************************************************
 * Declaration of file local functions
 ******************************************************************************/
static ErrorCode_e Protrom_Network_ReceiveHeader(Communication_t *Communication_p);
static ErrorCode_e Protrom_Network_ReceivePayload(Communication_t *Communication_p);
static ErrorCode_e Protrom_Network_TransmiterHandler(Communication_t *Communication_p);
static void Protrom_InHashCallback(void *Data_p, uint32 Length, const uint8 *const Hash_p, void *Param_p);
static void Protrom_QueueCallback(const void *const Queue_p, void *Param_p);

/*******************************************************************************
 * File scope types, constants and variables
 ******************************************************************************/
#define PROTROM_RESET_INBOUND(c, s) do { (c)->ReqData = 0; (c)->RecData = 0; (c)->ReqBuffOffset = 0; (c)->Target_p = NULL; (c)->State = (s); } while(0);
#define PROTROM_SYNC_HEADER(c, d, t) do { (c)->ReqData = (d); (c)->Target_p = (t); } while(0);
#define PROTROM_SET_INBOUND(c, s, d) do { (c)->ReqData = (d); (c)->RecData = 0; (c)->ReqBuffOffset = 0; (c)->State = (s); } while(0);

/*******************************************************************************
 * Definition of external functions
 ******************************************************************************/

/*
 * Initializes the PROTOROM network layer.
 *
 * @param [in]  Communication_p Communication module context.
 *
 * @retval  E_SUCCESS                   After successful execution.
 * @retval  E_FAILED_TO_INIT_COM_DEVICE Failed to initialize the communication
 *                                      device.
 */
ErrorCode_e Protrom_Network_Initialize(Communication_t *Communication_p)
{
    memset(PROTROM_NETWORK(Communication_p), 0, sizeof(Protrom_NetworkContext_t));
    PROTROM_NETWORK(Communication_p)->Outbound.TxCriticalSection = Do_CriticalSection_Create();

    /* Simulate a finished read to get the inbound state-machine going. */
    Protrom_Network_ReadCallback(NULL, 0, Communication_p->CommunicationDevice_p);
    (void)QUEUE(Communication_p, Fifo_SetCallback_Fn)(OBJECT_QUEUE(Communication_p), Communication_p->Outbound_p, QUEUE_NONEMPTY, Protrom_QueueCallback, Communication_p);

    return E_SUCCESS;
}

/*
 * Shutdown the PROTROM network layer.
 *
 * @param [in]  Communication_p Communication module context.
 *
 * @retval  E_SUCCESS After successful execution.
 */
ErrorCode_e Protrom_Network_Shutdown(const Communication_t *const Communication_p)
{
    Protrom_Inbound_t *In_p = &(PROTROM_NETWORK(Communication_p)->Inbound);

    if (NULL != In_p->Packet_p) {
        if (NULL != In_p->Packet_p->Buffer_p) {
            free(In_p->Packet_p->Buffer_p);
        }

        free(In_p->Packet_p);
        In_p->Packet_p = NULL;
    }

    (void)QUEUE(Communication_p, Fifo_SetCallback_Fn)(OBJECT_QUEUE(Communication_p), Communication_p->Outbound_p, QUEUE_NONEMPTY, NULL, NULL);
    (void)QUEUE(Communication_p, Fifo_SetCallback_Fn)(OBJECT_QUEUE(Communication_p), Communication_p->Outbound_p, QUEUE_EMPTY, NULL, NULL);

    (void)QUEUE(Communication_p, Fifo_SetCallback_Fn)(OBJECT_QUEUE(Communication_p), Communication_p->Inbound_p, QUEUE_NONEMPTY, NULL, NULL);
    (void)QUEUE(Communication_p, Fifo_SetCallback_Fn)(OBJECT_QUEUE(Communication_p), Communication_p->Inbound_p, QUEUE_EMPTY, NULL, NULL);

    Do_CriticalSection_Destroy(&(PROTROM_NETWORK(Communication_p)->Outbound.TxCriticalSection));

    return E_SUCCESS;
}

/*
 * This callback function handles the received packets.
 *
 * @param [in] Data_p  Pointer to the received data.
 * @param [in] Length  Length of the received data.
 * @param [in] Param_p Extra parameters.
 *
 * @return none.
 */
void Protrom_Network_ReadCallback(const void *Data_p, const uint32 Length, void *Param_p)
{
    Communication_t *Communication_p = (Communication_t *)(((CommunicationDevice_t *)Param_p)->Object_p);

    A_(printf("protrom_family.c (%d) RecLength(%d) RecBackupData (%d)\n", __LINE__, Length, PROTROM_NETWORK(Communication_p)->Inbound.RecBackupData);)
    PROTROM_NETWORK(Communication_p)->Inbound.RecData = Length + PROTROM_NETWORK(Communication_p)->Inbound.RecBackupData;
    PROTROM_NETWORK(Communication_p)->Inbound.RecBackupData = 0;

    if (PROTROM_NETWORK(Communication_p)->Inbound.ReqData == 0) {
        ErrorCode_e ReturnValue = E_GENERAL_COMMUNICATION_ERROR;

        if (PROTROM_NETWORK(Communication_p)->Inbound.StopTransfer) {
            if (PROTROM_NETWORK(Communication_p)->Inbound.PacketsBeforeReceiverStop) {
                PROTROM_NETWORK(Communication_p)->Inbound.PacketsBeforeReceiverStop--;
            } else {
                PROTROM_NETWORK(Communication_p)->Inbound.State = PROTROM_RECEIVE_IDLE;
            }
        }

        switch (PROTROM_NETWORK(Communication_p)->Inbound.State) {

        case PROTROM_RECEIVE_IDLE:
            ReturnValue = E_SUCCESS;
            break;

        case PROTROM_RECEIVE_HEADER:
            ReturnValue = Protrom_Network_ReceiveHeader(Communication_p);
            break;

        case PROTROM_RECEIVE_PAYLOAD:
            ReturnValue = Protrom_Network_ReceivePayload(Communication_p);

            if ((PROTROM_NETWORK(Communication_p)->Inbound.StopTransfer) && (0 == PROTROM_NETWORK(Communication_p)->Inbound.PacketsBeforeReceiverStop)) {
                PROTROM_NETWORK(Communication_p)->Inbound.ReqData = 0;
            }

            break;

        default:
            PROTROM_NETWORK(Communication_p)->Inbound.State = PROTROM_RECEIVE_HEADER;
            PROTROM_NETWORK(Communication_p)->Inbound.RecData = 0;
            PROTROM_NETWORK(Communication_p)->Inbound.ReqData = PROTROM_HEADER_LENGTH;
            PROTROM_NETWORK(Communication_p)->Inbound.Target_p = PROTROM_NETWORK(Communication_p)->Inbound.Scratch;
            return;
        }

        if (E_SUCCESS != ReturnValue) {
            PROTROM_NETWORK(Communication_p)->Inbound.State = PROTROM_RECEIVE_ERROR;
            return;
        }
    }
}

/*
 * This function checks if new data has been received.
 *
 * @param [in]  Communication_p Communication module context.
 *
 * @return  none.
 */
void Protrom_Network_ReceiverHandler(Communication_t *Communication_p)
{
    uint32 ReqData;
    uint32 ReqBufferOffset;
    Protrom_Inbound_t *In_p = &(PROTROM_NETWORK(Communication_p)->Inbound);

    /* new data for receiving ? */
    if (In_p->ReqData > 0) {
        if (Communication_p->BackupCommBufferSize != 0) {
            if (Communication_p->BackupCommBufferSize < In_p->ReqData) {
                memcpy(In_p->Target_p + In_p->ReqBuffOffset, Communication_p->BackupCommBuffer_p, Communication_p->BackupCommBufferSize);
                ReqData = In_p->ReqData;
                In_p->ReqData = 0;
                ReqBufferOffset = In_p->ReqBuffOffset;
                In_p->ReqBuffOffset = 0;
                In_p->RecBackupData = Communication_p->BackupCommBufferSize;
                Communication_p->BackupCommBufferSize = 0;
                In_p->RecData = 0;

                C_(printf("protrom_network.c (%d) ReqData(%d) RecData(%d)\n", __LINE__, ReqData, In_p->RecData);)
                C_(printf("protrom_network.c (%d) Communication_p->BackupCommBufferSize(%d) RecBackupData (%d)\n", __LINE__, Communication_p->BackupCommBufferSize, In_p->RecBackupData);)

#ifdef CFG_ENABLE_LOADER_TYPE

                if (E_SUCCESS != Communication_p->CommunicationDevice_p->Read(
                            In_p->Target_p + ReqBufferOffset + In_p->RecBackupData,
                            ReqData - In_p->RecBackupData, Protrom_Network_ReadCallback,
                            Communication_p->CommunicationDevice_p)) {
                    /* Read failed! Return to previous state. */
                    In_p->ReqData = ReqData;
                    In_p->ReqBuffOffset = ReqBufferOffset;
                    Communication_p->BackupCommBufferSize = In_p->RecBackupData;
                    In_p->RecBackupData = 0;
                }

#else
                (void)Communication_p->CommunicationDevice_p->Read(
                    In_p->Target_p + ReqBufferOffset + In_p->RecBackupData,
                    ReqData - In_p->RecBackupData, Protrom_Network_ReadCallback,
                    Communication_p->CommunicationDevice_p);
#endif
            } else {
                /* Copy content of backup buffer into receive buffer */
                memcpy(In_p->Target_p + In_p->ReqBuffOffset, Communication_p->BackupCommBuffer_p, In_p->ReqData);
                /* Move rest of backup data at the beginning of the backup buffer */
                memcpy(Communication_p->BackupCommBuffer_p, Communication_p->BackupCommBuffer_p + In_p->ReqData, Communication_p->BackupCommBufferSize - In_p->ReqData);
                /* Update the size of the backup buffer */
                Communication_p->BackupCommBufferSize = Communication_p->BackupCommBufferSize - In_p->ReqData;

                ReqData = In_p->ReqData;
                In_p->ReqData = 0;
                ReqBufferOffset = In_p->ReqBuffOffset;
                In_p->ReqBuffOffset = 0;
                In_p->RecData = 0;
                Protrom_Network_ReadCallback(In_p->Target_p + ReqBufferOffset, ReqData, Communication_p->CommunicationDevice_p);
            }
        } else {
            ReqData = In_p->ReqData;
            In_p->ReqData = 0;
            ReqBufferOffset = In_p->ReqBuffOffset;
            In_p->ReqBuffOffset = 0;
            In_p->RecData = 0;
            C_(printf("protrom_network.c (%d) ReqData(%d) RecData(%d) \n", __LINE__, ReqData, In_p->RecData);)
            C_(printf("protrom_network.c (%d) Communication_p->BackupCommBufferSize(%d) RecBackupData (%d)\n", __LINE__, Communication_p->BackupCommBufferSize, In_p->RecBackupData);)

#ifdef CFG_ENABLE_LOADER_TYPE

            if (E_SUCCESS != Communication_p->CommunicationDevice_p->Read(
                        In_p->Target_p + ReqBufferOffset, ReqData, Protrom_Network_ReadCallback,
                        Communication_p->CommunicationDevice_p)) {
                /* Read failed! Return to previous state. */
                In_p->ReqData = ReqData;
                In_p->ReqBuffOffset = ReqBufferOffset;
            }

#else
            (void)Communication_p->CommunicationDevice_p->Read(
                In_p->Target_p + ReqBufferOffset, ReqData, Protrom_Network_ReadCallback,
                Communication_p->CommunicationDevice_p);
#endif
        }
    }

    /* check for receiver synchronization */
    if (In_p->State == PROTROM_RECEIVE_ERROR) {
        In_p->ReqData = 0;
        In_p->RecData = 0;
        In_p->ReqBuffOffset = 0;
#ifdef CFG_ENABLE_LOADER_TYPE

        if (E_SUCCESS == Communication_p->CommunicationDevice_p->Read(In_p->Target_p,
                PROTROM_HEADER_LENGTH, Protrom_Network_ReadCallback,
                Communication_p->CommunicationDevice_p)) {
            In_p->State = PROTROM_RECEIVE_HEADER;
        }

#else
        (void)Communication_p->CommunicationDevice_p->Read(In_p->Target_p,
                PROTROM_HEADER_LENGTH, Protrom_Network_ReadCallback,
                Communication_p->CommunicationDevice_p);
        In_p->State = PROTROM_RECEIVE_HEADER;
#endif
    }
}

/*
 * Handler function that is called after successful transmission
 * of a packet.
 *
 * If new packet is ready for transmitting it starts
 * the transmission of the packet.
 *
 * @param [in] Data_p  Pointer to the data for transmitting.
 * @param [in] Length  Length of the received data.
 * @param [in] Param_p Extra parameters.
 *
 * @return none.
 */
void Protrom_Network_WriteCallback(const void *Data_p, const uint32 Length, void *Param_p)
{
    Communication_t *Communication_p = (Communication_t *)(((CommunicationDevice_t *)Param_p)->Object_p);
    Protrom_Outbound_t *Out_p = &(PROTROM_NETWORK(Communication_p)->Outbound);

    if (Out_p->State == PROTROM_SENDING_PAYLOAD) {
        if (NULL != Out_p->Packet_p) {
            if (NULL != Out_p->Packet_p->Buffer_p) {
                free(Out_p->Packet_p->Buffer_p);
            }

            free(Out_p->Packet_p);
            Out_p->Packet_p = NULL;
        }

        Out_p->State = PROTROM_SEND_IDLE;
    } else if (Out_p->State == PROTROM_SENDING_HEADER) {
        Out_p->State = PROTROM_SEND_PAYLOAD;
    }

    /* check for more stuff to send. */
    (void)Protrom_Network_TransmiterHandler(Communication_p);
}


/*******************************************************************************
 * Definition of internal functions
 ******************************************************************************/

static ErrorCode_e Protrom_Network_ReceiveHeader(Communication_t *Communication_p)
{
    Protrom_Inbound_t *In_p = &(PROTROM_NETWORK(Communication_p)->Inbound);

    if (In_p->RecData == 0) {
        In_p->ReqData = PROTROM_HEADER_LENGTH;
        In_p->Target_p = In_p->Scratch;
        In_p->ReqBuffOffset = 0;
    } else {
        if (Protrom_IsReceivedHeader(In_p)) {
            if (Protrom_IsValidHeader(In_p->Scratch)) {
                In_p->Packet_p = (Protrom_Packet_t *)malloc(sizeof(Protrom_Packet_t));

                if (NULL == In_p->Packet_p) {
                    return E_ALLOCATE_FAILED;
                }

                In_p->Packet_p->Communication_p = Communication_p;
                Protrom_DeserializeHeader(&In_p->Packet_p->Header, In_p->Scratch);
                In_p->Packet_p->Buffer_p = (uint8 *)malloc(In_p->Packet_p->Header.PayloadLength + PROTROM_HEADER_LENGTH + PROTROM_CRC_LENGTH);

                if (NULL == In_p->Packet_p->Buffer_p) {
                    return E_ALLOCATE_FAILED;
                }

                In_p->Target_p = In_p->Packet_p->Buffer_p;
                memcpy(In_p->Target_p, In_p->Scratch, PROTROM_HEADER_LENGTH);
                In_p->Target_p += PROTROM_HEADER_LENGTH;
                PROTROM_SET_INBOUND(In_p, PROTROM_RECEIVE_PAYLOAD, In_p->Packet_p->Header.PayloadLength + PROTROM_CRC_LENGTH);
            } else {
                /* Sync the header. */
                PROTROM_RESET_INBOUND(In_p, PROTROM_RECEIVE_HEADER);
                PROTROM_SYNC_HEADER(In_p, PROTROM_HEADER_LENGTH, In_p->Scratch);
            }
        }
    }

    return E_SUCCESS;
}


static ErrorCode_e Protrom_Network_ReceivePayload(Communication_t *Communication_p)
{
    Protrom_Inbound_t *In_p = &(PROTROM_NETWORK(Communication_p)->Inbound);
    Protrom_Packet_t *Packet_p = In_p->Packet_p;

    Packet_p->Communication_p = Communication_p;
    Communication_p->HashDevice_p->Calculate(OBJECT_HASH(Communication_p),
            Communication_p->CurrentFamilyHash, Packet_p->Buffer_p,
            Packet_p->Header.PayloadLength + PROTROM_HEADER_LENGTH,
            (uint8 *)&Packet_p->CRC, (HashCallback_t)Protrom_InHashCallback,
            (void *)Packet_p);

    In_p->Packet_p = NULL;
    PROTROM_RESET_INBOUND(In_p, PROTROM_RECEIVE_HEADER);
    PROTROM_SYNC_HEADER(In_p, PROTROM_HEADER_LENGTH, In_p->Scratch);
    return E_SUCCESS;
}


static ErrorCode_e Protrom_Network_TransmiterHandler(Communication_t *Communication_p)
{
    volatile Protrom_Outbound_t *Out_p = &(PROTROM_NETWORK(Communication_p)->Outbound);

    if (!Do_CriticalSection_Enter(Out_p->TxCriticalSection)) {
        return E_SUCCESS;
    }

    switch (Out_p->State) {
    case PROTROM_SEND_IDLE:
        Out_p->Packet_p = (Protrom_Packet_t *)QUEUE(Communication_p, FifoDequeue_Fn)(OBJECT_QUEUE(Communication_p), Communication_p->Outbound_p);

        if (NULL != Out_p->Packet_p) {
            /* get next packet for transmiting */
            Out_p->State = PROTROM_SEND_HEADER;
        } else {
            break;
        }

        /* FALLTHROUGH */
    case PROTROM_SEND_HEADER:
        Out_p->State = PROTROM_SENDING_HEADER;

        if (E_SUCCESS != Communication_p->CommunicationDevice_p->Write(Out_p->Packet_p->Buffer_p,
                PROTROM_HEADER_LENGTH, Protrom_Network_WriteCallback, Communication_p->CommunicationDevice_p)) {
            Out_p->State = PROTROM_SEND_HEADER;
            break;
        }

    case PROTROM_SENDING_HEADER:
        break;
    case PROTROM_SEND_PAYLOAD:
        Out_p->State = PROTROM_SENDING_PAYLOAD;

        if (E_SUCCESS != Communication_p->CommunicationDevice_p->Write(Out_p->Packet_p->Buffer_p + PROTROM_HEADER_LENGTH,
                Out_p->Packet_p->Header.PayloadLength + PROTROM_CRC_LENGTH,
                Protrom_Network_WriteCallback, Communication_p->CommunicationDevice_p)) {
            Out_p->State = PROTROM_SEND_PAYLOAD;
            break;
        }

        /* FALLTHROUGH */
    case PROTROM_SENDING_PAYLOAD:
        break;

    }

    Do_CriticalSection_Leave(Out_p->TxCriticalSection);

    return E_SUCCESS;
}

static void Protrom_QueueCallback(const void *const Queue_p, void *Param_p)
{
    (void)Protrom_Network_TransmiterHandler((Communication_t *)Param_p);
}

static void Protrom_InHashCallback(void *Data_p, uint32 Length, const uint8 *const Hash_p, void *Param_p)
{
    Protrom_Packet_t *Packet_p = (Protrom_Packet_t *)Param_p;

    if (memcmp(Hash_p, Packet_p->Buffer_p + PROTROM_HEADER_LENGTH + Packet_p->Header.PayloadLength, sizeof(uint16)) == 0) {
        (void)QUEUE(Packet_p->Communication_p, FifoEnqueue_Fn)(OBJECT_QUEUE(Packet_p->Communication_p), Packet_p->Communication_p->Inbound_p, Packet_p);
    } else {
        /* Invalid packet */
        free(Packet_p->Buffer_p);
        free(Packet_p);
    }
}

/** @} */
/** @} */
/** @} */