Debian Codegraph @

gobi-api/Gobi_2011-11-28-1533/Core/ProtocolServer.h

raw
1 /*===========================================================================
2 FILE:
3 ProtocolServer.h
4
5 DESCRIPTION:
6 Generic protocol packet server
7
8 PUBLIC CLASSES AND METHODS:
9 cProtocolServer
10 Abstract base class for protocol servers
11
12 Copyright (c) 2011, Code Aurora Forum. All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21 * Neither the name of Code Aurora Forum nor
22 the names of its contributors may be used to endorse or promote
23 products derived from this software without specific prior written
24 permission.
25
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 ===========================================================================*/
39
40 //---------------------------------------------------------------------------
41 // Pragmas
42 //---------------------------------------------------------------------------
43 #pragma once
44
45 //---------------------------------------------------------------------------
46 // Include Files
47 //---------------------------------------------------------------------------
48 #include "Comm.h"
49 #include "ProtocolRequest.h"
50 #include "ProtocolLog.h"
51 #include "Event.h"
52
53 #include <map>
54 #include <set>
55
56 //---------------------------------------------------------------------------
57 // Forward Declarations
58 //---------------------------------------------------------------------------
59 class cProtocolServer;
60 struct sServerControl;
61
62 //---------------------------------------------------------------------------
63 // Definitions
64 //---------------------------------------------------------------------------
65
66 // Invalid request ID
67 extern const ULONG INVALID_REQUEST_ID;
68
69 // Fill timespec with the time it will be in specified milliseconds
70 // Relative time to Absolute time
71 timespec TimeIn( ULONG millis );
72
73 // Find the milliseconds from current time this timespec will occur
74 // Absolute time to Relative time
75 ULONG TimeFromNow( timespec time );
76
77 // Provide a number for sequencing reference, similar to the windows function
78 ULONGLONG GetTickCount();
79
80 // timespec < comparison method
81 inline bool operator< (const timespec & first, const timespec & second)
82 {
83 return ( (first.tv_sec < second.tv_sec)
84 ||( (first.tv_sec == second.tv_sec)
85 &&(first.tv_nsec < second.tv_nsec) ) );
86 }
87
88 // timespec <= comparison method
89 inline bool operator<= (const timespec & first, const timespec & second)
90 {
91 return ( (first.tv_sec < second.tv_sec)
92 ||( (first.tv_sec == second.tv_sec)
93 &&(first.tv_nsec <= second.tv_nsec) ) );
94 }
95
96 /*=========================================================================*/
97 // Class cProtocolServerRxCallback
98 /*=========================================================================*/
99 class cProtocolServerRxCallback
100 {
101 public:
102 // (Inline) Constructor
103 cProtocolServerRxCallback()
104 : mpServer( 0 )
105 { };
106
107 // (Inline) Destructor
108 virtual ~cProtocolServerRxCallback() { };
109
110 // (Inline) Set server object to pass results to
111 void SetServer( cProtocolServer * pServer )
112 {
113 mpServer = pServer;
114 };
115
116 // The I/O has been completed, process the results
117 virtual void IOComplete(
118 DWORD status,
119 DWORD bytesReceived );
120
121 protected:
122 /* Protocol server to interact with */
123 cProtocolServer * mpServer;
124 };
125
126 /*=========================================================================*/
127 // Class cProtocolServer
128 /*=========================================================================*/
129 class cProtocolServer
130 {
131 public:
132 // Constructor
133 cProtocolServer(
134 eProtocolType rxType,
135 eProtocolType txType,
136 ULONG bufferSzRx,
137 ULONG logSz );
138
139 // Destructor
140 virtual ~cProtocolServer();
141
142 // Initialize the protocol server
143 bool Initialize();
144
145 // Exit the protocol server
146 bool Exit();
147
148 // Connect to the given communications port
149 bool Connect( LPCSTR pPort );
150
151 // Disconnect from target
152 bool Disconnect();
153
154 // Are we currently connected to a port?
155 bool IsConnected();
156
157 // Add an outgoing protocol request to the protocol server request queue
158 ULONG AddRequest( const sProtocolRequest & req );
159
160 // Remove a previously added protocol request
161 bool RemoveRequest( ULONG reqID );
162
163 // (Inline) Return the protocol log
164 const cProtocolLog & GetLog()
165 {
166 return mLog;
167 };
168
169 protected:
170 // Internal protocol server request/response structure, used to track
171 // info related to sending out a request
172 struct sProtocolReqRsp
173 {
174 public:
175 // Constructor
176 sProtocolReqRsp(
177 const sProtocolRequest & requestInfo,
178 ULONG requestID,
179 ULONG auxDataMTU );
180
181 // Copy constructor
182 sProtocolReqRsp( const sProtocolReqRsp & reqRsp );
183
184 // (Inline) Reset for next transmission attempt
185 void Reset()
186 {
187 mEncodedSize = mRequest.GetSize();
188
189 mCurrentAuxTx = 0;
190 mbWaitingForResponse = 0;
191 };
192
193 /* Request ID */
194 ULONG mID;
195
196 /* Number of times this request has been attempted */
197 ULONG mAttempts;
198
199 /* Size of encoded data being transmitted */
200 ULONG mEncodedSize;
201
202 /* Number of required auxiliary data transmissions */
203 ULONG mRequiredAuxTxs;
204
205 /* Current auxiliary data transmission */
206 ULONG mCurrentAuxTx;
207
208 /* Are we currently waiting for a response? */
209 bool mbWaitingForResponse;
210
211 /* Underlying protocol request */
212 sProtocolRequest mRequest;
213 };
214
215 // Handle the remove request
216 bool HandleRemoveRequest( ULONG reqID );
217
218 // Schedule a request for transmission
219 bool ScheduleRequest(
220 ULONG reqID,
221 ULONG schedule );
222
223 // (Inline) Get next request's time from mRequestSchedule
224 timespec GetNextRequestTime()
225 {
226 timespec outTime;
227
228 std::set <tSchedule>::iterator pScheduleIter;
229 pScheduleIter = mRequestSchedule.begin();
230 tSchedule entry = *pScheduleIter;
231
232 outTime = entry.first;
233 return outTime;
234 }
235
236 // (Inline) Validate a request that is about to be scheduled
237 virtual bool ValidateRequest( const sProtocolRequest & req )
238 {
239 return req.IsValid();
240 };
241
242 // Reschedule (or cleanup) the active request
243 void RescheduleActiveRequest();
244
245 // Process a single outgoing protocol request
246 void ProcessRequest();
247
248 // Check that system time hasn't moved backwards
249 bool CheckSystemTime();
250
251 // Perform protocol specific communications port initialization
252 virtual bool InitializeComm() = 0;
253
254 // Perform protocol specific communications port cleanup
255 virtual bool CleanupComm() = 0;
256
257 // Encode data for transmission
258 virtual sSharedBuffer * EncodeTxData(
259 sSharedBuffer * pBuffer,
260 bool & bEncoded ) = 0;
261
262 // Decode incoming data into packets returning the last response
263 virtual bool DecodeRxData(
264 ULONG bytesReceived,
265 ULONG & rspIdx,
266 bool & bAbortTx ) = 0;
267
268 // Handle completion of receive data operation
269 void RxComplete(
270 DWORD status,
271 DWORD bytesReceived );
272
273 // Handle the response timer expiring
274 void RxTimeout();
275
276 // Handle completion of transmit data operation
277 virtual void TxComplete();
278
279 // Handle a transmission error
280 void TxError();
281
282 /* Underlying communications object */
283 cComm mComm;
284
285 /* Rx callback */
286 cProtocolServerRxCallback mRxCallback;
287
288 /* ID of Schedule thread */
289 pthread_t mScheduleThreadID;
290
291 // ScheduleThread signal event
292 cEvent mThreadScheduleEvent;
293
294 // Schedule mutex
295 // Ensures exclusive access to mRequestSchedule
296 pthread_mutex_t mScheduleMutex;
297
298 // Is the thread in the process of exiting?
299 // (no new commands will be accepted)
300 bool mbExiting;
301
302 /* Client/server thread control object */
303 sSharedBuffer * mpServerControl;
304
305 /* Protocol request schedule (scheduled time/request ID) */
306 typedef std::pair <timespec, ULONG> tSchedule;
307 std::set < tSchedule, std::less <tSchedule> > mRequestSchedule;
308
309 /* Last system time value (used to check for time changes) */
310 timespec mLastTime;
311
312 /* Protocol request map (request ID mapped to internal req/rsp struct) */
313 std::map <ULONG, sProtocolReqRsp *> mRequestMap;
314
315 /* Last assigned request ID */
316 ULONG mLastRequestID;
317
318 /* Current request being processed */
319 sProtocolReqRsp * mpActiveRequest;
320
321 /* Absolute timeout for mpActiveRequest
322 based on when write was completed */
323 timespec mActiveRequestTimeout;
324
325 /* Data buffer for incoming data */
326 BYTE * mpRxBuffer;
327
328 /* Size of above buffer (i.e. how much data to read in at once) */
329 ULONG mRxBufferSize;
330
331 /* Protocol type for incoming/outgoing data*/
332 eProtocolType mRxType;
333 eProtocolType mTxType;
334
335 /* Protocol log */
336 cProtocolLog mLog;
337
338 // Get a lock on ScheduleMutex
339 bool GetScheduleMutex();
340
341 // Release lock on ScheduleMutex
342 // Signal ScheduleThread if desired
343 bool ReleaseScheduleMutex( bool bSignalThread = true );
344
345 // Schedule Thread gets full access
346 friend void * ScheduleThread( PVOID pArg );
347
348 // Callback objects get full access
349 friend class cProtocolServerRxCallback;
350 };
351