examples/NFC/nfcshareexample/nfcaiwengine.cpp

00001 /*
00002  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *  
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  * 
00007  * Redistributions of source code must retain the above copyright notice, this
00008  *  list of conditions and the following disclaimer.
00009  * Redistributions in binary form must reproduce the above copyright notice,
00010  *   this list of conditions and the following disclaimer in the documentation
00011  *   and/or other materials provided with the distribution.
00012  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *  may be used to endorse or promote products derived from this software
00014  * without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  * Description:  Source file for nfc AIW handler engine class.
00028  * 
00029  */
00030 
00031 // User Include Files.
00032 #include "nfcsharewrapper.h"
00033 #include "nfcaiwengine.h"
00034 
00035 /*
00036  * Constructor which will not leave. 
00037  */
00038 CNfcAiwEngine::CNfcAiwEngine(NfcShareWrapper* aNfcShareWrapper) : iNfcShareWrapper(aNfcShareWrapper)
00039     {
00040     }
00041 
00042 /*
00043  * Two phase Constructor.
00044  * @param aNfcShareWrapper The wrapper class pointer.
00045  */
00046 CNfcAiwEngine* CNfcAiwEngine::NewL(NfcShareWrapper* aNfcShareWrapper)
00047     {
00048     CNfcAiwEngine* self = CNfcAiwEngine::NewLC(aNfcShareWrapper);
00049     CleanupStack::Pop();
00050     return self;
00051     }
00052 
00053 /*
00054  * Two phase Constructor.
00055  * @param aNfcShareWrapper The wrapper class pointer.
00056  */
00057 CNfcAiwEngine* CNfcAiwEngine::NewLC(NfcShareWrapper* aNfcShareWrapper)
00058     {
00059     CNfcAiwEngine* self = new (ELeave) CNfcAiwEngine(aNfcShareWrapper);
00060     CleanupStack::PushL(self);
00061     self->ConstructL();
00062     return self;
00063     }
00064 
00065 /*
00066  * Standard EPOC 2nd phase constructor.
00067  */ 
00068 void CNfcAiwEngine::ConstructL()
00069     {
00070     iAiwServiceHandler = CAiwServiceHandler::NewL();
00071     }
00072 
00073 /*
00074  * Destructor.
00075  */
00076 CNfcAiwEngine::~CNfcAiwEngine()
00077     {
00078     iFs.Close();
00079     if (iAiwServiceHandler)
00080         {
00081          delete iAiwServiceHandler;
00082          iAiwServiceHandler = NULL;
00083         }
00084     }
00085 
00086 /*
00087  * Starts the easy setup service.
00088  */
00089 TInt CNfcAiwEngine::StartEasySetupServiceL()
00090     {
00091     // Create AIW interest.
00092     RCriteriaArray interest;
00093     CleanupClosePushL(interest);
00094     TUid base;
00095     _LIT8(KContentType,"*");
00096     base.iUid = KAiwClassBase;
00097     CAiwCriteriaItem* criteria = CAiwCriteriaItem::NewLC(20001, KAiwCmdNFCEasySetup, KContentType);
00098     criteria->SetServiceClass(base);
00099     User::LeaveIfError(interest.Append(criteria));
00100 
00101     // Attach to AIW interest.
00102     iAiwServiceHandler->Reset();
00103     iAiwServiceHandler->AttachL(interest);
00104     
00105     iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdNFCEasySetup,
00106             iAiwServiceHandler->InParamListL(),
00107             iAiwServiceHandler->OutParamListL(), 0, this);
00108     CleanupStack::PopAndDestroy(2); // criteria, interest. 
00109     return KErrNone;
00110     }
00111 
00112 /*
00113  * StartSharingServiceL(NfcSharingType aSharingType)
00114  * starts the sharing of files based on sharing type parameter.
00115  * @param aSharingType specifies the type of file shared.
00116  */
00117 TInt CNfcAiwEngine::StartSharingServiceL(NfcSharingType aSharingType)
00118     {
00119     // Create AIW interest.
00120     RCriteriaArray interest;
00121     CleanupClosePushL(interest);
00122     iSharingType = aSharingType;
00123     TUid base;
00124     base.iUid = KAiwClassBase;
00125     _LIT8(KContentType,"*");
00126     CAiwCriteriaItem* criteria = CAiwCriteriaItem::NewLC(20001, KAiwCmdNFCGive, KContentType);
00127     criteria->SetServiceClass(base);
00128     User::LeaveIfError(interest.Append(criteria));
00129 
00130     // Attach to AIW interest.
00131     iAiwServiceHandler->Reset();
00132     iAiwServiceHandler->AttachL(interest);
00133 
00134     iAiwServiceHandler->ExecuteServiceCmdL(KAiwCmdNFCGive,
00135             iAiwServiceHandler->InParamListL(),
00136             iAiwServiceHandler->OutParamListL(), 0, this);
00137     CleanupStack::PopAndDestroy(2); // criteria, interest.
00138     return KErrNone;
00139     }
00140 
00141 /*
00142 * Handles notifications caused by an asynchronous Execute CommandL call
00143 * or an event. 
00144 * The result of command processing is sent as signal to UI. 
00145 * @param aCmdId The service command associated to the event.
00146 * @param aEventId Occured event, see AiwCommon.hrh.
00147 * @param aEventParamList Event parameters, if any, as defined per
00148 *        each event.
00149 * @param aInParamList Input parameters, if any, given in the
00150 *        related HandleCommmandL.
00151 * @return Error code for the callback.
00152 */
00153 
00154 TInt CNfcAiwEngine::HandleNotifyL(TInt aCmdId, TInt aEventId,
00155         CAiwGenericParamList& /* aEventParamList */,
00156         const CAiwGenericParamList& /* aInParamList */)
00157     {
00158     // Process based on command id and the event id.
00159     if (aCmdId == KAiwCmdNFCEasySetup && aEventId == KAiwEventStarted)
00160         {  
00161         iAiwServiceHandler->ExecuteServiceCmdL(KAiwCmdNFCEasySetup,
00162                 iAiwServiceHandler->InParamListL(),
00163                 iAiwServiceHandler->OutParamListL(), 0, this);
00164 
00165         }    
00166     else if (aCmdId == KAiwCmdNFCGive && aEventId == KAiwEventStarted)
00167         {
00168         TInt err = KErrNone;
00169         if (ENfcvCard == iSharingType) 
00170             { // Share vCard
00171             DoCommandL( KVCardFileName, err );
00172             }
00173         else if(ENfcvCal == iSharingType)
00174             { // Share vCal
00175             DoCommandL( KCalenderFileName, err );
00176             }
00177         }
00178     else if ( aCmdId == KAiwCmdNFCGive && aEventId == KAiwEventCompleted)
00179         {
00180         // Send nothification to UI that file sharing is complete.
00181         if (ENfcvCard == iSharingType) 
00182             { // Sharing vCard is complete.
00183             QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("vCard sharing is successfully completed")));
00184             }
00185         else if(ENfcvCal == iSharingType)
00186             { // Sharing vCal is complete.
00187             QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("vCal sharing is successfully completed")));
00188             }        
00189         iAiwServiceHandler->Reset();
00190         } 
00191 
00192     else if ( aCmdId == KAiwCmdNFCEasySetup && aEventId == KAiwEventCompleted )
00193         {
00194         // Send notification to the UI that easy set up is complete.
00195         QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("Easy set up is complete.")));
00196         iAiwServiceHandler->Reset();
00197         }
00198     else if ( aCmdId == KAiwCmdNFCGive && aEventId == KAiwEventError )
00199         {
00200         // Send notifcation to the UI that error occured during asynchronous tranfer request. 
00201         QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("Error during asynchronous transfer request! Please try again")));
00202         iAiwServiceHandler->Reset();
00203         }
00204     else if ( aCmdId == KAiwCmdNFCEasySetup && aEventId == KAiwEventError )
00205         {
00206         // Send notifcation to the UI that error occured during asynchronous setup request.
00207         QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("Error during asynchronous service setup request!")));
00208         iAiwServiceHandler->Reset();
00209         }
00210     
00211     else if ( aEventId == KAiwEventCanceled )
00212         {
00213         // Sharing cancelled.
00214         iAiwServiceHandler->Reset();
00215         }
00216     else if ( aEventId == KAiwEventError )
00217         {
00218         // AIW event failure. so reset the service handler.
00219         iAiwServiceHandler->Reset();
00220         }
00221     else
00222         {
00223         // Sharing failed, so reset the service handler.
00224         iAiwServiceHandler->Reset();
00225         }
00226     return KErrNone;
00227     }
00228 
00229 /*
00230  * StopserviceL: stop the AIW service handlers from taking new request by deleting handler
00231  * and create a new handler for subsequent uses.
00232  */
00233 void CNfcAiwEngine::StopServiceL()
00234     {
00235     iAiwServiceHandler->Reset();
00236     if(iAiwServiceHandler)
00237         {
00238         delete iAiwServiceHandler;
00239         }
00240     iAiwServiceHandler = CAiwServiceHandler::NewL();
00241     }
00242 
00243 /*
00244  * DoCommandL
00245  * Executes the command to share the file via NFC channel(KAiwCmdNFCGive)
00246  * @param aFileName The file name to be shared.
00247  * @param error Failure value returned to the calling method.
00248  */
00249 TInt CNfcAiwEngine::DoCommandL( const TDesC& aFileName, TInt error )
00250     {
00251     error = iFs.Connect();
00252     if (error == KErrNone)
00253         {
00254         error = iFs.ShareProtected();
00255         }
00256 
00257     if (error == KErrNone)
00258         {// Open file to be shared in read or sharereadonly mode.
00259         error = iFile.Open(iFs, aFileName, EFileRead | EFileShareReadersOnly);
00260         }
00261     if (error != KErrNone)
00262         {
00263         return error;
00264         }
00265      
00266      CAiwGenericParamList* inParamList = CAiwGenericParamList::NewLC();
00267      TAiwGenericParam obj(EGenericParamFile, TAiwVariant(iFile));
00268      // Attach the file to inParamList.
00269      inParamList->AppendL(obj);
00270      // Send the file via NFC (asynchronous).
00271      iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdNFCGive, *inParamList,
00272                                              iAiwServiceHandler->OutParamListL(), 0,
00273                                              this);
00274      CleanupStack::PopAndDestroy(); // inParamList.
00275      iFile.Close(); 
00276      iFs.Close();
00277      return error;
00278     }

Generated by  doxygen 1.6.2