summaryrefslogtreecommitdiff
path: root/source/LcmInterface.cpp
blob: 516e111e23c88f9df60c6a85b23e353b4c8d7164 (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
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/

#include "lcdriver_error_codes.h"
#include "LcmInterface.h"
#include "Error.h"
#include <string.h>
#ifdef _WIN32
#include "WinApiWrappers.h"
#else
#include "LinuxApiWrappers.h"
#include <dlfcn.h>
#define GetProcAddress dlsym
#endif

char *LcmInterface::m_pchLCMLibPath = 0;
extern const char *LCD_LCM_CompatibilityList[];
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//***********************************************************************************************
// Name:    LcmInterface()
// Desc:    Constructor.
//**********************************************************************************************/
LcmInterface::LcmInterface() : m_pCommunication(NULL) , lcmError_(E_SUCCESS)
{
}

//***********************************************************************************************
// Name:    ~LcmInterface()
// Desc:    Destructor.
//**********************************************************************************************/
LcmInterface::~LcmInterface()
{
}

HMODULE                  LcmInterface::m_hDLL = 0;
R15BulkInterface_t       LcmInterface::R15Bulk;
R15CommandInterface_t    LcmInterface::R15Command;
A2CommandInterface_t     LcmInterface::A2Command;
CommunicationInterface_t LcmInterface::Communication;
CCriticalSectionObject   LcmInterface::m_CriticalSection;

//***********************************************************************************************
// Name:    RunOnce()
// Desc:    Load LCM module and initialize function pointers once only.
//**********************************************************************************************/
int LcmInterface::LoadLCMLibrary()
{
    if (m_pchLCMLibPath == NULL) {
#ifdef _WIN32
        m_hDLL = LoadLibrary("LCM");
#else
        m_hDLL = dlopen("./lcm_linux/liblcm.so.1", RTLD_LAZY);
#endif
    } else {
#ifdef _WIN32
        m_hDLL = LoadLibrary(m_pchLCMLibPath);
#else
        m_hDLL = dlopen(m_pchLCMLibPath, RTLD_LAZY);
#endif
    }

    if (m_hDLL == NULL) {
        return LCM_DLL_LOAD_LOADLIBRARY_ERROR;
    }

    // Link to all necessary methods in the LCM DLL
    Communication.Initialize_Fn             = (CommunicationInitialize_t)GetProcAddress(m_hDLL, "Do_Communication_Initialize");
    Communication.Shutdown_Fn               = (CommunicationShutdown_t)GetProcAddress(m_hDLL, "Do_Communication_Shutdown");
    Communication.Poll_Fn                   = (CommunicationPoll_t)GetProcAddress(m_hDLL, "Do_Communication_Poll");
    Communication.SetFamily_Fn              = (CommunicationSetFamily_t)GetProcAddress(m_hDLL, "Do_Communication_SetFamily");
    Communication.Send_Fn                   = (CommunicationSend_t)GetProcAddress(m_hDLL, "Do_Communication_Send");
    Communication.SetProtocolTimeouts_Fn    = (CommunicationSetProtocolTimeouts_t)GetProcAddress(m_hDLL, "Do_Communication_SetProtocolTimeouts");
    Communication.GetProtocolTimeouts_Fn    = (CommunicationGetProtocolTimeouts_t)GetProcAddress(m_hDLL, "Do_Communication_GetProtocolTimeouts");
    Communication.GetVersion_Fn             = (CommunicationGetVersion_t)GetProcAddress(m_hDLL, "Do_Communication_GetVersion");
    Communication.CancelReceiver_Fn         = (CommunicationCancelReceiver_t)GetProcAddress(m_hDLL, "Do_Communication_Cancel_Receiver");

    R15Command.Send_Fn                  = (R15CommandSend_t)GetProcAddress(m_hDLL, "Do_R15_Command_Send");
    R15Command.ResetSessionCounters_Fn  = (R15CommandResetSessionCounters_t)GetProcAddress(m_hDLL, "Do_R15_Command_ResetSessionCounters");

    R15Bulk.OpenSession_Fn              = (R15BulkOpenSession_t)GetProcAddress(m_hDLL, "Do_R15_Bulk_OpenSession");
    R15Bulk.CreateVector_Fn             = (R15BulkCreateVector_t)GetProcAddress(m_hDLL, "Do_R15_Bulk_CreateVector");
    R15Bulk.DestroyVector_Fn            = (R15BulkDestroyVector_t)GetProcAddress(m_hDLL, "Do_R15_Bulk_DestroyVector");
    R15Bulk.StartSession_Fn             = (R15BulkStartSession_t)GetProcAddress(m_hDLL, "Do_R15_Bulk_StartSession");
    R15Bulk.GetStatusSession_Fn         = (R15BulkGetStatusSession_t)GetProcAddress(m_hDLL, "Do_R15_Bulk_GetStatusSession");
    R15Bulk.CloseSession_Fn             = (R15BulkCloseSession_t)GetProcAddress(m_hDLL, "Do_R15_Bulk_CloseSession");
    R15Bulk.SetCallbacks_Fn             = (R15BulkSetCallbacks_t)GetProcAddress(m_hDLL, "Do_R15_Bulk_SetCallbacks");
    R15Bulk.SetBuffersRelease_Fn        = (R15BulkBuffersRelease_t)GetProcAddress(m_hDLL, "Do_R15_Bulk_SetBuffersRelease");

    A2Command.Send_Fn                       = (A2CommandSend_t)GetProcAddress(m_hDLL, "Do_A2_Command_Send");
    A2Command.SpeedflashStart_Fn            = (A2SpeedflashStart_t)GetProcAddress(m_hDLL, "Do_A2_Speedflash_Start");
    A2Command.SpeedflashSetLastBlock_Fn     = (A2SpeedflashSetLastBlock_t)GetProcAddress(m_hDLL, "Do_A2_Speedflash_SetLastBlock");
    A2Command.SpeedflashWriteBlock_Fn       = (A2SpeedflashWriteBlock_t)GetProcAddress(m_hDLL, "Do_A2_Speedflash_WriteBlock");

    if (
        Communication.Initialize_Fn          == 0 ||
        Communication.Shutdown_Fn            == 0 ||
        Communication.Poll_Fn                == 0 ||
        Communication.SetFamily_Fn           == 0 ||
        Communication.Send_Fn                == 0 ||
        Communication.SetProtocolTimeouts_Fn == 0 ||
        Communication.GetProtocolTimeouts_Fn == 0 ||
        Communication.GetVersion_Fn          == 0 ||
        R15Command.Send_Fn                   == 0 ||
        R15Command.ResetSessionCounters_Fn   == 0 ||
        R15Bulk.OpenSession_Fn               == 0 ||
        R15Bulk.CreateVector_Fn              == 0 ||
        R15Bulk.DestroyVector_Fn             == 0 ||
        R15Bulk.StartSession_Fn              == 0 ||
        R15Bulk.GetStatusSession_Fn          == 0 ||
        R15Bulk.CloseSession_Fn              == 0 ||
        R15Bulk.SetCallbacks_Fn              == 0 ||
        A2Command.Send_Fn                    == 0 ||
        A2Command.SpeedflashStart_Fn         == 0 ||
        A2Command.SpeedflashSetLastBlock_Fn  == 0 ||
        A2Command.SpeedflashWriteBlock_Fn    == 0
    ) {
        return LCM_DLL_LOAD_FUNCTION_NOT_FOUND;
    }

    return E_SUCCESS;
}


void LcmInterface::SetLCMLibPath(const char *lcmLibPath)
{
    size_t pathLength = strlen(lcmLibPath);

    if (0 != m_pchLCMLibPath) {
        delete[] m_pchLCMLibPath;
        m_pchLCMLibPath = NULL;
    }

    m_pchLCMLibPath = new char[pathLength + 1];
    strcpy_s(m_pchLCMLibPath, pathLength + 1, lcmLibPath);
    m_pchLCMLibPath[pathLength] = '\0';
}

void LcmInterface::CloseLCMLibrary()
{
    if (0 != m_pchLCMLibPath) {
        delete[] m_pchLCMLibPath;
        m_pchLCMLibPath = NULL;
    }

#ifdef _WIN32
    FreeLibrary(m_hDLL);
#else
    dlclose(m_hDLL);
#endif
}

ErrorCode_e LcmInterface::CommunicationInitialize(void *Object_p, Family_t Family, HashDevice_t *HashDevice_p, CommunicationDevice_t *CommunicationDevice_p, Do_CEH_Call_t CommandCallback_p, BuffersInterface_t *Buffers_p, TimersInterface_t *Timers_p, QueueInterface_t *Queue_p)
{
    int ReturnValue = E_SUCCESS;
    char *LCMVersion_p = NULL;
    LCM_t LCMType = PC_LCM;

    if (m_hDLL == NULL) {
        CLockCS lock(m_CriticalSection);
        VERIFY_SUCCESS(LoadLCMLibrary());
        LCMVersion_p = Communication.GetVersion_Fn(m_pCommunication);
        VERIFY_SUCCESS(CommunicationCheckVersion(LCMVersion_p, LCMType));
    }

    ReturnValue = Communication.Initialize_Fn(Object_p, &m_pCommunication, Family, HashDevice_p, CommunicationDevice_p, CommandCallback_p, Buffers_p, Timers_p, Queue_p);

ErrorExit:

    if (ReturnValue != E_SUCCESS) {
        CloseLCMLibrary();
        m_hDLL = NULL;
    }

    return static_cast<ErrorCode_e>(ReturnValue);
}

ErrorCode_e LcmInterface::CommunicationSend(void *InputData_p)
{
    return Communication.Send_Fn(m_pCommunication, InputData_p);
}

ErrorCode_e LcmInterface::CommunicationSetFamily(Family_t family, Do_CEH_Call_t CEHCallback)
{
    return Communication.SetFamily_Fn(m_pCommunication, family, CEHCallback);
}

ErrorCode_e LcmInterface::CommunicationSetProtocolTimeouts(void *TimeoutData_p)
{
    return Communication.SetProtocolTimeouts_Fn(m_pCommunication, TimeoutData_p);
}

ErrorCode_e LcmInterface::CommunicationGetProtocolTimeouts(void *TimeoutData_p)
{
    return Communication.GetProtocolTimeouts_Fn(m_pCommunication, TimeoutData_p);
}

ErrorCode_e LcmInterface::CommunicationCheckVersion(char *LCMVersion_p, LCM_t LCMType)
{

    int ReturnValue = LCM_LOAD_INCOMPATIBLE_PC_VERSION;
    int i = 0;

    if (LCMType == LDR_LCM) {
        ReturnValue = LCM_LOAD_INCOMPATIBLE_LDR_VERSION;
    }

    do {
        if (strcmp(LCMVersion_p, LCD_LCM_CompatibilityList[i]) == 0) {
            ReturnValue = E_SUCCESS;
            break;
        }

        i++;
    } while (LCD_LCM_CompatibilityList[i] != NULL);

    return static_cast<ErrorCode_e>(ReturnValue);
}

ErrorCode_e LcmInterface::CommunicationShutdown()
{
    return Communication.Shutdown_Fn(&m_pCommunication);
}

ErrorCode_e LcmInterface::CommunicationCancelReceiver(uint8 PacketsBeforeReceiverStop)
{
    return Communication.CancelReceiver_Fn(m_pCommunication, PacketsBeforeReceiverStop);
}
ErrorCode_e LcmInterface::CommandSend(CommandData_t *CmdData_p)
{
    return R15Command.Send_Fn(m_pCommunication, CmdData_p);
}

ErrorCode_e LcmInterface::CommunicationPoll()
{
    return Communication.Poll_Fn(m_pCommunication);
}

ErrorCode_e LcmInterface::CommandResetSessionCounters()
{
    return R15Command.ResetSessionCounters_Fn(m_pCommunication);
}

void LcmInterface::BulkSetCallbacks(void *BulkCommandCallback_p, void *BulkDataCallback_p, void *BulkDataEndOfDump_p)
{
    R15Bulk.SetCallbacks_Fn(m_pCommunication, BulkCommandCallback_p, BulkDataCallback_p, BulkDataEndOfDump_p);
}

void LcmInterface::BulkBuffersRelease(void *BulkBufferRelease_p)
{
    R15Bulk.SetBuffersRelease_Fn(m_pCommunication, BulkBufferRelease_p);
}

uint32 LcmInterface::BulkOpenSession(const uint16 SessionId, const TL_SessionMode_t Mode, uint32 Length)
{
    return R15Bulk.OpenSession_Fn(m_pCommunication, SessionId, Mode, Length);
}

TL_BulkVectorList_t *LcmInterface::BulkCreateVector(const uint32 BulkVector, uint32 Length, const uint32 BuffSize, TL_BulkVectorList_t *CreatedBulkVector_p)
{
    return R15Bulk.CreateVector_Fn(m_pCommunication, BulkVector, Length, BuffSize, CreatedBulkVector_p);
}

ErrorCode_e LcmInterface::BulkStartSession(TL_BulkVectorList_t *BulkVector_p, const uint64 Offset)
{
    return R15Bulk.StartSession_Fn(m_pCommunication, BulkVector_p, Offset);
}

uint32 LcmInterface::BulkDestroyVector(TL_BulkVectorList_t *BulkVector_p, boolean ReqReleaseBuffer)
{
    return R15Bulk.DestroyVector_Fn(m_pCommunication, BulkVector_p, ReqReleaseBuffer);
}

ErrorCode_e LcmInterface::BulkCloseSession(TL_BulkVectorList_t *BulkVector_p)
{
    return R15Bulk.CloseSession_Fn(m_pCommunication, BulkVector_p);
}

ErrorCode_e LcmInterface::A2CommandSend(A2_CommandData_t *CmdData_p)
{
    return A2Command.Send_Fn(m_pCommunication, CmdData_p);
}

void LcmInterface::A2SpeedflashStart()
{
    A2Command.SpeedflashStart_Fn(m_pCommunication);
}

void LcmInterface::A2SpeedflashSetLastBlock()
{
    A2Command.SpeedflashSetLastBlock_Fn(m_pCommunication);
}

ErrorCode_e LcmInterface::A2SpeedflashWriteBlock(const void *Buffer, const size_t BufferSize)
{
    return A2Command.SpeedflashWriteBlock_Fn(m_pCommunication, Buffer, BufferSize);
}