|
1 /* |
|
2 * Copyright (c) 2002-2005 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: Client interface for EcmtServer |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <e32svr.h> |
|
21 #include "EcmtClient.h" |
|
22 #include "EcmtUids.h" |
|
23 #include "EcmtClientServerCommon.h" |
|
24 #include "EcmtServerListener.h" |
|
25 |
|
26 _LIT(KEcmtServerFilename, "\\sys\\bin\\EcmtCore.exe"); |
|
27 const TInt KServerStartTimeout = 30000000; // 30 sec |
|
28 const TInt KServerCheckTimeout = 1000000; // 1 sec |
|
29 |
|
30 // ============================= LOCAL FUNCTIONS ============================= |
|
31 |
|
32 // --------------------------------------------------------------------------- |
|
33 // StartServer |
|
34 // Starts the Ecmt server if it is not yet running. |
|
35 // --------------------------------------------------------------------------- |
|
36 static TInt StartServer() |
|
37 { |
|
38 TFindServer findServer( KEcmtServerName ); |
|
39 TFullName name; |
|
40 // Try to find Ecmt server |
|
41 TInt result = findServer.Next( name ); |
|
42 |
|
43 if ( result != KErrNone ) |
|
44 { |
|
45 // Server not yet running, so must launch it. |
|
46 // Create a semaphore that will be signaled by server when |
|
47 // it has completed its "boot" sequence. |
|
48 RSemaphore semaphore; |
|
49 result = semaphore.CreateGlobal( KEcmtSemaphoreName, 0 ); |
|
50 |
|
51 if ( result == KErrNone || result == KErrAlreadyExists ) |
|
52 { |
|
53 // Semaphore created succesfully (or it existed already) |
|
54 // so launch the server process. |
|
55 RProcess server; |
|
56 |
|
57 const TUid KEcmtServerUid = { KECMT_SERVER_UID }; |
|
58 const TUidType serverUid( KNullUid, KNullUid, KEcmtServerUid ); |
|
59 |
|
60 result = server.Create(KEcmtServerFilename, KNullDesC, serverUid); |
|
61 if (result == KErrNone) |
|
62 { |
|
63 server.Resume(); |
|
64 |
|
65 TTime now, deadline; |
|
66 now.HomeTime(); |
|
67 deadline.HomeTime(); |
|
68 deadline += TTimeIntervalMicroSeconds32(KServerStartTimeout); |
|
69 result = KErrTimedOut; |
|
70 while (result == KErrTimedOut && now < deadline) |
|
71 { |
|
72 result = (server.ExitType() == EExitPending) ? |
|
73 semaphore.Wait(KServerCheckTimeout) : |
|
74 KErrGeneral; |
|
75 } |
|
76 |
|
77 server.Close(); |
|
78 } |
|
79 } |
|
80 else |
|
81 { |
|
82 semaphore.Wait(KServerStartTimeout); |
|
83 } |
|
84 |
|
85 semaphore.Close(); |
|
86 } |
|
87 return result; |
|
88 } |
|
89 |
|
90 // ============================ MEMBER FUNCTIONS ============================= |
|
91 |
|
92 // --------------------------------------------------------------------------- |
|
93 // REcmt::REcmt |
|
94 // --------------------------------------------------------------------------- |
|
95 EXPORT_C REcmt::REcmt( ) : iServerListener(0) |
|
96 { |
|
97 } |
|
98 |
|
99 // --------------------------------------------------------------------------- |
|
100 // REcmt::~REcmt |
|
101 // --------------------------------------------------------------------------- |
|
102 EXPORT_C REcmt::~REcmt( ) |
|
103 { |
|
104 delete iServerListener; |
|
105 } |
|
106 |
|
107 // --------------------------------------------------------------------------- |
|
108 // REcmt::Connect |
|
109 // --------------------------------------------------------------------------- |
|
110 EXPORT_C TInt REcmt::Connect( ) |
|
111 { |
|
112 // Start server (unless it is already running) |
|
113 TInt error = StartServer(); |
|
114 |
|
115 if (KErrNone == error) |
|
116 { |
|
117 error = CreateSession( KEcmtServerName, |
|
118 TVersion( KEcmtServerMajorVersionNumber, |
|
119 KEcmtServerMinorVersionNumber, |
|
120 KEcmtServerBuildVersionNumber ) ); |
|
121 } |
|
122 return error; |
|
123 } |
|
124 |
|
125 // --------------------------------------------------------------------------- |
|
126 // REcmt::Close |
|
127 // --------------------------------------------------------------------------- |
|
128 EXPORT_C void REcmt::Close( ) |
|
129 { |
|
130 if ( iServerListener ) |
|
131 { |
|
132 delete iServerListener; |
|
133 iServerListener = 0; |
|
134 } |
|
135 RHandleBase::Close(); |
|
136 } |
|
137 |
|
138 // --------------------------------------------------------------------------- |
|
139 // REcmt::SetServerObserver |
|
140 // --------------------------------------------------------------------------- |
|
141 EXPORT_C void REcmt::SetServerObserverL( MEcmtServerObserver* /* aObserver */ ) |
|
142 { |
|
143 User::Leave( KErrNotSupported ); |
|
144 } |
|
145 |
|
146 // --------------------------------------------------------------------------- |
|
147 // REcmt::SetServerObserver |
|
148 // --------------------------------------------------------------------------- |
|
149 EXPORT_C void REcmt::SetServerObserverL( TUid aUid, MEcmtServerObserver* aObserver ) |
|
150 { |
|
151 iServerListener = new (ELeave) CEcmtServerListener( aObserver, *this ); |
|
152 |
|
153 TIpcArgs messageParameters( aUid.iUid ); |
|
154 SendReceive( EEcmtNotifyUid, messageParameters ); // ignore return code |
|
155 } |
|
156 |
|
157 // --------------------------------------------------------------------------- |
|
158 // REcmt::RequestServerNotification |
|
159 // --------------------------------------------------------------------------- |
|
160 void REcmt::RequestServerNotification( TDes8& aDes, TRequestStatus& aStatus ) |
|
161 { |
|
162 TIpcArgs messageParameters( &aDes ); |
|
163 SendReceive( EEcmtRequestServerNotification, messageParameters, aStatus ); |
|
164 } |
|
165 |
|
166 // --------------------------------------------------------------------------- |
|
167 // REcmt::CancelRequestServerMessage |
|
168 // --------------------------------------------------------------------------- |
|
169 void REcmt::CancelRequestServerNotification( ) |
|
170 { |
|
171 SendReceive( EEcmtCancelRequestServerNotification, TIpcArgs( ) ); |
|
172 } |
|
173 |
|
174 // --------------------------------------------------------------------------- |
|
175 // REcmt::Send |
|
176 // --------------------------------------------------------------------------- |
|
177 EXPORT_C void REcmt::Send( const TUint uid, const TDesC8& aData ) |
|
178 { |
|
179 TIpcArgs messageParameters( uid, &aData ); |
|
180 SendReceive( EEcmtRequestSendData, messageParameters ); |
|
181 } |
|
182 |
|
183 // --------------------------------------------------------------------------- |
|
184 // REcmt::Write |
|
185 // --------------------------------------------------------------------------- |
|
186 EXPORT_C void REcmt::Write( const TDesC16& aText ) |
|
187 { |
|
188 TIpcArgs messageParameters( &aText ); |
|
189 SendReceive( EEcmtRequestSendText, messageParameters ); |
|
190 } |
|
191 |
|
192 // --------------------------------------------------------------------------- |
|
193 // REcmt::WriteFormat |
|
194 // --------------------------------------------------------------------------- |
|
195 EXPORT_C void REcmt::WriteFormat( TRefByValue<const TDesC16> aFmt, ... ) |
|
196 { |
|
197 VA_LIST list; |
|
198 VA_START( list, aFmt ); |
|
199 |
|
200 TBuf16<KFormatMaxSize> buf; |
|
201 buf.FormatList( aFmt, list ); |
|
202 Write( buf ); |
|
203 } |
|
204 |
|
205 // --------------------------------------------------------------------------- |
|
206 // REcmt::GetServer |
|
207 // Returns thread local instance of REcmt client |
|
208 // --------------------------------------------------------------------------- |
|
209 REcmt* REcmt::GetServer() |
|
210 { |
|
211 // Create a new REcmt reference for the static write functions, if a |
|
212 // reference has not been created earlier. |
|
213 REcmt* r = static_cast<REcmt*>(UserSvr::DllTls(KECMT_SERVER_UID)); |
|
214 if (!r) |
|
215 { |
|
216 r = new REcmt(); |
|
217 if (r) |
|
218 { |
|
219 TInt err = UserSvr::DllSetTls(KECMT_SERVER_UID, r); |
|
220 if (err == KErrNone) |
|
221 { |
|
222 r->Connect(); |
|
223 if (err == KErrNone) |
|
224 { |
|
225 return r; |
|
226 } |
|
227 |
|
228 // Cleanup |
|
229 UserSvr::DllSetTls(KECMT_SERVER_UID, NULL); |
|
230 } |
|
231 delete r; |
|
232 r = NULL; |
|
233 } |
|
234 } |
|
235 |
|
236 return r; |
|
237 } |
|
238 |
|
239 // --------------------------------------------------------------------------- |
|
240 // REcmt::SWrite |
|
241 // --------------------------------------------------------------------------- |
|
242 EXPORT_C void REcmt::SWrite(const TDesC16& aText) |
|
243 { |
|
244 REcmt* r = GetServer(); |
|
245 if (r) |
|
246 { |
|
247 r->Write(aText); |
|
248 } |
|
249 } |
|
250 |
|
251 // --------------------------------------------------------------------------- |
|
252 // REcmt::SWriteFormat |
|
253 // --------------------------------------------------------------------------- |
|
254 EXPORT_C void REcmt::SWriteFormat(TRefByValue<const TDesC16> aFmt, ...) |
|
255 { |
|
256 REcmt* r = GetServer(); |
|
257 if (r) |
|
258 { |
|
259 VA_LIST list; |
|
260 VA_START( list, aFmt ); |
|
261 r->WriteFormat(aFmt, list); |
|
262 } |
|
263 } |
|
264 |
|
265 // ----------------------------------------------------------------------------- |
|
266 // REcmt::SClose |
|
267 // Deallocates thread local instance of REcmt client |
|
268 // ----------------------------------------------------------------------------- |
|
269 EXPORT_C void REcmt::SClose() |
|
270 { |
|
271 // Don't use GetServer() here because it may create a new REcmt instance, |
|
272 // which would be exactly opposite to what we are trying to do here. |
|
273 REcmt* r = static_cast<REcmt*>(UserSvr::DllTls(KECMT_SERVER_UID)); |
|
274 if (r) |
|
275 { |
|
276 r->Close(); |
|
277 delete r; |
|
278 UserSvr::DllSetTls(KECMT_SERVER_UID, NULL); |
|
279 } |
|
280 } |
|
281 |
|
282 // End of File |