serviceproviders/sapi_sysinfo/sysinfoservice/src/sysaccessoryrequest.cpp
changeset 19 989d2f495d90
equal deleted inserted replaced
14:a36b1e19a461 19:989d2f495d90
       
     1 /*
       
     2 * Copyright (c) 2002 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 the License "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:  class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include <AccessoryConnection.h>
       
    19 
       
    20 #include "sysinfoservice.h"
       
    21 #include "sysaccessoryrequest.h"
       
    22 #include "entitykeys.h"
       
    23 #include "sysrequest.h"
       
    24 
       
    25 using namespace SysInfo;
       
    26 
       
    27 // --------------------------------------------------------------------
       
    28 // CAccessory::CAccessory()
       
    29 // C++ default constructor.
       
    30 // --------------------------------------------------------------------
       
    31 //
       
    32 CAccessory::CAccessory( TSysRequest::TRequestType aReqType, TInt32 aTransID,
       
    33                     ISystemObserver* aObsrvr ): CActiveRequest(
       
    34                     aReqType, aTransID, aObsrvr), iAccCountOld(0),
       
    35                     iAccState(CAccessoryInfo::EDisconnected)
       
    36     {
       
    37     }
       
    38 
       
    39 // --------------------------------------------------------------------
       
    40 // CAccessory::~CAccessory()
       
    41 // destructor.
       
    42 // --------------------------------------------------------------------
       
    43 //
       
    44 CAccessory::~CAccessory()
       
    45     {
       
    46     Cancel();
       
    47     iAccessoryConnection.CloseSubSession();
       
    48     iAccSrv.Disconnect();
       
    49     }
       
    50 
       
    51 // --------------------------------------------------------------------
       
    52 // CAccessory::NewL()
       
    53 // Two-phased constructor, returns instance of this class.
       
    54 // --------------------------------------------------------------------
       
    55 //
       
    56 CAccessory* CAccessory::NewL(const TSysRequest& aRequest)
       
    57     {
       
    58     CAccessory* self;
       
    59     self = new (ELeave) CAccessory(aRequest.RequestType(),
       
    60                             aRequest.TransactionID(), aRequest.Observer());
       
    61 
       
    62     CleanupStack::PushL(self);
       
    63     self->ConstructL();
       
    64     CleanupStack::Pop(self);
       
    65     return self;
       
    66     }
       
    67 	
       
    68 // --------------------------------------------------------------------
       
    69 // CAccessory::ConstructL()
       
    70 // 2nd Phase constructor to allocate required resources for this obj.
       
    71 // --------------------------------------------------------------------
       
    72 //
       
    73 void CAccessory::ConstructL()
       
    74     {
       
    75     //Supportes Only Notifications.
       
    76     if( TSysRequest::ENotification != RequestType())
       
    77         User::Leave(KErrNotSupported);
       
    78 
       
    79     User::LeaveIfError(iAccSrv.Connect());
       
    80     TInt err = iAccessoryConnection.CreateSubSession(iAccSrv);
       
    81     if( err )
       
    82         {
       
    83         iAccSrv.Disconnect();
       
    84         User::Leave(err);
       
    85         }
       
    86     }
       
    87 	
       
    88 // --------------------------------------------------------------------
       
    89 // CAccessory::Request()
       
    90 // Issues accesssory request.
       
    91 // --------------------------------------------------------------------
       
    92 //
       
    93 TInt CAccessory::Request()
       
    94     {
       
    95     if(!iAccCountOld)
       
    96         {
       
    97         // get all connected accessories into iGenIdArrayOld.
       
    98         iAccessoryConnection.GetAccessoryConnectionStatus(iGenIdArrayOld);
       
    99         //Store Connection Count.
       
   100         iAccCountOld = iGenIdArrayOld.Count();
       
   101         }
       
   102 
       
   103     //Issue request.
       
   104     if( !IsActive() )
       
   105         {
       
   106         iAccessoryConnection.NotifyAccessoryConnectionStatusChanged(iStatus, 
       
   107                                                                 iGenIdArrayCur);
       
   108         SetActive ();
       
   109         }
       
   110     return KErrNone;
       
   111     }
       
   112 	
       
   113 // --------------------------------------------------------------------
       
   114 // CAccessory::RunL()
       
   115 // Gets called in event of request completion or error situation.
       
   116 // --------------------------------------------------------------------
       
   117 //
       
   118 void CAccessory::RunL()
       
   119     {
       
   120     TSysRequest::TRequestType ReqType = RequestType();
       
   121     TInt32 transId = this->TransactionID();
       
   122 
       
   123     // get accessory count.
       
   124     iAccCountCur = iGenIdArrayCur.Count();
       
   125     iAccCountOld = iGenIdArrayOld.Count();
       
   126 
       
   127     // Check if it a was a connect or a disconnect
       
   128     if( iAccCountCur > iAccCountOld )
       
   129         iAccState = CAccessoryInfo::EConnected;
       
   130     else
       
   131         iAccState = CAccessoryInfo::EDisconnected;
       
   132 
       
   133     TInt error(iStatus.Int());
       
   134     
       
   135     if(error == KErrNone)
       
   136         {
       
   137         CAccessoryInfo* accInfo = NULL;
       
   138         //newly connected accessory will be at index '0'.
       
   139         if(iAccState == CAccessoryInfo::EConnected)
       
   140             {
       
   141             TAccPolGenericID ConnectedID;
       
   142             ConnectedID = iGenIdArrayCur.GetGenericIDL(0);
       
   143             accInfo = CAccessoryInfo::NewL(ConnectedID,iAccState);
       
   144             }
       
   145         else
       
   146             {
       
   147             TAccPolGenericID DisConnectedID;
       
   148             //get DisConnected accessory ID.
       
   149             DisConnectedAccessoryL(DisConnectedID);
       
   150             accInfo = CAccessoryInfo::NewL(DisConnectedID,iAccState);
       
   151             }
       
   152 
       
   153         CleanupStack::PushL(accInfo);
       
   154         TRAP_IGNORE(SystemObserver()->HandleResponseL(KGeneral, KAccessoryStatus,
       
   155                                         accInfo, transId, RequestType(), error));
       
   156         CleanupStack::Pop(accInfo);
       
   157         //Re-Issue request
       
   158         Request();
       
   159         }
       
   160     else
       
   161         {
       
   162         TRAP_IGNORE(SystemObserver()->HandleResponseL(KGeneral, KAccessoryStatus, 
       
   163                                         NULL, transId, RequestType(), error));
       
   164         }
       
   165     //copy current list to old list.
       
   166     iGenIdArrayOld = iGenIdArrayCur;
       
   167     }
       
   168 
       
   169 // --------------------------------------------------------------------
       
   170 // CAccessory::DoCancel()
       
   171 // Gets called in event of request completion or error situation.
       
   172 // --------------------------------------------------------------------
       
   173 //
       
   174 void CAccessory::DoCancel()
       
   175     {
       
   176     if(IsActive())
       
   177         iAccessoryConnection.CancelNotifyAccessoryConnectionStatusChanged();
       
   178     }
       
   179 	
       
   180 // --------------------------------------------------------------------
       
   181 // CAccessory::DisConnectedAccessoryL()
       
   182 // retruns disconnected accessory GenericID.
       
   183 // --------------------------------------------------------------------
       
   184 //
       
   185 void CAccessory::DisConnectedAccessoryL(TAccPolGenericID& aGenId)
       
   186     {
       
   187     for(TInt i=0; i<iAccCountOld; i++)
       
   188         {
       
   189         TAccPolGenericID DisConnected;
       
   190         //Take a GenericID in old list and search for it in cur list.
       
   191         //if present, continue with next.
       
   192         DisConnected = iGenIdArrayOld.GetGenericIDL(i);
       
   193         aGenId= DisConnected;
       
   194 
       
   195         for(TInt j=0; j<iAccCountCur;j++)
       
   196             {
       
   197             //stop searching when a missing entry in old list is found.
       
   198             if( DisConnected.UniqueID() != 
       
   199                         (iGenIdArrayCur.GetGenericIDL(j)).UniqueID() )
       
   200                 {
       
   201                 return;
       
   202                 }
       
   203             }
       
   204         }
       
   205     }
       
   206 
       
   207 // End of file.