localconnectivityservice/generichid/src/hidreportgenerator.cpp
branchRCL_3
changeset 20 4a793f564d72
parent 19 0aa8cc770c8a
child 21 74aa6861c87d
equal deleted inserted replaced
19:0aa8cc770c8a 20:4a793f564d72
     1 /*
       
     2 * Copyright (c) 2004-2007 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:  HID retport generator
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "hidreportgenerator.h"
       
    21 #include "hidreportroot.h"
       
    22 #include "hidinterfaces.h"
       
    23 
       
    24 // ======== MEMBER FUNCTIONS ========
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // NewLC()
       
    28 // ---------------------------------------------------------------------------
       
    29 //
       
    30 EXPORT_C CReportGenerator* CReportGenerator::NewLC(const CReportRoot*
       
    31     aReportRoot, TInt aReportId, CField::TType aType)
       
    32     {
       
    33     CReportGenerator* self = new (ELeave) CReportGenerator(aReportId, aType);
       
    34     CleanupStack::PushL(self);
       
    35     self->ConstructL(aReportRoot);
       
    36     return self;
       
    37     }
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // NewL()
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 EXPORT_C CReportGenerator* CReportGenerator::NewL(const CReportRoot*
       
    44     aReportRoot, TInt aReportId, CField::TType aType)
       
    45     {
       
    46     CReportGenerator* self =
       
    47         CReportGenerator::NewLC(aReportRoot, aReportId, aType);
       
    48     CleanupStack::Pop(self);
       
    49     return self;
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // Constructor
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CReportGenerator::CReportGenerator(TInt aReportId, CField::TType aType)
       
    57     : iReportId(aReportId), iType(aType)
       
    58     {
       
    59     // Nothing else to do
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // ConstructL()
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 void CReportGenerator::ConstructL(const CReportRoot* aReportRoot)
       
    67     {
       
    68     User::LeaveIfNull(const_cast<CReportRoot*>(aReportRoot));
       
    69     iReportRoot = aReportRoot;
       
    70 
       
    71     // Allocate the buffer, initialise to all zeros, and fill in the
       
    72     // report ID if used.  Zero should be a null value for all array fields
       
    73     // according to the HID parser error checking spec.
       
    74     iBuf = HBufC8::NewMaxL(aReportRoot->ReportSizeBytes(iReportId, iType));
       
    75     TPtr8 bufptr = iBuf->Des(); 
       
    76     bufptr.FillZ();
       
    77     if ( 0 != iReportId )
       
    78         {
       
    79         bufptr[0] = static_cast<TUint8>(iReportId);
       
    80         }
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Destructor
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CReportGenerator::~CReportGenerator()
       
    88     {
       
    89     delete iBuf;
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // SetField()
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 EXPORT_C TInt CReportGenerator::SetField(const CField* aField,
       
    97     TInt aUsageId, TInt aValue, TInt aControlOffset /*= 0*/)
       
    98     {
       
    99     TInt usageIndex;
       
   100 
       
   101     if ( aField && TReportUtils::GetIndexOfUsage(aField, aUsageId, usageIndex) )
       
   102         {
       
   103         if ( aField->IsArray() )
       
   104             {
       
   105             // Convert usage ID to logical value
       
   106             TInt logicalValue = usageIndex + aField->LogicalMin();
       
   107 
       
   108             // Find unused position in the array and write the logical
       
   109             // value to it
       
   110             for ( TInt i = 0; i < aField->Count(); i++ )
       
   111                 {
       
   112                 TInt value;
       
   113                 TInt error = TReportUtils::ReadData(*iBuf, aField, i, value);
       
   114 
       
   115                 if ( KErrNone != error )
       
   116                     {
       
   117                     return error;
       
   118                     }
       
   119 
       
   120                 if ( value == logicalValue )
       
   121                     {
       
   122                     // The array already contains this usage
       
   123                     return KErrNone;
       
   124                     }
       
   125                 else if ( 0 == value || value < aField->LogicalMin() ||
       
   126                     aField->LogicalMax() < value )
       
   127                     {
       
   128                     // This is an unused position
       
   129                     //
       
   130                     // NOTE: The comparison with zero is because the buffer is
       
   131                     // initialised to all zeros, and some reports erroneously
       
   132                     // include zero in the logical range.
       
   133                     //
       
   134                     return TReportUtils::WriteData(*iBuf, aField, i, logicalValue);
       
   135                     }
       
   136                 }
       
   137 
       
   138             return KErrNoSpaceInArray;
       
   139             }
       
   140         else
       
   141             {
       
   142             // Check the value to set is valid
       
   143             if ( aValue < aField->LogicalMin() ||
       
   144                 aField->LogicalMax() < aValue )
       
   145                 {
       
   146                 return KErrValueOutOfRange;
       
   147                 }
       
   148 
       
   149             return TReportUtils::WriteData(*iBuf, aField,
       
   150                 usageIndex + aControlOffset, aValue);
       
   151             }
       
   152         }
       
   153 
       
   154     return KErrUsageNotFound;
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // SetField()
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 EXPORT_C TPtr8 CReportGenerator::Report()
       
   162     {
       
   163     return iBuf->Des();
       
   164     }