datasourcemodules/bluetoothgpspositioningmodule/btgpspsy/src/Utils/BTGPSMessageSender.cpp
changeset 36 b47902b73a93
parent 0 9cfd9a3ee49c
equal deleted inserted replaced
35:a2efdd544abf 36:b47902b73a93
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <e32std.h>
       
    20 #include <es_sock.h>
       
    21 #include "BTGPSMessageSender.h"
       
    22 #include "BTGPSDeviceManager.h"
       
    23 #include "BTGPSConstantsManager.h"
       
    24 #include "BTGPSRequestHandler.h"
       
    25 #include "BTGPSLogging.h"
       
    26 
       
    27 // EXTERNAL DATA STRUCTURES
       
    28 
       
    29 // EXTERNAL FUNCTION PROTOTYPES  
       
    30 
       
    31 // CONSTANTS
       
    32 
       
    33 // Granularity
       
    34 const TInt KBTGPSArrayGranularity = 3;
       
    35 
       
    36 // MACROS
       
    37 
       
    38 // LOCAL CONSTANTS AND MACROS
       
    39 
       
    40 // MODULE DATA STRUCTURES
       
    41 
       
    42 // LOCAL FUNCTION PROTOTYPES
       
    43 
       
    44 // FORWARD DECLARATIONS
       
    45 
       
    46 // ============================= LOCAL FUNCTIONS ===============================
       
    47 
       
    48 // ============================ MEMBER FUNCTIONS ===============================
       
    49 
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CBTGPSMessageSender::NewL
       
    53 // -----------------------------------------------------------------------------
       
    54 CBTGPSMessageSender* CBTGPSMessageSender::NewL(
       
    55     RSocket& aSocket,
       
    56     CBTGPSDeviceManager& aDeviceManager)
       
    57     {
       
    58     CBTGPSMessageSender* self = new (ELeave) CBTGPSMessageSender(
       
    59         aSocket,
       
    60         aDeviceManager);
       
    61     CleanupStack::PushL(self);
       
    62     self->ConstructL();
       
    63     CleanupStack::Pop();
       
    64     return self;
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CBTGPSMessageSender::~CBTGPSMessageSender
       
    69 // -----------------------------------------------------------------------------
       
    70 CBTGPSMessageSender::~CBTGPSMessageSender()
       
    71     {
       
    72     Cancel();
       
    73     if(iTimer!=NULL)
       
    74         {
       
    75         iTimer->Cancel();
       
    76         delete iTimer;
       
    77         }
       
    78     delete iBuffer;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CBTGPSMessageSender::ConstructL
       
    83 // -----------------------------------------------------------------------------
       
    84 void CBTGPSMessageSender::ConstructL()
       
    85     {
       
    86     iBuffer = new (ELeave) CDesC8ArrayFlat(KBTGPSArrayGranularity);
       
    87     iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CBTGPSMessageSender::CBTGPSMessageSender
       
    92 // C++ default constructor can NOT contain any code, that
       
    93 // might leave.
       
    94 // -----------------------------------------------------------------------------
       
    95 CBTGPSMessageSender::CBTGPSMessageSender(
       
    96         RSocket& aSocket,
       
    97         CBTGPSDeviceManager& aDeviceManager)
       
    98     : CActive(EPriorityNormal),
       
    99       iSocket(aSocket),
       
   100       iDeviceManager(aDeviceManager)
       
   101     {
       
   102     CActiveScheduler::Add(this);
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CBTGPSMessageSender::SendL
       
   107 // -----------------------------------------------------------------------------
       
   108 void CBTGPSMessageSender::SendL(const TDesC8& aMsg)
       
   109     {
       
   110     //if device is not connected then fail the sending
       
   111     if(iDeviceManager.DeviceConnectStatus() != EBTDeviceConnected)
       
   112         {
       
   113         User::Leave(KErrDisconnected);
       
   114         }
       
   115     
       
   116     if(IsActive() || iTimer->IsActive())
       
   117         {
       
   118         //Add this message to buffer
       
   119         iBuffer->AppendL(aMsg);
       
   120         }
       
   121     else
       
   122         {
       
   123         //Send this message
       
   124         TRACESTRING("CBTGPSMessageSender::SendL sending")
       
   125         TRACESTRING8(aMsg)
       
   126         iSocket.Write(aMsg, iStatus);
       
   127         SetActive();
       
   128         StartTimer();
       
   129         }
       
   130     }
       
   131     
       
   132 // -----------------------------------------------------------------------------
       
   133 // CBTGPSMessageSender::StartTimer
       
   134 // -----------------------------------------------------------------------------
       
   135 void CBTGPSMessageSender::StartTimer()
       
   136     {
       
   137     iTimer->Cancel();
       
   138     iTimer->Start(
       
   139         CBTGPSRequestHandler::ConstantsManager().iSendDelay,
       
   140         CBTGPSRequestHandler::ConstantsManager().iSendDelay,
       
   141         TCallBack(TimerCallback, this));
       
   142     }
       
   143     
       
   144 // -----------------------------------------------------------------------------
       
   145 // CBTGPSMessageSender::TimerTick
       
   146 // -----------------------------------------------------------------------------
       
   147 void CBTGPSMessageSender::TimerTick()
       
   148     {
       
   149     if(iBuffer->MdcaCount()!=0)
       
   150         {
       
   151         //check device connectivity
       
   152         if(iDeviceManager.DeviceConnectStatus() != EBTDeviceConnected)
       
   153             {
       
   154             Cancel();
       
   155             }
       
   156         else
       
   157             {
       
   158             //Buffer is not empty
       
   159             if(!IsActive())
       
   160                 {
       
   161                 TRACESTRING("CBTGPSMessageSender::TimerTick sending")
       
   162                 TRACESTRING8(iBuffer->MdcaPoint(0))
       
   163                 iSocket.Write(iBuffer->MdcaPoint(0), iStatus); //Send the first one
       
   164                 SetActive();
       
   165                 iBuffer->Delete(0); //Delete it from buffer
       
   166                 }
       
   167             }
       
   168         }
       
   169     else
       
   170         {
       
   171         //Stop timer
       
   172         iTimer->Cancel();
       
   173         }
       
   174     }
       
   175         
       
   176 // -----------------------------------------------------------------------------
       
   177 // CBTGPSMessageSender::TimerCallback
       
   178 // -----------------------------------------------------------------------------
       
   179 TInt CBTGPSMessageSender::TimerCallback(TAny* aAny)
       
   180     {
       
   181     reinterpret_cast<CBTGPSMessageSender*>(aAny)->TimerTick();
       
   182     return 1; //continue periodic timer
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CBTGPSMessageSender::RunL
       
   187 // -----------------------------------------------------------------------------
       
   188 void CBTGPSMessageSender::RunL()
       
   189     {
       
   190     //error is ignored. do nothing
       
   191     }
       
   192         
       
   193 // -----------------------------------------------------------------------------
       
   194 // CBTGPSMessageSender::DoCancel
       
   195 // -----------------------------------------------------------------------------
       
   196 void CBTGPSMessageSender::DoCancel()
       
   197     {
       
   198     iSocket.CancelWrite();
       
   199     if(iBuffer!=NULL)
       
   200         {
       
   201         iBuffer->Reset();
       
   202         }
       
   203     }
       
   204         
       
   205 // -----------------------------------------------------------------------------
       
   206 // CBTGPSMessageSender::RunError
       
   207 // -----------------------------------------------------------------------------
       
   208 TInt CBTGPSMessageSender::RunError(TInt /*aError*/)
       
   209     {
       
   210     //should never be called
       
   211     return KErrNone;
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CBTGPSMessageSender::BTDeviceStatusChanged
       
   216 // -----------------------------------------------------------------------------
       
   217 void CBTGPSMessageSender::BTDeviceStatusChanged(
       
   218             TInt aConnectStatus, 
       
   219             TInt /*aDeviceType*/,
       
   220             TInt /*aErr*/)
       
   221     {
       
   222     if(aConnectStatus!=EBTDeviceConnected)
       
   223         {
       
   224         //reset buffer is device is not connected
       
   225         Cancel();
       
   226         }
       
   227     }
       
   228 
       
   229 //  End of File
       
   230 
       
   231 
       
   232