localconnectivityservice/generichid/src/hidgeneric.cpp
branchRCL_3
changeset 19 0aa8cc770c8a
equal deleted inserted replaced
18:453dfc402455 19:0aa8cc770c8a
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Generic hid implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32std.h>
       
    19 
       
    20 #include "debug.h"
       
    21 #include "hidgeneric.h"
       
    22 #include "hidreportroot.h"
       
    23 #include "hidparser.h"
       
    24 #include "hiddriveritem.h"
       
    25 #include "hidconnectioninfo.h"
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // NewLC
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 EXPORT_C CGenericHid* CGenericHid::NewLC(MTransportLayer* aTransportLayer)
       
    34     {
       
    35     TRACE_INFO((_L("[HID]\tCGenericHid::NewLC(0x%08x)"), aTransportLayer));
       
    36     CGenericHid* self = new (ELeave) CGenericHid(aTransportLayer);
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL();
       
    39     return self;
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // NewL
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C CGenericHid* CGenericHid::NewL(MTransportLayer* aTransportLayer)
       
    47     {
       
    48     TRACE_INFO((_L("[HID]\tCGenericHid::NewL(0x%08x)"), aTransportLayer));
       
    49 	CGenericHid* self = NewLC(aTransportLayer);
       
    50     CleanupStack::Pop();
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // ConstructL()
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 void CGenericHid::ConstructL()
       
    59     {
       
    60     TRACE_INFO(_L("[HID]\tCGenericHid::ConstructL()"));
       
    61     TRACE_INFO(_L("[HID]\tCGenericHid::ConstructL(): Creating Parser..."));
       
    62     iParser = CParser::NewL();
       
    63     iInputHandlingReg = CHidInputDataHandlingReg::NewL();
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CGenericHid()
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CGenericHid::CGenericHid(MTransportLayer* aTransportLayer) :
       
    71     iDriverList(_FOFF(CDriverListItem, iSlink)),
       
    72     iTransportLayer(aTransportLayer)
       
    73     {
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // Destructor
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CGenericHid::~CGenericHid()
       
    81     {
       
    82     TRACE_FUNC_ENTRY
       
    83     RemoveDrivers();
       
    84     iConnectionInfo.ResetAndDestroy();
       
    85     iConnectionInfo.Close();
       
    86     delete iInputHandlingReg;
       
    87     delete iParser;
       
    88     REComSession::FinalClose();
       
    89     TRACE_FUNC_EXIT
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // From MDriverAccess
       
    94 // CountryCodeL
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 TUint CGenericHid::CountryCodeL(TInt aConnID)
       
    98     {
       
    99     // Pass the request through to the transport layer.
       
   100     return (iTransportLayer->CountryCodeL(aConnID));
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // From MDriverAccess
       
   105 // VendorIdL
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 TUint CGenericHid::VendorIdL(TInt aConnID)
       
   109     {
       
   110     // Pass the request through to the transport layer.
       
   111     return (iTransportLayer->VendorIdL(aConnID));
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // From MDriverAccess
       
   116 // ProductIdL()
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 TUint CGenericHid::ProductIdL(TInt aConnID)
       
   120     {
       
   121     // Pass the request through to the transport layer.
       
   122     return iTransportLayer->ProductIdL(aConnID);
       
   123     }
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // From MDriverAccess
       
   127 // SetProtocol
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 void CGenericHid::SetProtocolL(TInt aConnectionId, TUint16 aInterface,
       
   131                               MDriverAccess::TProtocols aProtocol, 
       
   132                               CHidDriver* aDriver)
       
   133     {
       
   134     iTransportLayer->SetProtocolL(aConnectionId, static_cast<TUint16>(aProtocol), 
       
   135             aInterface);    
       
   136     CConnectionInfo* conninfo = SeekConnectionInfo( aConnectionId );
       
   137         if ( conninfo )
       
   138             {
       
   139             conninfo->SetLastCommandHandler(aDriver);
       
   140             }          
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // From MDriverAccess
       
   145 // GetProtocol
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CGenericHid::GetProtocolL(TInt aConnectionId,TUint16 aInterface)
       
   149     {
       
   150     iTransportLayer->GetProtocolL(aConnectionId, aInterface);    
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // From MDriverAccess
       
   155 // GetReport
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 void CGenericHid::GetReportL(TInt aConnectionId,
       
   159     TUint8 aReportId, TUint16 aInterface, TUint16 aLength)
       
   160     {
       
   161     iTransportLayer->GetReportL(aConnectionId, MDriverAccess::EInput, aReportId, 
       
   162             aInterface, aLength);    
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // From MDriverAccess
       
   167 // SetReport()
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 void CGenericHid::SetReportL(TInt aConnectionId, TUint8 aReportId,
       
   171     MDriverAccess::TReportType aReportType, const TDesC8& aPayload,
       
   172     TUint16 aInterface, CHidDriver* aDriver)
       
   173     {
       
   174     iTransportLayer->SetReportL(aConnectionId, static_cast<TUint8>(aReportType),
       
   175         aReportId, aInterface, aPayload);    
       
   176     CConnectionInfo* conninfo = SeekConnectionInfo( aConnectionId );
       
   177         if ( conninfo )
       
   178             {
       
   179             conninfo->SetLastCommandHandler(aDriver);
       
   180             }          
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // From MDriverAccess
       
   185 // DataOut()
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 void CGenericHid::DataOutL(TInt aConnectionId, TUint8 aReportId,
       
   189                           const TDesC8& aPayload,
       
   190                           TUint16 aInterface)
       
   191     {
       
   192     iTransportLayer->DataOutL(aConnectionId, aReportId, aInterface, aPayload);    
       
   193     }
       
   194 
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // From MDriverAccess
       
   198 // GetIdle()
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 void CGenericHid::GetIdleL(TInt aConnectionId, TUint8 aReportId,
       
   202     TUint16 aInterface )
       
   203     {
       
   204     iTransportLayer->GetIdleL(aConnectionId, aReportId, aInterface);    
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // From MDriverAccess
       
   209 // SetIdle()
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 void CGenericHid::SetIdleL(TInt aConnectionId, TUint8 aDuration,
       
   213     TUint8 aReportId, TUint16 aInterface, CHidDriver* aDriver)
       
   214     {
       
   215     iTransportLayer->SetIdleL(aConnectionId, aDuration, aReportId, aInterface);
       
   216     CConnectionInfo* conninfo = SeekConnectionInfo( aConnectionId );
       
   217     if ( conninfo )
       
   218         {
       
   219         conninfo->SetLastCommandHandler(aDriver);
       
   220         }          
       
   221                        
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // RemoveDrivers()
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 void CGenericHid::RemoveDrivers()
       
   229     {
       
   230     TRACE_FUNC
       
   231     // Driver instances
       
   232     CDriverListItem* driverItem;
       
   233     while ( !iDriverList.IsEmpty() )
       
   234         {
       
   235         driverItem = iDriverList.Last();
       
   236         iDriverList.Remove( *driverItem );
       
   237         delete driverItem;
       
   238         }
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // From CHidTransport
       
   243 // Disconnected()
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 TInt CGenericHid::Disconnected( TInt aConnectionId )
       
   247     {
       
   248     TRACE_FUNC_ENTRY
       
   249     TInt retVal = KErrNone;
       
   250     
       
   251     TSglQueIter<CDriverListItem> driverIter( iDriverList );
       
   252     driverIter.SetToFirst();    
       
   253 
       
   254     CDriverListItem* driverItem = driverIter;    
       
   255     while ( driverItem )
       
   256         {            
       
   257         driverIter++;    
       
   258         if ( driverItem->ConnectionId() == aConnectionId )
       
   259             {
       
   260             TRACE_INFO(_L("[HID]\tCGenericHid::Disconnected driver"));
       
   261             if (driverItem->Driver())
       
   262                 {
       
   263                 driverItem->Driver()->Disconnected(0);
       
   264                 }
       
   265             // Remove it from the list of driver instances           
       
   266             
       
   267             iDriverList.Remove(*driverItem);
       
   268             delete driverItem;
       
   269             driverItem = NULL;
       
   270             retVal = KErrNone;
       
   271             }                                  
       
   272         driverItem = driverIter;
       
   273         
       
   274         TRACE_INFO(_L("[HID]\tCGenericHid::Disconnected next driver"));
       
   275         }
       
   276     TRACE_INFO(_L("[HID]\tCGenericHid::Disconnected remove connection info"));
       
   277     TInt count = iConnectionInfo.Count();
       
   278     for (TInt i = count-1 ; i>=0; i--)
       
   279         {
       
   280         TRACE_INFO((_L("[HID]\tCGenericHid::Disconnected remove connection info %d"),i));
       
   281         CConnectionInfo* conninfo = iConnectionInfo[i];
       
   282         TRACE_INFO((_L("[HID]\tCGenericHid::Disconnected remove connection info %d"),i));
       
   283         if ( conninfo->ConnectionID() == aConnectionId )                
       
   284             {
       
   285             iConnectionInfo.Remove(i);
       
   286             delete conninfo;
       
   287             TRACE_INFO((_L("[HID]\tCGenericHid::Disconnected remove connection info %d removed"),i));
       
   288             }
       
   289         }     
       
   290     TRACE_FUNC_EXIT
       
   291     return retVal;
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // From CHidTransport
       
   296 // ConnectedL
       
   297 // HID device has been connected.  Attempt to find a driver that can
       
   298 // handle reports in the format specified by the report descriptor.
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 TInt CGenericHid::ConnectedL( TInt aConnectionId, const TDesC8& aDescriptor )
       
   302     {
       
   303     TRACE_INFO((_L("[HID]\tCGenericHid::ConnectedL(%d, ...)"), aConnectionId))    
       
   304     
       
   305     // Place the parsed report descriptor in the driver list item:
       
   306     
       
   307     TBool found = EFalse;
       
   308     CConnectionInfo* conninfo = SeekConnectionInfo( aConnectionId );
       
   309     if ( conninfo )
       
   310         {
       
   311         return KErrInUse;    
       
   312         }
       
   313     
       
   314     CReportRoot* reportRoot = iParser->ParseL( aDescriptor );
       
   315     CleanupStack::PushL(reportRoot);
       
   316         
       
   317     TRACE_INFO(_L("[HID]\tCGenericHid::ConnectedL: evaluate driver array"));
       
   318     // Implementation info array
       
   319     RImplInfoPtrArray implInfoArray;
       
   320     REComSession::ListImplementationsL( KHidDriverPluginInterfaceUid, implInfoArray );
       
   321     CleanupClosePushL(implInfoArray);    
       
   322     
       
   323     TRACE_INFO((_L("[HID]\tCGenericHid::ConnectedL: %d implementations found"), implInfoArray.Count()));
       
   324     TInt index = 0;
       
   325     TInt retVal = KErrHidNoDriver;
       
   326     TInt supportedfields = 0;
       
   327     CHidDriver* driver = NULL;
       
   328     for ( index  = 0; index < implInfoArray.Count(); index++ )
       
   329         {
       
   330         // parse implementation UID
       
   331         CImplementationInformation* info = implInfoArray[ index  ];
       
   332         TUid implUid = info->ImplementationUid();
       
   333         TRACE_INFO((_L("[HID]\tCGenericHid::ConnectedL: load plugin 0x%08x"),implUid ));
       
   334         // load driver
       
   335         // Trap so other drivers will be enumerated even if
       
   336         // this fails:
       
   337 
       
   338         TRAPD(retTrap, driver = CHidDriver::NewL( implUid, this ));
       
   339         if ( retTrap != KErrNone)
       
   340             {
       
   341             continue;    
       
   342             }
       
   343         CleanupStack::PushL(driver);
       
   344         TRACE_INFO((_L("[HID]\tCGenericHid::ConnectedL: init plugin 0x%08x"),implUid ));
       
   345         driver->InitialiseL( aConnectionId );
       
   346         TInt ret = driver->CanHandleReportL( reportRoot );
       
   347         if (ret == KErrNone)
       
   348             {
       
   349             TRACE_INFO(_L("[HID]\tCGenericHid::ConnectedL(): found driver"));            
       
   350 	        
       
   351 	        // Make a new driver list item:
       
   352 	        CDriverListItem* driverItem = new ( ELeave ) CDriverListItem( aConnectionId );	        
       
   353 	        CleanupStack::PushL( driverItem );	                
       
   354 	        driver->SetInputHandlingReg( iInputHandlingReg );
       
   355 	        supportedfields += driver->SupportedFieldCount();	        
       
   356             iDriverList.AddLast( *driverItem );
       
   357             CleanupStack::Pop( driverItem );
       
   358             driverItem->SetDriver( driver );    
       
   359             CleanupStack::Pop( driver );
       
   360             retVal = KErrNone;
       
   361             found = ETrue;
       
   362             }
       
   363         else
       
   364         	{
       
   365  	        CleanupStack::PopAndDestroy( driver );
       
   366         	}
       
   367         }
       
   368     TRACE_INFO((_L("[HID]\tCGenericHid::ConnectedL Partial supported hid device supported %d in report %d&"),supportedfields,iParser->FieldCount()));
       
   369     if (supportedfields < iParser->FieldCount() && found )
       
   370         {
       
   371         TRACE_INFO(_L("[HID]\tCGenericHid::ConnectedL Partial supported hid device"));
       
   372         }
       
   373     implInfoArray.ResetAndDestroy();     
       
   374     CleanupStack::PopAndDestroy();  // info
       
   375     if ( found )
       
   376         {   
       
   377         TRACE_INFO(_L("[HID]\tCGenericHid::ConnectedL append connection info"));    
       
   378         conninfo = CConnectionInfo::NewL(aConnectionId, reportRoot);
       
   379         CleanupStack::Pop(reportRoot); // ownership transfered to conninfo        
       
   380         CleanupStack::PushL(conninfo);
       
   381         iConnectionInfo.AppendL(conninfo);
       
   382         CleanupStack::Pop(conninfo);
       
   383         }         
       
   384     else
       
   385         {
       
   386         CleanupStack::PopAndDestroy(reportRoot);     
       
   387         }
       
   388     
       
   389     return retVal;
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // From CHidTransport
       
   394 // DataIn
       
   395 // Determine which driver is handling this connection ID and pass the payload
       
   396 // reference to it
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 TInt CGenericHid::DataIn(TInt aConnectionId,
       
   400     CHidTransport::THidChannelType aChannel, const TDesC8& aPayload)
       
   401     {
       
   402     TRACE_FUNC_ENTRY
       
   403     TInt retVal = KErrHidNoDriver;
       
   404     TInt ret = KErrNone;
       
   405     
       
   406     TSglQueIter<CDriverListItem> driverIter( iDriverList );
       
   407     driverIter.SetToFirst();    
       
   408 
       
   409     CDriverListItem* item = driverIter;
       
   410     TBool found = EFalse; 
       
   411     while ( item )
       
   412         {            
       
   413         if ( item->ConnectionId() == aConnectionId )
       
   414                 {                
       
   415                 ret = item->Driver()->DataIn( aChannel, aPayload );
       
   416                 if (ret == KErrNone)
       
   417                     {
       
   418                     TRACE_INFO(_L("[HID]\tCGenericHid::DataIn command handled"));
       
   419                     found = ETrue;
       
   420                     retVal = KErrNone;
       
   421                     }
       
   422                 }
       
   423          TRACE_INFO(_L("[HID]\tCGenericHid::DataIn next driver"));
       
   424          driverIter++;
       
   425          item = driverIter;
       
   426          }
       
   427     if ( !found && aChannel == CHidTransport::EHidChannelCtrl )
       
   428         {
       
   429         retVal = KErrNone;
       
   430         }
       
   431     iInputHandlingReg->Reset();    
       
   432     TRACE_FUNC_EXIT    
       
   433     return retVal;
       
   434     }
       
   435 
       
   436 
       
   437 // ---------------------------------------------------------------------------
       
   438 // ReportDescriptor
       
   439 // Provides access to the parsed results to the factory
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 CReportRoot* CGenericHid::ReportDescriptor(TInt aConnectionId)
       
   443     {
       
   444     CConnectionInfo* conninfo = SeekConnectionInfo( aConnectionId );
       
   445     if ( conninfo )
       
   446         {
       
   447         return conninfo->ReportRoot();
       
   448         }
       
   449     return NULL;
       
   450     }
       
   451 
       
   452 // ---------------------------------------------------------------------------
       
   453 // From CHidTransport
       
   454 // DriverActive()
       
   455 // ---------------------------------------------------------------------------
       
   456 //
       
   457 TInt CGenericHid::DriverActive(TInt aConnectionId,
       
   458     CHidTransport::TDriverState aActive)
       
   459     {
       
   460     TRACE_FUNC_ENTRY
       
   461     TInt retVal = KErrHidNoDriver;    
       
   462     // Find the driver handling the connection and stop it    
       
   463     TSglQueIter<CDriverListItem> driverIter( iDriverList );
       
   464     driverIter.SetToFirst();  
       
   465     CDriverListItem* item = driverIter;
       
   466     
       
   467     while ( item )
       
   468         {
       
   469         TRACE_INFO(_L("[HID]\tCGenericHid::DriverActive"));        
       
   470         if ( item->ConnectionId() == aConnectionId && item->Driver() )
       
   471             {
       
   472             TRACE_INFO(_L("[HID]\tCGenericHid::DriverActive driver found"));
       
   473             if ( aActive == CHidTransport::EActive )
       
   474                 {
       
   475                 TRAP(retVal, item->Driver()->StartL( aConnectionId ));
       
   476                 if (retVal != KErrNone)
       
   477                     {
       
   478                     break;
       
   479                     }
       
   480                 }
       
   481             else if ( aActive == CHidTransport::ESuspend)
       
   482                 {
       
   483                 item->Driver()->Stop();
       
   484                 retVal = KErrNone;
       
   485                 }            
       
   486             }   
       
   487          driverIter++;
       
   488          item = driverIter;
       
   489          }
       
   490     TRACE_FUNC_EXIT
       
   491     return retVal;
       
   492     }
       
   493 
       
   494 // ---------------------------------------------------------------------------
       
   495 // CommandResult()
       
   496 // ---------------------------------------------------------------------------
       
   497 //
       
   498 void CGenericHid::CommandResult(TInt aConnectionId, TInt aCmdAck)
       
   499     {
       
   500     // Get the driver handling this connection    
       
   501     CConnectionInfo* conninfo = SeekConnectionInfo( aConnectionId );
       
   502     if ( conninfo )
       
   503         {
       
   504         CHidDriver*  hiddriver = conninfo->ReturnLastCommandHandler();
       
   505         if (hiddriver)
       
   506             {
       
   507             hiddriver->CommandResult(aCmdAck);
       
   508             }
       
   509         }    
       
   510     }
       
   511 
       
   512 // ---------------------------------------------------------------------------
       
   513 // SeekConnectionInfo()
       
   514 // ---------------------------------------------------------------------------
       
   515 //
       
   516 CConnectionInfo* CGenericHid::SeekConnectionInfo(TInt aConnectionId)
       
   517     {
       
   518     TRACE_FUNC    
       
   519     CConnectionInfo* conninfo = NULL;
       
   520     TInt count = iConnectionInfo.Count();
       
   521     TRACE_INFO((_L("[HID]\tCGenericHid::SeekConnectionInfo count %d"), count));   
       
   522     for (TInt i = 0 ; i < count; i++)
       
   523         {
       
   524         conninfo = iConnectionInfo[i];
       
   525         TRACE_INFO((_L("[HID]\tCGenericHid::SeekConnectionInfo connection info check %d %d"),aConnectionId, conninfo->ConnectionID()));    
       
   526         if ( conninfo->ConnectionID() == aConnectionId)                
       
   527             {
       
   528             TRACE_INFO(_L("[HID]\tCGenericHid::SeekConnectionInfo connection info found"));    
       
   529             return conninfo;
       
   530             }
       
   531         }
       
   532     return NULL;
       
   533     }