sl@0: // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of the License "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: // rusbhostmsdevice.cpp
sl@0: //
sl@0: //
sl@0: 
sl@0: 
sl@0: 
sl@0: /**
sl@0:  @file
sl@0:  @internalTechnology
sl@0: */
sl@0: 
sl@0: 
sl@0: #include <e32std.h>
sl@0: #include <f32file.h>
sl@0: 
sl@0: #include "msmanclientserver.h"
sl@0: #include "rusbotgsession.h"
sl@0: #include "tmslog.h"
sl@0: #include "debug.h"
sl@0: 
sl@0: 
sl@0: TVersion RUsbOtgSession::Version() const
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     return(TVersion(KUsbOtgSrvMajorVersionNumber,
sl@0:                     KUsbOtgSrvMinorVersionNumber,
sl@0:                     KUsbOtgSrvBuildVersionNumber));
sl@0:     }
sl@0: 
sl@0: 
sl@0: EXPORT_C RUsbOtgSession::RUsbOtgSession()
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     }
sl@0: 
sl@0: 
sl@0: EXPORT_C RUsbOtgSession::RUsbOtgSession(TInt /* aParam */)
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     }
sl@0: 
sl@0: EXPORT_C TInt RUsbOtgSession::Connect()
sl@0:     {
sl@0:     __MSFNSLOG
sl@0: 
sl@0:     TInt retry = 2;
sl@0:     for (;;)
sl@0:         {
sl@0:         TInt r = CreateSession(KUsbOtgServerName, Version());
sl@0:         if ((r != KErrNotFound) && (r != KErrServerTerminated))
sl@0:             {
sl@0:             return r;
sl@0:             }
sl@0:         if (--retry == 0)
sl@0:             {
sl@0:             return r;
sl@0:             }
sl@0: 
sl@0:         r = StartServer();
sl@0:         if ((r != KErrNone) && (r != KErrAlreadyExists))
sl@0:             {
sl@0:             return r;
sl@0:             }
sl@0:         }
sl@0:     }
sl@0: 
sl@0: 
sl@0: TInt RUsbOtgSession::StartServer()
sl@0:     {
sl@0:     __MSFNSLOG
sl@0: 
sl@0:     const TUidType serverUid(KNullUid, KNullUid, KUsbOtgServerUid3);
sl@0: 
sl@0:     // Create the server process
sl@0:     RProcess server;
sl@0:     TInt r = server.Create(KUsbOtgServerName, KNullDesC, serverUid);
sl@0:     if (r != KErrNone)
sl@0:         {
sl@0:         return r;
sl@0:         }
sl@0: 
sl@0:     // Create the rendezvous request with the server process
sl@0:     TRequestStatus status;
sl@0:     server.Rendezvous(status);
sl@0:     if (status != KRequestPending)
sl@0:         {
sl@0:         User::WaitForRequest(status);
sl@0:         server.Kill(0);    // If the outstanding request is not pending then kill the server
sl@0:         server.Close();
sl@0:         return status.Int();
sl@0:         }
sl@0: 
sl@0: 	server.SetPriority(EPriorityHigh);
sl@0:     server.Resume(); // start the server
sl@0: 
sl@0:     // Test whether the process has ended and if it has ended, return how it ended.
sl@0:     User::WaitForRequest(status);
sl@0: 
sl@0:     if (status == KRequestPending)
sl@0:         {
sl@0:         server.Close();
sl@0:         return status.Int();
sl@0:         }
sl@0: 
sl@0:     server.Close();
sl@0:     return KErrNone;
sl@0:     }
sl@0: 
sl@0: 
sl@0: EXPORT_C TBool RUsbOtgSession::DeviceInserted()
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     TPckgBuf<TBool> pckg;
sl@0:     TIpcArgs args(&pckg);
sl@0: 
sl@0:     SendReceive(EUsbOtgDeviceInserted, args);
sl@0:     TBool res = pckg();
sl@0:     return res;
sl@0:     }
sl@0: 
sl@0: 
sl@0: EXPORT_C void RUsbOtgSession::NotifyChange(TBool& /* aChanged */, TRequestStatus& aStatus)
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     TPckgBuf<TBool> pckg;
sl@0:     TIpcArgs args(&pckg);
sl@0: 
sl@0:     SendReceive(EUsbOtgNotifyChange, args, aStatus);
sl@0:     }
sl@0: 
sl@0: 
sl@0: EXPORT_C TInt RUsbOtgSession::NotifyChangeCancel()
sl@0:     {
sl@0:     __MSFNSLOG
sl@0:     return SendReceive(EUsbOtgNotifyChangeCancel);
sl@0:     }
sl@0: 
sl@0: 
sl@0: EXPORT_C TInt RUsbOtgSession::BusDrop()
sl@0: 	{
sl@0:     __MSFNSLOG
sl@0: 	return SendReceive(EUsbOtgBusDrop);
sl@0: 	}