summaryrefslogtreecommitdiff
path: root/source/LCDriverThread.cpp
blob: 4b492ad09e6a0e46abef642928f596b4c1763229 (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
/*******************************************************************************
*
*    File name: LCDriverThread.cpp
*     Language: Visual C++
*  Description: Main loop for LCDriver
*
*
* Copyright (C) ST-Ericsson SA 2011
* License terms: 3-clause BSD license
*
*******************************************************************************/

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                               File LCDriverThread.cpp

#include "LCDriverThread.h"
#include "LCDriverMethods.h"
#include "lcdriver_error_codes.h"
#include "Event.h"
#ifdef _WIN32
#include "WinApiWrappers.h"
#endif

CLCDriverThread::CLCDriverThread(CLCDriverMethods *lcdriverMethods):
    lcdriverMethods_(lcdriverMethods),
    timerOn_(false),
    shutdown_(false)
{
}

void CLCDriverThread::MainExecutionLoop()
{
    while (!shutdown_) {
        // Limit the scope of the lock to avoid holding lock while sleeping. This
        // solves the problem with starvation of other threads trying to lock LCMPollCS.
        {
            CLockCS lock(LCMPollCS);
            lcdriverMethods_->m_pTimer->DoTimerHandler(timerOn_);

            ErrorCode_e ReturnValue = lcdriverMethods_->m_pLcmInterface->CommunicationPoll();

            if (E_SUCCESS != ReturnValue) {
#ifdef _THREADDEBUG
                lcdriverMethods_->m_pLogger->log("LCD MainThread: LCM polling ERROR = %d", ReturnValue);
#endif

                if (R15_FAMILY == lcdriverMethods_->m_CurrentProtocolFamily) {
                    lcdriverMethods_->SignalError(lcdriverMethods_->MapLcmError(ReturnValue));
                    IsDying = LCDRIVER_THREAD_STOPPED_AFTER_LCM_ERROR;
                    break;
                }
            }
        }

        OS::Sleep(1);
    }
}

void CLCDriverThread::SignalDeath()
{
    CLockCS lock(LCMPollCS);
    shutdown_ = true;
}

void CLCDriverThread::TimerOn()
{
    CLockCS lock(LCMPollCS);
    timerOn_ = true;
}

void CLCDriverThread::TimerOff()
{
    CLockCS lock(LCMPollCS);
    timerOn_ = false;
}

ErrorCode_e CLCDriverThread::SetLcmFamily(Family_t family, Do_CEH_Call_t CEHCallback)
{
    CLockCS lock(LCMPollCS);
    return lcdriverMethods_->m_pLcmInterface->CommunicationSetFamily(family, CEHCallback);
}

//                                                                                        End of file LCDriverThread.cpp
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////