javaextensions/sensor/src.s60/csensorbase.cpp
branchRCL_3
changeset 19 04becd199f91
child 50 023eef975703
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 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:  Base class for sensors
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "csensorbase.h"
       
    20 #include "csensorlimitcondition.h"
       
    21 #include "csensorrangecondition.h"
       
    22 #include "sensorlistener.h"
       
    23 #include "logger.h"
       
    24 #include <bautils.h>
       
    25 #include <barsc.h>
       
    26 
       
    27 const TUint JavaUpperTimeFor1970 = 14474675;
       
    28 const TUint JavaLowerTimeFor1970 = 254771200;
       
    29 
       
    30 CSensorBase::~CSensorBase()
       
    31 {
       
    32     JELOG2(ESensor);
       
    33     iConditions.ResetAndDestroy();
       
    34     iConditions.Close();
       
    35     Cancel();
       
    36     delete iActiveScheduler;
       
    37     iRunnerThread.Close();
       
    38     iMethodArray.Close();
       
    39 }
       
    40 CSensorBase::CSensorBase() : CActive(EPriorityHigh),
       
    41         iListeningType(0)
       
    42 {
       
    43     JELOG2(ESensor);
       
    44 }
       
    45 
       
    46 void CSensorBase::ConstructL()
       
    47 {
       
    48     JELOG2(ESensor);
       
    49     User::LeaveIfError(iRunnerThread.Open(RThread().Id()));
       
    50     if (!CActiveScheduler::Current())
       
    51     {
       
    52         iActiveScheduler = new(ELeave) CActiveScheduler();
       
    53         CActiveScheduler::Install(iActiveScheduler);
       
    54     }
       
    55     CActiveScheduler::Add(this);
       
    56 }
       
    57 
       
    58 TInt64 CSensorBase::TimeStamp(const TTime& aTime)
       
    59 {
       
    60     JELOG2(ESensor);
       
    61     // Create a TTime object that represents the Java Date
       
    62     // 'epoch' time of 00:00, 1 Jan 1970
       
    63     TInt64 javaEpocTimeNum =
       
    64         MAKE_TINT64(JavaUpperTimeFor1970, JavaLowerTimeFor1970);
       
    65     TTime javaEpochTime(javaEpocTimeNum);
       
    66     // Find difference in microseconds between 'epoch' and EPOC
       
    67     // date and adjust to milliseconds
       
    68     TTimeIntervalMicroSeconds microInterval =
       
    69         aTime.MicroSecondsFrom(javaEpochTime);
       
    70     TInt64 intervalNum = microInterval.Int64();
       
    71     intervalNum /= 1000;
       
    72     return intervalNum;
       
    73 }
       
    74 
       
    75 void CSensorBase::StartServer()
       
    76 {
       
    77     JELOG2(ESensor);
       
    78     CActiveScheduler::Start();
       
    79 }
       
    80 
       
    81 void CSensorBase::StopServer()
       
    82 {
       
    83     JELOG2(ESensor);
       
    84     TMethod method(TMethod::EStopServer);
       
    85     AsyncCallback(method);
       
    86 }
       
    87 
       
    88 int CSensorBase::StartDataListening(SensorData** aData,
       
    89                                     int aBufferSize,
       
    90                                     long aBufferingPeriod,
       
    91                                     bool aTimestampsIncluded,
       
    92                                     bool aValiditiesIncluded,
       
    93                                     bool aIsOneShot)
       
    94 {
       
    95     JELOG2(ESensor);
       
    96     TMethod method(TMethod::EStartDataListening);
       
    97     method.SetSensorData(aData);
       
    98     method.SetBufferSize(aBufferSize);
       
    99     method.SetBufferingPeriod(aBufferingPeriod);
       
   100     method.SetTimestampsIncluded(aTimestampsIncluded);
       
   101     method.SetValiditiesIncluded(aValiditiesIncluded);
       
   102     method.SetIsOneShot(aIsOneShot);
       
   103     AsyncCallback(method);
       
   104     return KErrNone;
       
   105 }
       
   106 
       
   107 int CSensorBase::StartConditionListening(int aJavaConditionEval)
       
   108 {
       
   109     JELOG2(ESensor);
       
   110     TMethod method(TMethod::EStartConditionListening);
       
   111     method.SetJavaConditionEval(aJavaConditionEval);
       
   112     AsyncCallback(method);
       
   113     return KErrNone;
       
   114 }
       
   115 
       
   116 int CSensorBase::CancelDataListening()
       
   117 {
       
   118     JELOG2(ESensor);
       
   119     TMethod method(TMethod::ECancelDataListening);
       
   120     AsyncCallback(method);
       
   121     return KErrNone;
       
   122 }
       
   123 
       
   124 int CSensorBase::StopConditionListening()
       
   125 {
       
   126     JELOG2(ESensor);
       
   127     TMethod method(TMethod::EStopConditionListening);
       
   128     AsyncCallback(method);
       
   129     return KErrNone;
       
   130 }
       
   131 
       
   132 void CSensorBase::CloseChannel()
       
   133 {
       
   134     JELOG2(ESensor);
       
   135     TMethod method(TMethod::ECloseChannel);
       
   136     AsyncCallback(method);
       
   137 }
       
   138 
       
   139 void CSensorBase::AsyncCallback(TMethod aMethod)
       
   140 {
       
   141     JELOG2(ESensor);
       
   142     iMethodArray.Append(aMethod);
       
   143 
       
   144     // If there is old request ongoing, wait it for completion
       
   145     if (iMethodArray.Count() > 1)
       
   146     {
       
   147         return;
       
   148     }
       
   149     iStatus = KRequestPending;
       
   150     TRequestStatus* status = &iStatus;
       
   151     SetActive();
       
   152     iRunnerThread.RequestComplete(status, KErrNone);
       
   153 }
       
   154 
       
   155 void CSensorBase::RunL()
       
   156 {
       
   157     JELOG2(ESensor);
       
   158     // RunL should never be running if there is no method in array
       
   159     __ASSERT_DEBUG(iMethodArray.Count() > 0, User::Invariant());
       
   160     TMethod method = iMethodArray[0];
       
   161 
       
   162     switch (method.GetMethodType())
       
   163     {
       
   164     case TMethod::EStartDataListening:
       
   165     {
       
   166         iData = method.GetSensorData();
       
   167         iBufferSize = method.GetBufferSize();
       
   168         iBufferingPeriod = 1000 * method.GetBufferingPeriod();
       
   169         iIsOneShot = method.GetIsOneShot();
       
   170         iTimestampsIncluded = method.GetTimestampsIncluded();
       
   171         iValiditiesIncluded = method.GetValiditiesIncluded();
       
   172 
       
   173         StartDataListeningL();
       
   174         break;
       
   175     }
       
   176     case TMethod::EStartConditionListening:
       
   177     {
       
   178         iJavaConditionEval = method.GetJavaConditionEval();
       
   179         StartConditionListeningL();
       
   180         break;
       
   181     }
       
   182     case TMethod::EStopConditionListening:
       
   183     {
       
   184         StopConditionListeningL();
       
   185         break;
       
   186     }
       
   187     case TMethod::ECancelDataListening:
       
   188     {
       
   189         CancelDataListeningL();
       
   190         break;
       
   191     }
       
   192     case TMethod::ECloseChannel:
       
   193     {
       
   194         CloseChannelL();
       
   195         break;
       
   196     }
       
   197     case TMethod::EStopServer:
       
   198     {
       
   199         CActiveScheduler::Stop();
       
   200         break;
       
   201     }
       
   202     case TMethod::ERemoveCondition:
       
   203     {
       
   204         RemoveConditionL(method.GetConditionToRemove());
       
   205         break;
       
   206     }
       
   207     default:
       
   208     {
       
   209         break;
       
   210     }
       
   211     }
       
   212     // If there is still methods left in array, then request new run
       
   213     if (iMethodArray.Count() > 1)
       
   214     {
       
   215         iStatus = KRequestPending;
       
   216         TRequestStatus* status = &iStatus;
       
   217         SetActive();
       
   218         iRunnerThread.RequestComplete(status, KErrNone);
       
   219     }
       
   220     iMethodArray.Remove(0);
       
   221 }
       
   222 
       
   223 void CSensorBase::DoCancel()
       
   224 {
       
   225     JELOG2(ESensor);
       
   226 }
       
   227 
       
   228 TBool CSensorBase::EvaluateConditions(TReal aValue, TInt aChannelId)
       
   229 {
       
   230     JELOG2(ESensor);
       
   231     TReal currentValue = InterpretValue(aValue);
       
   232     TBool matched = EFalse;
       
   233     TTime time;
       
   234     time.UniversalTime();
       
   235     TInt64 javaTime = TimeStamp(time);
       
   236     // Condition array is iterated backwards so we can
       
   237     // remove matched elements at the same time
       
   238     for (TInt i = iConditions.Count() - 1; i >= 0; i--)
       
   239     {
       
   240         if (iConditions[ i ]->Evaluate(currentValue, aChannelId))
       
   241         {
       
   242             matched = ETrue;
       
   243             CSensorConditionBase *condition = iConditions[i];
       
   244             iConditions.Remove(i);
       
   245             if (iSensorListener)
       
   246             {
       
   247                 iSensorListener->ConditionMet(
       
   248                     condition,
       
   249                     condition->GetChannelId(),
       
   250                     currentValue,
       
   251                     javaTime);
       
   252             }
       
   253             delete condition;
       
   254         }
       
   255     }
       
   256 
       
   257     // Also send all values separately if we have java side custom conditions
       
   258     if (iJavaConditionEval)
       
   259     {
       
   260         iSensorListener->ConditionMet(0, aChannelId, currentValue, javaTime);
       
   261     }
       
   262     return matched;
       
   263 }
       
   264 
       
   265 int CSensorBase::AddCondition(
       
   266     void** aHandle,
       
   267     int aChannelId,
       
   268     double aLowerLimit,
       
   269     double aUpperLimit,
       
   270     int aLowerOp,
       
   271     int aUpperOp)
       
   272 {
       
   273     JELOG2(ESensor);
       
   274     TRAPD(err, AddConditionL(aHandle,    aChannelId,
       
   275                              aLowerLimit, aUpperLimit, aLowerOp, aUpperOp));
       
   276     return err;
       
   277 }
       
   278 
       
   279 void CSensorBase::AddConditionL(void** aHandle, int aChannelId,
       
   280                                 double aLowerLimit, double aUpperLimit, int aLowerOp,
       
   281                                 int aUpperOp)
       
   282 {
       
   283     JELOG2(ESensor);
       
   284     CSensorConditionBase* condition;
       
   285     if (aUpperOp < 0)
       
   286     {
       
   287         condition = new(ELeave)CSensorLimitCondition(aChannelId,
       
   288                 aLowerLimit, aLowerOp);
       
   289     }
       
   290     else
       
   291     {
       
   292         condition = new(ELeave)CSensorRangeCondition(aChannelId,
       
   293                 aLowerLimit, aUpperLimit, aLowerOp, aUpperOp);
       
   294     }
       
   295     CleanupStack::PushL(condition);
       
   296     iConditions.AppendL(condition);
       
   297     CleanupStack::Pop();
       
   298     *aHandle = condition;
       
   299 }
       
   300 
       
   301 int CSensorBase::RemoveCondition(void* aHandle)
       
   302 {
       
   303     JELOG2(ESensor);
       
   304     TMethod method(TMethod::ERemoveCondition);
       
   305     method.SetConditionToRemove(aHandle);
       
   306     AsyncCallback(method);
       
   307     return KErrNone;
       
   308 }
       
   309 
       
   310 void CSensorBase::RemoveConditionL(
       
   311     void* aHandle)
       
   312 {
       
   313     JELOG2(ESensor);
       
   314     CSensorConditionBase* condition = (CSensorConditionBase*) aHandle;
       
   315     TInt index = iConditions.Find(condition);
       
   316     if (index < KErrNone)
       
   317     {
       
   318         return;
       
   319     }
       
   320     iConditions.Remove(index);
       
   321     delete condition;
       
   322 }
       
   323 
       
   324