|
1 /* |
|
2 * Copyright (c) 2009 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: |
|
15 * |
|
16 */ |
|
17 // INCLUDE FILES |
|
18 #include <e32std.h> |
|
19 |
|
20 #include "toolsprocess.h" |
|
21 |
|
22 #define SafeDelete(x) { if (x) delete x; x = NULL; } |
|
23 |
|
24 // LOCAL FUNCTION PROTOTYPES |
|
25 |
|
26 // LOCAL CONSTANTS |
|
27 |
|
28 |
|
29 // |
|
30 // CProcessTracker constructor |
|
31 // |
|
32 // Accepts CTrkDispatchLayer interface and the process ID of the |
|
33 // process to watch |
|
34 // |
|
35 CProcessTracker::CProcessTracker(CToolsProcess *aToolsProcess, TUint32 aProcessId) |
|
36 : CActive(EPriorityStandard), |
|
37 iProcessId(aProcessId), |
|
38 iToolsProcess(aToolsProcess) |
|
39 { |
|
40 CActiveScheduler::Add(this); |
|
41 |
|
42 TInt error = iProcess.Open(iProcessId); |
|
43 if (KErrNone != error) |
|
44 User::Panic(_L("Unable to open process"), __LINE__); |
|
45 } |
|
46 |
|
47 // |
|
48 // CProcessTracker destructor |
|
49 // |
|
50 CProcessTracker::~CProcessTracker() |
|
51 { |
|
52 Cancel(); |
|
53 Deque(); |
|
54 iProcess.Close(); |
|
55 } |
|
56 |
|
57 // |
|
58 // CProcessTracker::Watch |
|
59 // |
|
60 // Watch for the process to exit |
|
61 // |
|
62 void CProcessTracker::Watch() |
|
63 { |
|
64 iProcess.Logon(iStatus); |
|
65 SetActive(); |
|
66 } |
|
67 |
|
68 // |
|
69 // CProcessTracker::RunL |
|
70 // |
|
71 // Called when the process exits |
|
72 // |
|
73 void CProcessTracker::RunL() |
|
74 { |
|
75 // sometimes this is run when the process has yet to exit |
|
76 if (iProcess.ExitType() == EExitPending) |
|
77 { |
|
78 iProcess.LogonCancel(iStatus); |
|
79 Watch(); |
|
80 } |
|
81 else |
|
82 { |
|
83 iToolsProcess->ProcessDied(iProcess.ExitReason()); |
|
84 } |
|
85 } |
|
86 |
|
87 // |
|
88 // CProcessTracker::DoCancel |
|
89 // |
|
90 // Stop waiting for this process to exit |
|
91 // |
|
92 void CProcessTracker::DoCancel() |
|
93 { |
|
94 iProcess.LogonCancel(iStatus); |
|
95 } |
|
96 |
|
97 // |
|
98 // CToolsProcess implementation |
|
99 // |
|
100 |
|
101 TBool CToolsProcess::iFirstTimeAfterBoot = ETrue; |
|
102 // |
|
103 // CToolsProcess::NewL |
|
104 // |
|
105 CToolsProcess* CToolsProcess::NewL(const TDesC& aPath, const TDesC& aArgsUsb, const TDesC& aArgsXti) |
|
106 { |
|
107 CToolsProcess* self = new(ELeave) CToolsProcess; |
|
108 self->ConstructL(aPath, aArgsUsb, aArgsXti); |
|
109 return self; |
|
110 } |
|
111 |
|
112 // |
|
113 // CTrkEngine::NewLC |
|
114 // |
|
115 CToolsProcess* CToolsProcess::NewLC(const TDesC& aPath, const TDesC& aArgsUsb, const TDesC& aArgsXti) |
|
116 { |
|
117 CToolsProcess* self = new(ELeave) CToolsProcess; |
|
118 CleanupStack::PushL(self); |
|
119 |
|
120 self->ConstructL(aPath, aArgsUsb, aArgsXti); |
|
121 |
|
122 return self; |
|
123 } |
|
124 |
|
125 // |
|
126 // CToolsProcess::ConstructL |
|
127 // Constructor |
|
128 // |
|
129 void CToolsProcess::ConstructL(const TDesC& aPath, const TDesC& aArgsUsb, const TDesC& aArgsXti) |
|
130 { |
|
131 iPath.Copy(aPath); |
|
132 iArgsUsb.Copy(aArgsUsb); |
|
133 iArgsXti.Copy(aArgsXti); |
|
134 } |
|
135 // |
|
136 // CToolsProcess::CToolsProcess |
|
137 // Constructor |
|
138 // |
|
139 CToolsProcess::CToolsProcess() |
|
140 : iRunning(EFalse), |
|
141 iProcessTracker(NULL) |
|
142 { |
|
143 |
|
144 } |
|
145 |
|
146 // |
|
147 // CToolsProcess::~CToolsProcess |
|
148 // Destructor |
|
149 // |
|
150 CToolsProcess::~CToolsProcess() |
|
151 { |
|
152 SafeDelete(iProcessTracker); |
|
153 iProcess.Close(); |
|
154 } |
|
155 |
|
156 // |
|
157 // CToolsProcess::Start |
|
158 // |
|
159 TInt CToolsProcess::Start(TCmdLineConnType aConnType) |
|
160 { |
|
161 TBuf<KMaxCmdLineConnArgsLen> cmdConnArgs = _L(""); |
|
162 switch (aConnType) |
|
163 { |
|
164 case EUsb: |
|
165 cmdConnArgs = iArgsUsb; |
|
166 break; |
|
167 case EXti: |
|
168 cmdConnArgs = iArgsXti; |
|
169 break; |
|
170 default: |
|
171 return KErrNotFound; |
|
172 } |
|
173 |
|
174 TInt err = KErrNone; |
|
175 // check to see if we have already launched or not. |
|
176 // We only launch the first time and we track the process that we launched |
|
177 // Also check to see if the process is already running or not. |
|
178 // User could have manually launched the application in which case the user needs to |
|
179 // connect from the tool manually. |
|
180 if (!iRunning) |
|
181 { |
|
182 err = iProcess.Create(iPath, cmdConnArgs); |
|
183 if (!err) |
|
184 { |
|
185 iProcessId = iProcess.Id(); |
|
186 iProcess.Resume(); |
|
187 |
|
188 } |
|
189 if (!err) |
|
190 { |
|
191 iRunning = ETrue; //now the process is running.. |
|
192 } |
|
193 |
|
194 |
|
195 } |
|
196 |
|
197 return err; |
|
198 } |
|
199 |
|
200 // |
|
201 // CToolsProcess::Stop |
|
202 // |
|
203 void CToolsProcess::Stop() |
|
204 { |
|
205 if (iRunning) |
|
206 { |
|
207 iRunning = EFalse; |
|
208 iProcess.Kill(-1); |
|
209 iProcess.Close(); |
|
210 SafeDelete(iProcessTracker); |
|
211 } |
|
212 } |
|
213 |
|
214 // |
|
215 // CToolsProcess::Stop |
|
216 // |
|
217 void CToolsProcess::ProcessDied(TInt aExitReason) |
|
218 { |
|
219 iRunning = EFalse; |
|
220 iProcess.Close(); |
|
221 SafeDelete(iProcessTracker); |
|
222 } |
|
223 |