bluetoothengine/bthid/common/src/genericclient.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 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:  This is the implementation of application class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32math.h>
       
    20 
       
    21 #include "genericclient.h"
       
    22 #include "genericserver.h"
       
    23 
       
    24 // ----------------------------------------------------------------------
       
    25 
       
    26 TInt RGenericSession::StartSession(const TDesC& aServer,
       
    27         const TVersion& aVersion, const TDesC& aFilename,
       
    28         const TUidType& aUid, TInt aAsyncMessageSlots)
       
    29     {
       
    30     // The server may be running already, so try connecting immediately
       
    31     TInt error = CreateSession(aServer, aVersion, aAsyncMessageSlots);
       
    32 
       
    33     if (KErrNone != error)
       
    34         {
       
    35         // Failed to connect so create a new server instance and try again.
       
    36         // If creating the server fails, it may be due to another client
       
    37         // creating a server in the meantime, so ignore any error here.
       
    38         DBG(RDebug::Print(_L("RGenericSession::StartSession(): calling CreateServer()...")));
       
    39         CreateServer(aServer, aFilename, aUid);
       
    40         DBG(RDebug::Print(_L("RGenericSession::StartSession(): calling CreateSession()...")));
       
    41         error = CreateSession(aServer, aVersion, aAsyncMessageSlots);
       
    42         }
       
    43 
       
    44     DBG(RDebug::Print(_L("RGenericSession::StartSession(): returning %d"),error));
       
    45 
       
    46     return error;
       
    47     }
       
    48 
       
    49 TInt RGenericSession::CreateServer(const TDesC& /*aServer*/,
       
    50         const TDesC& aFilename, const TUidType& aUid)
       
    51     {
       
    52     TInt result;
       
    53     TRequestStatus startup = KRequestPending;
       
    54     TStartupRequest startupRequest(&startup, RThread().Id());
       
    55 
       
    56     RProcess server;
       
    57     TPtrC startupPtr(reinterpret_cast<TText*> (&startupRequest),
       
    58             KStartupTextLength);
       
    59 
       
    60     DBG(RDebug::Print(_L("RGenericSession::CreateServer(): calling server.Create()")));
       
    61     result = server.Create(aFilename, startupPtr, aUid);
       
    62 
       
    63     DBG(RDebug::Print(_L("RGenericSession::CreateServer(): server.Create() result: %d"),result));
       
    64 
       
    65     if (KErrNone == result)
       
    66         {
       
    67         TRequestStatus stat;
       
    68         server.Rendezvous(stat);
       
    69         if (stat != KRequestPending)
       
    70             {
       
    71             DBG(RDebug::Print(_L("RGenericSession::CreateServer(): __SECURE_API__ ClientStart, stat != KRequestPending, Kill process ")));
       
    72             server.Kill(0); // abort startup
       
    73             }
       
    74         else
       
    75             {
       
    76             DBG(RDebug::Print(_L("RGenericSession::CreateServer(): __SECURE_API__ ClientStart, logon OK - start the server ")));
       
    77             server.Resume(); // logon OK - start the server
       
    78             }
       
    79         User::WaitForRequest(stat); // wait for start or death
       
    80         // we can't use the 'exit reason' if the server panicked as this
       
    81         // is the panic 'reason' and may be '0' which cannot be distinguished
       
    82         // from KErrNone
       
    83         result = (server.ExitType() == EExitPanic) ? KErrGeneral : stat.Int();
       
    84         DBG(RDebug::Print(_L("RGenericSession::CreateServer(): __SECURE_API__ ClientStart, server exit result: %d"),result));
       
    85 
       
    86         server.Close();
       
    87         }
       
    88 
       
    89     return result;
       
    90     }