3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
31 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
34 #include "HIDParser.h"
36 uint8_t USB_ProcessHIDReport(const uint8_t* ReportData
, uint16_t ReportSize
, HID_ReportInfo_t
* const ParserData
)
38 HID_StateTable_t StateTable
[HID_STATETABLE_STACK_DEPTH
];
39 HID_StateTable_t
* CurrStateTable
= &StateTable
[0];
40 HID_CollectionPath_t
* CurrCollectionPath
= NULL
;
41 HID_ReportSizeInfo_t
* CurrReportIDInfo
= &ParserData
->ReportIDSizes
[0];
42 uint16_t UsageStack
[HID_USAGE_STACK_DEPTH
];
43 uint8_t UsageStackSize
= 0;
45 ParserData
->TotalReportItems
= 0;
46 ParserData
->TotalDeviceReports
= 1;
47 ParserData
->UsingReportIDs
= false;
49 for (uint8_t CurrCollection
= 0; CurrCollection
< HID_MAX_COLLECTIONS
; CurrCollection
++)
50 ParserData
->CollectionPaths
[CurrCollection
].Parent
= NULL
;
52 memset(CurrStateTable
, 0x00, sizeof(HID_StateTable_t
));
53 memset(CurrReportIDInfo
, 0x00, sizeof(HID_ReportSizeInfo_t
));
57 uint8_t HIDReportItem
= *ReportData
;
58 uint32_t ReportItemData
= 0;
63 switch (HIDReportItem
& DATA_SIZE_MASK
)
66 ReportItemData
= *((uint32_t*)ReportData
);
71 ReportItemData
= *((uint16_t*)ReportData
);
76 ReportItemData
= *((uint8_t*)ReportData
);
82 switch (HIDReportItem
& (TYPE_MASK
| TAG_MASK
))
84 case (TYPE_GLOBAL
| TAG_GLOBAL_PUSH
):
85 if (CurrStateTable
== &StateTable
[HID_STATETABLE_STACK_DEPTH
- 1])
86 return HID_PARSE_HIDStackOverflow
;
88 memcpy((CurrStateTable
+ 1),
90 sizeof(HID_ReportItem_t
));
94 case (TYPE_GLOBAL
| TAG_GLOBAL_POP
):
95 if (CurrStateTable
== &StateTable
[0])
96 return HID_PARSE_HIDStackUnderflow
;
100 case (TYPE_GLOBAL
| TAG_GLOBAL_USAGEPAGE
):
101 CurrStateTable
->Attributes
.Usage
.Page
= ReportItemData
;
103 case (TYPE_GLOBAL
| TAG_GLOBAL_LOGICALMIN
):
104 CurrStateTable
->Attributes
.Logical
.Minimum
= ReportItemData
;
106 case (TYPE_GLOBAL
| TAG_GLOBAL_LOGICALMAX
):
107 CurrStateTable
->Attributes
.Logical
.Maximum
= ReportItemData
;
109 case (TYPE_GLOBAL
| TAG_GLOBAL_PHYSMIN
):
110 CurrStateTable
->Attributes
.Physical
.Minimum
= ReportItemData
;
112 case (TYPE_GLOBAL
| TAG_GLOBAL_PHYSMAX
):
113 CurrStateTable
->Attributes
.Physical
.Maximum
= ReportItemData
;
115 case (TYPE_GLOBAL
| TAG_GLOBAL_UNITEXP
):
116 CurrStateTable
->Attributes
.Unit
.Exponent
= ReportItemData
;
118 case (TYPE_GLOBAL
| TAG_GLOBAL_UNIT
):
119 CurrStateTable
->Attributes
.Unit
.Type
= ReportItemData
;
121 case (TYPE_GLOBAL
| TAG_GLOBAL_REPORTSIZE
):
122 CurrStateTable
->Attributes
.BitSize
= ReportItemData
;
124 case (TYPE_GLOBAL
| TAG_GLOBAL_REPORTCOUNT
):
125 CurrStateTable
->ReportCount
= ReportItemData
;
127 case (TYPE_GLOBAL
| TAG_GLOBAL_REPORTID
):
128 CurrStateTable
->ReportID
= ReportItemData
;
130 if (ParserData
->UsingReportIDs
)
132 CurrReportIDInfo
= NULL
;
134 for (uint8_t i
= 0; i
< ParserData
->TotalDeviceReports
; i
++)
136 if (ParserData
->ReportIDSizes
[i
].ReportID
== CurrStateTable
->ReportID
)
138 CurrReportIDInfo
= &ParserData
->ReportIDSizes
[i
];
143 if (CurrReportIDInfo
== NULL
)
145 if (ParserData
->TotalDeviceReports
++ > HID_MAX_REPORT_IDS
)
146 return HID_PARSE_InsufficientReportIDItems
;
148 CurrReportIDInfo
= &ParserData
->ReportIDSizes
[ParserData
->TotalDeviceReports
- 1];
149 memset(CurrReportIDInfo
, 0x00, sizeof(HID_ReportSizeInfo_t
));
153 ParserData
->UsingReportIDs
= true;
155 CurrReportIDInfo
->ReportID
= CurrStateTable
->ReportID
;
157 case (TYPE_LOCAL
| TAG_LOCAL_USAGE
):
158 if (UsageStackSize
== HID_USAGE_STACK_DEPTH
)
159 return HID_PARSE_UsageStackOverflow
;
161 UsageStack
[UsageStackSize
++] = ReportItemData
;
163 case (TYPE_LOCAL
| TAG_LOCAL_USAGEMIN
):
164 CurrStateTable
->Attributes
.Usage
.MinMax
.Minimum
= ReportItemData
;
166 case (TYPE_LOCAL
| TAG_LOCAL_USAGEMAX
):
167 CurrStateTable
->Attributes
.Usage
.MinMax
.Maximum
= ReportItemData
;
169 case (TYPE_MAIN
| TAG_MAIN_COLLECTION
):
170 if (CurrCollectionPath
== NULL
)
172 CurrCollectionPath
= &ParserData
->CollectionPaths
[0];
176 HID_CollectionPath_t
* ParentCollectionPath
= CurrCollectionPath
;
178 CurrCollectionPath
= &ParserData
->CollectionPaths
[1];
180 while (CurrCollectionPath
->Parent
!= NULL
);
182 if (CurrCollectionPath
== &ParserData
->CollectionPaths
[HID_MAX_COLLECTIONS
- 1])
183 return HID_PARSE_InsufficientCollectionPaths
;
185 CurrCollectionPath
++;
188 CurrCollectionPath
->Parent
= ParentCollectionPath
;
191 CurrCollectionPath
->Type
= ReportItemData
;
192 CurrCollectionPath
->Usage
.Page
= CurrStateTable
->Attributes
.Usage
.Page
;
196 CurrCollectionPath
->Usage
.Usage
= UsageStack
[0];
198 for (uint8_t i
= 0; i
< UsageStackSize
; i
++)
199 UsageStack
[i
] = UsageStack
[i
+ 1];
205 CurrCollectionPath
->Usage
.Usage
= 0;
209 case (TYPE_MAIN
| TAG_MAIN_ENDCOLLECTION
):
210 if (CurrCollectionPath
== NULL
)
211 return HID_PARSE_UnexpectedEndCollection
;
213 CurrCollectionPath
= CurrCollectionPath
->Parent
;
216 case (TYPE_MAIN
| TAG_MAIN_INPUT
):
217 case (TYPE_MAIN
| TAG_MAIN_OUTPUT
):
218 case (TYPE_MAIN
| TAG_MAIN_FEATURE
):
219 uint8_t Count
= CurrStateTable
->ReportCount
;
220 for (uint8_t ReportItemNum
= 0; ReportItemNum
< Count
; ReportItemNum
++)
222 HID_ReportItem_t NewReportItem
;
224 memcpy(&NewReportItem
.Attributes
,
225 &CurrStateTable
->Attributes
,
226 sizeof(HID_ReportItem_Attributes_t
));
228 NewReportItem
.ItemFlags
= ReportItemData
;
229 NewReportItem
.CollectionPath
= CurrCollectionPath
;
230 NewReportItem
.ReportID
= CurrStateTable
->ReportID
;
234 NewReportItem
.Attributes
.Usage
.Usage
= UsageStack
[0];
236 for (uint8_t i
= 0; i
< UsageStackSize
; i
++)
237 UsageStack
[i
] = UsageStack
[i
+ 1];
243 NewReportItem
.Attributes
.Usage
.Usage
= 0;
246 uint8_t ReportSizeIndex
= 0;
248 switch (HIDReportItem
& TAG_MASK
)
251 NewReportItem
.ItemType
= REPORT_ITEM_TYPE_In
;
252 NewReportItem
.BitOffset
= CurrReportIDInfo
->ReportSizeBits
[REPORT_ITEM_TYPE_In
];
254 ReportSizeIndex
= REPORT_ITEM_TYPE_In
;
256 case TAG_MAIN_OUTPUT
:
257 NewReportItem
.ItemType
= REPORT_ITEM_TYPE_Out
;
258 NewReportItem
.BitOffset
= CurrReportIDInfo
->ReportSizeBits
[REPORT_ITEM_TYPE_Out
];
260 ReportSizeIndex
= REPORT_ITEM_TYPE_Out
;
262 case TAG_MAIN_FEATURE
:
263 NewReportItem
.ItemType
= REPORT_ITEM_TYPE_Feature
;
264 NewReportItem
.BitOffset
= CurrReportIDInfo
->ReportSizeBits
[REPORT_ITEM_TYPE_Feature
];
266 ReportSizeIndex
= REPORT_ITEM_TYPE_Feature
;
270 CurrReportIDInfo
->ReportSizeBits
[ReportSizeIndex
] += CurrStateTable
->Attributes
.BitSize
;
272 if (ParserData
->LargestReportSizeBits
< CurrReportIDInfo
->ReportSizeBits
[ReportSizeIndex
])
273 ParserData
->LargestReportSizeBits
= CurrReportIDInfo
->ReportSizeBits
[ReportSizeIndex
];
275 if (!(ReportItemData
& IOF_CONSTANT
) && CALLBACK_HIDParser_FilterHIDReportItem(&CurrStateTable
->Attributes
))
277 if (ParserData
->TotalReportItems
== HID_MAX_REPORTITEMS
)
278 return HID_PARSE_InsufficientReportItems
;
280 memcpy(&ParserData
->ReportItems
[ParserData
->TotalReportItems
],
281 &NewReportItem
, sizeof(HID_ReportItem_t
));
283 ParserData
->TotalReportItems
++;
292 if ((HIDReportItem
& TYPE_MASK
) == TYPE_MAIN
)
294 CurrStateTable
->Attributes
.Usage
.MinMax
.Minimum
= 0;
295 CurrStateTable
->Attributes
.Usage
.MinMax
.Maximum
= 0;
300 if (!(ParserData
->TotalReportItems
))
301 return HID_PARSE_NoUnfilteredReportItems
;
303 return HID_PARSE_Successful
;
306 bool USB_GetHIDReportItemInfo(const uint8_t* ReportData
, HID_ReportItem_t
* const ReportItem
)
308 uint16_t DataBitsRem
= ReportItem
->Attributes
.BitSize
;
309 uint16_t CurrentBit
= ReportItem
->BitOffset
;
310 uint32_t BitMask
= (1 << 0);
312 ReportItem
->Value
= 0;
314 if (ReportItem
->ReportID
)
316 if (ReportItem
->ReportID
!= ReportData
[0])
322 while (DataBitsRem
--)
324 if (ReportData
[CurrentBit
/ 8] & (1 << (CurrentBit
% 8)))
325 ReportItem
->Value
|= BitMask
;
334 void USB_SetHIDReportItemInfo(uint8_t* ReportData
, const HID_ReportItem_t
* ReportItem
)
336 uint16_t DataBitsRem
= ReportItem
->Attributes
.BitSize
;
337 uint16_t CurrentBit
= ReportItem
->BitOffset
;
338 uint32_t BitMask
= (1 << 0);
340 if (ReportItem
->ReportID
)
342 ReportData
[0] = ReportItem
->ReportID
;
346 while (DataBitsRem
--)
348 if (ReportItem
->Value
& (1 << (CurrentBit
% 8)))
349 ReportData
[CurrentBit
/ 8] |= BitMask
;
356 uint16_t USB_GetHIDReportSize(HID_ReportInfo_t
* const ParserData
, const uint8_t ReportID
, const uint8_t ReportType
)
358 for (uint8_t i
= 0; i
< HID_MAX_REPORT_IDS
; i
++)
360 if (ParserData
->ReportIDSizes
[i
].ReportID
== ReportID
)
361 return ParserData
->ReportIDSizes
[i
].ReportSizeBits
[ReportType
];