|
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 // |
|
15 |
|
16 #include "rasdef.h" |
|
17 #include <cdblen.h> |
|
18 |
|
19 CRasConnect::CRasConnect(CNtRas* aNtRas) |
|
20 : iNtRas(aNtRas) |
|
21 { |
|
22 __DECLARE_NAME(_S("CRasConnect")); |
|
23 } |
|
24 |
|
25 CRasConnect::~CRasConnect() |
|
26 { |
|
27 |
|
28 if(iCommOpen) |
|
29 { |
|
30 Disconnect(); |
|
31 CommClose(); |
|
32 } |
|
33 CommDelete(); |
|
34 TimerDelete(); |
|
35 delete iChat; |
|
36 } |
|
37 |
|
38 CRasConnect* CRasConnect::NewL(CNtRas* aRas) |
|
39 { |
|
40 CRasConnect* p = new (ELeave) CRasConnect(aRas); |
|
41 CleanupStack::PushL(p); |
|
42 p->ConstructL(); |
|
43 CleanupStack::Pop(); |
|
44 return p; |
|
45 } |
|
46 |
|
47 void CRasConnect::ConstructL() |
|
48 { |
|
49 |
|
50 CommConstructL(KCommReadPriority,KCommWritePriority); |
|
51 TimerConstructL(KCommTimerPrioiry); |
|
52 iState = ERasConClosed; |
|
53 iChat = new (ELeave) CCommChatter(this, KChatterPriority); |
|
54 iChat->CreateL(KChatBufferSize); |
|
55 iNtRas->ReadInt(NIF_IFSERVERMODE, iServerMode); |
|
56 |
|
57 if(iServerMode) |
|
58 iString=CCommChatString::NewL(_L8("CLIENT"), EFalse); |
|
59 else |
|
60 iString=CCommChatString::NewL(_L8("SERVER"), EFalse); |
|
61 |
|
62 iChat->AddString(iString); |
|
63 TFileName csy; |
|
64 TName port; |
|
65 |
|
66 TBuf<KCommsDbSvrMaxColumnNameLength> columnName=TPtrC(MODEM); |
|
67 columnName.Append(TChar(KSlashChar)); |
|
68 columnName.Append(TPtrC(MODEM_CSY_NAME)); |
|
69 User::LeaveIfError(iNtRas->ReadDes(columnName, csy)); |
|
70 |
|
71 columnName.SetLength(0); |
|
72 columnName=TPtrC(MODEM); |
|
73 columnName.Append(TChar(KSlashChar)); |
|
74 columnName.Append(TPtrC(MODEM_PORT_NAME)); |
|
75 User::LeaveIfError(iNtRas->ReadDes(columnName, port)); |
|
76 |
|
77 TInt err; |
|
78 err = CommOpen(csy, port, ECommShared); |
|
79 if (err==KErrNotFound) |
|
80 { |
|
81 Util::CommInit(); |
|
82 err = CommOpen(csy, port, ECommShared); |
|
83 } |
|
84 User::LeaveIfError(err); |
|
85 iCommOpen=ETrue; |
|
86 StartL(); |
|
87 } |
|
88 |
|
89 void CRasConnect::StartL() |
|
90 { |
|
91 User::LeaveIfError(InitComm()); |
|
92 iCommPort.ResetBuffers(); |
|
93 Disconnect(); // Will drop the lines |
|
94 TimerAfter(KCommLineDropTime); |
|
95 iState = ERasConDropLines; |
|
96 } |
|
97 |
|
98 TPtrC8 CRasConnect::ExcessData() |
|
99 { |
|
100 return TPtrC8(iRxBuf.Ptr()+iRxBufOffset, iRxBuf.Length()-iRxBufOffset); |
|
101 } |
|
102 |
|
103 void CRasConnect::CommReadComplete(TInt aStatus) |
|
104 { |
|
105 if (aStatus!=KErrNone) |
|
106 { |
|
107 Complete(aStatus); |
|
108 return; |
|
109 } |
|
110 for (iRxBufOffset=0; iRxBufOffset<iRxBuf.Length() && iState!=ERasConConnected; iRxBufOffset++) |
|
111 iChat->AddChar(iRxBuf[iRxBufOffset]); |
|
112 if (iState==ERasConConnected) |
|
113 { |
|
114 Complete(KErrNone); |
|
115 } |
|
116 else |
|
117 { |
|
118 CommReadOneOrMore(iRxBuf); |
|
119 iRxBufOffset = 0; |
|
120 } |
|
121 } |
|
122 |
|
123 void CRasConnect::CommWriteComplete(TInt aStatus) |
|
124 { |
|
125 if (aStatus!=KErrNone) |
|
126 { |
|
127 Complete(aStatus); |
|
128 return; |
|
129 } |
|
130 else if (iState==ERasConConnecting) |
|
131 { |
|
132 iTries = 0; |
|
133 SendToken(); |
|
134 } |
|
135 } |
|
136 |
|
137 void CRasConnect::TimerComplete(TInt /*aStatus */) |
|
138 { |
|
139 if (iState == ERasConDropLines) |
|
140 { |
|
141 TInt ret; |
|
142 if (ret = InitComm(), ret!=KErrNone) |
|
143 { |
|
144 Complete(ret); |
|
145 return; |
|
146 } |
|
147 |
|
148 iCommPort.SetSignalsToMark(KSignalRTS | KSignalDTR); |
|
149 |
|
150 if(iServerMode) |
|
151 { |
|
152 CommReadOneOrMore(iRxBuf); |
|
153 } |
|
154 else |
|
155 { |
|
156 CommWriteReady(); |
|
157 TimerAfter(KCommTimeout); |
|
158 } |
|
159 iState = ERasConConnecting; |
|
160 } |
|
161 else |
|
162 { |
|
163 Complete(KErrTimedOut); |
|
164 } |
|
165 } |
|
166 |
|
167 void CRasConnect::ChatStringMatch(CCommChatString* /*aString */) |
|
168 { |
|
169 if(iState==ERasConTokenSent) |
|
170 { |
|
171 iState = ERasConConnected; |
|
172 if(iServerMode) |
|
173 iRxBufOffset=0; |
|
174 } |
|
175 else if(iServerMode && iState!=ERasConTokenSent && !CommIsWriting()) |
|
176 { |
|
177 iChat->RemoveString(iString); |
|
178 delete iString; |
|
179 TRAPD(res, iString=CCommChatString::NewL(_L8("~"), EFalse);) |
|
180 if(res!=KErrNone) // Comment out here |
|
181 { |
|
182 Complete(res); |
|
183 return; |
|
184 } |
|
185 CommWriteReady(); |
|
186 } |
|
187 } |
|
188 |
|
189 void CRasConnect::ChatTimeout() |
|
190 { |
|
191 SendToken(); |
|
192 } |
|
193 |
|
194 void CRasConnect::SendToken() |
|
195 { |
|
196 |
|
197 // Changed to work on PPPD machines, comment line below and uncomment block for |
|
198 // NTRas to work |
|
199 //iNtRas->ConnectComplete(KErrNone); |
|
200 |
|
201 iState = ERasConTokenSent; |
|
202 if (iTries++>=KChatMaxTries) |
|
203 { |
|
204 Complete(KErrCouldNotConnect); |
|
205 return; |
|
206 } |
|
207 if(iServerMode) |
|
208 CommWrite(_L8("SERVER")); |
|
209 else |
|
210 CommWrite(_L8("CLIENT")); |
|
211 |
|
212 if(!CommIsReading()) |
|
213 CommReadOneOrMore(iRxBuf); |
|
214 iChat->StartTimer(KChatTimeout); |
|
215 //*/ |
|
216 } |
|
217 |
|
218 void CRasConnect::Complete(TInt aStatus) |
|
219 { |
|
220 CommCancel(); |
|
221 TimerCancel(); |
|
222 iChat->StopTimer(); |
|
223 iNtRas->ConnectComplete(aStatus); |
|
224 } |
|
225 |
|
226 void CRasConnect::Disconnect() |
|
227 { |
|
228 |
|
229 CommCancel(); |
|
230 |
|
231 //TInt ret; |
|
232 TCommConfig cbuf; |
|
233 TCommConfigV01 &cfg=cbuf(); |
|
234 iCommPort.Config(cbuf); |
|
235 cfg.iHandshake = KConfigFreeRTS | KConfigFreeDTR; |
|
236 iCommPort.SetConfig(cbuf); |
|
237 iCommPort.SetSignalsToSpace(KSignalRTS | KSignalDTR); |
|
238 } |
|
239 |
|
240 TInt CRasConnect::InitComm() |
|
241 { |
|
242 TInt ret; |
|
243 TCommConfig cbuf; |
|
244 TCommConfigV01 &cfg=cbuf(); |
|
245 iCommPort.Config(cbuf); |
|
246 |
|
247 TUint32 spd; |
|
248 iNtRas->ReadInt(RAS_COMMSPEED, spd); |
|
249 ret = Util::GetCommSpeed(spd, cfg.iRate); |
|
250 |
|
251 TBuf<32> buf; |
|
252 buf.SetMax(); |
|
253 iNtRas->ReadDes(RAS_COMMDATA, buf); |
|
254 if (ret = Util::GetCommFormat(buf, cfg.iDataBits, cfg.iStopBits, cfg.iParity), ret!=KErrNone) |
|
255 return ret; |
|
256 |
|
257 buf.SetMax(); |
|
258 iNtRas->ReadDes(RAS_COMMHANDSHAKE, buf); |
|
259 if (ret = Util::GetCommHandshake(buf, cfg.iHandshake), ret!=KErrNone) |
|
260 return ret; |
|
261 |
|
262 if (ret = iCommPort.SetConfig(cbuf), ret!=KErrNone) |
|
263 return ret; |
|
264 |
|
265 return KErrNone; |
|
266 } |