2 * @brief USB Human Interface Device (HID) Class report descriptor parser
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * Copyright(C) Dean Camera, 2011, 2012
\r
7 * All rights reserved.
\r
10 * Software that is described herein is for illustrative purposes only
\r
11 * which provides customers with programming information regarding the
\r
12 * LPC products. This software is supplied "AS IS" without any warranties of
\r
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
\r
14 * all warranties, express or implied, including all implied warranties of
\r
15 * merchantability, fitness for a particular purpose and non-infringement of
\r
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
\r
17 * or liability for the use of the software, conveys no license or rights under any
\r
18 * patent, copyright, mask work right, or any other intellectual property rights in
\r
19 * or to any products. NXP Semiconductors reserves the right to make changes
\r
20 * in the software without notification. NXP Semiconductors also makes no
\r
21 * representation or warranty that such application will be suitable for the
\r
22 * specified use without further testing or modification.
\r
25 * Permission to use, copy, modify, and distribute this software and its
\r
26 * documentation is hereby granted, under NXP Semiconductors' and its
\r
27 * licensor's relevant copyrights in the software, without fee, provided that it
\r
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
\r
29 * copyright, permission, and disclaimer notice must appear in all copies of
\r
34 #define __INCLUDE_FROM_USB_DRIVER
\r
35 #define __INCLUDE_FROM_HID_DRIVER
\r
36 #include "HIDParser.h"
\r
38 uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
\r
39 uint16_t ReportSize,
\r
40 HID_ReportInfo_t* const ParserData)
\r
42 HID_StateTable_t StateTable[HID_STATETABLE_STACK_DEPTH];
\r
43 HID_StateTable_t* CurrStateTable = &StateTable[0];
\r
44 HID_CollectionPath_t* CurrCollectionPath = NULL;
\r
45 HID_ReportSizeInfo_t* CurrReportIDInfo = &ParserData->ReportIDSizes[0];
\r
46 uint16_t UsageList[HID_USAGE_STACK_DEPTH];
\r
47 uint8_t UsageListSize = 0;
\r
48 HID_MinMax_t UsageMinMax = {0, 0};
\r
50 memset(ParserData, 0x00, sizeof(HID_ReportInfo_t));
\r
51 memset(CurrStateTable, 0x00, sizeof(HID_StateTable_t));
\r
52 memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
\r
54 ParserData->TotalDeviceReports = 1;
\r
58 uint8_t HIDReportItem = *ReportData;
\r
59 uint32_t ReportItemData = 0;
\r
64 switch (HIDReportItem & HID_RI_DATA_SIZE_MASK)
\r
66 case HID_RI_DATA_BITS_32:
\r
67 ReportItemData = le32_to_cpu(*((uint32_t*)ReportData));
\r
71 case HID_RI_DATA_BITS_16:
\r
72 ReportItemData = le16_to_cpu(*((uint16_t*)ReportData));
\r
76 case HID_RI_DATA_BITS_8:
\r
77 ReportItemData = *((uint8_t*)ReportData);
\r
83 switch (HIDReportItem & (HID_RI_TYPE_MASK | HID_RI_TAG_MASK))
\r
85 case HID_RI_PUSH(0):
\r
86 if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH - 1])
\r
87 return HID_PARSE_HIDStackOverflow;
\r
89 memcpy((CurrStateTable + 1),
\r
91 sizeof(HID_ReportItem_t));
\r
96 if (CurrStateTable == &StateTable[0])
\r
97 return HID_PARSE_HIDStackUnderflow;
\r
101 case HID_RI_USAGE_PAGE(0):
\r
102 if ((HIDReportItem & HID_RI_DATA_SIZE_MASK) == HID_RI_DATA_BITS_32)
\r
103 CurrStateTable->Attributes.Usage.Page = (ReportItemData >> 16);
\r
105 CurrStateTable->Attributes.Usage.Page = ReportItemData;
\r
107 case HID_RI_LOGICAL_MINIMUM(0):
\r
108 CurrStateTable->Attributes.Logical.Minimum = ReportItemData;
\r
110 case HID_RI_LOGICAL_MAXIMUM(0):
\r
111 CurrStateTable->Attributes.Logical.Maximum = ReportItemData;
\r
113 case HID_RI_PHYSICAL_MINIMUM(0):
\r
114 CurrStateTable->Attributes.Physical.Minimum = ReportItemData;
\r
116 case HID_RI_PHYSICAL_MAXIMUM(0):
\r
117 CurrStateTable->Attributes.Physical.Maximum = ReportItemData;
\r
119 case HID_RI_UNIT_EXPONENT(0):
\r
120 CurrStateTable->Attributes.Unit.Exponent = ReportItemData;
\r
122 case HID_RI_UNIT(0):
\r
123 CurrStateTable->Attributes.Unit.Type = ReportItemData;
\r
125 case HID_RI_REPORT_SIZE(0):
\r
126 CurrStateTable->Attributes.BitSize = ReportItemData;
\r
128 case HID_RI_REPORT_COUNT(0):
\r
129 CurrStateTable->ReportCount = ReportItemData;
\r
131 case HID_RI_REPORT_ID(0):
\r
132 CurrStateTable->ReportID = ReportItemData;
\r
134 if (ParserData->UsingReportIDs)
\r
136 CurrReportIDInfo = NULL;
\r
138 for (uint8_t i = 0; i < ParserData->TotalDeviceReports; i++)
\r
140 if (ParserData->ReportIDSizes[i].ReportID == CurrStateTable->ReportID)
\r
142 CurrReportIDInfo = &ParserData->ReportIDSizes[i];
\r
147 if (CurrReportIDInfo == NULL)
\r
149 if (ParserData->TotalDeviceReports == HID_MAX_REPORT_IDS)
\r
150 return HID_PARSE_InsufficientReportIDItems;
\r
152 CurrReportIDInfo = &ParserData->ReportIDSizes[ParserData->TotalDeviceReports++];
\r
153 memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
\r
157 ParserData->UsingReportIDs = true;
\r
159 CurrReportIDInfo->ReportID = CurrStateTable->ReportID;
\r
161 case HID_RI_USAGE(0):
\r
162 if (UsageListSize == HID_USAGE_STACK_DEPTH)
\r
163 return HID_PARSE_UsageListOverflow;
\r
165 UsageList[UsageListSize++] = ReportItemData;
\r
167 case HID_RI_USAGE_MINIMUM(0):
\r
168 UsageMinMax.Minimum = ReportItemData;
\r
170 case HID_RI_USAGE_MAXIMUM(0):
\r
171 UsageMinMax.Maximum = ReportItemData;
\r
173 case HID_RI_COLLECTION(0):
\r
174 if (CurrCollectionPath == NULL)
\r
176 CurrCollectionPath = &ParserData->CollectionPaths[0];
\r
180 HID_CollectionPath_t* ParentCollectionPath = CurrCollectionPath;
\r
182 CurrCollectionPath = &ParserData->CollectionPaths[1];
\r
184 while (CurrCollectionPath->Parent != NULL)
\r
186 if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])
\r
187 return HID_PARSE_InsufficientCollectionPaths;
\r
189 CurrCollectionPath++;
\r
192 CurrCollectionPath->Parent = ParentCollectionPath;
\r
195 CurrCollectionPath->Type = ReportItemData;
\r
196 CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;
\r
200 CurrCollectionPath->Usage.Usage = UsageList[0];
\r
202 for (uint8_t i = 0; i < UsageListSize; i++)
\r
203 UsageList[i] = UsageList[i + 1];
\r
207 else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)
\r
209 CurrCollectionPath->Usage.Usage = UsageMinMax.Minimum++;
\r
213 case HID_RI_END_COLLECTION(0):
\r
214 if (CurrCollectionPath == NULL)
\r
215 return HID_PARSE_UnexpectedEndCollection;
\r
217 CurrCollectionPath = CurrCollectionPath->Parent;
\r
219 case HID_RI_INPUT(0):
\r
220 case HID_RI_OUTPUT(0):
\r
221 case HID_RI_FEATURE(0):
\r
222 for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)
\r
224 HID_ReportItem_t NewReportItem;
\r
226 memcpy(&NewReportItem.Attributes,
\r
227 &CurrStateTable->Attributes,
\r
228 sizeof(HID_ReportItem_Attributes_t));
\r
230 NewReportItem.ItemFlags = ReportItemData;
\r
231 NewReportItem.CollectionPath = CurrCollectionPath;
\r
232 NewReportItem.ReportID = CurrStateTable->ReportID;
\r
236 NewReportItem.Attributes.Usage.Usage = UsageList[0];
\r
238 for (uint8_t i = 0; i < UsageListSize; i++)
\r
239 UsageList[i] = UsageList[i + 1];
\r
243 else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)
\r
245 NewReportItem.Attributes.Usage.Usage = UsageMinMax.Minimum++;
\r
248 uint8_t ItemTypeTag = (HIDReportItem & (HID_RI_TYPE_MASK | HID_RI_TAG_MASK));
\r
250 if (ItemTypeTag == HID_RI_INPUT(0))
\r
251 NewReportItem.ItemType = HID_REPORT_ITEM_In;
\r
252 else if (ItemTypeTag == HID_RI_OUTPUT(0))
\r
253 NewReportItem.ItemType = HID_REPORT_ITEM_Out;
\r
255 NewReportItem.ItemType = HID_REPORT_ITEM_Feature;
\r
257 NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType];
\r
259 CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType] += CurrStateTable->Attributes.BitSize;
\r
261 if (ParserData->LargestReportSizeBits < NewReportItem.BitOffset)
\r
262 ParserData->LargestReportSizeBits = NewReportItem.BitOffset;
\r
264 if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)
\r
265 return HID_PARSE_InsufficientReportItems;
\r
267 memcpy(&ParserData->ReportItems[ParserData->TotalReportItems],
\r
268 &NewReportItem, sizeof(HID_ReportItem_t));
\r
270 if (!(ReportItemData & HID_IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&NewReportItem))
\r
271 ParserData->TotalReportItems++;
\r
277 if ((HIDReportItem & HID_RI_TYPE_MASK) == HID_RI_TYPE_MAIN)
\r
279 UsageMinMax.Minimum = 0;
\r
280 UsageMinMax.Maximum = 0;
\r
285 if (!(ParserData->TotalReportItems))
\r
286 return HID_PARSE_NoUnfilteredReportItems;
\r
288 return HID_PARSE_Successful;
\r
291 bool USB_GetHIDReportItemInfo(const uint8_t* ReportData,
\r
292 HID_ReportItem_t* const ReportItem)
\r
294 if (ReportItem == NULL)
\r
297 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
\r
298 uint16_t CurrentBit = ReportItem->BitOffset;
\r
299 uint32_t BitMask = (1 << 0);
\r
301 if (ReportItem->ReportID)
\r
303 if (ReportItem->ReportID != ReportData[0])
\r
309 ReportItem->PreviousValue = ReportItem->Value;
\r
310 ReportItem->Value = 0;
\r
312 while (DataBitsRem--)
\r
314 if (ReportData[CurrentBit / 8] & (1 << (CurrentBit % 8)))
\r
315 ReportItem->Value |= BitMask;
\r
324 void USB_SetHIDReportItemInfo(uint8_t* ReportData,
\r
325 HID_ReportItem_t* const ReportItem)
\r
327 if (ReportItem == NULL)
\r
330 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
\r
331 uint16_t CurrentBit = ReportItem->BitOffset;
\r
332 uint32_t BitMask = (1 << 0);
\r
334 if (ReportItem->ReportID)
\r
336 ReportData[0] = ReportItem->ReportID;
\r
340 ReportItem->PreviousValue = ReportItem->Value;
\r
342 while (DataBitsRem--)
\r
344 if (ReportItem->Value & (1 << (CurrentBit % 8)))
\r
345 ReportData[CurrentBit / 8] |= BitMask;
\r
352 uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData,
\r
353 const uint8_t ReportID,
\r
354 const uint8_t ReportType)
\r
356 for (uint8_t i = 0; i < HID_MAX_REPORT_IDS; i++)
\r
358 uint16_t ReportSizeBits = ParserData->ReportIDSizes[i].ReportSizeBits[ReportType];
\r
360 if (ParserData->ReportIDSizes[i].ReportID == ReportID)
\r
361 return (ReportSizeBits / 8) + ((ReportSizeBits % 8) ? 1 : 0);
\r