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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*===========================================================================
FILE:
ProtocolServer.h
DESCRIPTION:
Generic protocol packet server
PUBLIC CLASSES AND METHODS:
cProtocolServer
Abstract base class for protocol servers
Copyright (c) 2011, Code Aurora Forum. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Code Aurora Forum nor
the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
===========================================================================*/
//---------------------------------------------------------------------------
// Pragmas
//---------------------------------------------------------------------------
#pragma once
//---------------------------------------------------------------------------
// Include Files
//---------------------------------------------------------------------------
#include "Comm.h"
#include "ProtocolRequest.h"
#include "ProtocolLog.h"
#include "Event.h"
#include <map>
#include <set>
//---------------------------------------------------------------------------
// Forward Declarations
//---------------------------------------------------------------------------
class cProtocolServer;
struct sServerControl;
//---------------------------------------------------------------------------
// Definitions
//---------------------------------------------------------------------------
// Invalid request ID
extern const ULONG INVALID_REQUEST_ID;
// Fill timespec with the time it will be in specified milliseconds
// Relative time to Absolute time
timespec TimeIn( ULONG millis );
// Find the milliseconds from current time this timespec will occur
// Absolute time to Relative time
ULONG TimeFromNow( timespec time );
// Provide a number for sequencing reference, similar to the windows function
ULONGLONG GetTickCount();
// timespec < comparison method
inline bool operator< (const timespec & first, const timespec & second)
{
return ( (first.tv_sec < second.tv_sec)
||( (first.tv_sec == second.tv_sec)
&&(first.tv_nsec < second.tv_nsec) ) );
}
// timespec <= comparison method
inline bool operator<= (const timespec & first, const timespec & second)
{
return ( (first.tv_sec < second.tv_sec)
||( (first.tv_sec == second.tv_sec)
&&(first.tv_nsec <= second.tv_nsec) ) );
}
/*=========================================================================*/
// Class cProtocolServerRxCallback
/*=========================================================================*/
class cProtocolServerRxCallback
{
public:
// (Inline) Constructor
cProtocolServerRxCallback()
: mpServer( 0 )
{ };
// (Inline) Destructor
virtual ~cProtocolServerRxCallback() { };
// (Inline) Set server object to pass results to
void SetServer( cProtocolServer * pServer )
{
mpServer = pServer;
};
// The I/O has been completed, process the results
virtual void IOComplete(
DWORD status,
DWORD bytesReceived );
protected:
/* Protocol server to interact with */
cProtocolServer * mpServer;
};
/*=========================================================================*/
// Class cProtocolServer
/*=========================================================================*/
class cProtocolServer
{
public:
// Constructor
cProtocolServer(
eProtocolType rxType,
eProtocolType txType,
ULONG bufferSzRx,
ULONG logSz );
// Destructor
virtual ~cProtocolServer();
// Initialize the protocol server
bool Initialize();
// Exit the protocol server
bool Exit();
// Connect to the given communications port
bool Connect( LPCSTR pPort );
// Disconnect from target
bool Disconnect();
// Are we currently connected to a port?
bool IsConnected();
// Add an outgoing protocol request to the protocol server request queue
ULONG AddRequest( const sProtocolRequest & req );
// Remove a previously added protocol request
bool RemoveRequest( ULONG reqID );
// (Inline) Return the protocol log
const cProtocolLog & GetLog()
{
return mLog;
};
protected:
// Internal protocol server request/response structure, used to track
// info related to sending out a request
struct sProtocolReqRsp
{
public:
// Constructor
sProtocolReqRsp(
const sProtocolRequest & requestInfo,
ULONG requestID,
ULONG auxDataMTU );
// Copy constructor
sProtocolReqRsp( const sProtocolReqRsp & reqRsp );
// (Inline) Reset for next transmission attempt
void Reset()
{
mEncodedSize = mRequest.GetSize();
mCurrentAuxTx = 0;
mbWaitingForResponse = 0;
};
/* Request ID */
ULONG mID;
/* Number of times this request has been attempted */
ULONG mAttempts;
/* Size of encoded data being transmitted */
ULONG mEncodedSize;
/* Number of required auxiliary data transmissions */
ULONG mRequiredAuxTxs;
/* Current auxiliary data transmission */
ULONG mCurrentAuxTx;
/* Are we currently waiting for a response? */
bool mbWaitingForResponse;
/* Underlying protocol request */
sProtocolRequest mRequest;
};
// Handle the remove request
bool HandleRemoveRequest( ULONG reqID );
// Schedule a request for transmission
bool ScheduleRequest(
ULONG reqID,
ULONG schedule );
// (Inline) Get next request's time from mRequestSchedule
timespec GetNextRequestTime()
{
timespec outTime;
std::set <tSchedule>::iterator pScheduleIter;
pScheduleIter = mRequestSchedule.begin();
tSchedule entry = *pScheduleIter;
outTime = entry.first;
return outTime;
}
// (Inline) Validate a request that is about to be scheduled
virtual bool ValidateRequest( const sProtocolRequest & req )
{
return req.IsValid();
};
// Reschedule (or cleanup) the active request
void RescheduleActiveRequest();
// Process a single outgoing protocol request
void ProcessRequest();
// Check that system time hasn't moved backwards
bool CheckSystemTime();
// Perform protocol specific communications port initialization
virtual bool InitializeComm() = 0;
// Perform protocol specific communications port cleanup
virtual bool CleanupComm() = 0;
// Encode data for transmission
virtual sSharedBuffer * EncodeTxData(
sSharedBuffer * pBuffer,
bool & bEncoded ) = 0;
// Decode incoming data into packets returning the last response
virtual bool DecodeRxData(
ULONG bytesReceived,
ULONG & rspIdx,
bool & bAbortTx ) = 0;
// Handle completion of receive data operation
void RxComplete(
DWORD status,
DWORD bytesReceived );
// Handle the response timer expiring
void RxTimeout();
// Handle completion of transmit data operation
virtual void TxComplete();
// Handle a transmission error
void TxError();
/* Underlying communications object */
cComm mComm;
/* Rx callback */
cProtocolServerRxCallback mRxCallback;
/* ID of Schedule thread */
pthread_t mScheduleThreadID;
// ScheduleThread signal event
cEvent mThreadScheduleEvent;
// Schedule mutex
// Ensures exclusive access to mRequestSchedule
pthread_mutex_t mScheduleMutex;
// Is the thread in the process of exiting?
// (no new commands will be accepted)
bool mbExiting;
/* Client/server thread control object */
sSharedBuffer * mpServerControl;
/* Protocol request schedule (scheduled time/request ID) */
typedef std::pair <timespec, ULONG> tSchedule;
std::set < tSchedule, std::less <tSchedule> > mRequestSchedule;
/* Last system time value (used to check for time changes) */
timespec mLastTime;
/* Protocol request map (request ID mapped to internal req/rsp struct) */
std::map <ULONG, sProtocolReqRsp *> mRequestMap;
/* Last assigned request ID */
ULONG mLastRequestID;
/* Current request being processed */
sProtocolReqRsp * mpActiveRequest;
/* Absolute timeout for mpActiveRequest
based on when write was completed */
timespec mActiveRequestTimeout;
/* Data buffer for incoming data */
BYTE * mpRxBuffer;
/* Size of above buffer (i.e. how much data to read in at once) */
ULONG mRxBufferSize;
/* Protocol type for incoming/outgoing data*/
eProtocolType mRxType;
eProtocolType mTxType;
/* Protocol log */
cProtocolLog mLog;
// Get a lock on ScheduleMutex
bool GetScheduleMutex();
// Release lock on ScheduleMutex
// Signal ScheduleThread if desired
bool ReleaseScheduleMutex( bool bSignalThread = true );
// Schedule Thread gets full access
friend void * ScheduleThread( PVOID pArg );
// Callback objects get full access
friend class cProtocolServerRxCallback;
};
|