|
|
1.1 root 1: /*
2: * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * The contents of this file constitute Original Code as defined in and
7: * are subject to the Apple Public Source License Version 1.1 (the
8: * "License"). You may not use this file except in compliance with the
9: * License. Please obtain a copy of the License at
10: * http://www.apple.com/publicsource and read it before using this file.
11: *
12: * This Original Code and all software distributed under the License are
13: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17: * License for the specific language governing rights and limitations
18: * under the License.
19: *
20: * @APPLE_LICENSE_HEADER_END@
21: */
22: /*
23: File: HIDGetData.c
24:
25: Contains: xxx put contents here xxx
26:
27: Version: xxx put version here xxx
28:
29: Copyright: � 1999 by Apple Computer, Inc., all rights reserved.
30:
31: File Ownership:
32:
33: DRI: xxx put dri here xxx
34:
35: Other Contact: xxx put other contact here xxx
36:
37: Technology: xxx put technology here xxx
38:
39: Writers:
40:
41: (BWS) Brent Schorsch
42:
43: Change History (most recent first):
44:
45: <USB2> 3/5/99 BWS [2311353] HIDGetData not masking properly, so not work at all
46: <USB1> 3/5/99 BWS first checked in
47: */
48:
49: #include "HIDLib.h"
50:
51: /*
52: *------------------------------------------------------------------------------
53: *
54: * HIDGetData - Get a single data item from a report
55: *
56: * Input:
57: * psReport - The report
58: * iReportLength - The length of the report
59: * iStart - Start Bit in report
60: * iSize - Number of Bits
61: * piValue - The place to write the data
62: * bSignExtend - Sign extend?
63: * Output:
64: * piValue - The data
65: * Returns:
66: * kHidP_Success - Success
67: * kHidP_NullPointer - Argument, Pointer was Null
68: *
69: *------------------------------------------------------------------------------
70: */
71: OSStatus HIDGetData(void * report, UInt32 iReportLength,
72: UInt32 iStart, UInt32 iSize, SInt32 *piValue,
73: Boolean bSignExtend)
74: {
75: Byte * psReport = report;
76: unsigned data;
77: unsigned iSignBit;
78: unsigned iExtendMask;
79: unsigned iStartByte = iStart/8;
80: unsigned startBit = iStart&7;
81: unsigned iLastBit = iStart + iSize - 1;
82: unsigned iLastByte = iLastBit/8;
83: int iCurrentByte; // needs to be signed, we terminate loop on -1
84: unsigned iMask;
85:
86: // Check the parameters
87: if ((iSize == 0) || (iLastByte >= iReportLength) || (iLastByte < iStartByte))
88: return kHIDBadParameterErr;
89:
90: // Pick up the data bytes backwards
91: data = 0;
92: for (iCurrentByte = iLastByte; iCurrentByte >= (int) iStartByte; iCurrentByte--)
93: {
94: data <<= 8;
95:
96: iMask = 0xff; // 1111 1111 initial mask
97: // if this is the 'last byte', then we need to mask off the top part of the byte
98: // to find the mask, we: find the position in this byte (lastBit % 8)
99: // then shift one to the left that many times plus one (to get one bit further)
100: // then subtract 1 to get all ones starting from the lastBit to the least signif bit
101: // ex: if iLastBit is 9, or iLastBit is 15, then we get:
102: // 1 7 (x % 8)
103: // 0000 0100 1 0000 0000 (1 << (x + 1))
104: // 0000 0011 0 1111 1111 (x - 1)
105: if (iCurrentByte == iLastByte)
106: iMask = ((1 << (((unsigned) iLastBit % 8) + 1)) - 1);
107:
108: data |= (unsigned) psReport[iCurrentByte] & iMask;
109: }
110:
111: // Shift to the right to byte align the least significant bit
112: data >>= startBit;
113:
114: // Sign extend the report item
115: if (bSignExtend)
116: {
117: iSignBit = 1;
118: if (iSize > 1)
119: iSignBit <<= (iSize-1);
120: iExtendMask = (iSignBit << 1) - 1;
121: if ((data & iSignBit)==0)
122: data &= iExtendMask;
123: else
124: data |= ~iExtendMask;
125: }
126:
127: // Return the value
128: *piValue = (SInt32) data;
129:
130: return kHIDSuccess;
131: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.