0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
* Includes
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
#include <e32math.h>
|
|
22 |
#include <e32std.h>
|
|
23 |
|
|
24 |
#include "CProtocolTypes.h"
|
|
25 |
#include "CSerialTransport.h"
|
|
26 |
#include "CTCPTransport.h"
|
|
27 |
#include "CUCCSDeviceProtocol.h"
|
|
28 |
#include "assert.h"
|
|
29 |
|
|
30 |
|
|
31 |
/*****************************************************************************
|
|
32 |
*
|
|
33 |
* Constructor
|
|
34 |
*
|
|
35 |
****************************************************************************/
|
|
36 |
CUCCSDeviceProtocol::CUCCSDeviceProtocol()
|
|
37 |
{
|
|
38 |
iTransport = NULL;
|
|
39 |
iStatus = TDP_IDLE;
|
|
40 |
|
|
41 |
// Seed the random number generator - this requires a 64 bit seed
|
|
42 |
TTime now;
|
|
43 |
now.HomeTime();
|
|
44 |
iRandomSeed = now.Int64();
|
|
45 |
}
|
|
46 |
|
|
47 |
|
|
48 |
/*****************************************************************************
|
|
49 |
*
|
|
50 |
* Destructor
|
|
51 |
*
|
|
52 |
****************************************************************************/
|
|
53 |
CUCCSDeviceProtocol::~CUCCSDeviceProtocol()
|
|
54 |
{
|
|
55 |
assert ( iStatus == TDP_IDLE );
|
|
56 |
assert ( iTransport == NULL );
|
|
57 |
}
|
|
58 |
|
|
59 |
|
|
60 |
/*****************************************************************************
|
|
61 |
*
|
|
62 |
* Initialise
|
|
63 |
*
|
|
64 |
****************************************************************************/
|
|
65 |
TDPError CUCCSDeviceProtocol::initialise(TBufC16<40> aRemoteHost, TDPTransport aTransportType)
|
|
66 |
{
|
|
67 |
// Check the state
|
|
68 |
assert ( iStatus == TDP_IDLE );
|
|
69 |
|
|
70 |
// Check params
|
|
71 |
assert (aRemoteHost.Length() > 0);
|
|
72 |
|
|
73 |
// Set the member data
|
|
74 |
iRand_UID = Math::Rand( iRandomSeed );
|
|
75 |
iRemoteHost = aRemoteHost;
|
|
76 |
|
|
77 |
// Initialise the serial transport
|
|
78 |
if( iTransport == NULL )
|
|
79 |
{
|
|
80 |
TInt r = 0;
|
|
81 |
if( aTransportType == TDP_SERIAL )
|
|
82 |
{
|
|
83 |
TRAP ( r, iTransport = CSerialTransport::NewL(_L("ECUART")) );
|
|
84 |
}
|
|
85 |
else if( aTransportType == TDP_TCP )
|
|
86 |
{
|
|
87 |
TRAP ( r, iTransport = new (ELeave) CTCPTransport() );
|
|
88 |
|
|
89 |
}
|
|
90 |
else
|
|
91 |
{
|
|
92 |
return TDP_ERRINTIALISING;
|
|
93 |
}
|
|
94 |
|
|
95 |
if ( r != KErrNone)
|
|
96 |
return TDP_ERRINTIALISING;
|
|
97 |
|
|
98 |
TRAPD ( s,iTransport->InitialiseL() );
|
|
99 |
if ( s != KErrNone)
|
|
100 |
{
|
|
101 |
iTransport->Release();
|
|
102 |
delete iTransport;
|
|
103 |
iTransport = NULL;
|
|
104 |
return TDP_ERRINTIALISING;
|
|
105 |
}
|
|
106 |
|
|
107 |
TRAPD ( t, iTransport->ConnectL(&iRemoteHost) );
|
|
108 |
if ( t != KErrNone)
|
|
109 |
{
|
|
110 |
iTransport->Disconnect();
|
|
111 |
iTransport->Release();
|
|
112 |
delete iTransport;
|
|
113 |
iTransport = NULL;
|
|
114 |
return TDP_ERRINTIALISING;
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
iStatus = TDP_CONNECTED;
|
|
119 |
return TDP_SUCCESS;
|
|
120 |
}
|
|
121 |
|
|
122 |
|
|
123 |
/*****************************************************************************
|
|
124 |
*
|
|
125 |
* Disconnect
|
|
126 |
*
|
|
127 |
****************************************************************************/
|
|
128 |
TDPError CUCCSDeviceProtocol::disconnect()
|
|
129 |
{
|
|
130 |
// check the state
|
|
131 |
if( iStatus == TDP_IDLE ) {
|
|
132 |
return TDP_SUCCESS;
|
|
133 |
}
|
|
134 |
assert ( iStatus == TDP_CONNECTED );
|
|
135 |
|
|
136 |
// if the serial transport is up then bring it down
|
|
137 |
if( iTransport != NULL ) {
|
|
138 |
iTransport->Disconnect();
|
|
139 |
iTransport->Release();
|
|
140 |
delete iTransport;
|
|
141 |
iTransport = NULL;
|
|
142 |
}
|
|
143 |
|
|
144 |
// update the state and return
|
|
145 |
iStatus = TDP_IDLE;
|
|
146 |
return TDP_SUCCESS;
|
|
147 |
}
|
|
148 |
|
|
149 |
|
|
150 |
/*****************************************************************************
|
|
151 |
*
|
|
152 |
* Receive Message
|
|
153 |
*
|
|
154 |
****************************************************************************/
|
|
155 |
TDPError CUCCSDeviceProtocol::receiveMessage(TPCommand* aCmd, int* aDataLength, void* aData)
|
|
156 |
{
|
|
157 |
TPHeader header;
|
|
158 |
TInt ret;
|
|
159 |
int bufferLen;
|
|
160 |
|
|
161 |
// Check the state
|
|
162 |
assert ( iStatus == TDP_CONNECTED );
|
|
163 |
|
|
164 |
// Check params
|
|
165 |
assert( aCmd != NULL );
|
|
166 |
assert( aDataLength != NULL );
|
|
167 |
assert( aData != NULL );
|
|
168 |
|
|
169 |
// Record the original length of the aData buffer b4 it gets overwritten
|
|
170 |
bufferLen = *aDataLength;
|
|
171 |
|
|
172 |
// Read the header
|
|
173 |
TPtr8 p( (TUint8*)&header,sizeof(header), sizeof(header) );
|
|
174 |
ret = iTransport->RequestReceive( &p, p.Size());
|
|
175 |
if ( ret != KErrNone)
|
|
176 |
return TDP_RECVERROR;
|
|
177 |
|
|
178 |
// Check that this is a response to our last message
|
|
179 |
if ( header.iUid != iRand_UID)
|
|
180 |
return TDP_UIDMISMATCH;
|
|
181 |
|
|
182 |
// Extract the header information
|
|
183 |
*aCmd = header.iCmdID;
|
|
184 |
*aDataLength = header.iDataLen;
|
|
185 |
|
|
186 |
// Now read the rest of the data
|
|
187 |
TPtr8 q( (TUint8*)aData, bufferLen, bufferLen);
|
|
188 |
ret = iTransport->RequestReceive( &q, q.Size());
|
|
189 |
if ( ret != KErrNone)
|
|
190 |
return TDP_RECVERROR;
|
|
191 |
|
|
192 |
// Increment the UID for next time
|
|
193 |
iRand_UID += 1;
|
|
194 |
return TDP_SUCCESS;
|
|
195 |
}
|
|
196 |
|
|
197 |
|
|
198 |
/*****************************************************************************
|
|
199 |
*
|
|
200 |
* Send Message
|
|
201 |
*
|
|
202 |
****************************************************************************/
|
|
203 |
TDPError CUCCSDeviceProtocol::sendMessage(TPCommand aCmd, int aDataLength, void *aData)
|
|
204 |
{
|
|
205 |
TPHeader header;
|
|
206 |
TInt ret;
|
|
207 |
|
|
208 |
// Check the state
|
|
209 |
assert ( iStatus == TDP_CONNECTED );
|
|
210 |
|
|
211 |
// Check params
|
|
212 |
assert( aData != NULL );
|
|
213 |
|
|
214 |
if( isValidCMDID(aCmd) == 0 )
|
|
215 |
{
|
|
216 |
return TDP_INVALIDCMDID;
|
|
217 |
}
|
|
218 |
|
|
219 |
// Create the header
|
|
220 |
header.iCmdID = aCmd;
|
|
221 |
header.iDataLen = aDataLength;
|
|
222 |
header.iUid = (int)iRand_UID;
|
|
223 |
|
|
224 |
// Send the header first
|
|
225 |
TPtrC8 p( (unsigned char*)&header , sizeof(header) );
|
|
226 |
ret = iTransport->RequestSend( &p, p.Size());
|
|
227 |
|
|
228 |
if (ret != KErrNone)
|
|
229 |
return TDP_SENDERROR;
|
|
230 |
|
|
231 |
// Now send the rest of the data
|
|
232 |
TPtrC8 q( (unsigned char*)aData , aDataLength );
|
|
233 |
ret = iTransport->RequestSend( &q , q.Size());
|
|
234 |
if (ret != KErrNone)
|
|
235 |
return TDP_SENDERROR;
|
|
236 |
|
|
237 |
return TDP_SUCCESS;
|
|
238 |
}
|
|
239 |
|
|
240 |
|
|
241 |
/**********************************************************************************************
|
|
242 |
*
|
|
243 |
* Check IDs
|
|
244 |
*
|
|
245 |
*********************************************************************************************/
|
|
246 |
bool CUCCSDeviceProtocol::isValidCMDID(TPCommand aCommand)
|
|
247 |
{
|
|
248 |
bool ret_val = true;
|
|
249 |
|
|
250 |
switch (aCommand)
|
|
251 |
{
|
|
252 |
case CMD_REQ_SIGNALID:
|
|
253 |
break;
|
|
254 |
|
|
255 |
case CMD_REQ_RENDEZVOUSID:
|
|
256 |
break;
|
|
257 |
|
|
258 |
case CMD_REQ_WAITID:
|
|
259 |
break;
|
|
260 |
|
|
261 |
case CMD_REQ_STARTUSECASEID:
|
|
262 |
break;
|
|
263 |
|
|
264 |
case CMD_REQ_ENDUSECASEID:
|
|
265 |
break;
|
|
266 |
|
|
267 |
case CMD_REQ_GETVARIABLENAMEID:
|
|
268 |
break;
|
|
269 |
|
|
270 |
case CMD_QUITID:
|
|
271 |
break;
|
|
272 |
|
|
273 |
case CMD_REQ_RUNCOMMAND:
|
|
274 |
break;
|
|
275 |
|
|
276 |
default:
|
|
277 |
ret_val = false;
|
|
278 |
break;
|
|
279 |
}
|
|
280 |
|
|
281 |
return ret_val;
|
|
282 |
}
|