cbsatplugin/atmisccmdplugin/src/scpbrcommandhandler.cpp
branchRCL_3
changeset 72 4b59561a31c0
parent 64 1934667b0e2b
equal deleted inserted replaced
64:1934667b0e2b 72:4b59561a31c0
     1 /*
       
     2  * Copyright (c) 2010 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  * Initial Contributors:
       
     9  * Nokia Corporation - initial contribution.
       
    10  *
       
    11  * Contributors:
       
    12  * Description :
       
    13  *
       
    14  */
       
    15 
       
    16 #include "scpbrcommandhandler.h"
       
    17 
       
    18 #include <mpbutil.h>
       
    19 #include "debug.h"
       
    20 #include "atmisccmdpluginconsts.h"
       
    21 
       
    22 CSCPBRCommandHandler::CSCPBRCommandHandler( 
       
    23 		MATMiscCmdPlugin* aCallback, 
       
    24 		TAtCommandParser& aATCmdParser, 
       
    25 		RMobilePhone& aPhone) 
       
    26 		: CATCmdAsyncBase( aCallback, aATCmdParser, aPhone )
       
    27 	{
       
    28     TRACE_FUNC_ENTRY
       
    29     TRACE_FUNC_EXIT
       
    30 	}
       
    31 
       
    32 CSCPBRCommandHandler::~CSCPBRCommandHandler()
       
    33 	{
       
    34     TRACE_FUNC_ENTRY
       
    35     Cancel();
       
    36     iPhoneBookStore.Close();
       
    37     iCustomApi.Close();
       
    38     iReply.Close();
       
    39     delete iPhoneBookBuffer;
       
    40     iEntries.Close();
       
    41     iContactsBuf.Close();
       
    42     TRACE_FUNC_EXIT
       
    43 	}
       
    44 
       
    45 void CSCPBRCommandHandler::ConstructL()
       
    46 	{
       
    47     TRACE_FUNC_ENTRY
       
    48     TInt err = iPhoneBookStore.Open( iPhone, KETelIccAdnPhoneBook);
       
    49     if( err != KErrNone )  // if the phonebookstore can't be opened or error occurs when try to open it.
       
    50         {
       
    51         iState = ESCPBRStateNotSupported;
       
    52         TRACE_FUNC_EXIT
       
    53         return;
       
    54         }
       
    55     err = iCustomApi.Open( iPhone );
       
    56     if( err != KErrNone )  // If the custom api can't be opened.
       
    57         {
       
    58         iPhoneBookStore.Close();
       
    59         iState = ESCPBRStateNotSupported;
       
    60         TRACE_FUNC_EXIT
       
    61         return;
       
    62         }
       
    63     iReply.CreateL( KBufBlockSize );
       
    64 	iPhoneBookBuffer = new ( ELeave ) CPhoneBookBuffer();	
       
    65     TRACE_FUNC_EXIT
       
    66 	}
       
    67 
       
    68 CSCPBRCommandHandler* CSCPBRCommandHandler::NewL( 
       
    69 		MATMiscCmdPlugin* aCallback, 
       
    70 		TAtCommandParser& aATCmdParser, 
       
    71 		RMobilePhone& aPhone )
       
    72 	{
       
    73     TRACE_FUNC_ENTRY
       
    74 	CSCPBRCommandHandler * self = new ( ELeave ) CSCPBRCommandHandler( aCallback, aATCmdParser, aPhone );
       
    75 	CleanupStack::PushL( self );
       
    76 	self->ConstructL();
       
    77 	CleanupStack::Pop( self );
       
    78     TRACE_FUNC_EXIT
       
    79 	return self;
       
    80 	}
       
    81 
       
    82 void CSCPBRCommandHandler::HandleCommand(const TDesC8& /*aCmd*/, RBuf8& /*aReply*/, TBool /*aReplyNeeded*/ )
       
    83 	{
       
    84     TRACE_FUNC_ENTRY
       
    85     if( iState == ESCPBRStateIdle )  // Not supported or have unfinished task.
       
    86         {
       
    87         iReply.Zero();  
       
    88         TAtCommandParser::TCommandHandlerType cmdHandlerType = iATCmdParser.CommandHandlerType();
       
    89         switch( cmdHandlerType )
       
    90             {
       
    91             case TAtCommandParser::ECmdHandlerTypeTest: // command: AT^SCPBR=?
       
    92                 {
       
    93                 if( iTotalEntries == 0 )
       
    94                     {
       
    95                     iState = ESCPBRStateGetPhoneBookInfo;
       
    96                     RMobilePhoneBookStore::TMobilePhoneBookInfoV1Pckg pkg( iPhoneBookInfo );
       
    97                     iPhoneBookStore.GetInfo( iStatus, pkg );
       
    98                     SetActive();
       
    99                     }
       
   100                 else if ( iMLength ==0 )
       
   101                     {
       
   102                     iState = ESCPBRStateGet3GPBInfo;
       
   103                     iCustomApi.Get3GPBInfo( iStatus, i3GPBInfo );
       
   104                     SetActive();
       
   105                     }
       
   106                 else
       
   107                     {
       
   108                     iReply.Format( KSCPBRSupportedEntriesIndexList, 
       
   109                             iTotalEntries, iNLength, iTLength, iMLength );
       
   110                     iCallback->CreateReplyAndComplete( EReplyTypeOk, iReply );
       
   111                     }
       
   112                 break;
       
   113                 }
       
   114             case TAtCommandParser::ECmdHandlerTypeSet:  // command: AT^SCPBR=<index1>[,<index2>]
       
   115                 {
       
   116                 if( ParseParameters() == KErrNone )
       
   117                     {
       
   118                     iEntries.Reset();
       
   119                     TInt amountOfEntries = iIndex2 - iIndex1 + 1;  // the amount of entries.
       
   120                     Trace( _L8("The amount of entries: %d "), amountOfEntries );
       
   121                     iContactsBuf.Zero();
       
   122                     TInt bufSize;
       
   123                     if( amountOfEntries >= KSCPBRMaxEntryCount )
       
   124                         {
       
   125                         bufSize= KSCPBRMaxEntryCount * KSCPBRDefaultSizeOfOneEntry;
       
   126                         amountOfEntries = KSCPBRMaxEntryCount;
       
   127                         }
       
   128                     else
       
   129                         {
       
   130                         bufSize = amountOfEntries * KSCPBRDefaultSizeOfOneEntry;
       
   131                         }
       
   132                     
       
   133                     if( bufSize >= iContactsBuf.MaxLength() )
       
   134                         {
       
   135                         TInt err = iContactsBuf.ReAlloc( bufSize );
       
   136                         Trace( _L8("The error code of the realloc: %d "), err );
       
   137                         if( err != KErrNone )
       
   138                             {
       
   139                             iState = ESCPBRStateIdle;
       
   140                             iCallback->CreateReplyAndComplete( EReplyTypeError );
       
   141                             break;
       
   142                             }
       
   143                         }
       
   144                     iState = ESCPBRStateRead;
       
   145                     iPhoneBookStore.Read( iStatus,  iIndex1, amountOfEntries, iContactsBuf );
       
   146                     SetActive();
       
   147                     }
       
   148                 else
       
   149                     {
       
   150                     iCallback->CreateReplyAndComplete( EReplyTypeError );
       
   151                     }
       
   152                 break;
       
   153                 }
       
   154             default:
       
   155                 {
       
   156                 iCallback->CreateReplyAndComplete( EReplyTypeError );
       
   157                 break;
       
   158                 }
       
   159             }
       
   160         }
       
   161     else
       
   162         {
       
   163         iCallback->CreateReplyAndComplete( EReplyTypeError );
       
   164         } 
       
   165        TRACE_FUNC_EXIT
       
   166 	}
       
   167 
       
   168 TInt CSCPBRCommandHandler::ParseParameters()
       
   169     {
       
   170     TRACE_FUNC_ENTRY
       
   171     iIndex1 = 0; 
       
   172     iIndex2 = 0;
       
   173     TInt index3;
       
   174     TInt ret1 = iATCmdParser.NextIntParam( iIndex1 );
       
   175     TInt ret2 = iATCmdParser.NextIntParam( iIndex2 );
       
   176     TInt ret3 = iATCmdParser.NextIntParam( index3 );
       
   177     
       
   178     if( ret1 != KErrNone 
       
   179             || ( ret2 != KErrNone && ret2 != KErrNotFound )
       
   180             || ret3 != KErrNotFound )
       
   181         {
       
   182         TRACE_FUNC_EXIT
       
   183         return KErrArgument;
       
   184         }
       
   185     if( ret2 == KErrNotFound )
       
   186         {
       
   187         iIndex2 = iIndex1;
       
   188         }
       
   189     if (iIndex2 < iIndex1)
       
   190         {
       
   191         TRACE_FUNC_EXIT
       
   192         return KErrArgument;
       
   193         }
       
   194     TRACE_FUNC_EXIT
       
   195     return KErrNone;
       
   196     }
       
   197 
       
   198 void CSCPBRCommandHandler::RunL()
       
   199 	{
       
   200     TRACE_FUNC_ENTRY
       
   201     iReply.Zero();
       
   202     TInt result = iStatus.Int();
       
   203     if( result == KErrNone )
       
   204         {
       
   205         switch( iState )
       
   206             {
       
   207             case ESCPBRStateRead:
       
   208                 {
       
   209                 ExtractEntriesL();
       
   210                 FormatReplyL();
       
   211                 iState = ESCPBRStateIdle;
       
   212                 iCallback->CreateReplyAndComplete(EReplyTypeOk, iReply);
       
   213                 break;
       
   214                 }
       
   215             case ESCPBRStateGetPhoneBookInfo:
       
   216                 {
       
   217                 iTotalEntries = iPhoneBookInfo.iTotalEntries;
       
   218                 iNLength = iPhoneBookInfo.iMaxNumLength;
       
   219                 iTLength = iPhoneBookInfo.iMaxTextLength;
       
   220                 if (iMLength == 0)
       
   221                     {
       
   222                     iState = ESCPBRStateGet3GPBInfo;
       
   223                     iCustomApi.Get3GPBInfo( iStatus, i3GPBInfo );
       
   224                     SetActive();
       
   225                     }
       
   226                 else 
       
   227                     {
       
   228                     iReply.Format( KSCPBRSupportedEntriesIndexList, 
       
   229                             iTotalEntries, iNLength, iTLength, iMLength );
       
   230                     iState = ESCPBRStateIdle;
       
   231                     iCallback->CreateReplyAndComplete( EReplyTypeOk, iReply );
       
   232                     }
       
   233                 break;
       
   234                 }
       
   235             case ESCPBRStateGet3GPBInfo:
       
   236                 {
       
   237                 iMLength = i3GPBInfo.iMaxLenEmail;
       
   238                 iReply.Format( KSCPBRSupportedEntriesIndexList, 
       
   239                         iTotalEntries, iNLength, iTLength, iMLength );
       
   240                 iState = ESCPBRStateIdle;
       
   241                 iCallback->CreateReplyAndComplete( EReplyTypeOk, iReply );
       
   242                 break;
       
   243                 }
       
   244             default: 
       
   245                 {
       
   246                 iState = ESCPBRStateIdle;
       
   247                 iCallback->CreateReplyAndComplete( EReplyTypeError );
       
   248                 break;
       
   249                 }
       
   250             }
       
   251         }
       
   252     else
       
   253         {
       
   254         iState = ESCPBRStateIdle;
       
   255         iCallback->CreateCMEReplyAndComplete( result );
       
   256         }
       
   257     TRACE_FUNC_EXIT
       
   258 	}
       
   259 
       
   260 TInt CSCPBRCommandHandler::RunError(TInt aError)
       
   261     {
       
   262     TRACE_FUNC_ENTRY
       
   263     iState = ESCPBRStateIdle;
       
   264     iCallback->CreateReplyAndComplete( EReplyTypeError );
       
   265     TRACE_FUNC_EXIT
       
   266     return KErrNone;
       
   267     }
       
   268 
       
   269 void CSCPBRCommandHandler::ExtractEntriesL()
       
   270     {
       
   271     TRACE_FUNC_ENTRY
       
   272     iPhoneBookBuffer->Set( &iContactsBuf );
       
   273     iPhoneBookBuffer->StartRead();
       
   274     TUint8 fieldTag;
       
   275     CPhoneBookBuffer::TPhBkTagType dataType;
       
   276     TInt numCount = 0;
       
   277 	// clear entry arry for use.
       
   278     iEntries.Reset();
       
   279     User::LeaveIfError(iPhoneBookBuffer->GetTagAndType( fieldTag, dataType ));
       
   280     if( fieldTag != RMobilePhoneBookStore::ETagPBNewEntry )
       
   281         {
       
   282         // Buffer corrupt
       
   283         User::Leave(KErrCorrupt);
       
   284         }
       
   285     TInt ret = KErrNone;
       
   286     TInt index = -1; // at least one entry
       
   287     while (ret != KErrNotFound) // if reach the buffer end.
       
   288         {
       
   289         switch (fieldTag)
       
   290             {
       
   291             case RMobilePhoneBookStore::ETagPBNewEntry:
       
   292                 {
       
   293                 TPhoneBookEntry entry;
       
   294                 iEntries.AppendL(entry);
       
   295                 ++index;
       
   296                 numCount = 0; // set the number count of new entry to 0. 
       
   297                 break;
       
   298                 }
       
   299             case RMobilePhoneBookStore::ETagPBAdnIndex:
       
   300                 {
       
   301                 TUint16 simIndex = 0;
       
   302                 if (dataType != CPhoneBookBuffer::EPhBkTypeInt16)
       
   303                     {
       
   304                     User::Leave(KErrCorrupt);
       
   305                     }
       
   306                 User::LeaveIfError( iPhoneBookBuffer->GetValue(simIndex) );
       
   307 
       
   308                 iEntries[index].iIndex = simIndex;
       
   309                 break;
       
   310                 }
       
   311             case RMobilePhoneBookStore::ETagPBText:
       
   312                 {
       
   313                 TPtrC16 text;
       
   314                 if (dataType != CPhoneBookBuffer::EPhBkTypeDes16)
       
   315                     {
       
   316                     User::Leave(KErrCorrupt);
       
   317                     }
       
   318                 User::LeaveIfError( iPhoneBookBuffer->GetValue(text) );
       
   319                 CopyToPhonebookEntryField(iEntries[index].iName, text);
       
   320                 break;
       
   321                 }
       
   322             case RMobilePhoneBookStore::ETagPBNumber:
       
   323                 {
       
   324                 TPtrC16 number;
       
   325                 if (dataType != CPhoneBookBuffer::EPhBkTypeDes16)
       
   326                     {
       
   327                     User::Leave(KErrCorrupt);
       
   328                     }
       
   329                 User::LeaveIfError( iPhoneBookBuffer->GetValue(number) );
       
   330                 ++numCount;
       
   331                 switch (numCount)
       
   332                     {
       
   333                     case 1: // The first number
       
   334                         CopyToPhonebookEntryField(iEntries[index].iNumber1, number);
       
   335                         break;
       
   336                     case 2: // The Second number
       
   337                         CopyToPhonebookEntryField(iEntries[index].iNumber2, number);
       
   338                         break;
       
   339                     case 3: // the Third number
       
   340                         CopyToPhonebookEntryField(iEntries[index].iNumber3, number);
       
   341                         break;
       
   342                     case 4: // the fourth number.
       
   343                         CopyToPhonebookEntryField(iEntries[index].iNumber4, number);
       
   344                         break;
       
   345                     }
       
   346                 break;
       
   347                 }
       
   348             case RMobilePhoneBookStore::ETagPBEmailAddress:
       
   349                 {
       
   350                 TPtrC16 email;
       
   351                 if (dataType != CPhoneBookBuffer::EPhBkTypeDes16)
       
   352                     {
       
   353                     User::Leave(KErrCorrupt);
       
   354                     }
       
   355                 User::LeaveIfError( iPhoneBookBuffer->GetValue(email) );
       
   356                 CopyToPhonebookEntryField(iEntries[index].iEmail, email);
       
   357                 break;
       
   358                 }
       
   359             default:
       
   360                 {
       
   361                 iPhoneBookBuffer->SkipValue(dataType);
       
   362                 break;
       
   363                 }
       
   364             }
       
   365         ret = iPhoneBookBuffer->GetTagAndType(fieldTag, dataType);
       
   366         }
       
   367  
       
   368     TRACE_FUNC_EXIT
       
   369     }
       
   370 
       
   371 void CSCPBRCommandHandler::CopyToPhonebookEntryField(TDes8& aDest, const TDesC16& aSrc)
       
   372     {
       
   373     TRACE_FUNC_ENTRY
       
   374     TInt maxLength = aDest.MaxLength();
       
   375     if ( aSrc.Length() <= maxLength )
       
   376         {
       
   377         aDest.Copy(aSrc);
       
   378         }
       
   379     else
       
   380         {
       
   381         aDest.Copy( aSrc.Left(maxLength) );
       
   382         }
       
   383     TRACE_FUNC_EXIT    
       
   384     }
       
   385 
       
   386 void CSCPBRCommandHandler::FormatReplyL()
       
   387     {
       
   388     TRACE_FUNC_ENTRY
       
   389 
       
   390     TInt count = iEntries.Count();
       
   391     for (TInt i = 0; i < count; ++i)
       
   392         {
       
   393         AppendEntryL(iEntries[i]);
       
   394         }
       
   395     iReply.Append(KCRLF);
       
   396     
       
   397     TRACE_FUNC_EXIT
       
   398     }
       
   399 
       
   400 void CSCPBRCommandHandler::AppendEntryL(const TPhoneBookEntry& aEntry)
       
   401     {
       
   402     TRACE_FUNC_ENTRY
       
   403     TBuf8<KSCPBRDefaultSizeOfOneEntry> entryBuffer;
       
   404     aEntry.Externalize(entryBuffer);
       
   405     
       
   406     TInt length = entryBuffer.Length() + KSCPBR().Length(); 
       
   407     if (length + iReply.Length() > iReply.MaxLength())
       
   408         {
       
   409         iReply.ReAllocL(iReply.MaxLength()+KBufBlockSize);
       
   410         }
       
   411     
       
   412     iReply.Append(KSCPBR);
       
   413     iReply.Append(entryBuffer);
       
   414     TRACE_FUNC_EXIT
       
   415     }
       
   416 
       
   417 void CSCPBRCommandHandler::DoCancel()
       
   418 	{
       
   419     TRACE_FUNC_ENTRY
       
   420     switch( iState )
       
   421         {
       
   422         case ESCPBRStateRead:
       
   423             {
       
   424             iPhoneBookStore.CancelAsyncRequest( EMobilePhoneStoreRead );
       
   425             break;
       
   426             }
       
   427         case ESCPBRStateGetPhoneBookInfo:
       
   428             {
       
   429             iPhoneBookStore.CancelAsyncRequest( EMobilePhoneStoreGetInfo );
       
   430             break;
       
   431             }
       
   432         case ESCPBRStateGet3GPBInfo:
       
   433             {
       
   434             iPhoneBookStore.CancelAsyncRequest( EGet3GPBInfoIPC );
       
   435             break;
       
   436             }
       
   437         default:
       
   438             break;
       
   439         }
       
   440     iState = ESCPBRStateIdle;
       
   441     TRACE_FUNC_EXIT
       
   442 	}
       
   443 
       
   444 
       
   445 void CSCPBRCommandHandler::TPhoneBookEntry::Externalize(TDes8& aDes) const
       
   446     {
       
   447     TRACE_FUNC_ENTRY
       
   448     
       
   449     aDes.Format(KSCPBRReplyOneEntry, iIndex, &iNumber1, &iNumber2, &iNumber3, &iNumber4, &iName, &iEmail);
       
   450     
       
   451     TRACE_FUNC_EXIT
       
   452     }