|
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 "RTcWatcher.h" |
|
19 #include "WatcherConstants.h" |
|
20 |
|
21 EXPORT_C RTcWatcher::RTcWatcher() |
|
22 : iIsConnected( EFalse ) |
|
23 { |
|
24 } |
|
25 |
|
26 EXPORT_C void RTcWatcher::Close() |
|
27 { |
|
28 // Close the session if we are connected |
|
29 if( iIsConnected ) |
|
30 { |
|
31 SendReceive( ECloseSession, |
|
32 #ifdef __IPC_V2_PRESENT__ |
|
33 TIpcArgs() |
|
34 #else |
|
35 NULL |
|
36 #endif |
|
37 ); |
|
38 |
|
39 RHandleBase::Close(); |
|
40 iIsConnected = EFalse; |
|
41 } |
|
42 } |
|
43 |
|
44 EXPORT_C TInt RTcWatcher::Connect( const TDesC& aAppName, TInt aTimeout ) |
|
45 { |
|
46 // Are we already connected? |
|
47 if( iIsConnected ) |
|
48 { |
|
49 return KErrAlreadyExists; |
|
50 } |
|
51 |
|
52 // Try starting the server, it is safe |
|
53 // to call StartServer() even if a server is already running. |
|
54 TRAPD( status, StartServerL() ); |
|
55 |
|
56 // If server was started (or was already running), |
|
57 // establish a session to it. |
|
58 if( status == KErrNone ) |
|
59 { |
|
60 status = CreateSession( KTcWatcherName, |
|
61 Version(), |
|
62 KTcWatcherMessageSlots ); |
|
63 |
|
64 // if connection was established succesfully, go configure the session. |
|
65 if( status == KErrNone ) |
|
66 { |
|
67 iIsConnected = ETrue; |
|
68 |
|
69 #ifdef __IPC_V2_PRESENT__ |
|
70 return SendReceive( EConfigAndStart, |
|
71 TIpcArgs( &aAppName, aTimeout ) ); |
|
72 #else |
|
73 TInt p[ KMaxMessageArguments ] = |
|
74 { |
|
75 (TInt)const_cast< TDesC*>( &aAppName ), |
|
76 aTimeout |
|
77 }; |
|
78 status = SendReceive( EConfigAndStart, p ); |
|
79 #endif |
|
80 |
|
81 if( status != KErrNone ) |
|
82 { |
|
83 Close(); |
|
84 } |
|
85 } |
|
86 } |
|
87 |
|
88 return status; |
|
89 } |
|
90 |
|
91 EXPORT_C TInt RTcWatcher::Ping() |
|
92 { |
|
93 return SendReceive( EPing, |
|
94 #ifdef __IPC_V2_PRESENT__ |
|
95 TIpcArgs() |
|
96 #else |
|
97 NULL |
|
98 #endif |
|
99 ); |
|
100 } |
|
101 |
|
102 EXPORT_C TVersion RTcWatcher::Version() const |
|
103 { |
|
104 TVersion version( KTcWatcherMajorVersion, |
|
105 KTcWatcherMinorVersion, |
|
106 KTcWatcherBuildVersion ); |
|
107 return version; |
|
108 } |
|
109 |
|
110 void RTcWatcher::StartServerL() |
|
111 { |
|
112 // Let the server have a change at dying if it was just doing so :-) |
|
113 if( IsServerStarted() ) |
|
114 { |
|
115 User::After( KTcServerDyingTime ); |
|
116 } |
|
117 |
|
118 if( !IsServerStarted() ) |
|
119 { |
|
120 // Create a global semaphore for waiting the server to start |
|
121 RSemaphore semaphore; |
|
122 TInt status = semaphore.OpenGlobal( KTcWatcherName ); |
|
123 if( status == KErrNotFound ) |
|
124 { |
|
125 User::LeaveIfError( semaphore.CreateGlobal( KTcWatcherName, 0 ) ); |
|
126 } |
|
127 CleanupClosePushL( semaphore ); |
|
128 |
|
129 // We need to create a thread on WINS and a new process on ARMI/THUMB. |
|
130 #if ( defined (__WINS__) && !defined (EKA2) ) |
|
131 |
|
132 // Load the server dll |
|
133 RLibrary library; |
|
134 User::LeaveIfError( library.Load( KTcWatcherName, |
|
135 TUidType( KNullUid, KNullUid, KNullUid ) ) ); |
|
136 CleanupClosePushL( library ); |
|
137 |
|
138 // Look up the entry point function (always at ordinal #1) |
|
139 TLibraryFunction first = library.Lookup( 1 ); |
|
140 TThreadFunction entry = reinterpret_cast< TThreadFunction >( first() ); |
|
141 if( !entry ) |
|
142 { |
|
143 User::Leave( KErrBadLibraryEntryPoint ); |
|
144 } |
|
145 |
|
146 // We have to simulate the process with a thread on WINS |
|
147 RThread server; |
|
148 User::LeaveIfError( server.Create( KTcWatcherName, |
|
149 entry, |
|
150 KDefaultStackSize, |
|
151 NULL, |
|
152 &library, |
|
153 NULL, |
|
154 KTcDefaultHeapSize, |
|
155 KTcMaxHeapSize, |
|
156 EOwnerProcess |
|
157 ) ); |
|
158 |
|
159 |
|
160 // If successful, server thread has handle to library now |
|
161 CleanupStack::PopAndDestroy(); // library |
|
162 |
|
163 #else |
|
164 // Load the server executable. |
|
165 // Path (\System\Programs or \sys\bin) and extension (.exe) are added automatically |
|
166 RProcess server; |
|
167 User::LeaveIfError( server.Create( KTcWatcherName, |
|
168 KNullDesC, |
|
169 TUidType( KNullUid, KNullUid, KNullUid ) ) ); |
|
170 #endif |
|
171 |
|
172 CleanupClosePushL( server ); |
|
173 |
|
174 // Start executing the server. |
|
175 server.Resume(); |
|
176 // Wait until it has initialized. |
|
177 semaphore.Wait(); |
|
178 |
|
179 // Close handles |
|
180 CleanupStack::PopAndDestroy( 2 ); // server, semaphore |
|
181 } |
|
182 |
|
183 } |
|
184 |
|
185 TBool RTcWatcher::IsServerStarted() const |
|
186 { |
|
187 TFindServer findServer( KTcWatcherName ); |
|
188 TFullName name; |
|
189 return ( findServer.Next( name ) == KErrNone ); |
|
190 } |