sensorservices/sensorserver/src/util/sensrvchannelconditionsetimpl.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Channel condition set object implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "sensrvchannelconditionsetimpl.h"
       
    20 #include "sensrvchannelconditionimpl.h"
       
    21 #include "sensrvtrace.h"
       
    22 #include "sensrvdefines.h"
       
    23 
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // C++ constructor
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 CSensrvChannelConditionSetImpl::CSensrvChannelConditionSetImpl(
       
    30     TSensrvConditionSetType aConditionSetType)
       
    31     : iSetType(aConditionSetType),
       
    32       iJustAdded(ETrue)
       
    33     {
       
    34     }
       
    35 
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // 2nd phase of construction
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 void CSensrvChannelConditionSetImpl::ConstructL()
       
    42     {
       
    43     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::ConstructL()" ) ) );
       
    44     }
       
    45 
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // Destructor
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CSensrvChannelConditionSetImpl::~CSensrvChannelConditionSetImpl()
       
    52     {
       
    53     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::~CSensrvChannelConditionSetImpl()" ) ) );
       
    54     
       
    55     iConditionList.ResetAndDestroy();
       
    56     delete iMetData;
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // Return condition set type
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 TSensrvConditionSetType CSensrvChannelConditionSetImpl::ConditionSetType() const
       
    64     {
       
    65     return iSetType;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // Add channel condition to the condition set.
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 void CSensrvChannelConditionSetImpl::AddChannelConditionL(CSensrvChannelCondition* aChannelCondition)
       
    73     {
       
    74     API_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::AddChannelConditionL(aChannelCondition: 0x%x)" ), aChannelCondition ) );
       
    75     
       
    76     if(aChannelCondition)
       
    77         {
       
    78         TBool newOperatorGreaterThan(EFalse);        
       
    79         if (   aChannelCondition->ConditionOperator() == ESensrvOperatorGreaterThan
       
    80             || aChannelCondition->ConditionOperator() == ESensrvOperatorGreaterThanOrEquals )
       
    81             {
       
    82             newOperatorGreaterThan = ETrue;
       
    83             }
       
    84         
       
    85         // Check that this index doesn't already have a condition, or if it has, 
       
    86         // it is a matching range condition.
       
    87         TInt indexCount(0);
       
    88         TInt existingIndex(KErrNotFound);
       
    89         for(TInt i=0; i< iConditionList.Count(); i++)
       
    90             {
       
    91             TBool oldOperatorGreaterThan(EFalse);
       
    92             if (   iConditionList[i]->ConditionOperator() == ESensrvOperatorGreaterThan
       
    93                 || iConditionList[i]->ConditionOperator() == ESensrvOperatorGreaterThanOrEquals )
       
    94                 {
       
    95                 oldOperatorGreaterThan = ETrue;
       
    96                 }
       
    97 
       
    98             if ( iConditionList[i]->ConditionItemIndex() == aChannelCondition->ConditionItemIndex() )
       
    99                 {
       
   100                 indexCount++;
       
   101                 existingIndex = i;
       
   102                 
       
   103                 if (
       
   104                     // Condition types of new and old condition for same index 
       
   105                     // must be a matched pair of range conditions 
       
   106                     // Note: Custom condition types cannot have multi-part conditions.
       
   107                     //       If custom type requires multiple checks for single condition,
       
   108                     //       it needs to define the condition value accordingly.
       
   109                     !( (   iConditionList[i]->ConditionType() == ESensrvRangeConditionLowerLimit
       
   110                         && aChannelCondition->ConditionType() == ESensrvRangeConditionUpperLimit ) 
       
   111                       || ( iConditionList[i]->ConditionType() == ESensrvRangeConditionUpperLimit
       
   112                         && aChannelCondition->ConditionType() == ESensrvRangeConditionLowerLimit ) )
       
   113                     // Operators must be different in range conditions
       
   114                     || oldOperatorGreaterThan == newOperatorGreaterThan
       
   115                     // Maximum of KSensrvMaxConditionIndexCount (=2) conditions per index    
       
   116                     || indexCount >= KSensrvMaxConditionIndexCount
       
   117                    ) 
       
   118                     {
       
   119                     ERROR_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::AddChannelConditionL - ERROR: Mismatch with existing condition for same index)" ) ) );
       
   120                     User::Leave(KErrArgument);
       
   121                     }
       
   122                 }
       
   123             } // end for
       
   124 
       
   125         if (!indexCount)
       
   126             {
       
   127             // New index, just append
       
   128             iConditionList.AppendL(aChannelCondition);
       
   129             }
       
   130         else 
       
   131             {
       
   132             // Second part of range index, always insert after existing condition.
       
   133             // Always order range conditions so that lower limit is first
       
   134             if ( aChannelCondition->ConditionType() == ESensrvRangeConditionLowerLimit )
       
   135                 {
       
   136                 iConditionList.InsertL(aChannelCondition, existingIndex);
       
   137                 }
       
   138             else if ( aChannelCondition->ConditionType() == ESensrvRangeConditionUpperLimit )
       
   139                 {
       
   140                 iConditionList.InsertL(aChannelCondition, existingIndex+1);
       
   141                 }
       
   142             else
       
   143                 {
       
   144                 // Never gets here, unless there is an error
       
   145                 User::Panic(KSensrvPanicCategory, ESensrvPanicUnexpectedBranch);
       
   146                 }
       
   147             }
       
   148         }
       
   149     else
       
   150         {
       
   151         ERROR_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::AddChannelConditionL - ERROR: NULL condition)" ) ) );
       
   152         User::Leave(KErrArgument);
       
   153         }
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // Get all channel conditions added to this set.
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 const RSensrvChannelConditionList& CSensrvChannelConditionSetImpl::AllConditions() const
       
   161     {
       
   162     API_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::AllConditions()" ) ) );
       
   163     
       
   164     return iConditionList;
       
   165     }
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // Check if set is complete.
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 TBool CSensrvChannelConditionSetImpl::IsComplete() const
       
   172     {
       
   173     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::IsComplete()" ) ) );
       
   174     
       
   175     TBool complete(iConditionList.Count() > 0 ? ETrue : EFalse);
       
   176     TInt unmatchedIndex(KErrNotFound); 
       
   177     
       
   178     for ( TInt i=0; complete && i < iConditionList.Count(); i++ )
       
   179         {
       
   180         // If previous index was unmatched index, compare it to current index.
       
   181         // No need to check if current condition is range condition, as it is not
       
   182         // possible to add conditions with duplicate indexes unless they are 
       
   183         // range conditions.
       
   184         if ( unmatchedIndex != KErrNotFound )
       
   185             {
       
   186             if (   iConditionList[i]->ConditionItemIndex() != unmatchedIndex
       
   187                 || iConditionList[i]->ConditionType() != ESensrvRangeConditionUpperLimit )
       
   188                 {
       
   189                 complete = EFalse;
       
   190                 }
       
   191             else
       
   192                 {
       
   193                 unmatchedIndex = KErrNotFound;
       
   194                 }
       
   195             }
       
   196         else if ( iConditionList[i]->ConditionType() == ESensrvRangeConditionLowerLimit )
       
   197             {
       
   198             // Next one in array should also be ESensrvRangeConditionUpperLimit with same index,
       
   199             // as range conditions are always inserted next to each other, with lower limit
       
   200             // being first one.
       
   201             unmatchedIndex = iConditionList[i]->ConditionItemIndex(); 
       
   202             }
       
   203         else if ( iConditionList[i]->ConditionType() == ESensrvRangeConditionUpperLimit )
       
   204             {
       
   205             complete = EFalse;
       
   206             }
       
   207         else
       
   208             {
       
   209             // Do nothing, 
       
   210             }
       
   211         }
       
   212 
       
   213     // Not complete, if last condition was unmatched
       
   214     if ( unmatchedIndex != KErrNotFound )
       
   215         {
       
   216         complete = EFalse;
       
   217         }
       
   218         
       
   219     return complete;        
       
   220     }
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // Serializes this set to a single buffer that can be sent to server
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 HBufC8* CSensrvChannelConditionSetImpl::SerializeLC() const
       
   227     {
       
   228     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::SerializeLC()" ) ) );
       
   229 
       
   230     TInt setSize(0);
       
   231     TInt conditionCount(iConditionList.Count());
       
   232     
       
   233     // Calculate the size of serialization buffer needed
       
   234 
       
   235     setSize += sizeof(TSensrvConditionSetType); // iSetType
       
   236     setSize += sizeof(TInt);                    // iId
       
   237     setSize += sizeof(TInt);                    // iConditionList.Count()    
       
   238         
       
   239     for (TInt i = 0; i < conditionCount; i++ ) // iConditionList
       
   240         {
       
   241         CSensrvChannelConditionImpl* condImpl = 
       
   242             static_cast<CSensrvChannelConditionImpl*>(iConditionList[i]);
       
   243             
       
   244         setSize += sizeof(TSensrvConditionType);      // CSensrvChannelCondition::iType
       
   245         setSize += sizeof(TSensrvConditionOperator);  // CSensrvChannelCondition::iOperator
       
   246         setSize += sizeof(TInt);                      // CSensrvChannelCondition::iIndex
       
   247         setSize += sizeof(TInt);                      // CSensrvChannelCondition::iValue size
       
   248         setSize += condImpl->iValue->Size();          // CSensrvChannelCondition::iValue
       
   249         }
       
   250     
       
   251     
       
   252     // Note: CSensrvChannelConditionSetImpl::iJustAdded is used only by server, 
       
   253     // so no need to serialize it
       
   254     
       
   255     // Note2: CSensrvChannelCondition::iValue size is added in conjunction of each iValue,
       
   256     // as there is no guarantee, that iValue actually is a channel data item.
       
   257     // Some custom implementations might pass some additional data there, too.
       
   258 
       
   259     // Create new serialization buffer    
       
   260     HBufC8* serializedSet = HBufC8::NewLC(setSize);
       
   261     
       
   262     // Append all relevant set elements to the serialization buffer
       
   263     serializedSet->Des().Append(reinterpret_cast<const TUint8*>(&iSetType), sizeof(TSensrvConditionSetType));
       
   264     serializedSet->Des().Append(reinterpret_cast<const TUint8*>(&iId), sizeof(TInt));
       
   265     serializedSet->Des().Append(reinterpret_cast<const TUint8*>(&conditionCount), sizeof(TInt));
       
   266     
       
   267     for (TInt i = 0; i < conditionCount; i++ ) 
       
   268         {
       
   269         CSensrvChannelConditionImpl* condImpl = static_cast<CSensrvChannelConditionImpl*>(iConditionList[i]);
       
   270         TInt valueSize(condImpl->iValue->Size());
       
   271         serializedSet->Des().Append(reinterpret_cast<const TUint8*>(&(condImpl->iType)), sizeof(TSensrvConditionType));
       
   272         serializedSet->Des().Append(reinterpret_cast<const TUint8*>(&(condImpl->iOperator)), sizeof(TSensrvConditionOperator));
       
   273         serializedSet->Des().Append(reinterpret_cast<const TUint8*>(&(condImpl->iIndex)), sizeof(TInt));
       
   274         serializedSet->Des().Append(reinterpret_cast<const TUint8*>(&valueSize), sizeof(TInt));
       
   275         serializedSet->Des().Append(*(condImpl->iValue));
       
   276         }
       
   277     
       
   278     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::SerializeLC return 0x%x" ), serializedSet ) );
       
   279     
       
   280     return serializedSet;
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // Makes this set a clone of serialized set given in parameter
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 TInt CSensrvChannelConditionSetImpl::Deserialize( const TDesC8& aSerializedSet )
       
   288     {
       
   289     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::Deserialize(<input>)" ) ) );
       
   290     
       
   291     TInt err(KErrNone);
       
   292     
       
   293     const TUint8* ptr(aSerializedSet.Ptr());
       
   294     
       
   295     // Get iSetType
       
   296     iSetType = *reinterpret_cast<const TSensrvConditionSetType*>(ptr);
       
   297     ptr += (sizeof(TSensrvConditionSetType)/sizeof(TUint8));
       
   298     
       
   299     // Get iId 
       
   300     iId = *reinterpret_cast<const TInt*>(ptr);
       
   301     ptr += (sizeof(TInt)/sizeof(TUint8));
       
   302 
       
   303     // Get condition count
       
   304     TInt conditionCount = *reinterpret_cast<const TInt*>(ptr);
       
   305     ptr += (sizeof(TInt)/sizeof(TUint8));
       
   306     
       
   307     for (TInt i = 0; err == KErrNone && i < conditionCount; i++ ) 
       
   308         {
       
   309         // Get CSensrvChannelCondition::iType
       
   310         TSensrvConditionType type = *reinterpret_cast<const TSensrvConditionType*>(ptr);
       
   311         ptr += (sizeof(TSensrvConditionType)/sizeof(TUint8));
       
   312         
       
   313         // Get CSensrvChannelCondition::iOperator
       
   314         TSensrvConditionOperator oper = *reinterpret_cast<const TSensrvConditionOperator*>(ptr);
       
   315         ptr += (sizeof(TSensrvConditionOperator)/sizeof(TUint8));
       
   316         
       
   317         // Get CSensrvChannelCondition::iIndex
       
   318         TInt index = *reinterpret_cast<const TInt*>(ptr);
       
   319         ptr += (sizeof(TInt)/sizeof(TUint8));
       
   320         
       
   321         // Get CSensrvChannelCondition::iValue size
       
   322         TInt valueSize = *reinterpret_cast<const TInt*>(ptr);
       
   323         ptr += (sizeof(TInt)/sizeof(TUint8));
       
   324 
       
   325         // Get CSensrvChannelCondition::iValue
       
   326         TPtrC8 value(ptr, valueSize);
       
   327         ptr += (valueSize/sizeof(TUint8));
       
   328         
       
   329         // Construct new condition and add it to this set
       
   330         CSensrvChannelCondition* condition = NULL;
       
   331         TRAP( err, condition = CSensrvChannelCondition::NewL(type, oper, index, value ) );
       
   332         
       
   333         if( KErrNone == err )
       
   334             {
       
   335             TRAP(err, AddChannelConditionL(condition));
       
   336             }
       
   337         }
       
   338     
       
   339     
       
   340     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::Deserialize - return %d" ), err ) );
       
   341     
       
   342     return err;
       
   343     }
       
   344 
       
   345 // ---------------------------------------------------------------------------
       
   346 // Set met data.
       
   347 // ---------------------------------------------------------------------------
       
   348 //
       
   349 TInt CSensrvChannelConditionSetImpl::SetMetData( const TDesC8& aMetData )
       
   350     {
       
   351     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::SetMetData(<input>)" ) ) );
       
   352     
       
   353     TInt err(KErrNone);
       
   354     
       
   355     if (iMetData)
       
   356         {
       
   357         ERROR_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::SetMetData - ERROR: Met data already exists" ) ) );
       
   358         err = KErrAlreadyExists;
       
   359         }
       
   360     else
       
   361         {
       
   362         iMetData = aMetData.Alloc();
       
   363         
       
   364         if (!iMetData)
       
   365             {
       
   366             ERROR_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::SetMetData - ERROR: Met data allocation failure" ) ) );
       
   367             err = KErrNoMemory;
       
   368             }
       
   369         }
       
   370     
       
   371     COMPONENT_TRACE( ( _L( "Sensrv Util - CSensrvChannelConditionSetImpl::SetMetData - return %d" ), err ) );
       
   372     
       
   373     return err;
       
   374     }
       
   375