localconnectivityservice/generichid/src/hidreportbase.cpp
author hgs
Thu, 04 Nov 2010 15:31:42 +0800
changeset 60 841f70763fbe
parent 29 3ae5cb0b4c02
permissions -rw-r--r--
201044_04
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     1
/*
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     2
* Copyright (c) 2004-2007 Nokia Corporation and/or its subsidiary(-ies).
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     3
* All rights reserved.
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     8
*
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
     9
* Initial Contributors:
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    11
*
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    12
* Contributors:
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    13
*
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    14
* Description:  Report base class implementation
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    15
*
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    16
*/
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    17
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    18
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    19
#include "hidfield.h"
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    20
#include "hidtranslate.h"
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    21
#include "hidinterfaces.h"
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    22
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    23
const TUint KValueMask      = 0xFFFFFFFF;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    24
const TUint KSignBitMask    = 0x80000000;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    25
const TInt  KThreeLSB       = 7;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    26
const TInt  KThreeLSBShift  = 3;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    27
// ======== MEMBER FUNCTIONS ========
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    28
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    29
// ---------------------------------------------------------------------------
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    30
// GetIndexOfUsage()
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    31
// ---------------------------------------------------------------------------
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    32
//
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    33
TBool TReportUtils::GetIndexOfUsage(const CField* aField,
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    34
    TInt aUsageId, TInt& aUsageIndex)
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    35
    {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    36
    TArray<TInt> usages(aField->UsageArray());
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    37
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    38
    if ( usages.Count() > 0 )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    39
        {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    40
        // Find the usage in the array
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    41
        for ( TInt i = 0; i < usages.Count(); i++ )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    42
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    43
            if ( usages[i] == aUsageId )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    44
                {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    45
                aUsageIndex = i;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    46
                return ETrue;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    47
                }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    48
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    49
        }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    50
    else
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    51
        {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    52
        // The field includes all usages between the min and max
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    53
        if ( aField->UsageMin() <= aUsageId && aUsageId <= aField->UsageMax() )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    54
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    55
            aUsageIndex = aUsageId - aField->UsageMin();
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    56
            return ETrue;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    57
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    58
        }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    59
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    60
    return EFalse;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    61
    }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    62
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    63
// ---------------------------------------------------------------------------
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    64
// UsageAtIndex()
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    65
// ---------------------------------------------------------------------------
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    66
//
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    67
TInt TReportUtils::UsageAtIndex(const CField* aField, TInt aUsageIndex)
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    68
    {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    69
    TInt usageId = 0;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    70
    TArray<TInt> usages(aField->UsageArray());
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    71
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    72
    if ( usages.Count() > 0 )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    73
        {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    74
        if ( aUsageIndex < 0 )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    75
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    76
            // Null state for array control
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    77
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    78
        else if ( aUsageIndex < usages.Count() )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    79
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    80
            // Get the usage ID from the set of possible usages
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    81
            usageId = usages[aUsageIndex];
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    82
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    83
        else
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    84
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    85
            // If there aren't enough usages in the set, the last one repeats
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    86
            usageId = usages[usages.Count() - 1];
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    87
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    88
        }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    89
    else
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    90
        {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    91
        // Get the usage ID from the range
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    92
        if ( 0 <= aUsageIndex
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    93
                && aUsageIndex <= (aField->UsageMax() - aField->UsageMin()) )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    94
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    95
            usageId = aField->UsageMin() + aUsageIndex;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    96
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    97
        }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    98
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
    99
    return usageId;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   100
    }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   101
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   102
// ---------------------------------------------------------------------------
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   103
// WriteData()
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   104
// ---------------------------------------------------------------------------
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   105
//
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   106
TInt TReportUtils::WriteData(HBufC8& aData, const CField* aField,
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   107
    TInt aIndex, TInt aValue)
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   108
    {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   109
    if ( 0 <= aIndex && aIndex < aField->Count() )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   110
        {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   111
        // The offset in bits from the start of the report to the value
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   112
        TInt offset = aField->Offset() + aIndex * aField->Size();
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   113
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   114
        // How many bits in the least significant byte are not part of the value
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   115
        TInt bitsToShift = offset & KThreeLSB;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   116
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   117
        TUint mask = KValueMask >> ((KSizeOfByte * sizeof(TInt)) - aField->Size());
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   118
        mask <<= bitsToShift;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   119
        aValue <<= bitsToShift;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   120
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   121
        TPtr8 data = aData.Des();
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   122
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   123
        // Write out the bytes, least significant first
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   124
        for ( TInt i = offset >> KThreeLSBShift; mask && i < aData.Length(); i++ )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   125
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   126
            TUint8 maskByte = static_cast<TUint8>(mask);
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   127
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   128
            // The extra cast is because MSVC6 thinks that or-ing 2
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   129
            // TUint8s together gives an int.
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   130
            data[i] = static_cast<TUint8>(
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   131
                (static_cast<TUint8>(aValue) & maskByte)
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   132
                | (aData[i] & ~maskByte));
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   133
            mask >>= KSizeOfByte;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   134
            aValue >>= KSizeOfByte;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   135
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   136
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   137
        return KErrNone;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   138
        }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   139
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   140
    return KErrBadControlIndex;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   141
    }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   142
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   143
// ---------------------------------------------------------------------------
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   144
// ReadData()
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   145
// ---------------------------------------------------------------------------
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   146
//
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   147
TInt TReportUtils::ReadData(const TDesC8& aData, const CField* aField,
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   148
    TInt aIndex, TInt& aValue)
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   149
    {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   150
    if ( 0 <= aIndex && aIndex < aField->Count() )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   151
        {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   152
        // The offset in bits from the start of the report to the value
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   153
        TInt offset = aField->Offset() + aIndex * aField->Size();
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   154
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   155
        // How many bits in the least significant byte are not part of
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   156
        // the value
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   157
        TInt bitsToShift = offset & KThreeLSB;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   158
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   159
        // How many consecutive bytes we need to read to get the whole
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   160
        // value. According to the HID spec, a value cannot span more
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   161
        // than 4 bytes in a report
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   162
        TInt bytesToRead = (bitsToShift + aField->Size() + KThreeLSB) / KSizeOfByte;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   163
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   164
        // Make sure we don't read past the end of the data
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   165
        if ( (offset >> KThreeLSBShift) + bytesToRead > aData.Length() )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   166
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   167
            bytesToRead = aData.Length() - (offset >> KThreeLSBShift);
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   168
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   169
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   170
        TInt value = 0;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   171
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   172
        // Read in the bytes, most significant first
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   173
        for ( TInt i = bytesToRead - 1; i >= 0; i-- )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   174
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   175
            value = (value << KSizeOfByte) | aData[(offset >> KThreeLSBShift) + i];
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   176
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   177
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   178
        value >>= bitsToShift;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   179
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   180
        // Make masks for the whole value and just the sign bit
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   181
        TUint valueMask = KValueMask >> ((KSizeOfByte * sizeof(TInt)) - aField->Size());
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   182
        TUint signMask = KSignBitMask >> ((KSizeOfByte * sizeof(TInt)) - aField->Size());
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   183
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   184
        if ( aField->LogicalMin() < 0 && (value & signMask) )
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   185
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   186
            // The value is negative, so the leading bits should be 1s
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   187
            aValue = value | ~valueMask;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   188
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   189
        else
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   190
            {
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   191
            // The value is positive, so the leading bits should be 0s
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   192
            aValue = value & valueMask;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   193
            }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   194
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   195
        return KErrNone;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   196
        }
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   197
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   198
    return KErrBadControlIndex;
3ae5cb0b4c02 201025_07
hgs
parents:
diff changeset
   199
    }