summaryrefslogtreecommitdiff
path: root/lcmodule/source/cnh1606344_ldr_communication_module/source/r15_family.c
blob: 29be578902858e065eb5215dbb9eba52203fd2c1 (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
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/
/**
 * @addtogroup ldr_communication_serv
 * @{
 */

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "r_r15_family.h"
#include "r_r15_transport_layer.h"
#include "r_r15_network_layer.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "r_debug_macro.h"
/*******************************************************************************
 * File scope types, constants and variables
 ******************************************************************************/

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

/*
 * R15 family protocols initialization.
 *
 * @param [in] Communication_p Communication module context.
 *
 * @retval  E_SUCCESS                   After successful execution.
 * @retval  E_INVALID_INPUT_PARAMETERS  Invalid input parameter.
 */
ErrorCode_e R15_Family_Init(Communication_t *Communication_p)
{
    ErrorCode_e ReturnValue = E_INVALID_INPUT_PARAMETERS;

    VERIFY(NULL != Communication_p, E_INVALID_INPUT_PARAMETERS);

    Communication_p->FamilyContext_p = malloc(sizeof(R15_FamilyContext_t));
    VERIFY(NULL != Communication_p->FamilyContext_p, E_ALLOCATE_FAILED);

#ifndef CFG_ENABLE_LOADER_TYPE
    Communication_p->CurrentFamilyHash = HASH_NONE;
#else

#ifdef SKIP_PAYLOAD_VERIFICATION
    Communication_p->CurrentFamilyHash = HASH_NONE;
#else
    Communication_p->CurrentFamilyHash = HASH_SHA256;
#endif

#endif

    R15_NETWORK(Communication_p)->Inbound.StopTransfer = FALSE;

    if (NULL != Communication_p->BackupCommBuffer_p) {
        if (Communication_p->BackupCommBufferSize < ALIGNED_HEADER_LENGTH) {
            memcpy(R15_NETWORK(Communication_p)->Inbound.Scratch, Communication_p->BackupCommBuffer_p, Communication_p->BackupCommBufferSize);
            R15_NETWORK(Communication_p)->Inbound.ReqData = ALIGNED_HEADER_LENGTH - Communication_p->BackupCommBufferSize;
            R15_NETWORK(Communication_p)->Inbound.ReqBuffOffset = Communication_p->BackupCommBufferSize;
            R15_NETWORK(Communication_p)->Inbound.RecBackupData = Communication_p->BackupCommBufferSize;
            Communication_p->BackupCommBufferSize = 0;
        } else {
            /* Copy content of backup buffer into scratch buffer */
            memcpy(R15_NETWORK(Communication_p)->Inbound.Scratch, Communication_p->BackupCommBuffer_p, ALIGNED_HEADER_LENGTH);
            /* Move rest of backup data at the beginning of the backup buffer */
            memcpy(Communication_p->BackupCommBuffer_p, Communication_p->BackupCommBuffer_p + ALIGNED_HEADER_LENGTH, Communication_p->BackupCommBufferSize - ALIGNED_HEADER_LENGTH);
            /* Update the size of the backup buffer */
            Communication_p->BackupCommBufferSize = Communication_p->BackupCommBufferSize - ALIGNED_HEADER_LENGTH;
            R15_NETWORK(Communication_p)->Inbound.RecBackupData = ALIGNED_HEADER_LENGTH;
        }

        C_(printf("r15_family.c(%d) BackupBuffer=0x%x Size=%d\n", __LINE__, Communication_p->BackupCommBuffer_p, Communication_p->BackupCommBufferSize);)
        R15_NETWORK(Communication_p)->Inbound.State = RECEIVE_HEADER;
        R15_NETWORK(Communication_p)->Inbound.Target_p = R15_NETWORK(Communication_p)->Inbound.Scratch;
        R15_NETWORK(Communication_p)->Inbound.LCM_Error = E_SUCCESS;
    } else {
        Communication_p->BackupCommBufferSize = 0;
    }

    ReturnValue = R15_Transport_Initialize(Communication_p);
    VERIFY(E_SUCCESS == ReturnValue, ReturnValue);

    ReturnValue = R15_Network_Initialize(Communication_p);

ErrorExit:
    A_(printf("r15_family.c(%d) ErrorCode=%d\n", __LINE__, ReturnValue);)
    return ReturnValue;
}


/*
 * R15 family protocols shutdown.
 *
 * @param [in] Communication_p Communication module context.
 *
 * @retval  E_SUCCESS                   After successful execution.
 * @retval  E_INVALID_INPUT_PARAMETERS  Invalid input parameter.
 */
ErrorCode_e R15_Family_Shutdown(Communication_t *Communication_p)
{
    ErrorCode_e ReturnValue = E_INVALID_INPUT_PARAMETERS;

    VERIFY(NULL != Communication_p, E_INVALID_INPUT_PARAMETERS);

    if (NULL != Communication_p->BackupCommBuffer_p) {
        free(Communication_p->BackupCommBuffer_p);
        Communication_p->BackupCommBuffer_p = NULL;
    }

    Communication_p->BackupCommBuffer_p = (uint8 *)malloc(HEADER_LENGTH + BULK_EXTENDED_HEADER_LENGTH);
    VERIFY(NULL != Communication_p->BackupCommBuffer_p, E_ALLOCATE_FAILED);
    memcpy(Communication_p->BackupCommBuffer_p, R15_NETWORK(Communication_p)->Inbound.Scratch, HEADER_LENGTH + BULK_EXTENDED_HEADER_LENGTH);
    Communication_p->BackupCommBufferSize = HEADER_LENGTH + BULK_EXTENDED_HEADER_LENGTH;
    C_(printf("r15_family.c(%d) BackupBuffer=0x%x, Size=%d \n", __LINE__, Communication_p->BackupCommBuffer_p, Communication_p->BackupCommBufferSize);)

    Communication_p->CurrentFamilyHash = HASH_NONE;

    ReturnValue = R15_Transport_Shutdown(Communication_p);
    VERIFY(E_SUCCESS == ReturnValue, ReturnValue);

    ReturnValue = R15_Network_Shutdown(Communication_p);
    VERIFY(E_SUCCESS == ReturnValue, ReturnValue);

ErrorExit:

    if (NULL != Communication_p) {
        if (NULL != Communication_p->FamilyContext_p) {
            free(Communication_p->FamilyContext_p);
            Communication_p->FamilyContext_p = NULL;
        }
    }

    A_(printf("r15_family.c(%d) ErrorCode=%d\n", __LINE__, ReturnValue);)

    return ReturnValue;
}

/*
 * R15 Cancel Transmission.
 *
 * @param [in] Communication_p Communication module context.
 * @param [in] PacketsBeforeTransferStop Number of packets that will be transmitted before stopping the transmission.
 *
 * @retval  E_SUCCESS                   After successful execution.
 * @retval  E_INVALID_INPUT_PARAMETERS  Invalid input parameter.
 */
ErrorCode_e R15_CancelReceiver(Communication_t *Communication_p, uint8 PacketsBeforeReceiverStop)
{
    ErrorCode_e ReturnValue = E_SUCCESS;

    VERIFY(NULL != Communication_p, E_INVALID_INPUT_PARAMETERS);

    R15_NETWORK(Communication_p)->Inbound.PacketsBeforeReceiverStop = PacketsBeforeReceiverStop;
    R15_NETWORK(Communication_p)->Inbound.StopTransfer = TRUE;

ErrorExit:
    A_(printf("protrom_family.c(%d) ErrorCode=%d\n", __LINE__, ReturnValue);)
    return ReturnValue;
}

/** @} */