-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Worker.cpp
227 lines (176 loc) · 7.95 KB
/
Worker.cpp
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
#include "Worker.h"
#include "Acceptor.h"
#include <termios.h>
#include <iostream>
namespace MPOST
{
extern pthread_mutex_t inStandardPollMutex;
extern pthread_mutex_t mutex1;
extern pthread_cond_t condition1;
void* CWorker::MessageLoopThread(void* param)
{
CAcceptor* acceptor = static_cast<CAcceptor*>(param);
CDataLinkLayer* dataLinkLayer = new CDataLinkLayer(acceptor);
acceptor->_dataLinkLayer = dataLinkLayer;
// We want to poll every 100 ms (arbitrary value within recommended range of 100-200)
// if there is no other command, but rather than sleeping for 100 ms, we only sleep
// for 10 ms at a time so that we can check the message messageQueue more frequently. At every
// 10 loop cycles without a command we issue a standard poll and reset the cycle
// counter.
int loopCycleCounter = 0;
// If the BA does not reply to a command, we ignore it and continue. However, after 30 seconds
// without receiving any replies, we raise a Disconnected event.
long timeoutStartTickCount = CAcceptor::GetTickCount();
while (true)
{
if (!acceptor->_inSoftResetWaitForReply)
{
usleep(10 * MICRO_TO_MILLI);
}
else
{
// After a soft reset, the BA may take up to 15 seconds to start communicating, so
// we cut the poll rate to once per second.
usleep(1000 * MICRO_TO_MILLI);
}
if ((CAcceptor::GetTickCount() - timeoutStartTickCount) > CAcceptor::CommunicationDisconnectTimeout)
{
// NOTE
// This condition was added when the post-download wait was increased to 30 seconds because testing
// of the SK2 showed that a 15-second delay was insufficient. In order to avoid a Disconnected event
// from being raised, we have to suppress the worker thread's checking for disconnection when downloading.
if (acceptor->_deviceState != Downloading && acceptor->_deviceState != DownloadRestart)
{
acceptor->_connected = false;
if (acceptor->_shouldRaiseDisconnectedEvent)
acceptor->RaiseDisconnectedEvent();
acceptor->_wasDisconnected = true;
timeoutStartTickCount = CAcceptor::GetTickCount();
}
}
if (acceptor->_stopWorkerThread)
{
// Clear the message queue so that if the worker thread is started, stopped, and then restarted,
// old messages will be discarded.
acceptor->_messageQueue.clear();
acceptor->_stopWorkerThread = false;
acceptor->_dataLinkLayer = NULL;
delete dataLinkLayer;
acceptor->_workerThread = NULL;
return 0;
}
if (!acceptor->_messageQueue.empty())
{
loopCycleCounter = 0;
CMessage* message = acceptor->_messageQueue.front();
tcflush(acceptor->_port, TCIOFLUSH);
acceptor->_messageQueue.pop_front();
dataLinkLayer->SendPacket(message->_payload, message->_payloadLength);
vector<char>* reply = dataLinkLayer->ReceiveReply();
if (reply->size() > 0)
{
timeoutStartTickCount = CAcceptor::GetTickCount();
if (acceptor->_wasDisconnected)
{
acceptor->_wasDisconnected = false;
// If OpenThread is still active, that means that the entire connection process was never
// completed, so we want to let OpenThread finish and issue the Connected event itself.
if (acceptor->_openThread == 0)
{
// NOTE
// We cannot simply test _deviceState because it is not set until ProcessReply.
if (((*reply)[2] & 0x70) != 0x50)
{
acceptor->_connected = true;
acceptor->RaiseConnectedEvent();
}
}
}
if (acceptor->_inSoftResetWaitForReply)
{
acceptor->_inSoftResetWaitForReply = false;
}
acceptor->_isReplyAcked = dataLinkLayer->ReplyAcked(reply);
if (message->_isSynchronous)
{
acceptor->_replyQueue.push_back(reply);
// pthread_mutex_unlock( &mutex1 );
// pthread_cond_signal(&condition1);
}
else
acceptor->ProcessReply(*reply);
}
else
{
// If we receive no reply and the command was synchronous, we still post an empty
// reply to the messageQueue so that the caller does not block. The caller is
// expected to handle an empty reply.
if (message->_isSynchronous)
{
acceptor->_replyQueue.push_back(reply);
}
}
delete message;
}
else
{
// NOTE
// This flag exists solely to handle a scenario in which the Calibrate method sets
// DeviceState to CalibrateStart and the CWorker thread somehow executes another standard
// poll before the main thread has had a chance to issue the Calibrate command. When that
// happens ProcessReply sees that the Calibrate bit is not set but DeviceState == CalibrateStart,
// and it erroneously thinks calibration has completed. Our solution, perhaps not the best
// is to suppress that standard poll. This flag is reset when a command is dequeued
// from the message queue. Needless to say, we should not set this flag unless we are
// about to issue a command.
if (acceptor->_suppressStandardPoll)
continue;
loopCycleCounter++;
if (loopCycleCounter == 9)
{
loopCycleCounter = 0;
pthread_mutex_lock(&inStandardPollMutex);
char payload[4];
acceptor->ConstructOmnibusCommand(payload, CmdOmnibus, 1);
dataLinkLayer->SendPacket(payload, sizeof(payload));
vector<char>* reply = dataLinkLayer->ReceiveReply();
if (reply->size() > 0)
{
timeoutStartTickCount = CAcceptor::GetTickCount();
if (acceptor->_wasDisconnected)
{
acceptor->_wasDisconnected = false;
// NOTE
// We cannot simply test _deviceState because it is not set until ProcessReply.
if (((*reply)[2] & 0x70) != 0x50)
{
acceptor->_connected = true;
acceptor->RaiseConnectedEvent();
}
else
{
acceptor->RaiseDownloadRestartEvent();
}
}
if (acceptor->_inSoftResetWaitForReply)
{
acceptor->_inSoftResetWaitForReply = false;
}
// NOTE
// My preference was to reset the event at the beginning of ProcessReply, but
// the problem is that there is a tiny possibility that between the time that
// execution enters ProcessReply and the event is reset, that Calibrate
// will test the event and find it non-signaled.
acceptor->_isReplyAcked = dataLinkLayer->ReplyAcked(reply);
acceptor->ProcessReply(*reply);
}
else
{
delete reply;
}
pthread_mutex_unlock(&inStandardPollMutex);
}
}
}
}
}