Applied STATIC_ENDPOINT_CONFIGURATION and FIXED_CONTROL_SIZE tokens to all Device...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / HIDParser.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #include "HIDParser.h"
32
33 uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID_ReportInfo_t* const ParserData)
34 {
35 HID_StateTable_t StateTable[HID_STATETABLE_STACK_DEPTH];
36 HID_StateTable_t* CurrStateTable = &StateTable[0];
37 uint16_t UsageStack[HID_USAGE_STACK_DEPTH];
38 uint8_t UsageStackSize = 0;
39 uint16_t BitOffsetIn = 0;
40 uint16_t BitOffsetOut = 0;
41 #if defined(HID_ENABLE_FEATURE_PROCESSING)
42 uint16_t BitOffsetFeature = 0;
43 #endif
44 HID_CollectionPath_t* CurrCollectionPath = NULL;
45
46 memset((void*)ParserData, 0x00, sizeof(HID_ReportInfo_t));
47 memset((void*)StateTable, 0x00, sizeof(StateTable));
48
49 while (ReportSize)
50 {
51 uint32_t ReportItemData = 0;
52
53 switch (*ReportData & DATA_SIZE_MASK)
54 {
55 case DATA_SIZE_4:
56 ReportItemData = *((uint32_t*)(ReportData + 1));
57 break;
58 case DATA_SIZE_2:
59 ReportItemData = *((uint16_t*)(ReportData + 1));
60 break;
61 case DATA_SIZE_1:
62 ReportItemData = *((uint8_t*)(ReportData + 1));
63 break;
64 }
65
66 switch (*ReportData & (TYPE_MASK | TAG_MASK))
67 {
68 case (TYPE_GLOBAL | TAG_GLOBAL_PUSH):
69 if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH])
70 return HID_PARSE_HIDStackOverflow;
71
72 memcpy((CurrStateTable - 1),
73 CurrStateTable,
74 sizeof(HID_ReportItem_t));
75
76 CurrStateTable++;
77 break;
78 case (TYPE_GLOBAL | TAG_GLOBAL_POP):
79 if (CurrStateTable == &StateTable[0])
80 return HID_PARSE_HIDStackUnderflow;
81
82 CurrStateTable--;
83 break;
84 case (TYPE_GLOBAL | TAG_GLOBAL_USAGEPAGE):
85 CurrStateTable->Attributes.Usage.Page = ReportItemData;
86 break;
87 case (TYPE_GLOBAL | TAG_GLOBAL_LOGICALMIN):
88 CurrStateTable->Attributes.Logical.Minimum = ReportItemData;
89 break;
90 case (TYPE_GLOBAL | TAG_GLOBAL_LOGICALMAX):
91 CurrStateTable->Attributes.Logical.Maximum = ReportItemData;
92 break;
93 case (TYPE_GLOBAL | TAG_GLOBAL_PHYSMIN):
94 CurrStateTable->Attributes.Physical.Minimum = ReportItemData;
95 break;
96 case (TYPE_GLOBAL | TAG_GLOBAL_PHYSMAX):
97 CurrStateTable->Attributes.Physical.Maximum = ReportItemData;
98 break;
99 case (TYPE_GLOBAL | TAG_GLOBAL_UNITEXP):
100 CurrStateTable->Attributes.Unit.Exponent = ReportItemData;
101 break;
102 case (TYPE_GLOBAL | TAG_GLOBAL_UNIT):
103 CurrStateTable->Attributes.Unit.Type = ReportItemData;
104 break;
105 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTSIZE):
106 CurrStateTable->Attributes.BitSize = ReportItemData;
107 break;
108 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTCOUNT):
109 CurrStateTable->ReportCount = ReportItemData;
110 break;
111 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTID):
112 CurrStateTable->ReportID = ReportItemData;
113 break;
114 case (TYPE_LOCAL | TAG_LOCAL_USAGE):
115 if (UsageStackSize == HID_USAGE_STACK_DEPTH)
116 return HID_PARSE_UsageStackOverflow;
117
118 UsageStack[UsageStackSize++] = ReportItemData;
119 break;
120 case (TYPE_LOCAL | TAG_LOCAL_USAGEMIN):
121 CurrStateTable->Attributes.Usage.MinMax.Minimum = ReportItemData;
122 break;
123 case (TYPE_LOCAL | TAG_LOCAL_USAGEMAX):
124 CurrStateTable->Attributes.Usage.MinMax.Maximum = ReportItemData;
125 break;
126 case (TYPE_MAIN | TAG_MAIN_COLLECTION):
127 if (CurrCollectionPath == NULL)
128 {
129 CurrCollectionPath = &ParserData->CollectionPaths[0];
130 }
131 else
132 {
133 HID_CollectionPath_t* ParentCollectionPath = CurrCollectionPath;
134
135 CurrCollectionPath = &ParserData->CollectionPaths[1];
136
137 while (CurrCollectionPath->Parent != NULL);
138 {
139 if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS])
140 return HID_PARSE_InsufficientCollectionPaths;
141
142 CurrCollectionPath++;
143 }
144
145 CurrCollectionPath->Parent = ParentCollectionPath;
146 }
147
148 CurrCollectionPath->Type = ReportItemData;
149 CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;
150
151 if (UsageStackSize)
152 {
153 CurrCollectionPath->Usage.Usage = UsageStack[0];
154
155 for (uint8_t i = 0; i < UsageStackSize; i++)
156 UsageStack[i] = UsageStack[i + 1];
157
158 UsageStackSize--;
159 }
160 else
161 {
162 CurrCollectionPath->Usage.Usage = 0;
163 }
164
165 break;
166 case (TYPE_MAIN | TAG_MAIN_ENDCOLLECTION):
167 if (CurrCollectionPath == NULL)
168 return HID_PARSE_UnexpectedEndCollection;
169
170 CurrCollectionPath = CurrCollectionPath->Parent;
171
172 break;
173 case (TYPE_MAIN | TAG_MAIN_INPUT):
174 case (TYPE_MAIN | TAG_MAIN_OUTPUT):
175 #if defined(HID_ENABLE_FEATURE_PROCESSING)
176 case (TYPE_MAIN | TAG_MAIN_FEATURE):
177 #endif
178 for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)
179 {
180 HID_ReportItem_t* CurrReportItem = &ParserData->ReportItems[ParserData->TotalReportItems];
181
182 if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)
183 return HID_PARSE_InsufficientReportItems;
184
185 memcpy(&CurrReportItem->Attributes,
186 &CurrStateTable->Attributes,
187 sizeof(HID_ReportItem_Attributes_t));
188
189 CurrReportItem->ItemFlags = ReportItemData;
190 CurrReportItem->CollectionPath = CurrCollectionPath;
191 CurrReportItem->ReportID = CurrStateTable->ReportID;
192
193 if (UsageStackSize)
194 {
195 CurrReportItem->Attributes.Usage.Usage = UsageStack[0];
196
197 for (uint8_t i = 0; i < UsageStackSize; i++)
198 UsageStack[i] = UsageStack[i + 1];
199
200 UsageStackSize--;
201 }
202 else
203 {
204 CurrReportItem->Attributes.Usage.Usage = 0;
205 }
206
207 switch (*ReportData & TAG_MASK)
208 {
209 case TAG_MAIN_INPUT:
210 CurrReportItem->ItemType = REPORT_ITEM_TYPE_In;
211 CurrReportItem->BitOffset = BitOffsetIn;
212
213 BitOffsetIn += CurrStateTable->Attributes.BitSize;
214
215 break;
216 case TAG_MAIN_OUTPUT:
217 CurrReportItem->ItemType = REPORT_ITEM_TYPE_Out;
218 CurrReportItem->BitOffset = BitOffsetOut;
219
220 BitOffsetOut += CurrStateTable->Attributes.BitSize;
221
222 break;
223 #if defined(HID_ENABLE_FEATURE_PROCESSING)
224 case TAG_MAIN_FEATURE:
225 CurrReportItem->ItemType = REPORT_ITEM_TYPE_Feature;
226 CurrReportItem->BitOffset = BitOffsetFeature;
227
228 BitOffsetFeature += CurrStateTable->Attributes.BitSize;
229
230 break;
231 #endif
232 }
233
234 #if !defined(HID_INCLUDE_CONSTANT_DATA_ITEMS)
235 if (!(ReportItemData & IOF_CONSTANT))
236 ParserData->TotalReportItems++;
237 #else
238 ParserData->TotalReportItems++;
239 #endif
240 }
241
242 UsageStackSize = 0;
243
244 break;
245 }
246
247 if ((*ReportData & TYPE_MASK) == TYPE_MAIN)
248 {
249 CurrStateTable->Attributes.Usage.MinMax.Minimum = 0;
250 CurrStateTable->Attributes.Usage.MinMax.Maximum = 0;
251 UsageStackSize = 0;
252 }
253
254 switch (*ReportData & DATA_SIZE_MASK)
255 {
256 case DATA_SIZE_4:
257 ReportSize -= 5;
258 ReportData += 5;
259 break;
260 case DATA_SIZE_2:
261 ReportSize -= 3;
262 ReportData += 3;
263 break;
264 case DATA_SIZE_1:
265 ReportSize -= 2;
266 ReportData += 2;
267 break;
268 case DATA_SIZE_0:
269 ReportSize -= 1;
270 ReportData += 1;
271 break;
272 }
273 }
274
275 return HID_PARSE_Successful;
276 }
277
278 bool USB_GetHIDReportItemInfo(const uint8_t* ReportData, HID_ReportItem_t* const ReportItem)
279 {
280 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
281 uint16_t CurrentBit = ReportItem->BitOffset;
282 uint32_t BitMask = (1 << 0);
283
284 ReportItem->Value = 0;
285
286 if (ReportItem->ReportID)
287 {
288 if (ReportItem->ReportID != ReportData[0])
289 return false;
290
291 ReportData++;
292 }
293
294 while (DataBitsRem--)
295 {
296 if (ReportData[CurrentBit / 8] & (1 << (CurrentBit % 8)))
297 ReportItem->Value |= BitMask;
298
299 CurrentBit++;
300 BitMask <<= 1;
301 }
302
303 return true;
304 }
305
306 void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem)
307 {
308 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
309 uint16_t CurrentBit = ReportItem->BitOffset;
310 uint32_t BitMask = (1 << 0);
311
312 if (ReportItem->ReportID)
313 {
314 ReportData[0] = ReportItem->ReportID;
315 ReportData++;
316 }
317
318 while (DataBitsRem--)
319 {
320 if (ReportItem->Value & (1 << (CurrentBit % 8)))
321 ReportData[CurrentBit / 8] |= BitMask;
322
323 CurrentBit++;
324 BitMask <<= 1;
325 }
326 }