bluetoothengine/headsetsimulator/core/src/Server/hsclient.cpp
branchheadsetsimulator
changeset 60 90dbfc0435e3
equal deleted inserted replaced
59:02103bf20ee5 60:90dbfc0435e3
       
     1 /*
       
     2  * Component Name: Headset Simulator
       
     3  * Author: Comarch S.A.
       
     4  * Version: 1.0
       
     5  * Copyright (c) 2010 Comarch S.A.
       
     6  *  
       
     7  * This Software is submitted by Comarch S.A. to Symbian Foundation Limited on 
       
     8  * the basis of the Member Contribution Agreement entered between Comarch S.A. 
       
     9  * and Symbian Foundation Limited on 5th June 2009 (“Agreement”) and may be 
       
    10  * used only in accordance with the terms and conditions of the Agreement. 
       
    11  * Any other usage, duplication or redistribution of this Software is not 
       
    12  * allowed without written permission of Comarch S.A.
       
    13  * 
       
    14  */
       
    15 
       
    16 #include "hsclient.h"
       
    17 #include "hsclientobservers.h"
       
    18 #include "debug.h"
       
    19 
       
    20 CHsClient* CHsClient::NewL( CBluetoothSocket *aClient,
       
    21         MHsClientStateNotifier *aClientStateNotifier,
       
    22         MHsClientObserver *aClientDisconnectNotifier )
       
    23     {
       
    24     User::LeaveIfNull( aClient );
       
    25 
       
    26     CHsClient *self = CHsClient::NewLC( aClient, aClientStateNotifier,
       
    27             aClientDisconnectNotifier );
       
    28     CleanupStack::Pop( self );
       
    29 
       
    30     return self;
       
    31     }
       
    32 
       
    33 CHsClient* CHsClient::NewLC( CBluetoothSocket *aClient,
       
    34         MHsClientStateNotifier *aClientStateNotifier,
       
    35         MHsClientObserver *aClientDisconnectNotifier )
       
    36     {
       
    37 
       
    38     User::LeaveIfNull( aClient );
       
    39 
       
    40     CHsClient *self = new ( ELeave ) CHsClient( *aClient, aClientStateNotifier,
       
    41             aClientDisconnectNotifier );
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44 
       
    45     return self;
       
    46     }
       
    47 
       
    48 CHsClient::~CHsClient()
       
    49     {
       
    50     TRACE_FUNC_ENTRY
       
    51 
       
    52     if ( iSocket )
       
    53         {
       
    54         iSocket->CancelAll();
       
    55         delete iSocket;
       
    56         }
       
    57 
       
    58     if ( iConnected )
       
    59         {
       
    60         if ( iStateNotifier )
       
    61             {
       
    62             TRAP_IGNORE( iStateNotifier->HandleClientDisconnectL(KErrNone) );
       
    63             }
       
    64         }
       
    65 
       
    66     TRACE_FUNC_EXIT
       
    67     }
       
    68 
       
    69 void CHsClient::Send( const TDesC8 &aData )
       
    70     {
       
    71     TRACE_FUNC_ENTRY
       
    72 
       
    73     if ( iSocket && iConnected )
       
    74         {
       
    75         TRAP_IGNORE( BufferedProcessL(aData) );
       
    76         }
       
    77 
       
    78     TRACE_FUNC_EXIT
       
    79     }
       
    80 
       
    81 void CHsClient::SetClientDisconnectNotfier( MHsClientObserver &aNotifier )
       
    82     {
       
    83     TRACE_FUNC_ENTRY
       
    84 
       
    85     iClientObserver = &aNotifier;
       
    86 
       
    87     TRACE_FUNC_EXIT
       
    88     }
       
    89 
       
    90 void CHsClient::SetClientStateNotfier( MHsClientStateNotifier &aNotifier )
       
    91     {
       
    92     TRACE_FUNC_ENTRY
       
    93 
       
    94     iStateNotifier = &aNotifier;
       
    95 
       
    96     TRACE_FUNC_EXIT
       
    97     }
       
    98 
       
    99 MHsClientStateNotifier* CHsClient::GetClientStateNotifer()
       
   100     {
       
   101     TRACE_FUNC_ENTRY
       
   102 
       
   103     TRACE_FUNC_EXIT
       
   104     return iStateNotifier;
       
   105     }
       
   106 
       
   107 CHsClient::CHsClient( CBluetoothSocket &aClient,
       
   108         MHsClientStateNotifier *aClientStateNotifier,
       
   109         MHsClientObserver *aClientDisconnectNotifier )
       
   110     {
       
   111     TRACE_FUNC_ENTRY
       
   112 
       
   113     iSocket = &aClient;
       
   114     iStateNotifier = aClientStateNotifier;
       
   115     iState = CHsClient::EIdle;
       
   116     iConnected = ETrue;
       
   117     iClientObserver = aClientDisconnectNotifier;
       
   118     iSocket->SetNotifier( *this );
       
   119 
       
   120     TRACE_FUNC_EXIT
       
   121     }
       
   122 
       
   123 void CHsClient::ConstructL()
       
   124     {
       
   125     TRACE_FUNC_ENTRY
       
   126     BufferedProcessL();
       
   127     TRACE_FUNC_EXIT
       
   128     }
       
   129 
       
   130 void CHsClient::RawRecvL( TDes8 &aData )
       
   131     {
       
   132     TRACE_FUNC_ENTRY
       
   133 
       
   134     User::LeaveIfNull( iSocket );
       
   135     if ( !iConnected )
       
   136         {
       
   137         TRACE_INFO( _L(" Client is no longer connected" ) )
       
   138         User::LeaveIfError( KErrDisconnected );
       
   139         }
       
   140     iState = CHsClient::EReceiving;
       
   141     User::LeaveIfError( iSocket->RecvOneOrMore( aData, iReceiveFlag,
       
   142             iReceiveMessageLength ) );
       
   143 
       
   144     TRACE_FUNC_EXIT
       
   145     }
       
   146 
       
   147 void CHsClient::RawSendL( const TDesC8 &aData )
       
   148     {
       
   149     TRACE_FUNC_ENTRY
       
   150 
       
   151     User::LeaveIfNull( iSocket );
       
   152     if ( !iConnected )
       
   153         {
       
   154         User::LeaveIfError( KErrDisconnected );
       
   155         }
       
   156     if ( aData.Length() == 0 )
       
   157         {
       
   158         User::Leave( KErrWrite );
       
   159         }
       
   160     iState = CHsClient::ESending;
       
   161     User::LeaveIfError( iSocket->Send( aData, iSendFlag ) );
       
   162 
       
   163     TRACE_FUNC_EXIT
       
   164     }
       
   165 
       
   166 void CHsClient::BufferedProcessL( const TDesC8 &aData )
       
   167     {
       
   168     TRACE_FUNC_ENTRY
       
   169 
       
   170     if ( iState != CHsClient::ESending )
       
   171         {
       
   172         iTempSendBuf.Append( aData );
       
   173         SwapBuffers();
       
   174         if ( iSendBuf.Length() > 0 )
       
   175             {
       
   176             iSocket->CancelRecv();
       
   177             RawSendL( iSendBuf );
       
   178             }
       
   179         else if ( iState != CHsClient::ESending )
       
   180             {
       
   181             RawRecvL( iReceiveMessageBuffer );
       
   182             }
       
   183         }
       
   184     else
       
   185         {
       
   186 
       
   187         iTempSendBuf.Append( aData );
       
   188         }
       
   189 
       
   190     TRACE_FUNC_EXIT
       
   191     }
       
   192 
       
   193 void CHsClient::SwapBuffers()
       
   194     {
       
   195     TRACE_FUNC_ENTRY
       
   196 
       
   197     iSendBuf.Copy( iTempSendBuf );
       
   198     iTempSendBuf.Zero();
       
   199 
       
   200     TRACE_FUNC_EXIT
       
   201     }
       
   202 
       
   203 void CHsClient::HandleConnectCompleteL( TInt aErr )
       
   204     {
       
   205 
       
   206     TRACE_FUNC_ENTRY
       
   207     TRACE_INFO( ( _L("aErr = %d"), aErr) )
       
   208 
       
   209     TRACE_FUNC_EXIT
       
   210     }
       
   211 
       
   212 void CHsClient::HandleAcceptCompleteL( TInt aErr )
       
   213     {
       
   214     TRACE_FUNC_ENTRY
       
   215     TRACE_INFO( ( _L("aErr = %d"), aErr) )
       
   216 
       
   217     TRACE_FUNC_EXIT
       
   218     }
       
   219 
       
   220 void CHsClient::HandleShutdownCompleteL( TInt aErr )
       
   221     {
       
   222     TRACE_FUNC_ENTRY
       
   223     TRACE_INFO( ( _L("aErr = %d"), aErr) )
       
   224 
       
   225     TRACE_FUNC_EXIT
       
   226     }
       
   227 
       
   228 void CHsClient::HandleSendCompleteL( TInt aErr )
       
   229     {
       
   230     TRACE_FUNC_ENTRY
       
   231     TRACE_INFO( ( _L("aErr = %d"), aErr) )
       
   232 
       
   233     iState = CHsClient::EIdle;
       
   234     iSendBuf.Zero();
       
   235     if ( aErr != KErrDisconnected )
       
   236         {
       
   237         BufferedProcessL( KNullDesC8 );
       
   238         }
       
   239     if ( iStateNotifier )
       
   240         {
       
   241         if ( aErr != KErrDisconnected )
       
   242             {
       
   243             iStateNotifier->HandleClientSendCompleteL( aErr );
       
   244             }
       
   245         else
       
   246             {
       
   247             iConnected = EFalse;
       
   248             iStateNotifier->HandleClientDisconnectL( aErr );
       
   249             iClientObserver->HandleClientDisconnect( this, aErr );
       
   250             }
       
   251         }
       
   252 
       
   253     TRACE_FUNC_EXIT
       
   254     }
       
   255 
       
   256 void CHsClient::HandleReceiveCompleteL( TInt aErr )
       
   257     {
       
   258     TRACE_FUNC_ENTRY
       
   259     TRACE_INFO( ( _L("aErr = %d"), aErr) )
       
   260 
       
   261     iState = CHsClient::EIdle;
       
   262     if ( iStateNotifier )
       
   263         {
       
   264         if ( aErr == KErrNone )
       
   265             {
       
   266             TRAP_IGNORE( iStateNotifier->HandleClientReceiveCompleteL(
       
   267                             iReceiveMessageBuffer, aErr ) );
       
   268 
       
   269             }
       
   270         else
       
   271             {
       
   272             iConnected = EFalse;
       
   273             iStateNotifier->HandleClientDisconnectL( aErr );
       
   274             iClientObserver->HandleClientDisconnect( this, aErr );
       
   275             }
       
   276         }
       
   277 
       
   278     if ( aErr == KErrNone )
       
   279         {
       
   280         BufferedProcessL( KNullDesC8 );
       
   281         }
       
   282     TRACE_FUNC_EXIT
       
   283     }
       
   284 
       
   285 void CHsClient::HandleIoctlCompleteL( TInt aErr )
       
   286     {
       
   287     TRACE_FUNC_ENTRY
       
   288     TRACE_INFO( ( _L("aErr = %d"), aErr) );
       
   289 
       
   290     TRACE_FUNC_EXIT
       
   291     }
       
   292 
       
   293 void CHsClient::HandleActivateBasebandEventNotifierCompleteL( TInt aErr,
       
   294         TBTBasebandEventNotification& aEventNotification )
       
   295     {
       
   296     TRACE_FUNC_ENTRY
       
   297     TRACE_INFO( ( _L("aErr = %d"), aErr) );
       
   298 
       
   299     TRACE_FUNC_EXIT
       
   300     }