24
|
1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// This file contains the implementation of the CSimNtRas class.
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
/**
|
|
19 |
@file
|
|
20 |
*/
|
|
21 |
|
|
22 |
#include "CSimNtras.h"
|
|
23 |
#include "Simlog.h"
|
|
24 |
|
|
25 |
_LIT8(KNtRasWriteText,"CLIENT"); // < Solicitation text, send to start the NTRas server
|
|
26 |
_LIT8(KNtRasReceiveText,"SERVER"); // < NTRas server response, indicating the PPP session can commence.
|
|
27 |
const TInt KNtRasServerResponseTimeout=5; // < Connection attempt time out value in seconds.
|
|
28 |
|
|
29 |
const TDataBits KDefaultDataBits=EData8; // < Default configuration is 8N1
|
|
30 |
const TParity KDefaultParity=EParityNone; // < Default configuration is 8N1
|
|
31 |
const TStopBits KDefaultStopBits=EStop1; // < Default configuration is 8N1
|
|
32 |
const TText8 KDefaultXon=17; // < Default XON character is <ctrl>Q
|
|
33 |
const TText8 KDefaultXoff=19; // < Default XOFF character is <ctrl>S
|
|
34 |
const TInt KTxMaxRetries=3; // < Maximum number of transmit retries before giving up
|
|
35 |
|
|
36 |
CSimNtRas* CSimNtRas::NewL(CSimPhone* aPhone)
|
|
37 |
/**
|
|
38 |
* Standard two phase constructor.
|
|
39 |
*/
|
|
40 |
{
|
|
41 |
CSimNtRas* self=new(ELeave) CSimNtRas(aPhone);
|
|
42 |
CleanupStack::PushL(self);
|
|
43 |
self->ConstructL();
|
|
44 |
CleanupStack::Pop();
|
|
45 |
return self;
|
|
46 |
}
|
|
47 |
|
|
48 |
CSimNtRas::CSimNtRas(CSimPhone* aPhone)
|
|
49 |
: iPhone(aPhone), iPortOpen(EFalse)
|
|
50 |
/**
|
|
51 |
* Trivial first phase constructor.
|
|
52 |
*/
|
|
53 |
{
|
|
54 |
}
|
|
55 |
|
|
56 |
void CSimNtRas::ConstructL()
|
|
57 |
/**
|
|
58 |
* Second phase constructor. Create heap-based objects, in this instance the timer
|
|
59 |
* and the Comm Port classes.
|
|
60 |
*/
|
|
61 |
{
|
|
62 |
iTimer=CSimTimer::NewL(iPhone);
|
|
63 |
CommConstructL(CActive::EPriorityStandard,CActive::EPriorityStandard);
|
|
64 |
}
|
|
65 |
|
|
66 |
CSimNtRas::~CSimNtRas()
|
|
67 |
/**
|
|
68 |
* Standard destructor. Close Comm Port and destroy heap-based objects.
|
|
69 |
*/
|
|
70 |
{
|
|
71 |
if(iPortOpen)
|
|
72 |
{
|
|
73 |
CommCancel();
|
|
74 |
CommClose();
|
|
75 |
}
|
|
76 |
delete iTimer;
|
|
77 |
}
|
|
78 |
|
|
79 |
TInt CSimNtRas::Connect(const TDesC& aCsyName, const TDesC& aPort,
|
|
80 |
const TCommConfigV01& aConfig, MNTRasCallBack* aCallback)
|
|
81 |
/**
|
|
82 |
* Attempt to connect to an NT Ras "Cable Connection" service.
|
|
83 |
* @param aCsyName The CSY to be used for the connection.
|
|
84 |
* @param aPort The Port to be used for the connection.
|
|
85 |
* @param aConfig The port configuration to be used for the connection.
|
|
86 |
* @param aCallback The callback class that must be signalled when the connection completes.
|
|
87 |
* @return TInt Standard error value.
|
|
88 |
*/
|
|
89 |
{
|
|
90 |
LOGDATA1("Starting to attempt a NT RAS Connection");
|
|
91 |
iReadBuf.Zero();
|
|
92 |
iInputBuf.Zero();
|
|
93 |
|
|
94 |
// Open and configuration the port
|
|
95 |
TInt ret=CommOpen(aCsyName,aPort,ECommShared);
|
|
96 |
if(ret!=KErrNone)
|
|
97 |
return ret;
|
|
98 |
iPortOpen=ETrue;
|
|
99 |
|
|
100 |
TCommConfig configPckg;
|
|
101 |
TCommConfigV01& config=configPckg();
|
|
102 |
iCommPort.Config(configPckg);
|
|
103 |
|
|
104 |
config.iDataBits=KDefaultDataBits;
|
|
105 |
config.iParity=KDefaultParity;
|
|
106 |
config.iStopBits=KDefaultStopBits;
|
|
107 |
config.iXonChar=KDefaultXon;
|
|
108 |
config.iXoffChar=KDefaultXoff;
|
|
109 |
config.iTerminatorCount=0;
|
|
110 |
config.iSpecialRate=0;
|
|
111 |
|
|
112 |
config.iRate=aConfig.iRate;
|
|
113 |
config.iHandshake=aConfig.iHandshake;
|
|
114 |
ret=iCommPort.SetConfig(configPckg);
|
|
115 |
if(ret!=KErrNone)
|
|
116 |
return ret;
|
|
117 |
|
|
118 |
// Power up the port ready for receives and transmits
|
|
119 |
CommReadReady();
|
|
120 |
CommWriteReady();
|
|
121 |
|
|
122 |
iAttemptCnt=0;
|
|
123 |
iCallback=aCallback;
|
|
124 |
CommReadOneOrMore(iReadBuf);
|
|
125 |
AttemptConnect();
|
|
126 |
return KErrNone;
|
|
127 |
}
|
|
128 |
|
|
129 |
void CSimNtRas::AttemptConnect()
|
|
130 |
/**
|
|
131 |
* Transmit "CLIENT" and attempt a connection
|
|
132 |
*/
|
|
133 |
{
|
|
134 |
LOGDATA1("NTRAS Writing CLIENT...");
|
|
135 |
iAttemptCnt++;
|
|
136 |
// Send the soliciting message, and await the NT Ras server's response.
|
|
137 |
CommWrite(KNtRasWriteText);
|
|
138 |
iTimer->Start(KNtRasServerResponseTimeout,this);
|
|
139 |
}
|
|
140 |
|
|
141 |
void CSimNtRas::Cancel()
|
|
142 |
/**
|
|
143 |
* Cancel any outstanding comm port reads, writes or timer requests.
|
|
144 |
*/
|
|
145 |
{
|
|
146 |
iTimer->Cancel();
|
|
147 |
CommCancel();
|
|
148 |
}
|
|
149 |
|
|
150 |
void CSimNtRas::Terminate()
|
|
151 |
/**
|
|
152 |
* Terminate the NTRas connection. If opened, cancel any outstanding requests and close
|
|
153 |
* the port. Also, cancel any outstanding timer request.
|
|
154 |
*/
|
|
155 |
{
|
|
156 |
if(iPortOpen)
|
|
157 |
{
|
|
158 |
CommCancel();
|
|
159 |
CommClose();
|
|
160 |
}
|
|
161 |
iTimer->Cancel();
|
|
162 |
}
|
|
163 |
|
|
164 |
void CSimNtRas::CommReadComplete(TInt aStatus)
|
|
165 |
/**
|
|
166 |
* Serial port read completion callback. If there's no read error, append the read
|
|
167 |
* data to the input buffer and scan for the server's response.
|
|
168 |
* If no match is found, re-post the read.
|
|
169 |
* @param aStatus Standard error value, indicating the result of the read request.
|
|
170 |
*/
|
|
171 |
{
|
|
172 |
LOGDATA2("NTRAS Rx Completion: %S",&iInputBuf);
|
|
173 |
if(aStatus!=KErrNone)
|
|
174 |
{
|
|
175 |
iTimer->Cancel();
|
|
176 |
iCallback->NTRasCallBack(aStatus);
|
|
177 |
return;
|
|
178 |
}
|
|
179 |
|
|
180 |
if((iInputBuf.Length()+iReadBuf.Length())>KInputBufferSize)
|
|
181 |
{
|
|
182 |
iTimer->Cancel();
|
|
183 |
iCallback->NTRasCallBack(KErrNotFound);
|
|
184 |
return;
|
|
185 |
}
|
|
186 |
|
|
187 |
iInputBuf.Append(iReadBuf);
|
|
188 |
TInt pos=iInputBuf.FindF(KNtRasReceiveText);
|
|
189 |
if(pos>=0)
|
|
190 |
{
|
|
191 |
iTimer->Cancel();
|
|
192 |
iCallback->NTRasCallBack(KErrNone);
|
|
193 |
LOGDATA1("NTRAS Received SERVER!");
|
|
194 |
return;
|
|
195 |
}
|
|
196 |
|
|
197 |
CommReadOneOrMore(iReadBuf);
|
|
198 |
}
|
|
199 |
|
|
200 |
void CSimNtRas::CommWriteComplete(TInt aStatus)
|
|
201 |
/**
|
|
202 |
* Serial port write completion callback. Terminate the connection attempt if an error
|
|
203 |
* has occurred.
|
|
204 |
*/
|
|
205 |
{
|
|
206 |
LOGDATA1("NTRAS Write Completion");
|
|
207 |
if(aStatus!=KErrNone)
|
|
208 |
{
|
|
209 |
iTimer->Cancel();
|
|
210 |
iCallback->NTRasCallBack(aStatus);
|
|
211 |
}
|
|
212 |
}
|
|
213 |
|
|
214 |
void CSimNtRas::TimerCallBack(TInt /*aId*/)
|
|
215 |
/**
|
|
216 |
* Timer expired callback. Give up the attempted connection with a time-out error.
|
|
217 |
*/
|
|
218 |
{
|
|
219 |
LOGDATA1("NTRAS Timeout Occurred");
|
|
220 |
CommWriteCancel();
|
|
221 |
if(iAttemptCnt<KTxMaxRetries)
|
|
222 |
AttemptConnect();
|
|
223 |
else
|
|
224 |
{
|
|
225 |
CommReadCancel();
|
|
226 |
iCallback->NTRasCallBack(KErrTimedOut);
|
|
227 |
}
|
|
228 |
}
|