connectivitymodules/SeCon/clients/pcconn/src/sconpcconnclient.cpp
branchRCL_3
changeset 42 0aa8cc770c8a
parent 40 453dfc402455
child 43 4a793f564d72
equal deleted inserted replaced
40:453dfc402455 42:0aa8cc770c8a
     1 /*
       
     2 * Copyright (c) 2005-2009 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:  PC Connectivity client
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // -----------------------------------------------------------------------------
       
    21 // Includes
       
    22 // -----------------------------------------------------------------------------
       
    23 #include <e32base.h>
       
    24 #include <s32mem.h>
       
    25 
       
    26 // Client - server 
       
    27 #include "sconpcconnclient.h"
       
    28 #include "sconpcconnclientserver.h"
       
    29 #include "debug.h"
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // RSConPCConnSession::RSConPCConnSession()
       
    33 // Default constructor
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C RSConPCConnSession::RSConPCConnSession()
       
    37     {
       
    38     TRACE_FUNC;
       
    39     }
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // RSConPCConnSession::~RSConPCConnSession()
       
    43 // Default destructor
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C RSConPCConnSession::~RSConPCConnSession()
       
    47     {
       
    48     TRACE_FUNC;
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // RSConPCConnSession::Connect()
       
    53 // Creates a chunk and server, starts the server connection
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C TInt RSConPCConnSession::Connect()
       
    57     {
       
    58     TRACE_FUNC_ENTRY;
       
    59  	
       
    60     TInt ret ( KErrNone );
       
    61 
       
    62     ret = CreateSession(KSConPCConnServerName,Version());
       
    63     
       
    64     if ( ret == KErrNotFound || ret == KErrServerTerminated )
       
    65     	{
       
    66     	ret = LaunchServer();
       
    67     	
       
    68     	if ( ret == KErrNone || ret == KErrAlreadyExists )
       
    69     		{
       
    70     		ret = CreateSession( KSConPCConnServerName, Version() );
       
    71     		}
       
    72     	}
       
    73     	
       
    74     if ( ret == KErrNone )
       
    75     	{
       
    76     	ret = CreateAndSendChunkHandle();
       
    77     	if ( ret != KErrNone )
       
    78     	    {
       
    79     	    LOGGER_WRITE("CreateAndSendChunkHandle failed, close session");
       
    80     	    Close();
       
    81     	    }
       
    82     	}
       
    83     
       
    84     LOGGER_WRITE_1( "RSConPCConnSession::Connect() : returned %d", ret );
       
    85     return ret; 
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // RSConPCConnSession::Close()
       
    90 // Closes the server connection
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 EXPORT_C void RSConPCConnSession::Close()
       
    94     {
       
    95     TRACE_FUNC_ENTRY;
       
    96     iChunk.Close();
       
    97     RSessionBase::Close();
       
    98     TRACE_FUNC_EXIT;
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // RSConPCConnSession::Version()
       
   103 // Returns the version of the client
       
   104 // -----------------------------------------------------------------------------
       
   105 //	
       
   106 TVersion RSConPCConnSession::Version(void) const
       
   107     {
       
   108     return (TVersion(KSConPCConnServerVersionMajor, 
       
   109                                KSConPCConnServerVersionMinor, 
       
   110                                KSConPCConnServerVersionBuild));
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // RSConPCConnSession::Version()
       
   115 // Forwards a PUT -message to the server
       
   116 // -----------------------------------------------------------------------------
       
   117 //	
       
   118 EXPORT_C TInt RSConPCConnSession::PutPacketL( 
       
   119     const TDesC& aNameHeader, const TDesC8& aTypeHeader, CBufFlat*& aBuffer )
       
   120     {
       
   121     TRACE_FUNC_ENTRY;
       
   122 
       
   123     if ( !aBuffer )
       
   124         {
       
   125         return KErrArgument;
       
   126         }
       
   127 
       
   128     if ((aBuffer->Size() + aTypeHeader.Size() + 
       
   129          aNameHeader.Size()) > KSConPCConnChunkMaxSize)
       
   130         {
       
   131         LOGGER_WRITE( "RSConPCConnSession::PutPacketL() :\
       
   132          WBXML document too big" );
       
   133         return KErrTooBig;
       
   134         }
       
   135 	
       
   136     TInt ret (KErrNone);
       
   137     iChunk.Adjust( KSConPCConnChunkMaxSize );
       
   138     RMemWriteStream buf( iChunk.Base(), iChunk.Size() );
       
   139 	
       
   140     buf.WriteInt32L( aNameHeader.Size() );
       
   141     buf.WriteL( aNameHeader );
       
   142 
       
   143     buf.WriteInt32L( aTypeHeader.Size() );
       
   144     buf.WriteL( aTypeHeader );
       
   145 	
       
   146     // WBXML document	
       
   147     buf.WriteInt32L( aBuffer->Size() );
       
   148     buf.WriteL( aBuffer->Ptr(0) );
       
   149     buf.CommitL();
       
   150     buf.Close();
       
   151     
       
   152     TIpcArgs args;
       
   153     ret = SendReceive ( EPutMessage, args );
       
   154 
       
   155     LOGGER_WRITE_1( "RSConPCConnSession::PutPacketL() : returned %d", ret );
       
   156     return ret;
       
   157     }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // RSConPCConnSession::Version()
       
   161 // Forwards a GET -message to the server
       
   162 // -----------------------------------------------------------------------------
       
   163 //	
       
   164 EXPORT_C TInt RSConPCConnSession::GetPacketL( 
       
   165     const TDesC& aNameHeader, const TDesC8& aTypeHeader, CBufFlat*& aBuffer )
       
   166     {
       
   167     TRACE_FUNC_ENTRY;
       
   168 
       
   169     if ( !aBuffer )
       
   170         {
       
   171         return KErrArgument;
       
   172         }
       
   173 
       
   174     TInt ret (KErrNone);
       
   175 	
       
   176     iChunk.Adjust( KSConPCConnChunkMaxSize);
       
   177     RMemWriteStream writeBuf( iChunk.Base(), iChunk.Size() );
       
   178     writeBuf.WriteInt32L( aNameHeader.Size() );
       
   179     writeBuf.WriteL( aNameHeader );
       
   180     
       
   181     writeBuf.WriteInt32L( aTypeHeader.Size() );
       
   182     writeBuf.WriteL( aTypeHeader );
       
   183 
       
   184     writeBuf.CommitL();
       
   185     writeBuf.Close();
       
   186     
       
   187     TIpcArgs args;
       
   188     ret = SendReceive ( EGetMessage, args );
       
   189 
       
   190     if ( ret != KErrNone) 
       
   191         {
       
   192         LOGGER_WRITE_1( "RSConPCConnSession::GetPacketL() :\
       
   193          Send Receive failed with code %d", ret );
       
   194         return ret;
       
   195         };
       
   196 	
       
   197     // copy data from the chunk
       
   198     RMemReadStream readBuf( iChunk.Base(), iChunk.Size() );
       
   199     TInt32 length ( 0 );
       
   200 
       
   201     aBuffer->Reset();
       
   202     length = readBuf.ReadInt32L();
       
   203 	
       
   204     HBufC8* data = HBufC8::NewLC( length );
       
   205     TPtr8 dataPtr = data->Des();
       
   206 	
       
   207     readBuf.ReadL( dataPtr, length );
       
   208     aBuffer->ExpandL( 0, length );
       
   209     aBuffer->Write ( 0, dataPtr );
       
   210 	
       
   211     readBuf.Close();
       
   212 	
       
   213     CleanupStack::PopAndDestroy(); // data;
       
   214 
       
   215     LOGGER_WRITE_1( "RSConPCConnSession::GetPacketL()\
       
   216      : returned %d", ret );
       
   217     return ret;
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // RSConPCConnSession::Version()
       
   222 // Forwards a Reset -message to the server
       
   223 // -----------------------------------------------------------------------------
       
   224 //	
       
   225 EXPORT_C TInt RSConPCConnSession::ResetServer()
       
   226     {
       
   227     TRACE_FUNC_ENTRY;
       
   228     TInt ret ( KErrNone );
       
   229     TIpcArgs args;
       
   230     ret = SendReceive ( EResetMessage, args );
       
   231     LOGGER_WRITE_1( "RSConPCConnSession::ResetServerL(): ret %d", ret );
       
   232     return ret;
       
   233     }
       
   234     
       
   235 // -----------------------------------------------------------------------------
       
   236 // RSConPCConnSession::LaunchServer()
       
   237 // Launches the server
       
   238 // -----------------------------------------------------------------------------
       
   239 //	
       
   240 TInt RSConPCConnSession::LaunchServer()
       
   241 	{
       
   242 	TRACE_FUNC_ENTRY;
       
   243 	TInt ret( KErrNone );
       
   244     
       
   245     // Create server
       
   246     RProcess p;
       
   247     ret = p.Create(KSConPCConnServerExe, KNullDesC);
       
   248     
       
   249     if ( ret != KErrNone )
       
   250         {
       
   251         // Loading failed.
       
   252         LOGGER_WRITE_1( "RProcess::Create failed, err: %d", ret );
       
   253         return ret;
       
   254         }
       
   255     
       
   256     
       
   257     TRequestStatus status;
       
   258     p.Rendezvous( status );
       
   259     
       
   260     if ( status != KRequestPending )
       
   261         {
       
   262         p.Kill(0);		// abort startup
       
   263         p.Close();
       
   264         LOGGER_WRITE("Abort startup, return KErrGeneral");
       
   265         return KErrGeneral;   
       
   266         }
       
   267     else
       
   268         {
       
   269         p.Resume();	// logon OK - start the server
       
   270         }
       
   271     
       
   272     User::WaitForRequest(status);
       
   273     
       
   274     p.Close();
       
   275     
       
   276 	LOGGER_WRITE_1( "RSConPCConnSession::LaunchServerL(): returned %d", status.Int() );
       
   277     return status.Int();
       
   278 	}
       
   279 	
       
   280 // -----------------------------------------------------------------------------
       
   281 // RSConPCConnSession::CreateAndSendChunkHandle()
       
   282 // Creates a chunk and sends a handle to server
       
   283 // -----------------------------------------------------------------------------
       
   284 //	
       
   285 TInt RSConPCConnSession::CreateAndSendChunkHandle()
       
   286 	{
       
   287 	TRACE_FUNC_ENTRY;
       
   288 	TInt err = iChunk.CreateGlobal( KNullDesC, 
       
   289                                     KSConPCConnChunkSize, 
       
   290                                     KSConPCConnChunkMaxSize );
       
   291     if ( err != KErrNone )
       
   292         {
       
   293         LOGGER_WRITE_1("iChunk.CreateGlobal failed, err: %d", err);
       
   294         return err;
       
   295         }                           
       
   296     TIpcArgs args;
       
   297  	args.Set( 0, iChunk );
       
   298  	err = SendReceive( EChunkMessage, args );
       
   299     
       
   300     LOGGER_WRITE_1("RSConPCConnSession::CreateAndSendChunkHandle : returned: %d", err);
       
   301     return err;
       
   302 	}
       
   303 	
       
   304 // End of file