hti/HtiCommPlugins/HtiBtCommPlugin/HtiBtCommServer/src/HtiBtCommInterface.cpp
branchRCL_3
changeset 59 8ad140f3dd41
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 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: Implementation of RHtiBtCommInterface class.
       
    15 *              Symbian side component uses this interface to receive and
       
    16 *              send HTI data to HtiBtCommServer.
       
    17 *              HtiBtCommServer acts as proxy to send and receive data
       
    18 *              to and from PC.
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 #include "HtiBtClientServerCommon.h"
       
    25 #include "HtiBtCommInterface.h"
       
    26 
       
    27 #include <e32uid.h>
       
    28 #include <HtiLogging.h>
       
    29 
       
    30 // CONSTANTS
       
    31 const TUint KBtCommServerDefaultMessageSlots = 4; // Read, write, readCancel, writeCancel
       
    32 
       
    33 // Constants
       
    34 
       
    35 /*---------------------------------------------------------------------------*/
       
    36 EXPORT_C RHtiBtCommInterface::RHtiBtCommInterface()
       
    37     {
       
    38     HTI_LOG_TEXT( "RHtiBtCommInterface::RHtiBtCommInterface()" );
       
    39     }
       
    40 
       
    41 /*---------------------------------------------------------------------------*/
       
    42 EXPORT_C TInt RHtiBtCommInterface::Connect( TDesC8& aDeviceNameOrAddress,
       
    43                                             TInt aPort )
       
    44     {
       
    45     HTI_LOG_TEXT( "RHtiBtCommInterface::Connect()" );
       
    46 
       
    47     TInt error = StartThread(); // see HtiBtClientServerCommon.h
       
    48     if (error == KErrNone)
       
    49         {
       
    50         error = CreateSession( KBtCommServerName,
       
    51                                Version(),
       
    52                                KBtCommServerDefaultMessageSlots );
       
    53         }
       
    54     if ( error )
       
    55         return error;
       
    56 
       
    57     return ConnectBt( aDeviceNameOrAddress, aPort );
       
    58     }
       
    59 
       
    60 /*---------------------------------------------------------------------------*/
       
    61 EXPORT_C void RHtiBtCommInterface::Close()
       
    62     {
       
    63     HTI_LOG_FUNC_IN( "RHtiBtCommInterface::Close" );
       
    64 
       
    65     // Use thread finder to find the server thread
       
    66     TFullName threadName;
       
    67     TFullName matchPattern;
       
    68     matchPattern.Append( _L( "*" ) );
       
    69     matchPattern.Append( KBtCommServerName );
       
    70     matchPattern.Append( _L( "*" ) );
       
    71 
       
    72     TFindThread threadFinder;
       
    73     threadFinder.Find( matchPattern );
       
    74     TInt err = threadFinder.Next( threadName );
       
    75     if ( err )
       
    76         {
       
    77         HTI_LOG_FORMAT( "RHtiBtCommInterface::Close: Failed to find server thread: %d", err );
       
    78         User::Panic( _L( "BtIf Close, server thread not found" ), err );
       
    79         }
       
    80 
       
    81     HTI_LOG_TEXT( "RHtiBtCommInterface::Close: Found server thread:" );
       
    82     HTI_LOG_DES( threadName );
       
    83 
       
    84     RThread thread;
       
    85     err = thread.Open( threadName );
       
    86     if ( err )
       
    87         {
       
    88         HTI_LOG_FORMAT( "RHtiBtCommInterface::Close: Failed to open server thread: %d", err );
       
    89         User::Panic( _L( "BtIf Close, error opening server thread" ), err );
       
    90         }
       
    91     else
       
    92         {
       
    93         // initiate server shutdown
       
    94         RSessionBase::Close();
       
    95 
       
    96         // For clean server stop, wait for its death
       
    97         HTI_LOG_TEXT( "RHtiBtCommInterface::Close: Waiting for server thread to die..." );
       
    98         TRequestStatus status;
       
    99         thread.Logon( status );
       
   100         User::WaitForRequest( status );
       
   101         HTI_LOG_TEXT( "RHtiBtCommInterface::Close: Server thread dead." );
       
   102         }
       
   103 
       
   104     thread.Close();
       
   105     HTI_LOG_FUNC_OUT( "RHtiBtCommInterface::Close" );
       
   106     }
       
   107 
       
   108 /*---------------------------------------------------------------------------*/
       
   109 EXPORT_C TVersion RHtiBtCommInterface::Version(void) const
       
   110     {
       
   111     HTI_LOG_TEXT( "RHtiBtCommInterface::Connect()" );
       
   112     return ( TVersion( KBtCommServerMajorVersionNumber,
       
   113                        KBtCommServerMinorVersionNumber,
       
   114                        KBtCommServerBuildVersionNumber ) );
       
   115 
       
   116     }
       
   117 
       
   118 /*---------------------------------------------------------------------------*/
       
   119 EXPORT_C TInt RHtiBtCommInterface::GetSendBufferSize() const
       
   120     {
       
   121     return KClientSendBufferMaxSize;
       
   122     }
       
   123 
       
   124 /*---------------------------------------------------------------------------*/
       
   125 EXPORT_C TInt RHtiBtCommInterface::GetReceiveBufferSize() const
       
   126     {
       
   127     return KClientReceiveBufferMaxSize;
       
   128     }
       
   129 
       
   130 /*---------------------------------------------------------------------------*/
       
   131 EXPORT_C void RHtiBtCommInterface::Receive(TDes8& aData,
       
   132                                         TRequestStatus& aStatus)
       
   133     {
       
   134     HTI_LOG_TEXT( "RHtiBtCommInterface::Receive()" );
       
   135     SendReceive( EBtCommServerRecv, TIpcArgs( &aData ), aStatus );
       
   136     }
       
   137 
       
   138 /*---------------------------------------------------------------------------*/
       
   139 EXPORT_C void RHtiBtCommInterface::Send(const TDesC8& aData,
       
   140                                         TRequestStatus& aStatus)
       
   141     {
       
   142     HTI_LOG_TEXT( "RHtiBtCommInterface::Send()" );
       
   143     SendReceive( EBtCommServerSend, TIpcArgs( &aData ), aStatus );
       
   144     }
       
   145 
       
   146 /*---------------------------------------------------------------------------*/
       
   147 EXPORT_C void RHtiBtCommInterface::CancelReceive()
       
   148     {
       
   149     HTI_LOG_TEXT( "RHtiBtCommInterface::CancelReceive()" );
       
   150     SendReceive( ECancelBtCommServerRecv, TIpcArgs( NULL ) );
       
   151     }
       
   152 
       
   153 /*---------------------------------------------------------------------------*/
       
   154 EXPORT_C void RHtiBtCommInterface::CancelSend()
       
   155     {
       
   156     HTI_LOG_TEXT( "RHtiBtCommInterface::CancelSend()" );
       
   157     SendReceive( ECancelBtCommServerSend, TIpcArgs( NULL ) );
       
   158     }
       
   159 
       
   160 /*---------------------------------------------------------------------------*/
       
   161 EXPORT_C TInt RHtiBtCommInterface::GetPortNumber() const
       
   162     {
       
   163     HTI_LOG_TEXT( "RHtiBtCommInterface::GetPortNumber()" );
       
   164     TPckgBuf<TInt> pckg;
       
   165     TInt ret = SendReceive( EGetServicePortNumber, TIpcArgs( &pckg ) );
       
   166     if ( ret == KErrNone )
       
   167         {
       
   168         ret = pckg();
       
   169         }
       
   170     return ret;
       
   171     }
       
   172 
       
   173 /*---------------------------------------------------------------------------*/
       
   174 TInt RHtiBtCommInterface::ConnectBt( TDesC8& aDeviceNameOrAddress, TInt aPort )
       
   175     {
       
   176     HTI_LOG_TEXT( "RHtiBtCommInterface::ConnectBt()" );
       
   177     return SendReceive( EBtCommServerConnect,
       
   178                             TIpcArgs( &aDeviceNameOrAddress, aPort ) );
       
   179     }
       
   180 
       
   181 /*---------------------------------------------------------------------------*/
       
   182 GLDEF_C TInt E32Main()
       
   183     {
       
   184     HTI_LOG_TEXT( "RHtiBtCommInterface::E32Main()" );
       
   185     return KErrNone;
       
   186     }
       
   187 
       
   188 // End of the file