loadgen/engine/src/loadgen_bluetooth.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     1 /*
       
     2 * Copyright (c) 2010 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "loadgen_bluetooth.h"
       
    21 #include "loadgen_utils.h"
       
    22 #include "loadgen.hrh"
       
    23 #include "loadgen_traces.h"
       
    24 #include <loadgen.rsg>
       
    25 #include <e32hal.h>
       
    26 #include <u32hal.h>
       
    27 #include <e32math.h>
       
    28 #include <eikenv.h>
       
    29 #include <btserversdkcrkeys.h>
       
    30 #include <btnotifierapi.h>
       
    31 #include <btpm.h>
       
    32 
       
    33 _LIT(KThreadName, "Bluetooth %d");
       
    34 _LIT(KBTLinkManagerStr, "BTLinkManager");
       
    35 
       
    36 const TInt KDefaultStart = 50;
       
    37 const TInt KDefaultPeriod = 5000000;
       
    38    
       
    39 // ===================================== MEMBER FUNCTIONS =====================================
       
    40 
       
    41 CBluetooth* CBluetooth::NewL(TBluetoothAttributes& aAttributes, TInt aReferenceNumber)
       
    42     {
       
    43     CBluetooth* self = new(ELeave) CBluetooth(aAttributes, aReferenceNumber);
       
    44     CleanupStack::PushL(self);
       
    45     TRAPD(err, self->ConstructL());
       
    46     if( err != KErrNone )
       
    47         {
       
    48         User::Leave(err);
       
    49         }
       
    50     CleanupStack::Pop(self);
       
    51     return self;    
       
    52     }
       
    53 
       
    54 // --------------------------------------------------------------------------------------------
       
    55 
       
    56 CBluetooth::~CBluetooth()
       
    57     {
       
    58     Close();
       
    59     }
       
    60 
       
    61 // --------------------------------------------------------------------------------------------
       
    62 
       
    63 CBluetooth::CBluetooth(TBluetoothAttributes& aAttributes, TInt aReferenceNumber) : iAttributes(aAttributes)
       
    64     {
       
    65     iAttributes.iId = aReferenceNumber;
       
    66     }
       
    67 
       
    68 // --------------------------------------------------------------------------------------------
       
    69 
       
    70 void CBluetooth::ConstructL()
       
    71     {    
       
    72     CLoadBase::ConstructL();
       
    73     
       
    74     iType = ELoadGenCmdNewLoadBluetooth;
       
    75     
       
    76     InitializeBluetoothL();
       
    77                         
       
    78     TBuf<64> threadName;
       
    79     threadName.Format(KThreadName, iAttributes.iId);
       
    80             
       
    81     // create a thread
       
    82     User::LeaveIfError(iThread.Create(threadName, ThreadFunction, KDefaultStackSize*2, KMinHeapSize, 1024*KMinHeapSize, (TAny*) &iAttributes ));
       
    83     
       
    84     // set priority of the thread
       
    85     SetPriority();
       
    86     iState = ELoadStateConstructed;
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------------------------
       
    90 
       
    91 TInt CBluetooth::ThreadFunction(TAny* aThreadArg)
       
    92     {
       
    93     TInt err = KErrNone;
       
    94     
       
    95     CTrapCleanup* pC = CTrapCleanup::New();
       
    96     CActiveScheduler* pS = new CActiveScheduler;
       
    97     CActiveScheduler::Install(pS);
       
    98 
       
    99     // start generating load, pass pointer to arguments
       
   100     GenerateLoad(*((TBluetoothAttributes*) aThreadArg));
       
   101 
       
   102     delete pS;
       
   103     delete pC;
       
   104     
       
   105     return err;
       
   106     }
       
   107 
       
   108 // --------------------------------------------------------------------------------------------
       
   109 
       
   110 void CBluetooth::GenerateLoad(TBluetoothAttributes& aAttributes)
       
   111     {    
       
   112     CBTManager* btManager = 0;
       
   113     TRAPD(err, btManager = CBTManager::NewL(aAttributes));
       
   114     if (err == KErrNone) CActiveScheduler::Start();
       
   115     delete btManager;    
       
   116     }
       
   117 
       
   118 // --------------------------------------------------------------------------------------------
       
   119 
       
   120 void CBluetooth::Resume()
       
   121     {    
       
   122     CLoadBase::Resume();    
       
   123     iThread.Resume();
       
   124     }
       
   125 
       
   126 // --------------------------------------------------------------------------------------------
       
   127 
       
   128 void CBluetooth::Suspend()
       
   129     {
       
   130     CLoadBase::Suspend();    
       
   131     iThread.Suspend();
       
   132     }
       
   133 
       
   134 // --------------------------------------------------------------------------------------------
       
   135 
       
   136 void CBluetooth::SetPriority()
       
   137     {
       
   138     CLoadBase::SetPriority();    
       
   139     iThread.SetPriority(CLoadGenUtils::SettingItemToThreadPriority(iAttributes.iPriority));
       
   140     }
       
   141     
       
   142 // --------------------------------------------------------------------------------------------
       
   143 
       
   144 void CBluetooth::Close()
       
   145     {
       
   146     LOGSTRING2("LoadGen: CBluetooth::~CBluetooth() - State: %d", iState);
       
   147     if( iState != ELoadStateInvalid )
       
   148         {
       
   149         CLoadBase::Close();        
       
   150         // kill the thread immediately
       
   151         iThread.Kill(0);
       
   152         iThread.Close();
       
   153         }
       
   154     }
       
   155     
       
   156 // --------------------------------------------------------------------------------------------
       
   157 TPtrC CBluetooth::Description()
       
   158     {
       
   159     TBuf<256> buf;
       
   160     TBuf<16> prioBuf;
       
   161     CLoadGenUtils::SettingItemToThreadDescription(iAttributes.iPriority, prioBuf);
       
   162         
       
   163     _LIT(KBluetoothEntry, "[%d] Bluetooth prio=%S idle=%dms random=%d%%");
       
   164     buf.Format(KBluetoothEntry, iAttributes.iId, &prioBuf, iAttributes.iIdle, iAttributes.iRandomVariance);
       
   165        
       
   166     return TPtrC(buf);
       
   167     }
       
   168 
       
   169 // --------------------------------------------------------------------------------------------
       
   170 void CBluetooth::InitializeBluetoothL()
       
   171     {
       
   172     // Create central repository for checking and setting bluetooth power state    
       
   173     CRepository* btPowerStateCRepo = CRepository::NewL(KCRUidBluetoothPowerState);
       
   174     
       
   175     // Check if the BT is already turned on:    
       
   176     TInt btPowerState = 0;
       
   177     btPowerStateCRepo->Get(KBTPowerState, btPowerState);
       
   178     delete btPowerStateCRepo;
       
   179     btPowerStateCRepo = 0;
       
   180         
       
   181     if(btPowerState == EBTPowerOff)
       
   182         {
       
   183         // Switch bt power on:
       
   184         if (SetBTPowerState(ETrue) == EFalse)
       
   185             {
       
   186             LOGSTRING("LoadGen: Bluetooth could not be switched on");
       
   187             User::Leave(KErrNotReady);
       
   188             }
       
   189         }
       
   190     }
       
   191 
       
   192 // --------------------------------------------------------------------------------------------
       
   193 TBool CBluetooth::SetBTPowerState(TBool aState)
       
   194     {    
       
   195     // Ask user to turn on the bluetooth device:
       
   196     RNotifier btPowerNotifier;
       
   197     TInt errCode = btPowerNotifier.Connect();
       
   198     
       
   199     TBool retVal = errCode == KErrNone;
       
   200     
       
   201     if( errCode == KErrNone )
       
   202         {
       
   203         TPckgBuf<TBool> powerRequest(aState);
       
   204         TPckgBuf<TBool> powerReply(EFalse);
       
   205         TRequestStatus powerRequestStatus;
       
   206         btPowerNotifier.StartNotifierAndGetResponse(powerRequestStatus, 
       
   207                                                 KPowerModeSettingNotifierUid, 
       
   208                                                 powerRequest, 
       
   209                                                 powerReply);
       
   210         User::WaitForRequest(powerRequestStatus);
       
   211         btPowerNotifier.CancelNotifier(KPowerModeSettingNotifierUid);
       
   212         btPowerNotifier.Close();  
       
   213         if( powerReply() == EFalse )
       
   214             {
       
   215             LOGSTRING("LoadGen: Bluetooth activating cancelled.");
       
   216             }
       
   217         retVal = powerReply();
       
   218         }
       
   219     
       
   220     return retVal; 
       
   221     }
       
   222 
       
   223 // --------------------------------------------------------------------------------------------
       
   224 CBTManager* CBTManager::NewL(   TBluetoothAttributes& aAttrs)
       
   225     {
       
   226     CBTManager* self = new (ELeave) CBTManager( aAttrs);
       
   227     CleanupStack::PushL(self);
       
   228     self->ConstructL();
       
   229     CleanupStack::Pop(self);
       
   230     return self;
       
   231     }
       
   232  
       
   233 // --------------------------------------------------------------------------------------------
       
   234 CBTManager::~CBTManager()
       
   235     {    
       
   236     if (iPeriodicTimer)
       
   237         {
       
   238         iPeriodicTimer->Cancel();
       
   239         delete iPeriodicTimer;
       
   240         }    
       
   241     iHostResolver.Close();        
       
   242     Cancel();
       
   243     }
       
   244  
       
   245 // --------------------------------------------------------------------------------------------
       
   246 CBTManager::CBTManager( TBluetoothAttributes& aAttrs) 
       
   247 : 
       
   248 CActive(EPriorityStandard),
       
   249 iAttributes(aAttrs)
       
   250     {    
       
   251     }
       
   252  
       
   253 // --------------------------------------------------------------------------------------------
       
   254 void CBTManager::ConstructL()
       
   255     {
       
   256     // Socket server session initiation:
       
   257     User::LeaveIfError(iSocketServerHnd.Connect());    
       
   258 
       
   259     CActiveScheduler::Add(this);
       
   260     // set the status as pending
       
   261     iStatus = KRequestPending;
       
   262     SetActive();
       
   263         
       
   264     // set the death status pointer point to the request status of this ao
       
   265     iAttributes.iDeathStatus = &iStatus;
       
   266     
       
   267     // start timer    
       
   268     iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
   269     iPeriodicTimer->Start(KDefaultStart, KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this));
       
   270     }
       
   271 
       
   272 // --------------------------------------------------------------------------------------------
       
   273 void CBTManager::DoCancel()
       
   274     {
       
   275     LOGSTRING("LoadGen: CBTManager::DoCancel()");    
       
   276     }
       
   277 
       
   278 // --------------------------------------------------------------------------------------------
       
   279 void CBTManager::RunL()
       
   280     {    
       
   281     // request status has completed by the main thread meaning that we need to stop now
       
   282     CActiveScheduler::Stop();    
       
   283     }
       
   284 
       
   285 // --------------------------------------------------------------------------------------------
       
   286 TInt CBTManager::StartBTDeviceDiscovery()
       
   287     {
       
   288     TInt retVal = KErrNone; 
       
   289     RThread myThread;
       
   290            
       
   291     // Initialize host resolver      
       
   292     iHostResolver.Close();        
       
   293     TProtocolDesc protocolInfo;
       
   294     retVal = iSocketServerHnd.FindProtocol(KBTLinkManagerStr(), protocolInfo);
       
   295     if( retVal == KErrNone )
       
   296         {
       
   297         retVal = iHostResolver.Open(iSocketServerHnd, protocolInfo.iAddrFamily, protocolInfo.iProtocol);        
       
   298         }
       
   299     
       
   300     if( retVal == KErrNone )
       
   301         {
       
   302         TInquirySockAddr socketAddress;
       
   303         socketAddress.SetIAC( KGIAC );
       
   304         socketAddress.SetAction(KHostResInquiry|KHostResName|KHostResIgnoreCache);
       
   305         retVal = iHostResolver.GetByAddress(socketAddress, iBTNameEntry);        
       
   306         // Loop all discovered devices:
       
   307         while( retVal == KErrNone )        
       
   308             {
       
   309             LOGSTRING3("LoadGen: Thread %S found device: %S", &(myThread.Name()), &(iBTNameEntry().iName) );
       
   310             // Get next device
       
   311             retVal = iHostResolver.Next(iBTNameEntry);            
       
   312             }
       
   313         }
       
   314     
       
   315     // KErrEof is returned when no more devices to loop
       
   316     if( retVal == KErrEof )
       
   317         {
       
   318         retVal = KErrNone;
       
   319         }
       
   320             
       
   321     iPeriodicTimer->Start( CLoadGenUtils::MilliSecondsToMicroSeconds( iAttributes.iIdle,
       
   322                         iAttributes.iRandomVariance ), KDefaultPeriod, 
       
   323                         TCallBack( PeriodicTimerCallBack, this ) );
       
   324 
       
   325     return retVal;
       
   326     }
       
   327 
       
   328 // --------------------------------------------------------------------------------------------
       
   329 
       
   330 TInt CBTManager::PeriodicTimerCallBack(TAny* aAny)
       
   331     {        
       
   332     CBTManager* self = static_cast<CBTManager*>( aAny );
       
   333     self->iPeriodicTimer->Cancel();
       
   334     // Perform device discovery:
       
   335     self->StartBTDeviceDiscovery();
       
   336     return KErrNone;
       
   337     }
       
   338 
       
   339 // End of File