|
1 // tcpcsy.cpp |
|
2 // |
|
3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "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 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include <c32comm.h> |
|
14 #include "panic.h" |
|
15 #include "tcpcsy.h" |
|
16 #include "tcpcsylog.h" |
|
17 |
|
18 |
|
19 // |
|
20 // CTcpPort. |
|
21 // |
|
22 |
|
23 TInt CTcpPort::LinkOffset() |
|
24 { |
|
25 return _FOFF(CTcpPort, iLink); |
|
26 } |
|
27 |
|
28 CTcpPort* CTcpPort::NewLC(CTcpPortFactory& aFactory, CConfig::TMode aMode, TUint aProtocolFamily, TUint aProtocol, const TInetAddr& aAddress, RSocketServ& aSocketServ) |
|
29 { |
|
30 CTcpPort* self = new(ELeave) CTcpPort(aFactory); |
|
31 CleanupClosePushL(*self); |
|
32 self->ConstructL(aMode, aProtocolFamily, aProtocol, aAddress, aSocketServ); |
|
33 return self; |
|
34 } |
|
35 |
|
36 CTcpPort::CTcpPort(CTcpPortFactory& aFactory) |
|
37 : iFactory(aFactory) |
|
38 { |
|
39 LOG("Created new CTcpPort object 0x%08x", this); |
|
40 } |
|
41 |
|
42 void CTcpPort::ConstructL(CConfig::TMode aMode, TUint aProtocolFamily, TUint aProtocol, const TInetAddr& aAddress, RSocketServ& aSocketServ) |
|
43 { |
|
44 iInetAddr = aAddress; |
|
45 |
|
46 if (aMode == CConfig::EActive) |
|
47 { |
|
48 iConnector = CConnector::NewL(aSocketServ, iSocket, aProtocolFamily, aProtocol, iInetAddr, *this); |
|
49 } |
|
50 else |
|
51 { |
|
52 iListener = CListener::NewL(aSocketServ, iSocket, aProtocolFamily, aProtocol, iInetAddr, *this); |
|
53 } |
|
54 |
|
55 iReader = CReader::NewL(iSocket, *this); |
|
56 iWriter = CWriter::NewL(iSocket, *this); |
|
57 } |
|
58 |
|
59 CTcpPort::~CTcpPort() |
|
60 { |
|
61 delete iWriter; |
|
62 delete iReader; |
|
63 delete iConnector; |
|
64 if (iSocketConnected) |
|
65 { |
|
66 TRequestStatus status; |
|
67 iSocket.Shutdown(RSocket::ENormal, status); |
|
68 User::WaitForRequest(status); |
|
69 iSocket.Close(); |
|
70 } |
|
71 iLink.Deque(); |
|
72 } |
|
73 |
|
74 void CTcpPort::StartRead(const TAny*, TInt aLength) |
|
75 { |
|
76 LOG("CTcpPort::StartRead this=0x%08x, aLength=%d", this, aLength); |
|
77 |
|
78 if (iError) |
|
79 { |
|
80 ReadError(iError); |
|
81 } |
|
82 else if (aLength == 0) |
|
83 { |
|
84 ReadCompleted(KErrNone); |
|
85 } |
|
86 else |
|
87 { |
|
88 if (aLength < 0) |
|
89 { |
|
90 iReader->StartRead(-aLength, CReader::EOneOrMore); |
|
91 } |
|
92 else |
|
93 { |
|
94 iReader->StartRead(aLength, CReader::EFull); |
|
95 } |
|
96 } |
|
97 } |
|
98 |
|
99 void CTcpPort::ReadCancel() |
|
100 { |
|
101 LOG("CTcpPort::ReadCancel this=0x%08x", this); |
|
102 iReader->Abort(); |
|
103 ReadError(KErrCancel); |
|
104 } |
|
105 |
|
106 TInt CTcpPort::QueryReceiveBuffer(TInt& aLength) const |
|
107 { |
|
108 iReader->GetBufferLength(aLength); |
|
109 return KErrNone; |
|
110 } |
|
111 |
|
112 void CTcpPort::ResetBuffers(TUint) |
|
113 { |
|
114 // Doesn't really have much meaning for the TCPCSY as reads and writes are mapped directly onto RSocket calls. |
|
115 } |
|
116 |
|
117 void CTcpPort::StartWrite(const TAny* aClientBuffer, TInt aLength) |
|
118 { |
|
119 LOG("CTcpPort::StartWrite this=0x%08x, aClientBuff=0x%08x, aLength=%d", this, aClientBuffer, aLength); |
|
120 |
|
121 TInt err = KErrNone; |
|
122 |
|
123 if (iError) |
|
124 { |
|
125 err = iError; |
|
126 } |
|
127 else if (aLength > 0) |
|
128 { |
|
129 if (iWriteBuf.MaxLength() < aLength) |
|
130 { |
|
131 err = iWriteBuf.ReAlloc(aLength); |
|
132 } |
|
133 |
|
134 if (err == KErrNone) |
|
135 { |
|
136 iWriteBuf.Zero(); |
|
137 err = IPCRead(aClientBuffer, iWriteBuf, 0); |
|
138 if (err == KErrNone) |
|
139 { |
|
140 iWriter->StartWrite(iWriteBuf); |
|
141 } |
|
142 } |
|
143 } |
|
144 |
|
145 if (err || (aLength == 0)) |
|
146 { |
|
147 WriteComplete(err); |
|
148 } |
|
149 } |
|
150 |
|
151 void CTcpPort::WriteCancel() |
|
152 { |
|
153 LOG("CTcpPort::WriteCancel this=0x%08x", this); |
|
154 iWriter->Abort(); |
|
155 WriteComplete(KErrCancel); |
|
156 } |
|
157 |
|
158 void CTcpPort::Break(TInt) |
|
159 { |
|
160 } |
|
161 |
|
162 void CTcpPort::BreakCancel() |
|
163 { |
|
164 } |
|
165 |
|
166 TInt CTcpPort::GetConfig(TDes8&) const |
|
167 { |
|
168 return KErrNone; |
|
169 } |
|
170 |
|
171 TInt CTcpPort::SetConfig(const TDesC8&) |
|
172 { |
|
173 return KErrNone; |
|
174 } |
|
175 |
|
176 TInt CTcpPort::GetCaps(TDes8&) |
|
177 { |
|
178 return KErrNone; |
|
179 } |
|
180 |
|
181 TInt CTcpPort::SetServerConfig(const TDesC8&) |
|
182 { |
|
183 return KErrNone; |
|
184 } |
|
185 |
|
186 TInt CTcpPort::GetServerConfig(TDes8&) |
|
187 { |
|
188 return KErrNone; |
|
189 } |
|
190 |
|
191 TInt CTcpPort::GetSignals(TUint&) |
|
192 { |
|
193 return KErrNone; |
|
194 } |
|
195 |
|
196 TInt CTcpPort::SetSignalsToMark(TUint) |
|
197 { |
|
198 return KErrNone; |
|
199 } |
|
200 |
|
201 TInt CTcpPort::SetSignalsToSpace(TUint) |
|
202 { |
|
203 return KErrNone; |
|
204 } |
|
205 |
|
206 TInt CTcpPort::GetReceiveBufferLength(TInt&) const |
|
207 { |
|
208 return KErrNone; |
|
209 } |
|
210 |
|
211 TInt CTcpPort::SetReceiveBufferLength(TInt) |
|
212 { |
|
213 return KErrNone; |
|
214 } |
|
215 |
|
216 void CTcpPort::Destruct() |
|
217 { |
|
218 delete this; |
|
219 } |
|
220 |
|
221 void CTcpPort::NotifySignalChange(TUint) |
|
222 { |
|
223 } |
|
224 |
|
225 void CTcpPort::NotifySignalChangeCancel() |
|
226 { |
|
227 } |
|
228 |
|
229 |
|
230 void CTcpPort::NotifyConfigChange() |
|
231 { |
|
232 } |
|
233 |
|
234 void CTcpPort::NotifyConfigChangeCancel() |
|
235 { |
|
236 } |
|
237 |
|
238 void CTcpPort::NotifyFlowControlChange() |
|
239 { |
|
240 } |
|
241 |
|
242 void CTcpPort::NotifyFlowControlChangeCancel() |
|
243 { |
|
244 } |
|
245 |
|
246 void CTcpPort::NotifyBreak() |
|
247 { |
|
248 } |
|
249 |
|
250 void CTcpPort::NotifyBreakCancel() |
|
251 { |
|
252 } |
|
253 |
|
254 void CTcpPort::NotifyDataAvailable() |
|
255 { |
|
256 } |
|
257 |
|
258 void CTcpPort::NotifyDataAvailableCancel() |
|
259 { |
|
260 } |
|
261 |
|
262 void CTcpPort::NotifyOutputEmpty() |
|
263 { |
|
264 } |
|
265 |
|
266 void CTcpPort::NotifyOutputEmptyCancel() |
|
267 { |
|
268 } |
|
269 |
|
270 TInt CTcpPort::GetFlowControlStatus(TFlowControl&) |
|
271 { |
|
272 return KErrNotSupported; |
|
273 } |
|
274 |
|
275 TInt CTcpPort::GetRole(TCommRole& aRole) |
|
276 { |
|
277 aRole = iRole; |
|
278 return KErrNone; |
|
279 } |
|
280 |
|
281 TInt CTcpPort::SetRole(TCommRole aRole) |
|
282 { |
|
283 iRole = aRole; |
|
284 return KErrNone; |
|
285 } |
|
286 |
|
287 void CTcpPort::FreeMemory() |
|
288 { |
|
289 } |
|
290 |
|
291 void CTcpPort::ConnectionComplete(TInt aError) |
|
292 { |
|
293 LOG("CTcpPort::ConnectionComplete this=0x%08x, aError=%d", this, aError); |
|
294 if (aError == KErrNone) |
|
295 { |
|
296 iSocketConnected = ETrue; |
|
297 iReader->SocketConnected(); |
|
298 iWriter->SocketConnected(); |
|
299 } |
|
300 else |
|
301 { |
|
302 iError = aError; |
|
303 } |
|
304 } |
|
305 |
|
306 void CTcpPort::ListenComplete(TInt aError) |
|
307 { |
|
308 LOG("CTcpPort::ListenComplete this=0x%08x, aError=%d", this, aError); |
|
309 if (aError == KErrNone) |
|
310 { |
|
311 iReader->SocketConnected(); |
|
312 iWriter->SocketConnected(); |
|
313 } |
|
314 else |
|
315 { |
|
316 iError = aError; |
|
317 } |
|
318 } |
|
319 |
|
320 void CTcpPort::ReadComplete(const TDesC8& aData) |
|
321 { |
|
322 LOG("CTcpPort::ReadComplete this=0x%08x", this); |
|
323 TInt err = IPCWrite(NULL, aData, 0); |
|
324 ReadCompleted(KErrNone); |
|
325 } |
|
326 |
|
327 void CTcpPort::ReadError(TInt aError) |
|
328 { |
|
329 LOG("CTcpPort::ReadError this=0x%08x, aError=%d", this, aError); |
|
330 ReadCompleted(aError); |
|
331 } |
|
332 |
|
333 void CTcpPort::WriteComplete(TInt aError) |
|
334 { |
|
335 LOG("CTcpPort::WriteComplete this=0x%08x, aError=%d", this, aError); |
|
336 WriteCompleted(aError); |
|
337 } |
|
338 |
|
339 |
|
340 |
|
341 // |
|
342 // CTcpPortFactory. |
|
343 // |
|
344 |
|
345 CTcpPortFactory* CTcpPortFactory::NewL() |
|
346 { |
|
347 CTcpPortFactory* self = new(ELeave)CTcpPortFactory; |
|
348 CleanupClosePushL(*self); |
|
349 self->ConstructL(); |
|
350 CleanupStack::Pop(self); |
|
351 return self; |
|
352 } |
|
353 |
|
354 CPort* CTcpPortFactory::NewPortL(const TUint aUnit) |
|
355 { |
|
356 TInetAddr inetAddr; |
|
357 iConfig->GetAddressL(aUnit, inetAddr); |
|
358 CTcpPort* port = CTcpPort::NewLC(*this, iConfig->ModeL(aUnit), iConfig->ProtocolFamilyL(aUnit), iConfig->ProtocolL(aUnit), inetAddr, iSocketServ); |
|
359 TBuf<10> name; |
|
360 name.Num(aUnit); |
|
361 port->SetNameL(&name); |
|
362 CleanupStack::Pop(port); |
|
363 iPorts.AddLast(*port); |
|
364 return port; |
|
365 } |
|
366 |
|
367 void CTcpPortFactory::Info(TSerialInfo& aSerialInfo) |
|
368 /** |
|
369 * This method fills information into the passed structure. It is required for factory objects. |
|
370 * |
|
371 * @param aSerialInfo - a reference to the structure to fill in. |
|
372 * |
|
373 * @return None |
|
374 */ |
|
375 { |
|
376 aSerialInfo.iDescription = KCsyName; |
|
377 aSerialInfo.iName = KCsyName; |
|
378 iConfig->UnitRange(aSerialInfo.iLowUnit, aSerialInfo.iHighUnit); |
|
379 } |
|
380 |
|
381 CTcpPortFactory::CTcpPortFactory() |
|
382 : iPorts(CTcpPort::LinkOffset()) |
|
383 { |
|
384 } |
|
385 |
|
386 void CTcpPortFactory::ConstructL() |
|
387 { |
|
388 #ifdef __FLOG_ACTIVE |
|
389 |
|
390 #ifdef LOG_CSY_EVENTS |
|
391 #pragma message("General logging enabled") |
|
392 (void) iEventLogger.Connect(); |
|
393 iEventLogger.SetLogTags(KDebugSubSystem(), KDebugCategoryEvents()); |
|
394 #endif // LOG_CSY_EVENTS |
|
395 |
|
396 #ifdef LOG_CSY_TX |
|
397 #pragma message("Tx logging enabled") |
|
398 (void) iTxLogger.Connect(); |
|
399 iTxLogger.SetLogTags(KDebugSubSystem(), KDebugCategoryTx()); |
|
400 #endif // LOG_CSY_TX |
|
401 |
|
402 #ifdef LOG_CSY_RX |
|
403 #pragma message("Rx logging enabled") |
|
404 (void) iRxLogger.Connect(); |
|
405 iRxLogger.SetLogTags(KDebugSubSystem(), KDebugCategoryRx()); |
|
406 #endif // LOG_CSY_RX |
|
407 |
|
408 #endif // __FLOG_ACTIVE |
|
409 |
|
410 SetNameL(&(KCsyName())); |
|
411 iConfig = CConfig::NewL(*this); |
|
412 User::LeaveIfError(iSocketServ.Connect()); |
|
413 } |
|
414 |
|
415 CTcpPortFactory::~CTcpPortFactory() |
|
416 { |
|
417 delete iConfig; |
|
418 iSocketServ.Close(); |
|
419 |
|
420 #ifdef __FLOG_ACTIVE |
|
421 |
|
422 #ifdef LOG_CSY_EVENTS |
|
423 iEventLogger.Close(); |
|
424 #endif // LOG_CSY_EVENTS |
|
425 |
|
426 #ifdef LOG_CSY_TX |
|
427 iTxLogger.Close(); |
|
428 #endif // LOG_CSY_TX |
|
429 |
|
430 #ifdef LOG_CSY_RX |
|
431 iRxLogger.Close(); |
|
432 #endif // LOG_CSY_RX |
|
433 |
|
434 #endif // __FLOG_ACTIVE |
|
435 } |
|
436 |
|
437 TSecurityPolicy CTcpPortFactory::PortPlatSecCapability(TUint /*aPort*/) const |
|
438 { |
|
439 return TSecurityPolicy(ECapabilityNetworkServices); |
|
440 } |
|
441 |
|
442 extern "C" |
|
443 { |
|
444 IMPORT_C CSerial * LibEntry(void); |
|
445 } |
|
446 |
|
447 |
|
448 EXPORT_C CSerial* LibEntry(void) |
|
449 { |
|
450 return CTcpPortFactory::NewL(); |
|
451 } |
|
452 |
|
453 void Panic(TTcpCsyPanicReason aReason) |
|
454 { |
|
455 _LIT(KCategory, "tcpcsy"); |
|
456 User::Panic(KCategory(), aReason); |
|
457 } |