1 /** |
|
2 * Copyright (c) 2010 Sasken Communication Technologies Ltd. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "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 * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Manasij Roy, Nalina Hariharan |
|
14 */ |
|
15 |
|
16 #include "smfclientsymbian.h" |
|
17 #include <e32cmn.h> |
|
18 #include <QtGlobal> |
|
19 #include <e32svr.h> |
|
20 #include <e32base.h> |
|
21 #include <QByteArray> |
|
22 #include "smfglobal.h" |
|
23 #include "smfclientglobal.h" |
|
24 //testing purpose |
|
25 #include <QProcess> |
|
26 #include <QTextStream> |
|
27 #include <QFile> |
|
28 //testing end |
|
29 // For starting the server process |
|
30 static TInt StartServer(); |
|
31 static TInt CreateServerProcessL(); |
|
32 |
|
33 CSmfClientSymbian::CSmfClientSymbian(smfObserver* aObserver) |
|
34 : iObserver(aObserver),CActive( EPriorityStandard ),iDataPtr(NULL, 0, 0) |
|
35 { |
|
36 CActiveScheduler::Add(this); |
|
37 } |
|
38 |
|
39 CSmfClientSymbian* CSmfClientSymbian::NewL(smfObserver* aObserver ) |
|
40 { |
|
41 CSmfClientSymbian* self = NewLC( aObserver ); |
|
42 CleanupStack::Pop( self ); |
|
43 return( self ) ; |
|
44 } |
|
45 |
|
46 CSmfClientSymbian* CSmfClientSymbian::NewLC(smfObserver* aObserver ) |
|
47 { |
|
48 CSmfClientSymbian* self = |
|
49 new ( ELeave ) CSmfClientSymbian( aObserver ); |
|
50 CleanupStack::PushL( self ); |
|
51 self->ConstructL(); |
|
52 return self; |
|
53 } |
|
54 |
|
55 void CSmfClientSymbian::ConstructL() |
|
56 { |
|
57 writeLog("CSmfClientSymbian::ConstructL"); |
|
58 |
|
59 User::LeaveIfError(iSession.connectToServer()); |
|
60 } |
|
61 void CSmfClientSymbian::writeLog(QString log) |
|
62 { |
|
63 #ifdef WRITE_LOG |
|
64 QFile file("c:\\data\\SmfClientLogs.txt"); |
|
65 if (!file.open(QIODevice::Append | QIODevice::Text)) |
|
66 return; |
|
67 QTextStream out(&file); |
|
68 out << log << "\n"; |
|
69 file.close(); |
|
70 #endif |
|
71 } |
|
72 |
|
73 void CSmfClientSymbian::DoCancel() |
|
74 { |
|
75 Cancel(); |
|
76 } |
|
77 |
|
78 TInt CSmfClientSymbian::RunError(TInt aError) |
|
79 { |
|
80 QString log2("CSmfClientSymbian::RunError="); |
|
81 |
|
82 log2 += QString::number(aError); |
|
83 writeLog(log2); |
|
84 return KErrNone; |
|
85 } |
|
86 |
|
87 void CSmfClientSymbian::RunL() |
|
88 { |
|
89 QString log2("CSmfClientSymbian::RunL="); |
|
90 log2 += QString::number(iStatus.Int()); |
|
91 writeLog(log2); |
|
92 switch ( iStatus.Int() ) |
|
93 { |
|
94 case KErrCancel: |
|
95 // The request was canceled |
|
96 writeLog("KErrCancel"); |
|
97 break ; |
|
98 |
|
99 case KErrNotReady: |
|
100 writeLog("KErrNotReady"); |
|
101 break; |
|
102 |
|
103 default: |
|
104 { |
|
105 writeLog("RunL:-default"); |
|
106 //This contains error followed by actual data |
|
107 QByteArray receivedData(reinterpret_cast<const char*>(iSession.iDataPtr.Ptr()),iSession.iDataPtr.Length()); |
|
108 writeLog("receivedData size="); |
|
109 writeLog(QString::number(receivedData.size())); |
|
110 SmfError errVal; |
|
111 int errInt; |
|
112 QByteArray data; |
|
113 QDataStream reader(&receivedData,QIODevice::ReadOnly); |
|
114 reader>>errInt; |
|
115 writeLog("errInt="); |
|
116 writeLog(QString::number(errInt)); |
|
117 errVal = (SmfError)errInt; |
|
118 reader>>data; |
|
119 writeLog("data size="); |
|
120 writeLog(QString::number(data.size())); |
|
121 SmfRequestTypeID opcode = (SmfRequestTypeID)iSession.getLastRequest(); |
|
122 if(iObserver) |
|
123 { |
|
124 iObserver->resultsAvailable(data,opcode,errVal); |
|
125 } |
|
126 } |
|
127 break; |
|
128 } |
|
129 } |
|
130 |
|
131 QByteArray CSmfClientSymbian::sendRequest(QString aInterfaceName, |
|
132 SmfRequestTypeID requestType,TInt maxSize) |
|
133 { |
|
134 //This will be a synchronous request |
|
135 //note session is opened in ctor and closed in dtor |
|
136 writeLog("CSmfClientSymbian::sendRequest="); |
|
137 writeLog(aInterfaceName); |
|
138 //Gets data synchronously from the server |
|
139 TPtr8 symbianBuf(iSession.sendSyncRequest(aInterfaceName,requestType,maxSize)); |
|
140 //convert this into bytearray |
|
141 QByteArray receivedData(reinterpret_cast<const char*>(symbianBuf.Ptr()),symbianBuf.Length()); |
|
142 return receivedData; |
|
143 // |
|
144 } |
|
145 QByteArray CSmfClientSymbian::sendDSMSyncRequest(SmfRequestTypeID requestType,QByteArray& aSerializedData,SmfError& aErr,TInt maxSize) |
|
146 { |
|
147 writeLog("CSmfClientSymbian::sendDSMSyncRequest="); |
|
148 writeLog(QString::number(requestType)); |
|
149 SmfError err; |
|
150 //Gets data synchronously from the server |
|
151 TPtr8 symbianBuf(iSession.sendDSMSyncRequest(requestType,aSerializedData,err,maxSize)); |
|
152 //convert this into bytearray |
|
153 QByteArray receivedData(reinterpret_cast<const char*>(symbianBuf.Ptr()),symbianBuf.Length()); |
|
154 writeLog("receivedData size="); |
|
155 writeLog(QString::number(receivedData.size())); |
|
156 return receivedData; |
|
157 } |
|
158 TInt CSmfClientSymbian::sendRequest(QByteArray& aSerializedData, |
|
159 QString aInterfaceName, |
|
160 SmfRequestTypeID requestType,TInt aMaxAllocation,QByteArray xtraInfo) |
|
161 { |
|
162 //RSessionBase objects sendreceive is called |
|
163 iSession.sendAsyncRequest(aSerializedData,aInterfaceName,requestType,iStatus,aMaxAllocation,xtraInfo); |
|
164 SetActive(); |
|
165 } |
|
166 |
|
167 TInt CSmfClientSymbian::sendDummyRequest(QByteArray* provider,QString aInterfaceName, |
|
168 SmfRequestTypeID requestType) |
|
169 { |
|
170 switch(requestType) |
|
171 { |
|
172 case SmfTest: |
|
173 { |
|
174 |
|
175 } |
|
176 break; |
|
177 default: |
|
178 //should panic |
|
179 break; |
|
180 } |
|
181 } |
|
182 |
|
183 CSmfClientSymbian::~CSmfClientSymbian() |
|
184 { |
|
185 writeLog("~CSmfClientSymbian"); |
|
186 Cancel(); // Causes call to DoCancel() |
|
187 iSession.Close(); |
|
188 } |
|
189 |
|
190 RSmfClientSymbianSession::RSmfClientSymbianSession() |
|
191 :iDataPtr(NULL, 0, 0),iDataPtr16(NULL,0), |
|
192 iIntfNamePtr(NULL,0),iIntfNamePtr8(NULL,0), |
|
193 iPtrProvider(NULL,0),iPtrToSlot0(NULL,0) |
|
194 { |
|
195 // No implementation required |
|
196 } |
|
197 |
|
198 TInt RSmfClientSymbianSession::connectToServer() |
|
199 { |
|
200 writeLog("RSmfClientSymbianSession::connectToServer"); |
|
201 TInt error = ::StartServer(); |
|
202 writeLog("StartServerL="); |
|
203 QString err = QString::number(error); |
|
204 writeLog(err); |
|
205 if ( KErrNone == error ) |
|
206 { |
|
207 |
|
208 error = CreateSession(KSmfServerName, |
|
209 Version(), |
|
210 4 ); |
|
211 QString crtSessionErr = QString::number(error); |
|
212 writeLog(crtSessionErr); |
|
213 } |
|
214 return error; |
|
215 } |
|
216 |
|
217 //testing |
|
218 void RSmfClientSymbianSession::writeLog(QString log) const |
|
219 { |
|
220 #ifdef WRITE_LOG |
|
221 QFile file("c:\\data\\SmfClientLogs.txt"); |
|
222 if (!file.open(QIODevice::Append | QIODevice::Text)) |
|
223 ; |
|
224 QTextStream out(&file); |
|
225 out << log << "\n"; |
|
226 file.close(); |
|
227 #endif |
|
228 } |
|
229 /** |
|
230 * Sends sync request to the Smf server |
|
231 */ |
|
232 TPtr8 RSmfClientSymbianSession::sendDSMSyncRequest(SmfRequestTypeID aRequestType, |
|
233 QByteArray& aSerializedData,SmfError aErr, |
|
234 TInt maxSize) |
|
235 { |
|
236 /** |
|
237 * Slot 0:- Data to be passed to DSM |
|
238 * Slot 1:- Data returned from DSM |
|
239 * Slot 2:- Error |
|
240 */ |
|
241 writeLog("RSmfClientSymbianSession::sendDSMSyncRequest"); |
|
242 iLastRequest = aRequestType; |
|
243 if(iSlot0Buffer) |
|
244 { |
|
245 delete iSlot0Buffer; |
|
246 iSlot0Buffer = NULL; |
|
247 } |
|
248 iSlot0Buffer = HBufC8::NewL(aSerializedData.size()); |
|
249 iPtrToSlot0.Set(iSlot0Buffer->Des()); |
|
250 if(iBuffer) |
|
251 { |
|
252 delete iBuffer; |
|
253 iBuffer = NULL; |
|
254 } |
|
255 iBuffer = HBufC8::NewL(maxSize); |
|
256 iDataPtr.Set(iBuffer->Des()); |
|
257 |
|
258 TIpcArgs args; |
|
259 |
|
260 args.Set(0, &iPtrToSlot0); |
|
261 args.Set(1, &iDataPtr); |
|
262 iDSMErr.Zero(); |
|
263 args.Set(2,&iDSMErr); |
|
264 |
|
265 TInt sendErr = SendReceive(aRequestType,args); |
|
266 writeLog("SendReceive="); |
|
267 writeLog(QString::number(sendErr)); |
|
268 TInt numIndex; |
|
269 TLex iLex(iDSMErr); |
|
270 |
|
271 iLex.Val(numIndex); |
|
272 aErr = (SmfError)numIndex; |
|
273 return iDataPtr; |
|
274 } |
|
275 TPtr8 RSmfClientSymbianSession::sendSyncRequest(QString aInterfaceName, |
|
276 SmfRequestTypeID aRequestType, |
|
277 TInt maxSize) |
|
278 { |
|
279 iLastRequest = aRequestType; |
|
280 /** |
|
281 * The message body consists of.- |
|
282 * 1. Interface name as string ("org.symbian.smf.client.gallery") |
|
283 * 2. Data pointer to be filled by serialized data(QList<smfProvider>) |
|
284 */ |
|
285 QString log("RSmfClientSymbianSession::sendSyncRequest-start-"); |
|
286 writeLog(log); |
|
287 writeLog(QString("aInterfaceName=")+aInterfaceName); |
|
288 |
|
289 iInterfaceNamebyte.clear(); |
|
290 //Convert the interface name into TPtr |
|
291 iInterfaceName.clear(); |
|
292 iInterfaceName = aInterfaceName ; |
|
293 writeLog(QString("iInterfaceName=")+iInterfaceName); |
|
294 //lets pass serialized QString |
|
295 QDataStream intfNameStream(&iInterfaceNamebyte,QIODevice::WriteOnly); |
|
296 intfNameStream<<iInterfaceName; |
|
297 log.clear(); |
|
298 log = QString("iInterfaceNamebyte size="); |
|
299 log += QString::number(iInterfaceNamebyte.size()); |
|
300 writeLog(log); |
|
301 if(iIntfNameBuffer8) |
|
302 { |
|
303 delete iIntfNameBuffer8; |
|
304 iIntfNameBuffer8 =NULL; |
|
305 } |
|
306 TInt serializedintfsize = iInterfaceNamebyte.size(); |
|
307 writeLog("iInterfaceNamebyte.size()="); |
|
308 writeLog(QString::number(serializedintfsize)); |
|
309 iIntfNameBuffer8 = HBufC8::NewL(iInterfaceNamebyte.size()*2); |
|
310 iIntfNamePtr8.Set(iIntfNameBuffer8->Des()); |
|
311 iIntfNamePtr8.Copy(reinterpret_cast<TUint8*>(iInterfaceNamebyte.data()),iInterfaceNamebyte.length()); |
|
312 log.clear(); |
|
313 log = QString("iIntfNamePtr8 size=")+QString::number(iIntfNamePtr8.Size()); |
|
314 writeLog(log); |
|
315 |
|
316 writeLog("Before provider symbian copy"); |
|
317 |
|
318 |
|
319 iInterfaceSymbian8.Copy(iIntfNamePtr8); |
|
320 if(iBuffer) |
|
321 { |
|
322 delete iBuffer; |
|
323 iBuffer = NULL; |
|
324 } |
|
325 writeLog("Allocated for SmfProviderList="); |
|
326 writeLog(QString::number(maxSize)); |
|
327 if(iBuffer) |
|
328 { |
|
329 delete iBuffer; |
|
330 iBuffer = NULL; |
|
331 } |
|
332 iBuffer = HBufC8::NewL(maxSize); |
|
333 iDataPtr.Set(iBuffer->Des()); |
|
334 log.clear(); |
|
335 log = QString("After iDataPtr.Set"); |
|
336 writeLog(log); |
|
337 |
|
338 |
|
339 TIpcArgs args; |
|
340 |
|
341 args.Set(0, &iInterfaceSymbian8); |
|
342 args.Set(1, &iDataPtr); |
|
343 if(maxSize) |
|
344 { |
|
345 iMaxSize = maxSize; |
|
346 args.Set(2,iMaxSize); |
|
347 } |
|
348 TInt err(KErrBadHandle); |
|
349 writeLog("Before handle"); |
|
350 log.clear(); |
|
351 log = QString("iInterfaceSymbian8 size=")+QString::number(iInterfaceSymbian8.Size()); |
|
352 writeLog(log); |
|
353 if (Handle()) |
|
354 { |
|
355 err = KErrNone; |
|
356 log.clear(); |
|
357 log = QString("Before sendreceive"); |
|
358 writeLog(log); |
|
359 //synchronous request |
|
360 TInt sendErr = SendReceive(aRequestType, args); |
|
361 if(sendErr) |
|
362 { |
|
363 writeLog("SendReceive error="); |
|
364 QString errStr = QString::number(sendErr); |
|
365 writeLog(errStr); |
|
366 } |
|
367 return iDataPtr; |
|
368 } |
|
369 } |
|
370 |
|
371 /** |
|
372 * Calls SendReceive() after converting into symbian descriptors |
|
373 * |
|
374 */ |
|
375 void RSmfClientSymbianSession::sendAsyncRequest(QByteArray& aSerializedData, |
|
376 QString aInterfaceName, |
|
377 SmfRequestTypeID aRequestType, |
|
378 TRequestStatus& aStatus, |
|
379 TInt aMaxAllocation, |
|
380 QByteArray aXtraInfo) |
|
381 { |
|
382 /** |
|
383 * The message body consists of.- |
|
384 * Modified,- |
|
385 * slot 0 = SmfProvider +PageInfo flag+ aPageNum + aPerPage + XtraInfo flag(size of xtra data) Serialized |
|
386 * slot 1 = Interface name serialized |
|
387 * slot 2 = Data pointer to filled by server |
|
388 * slot 3= Xtra info when required by server else empty buffer |
|
389 */ |
|
390 writeLog("RSmfClientSymbianSession::sendAsyncRequest-start-"); |
|
391 iLastRequest = aRequestType; |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 if(iProviderBuf) |
|
397 { |
|
398 delete iProviderBuf; |
|
399 iProviderBuf = NULL; |
|
400 } |
|
401 iProviderBuf = HBufC8::NewL(aSerializedData.size()); |
|
402 iPtrProvider.Set(iProviderBuf->Des()); |
|
403 iPtrProvider.Copy(reinterpret_cast<const TText8*>(aSerializedData.constData()),aSerializedData.length()); |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 //convert the QByteArray into TPtr |
|
409 TPtrC8 ptrSlot0(reinterpret_cast<const TText8*>(aSerializedData.constData()),aSerializedData.length()); |
|
410 writeLog("ptrSlot0 size="); |
|
411 writeLog(QString::number(ptrSlot0.Size())); |
|
412 //Convert the interface name into TPtr |
|
413 iInterfaceName.clear(); |
|
414 iInterfaceName.append(aInterfaceName) ; |
|
415 writeLog(QString("iInterfaceName=")+iInterfaceName); |
|
416 iInterfaceNamebyte.clear(); |
|
417 //Pass serialized QString for interface name |
|
418 QDataStream intfNameStream(&iInterfaceNamebyte,QIODevice::WriteOnly); |
|
419 intfNameStream<<iInterfaceName; |
|
420 writeLog("iInterfaceNamebyte size="); |
|
421 writeLog(QString::number(iInterfaceNamebyte.size())); |
|
422 if(iIntfNameBuffer8) |
|
423 { |
|
424 delete iIntfNameBuffer8; |
|
425 iIntfNameBuffer8 = NULL; |
|
426 } |
|
427 iIntfNameBuffer8 = HBufC8::NewL(iInterfaceNamebyte.size()); |
|
428 iIntfNamePtr8.Set(iIntfNameBuffer8->Des()); |
|
429 iIntfNamePtr8.Copy(reinterpret_cast<const TText8*>(iInterfaceNamebyte.constData()),iInterfaceNamebyte.length()); |
|
430 writeLog("After iIntfNamePtr8.Copy"); |
|
431 if(iBuffer) |
|
432 { |
|
433 delete iBuffer; |
|
434 iBuffer = NULL; |
|
435 } |
|
436 iBuffer = HBufC8::NewL(aMaxAllocation); |
|
437 iDataPtr.Set(iBuffer->Des()); |
|
438 writeLog("After iDataPtr.Set"); |
|
439 TIpcArgs args; |
|
440 |
|
441 //filling the slots |
|
442 args.Set(0, &iPtrProvider); |
|
443 args.Set(1, &iIntfNamePtr8); |
|
444 args.Set(2, &iDataPtr); |
|
445 writeLog("After setting 0,1,2 slots"); |
|
446 if(aXtraInfo.size()) |
|
447 { |
|
448 TPtrC8 ptrToXtraInfo(reinterpret_cast<const TText8*>(aXtraInfo.constData()),aXtraInfo.length()); |
|
449 writeLog("ptrToXtraInfo size="); |
|
450 writeLog(QString::number(ptrToXtraInfo.Size())); |
|
451 args.Set(3, &ptrToXtraInfo); |
|
452 } |
|
453 TInt err(KErrBadHandle); |
|
454 writeLog("Before Handle()"); |
|
455 if (Handle()) |
|
456 { |
|
457 err = KErrNone; |
|
458 writeLog("Before sendreceive"); |
|
459 SendReceive(aRequestType, args, aStatus); |
|
460 } |
|
461 } |
|
462 |
|
463 // ----------------------------------------------------------------------------- |
|
464 // CreateServerProcessL() |
|
465 // Creates a server process |
|
466 // ----------------------------------------------------------------------------- |
|
467 // |
|
468 static TInt CreateServerProcessL() |
|
469 { |
|
470 TInt result; |
|
471 TUid KSmfServerUID3 = { 0xE5027327 }; |
|
472 const TUidType serverUid( KNullUid, KNullUid, KSmfServerUID3 ); |
|
473 |
|
474 RProcess server; |
|
475 |
|
476 result = server.Create( KSmfServerFilename, KNullDesC, serverUid ); |
|
477 |
|
478 if ( result != KErrNone ) |
|
479 { |
|
480 return result; |
|
481 } |
|
482 |
|
483 server.Resume(); |
|
484 server.Close(); |
|
485 |
|
486 return KErrNone; |
|
487 } |
|
488 static TInt StartServer() |
|
489 { |
|
490 TInt result; |
|
491 |
|
492 TFindServer findSmfServer( KSmfServerFilename ); |
|
493 TFullName name; |
|
494 |
|
495 result = findSmfServer.Next( name ); |
|
496 if ( result == KErrNone ) |
|
497 { |
|
498 // Server already running |
|
499 return KErrNone; |
|
500 } |
|
501 |
|
502 RSemaphore semaphore; |
|
503 result = semaphore.CreateGlobal( KSmfServerSemaphoreName, 0 ); |
|
504 if ( result != KErrNone ) |
|
505 { |
|
506 return result; |
|
507 } |
|
508 |
|
509 result = CreateServerProcessL(); |
|
510 if ( result != KErrNone ) |
|
511 { |
|
512 return result; |
|
513 } |
|
514 |
|
515 semaphore.Wait(); |
|
516 semaphore.Close(); |
|
517 |
|
518 return KErrNone; |
|
519 } |
|
520 |
|
521 // ----------------------------------------------------------------------------- |
|
522 // CreateServerProcess() |
|
523 // Creates a server process |
|
524 // ----------------------------------------------------------------------------- |
|
525 // |
|
526 static TInt CreateServerProcess() |
|
527 { |
|
528 TInt result; |
|
529 //SmfServer SID |
|
530 TUid KSmfServerUID3 = { 0xE5027327 }; |
|
531 const TUidType serverUid( KNullUid, KNullUid, KSmfServerUID3 ); |
|
532 |
|
533 RProcess server; |
|
534 |
|
535 result = server.Create( KSmfServerName, KNullDesC, serverUid ); |
|
536 |
|
537 if ( result != KErrNone ) |
|
538 { |
|
539 return result; |
|
540 } |
|
541 |
|
542 server.Resume(); |
|
543 server.Close(); |
|
544 |
|
545 return KErrNone; |
|
546 } |
|