sensorservices/sensorserver/src/client/sensrvdatahandler.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2006-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:  CSensrvDataHandler implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "sensrvdatahandler.h"
       
    21 #include "sensrvtrace.h"
       
    22 #include "sensrvdatahandlerobserver.h"
       
    23 #include "sensrvdefines.h"
       
    24 
       
    25 
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // C++ constructor
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 
       
    34 CSensrvDataHandler::CSensrvDataHandler( RSensrvClient& aSensrvClient, 
       
    35                                         const TSensrvChannelInfo& aChannelInfo )
       
    36     : CActive( EPriorityStandard ), 
       
    37       iSensrvClient( aSensrvClient ), 
       
    38       iChannelInfo( aChannelInfo )
       
    39     {
       
    40     
       
    41     CActiveScheduler::Add( this );
       
    42     
       
    43     }
       
    44 
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // 2nd phase of construction
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 void CSensrvDataHandler::ConstructL()
       
    51     {
       
    52     
       
    53     }
       
    54 
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // 2-phase constructor
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CSensrvDataHandler* CSensrvDataHandler::NewL( RSensrvClient& aSensrvClient, 
       
    61                                         const TSensrvChannelInfo& aChannelInfo )
       
    62     {
       
    63     CSensrvDataHandler* self = CSensrvDataHandler::NewLC( aSensrvClient, aChannelInfo );
       
    64     CleanupStack::Pop( self );
       
    65     return self;
       
    66     }
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // 2-phase constructor
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CSensrvDataHandler* CSensrvDataHandler::NewLC( RSensrvClient& aSensrvClient, 
       
    74                                         const TSensrvChannelInfo& aChannelInfo )
       
    75     {
       
    76     CSensrvDataHandler* self = new( ELeave ) CSensrvDataHandler( aSensrvClient, aChannelInfo );
       
    77     CleanupStack::PushL( self );
       
    78     self->ConstructL();
       
    79     return self;
       
    80     }
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Destructor
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CSensrvDataHandler::~CSensrvDataHandler()
       
    88     {
       
    89     Cancel();
       
    90     delete iWriteBuffer;
       
    91     delete iWriteBufferPtr;
       
    92     delete iReadBuffer;
       
    93     }
       
    94 
       
    95 
       
    96 
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // From class CActive
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 void CSensrvDataHandler::DoCancel()
       
   103     {
       
   104     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::DoCancel()" ) ) );
       
   105     
       
   106     iSensrvClient.SendReceiveSync( ESensrvSrvReqStopListening, 
       
   107                                         TIpcArgs( iChannelInfo.iChannelId ) );
       
   108                                         
       
   109     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::DoCancel - return void" ) ) );
       
   110     }
       
   111     
       
   112 // ---------------------------------------------------------------------------
       
   113 // From class CActive
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CSensrvDataHandler::RunL()
       
   117     {
       
   118     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::RunL - iStatus %d" ), iStatus.Int() ) );
       
   119 
       
   120 	Deque();
       
   121 	CActiveScheduler::Add(this);
       
   122     if( iStatus.Int() == KErrNone )
       
   123         {
       
   124         if( iErrorCount > 0 )
       
   125             {
       
   126             iErrorCount = 0;
       
   127             }
       
   128         HBufC8* temp = iReadBuffer;
       
   129         iReadBuffer = iWriteBuffer;
       
   130         iWriteBuffer = temp;    
       
   131         
       
   132         TSensrvAsyncChannelDataCountsRetval dataCounts = iDataCountsPckgBuf();
       
   133         COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::RunL - DataItemCount = %d, DataLostCount = %d "), dataCounts.iDataItemCount, dataCounts.iDataLostCount ) );
       
   134         TInt lostCount( dataCounts.iDataLostCount );
       
   135         lostCount += ( iReadBufferCount - iGetDataCount );
       
   136         
       
   137         // Reset buffer counts
       
   138         iReadBufferCount = dataCounts.iDataItemCount;
       
   139         iGetDataCount = 0; 
       
   140         
       
   141         CreateAndSendRequest();
       
   142         
       
   143         // Notify to listener
       
   144         iDataObserver->DataReceived( iReadBufferCount, lostCount );
       
   145         
       
   146         }
       
   147     else
       
   148         {
       
   149         if( iErrorCount == 0 )
       
   150             {
       
   151             iErrorCount++;
       
   152             CreateAndSendRequest();
       
   153             
       
   154             // Send minor error
       
   155             iDataObserver->DataError( ESensrvErrorSeverityMinor );
       
   156             
       
   157             }
       
   158         else if( iErrorCount == 1 )
       
   159             {
       
   160             iErrorCount++;
       
   161             // Reopen channel
       
   162             
       
   163             TInt maxBufferCount(0);
       
   164             iDataObserver->ReopenChannelForDataListeningL( maxBufferCount );
       
   165             
       
   166             TSensrvStartListeningParametersPckgBuf startListeningPckg( iListeningParameters );
       
   167 
       
   168             COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::RunL - Restart listening - iDesiredBufferingCount = %d, iMaximumBufferingCount = %d, iBufferingPeriod = %d "), 
       
   169                 iListeningParameters.iDesiredBufferingCount, 
       
   170                 iListeningParameters.iMaximumBufferingCount, 
       
   171                 iListeningParameters.iBufferingPeriod ) );
       
   172             
       
   173             
       
   174             TInt err = iSensrvClient.SendReceiveSync( ESensrvSrvReqStartListening, 
       
   175                             TIpcArgs( iChannelInfo.iChannelId, &startListeningPckg ) );
       
   176             if( err )
       
   177                 {
       
   178                 // send fatal error
       
   179                 iDataObserver->DataError( ESensrvErrorSeverityFatal );                    
       
   180                 }
       
   181             else
       
   182                 {
       
   183                 TInt newMaxSize( maxBufferCount * iChannelInfo.iDataItemSize );
       
   184                 if( iWriteBuffer->Length() > newMaxSize )
       
   185                     {
       
   186                     iWriteBuffer->ReAllocL( newMaxSize );
       
   187                     iReadBuffer->ReAllocL( newMaxSize );
       
   188                     delete iWriteBufferPtr;
       
   189                     iWriteBufferPtr = NULL;
       
   190                     iWriteBufferPtr = new (ELeave) TPtr8( const_cast<TUint8*>( iWriteBuffer->Des().Ptr() ), newMaxSize );
       
   191                     }
       
   192                 // Start listening
       
   193                 CreateAndSendRequest();
       
   194                 }
       
   195             }
       
   196         else
       
   197             {
       
   198             // send fatal error
       
   199             iDataObserver->DataError( ESensrvErrorSeverityFatal );  
       
   200             }
       
   201         
       
   202         }
       
   203         
       
   204     }
       
   205     
       
   206 // ---------------------------------------------------------------------------
       
   207 // CSensrvDataHandler::RunError
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 TInt CSensrvDataHandler::RunError( TInt /*aError*/ )
       
   211     {
       
   212     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::RunError" ) ) );
       
   213     // send fatal error
       
   214     iDataObserver->DataError( ESensrvErrorSeverityFatal );
       
   215     return KErrNone;
       
   216     }
       
   217 
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // CSensrvDataHandler::StartListening
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 void CSensrvDataHandler::StartListeningL( MSensrvDataHandlerObserver* aDataObserver, 
       
   224                                 TSensrvStartListeningParameters aListeningParameters,
       
   225                                 TInt aMaxBufferCount )
       
   226     {
       
   227     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::StartListening - Start" ) ) );
       
   228     
       
   229     if( IsActive() )
       
   230         {
       
   231         COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::StartListening - Return" ) ) );
       
   232         return;
       
   233         }
       
   234     
       
   235     iDataObserver = aDataObserver;
       
   236     iListeningParameters = aListeningParameters;
       
   237         
       
   238     iErrorCount = 0;
       
   239     
       
   240     TInt size = iListeningParameters.iMaximumBufferingCount * iChannelInfo.iDataItemSize;
       
   241     
       
   242     // Check size
       
   243     if( size <= 0 || iListeningParameters.iMaximumBufferingCount > ( KMaxTInt / iChannelInfo.iDataItemSize )
       
   244         || iListeningParameters.iMaximumBufferingCount < iListeningParameters.iDesiredBufferingCount 
       
   245         || iListeningParameters.iMaximumBufferingCount > aMaxBufferCount )
       
   246         {
       
   247         size = aMaxBufferCount * iChannelInfo.iDataItemSize;
       
   248         }
       
   249     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::StartListening - bufferSize: %d" ), size ) );
       
   250     delete iWriteBuffer;
       
   251     iWriteBuffer = NULL;
       
   252     iWriteBuffer = HBufC8::NewL( size );
       
   253     delete iReadBuffer;
       
   254     iReadBuffer = NULL;
       
   255     iReadBuffer = HBufC8::NewL( size );
       
   256 
       
   257     delete iWriteBufferPtr;
       
   258     iWriteBufferPtr = NULL;
       
   259     iWriteBufferPtr = new (ELeave) TPtr8( const_cast<TUint8*>( iWriteBuffer->Des().Ptr() ), size );
       
   260     
       
   261 
       
   262     CreateAndSendRequest();
       
   263             
       
   264     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::StartListening - Return" ) ) );
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // CSensrvDataHandler::GetData
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 TInt CSensrvDataHandler::GetData( TDes8& aDataPtr )
       
   272     {
       
   273     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::GetData - Start" ) ) );
       
   274     TInt err( KErrNone );
       
   275     
       
   276     if( aDataPtr.Length() < iChannelInfo.iDataItemSize )
       
   277         {
       
   278         // aDataPtr is too small
       
   279         err = KErrOverflow;
       
   280         }
       
   281     else if( iGetDataCount >= iReadBufferCount || !iReadBuffer )
       
   282         {
       
   283         // data not found.
       
   284         err = KErrNotFound;
       
   285         }
       
   286     else
       
   287         {
       
   288         // set data
       
   289         TUint8* ptr = const_cast<TUint8*>( iReadBuffer->Des().Ptr() );
       
   290         ptr += ( iGetDataCount * iChannelInfo.iDataItemSize ); 
       
   291         
       
   292         aDataPtr.Copy( ptr, iChannelInfo.iDataItemSize );
       
   293         ++iGetDataCount;
       
   294         }
       
   295     
       
   296     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::GetData - Return %d" ), err ) );
       
   297     return err;
       
   298     }
       
   299     
       
   300 // ---------------------------------------------------------------------------
       
   301 // CSensrvDataHandler::CreateAndSendRequestL
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 void CSensrvDataHandler::CreateAndSendRequest()
       
   305     {
       
   306     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::CreateAndSendRequestL - Start" ) ) );
       
   307     
       
   308     iWriteBufferPtr->Set( iWriteBuffer->Des() );
       
   309     
       
   310     iSensrvClient.SendReceiveAsync( ESensrvSrvReqAsyncChannelData, 
       
   311             TIpcArgs( iChannelInfo.iChannelId, iWriteBufferPtr, &iDataCountsPckgBuf ), iStatus );
       
   312     
       
   313     SetActive();
       
   314     
       
   315     COMPONENT_TRACE( ( _L( "Sensrv Client - CSensrvDataHandler::CreateAndSendRequestL - Return" ) ) );
       
   316     }
       
   317