qtmobility/src/location/qmlbackendmonitorcreatetriggerao_s60.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qmlbackendmonitorao_s60_p.h"
       
    43 #include "qgeoareamonitor_s60_p.h"
       
    44 #include "qmlbackendmonitorinfo_s60_p.h"
       
    45 #include "qmlbackendmonitorcreatetriggerao_s60_p.h"
       
    46 
       
    47 QTM_BEGIN_NAMESPACE
       
    48 
       
    49 //Sets the radius of the monitoring area, to  aRadius.If the Radius is less than the MinimumTriggerAreaSize(),
       
    50 //then aRadius will be set to MinimumTriggerAreaSize() supported by the LBT implementation.If the aRadius is
       
    51 //greater than the MinimumTriggerAreaSize() it is unaltered.
       
    52 TInt QMLBackendMonitorCreateTriggerAO::getRadius(qreal& aRadius)
       
    53 {
       
    54     TInt ret = KErrNone;
       
    55 
       
    56     qreal minimumRadius;
       
    57 
       
    58     TLbtTriggeringSystemSettings triggerSettings;
       
    59 
       
    60     //gets the triggering System Setting
       
    61     TRAP(ret, iLbt.GetTriggeringSystemSettingsL(triggerSettings));
       
    62 
       
    63     if (ret != KErrNone)
       
    64         return ret;
       
    65 
       
    66     minimumRadius = triggerSettings.MinimumTriggerAreaSize();
       
    67 
       
    68     if (aRadius < minimumRadius)
       
    69         aRadius = minimumRadius;
       
    70 
       
    71     return ret;
       
    72 }
       
    73 
       
    74 //creates a trigger of type aType(EntryTrigger/ExitTrigger), with the coordinates and radius as
       
    75 //supplied in the argument
       
    76 bool  QMLBackendMonitorCreateTriggerAO::InitializeTrigger(QGeoAreaMonitorS60* aParent ,
       
    77         enTriggerType aType,
       
    78         TCoordinate& aCoordinate,
       
    79         qreal& aRadius)
       
    80 {
       
    81     TInt ret = KErrGeneral;
       
    82 
       
    83     TLbtTriggerId triggerID = NULL;
       
    84 
       
    85     //try retrieving the trigger information from the linked list corresponding to the aParent and aType
       
    86     CMonitorTriggerInfo* triggerInfo = iTriggerMonitorInfo->getMonitorTriggerInfo(aParent, aType);
       
    87 
       
    88     //if no triggerinfo available in the linked list
       
    89     if (triggerInfo ==  NULL) {
       
    90         //Define the triggering area
       
    91         CLbtGeoCircle* trigArea = NULL;
       
    92 
       
    93         TRAP(ret, trigArea = CLbtGeoCircle::NewL(
       
    94                                  aCoordinate ,//center coordinate
       
    95                                  aRadius               //radius in meters. If
       
    96                                  //NaN is used, Location
       
    97                                  //Triggering Server will
       
    98                                  //use minimal size of trigger
       
    99                                  //area as the radius of the
       
   100                                  //trigger
       
   101                              ));
       
   102 
       
   103         if ((ret != KErrNone) || !trigArea)
       
   104             return FALSE;
       
   105 
       
   106         CleanupStack::PushL(trigArea);
       
   107 
       
   108         CLbtTriggerConditionArea* cond;
       
   109 
       
   110         if (aType == EntryTrigger) {
       
   111             //2: Construct a entry type of trigger condition
       
   112             TRAP(ret, cond = CLbtTriggerConditionArea::NewL(
       
   113                                  trigArea,
       
   114                                  CLbtTriggerConditionArea::EFireOnEnter
       
   115                              ));
       
   116         } else if (aType == ExitTrigger) {
       
   117             TRAP(ret, cond = CLbtTriggerConditionArea::NewL(
       
   118                                  trigArea,
       
   119                                  CLbtTriggerConditionArea::EFireOnExit
       
   120                              ));
       
   121         }
       
   122 
       
   123 
       
   124         if ((ret != KErrNone) || !cond) {
       
   125             CleanupStack::PopAndDestroy(trigArea);
       
   126             return FALSE;
       
   127         }
       
   128 
       
   129         CleanupStack::Pop(trigArea);   //ownership of trigArea is transferred.
       
   130 
       
   131         CleanupStack::PushL(cond);
       
   132 
       
   133         RRequestorStack reqStack;
       
   134 
       
   135         CleanupClosePushL(reqStack);
       
   136 
       
   137         //trigger name.
       
   138         _LIT(KMyTriggerName, "EntryTrigger");
       
   139         TDesC triggerName(KMyTriggerName);
       
   140 
       
   141         if (aType == ExitTrigger) {
       
   142             _LIT(KMyTriggerName, "ExitTrigger");
       
   143             triggerName = KMyTriggerName;
       
   144         }
       
   145 
       
   146         //Construct requestor
       
   147         _LIT(KMyRequestorName, "QTLBTBackend");   //Application name used as requestor identifier
       
   148 
       
   149         CRequestor *req = NULL;
       
   150 
       
   151         TRAP(ret, req = CRequestor::NewL(
       
   152                             CRequestorBase::ERequestorService,
       
   153                             CRequestorBase::EFormatApplication,
       
   154                             KMyRequestorName
       
   155                         ));
       
   156 
       
   157         if ((ret != KErrNone) || !req) {
       
   158             CleanupStack::PopAndDestroy(&reqStack);
       
   159             CleanupStack::PopAndDestroy(cond);
       
   160             return FALSE;
       
   161         }
       
   162 
       
   163         CleanupStack::PushL(req);
       
   164 
       
   165         TRAP(ret, reqStack.AppendL(req));
       
   166 
       
   167         CleanupStack::Pop(req);
       
   168 
       
   169         if (ret != KErrNone) {
       
   170             CleanupStack::PopAndDestroy(&reqStack);
       
   171             CleanupStack::PopAndDestroy(cond);
       
   172             return FALSE;
       
   173         }
       
   174 
       
   175         TUid managerUid = TUid::Uid(0);
       
   176 
       
   177         CLbtSessionTrigger* trig = NULL;
       
   178 
       
   179         TRAP(ret, trig =  CLbtSessionTrigger::NewL(
       
   180                               triggerName,
       
   181                               CLbtTriggerEntry::EStateDisabled,
       
   182                               reqStack,
       
   183                               managerUid,
       
   184                               cond
       
   185                           ));
       
   186 
       
   187         CleanupStack::PopAndDestroy(&reqStack);
       
   188 
       
   189         CleanupStack::Pop(cond);
       
   190 
       
   191         trig->SetTimeToRearm(0);
       
   192 
       
   193         if ((ret != KErrNone) || (!trig)) {
       
   194             CleanupStack::PopAndDestroy(cond);
       
   195             return FALSE;
       
   196         }
       
   197 
       
   198         CleanupStack::PushL(trig);
       
   199 
       
   200         //iLbt.CreateTrigger(*trig, triggerID, ETrue, iStatus);
       
   201 
       
   202         //CleanupStack::PopAndDestroy(trig );
       
   203 
       
   204         TRAP(ret, iActiveSchedulerwait = new(ELeave) CActiveSchedulerWait);
       
   205 
       
   206         if ((ret != KErrNone) || !iActiveSchedulerwait) {
       
   207             CleanupStack::PopAndDestroy(trig);
       
   208             return FALSE;
       
   209         }
       
   210 
       
   211         iTriggerCreation = FALSE;
       
   212 
       
   213         //create a trigger asynchronously
       
   214         iLbt.CreateTrigger(*trig, triggerID, ETrue, iStatus);
       
   215 
       
   216         SetActive();
       
   217 
       
   218         //wait till the iActiveSchedularwait->AsyncStop() is called in RunL
       
   219         iActiveSchedulerwait->Start();
       
   220 
       
   221         delete iActiveSchedulerwait;
       
   222 
       
   223         CleanupStack::PopAndDestroy(trig);
       
   224 
       
   225         //if the trigger creation is successful, add the triggerinfo to the linked list
       
   226         if (iTriggerCreation == TRUE)
       
   227             iTriggerMonitorInfo->addMonitorTriggerInfo(aParent, triggerID, aType);
       
   228 
       
   229         delete req;
       
   230 
       
   231         return iTriggerCreation;
       
   232     } else {     //triggerinfo available in the linked list
       
   233 
       
   234         CLbtSessionTrigger* trig = NULL;
       
   235 
       
   236         //Define the triggering area
       
   237         CLbtGeoCircle* trigArea = NULL;
       
   238 
       
   239         TRAP(ret, trigArea = CLbtGeoCircle::NewL(
       
   240                                  aCoordinate ,   //center coordinate
       
   241                                  aRadius          //radius in meters. If
       
   242                                  //NaN is used, Location
       
   243                                  //Triggering Server will
       
   244                                  //use minimal size of trigger
       
   245                                  //area as the radius of the
       
   246                                  //trigger
       
   247                              ));
       
   248 
       
   249         if ((ret != KErrNone) || (!trigArea)) {
       
   250 
       
   251             return FALSE;
       
   252         }
       
   253 
       
   254         CleanupStack::PushL(trigArea);
       
   255 
       
   256         //2: Construct a entry type of trigger condition
       
   257         CLbtTriggerConditionArea* cond = NULL;
       
   258 
       
   259         if (aType == EntryTrigger) {
       
   260             //2: Construct a entry type of trigger condition
       
   261             TRAP(ret, cond = CLbtTriggerConditionArea::NewL(
       
   262                                  trigArea,
       
   263                                  CLbtTriggerConditionArea::EFireOnEnter
       
   264                              ));
       
   265         } else if (aType == ExitTrigger) {
       
   266             TRAP(ret, cond = CLbtTriggerConditionArea::NewL(
       
   267                                  trigArea,
       
   268                                  CLbtTriggerConditionArea::EFireOnExit
       
   269                              ));
       
   270         }
       
   271 
       
   272 
       
   273         if ((ret != KErrNone) || !cond) {
       
   274             CleanupStack::PopAndDestroy(trigArea);
       
   275             return FALSE;
       
   276         }
       
   277 
       
   278         CleanupStack::Pop(trigArea);   //ownership of trigArea is transferred.
       
   279 
       
   280         CleanupStack::PushL(cond);
       
   281 
       
   282         //create a session trigger
       
   283         TRAP(ret, trig = CLbtSessionTrigger::NewL());
       
   284 
       
   285         if ((ret != KErrNone) || (!trig)) {
       
   286             CleanupStack::PopAndDestroy(cond);
       
   287             return FALSE;
       
   288         }
       
   289 
       
   290         //set the condition for the trigger
       
   291         trig->SetCondition(cond);
       
   292 
       
   293         CleanupStack::Pop(cond);
       
   294 
       
   295         CleanupStack::PushL(trig);
       
   296 
       
   297         //set the trigger ID
       
   298         trig->SetId(triggerInfo->iTriggerID);
       
   299 
       
   300         iLbt.SetTriggerStateL(triggerInfo->iTriggerID, CLbtTriggerEntry::EStateDisabled, ELbtTrue);
       
   301 
       
   302         //update the trigger with the new condition in LBT server
       
   303         TRAP(ret, iLbt.UpdateTriggerL(*trig, CLbtTriggerEntry::EAttributeCondition, ELbtTrue));
       
   304 
       
   305         CleanupStack::PopAndDestroy(trig);
       
   306 
       
   307 
       
   308         if (ret != KErrNone) {
       
   309             return FALSE;;
       
   310         }
       
   311 
       
   312         return TRUE;
       
   313     }
       
   314 }
       
   315 
       
   316 
       
   317 QMLBackendMonitorCreateTriggerAO::~QMLBackendMonitorCreateTriggerAO()
       
   318 {
       
   319     Cancel();
       
   320     iLbt.Close();   //closes the subsession
       
   321 }
       
   322 
       
   323 
       
   324 void  QMLBackendMonitorCreateTriggerAO::DoCancel()
       
   325 {
       
   326     if (!IsActive()) {
       
   327         iActiveSchedulerwait->AsyncStop();
       
   328     }
       
   329 }
       
   330 
       
   331 void QMLBackendMonitorCreateTriggerAO::RunL()
       
   332 {
       
   333     switch (iStatus.Int()) {
       
   334         case KErrNone :
       
   335             iTriggerCreation = TRUE;
       
   336             break;
       
   337         default :
       
   338             break;
       
   339     }
       
   340     //stops the AO, waiting in the iActiveSchedulerwait->Start()
       
   341     iActiveSchedulerwait->AsyncStop();
       
   342 }
       
   343 
       
   344 QMLBackendMonitorCreateTriggerAO* QMLBackendMonitorCreateTriggerAO::NewL(QGeoAreaMonitorS60* aParent , RLbtServer &aLbt)
       
   345 {
       
   346 
       
   347     QMLBackendMonitorCreateTriggerAO* self = QMLBackendMonitorCreateTriggerAO::
       
   348             NewLC(aParent, aLbt);
       
   349     CleanupStack::Pop();
       
   350 
       
   351     return self;
       
   352 }
       
   353 
       
   354 QMLBackendMonitorCreateTriggerAO* QMLBackendMonitorCreateTriggerAO::NewLC(QGeoAreaMonitorS60* aParent , RLbtServer &aLbtServer)
       
   355 {
       
   356     QMLBackendMonitorCreateTriggerAO *self = new(ELeave) QMLBackendMonitorCreateTriggerAO;
       
   357     CleanupStack::PushL(self);
       
   358     self->ConstructL(aLbtServer);
       
   359     if (!self->isValid()) {
       
   360         delete self;
       
   361         self = NULL;
       
   362     }
       
   363     return self;
       
   364 }
       
   365 
       
   366 void QMLBackendMonitorCreateTriggerAO::ConstructL(RLbtServer &aLbtServ)
       
   367 {
       
   368     if (iLbt.Open(aLbtServ) == KErrNone) {      //opens the subseesion
       
   369         subsessionCreated = TRUE;
       
   370         //get the singleton object of CBackendMonitorInfo class
       
   371         iTriggerMonitorInfo = CBackendMonitorInfo::NewL();
       
   372     }
       
   373 }
       
   374 
       
   375 QMLBackendMonitorCreateTriggerAO::QMLBackendMonitorCreateTriggerAO()
       
   376         : CActive(EPriorityStandard), // Standard priority
       
   377         subsessionCreated(FALSE), iTriggerCreation(FALSE)
       
   378 {
       
   379     CActiveScheduler::Add(this);        //add AO to the Schedular
       
   380 }
       
   381 
       
   382 //Enables/Disables the trigger state depending on the aStatus
       
   383 void QMLBackendMonitorCreateTriggerAO::SetTriggerState(QGeoAreaMonitorS60* aParent, enTriggerType aType, bool aStatus)
       
   384 {
       
   385     //retrieve the triggerinfo from the linked list from the supplied aPrent and aType
       
   386     CMonitorTriggerInfo* triggerInfo = iTriggerMonitorInfo->getMonitorTriggerInfo(aParent, aType);
       
   387 
       
   388     if (aStatus == true) {
       
   389         TRAPD(err, iLbt.SetTriggerStateL(triggerInfo->iTriggerID, CLbtTriggerEntry::EStateEnabled, ELbtTrue));
       
   390     } else {
       
   391         TRAPD(err, iLbt.SetTriggerStateL(triggerInfo->iTriggerID, CLbtTriggerEntry::EStateDisabled, ELbtTrue));
       
   392     }
       
   393 }
       
   394 
       
   395 //checks whether trigger is Initialized. The trigger entry corresponding to the aParent and aType is
       
   396 //searched in the linked list
       
   397 bool QMLBackendMonitorCreateTriggerAO::isTriggerInitialized(QGeoAreaMonitorS60* aParent, enTriggerType aType)
       
   398 {
       
   399     CMonitorTriggerInfo* triggerInfo = iTriggerMonitorInfo->getMonitorTriggerInfo(aParent, aType);
       
   400 
       
   401     return (triggerInfo != NULL) ? TRUE : FALSE;
       
   402 }
       
   403 
       
   404 QTM_END_NAMESPACE