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 uint16_t UsageStack
[HID_USAGE_STACK_DEPTH
];
41 uint8_t UsageStackSize
= 0;
42 uint16_t BitOffsetIn
= 0;
43 uint16_t BitOffsetOut
= 0;
44 #if defined(HID_ENABLE_FEATURE_PROCESSING)
45 uint16_t BitOffsetFeature
= 0;
47 HID_CollectionPath_t
* CurrCollectionPath
= NULL
;
49 memset(ParserData
, 0x00, sizeof(HID_ReportInfo_t
));
50 memset(StateTable
, 0x00, sizeof(StateTable
));
54 uint8_t HIDReportItem
= *(ReportData
++);
55 uint32_t ReportItemData
= 0;
59 switch (HIDReportItem
& DATA_SIZE_MASK
)
62 ReportItemData
= *((uint32_t*)ReportData
);
67 ReportItemData
= *((uint16_t*)ReportData
);
72 ReportItemData
= *((uint8_t*)ReportData
);
78 switch (HIDReportItem
& (TYPE_MASK
| TAG_MASK
))
80 case (TYPE_GLOBAL
| TAG_GLOBAL_PUSH
):
81 if (CurrStateTable
== &StateTable
[HID_STATETABLE_STACK_DEPTH
- 1])
82 return HID_PARSE_HIDStackOverflow
;
84 memcpy(CurrStateTable
,
86 sizeof(HID_ReportItem_t
));
90 case (TYPE_GLOBAL
| TAG_GLOBAL_POP
):
91 if (CurrStateTable
== &StateTable
[0])
92 return HID_PARSE_HIDStackUnderflow
;
96 case (TYPE_GLOBAL
| TAG_GLOBAL_USAGEPAGE
):
97 CurrStateTable
->Attributes
.Usage
.Page
= ReportItemData
;
99 case (TYPE_GLOBAL
| TAG_GLOBAL_LOGICALMIN
):
100 CurrStateTable
->Attributes
.Logical
.Minimum
= ReportItemData
;
102 case (TYPE_GLOBAL
| TAG_GLOBAL_LOGICALMAX
):
103 CurrStateTable
->Attributes
.Logical
.Maximum
= ReportItemData
;
105 case (TYPE_GLOBAL
| TAG_GLOBAL_PHYSMIN
):
106 CurrStateTable
->Attributes
.Physical
.Minimum
= ReportItemData
;
108 case (TYPE_GLOBAL
| TAG_GLOBAL_PHYSMAX
):
109 CurrStateTable
->Attributes
.Physical
.Maximum
= ReportItemData
;
111 case (TYPE_GLOBAL
| TAG_GLOBAL_UNITEXP
):
112 CurrStateTable
->Attributes
.Unit
.Exponent
= ReportItemData
;
114 case (TYPE_GLOBAL
| TAG_GLOBAL_UNIT
):
115 CurrStateTable
->Attributes
.Unit
.Type
= ReportItemData
;
117 case (TYPE_GLOBAL
| TAG_GLOBAL_REPORTSIZE
):
118 CurrStateTable
->Attributes
.BitSize
= ReportItemData
;
120 case (TYPE_GLOBAL
| TAG_GLOBAL_REPORTCOUNT
):
121 CurrStateTable
->ReportCount
= ReportItemData
;
123 case (TYPE_GLOBAL
| TAG_GLOBAL_REPORTID
):
124 CurrStateTable
->ReportID
= ReportItemData
;
128 case (TYPE_LOCAL
| TAG_LOCAL_USAGE
):
129 if (UsageStackSize
== HID_USAGE_STACK_DEPTH
)
130 return HID_PARSE_UsageStackOverflow
;
132 UsageStack
[UsageStackSize
++] = ReportItemData
;
134 case (TYPE_LOCAL
| TAG_LOCAL_USAGEMIN
):
135 CurrStateTable
->Attributes
.Usage
.MinMax
.Minimum
= ReportItemData
;
137 case (TYPE_LOCAL
| TAG_LOCAL_USAGEMAX
):
138 CurrStateTable
->Attributes
.Usage
.MinMax
.Maximum
= ReportItemData
;
140 case (TYPE_MAIN
| TAG_MAIN_COLLECTION
):
141 if (CurrCollectionPath
== NULL
)
143 CurrCollectionPath
= &ParserData
->CollectionPaths
[0];
147 HID_CollectionPath_t
* ParentCollectionPath
= CurrCollectionPath
;
149 CurrCollectionPath
= &ParserData
->CollectionPaths
[1];
151 while (CurrCollectionPath
->Parent
!= NULL
);
153 if (CurrCollectionPath
== &ParserData
->CollectionPaths
[HID_MAX_COLLECTIONS
- 1])
154 return HID_PARSE_InsufficientCollectionPaths
;
156 CurrCollectionPath
++;
159 CurrCollectionPath
->Parent
= ParentCollectionPath
;
162 CurrCollectionPath
->Type
= ReportItemData
;
163 CurrCollectionPath
->Usage
.Page
= CurrStateTable
->Attributes
.Usage
.Page
;
167 CurrCollectionPath
->Usage
.Usage
= UsageStack
[0];
169 for (uint8_t i
= 0; i
< UsageStackSize
; i
++)
170 UsageStack
[i
] = UsageStack
[i
+ 1];
176 CurrCollectionPath
->Usage
.Usage
= 0;
180 case (TYPE_MAIN
| TAG_MAIN_ENDCOLLECTION
):
181 if (CurrCollectionPath
== NULL
)
182 return HID_PARSE_UnexpectedEndCollection
;
184 CurrCollectionPath
= CurrCollectionPath
->Parent
;
187 case (TYPE_MAIN
| TAG_MAIN_INPUT
):
188 case (TYPE_MAIN
| TAG_MAIN_OUTPUT
):
189 #if defined(HID_ENABLE_FEATURE_PROCESSING)
190 case (TYPE_MAIN
| TAG_MAIN_FEATURE
):
192 for (uint8_t ReportItemNum
= 0; ReportItemNum
< CurrStateTable
->ReportCount
; ReportItemNum
++)
194 HID_ReportItem_t
* CurrReportItem
= &ParserData
->ReportItems
[ParserData
->TotalReportItems
];
196 if (ParserData
->TotalReportItems
== HID_MAX_REPORTITEMS
)
197 return HID_PARSE_InsufficientReportItems
;
199 memcpy(&CurrReportItem
->Attributes
,
200 &CurrStateTable
->Attributes
,
201 sizeof(HID_ReportItem_Attributes_t
));
203 CurrReportItem
->ItemFlags
= ReportItemData
;
204 CurrReportItem
->CollectionPath
= CurrCollectionPath
;
205 CurrReportItem
->ReportID
= CurrStateTable
->ReportID
;
209 CurrReportItem
->Attributes
.Usage
.Usage
= UsageStack
[0];
211 for (uint8_t i
= 0; i
< UsageStackSize
; i
++)
212 UsageStack
[i
] = UsageStack
[i
+ 1];
218 CurrReportItem
->Attributes
.Usage
.Usage
= 0;
221 switch (HIDReportItem
& TAG_MASK
)
224 CurrReportItem
->ItemType
= REPORT_ITEM_TYPE_In
;
225 CurrReportItem
->BitOffset
= BitOffsetIn
;
227 BitOffsetIn
+= CurrStateTable
->Attributes
.BitSize
;
230 case TAG_MAIN_OUTPUT
:
231 CurrReportItem
->ItemType
= REPORT_ITEM_TYPE_Out
;
232 CurrReportItem
->BitOffset
= BitOffsetOut
;
234 BitOffsetOut
+= CurrStateTable
->Attributes
.BitSize
;
237 #if defined(HID_ENABLE_FEATURE_PROCESSING)
238 case TAG_MAIN_FEATURE
:
239 CurrReportItem
->ItemType
= REPORT_ITEM_TYPE_Feature
;
240 CurrReportItem
->BitOffset
= BitOffsetFeature
;
242 BitOffsetFeature
+= CurrStateTable
->Attributes
.BitSize
;
248 #if defined(HID_INCLUDE_CONSTANT_DATA_ITEMS)
249 ParserData
->TotalReportItems
++;
251 if (!(ReportItemData
& IOF_CONSTANT
))
252 ParserData
->TotalReportItems
++;
261 if ((HIDReportItem
& TYPE_MASK
) == TYPE_MAIN
)
263 CurrStateTable
->Attributes
.Usage
.MinMax
.Minimum
= 0;
264 CurrStateTable
->Attributes
.Usage
.MinMax
.Maximum
= 0;
269 return HID_PARSE_Successful
;
272 bool USB_GetHIDReportItemInfo(const uint8_t* ReportData
, HID_ReportItem_t
* const ReportItem
)
274 uint16_t DataBitsRem
= ReportItem
->Attributes
.BitSize
;
275 uint16_t CurrentBit
= ReportItem
->BitOffset
;
276 uint32_t BitMask
= (1 << 0);
278 ReportItem
->Value
= 0;
280 if (ReportItem
->ReportID
)
282 if (ReportItem
->ReportID
!= ReportData
[0])
288 while (DataBitsRem
--)
290 if (ReportData
[CurrentBit
/ 8] & (1 << (CurrentBit
% 8)))
291 ReportItem
->Value
|= BitMask
;
300 void USB_SetHIDReportItemInfo(uint8_t* ReportData
, const HID_ReportItem_t
* ReportItem
)
302 uint16_t DataBitsRem
= ReportItem
->Attributes
.BitSize
;
303 uint16_t CurrentBit
= ReportItem
->BitOffset
;
304 uint32_t BitMask
= (1 << 0);
306 if (ReportItem
->ReportID
)
308 ReportData
[0] = ReportItem
->ReportID
;
312 while (DataBitsRem
--)
314 if (ReportItem
->Value
& (1 << (CurrentBit
% 8)))
315 ReportData
[CurrentBit
/ 8] |= BitMask
;