|
1 /* |
|
2 * Copyright (c) 2004 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: Implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <es_enum.h> |
|
19 #include <in_sock.h> |
|
20 |
|
21 #include "CTcTcpManager.h" |
|
22 #include "TTcTcpFactory.h" |
|
23 #include "debuglog.h" |
|
24 |
|
25 CTcTcpManager* CTcTcpManager::NewL( const TTcTcpFactory& aFactory ) |
|
26 { |
|
27 CTcTcpManager* self = new( ELeave ) CTcTcpManager( aFactory ); |
|
28 |
|
29 CleanupStack::PushL( self ); |
|
30 self->ConstructL( ); |
|
31 CleanupStack::Pop( self ); |
|
32 |
|
33 return self; |
|
34 } |
|
35 |
|
36 CTcTcpManager::~CTcTcpManager() |
|
37 { |
|
38 LOG( _L("CTcTcpManager::~CTcTcpManager()") ); |
|
39 // Close connection |
|
40 Close(); |
|
41 iConnServ.Close(); |
|
42 iSockServ.Close(); |
|
43 delete iRemoteAddr; |
|
44 } |
|
45 |
|
46 CTcTcpManager::CTcTcpManager( const TTcTcpFactory& aFactory ) |
|
47 : CActive( CActive::EPriorityStandard ), |
|
48 iConn( aFactory.iPort ), iIAPId( aFactory.iIAPId ) |
|
49 { |
|
50 CActiveScheduler::Add( this ); |
|
51 } |
|
52 |
|
53 void CTcTcpManager::ConstructL() |
|
54 { |
|
55 LOG( _L("CTcTcpManager::ConstructL()") ); |
|
56 // Open a session to socket server and connection server |
|
57 User::LeaveIfError( iSockServ.Connect() ); |
|
58 User::LeaveIfError( iConnServ.Open( iSockServ ) ); |
|
59 iConn.SetObserver( this ); |
|
60 iConn.SetConnection( &iSockServ, &iConnServ ); |
|
61 |
|
62 iPrefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt ); |
|
63 iPrefs.SetDirection( ECommDbConnectionDirectionOutgoing ); |
|
64 iPrefs.SetIapId( iIAPId ); |
|
65 } |
|
66 |
|
67 void CTcTcpManager::RunL() |
|
68 { |
|
69 LOG( _L("CTcTcpManager::RunL() start") ); |
|
70 |
|
71 // Report any intermediate connection errors |
|
72 TInt status( iStatus.Int() ); |
|
73 if( status != KErrNone ) |
|
74 { |
|
75 BearerCompletion( MTcBearerObserver::EConnect, status ); |
|
76 } |
|
77 else |
|
78 { |
|
79 TInt err( KErrNone ); |
|
80 // Start waiting for connections |
|
81 if ( iRemoteAddr ) |
|
82 { |
|
83 TRAP( err, iConn.ConnectL( *iRemoteAddr ) ); |
|
84 } |
|
85 else |
|
86 { |
|
87 TRAP( err, iConn.ConnectL() ); |
|
88 } |
|
89 // Catch and report errors |
|
90 if( err ) |
|
91 { |
|
92 BearerCompletion( MTcBearerObserver::EConnect, err ); |
|
93 } |
|
94 } |
|
95 |
|
96 LOG( _L("CTcTcpManager::RunL() end") ); |
|
97 } |
|
98 |
|
99 void CTcTcpManager::DoCancel() |
|
100 { |
|
101 LOG( _L("CTcTcpManager::DoCancel()") ); |
|
102 // Cannot use Stop() as it forces the whole IAP connection down |
|
103 // even if somebody else is still using it. |
|
104 iConnServ.Close(); |
|
105 } |
|
106 |
|
107 void CTcTcpManager::ConnectL( TInetAddr* aRemoteAddr ) |
|
108 { |
|
109 LOG( _L("CTcTcpManager::ConnectL() start, IAP = %d"), iIAPId ); |
|
110 |
|
111 delete iRemoteAddr; |
|
112 iRemoteAddr = NULL; |
|
113 |
|
114 if ( aRemoteAddr ) |
|
115 { |
|
116 iRemoteAddr = new ( ELeave ) TInetAddr( *aRemoteAddr ); |
|
117 } |
|
118 |
|
119 // Fire up a net connection, use the defined IAP if any |
|
120 if( iIAPId ) |
|
121 { |
|
122 iConnServ.Start( iPrefs, iStatus ); |
|
123 } |
|
124 else |
|
125 { |
|
126 // This will query the user for an IAP |
|
127 iConnServ.Start( iStatus ); |
|
128 } |
|
129 |
|
130 SetActive(); |
|
131 |
|
132 LOG( _L("CTcTcpManager::ConnectL() end") ); |
|
133 } |
|
134 |
|
135 void CTcTcpManager::Close() |
|
136 { |
|
137 LOG( _L("CTcTcpManager::Close()") ); |
|
138 |
|
139 Cancel(); |
|
140 // Shut down the service port |
|
141 iConn.Close(); |
|
142 } |
|
143 |
|
144 void CTcTcpManager::Send( const TDesC8& aDes ) |
|
145 { |
|
146 iConn.Send( aDes ); |
|
147 } |
|
148 |
|
149 void CTcTcpManager::Receive( TDes8& aDes ) |
|
150 { |
|
151 iConn.Receive( aDes ); |
|
152 } |
|
153 |
|
154 void CTcTcpManager::ReceiveOneOrMore( TDes8& aDes ) |
|
155 { |
|
156 iConn.ReceiveOneOrMore( aDes ); |
|
157 } |
|
158 |
|
159 void CTcTcpManager::SetObserver( MTcBearerObserver* aObserver ) |
|
160 { |
|
161 iObserver = aObserver; |
|
162 } |
|
163 |
|
164 void CTcTcpManager::GetLocalAddressL( TDes& aDes ) |
|
165 { |
|
166 // Array for addresses |
|
167 RArray< TBuf<40> > addresses; |
|
168 |
|
169 // Open socket for querying socket options |
|
170 RSocket socket; |
|
171 User::LeaveIfError(socket.Open( |
|
172 iSockServ, KAfInet, KSockDatagram, KProtocolInetUdp, iConnServ)); |
|
173 |
|
174 // Get IAP id from CommDb for the active connection |
|
175 TUint32 iapId; |
|
176 User::LeaveIfError(iConnServ.GetIntSetting(_L("IAP\\Id"), iapId)); |
|
177 |
|
178 // Get address from socket options |
|
179 TInt err = socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl); |
|
180 if (err == KErrNone) |
|
181 { |
|
182 TPckgBuf<TSoInetInterfaceInfo> info; |
|
183 err = socket.GetOpt(KSoInetNextInterface,KSolInetIfCtrl,info); |
|
184 while (err == KErrNone) |
|
185 { |
|
186 TPckgBuf<TSoInetIfQuery> query; |
|
187 query().iName = info().iName; |
|
188 err = socket.GetOpt(KSoInetIfQueryByName,KSolInetIfQuery,query); |
|
189 if (err == KErrNone) |
|
190 { |
|
191 if (!info().iAddress.IsUnspecified() && |
|
192 !info().iAddress.IsLoopback() && |
|
193 !info().iAddress.IsLinkLocal()) |
|
194 { |
|
195 |
|
196 TInetAddr addr; |
|
197 |
|
198 // Initialize address for both ipv4 and ipv6 |
|
199 addr.Init(KAfInet6); |
|
200 |
|
201 addr = info().iAddress; |
|
202 |
|
203 TBuf<40> addrDesc; |
|
204 addr.Output( addrDesc ); |
|
205 |
|
206 TInt oldIndex = addresses.Find(addrDesc); |
|
207 |
|
208 // Address of current IAP is shown first |
|
209 if (query().iZone[1] == iapId) |
|
210 { |
|
211 // remove the old one |
|
212 if(KErrNotFound != oldIndex){ |
|
213 addresses.Remove(oldIndex); |
|
214 } |
|
215 addresses.Insert(addrDesc,0); |
|
216 } |
|
217 else |
|
218 { |
|
219 if(KErrNotFound == oldIndex){ |
|
220 addresses.Append(addrDesc); |
|
221 } |
|
222 } |
|
223 } |
|
224 err = socket.GetOpt(KSoInetNextInterface,KSolInetIfCtrl,info); |
|
225 } |
|
226 } |
|
227 } |
|
228 |
|
229 socket.Close(); |
|
230 |
|
231 if (err == KErrNoMemory) |
|
232 { |
|
233 User::LeaveNoMemory(); |
|
234 } |
|
235 _LIT(KLineFeed,"\n"); |
|
236 |
|
237 // append address from the array to descriptor |
|
238 for(TInt i=0;i<addresses.Count();i++) |
|
239 { |
|
240 aDes.Append(addresses[i]); |
|
241 aDes.Append(KLineFeed); |
|
242 } |
|
243 addresses.Close(); |
|
244 |
|
245 } |
|
246 |
|
247 void CTcTcpManager::BearerCompletion( MTcBearerObserver::TOperation aOp, |
|
248 TInt aStatus ) |
|
249 { |
|
250 LOG( _L("CTcTcpManager::BearerCompletion( %d, %d ) start"), aOp, aStatus ); |
|
251 if( iObserver ) |
|
252 { |
|
253 iObserver->BearerCompletion( aOp, aStatus ); |
|
254 } |
|
255 LOG( _L("CTcTcpManager::BearerCompletion() end") ); |
|
256 } |