summaryrefslogtreecommitdiff
path: root/lcmodule/source/cnh1605204_ldr_transport_layer/source/command_protocol.c
blob: 08b48191beeeb85dfc43d66af68669208eb4dbcb (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
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/
/**
 *  @addtogroup ldr_communication_serv
 *  @{
 *    @addtogroup r15_family
 *    @{
 *      @addtogroup command_protocol
 *      @{
 */
/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "t_basicdefinitions.h"
#include "r_command_protocol.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "r_communication_service.h"
#include "r_r15_family.h"
#include "r_r15_network_layer.h"
#include "r_r15_transport_layer.h"
#include "t_r15_header.h"
#include "t_security_algorithms.h"
#include "r_debug.h"
#include "r_debug_macro.h"
#include "r_r15_header.h"

/*******************************************************************************
 * File scope types, constants and variables
 ******************************************************************************/

/*******************************************************************************
 * Declaration of file local functions
 ******************************************************************************/
static ErrorCode_e ProcessAcknowledgement(Communication_t *Communication_p, const PacketMeta_t *Packet_p, const uint16 *const SessionState_p);
static ErrorCode_e ProcessCommand(Communication_t *Communication_p, PacketMeta_t *Packet_p, uint16 *SessionState);
static ErrorCode_e ProcessGeneralResponse(Communication_t *Communication_p, PacketMeta_t *Packet_p, uint16 *SessionState_p);
static ErrorCode_e SendAcknowledge(Communication_t *Communication_p, const PacketMeta_t *const Packet_p);
static uint16 GetSendSession(const Communication_t *const Communication_p, CommandData_t *CmdData_p);
static ErrorCode_e DispatchCommand(Communication_t *Communication_p, PacketMeta_t *Packet_p);

/*******************************************************************************
 * Definition of external functions
 ******************************************************************************/
/*
 * Reset the Session counters.
 *
 * @param [in]  Communication_p Communication module context.
 *
 * @retval  E_SUCCESS After successful execution.
 */
ErrorCode_e Do_R15_Command_ResetSessionCounters(const Communication_t *const Communication_p)
{
    /* Iintialize the session counters */
    R15_TRANSPORT(Communication_p)->SessionStateIn  = 0;
    R15_TRANSPORT(Communication_p)->SessionStateOut = 0;

    return E_SUCCESS;
}


/*
 * Sends command packet with command protocol.
 *
 * @param [in] Communication_p Communication module context.
 * @param [in] CmdData_p       Pointer to the command data.
 *
 * @retval  E_SUCCESS                  After successful execution.
 * @retval  E_INVALID_INPUT_PARAMETERS If CmdData_p is NULL.
 */
ErrorCode_e Do_R15_Command_Send(Communication_t *Communication_p, CommandData_t *CmdData_p)
{
    CommandExtendedHeader_t ExtendedHeader;
    R15_Header_t  Header;
    SendData_LP_t Param;

    if (NULL == CmdData_p) {
        return E_INVALID_INPUT_PARAMETERS;
    }

    Param.Header_p = &Header;
    Param.ExtendedHeader_p = &ExtendedHeader;
    Param.Payload_p = CmdData_p->Payload.Data_p;

    Header.Protocol = COMMAND_PROTOCOL;
    Header.Flags = Communication_p->CurrentFamilyHash;
    Header.PayloadLength = CmdData_p->Payload.Size;
    Header.PayloadChecksum = 0;
    Header.ExtendedHeaderLength = COMMAND_EXTENDED_HEADER_LENGTH;
    Header.ExtendedHeaderChecksum = 0;

    ExtendedHeader.SessionState = GetSendSession(Communication_p, CmdData_p);
    ExtendedHeader.Command = CmdData_p->CommandNr;
    ExtendedHeader.CommandGroup = CmdData_p->ApplicationNr;

    // TODO: Don't we need a timer callback for this to actually do something?
    Param.Time = R15_TIMEOUTS(Communication_p)->TCACK; //ACK_TIMEOUT_IN_MS;
    Param.TimerCallBackFn_p = NULL; //NOTE: the timer will be assigned in "R15_Transport_Send"

    C_(printf("command_protocol.c (%d): R15_Transport_Send! Type:%d Session:%d Command:%d CommandGroup:%d\n", __LINE__, CmdData_p->Type, ExtendedHeader.SessionState, ExtendedHeader.Command, ExtendedHeader.CommandGroup);)

    return R15_Transport_Send(Communication_p, &Param);
}

/*
 * Decode received command.
 *
 * @param [in] Communication_p Communication module context.
 * @param [in] Packet_p        Pointer to the received buffer.
 *
 * @retval E_SUCCESS                  After successful execution.
 * @retval E_INVALID_INPUT_PARAMETERS if Packet_p is NULL.
 */
ErrorCode_e R15_Command_Process(Communication_t *Communication_p, PacketMeta_t *Packet_p)
{
    ErrorCode_e ReturnValue = E_SUCCESS;
    CommandExtendedHeader_t CommandExtendedHeader = {0};

    if (NULL == Packet_p) {
        return E_INVALID_INPUT_PARAMETERS;
    }

    R15_DeserializeCommandExtendedHeader(&CommandExtendedHeader, Packet_p->ExtendedHeader_p);

    switch (CommandExtendedHeader.SessionState & MASK_SELECT_STATE) {
    case COMMAND_PACKAGE:
        ReturnValue = ProcessCommand(Communication_p, Packet_p, &(R15_TRANSPORT(Communication_p)->SessionStateIn));
        break;

    case ACK_PACKAGE:
        ReturnValue = ProcessAcknowledgement(Communication_p, Packet_p, &(R15_TRANSPORT(Communication_p)->SessionStateOut));
        break;

    case GENERAL_RESPONSE_PACKAGE:
        ReturnValue = ProcessGeneralResponse(Communication_p, Packet_p, &(R15_TRANSPORT(Communication_p)->SessionStateOut));
        break;

    case ACK_GENERAL_RESPONSE_PACKAGE:
        ReturnValue = ProcessAcknowledgement(Communication_p, Packet_p, &(R15_TRANSPORT(Communication_p)->SessionStateIn));
        break;

    default:
        ReturnValue = E_SUCCESS;
        break;
    }

    return ReturnValue;
}

/*******************************************************************************
 * Definition of internal functions
 ******************************************************************************/
static uint16 GetSendSession(const Communication_t *const Communication_p, CommandData_t *CmdData_p)
{
    uint16 value;

    if (CmdData_p->Type == GENERAL_RESPONSE) {
        value = CmdData_p->SessionNr << 2 | GENERAL_RESPONSE_PACKAGE;
    } else {
        R15_TRANSPORT(Communication_p)->SessionStateOut += 4;
        value = R15_TRANSPORT(Communication_p)->SessionStateOut;
        CmdData_p->SessionNr = value >> 2;
    }

    return value;
}


static ErrorCode_e ProcessAcknowledgement(Communication_t *Communication_p, const PacketMeta_t *Packet_p, const uint16 *const SessionState_p)
{
    ErrorCode_e ReturnValue = E_GENERAL_FATAL_ERROR;
    uint32 Key;

    if (NULL == Packet_p) {
        return E_INVALID_INPUT_PARAMETERS;
    }

    Key = R15_Network_CreateUniqueKey(Packet_p, 0);
    (void)R15_Network_CancelRetransmission(Communication_p, Key);
    B_(printf("command_protocol.c (%d): Received Acknowledge!\n", __LINE__);)

    ReturnValue = R15_Network_PacketRelease(Communication_p, (PacketMeta_t *)Packet_p);
    return ReturnValue;
}


static ErrorCode_e ProcessCommand(Communication_t *Communication_p, PacketMeta_t *Packet_p, uint16 *SessionState_p)
{
    ErrorCode_e ReturnValue;
    CommandExtendedHeader_t CommandExtendedHeader = {0};

    if (NULL == Packet_p) {
        return E_INVALID_INPUT_PARAMETERS;
    }

    R15_DeserializeCommandExtendedHeader(&CommandExtendedHeader, Packet_p->ExtendedHeader_p);

    if ((CommandExtendedHeader.SessionState & SESSION_MASK) == (*SessionState_p & SESSION_MASK) + 4) {
        *SessionState_p += 4;
    } else {
        ReturnValue = SendAcknowledge(Communication_p, Packet_p);

        if (E_SUCCESS != ReturnValue) {
            return ReturnValue;
        }

        ReturnValue = R15_Network_PacketRelease(Communication_p, Packet_p);
        return ReturnValue;
    }

    return DispatchCommand(Communication_p, Packet_p);
}

static ErrorCode_e ProcessGeneralResponse(Communication_t *Communication_p, PacketMeta_t *Packet_p, uint16 *SessionState_p)
{
    ErrorCode_e ReturnValue;
    CommandExtendedHeader_t ExtendedHeader = {0};

    if (NULL == Packet_p) {
        return E_INVALID_INPUT_PARAMETERS;
    }

    R15_DeserializeCommandExtendedHeader(&ExtendedHeader, Packet_p->ExtendedHeader_p);

    if ((ExtendedHeader.SessionState & SESSION_MASK) > (*SessionState_p & SESSION_MASK)) {
        ReturnValue = SendAcknowledge(Communication_p, Packet_p);

        if (E_SUCCESS != ReturnValue) {
            return ReturnValue;
        }

        ReturnValue = R15_Network_PacketRelease(Communication_p, Packet_p);
        return ReturnValue;
    }

    return DispatchCommand(Communication_p, Packet_p);
}


static ErrorCode_e DispatchCommand(Communication_t *Communication_p, PacketMeta_t *Packet_p)
{
    ErrorCode_e ReturnValue;
    CommandExtendedHeader_t ExtendedHeader = {0};
    CommandData_t CmdData;

    ReturnValue = SendAcknowledge(Communication_p, Packet_p);

    if (E_SUCCESS != ReturnValue) {
        return E_SUCCESS;
    }

    R15_DeserializeCommandExtendedHeader(&ExtendedHeader, Packet_p->ExtendedHeader_p);

    CmdData.Type = (CommandType_t)(ExtendedHeader.SessionState & MASK_SELECT_STATE);
    CmdData.CommandNr = ExtendedHeader.Command;
    CmdData.ApplicationNr = ExtendedHeader.CommandGroup;
    CmdData.SessionNr = ExtendedHeader.SessionState >> 2;
    CmdData.Payload.Size = Packet_p->Header.PayloadLength;
    CmdData.Payload.Data_p = NULL;

    if (0 != CmdData.Payload.Size) {
        CmdData.Payload.Data_p = (uint8 *)malloc(Packet_p->Header.PayloadLength);

        if (NULL == CmdData.Payload.Data_p) {
            return E_ALLOCATE_FAILED;
        }

        memcpy(CmdData.Payload.Data_p, Packet_p->Payload_p, Packet_p->Header.PayloadLength);
    }

    ReturnValue = R15_Network_PacketRelease(Communication_p, Packet_p);

    ReturnValue = Communication_p->Do_CEH_Call_Fn(OBJECT_CEH_CALL(Communication_p), &CmdData);

    if (NULL != CmdData.Payload.Data_p) {
        free(CmdData.Payload.Data_p);
        CmdData.Payload.Data_p = NULL;
    }

    return ReturnValue;
}

static ErrorCode_e SendAcknowledge(Communication_t *Communication_p, const PacketMeta_t *const Packet_p)
{
    CommandExtendedHeader_t ExtendedHeader;
    R15_Header_t  Header;
    SendData_LP_t Param;

    memcpy(&ExtendedHeader, Packet_p->ExtendedHeader_p, COMMAND_EXTENDED_HEADER_LENGTH);
    ExtendedHeader.SessionState++;
    memcpy(&Header, &Packet_p->Header, HEADER_LENGTH);
    Header.Flags = Communication_p->CurrentFamilyHash;
    Header.PayloadLength = 0;
    Header.PayloadChecksum = 0;
    Header.ExtendedHeaderChecksum = 0;

    Param.Header_p = &Header;
    Param.ExtendedHeader_p = &ExtendedHeader;
    Param.Payload_p = NULL;
    Param.Time = 0;
    Param.TimerCallBackFn_p = NULL;

    return R15_Transport_Send(Communication_p, &Param);
}

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