|
1 // Copyright (c) 2006-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 |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "HWRMServer.h" |
|
21 #include "HWRMServerActiveScheduler.h" |
|
22 #include "HWRMConfiguration.h" |
|
23 #include "HWRMtrace.h" |
|
24 |
|
25 // EXTERNAL DATA STRUCTURES |
|
26 // None |
|
27 |
|
28 // EXTERNAL FUNCTION PROTOTYPES |
|
29 // None |
|
30 |
|
31 // CONSTANTS |
|
32 // None |
|
33 |
|
34 // MACROS |
|
35 // None |
|
36 |
|
37 // LOCAL CONSTANTS AND MACROS |
|
38 // None |
|
39 |
|
40 // MODULE DATA STRUCTURES |
|
41 // None |
|
42 |
|
43 // LOCAL FUNCTION PROTOTYPES |
|
44 TInt E32Main(void); // Process entry point |
|
45 |
|
46 // FORWARD DECLARATIONS |
|
47 // None |
|
48 |
|
49 // ============================= LOCAL FUNCTIONS =============================== |
|
50 |
|
51 // ----------------------------------------------------------------------------- |
|
52 // Function that starts the HWRMServer. |
|
53 // ----------------------------------------------------------------------------- |
|
54 // |
|
55 static TInt RunServer() |
|
56 { |
|
57 COMPONENT_TRACE1(_L( "HWRM Server - RunServer()" )); |
|
58 |
|
59 RSemaphore startLock; |
|
60 CHWRMServerActiveScheduler* scheduler = NULL; |
|
61 CHWRMServer* server = NULL; |
|
62 |
|
63 // Create lock |
|
64 TInt err = startLock.CreateGlobal( KServerSemaphoreName, 1, EOwnerProcess ); |
|
65 |
|
66 if ( err == KErrAlreadyExists ) |
|
67 { |
|
68 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - Semaphore already exists, opening existing one." )); |
|
69 err = startLock.OpenGlobal( KServerSemaphoreName ); |
|
70 } |
|
71 else |
|
72 { |
|
73 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - Created a new semaphore." )); |
|
74 } |
|
75 |
|
76 // If there is error, server doesn't need to be started as it is either |
|
77 // very recently started (i.e. semaphore doesn't exist anymore even though creating it failed) |
|
78 // or there is something very wrong in system. |
|
79 if ( err == KErrNone ) |
|
80 { |
|
81 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - Waiting on a semaphore." )); |
|
82 startLock.Wait(); |
|
83 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - Semaphore wait over." )); |
|
84 |
|
85 // Check if HWRMServer process is already running. |
|
86 TFindProcess processFinder(KServerFindPattern); |
|
87 TFullName processName; |
|
88 err = processFinder.Next( processName ); |
|
89 |
|
90 COMPONENT_TRACE2(_L( "HWRM Server - RunServer() - Process find result: %d." ), err); |
|
91 |
|
92 // If HWRMServer process is not yet running, properly start it |
|
93 if ( err == KErrNotFound ) |
|
94 { |
|
95 // Naming the server process and thread after the startup helps to debug panics |
|
96 TInt renameErr1 = User::RenameProcess(KServerProcessName); |
|
97 if (renameErr1 != KErrNone) |
|
98 { |
|
99 COMPONENT_TRACE2(_L( "HWRM Server - RunServer() - Error %d renaming server process" ), renameErr1); |
|
100 } |
|
101 TInt renameErr2 = User::RenameThread(KServerProcessName); |
|
102 if (renameErr2 != KErrNone) |
|
103 { |
|
104 COMPONENT_TRACE2(_L( "HWRM Server - RunServer() - Error %d renaming server thread" ), renameErr2); |
|
105 } |
|
106 |
|
107 // Create and install the active scheduler we need |
|
108 if ( ( renameErr1 == KErrNone ) && (renameErr2 == KErrNone) ) |
|
109 { |
|
110 TRAP( err, scheduler = CHWRMServerActiveScheduler::NewL() ); |
|
111 |
|
112 // Note: Cannot use leaves and cleanupStack to reduce ifs here, because semaphore needs to be signaled in any case. |
|
113 if ( err == KErrNone ) |
|
114 { |
|
115 CActiveScheduler::Install( scheduler ); |
|
116 |
|
117 // Now we are ready to instantiate the actual CHWRMServer instance |
|
118 TRAP( err, server = CHWRMServer::NewL( KServerCActivePriority ) ); |
|
119 |
|
120 if ( err == KErrNone ) |
|
121 { |
|
122 // Initialisation complete, now signal the client |
|
123 RProcess::Rendezvous(KErrNone); |
|
124 |
|
125 // Server is now up and running, so signal semaphore |
|
126 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - Signaling semaphore." )); |
|
127 startLock.Signal(); |
|
128 startLock.Close(); // close already here to delete unnecessary reference |
|
129 |
|
130 // Ready to run |
|
131 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - Starting scheduler..." )); |
|
132 CActiveScheduler::Start(); |
|
133 |
|
134 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - Scheduler stopped" )); |
|
135 } |
|
136 } |
|
137 } |
|
138 |
|
139 // In case there was any error, signal the semaphore as it has not been yet signaled |
|
140 if ( ( err != KErrNone ) || ( renameErr1 != KErrNone ) || ( renameErr2 != KErrNone ) ) |
|
141 { |
|
142 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - error occurred - signaling semaphore." ) ); |
|
143 startLock.Signal(); |
|
144 // make sure we return an error code if there was an error |
|
145 if (err == KErrNone) |
|
146 { |
|
147 err = (renameErr1 == KErrNone) ? renameErr2 : renameErr1; |
|
148 } |
|
149 } |
|
150 } |
|
151 else // Probably KErrNone |
|
152 { |
|
153 // Already running, do not restart |
|
154 COMPONENT_TRACE1(_L( "HWRM Server - RunServer() - HWRM Server already started, signaling semaphore" ) ); |
|
155 startLock.Signal(); |
|
156 } |
|
157 } |
|
158 else |
|
159 { |
|
160 COMPONENT_TRACE2(_L( "HWRM Server - RunServer() - Problems opening startup semaphore: %d." ), err); |
|
161 } |
|
162 |
|
163 // Cleanup |
|
164 startLock.Close(); |
|
165 delete server; |
|
166 delete scheduler; |
|
167 |
|
168 COMPONENT_TRACE2(_L( "HWRM Server - RunServer() - return %d" ), err); |
|
169 return err; |
|
170 } |
|
171 |
|
172 // ----------------------------------------------------------------------------- |
|
173 // Main function |
|
174 // ----------------------------------------------------------------------------- |
|
175 // |
|
176 TInt E32Main() |
|
177 { |
|
178 COMPONENT_TRACE1(_L( "HWRM Server - E32Main()" )); |
|
179 |
|
180 __UHEAP_MARK; |
|
181 |
|
182 TInt ret(KErrNone); |
|
183 |
|
184 // No process with same name found, start server. |
|
185 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
186 ret = KErrNoMemory; |
|
187 |
|
188 if ( cleanup ) |
|
189 { |
|
190 ret = RunServer(); |
|
191 |
|
192 delete cleanup; |
|
193 } |
|
194 |
|
195 __UHEAP_MARKEND; |
|
196 |
|
197 COMPONENT_TRACE2(_L( "HWRM Server - E32Main - return %d" ), ret ); |
|
198 |
|
199 return ret; |
|
200 } |
|
201 |
|
202 // End of File |