localconnectivityservice/dun/plugins/src/ir/DunIrPlugin.cpp
branchRCL_3
changeset 19 0aa8cc770c8a
equal deleted inserted replaced
18:453dfc402455 19:0aa8cc770c8a
       
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  DUN Infrared plugin
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "DunIrPlugin.h"
       
    20 #include "DunUtils.h"
       
    21 #include "DunDebug.h"
       
    22 
       
    23 _LIT( KIrdaCsy,       "IRCOMM"    );
       
    24 _LIT( KIrdaCsy0,      "IRCOMM::0" );
       
    25 _LIT( KIrChannelName, "DUNIR::0"  );
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CDunIrPlugin::CDunIrPlugin
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CDunIrPlugin::CDunIrPlugin() :
       
    34     iServer( NULL ),
       
    35     iTransporter( NULL )
       
    36     {
       
    37     }
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // Destructor.
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 CDunIrPlugin::~CDunIrPlugin()
       
    44     {
       
    45     FTRACE(FPrint( _L( "CDunIrPlugin::~CDunIrPlugin()" ) ));
       
    46     Uninitialize();
       
    47     FTRACE(FPrint( _L( "CDunIrPlugin::~CDunIrPlugin() complete" ) ));
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // Gets state of this plugin
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 TDunPluginState CDunIrPlugin::PluginState()
       
    55     {
       
    56     return iServer->GetPluginStateByUid( KDunIrPluginUid );
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // Constructs a listener object for this plugin
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 TInt CDunIrPlugin::ConstructListener()
       
    64     {
       
    65     FTRACE(FPrint(_L("CDunIrPlugin::ConstructListenerL()")));
       
    66     if ( PluginState() != EDunStateLoaded )
       
    67         {
       
    68         FTRACE(FPrint(_L("CDunIrPlugin::ConstructListenerL() (not ready) complete")));
       
    69         return KErrNotReady;
       
    70         }
       
    71     ReportStateChangeUp( EDunStateTryListen );
       
    72     TInt retTemp = InitPort();
       
    73     if ( retTemp != KErrNone )
       
    74         {
       
    75         FTRACE(FPrint(_L("CDunIrPlugin::ConstructListenerL() (ERROR) complete")));
       
    76         return retTemp;
       
    77         }
       
    78     ReportStateChangeUp( EDunStateListening );
       
    79     ReportStateChangeUp( EDunStateTryChannel );
       
    80     TRAPD( retTrap, AllocateChannelL() );
       
    81     if ( retTrap != KErrNone )
       
    82         {
       
    83         iTransporter->FreeChannel( &iIrPort );
       
    84         FTRACE(FPrint(_L("CDunIrPlugin::ConstructListenerL() (trapped!) complete")));
       
    85         return retTrap;
       
    86         }
       
    87     ReportStateChangeUp( EDunStateChanneled );
       
    88     FTRACE(FPrint(_L("CDunIrPlugin::ConstructListenerL() complete")));
       
    89     return KErrNone;
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // Sets new state
       
    94 // New state must be one more than the old state
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 TInt CDunIrPlugin::ReportStateChangeUp( TDunPluginState aPluginState )
       
    98     {
       
    99     FTRACE(FPrint(_L("CDunIrPlugin::ReportStateChangeUp()")));
       
   100     TInt retTemp = iServer->NotifyPluginStateChangeUp( aPluginState,
       
   101                                                        KDunIrPluginUid );
       
   102     if ( retTemp != KErrNone )
       
   103         {
       
   104         FTRACE(FPrint(_L("CDunIrPlugin::ReportStateChangeUp() (ERROR) complete")));
       
   105         return retTemp;
       
   106         }
       
   107     FTRACE(FPrint(_L("CDunIrPlugin::ReportStateChangeUp() complete")));
       
   108     return KErrNone;
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Sets new state
       
   113 // New state must be one less than the old state
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 TInt CDunIrPlugin::ReportStateChangeDown( TDunPluginState aPluginState )
       
   117     {
       
   118     FTRACE(FPrint(_L("CDunIrPlugin::ReportStateChangeDown()")));
       
   119     TInt retTemp = iServer->NotifyPluginStateChangeDown( aPluginState,
       
   120                                                          KDunIrPluginUid );
       
   121     if ( retTemp != KErrNone )
       
   122         {
       
   123         FTRACE(FPrint(_L("CDunIrPlugin::ReportStateChangeDown() (ERROR) complete")));
       
   124         return retTemp;
       
   125         }
       
   126     FTRACE(FPrint(_L("CDunIrPlugin::ReportStateChangeDown() complete")));
       
   127     return KErrNone;
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // Initializes one infrared port with role DCE
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 TInt CDunIrPlugin::InitPort()
       
   135     {
       
   136     FTRACE(FPrint( _L( "CDunIrPlugin::InitPort()" ) ));
       
   137     TInt retTemp;
       
   138     retTemp = CDunUtils::ConnectCommsServer( iCommServer );
       
   139     if ( retTemp != KErrNone )
       
   140         {
       
   141         FTRACE(FPrint( _L( "CDunIrPlugin::InitPort() (ERROR) complete" ) ));
       
   142         return retTemp;
       
   143         }
       
   144     retTemp = iCommServer.LoadCommModule( KIrdaCsy );
       
   145     if ( retTemp != KErrNone )
       
   146         {
       
   147         FTRACE(FPrint(_L("CDunIrPlugin::InitPort() (load module) failed!") ));
       
   148         return retTemp;
       
   149         }
       
   150     if ( iIrPort.SubSessionHandle() )
       
   151         {
       
   152         FTRACE(FPrint(_L("CDunIrPlugin::InitPort (already exists!) complete") ));
       
   153         return KErrAlreadyExists;
       
   154         }
       
   155     retTemp = iIrPort.Open( iCommServer,
       
   156                             KIrdaCsy0,
       
   157                             ECommExclusive,
       
   158                             ECommRoleDCE );
       
   159     if ( retTemp != KErrNone )
       
   160         {
       
   161         FTRACE(FPrint(_L("CDunIrPlugin::InitPort() (open) failed!") ));
       
   162         return retTemp;
       
   163         }
       
   164     return KErrNone;
       
   165     }
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // Allocates a channel
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 void CDunIrPlugin::AllocateChannelL()
       
   172     {
       
   173     FTRACE(FPrint(_L("CDunIrPlugin::AllocateChannelL()")));
       
   174     HBufC8* channelName = HBufC8::NewMaxLC( KIrChannelName().Length() );
       
   175     TPtr8 channelNamePtr = channelName->Des();
       
   176     channelNamePtr.Copy( KIrChannelName );
       
   177     iTransporter->AllocateChannelL( &iIrPort,
       
   178                                     KDunIrPluginUid,
       
   179                                     channelNamePtr,
       
   180                                     ETrue );
       
   181     iTransporter->AddConnMonCallbackL( &iIrPort,
       
   182                                        this,
       
   183                                        EDunReaderUpstream,
       
   184                                        EFalse );
       
   185     iTransporter->AddConnMonCallbackL( &iIrPort,
       
   186                                        this,
       
   187                                        EDunWriterUpstream,
       
   188                                        EFalse );
       
   189     iTransporter->AddConnMonCallbackL( &iIrPort,
       
   190                                        this,
       
   191                                        EDunReaderDownstream,
       
   192                                        EFalse );
       
   193     iTransporter->AddConnMonCallbackL( &iIrPort,
       
   194                                        this,
       
   195                                        EDunWriterDownstream,
       
   196                                        EFalse );
       
   197     iTransporter->IssueTransferRequestsL( &iIrPort );
       
   198     CleanupStack::PopAndDestroy( channelName );
       
   199     FTRACE(FPrint(_L("CDunIrPlugin::AllocateChannelL() complete")));
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // Uninitializes this plugin
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 TInt CDunIrPlugin::Uninitialize()
       
   207     {
       
   208     FTRACE(FPrint(_L("CDunIrPlugin::Uninitialize()" )));
       
   209     ReportStateChangeDown( EDunStateTryUninitialize );
       
   210     if ( iIrPort.SubSessionHandle() )
       
   211         {
       
   212         iTransporter->FreeChannel( &iIrPort );
       
   213         iIrPort.SetSignals( 0, KSignalDCEOutputs );
       
   214         iIrPort.Close();
       
   215         }
       
   216     if ( iCommServer.Handle() )
       
   217         {
       
   218         iCommServer.UnloadCommModule( KIrdaCsy );
       
   219         iCommServer.Close();
       
   220         }
       
   221     ReportStateChangeUp( EDunStateUninitialized );
       
   222     ReportStateChangeUp( EDunStateTryLoad );
       
   223     ReportStateChangeUp( EDunStateLoaded );
       
   224     FTRACE(FPrint(_L("CDunIrPlugin::Uninitialize() complete" )));
       
   225     return KErrNone;
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // From class MDunLocalMediaPlugin.
       
   230 // CDunIrPlugin::ConstructL
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 void CDunIrPlugin::ConstructL( MDunServerCallback* aServer,
       
   234                                CDunTransporter* aTransporter )
       
   235     {
       
   236     FTRACE(FPrint(_L("CDunIrPlugin::ConstructL()")));
       
   237     if ( !aServer || !aTransporter )
       
   238         {
       
   239         FTRACE(FPrint(_L("CDunIrPlugin::ConstructL() not initialized!")));
       
   240         User::Leave( KErrGeneral );
       
   241         }
       
   242     iServer = aServer;
       
   243     iTransporter = aTransporter;
       
   244     FTRACE(FPrint(_L("CDunIrPlugin::ConstructL() complete")));
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // From class MDunLocalMediaPlugin.
       
   249 // Gets called when server changes a plugin's state
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 TInt CDunIrPlugin::NotifyServerStateChange( TDunPluginState aPluginState )
       
   253     {
       
   254     FTRACE(FPrint(_L("CDunIrPlugin::NotifyServerStateChange()")));
       
   255     TInt retTemp;
       
   256     switch ( aPluginState )
       
   257         {
       
   258         case EDunStateTryListen:
       
   259             if ( PluginState() != EDunStateLoaded )
       
   260                 {
       
   261                 FTRACE(FPrint(_L("CDunIrPlugin::NotifyServerStateChange() (not ready) complete")));
       
   262                 return KErrNotReady;
       
   263                 }
       
   264             // Change to listening mode
       
   265             retTemp = ConstructListener();
       
   266             if ( retTemp != KErrNone )
       
   267                 {
       
   268                 FTRACE(FPrint(_L("CDunIrPlugin::NotifyServerStateChange() (ERROR) complete (%d)"), retTemp));
       
   269                 return retTemp;
       
   270                 }
       
   271             break;
       
   272         case EDunStateTryUninitialize:
       
   273             if ( PluginState() == EDunStateUninitialized )
       
   274                 {
       
   275                 FTRACE(FPrint(_L("CDunIrPlugin::NotifyServerStateChange() (not ready) complete")));
       
   276                 return KErrNotReady;
       
   277                 }
       
   278             // Uninitialize
       
   279             retTemp = Uninitialize();
       
   280             if ( retTemp != KErrNone )
       
   281                 {
       
   282                 FTRACE(FPrint(_L("CDunIrPlugin::NotifyServerStateChange() (ERROR) complete (%d)"), retTemp));
       
   283                 return retTemp;
       
   284                 }
       
   285             break;
       
   286         default:
       
   287             FTRACE(FPrint(_L("CDunIrPlugin::NotifyServerStateChange() (unknown state) complete")));
       
   288             return KErrNotSupported;
       
   289         }
       
   290     FTRACE(FPrint(_L("CDunIrPlugin::NotifyServerStateChange() complete")));
       
   291     return KErrNone;
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // From class MDunLocalMediaPlugin.
       
   296 // Gets called when server needs to know the active connection
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 TConnId CDunIrPlugin::ActiveConnection()
       
   300     {
       
   301     FTRACE(FPrint(_L("CDunIrPlugin::ActiveConnection()")));
       
   302     FTRACE(FPrint(_L("CDunIrPlugin::ActiveConnection() (not found) complete")));
       
   303     return NULL;
       
   304     }
       
   305 
       
   306 // ---------------------------------------------------------------------------
       
   307 // From class MDunConnMon.
       
   308 // Gets called when line status changes or when any type of error is detected
       
   309 // ---------------------------------------------------------------------------
       
   310 //
       
   311 void CDunIrPlugin::NotifyProgressChangeL(
       
   312     TConnId aConnId,
       
   313     TDunConnectionReason /*aConnReason*/ )
       
   314     {
       
   315     FTRACE(FPrint( _L( "CDunIrPlugin::NotifyProgressChangeL()" ) ));
       
   316     RComm* irConn = static_cast<RComm*>( aConnId );
       
   317     if ( &iIrPort != irConn )
       
   318         {
       
   319         FTRACE(FPrint( _L( "CDunIrPlugin::NotifyProgressChangeL() (not found) complete")) );
       
   320         User::Leave( KErrNotFound );
       
   321         }
       
   322     // Now indications are down indications from local media side
       
   323     FTRACE(FPrint( _L( "CDunIrPlugin::NotifyProgressChangeL() restart plugin" ) ));
       
   324     iServer->NotifyPluginRestart( KDunIrPluginUid );
       
   325     FTRACE(FPrint( _L( "CDunIrPlugin::NotifyProgressChangeL() complete")) );
       
   326     }
       
   327 
       
   328 // ======== GLOBAL FUNCTIONS ========
       
   329 
       
   330 // ---------------------------------------------------------------------------
       
   331 // NewLocalPluginL implements factory construction for
       
   332 // the class CDunIrPlugin.
       
   333 // The function is exported at ordinal 1.
       
   334 // Returns: Pointer: The new instance of CDunIrPlugin
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 EXPORT_C MDunLocalMediaPlugin* NewLocalPluginL()
       
   338     {
       
   339     return new (ELeave) CDunIrPlugin;
       
   340     }