|
1 /* |
|
2 * Copyright (c) 2006, 2007 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: This class implements functions of set mtp personality, the |
|
15 * notification of the MTP printer connection and the |
|
16 * notification of the MTP printer disconnection. |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 #include <e32debug.h> |
|
22 #include <usbstates.h> |
|
23 #include <rptp.h> |
|
24 #include "dpsusbnotifier.h" |
|
25 #include "dpsconst.h" |
|
26 #include "dpsptpnotifier.h" |
|
27 #include "dpsconnectnotifier.h" |
|
28 |
|
29 #ifdef _DEBUG |
|
30 # define IF_DEBUG(t) {RDebug::t;} |
|
31 #else |
|
32 # define IF_DEBUG(t) |
|
33 #endif |
|
34 |
|
35 const TInt KUnknownPersonality = 0; |
|
36 |
|
37 // --------------------------------------------------------------------------- |
|
38 // |
|
39 // --------------------------------------------------------------------------- |
|
40 // |
|
41 CDpsUsbNotifier* CDpsUsbNotifier::NewL(CDpsEngine* aEngine) |
|
42 { |
|
43 IF_DEBUG(Print(_L("CDpsUsbNotifier::NewL"))); |
|
44 CDpsUsbNotifier* self = new(ELeave) CDpsUsbNotifier(aEngine); |
|
45 CleanupStack::PushL(self); |
|
46 self->ConstructL(); |
|
47 CleanupStack::Pop(); |
|
48 return self; |
|
49 } |
|
50 |
|
51 // --------------------------------------------------------------------------- |
|
52 // |
|
53 // --------------------------------------------------------------------------- |
|
54 // |
|
55 CDpsUsbNotifier::CDpsUsbNotifier(CDpsEngine* aEngine) : |
|
56 CActive(EPriorityNormal), iEngine(aEngine), |
|
57 iPersonality(KUnknownPersonality), |
|
58 iConnectState(EUsbDeviceStateUndefined), iConfigured(EFalse), |
|
59 iConnection(CDpsEngine::ENotConnected), iRollback(EFalse) |
|
60 { |
|
61 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::Ctor"))); |
|
62 CActiveScheduler::Add(this); |
|
63 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::Ctor"))); |
|
64 } |
|
65 |
|
66 // --------------------------------------------------------------------------- |
|
67 // |
|
68 // --------------------------------------------------------------------------- |
|
69 // |
|
70 CDpsUsbNotifier::~CDpsUsbNotifier() |
|
71 { |
|
72 IF_DEBUG(Print(_L(">>>~CDpsUsbNotifier"))); |
|
73 Cancel(); |
|
74 Rollback(); |
|
75 delete iPtpP; iPtpP = NULL; |
|
76 delete iConnectP; iConnectP = NULL; |
|
77 iUsbM.Close(); |
|
78 iUsbW.Close(); |
|
79 IF_DEBUG(Print(_L("<<<~CDpsUsbNotifier"))); |
|
80 } |
|
81 |
|
82 // --------------------------------------------------------------------------- |
|
83 // |
|
84 // --------------------------------------------------------------------------- |
|
85 // |
|
86 void CDpsUsbNotifier::ConstructL() |
|
87 { |
|
88 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::ConstructL"))); |
|
89 User::LeaveIfError(iUsbM.Connect()); |
|
90 User::LeaveIfError(iUsbW.Connect()); |
|
91 iPtpP = CDpsPtpNotifier::NewL(this); |
|
92 iConnectP = CDpsConnectNotifier::NewL(this); |
|
93 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::ConstructL"))); |
|
94 } |
|
95 |
|
96 // --------------------------------------------------------------------------- |
|
97 // |
|
98 // --------------------------------------------------------------------------- |
|
99 // |
|
100 void CDpsUsbNotifier::WaitForPrinterNotify() |
|
101 { |
|
102 IF_DEBUG(Print(_L("CDpsUsbNotifier::WaitForPrinterNotify"))); |
|
103 iPtpP->ChangePtpPersonality(); |
|
104 } |
|
105 |
|
106 // --------------------------------------------------------------------------- |
|
107 // |
|
108 // --------------------------------------------------------------------------- |
|
109 // |
|
110 void CDpsUsbNotifier::CancelPrinterNotify() |
|
111 { |
|
112 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::CancelPrinterNotify"))); |
|
113 if (CDpsEngine::ENotConnected == iConnection) |
|
114 { |
|
115 iPtpP->Cancel(); |
|
116 iConfigured = EFalse; |
|
117 } |
|
118 else if (CDpsEngine::EPrinterConnected == iConnection) |
|
119 { |
|
120 iConnectP->Cancel(); |
|
121 } |
|
122 // if the request is replied through RunL before the cancel |
|
123 // iPrinterConnectRequest will be NULL and we don't need to cancel anything |
|
124 if (iEngine->PrinterConnectRequest()) |
|
125 { |
|
126 User::RequestComplete(iEngine->PrinterConnectRequest(), KErrCancel); |
|
127 } |
|
128 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::CancelWaitForPrinterNotify"))); |
|
129 } |
|
130 |
|
131 // --------------------------------------------------------------------------- |
|
132 // |
|
133 // --------------------------------------------------------------------------- |
|
134 // |
|
135 void CDpsUsbNotifier::ConnectNotify() |
|
136 { |
|
137 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::ConnectNotify"))); |
|
138 iConnectP->ConnectNotify(); |
|
139 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::ConnectNotify"))); |
|
140 } |
|
141 |
|
142 // --------------------------------------------------------------------------- |
|
143 // |
|
144 // --------------------------------------------------------------------------- |
|
145 // |
|
146 void CDpsUsbNotifier::Rollback() |
|
147 { |
|
148 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::Rollback"))); |
|
149 // only when the personality has changed, we switch back to the previous |
|
150 // personality |
|
151 if (iPersonality) |
|
152 { |
|
153 if (!iConfigured || iRollback) |
|
154 { |
|
155 iUsbW.SetPreviousPersonality(); |
|
156 } |
|
157 else |
|
158 { |
|
159 iUsbW.SetPreviousPersonalityOnDisconnect(); |
|
160 } |
|
161 } |
|
162 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::Rollback"))); |
|
163 } |
|
164 |
|
165 // --------------------------------------------------------------------------- |
|
166 // |
|
167 // --------------------------------------------------------------------------- |
|
168 // |
|
169 void CDpsUsbNotifier::PtpNotify(TInt aErr) |
|
170 { |
|
171 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::PtpNotify %x %d"), |
|
172 iConnectState, aErr)); |
|
173 if (aErr == KErrNone) |
|
174 { |
|
175 // personality changed to MTP, but cable is not connected |
|
176 if (iConnectState != EUsbDeviceStateUndefined) |
|
177 { |
|
178 if (!IsActive()) |
|
179 { |
|
180 iEngine->Ptp().IsDpsPrinter(iStatus); |
|
181 SetActive(); |
|
182 } |
|
183 } |
|
184 else |
|
185 { |
|
186 iConnection = CDpsEngine::ENotConnected; |
|
187 User::RequestComplete(iEngine->PrinterConnectRequest(), iConnection); |
|
188 } |
|
189 } |
|
190 else |
|
191 { |
|
192 iConnection = CDpsEngine::EWrongPrintModeConnected; |
|
193 User::RequestComplete(iEngine->PrinterConnectRequest(), iConnection); |
|
194 } |
|
195 |
|
196 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::PtpNotify"))); |
|
197 } |
|
198 |
|
199 // --------------------------------------------------------------------------- |
|
200 // |
|
201 // --------------------------------------------------------------------------- |
|
202 // |
|
203 void CDpsUsbNotifier::PersonalityChanged() |
|
204 { |
|
205 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::PersonalityChanged %x"), |
|
206 iPersonality)); |
|
207 if (iPersonality != KUsbPersonalityIdMTP) |
|
208 { |
|
209 iConnection = CDpsEngine::EWrongPrintModeConnected; |
|
210 iConfigured = EFalse; |
|
211 if (iEngine->PrinterConnectRequest()) |
|
212 { |
|
213 User::RequestComplete(iEngine->PrinterConnectRequest(), |
|
214 iConnection); |
|
215 } |
|
216 } |
|
217 // when UI gets this notification, it must quit. As the result, the dps |
|
218 // engine will be deleted so we do not need to care the further change. |
|
219 |
|
220 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::PersonalityChanged "))); |
|
221 } |
|
222 |
|
223 // --------------------------------------------------------------------------- |
|
224 // |
|
225 // --------------------------------------------------------------------------- |
|
226 // |
|
227 void CDpsUsbNotifier::RunL() |
|
228 { |
|
229 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::RunL"))); |
|
230 |
|
231 if (EPrinterAvailable == iStatus.Int()) |
|
232 { |
|
233 iConnection = CDpsEngine::EPrinterConnected; |
|
234 iConfigured = ETrue; |
|
235 iEngine->SetDpsFolder(iEngine->Ptp().PtpFolder()); |
|
236 } |
|
237 else if (iStatus.Int() != KErrCancel) |
|
238 { |
|
239 iConnection = CDpsEngine::EOtherConnected; |
|
240 } |
|
241 User::RequestComplete(iEngine->PrinterConnectRequest(), iConnection); |
|
242 |
|
243 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::RunL"))); |
|
244 } |
|
245 |
|
246 // --------------------------------------------------------------------------- |
|
247 // |
|
248 // --------------------------------------------------------------------------- |
|
249 // |
|
250 void CDpsUsbNotifier::DoCancel() |
|
251 { |
|
252 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::DoCancel"))); |
|
253 iEngine->Ptp().CancelIsDpsPrinter(); |
|
254 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::DoCancel"))); |
|
255 } |
|
256 |
|
257 // --------------------------------------------------------------------------- |
|
258 // |
|
259 // --------------------------------------------------------------------------- |
|
260 // |
|
261 TInt CDpsUsbNotifier::RunError(TInt aErr) |
|
262 { |
|
263 IF_DEBUG(Print(_L("CDpsUsbNotifier::RunError is %d"), aErr)); |
|
264 return aErr; |
|
265 } |
|
266 |
|
267 // --------------------------------------------------------------------------- |
|
268 // |
|
269 // --------------------------------------------------------------------------- |
|
270 // |
|
271 void CDpsUsbNotifier::DisconnectNotify(TUsbDeviceState aState) |
|
272 { |
|
273 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::DisconnectNotify %d"), aState)); |
|
274 if (iConfigured) |
|
275 { |
|
276 iConnection = CDpsEngine::EPrinterDisconnected; |
|
277 } |
|
278 else |
|
279 { |
|
280 iConnection = CDpsEngine::ENotConnected; |
|
281 } |
|
282 iConfigured = EFalse; |
|
283 if (EUsbDeviceStateUndefined == aState) |
|
284 { |
|
285 iRollback = ETrue; |
|
286 } |
|
287 if (iEngine->PrinterConnectRequest()) |
|
288 { |
|
289 User::RequestComplete(iEngine->PrinterConnectRequest(), iConnection); |
|
290 } |
|
291 |
|
292 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::DisconnectNotify"))); |
|
293 } |
|
294 |
|
295 // --------------------------------------------------------------------------- |
|
296 // |
|
297 // --------------------------------------------------------------------------- |
|
298 // |
|
299 TInt CDpsUsbNotifier::ConnectState() |
|
300 { |
|
301 IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::ConnectState"))); |
|
302 TInt ret = iUsbM.GetDeviceState(iConnectState); |
|
303 IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::ConnectState %x"), iConnectState)); |
|
304 return ret; |
|
305 } |
|
306 |
|
307 // --------------------------------------------------------------------------- |
|
308 // |
|
309 // --------------------------------------------------------------------------- |
|
310 // |
|
311 TBool CDpsUsbNotifier::IsSetPrintModeIssued() |
|
312 { |
|
313 return (iPersonality != KUnknownPersonality); |
|
314 } |
|
315 |
|
316 // --------------------------------------------------------------------------- |
|
317 // |
|
318 // --------------------------------------------------------------------------- |
|
319 // |
|
320 TBool CDpsUsbNotifier::IsConfigured() const |
|
321 { |
|
322 return iConfigured; |
|
323 } |