bluetoothengine/btnotif/btnotifwrapper/src/btnotifwrapper.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : btnotifwrapper.cpp
       
     4 *  Part of     : bluetoothengine / btnotif
       
     5 *  Description : Wrapper for Bluetooth Notifiers
       
     6 *
       
     7 *  Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     8 *  All rights reserved.
       
     9 *  This component and the accompanying materials are made available
       
    10 *  under the terms of "Eclipse Public License v1.0"
       
    11 *  which accompanies this distribution, and is available
       
    12 *  at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    13 *
       
    14 *  Initial Contributors:
       
    15 *  Nokia Corporation - initial contribution.
       
    16 *
       
    17 *  Contributors:
       
    18 *  Nokia Corporation
       
    19 * ============================================================================
       
    20 * Template version: 4.1
       
    21 */
       
    22 
       
    23 #include "btnotifwrapper.h"
       
    24 #include <btnotif.h>
       
    25 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    26 #include <btextnotifierspartner.h>
       
    27 #endif
       
    28 
       
    29 /**  Identifier of the active object. */
       
    30 const TInt KWrapperActive = 10;
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // ?description_if_needed
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CBTNotifWrapper::CBTNotifWrapper( const TUid& aUid, const TUid& aChannel )
       
    40 :   MEikSrvNotifierBase2(),
       
    41     iUid( aUid ),
       
    42     iChannel( aChannel ),
       
    43     iResponsePtr( NULL, 0 )
       
    44     {
       
    45     }
       
    46 
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // ?description_if_needed
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 void CBTNotifWrapper::ConstructL()
       
    53     {
       
    54     // lazy initialization: members are created and destroyed when needed.
       
    55     }
       
    56 
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // NewLC
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CBTNotifWrapper* CBTNotifWrapper::NewLC( const TUid& aUid, const TUid& aChannel )
       
    63     {
       
    64     CBTNotifWrapper* self = new( ELeave ) CBTNotifWrapper( aUid, aChannel );
       
    65     CleanupStack::PushL( self );
       
    66     self->ConstructL();
       
    67     return self;
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // ?description_if_needed
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 CBTNotifWrapper::~CBTNotifWrapper()
       
    75     {
       
    76     Cancel();   // Cleans up buffers
       
    77     delete iActive;
       
    78     iBTNotif.Close();
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // From class MEikSrvNotifierBase2.
       
    83 // Frees all the resources i.e. destruct ourselves.
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 void CBTNotifWrapper::Release()
       
    87     {
       
    88     delete this;
       
    89     }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // From class MEikSrvNotifierBase2.
       
    93 // ?implementation_description
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 MEikSrvNotifierBase2::TNotifierInfo CBTNotifWrapper::RegisterL()
       
    97     {
       
    98     return Info();
       
    99     }
       
   100 
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // From class MEikSrvNotifierBase2.
       
   104 // ?implementation_description
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 MEikSrvNotifierBase2::TNotifierInfo CBTNotifWrapper::Info() const
       
   108     {
       
   109     MEikSrvNotifierBase2::TNotifierInfo info;
       
   110     info.iUid = iUid;
       
   111     info.iChannel = iChannel;
       
   112     info.iPriority = MEikSrvNotifierBase2::ENotifierPriorityVHigh;
       
   113     return info;
       
   114     }
       
   115 
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // From class MEikSrvNotifierBase2.
       
   119 // ?implementation_description
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 TPtrC8 CBTNotifWrapper::StartL(const TDesC8& aBuffer)
       
   123     {
       
   124     if( !IsSync() )
       
   125         {
       
   126         // due to the charasteristics of Bluetooth and UI operations,
       
   127         // most of Bluetooth notifiers do not support synchronous version.
       
   128         User::Leave( KErrNotSupported );
       
   129         }
       
   130     // Call notifier server and get response.
       
   131     if( !iBTNotif.Handle() )
       
   132         {
       
   133         User::LeaveIfError( iBTNotif.Connect() );
       
   134         }
       
   135     User::LeaveIfError( iBTNotif.StartNotifier( iUid, aBuffer ) );
       
   136     iBTNotif.Close();
       
   137     return KNullDesC8();
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // From class MEikSrvNotifierBase2.
       
   142 // ?implementation_description
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 void CBTNotifWrapper::StartL(const TDesC8& aBuffer, TInt aReplySlot, 
       
   146     const RMessagePtr2& aMessage)
       
   147     {
       
   148     if( !iMessage.IsNull() )
       
   149         {
       
   150         aMessage.Complete( KErrAlreadyExists );
       
   151         return;
       
   152         }
       
   153 
       
   154     // Call notifier server and get response.
       
   155     if( !iBTNotif.Handle() )
       
   156         {
       
   157         User::LeaveIfError( iBTNotif.Connect() );
       
   158         }
       
   159     if( !iActive )
       
   160         {
       
   161         iActive = CBtSimpleActive::NewL( *this, KWrapperActive );
       
   162         }
       
   163 
       
   164     // We need to store the parameters locally, as aBuffer is destroyed after
       
   165     // returning from this call. We do it on the heap, so we do not permanently
       
   166     // consume memory for the buffer.
       
   167 
       
   168     iParamsBuf.CreateL( aBuffer );
       
   169     TInt len = aMessage.GetDesMaxLength( aReplySlot );
       
   170     
       
   171     iResponseBuf.CreateL( len );
       
   172     aMessage.ReadL( aReplySlot, iResponseBuf );
       
   173 
       
   174     iBTNotif.StartNotifierAndGetResponse( iActive->RequestStatus(),
       
   175                 iUid, iParamsBuf, iResponseBuf );
       
   176     iActive->GoActive();
       
   177     // record the request
       
   178     iReplySlot = aReplySlot;
       
   179     iMessage = aMessage;
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // From class MEikSrvNotifierBase2.
       
   184 // ?implementation_description
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 void CBTNotifWrapper::Cancel()
       
   188     {
       
   189     // Call notifier server to cancel.
       
   190     if( iActive  )
       
   191         {
       
   192         iActive->Cancel(); 
       
   193         }
       
   194     if( !iMessage.IsNull() )
       
   195         {
       
   196         iMessage.Complete( KErrCancel );
       
   197         }
       
   198     iReplySlot = 0;
       
   199     iParamsBuf.Close();
       
   200     iResponseBuf.Close();
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // From class MEikSrvNotifierBase2.
       
   205 // Synchronous notifier update.
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 TPtrC8 CBTNotifWrapper::UpdateL(const TDesC8& aBuffer)
       
   209     {
       
   210     // Call notifier server and get response.
       
   211     TBuf8<256> response;
       
   212     if( !iBTNotif.Handle() )
       
   213         {
       
   214         User::LeaveIfError( iBTNotif.Connect() );
       
   215         }
       
   216     User::LeaveIfError( iBTNotif.UpdateNotifier( iUid, aBuffer, response ) );
       
   217     return response;
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // From class MEikSrvNotifierBase2.
       
   222 // Asynchronous notifier update.
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 void CBTNotifWrapper::UpdateL(const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage)
       
   226     {
       
   227     (void) aReplySlot;
       
   228     (void) aBuffer;
       
   229     if( iMessage.IsNull() )
       
   230         {
       
   231         // There is no outstanding request, can't relate this to anything.
       
   232         aMessage.Complete( KErrNotFound );
       
   233         return;
       
   234         }
       
   235     // Call notifier server and get response.
       
   236     // Async updates are just for updating parameters, so they are still 
       
   237     // done synchronously between here and btnotif, as they don't 
       
   238     // require any user feedback or other response with
       
   239     // asynchronous/long cycles.
       
   240     TPtrC8 response = UpdateL( aBuffer );
       
   241     aMessage.WriteL( aReplySlot, response );
       
   242     aMessage.Complete( KErrNone );
       
   243     }
       
   244 
       
   245 // ---------------------------------------------------------------------------
       
   246 // From class MBtSimpleActiveObserver.
       
   247 // Callback to notify that an outstanding request has completed.
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CBTNotifWrapper::RequestCompletedL( CBtSimpleActive* aActive, TInt aStatus )
       
   251     {
       
   252     ASSERT( aActive->RequestId() == KWrapperActive );
       
   253     (void) aActive;
       
   254     if( !iMessage.IsNull() )
       
   255         {
       
   256         TInt err( aStatus );
       
   257         if( !err )
       
   258             {
       
   259             err = iMessage.Write( iReplySlot, iResponseBuf );
       
   260             }
       
   261         iMessage.Complete( err );
       
   262         }
       
   263     // Clean up after usage.
       
   264     iBTNotif.Close();
       
   265     delete iActive;
       
   266     iActive = NULL;
       
   267     }
       
   268 
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // From class MBtSimpleActiveObserver.
       
   272 // Cancel and clean up all requests related to the active object.
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 void CBTNotifWrapper::CancelRequest( TInt aRequestId )
       
   276     {
       
   277     ASSERT( aRequestId == KWrapperActive );
       
   278     (void) aRequestId;
       
   279     iBTNotif.CancelNotifier( iUid );
       
   280     }
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // From class MBtSimpleActiveObserver.
       
   284 // 
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CBTNotifWrapper::HandleError( CBtSimpleActive* aActive, 
       
   288         TInt aError )
       
   289     {
       
   290     // RequestCompletedL() is not leavable.
       
   291     (void) aActive;
       
   292     (void) aError;
       
   293     }
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // Check if this is a synchronous notifier or not.
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 TBool CBTNotifWrapper::IsSync() const
       
   300     {
       
   301     return ( iUid == KBTGenericInfoNotifierUid );
       
   302     }