Debian Codegraph @

ports/stm32/usbhost/Class/HID/Src/usbh_hid_parser.c

raw
1 /**
2 ******************************************************************************
3 * @file usbh_hid_parser.c
4 * @author MCD Application Team
5 * @version V3.0.0
6 * @date 18-February-2014
7 * @brief This file is the HID Layer Handlers for USB Host HID class.
8 ******************************************************************************
9 * @attention
10 *
11 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
12 *
13 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14 * You may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at:
16 *
17 * http://www.st.com/software_license_agreement_liberty_v2
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 ******************************************************************************
26 */
27 /* Includes ------------------------------------------------------------------*/
28 #include "usbh_hid_parser.h"
29
30
31 /** @addtogroup USBH_LIB
32 * @{
33 */
34
35 /** @addtogroup USBH_CLASS
36 * @{
37 */
38
39 /** @addtogroup USBH_HID_CLASS
40 * @{
41 */
42
43 /** @defgroup USBH_HID_PARSER
44 * @brief This file includes HID parsers for USB Host HID class.
45 * @{
46 */
47
48 /** @defgroup USBH_HID_PARSER_Private_TypesDefinitions
49 * @{
50 */
51 /**
52 * @}
53 */
54
55
56 /** @defgroup USBH_HID_PARSER_Private_Defines
57 * @{
58 */
59 /**
60 * @}
61 */
62
63
64 /** @defgroup USBH_HID_PARSER_Private_Macros
65 * @{
66 */
67 /**
68 * @}
69 */
70
71 /** @defgroup USBH_HID_PARSER_Private_FunctionPrototypes
72 * @{
73 */
74
75 /**
76 * @}
77 */
78
79
80 /** @defgroup USBH_HID_PARSER_Private_Variables
81 * @{
82 */
83
84 /**
85 * @}
86 */
87
88
89 /** @defgroup USBH_HID_PARSER_Private_Functions
90 * @{
91 */
92
93 /**
94 * @brief HID_ReadItem
95 * The function read a report item.
96 * @param ri: report item
97 * @param ndx: report index
98 * @retval status (0 : fail / otherwise: item value)
99 */
100 uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx)
101 {
102 uint32_t val=0;
103 uint32_t x=0;
104 uint32_t bofs;
105 uint8_t *data=ri->data;
106 uint8_t shift=ri->shift;
107
108 /* get the logical value of the item */
109
110 /* if this is an array, wee may need to offset ri->data.*/
111 if (ri->count > 0)
112 {
113 /* If app tries to read outside of the array. */
114 if (ri->count <= ndx)
115 {
116 return(0);
117 }
118
119 /* calculate bit offset */
120 bofs = ndx*ri->size;
121 bofs += shift;
122 /* calculate byte offset + shift pair from bit offset. */
123 data+=bofs/8;
124 shift=(uint8_t)(bofs%8);
125 }
126 /* read data bytes in little endian order */
127 for(x=0; x < ((ri->size & 0x7) ? (ri->size/8)+1 : (ri->size/8)); x++)
128 {
129 val=(uint32_t)(*data << (x*8));
130 }
131 val=(val >> shift) & ((1<<ri->size)-1);
132
133 if (val < ri->logical_min || val > ri->logical_max)
134 {
135 return(0);
136 }
137
138 /* convert logical value to physical value */
139 /* See if the number is negative or not. */
140 if ((ri->sign) && (val & (1<<(ri->size-1))))
141 {
142 /* yes, so sign extend value to 32 bits. */
143 int vs=(int)((-1 & ~((1<<(ri->size))-1)) | val);
144
145 if(ri->resolution == 1)
146 {
147 return((uint32_t)vs);
148 }
149 return((uint32_t)(vs*ri->resolution));
150 }
151 else
152 {
153 if(ri->resolution == 1)
154 {
155 return(val);
156 }
157 return(val*ri->resolution);
158 }
159 }
160
161 /**
162 * @brief HID_WriteItem
163 * The function write a report item.
164 * @param ri: report item
165 * @param ndx: report index
166 * @retval status (1: fail/ 0 : Ok)
167 */
168 uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx)
169 {
170 uint32_t x;
171 uint32_t mask;
172 uint32_t bofs;
173 uint8_t *data=ri->data;
174 uint8_t shift=ri->shift;
175
176 if (value < ri->physical_min || value > ri->physical_max)
177 {
178 return(1);
179 }
180
181 /* if this is an array, wee may need to offset ri->data.*/
182 if (ri->count > 0)
183 {
184 /* If app tries to read outside of the array. */
185 if (ri->count >= ndx)
186 {
187 return(0);
188 }
189 /* calculate bit offset */
190 bofs = ndx*ri->size;
191 bofs += shift;
192 /* calculate byte offset + shift pair from bit offset. */
193 data+=bofs/8;
194 shift=(uint8_t)(bofs%8);
195
196 }
197
198 /* Convert physical value to logical value. */
199 if (ri->resolution != 1)
200 {
201 value=value/ri->resolution;
202 }
203
204 /* Write logical value to report in little endian order. */
205 mask=(uint32_t)((1<<ri->size)-1);
206 value = (value & mask) << shift;
207
208 for(x=0; x < ((ri->size & 0x7) ? (ri->size/8)+1 : (ri->size/8)); x++)
209 {
210 *(ri->data+x)=(uint8_t)((*(ri->data+x) & ~(mask>>(x*8))) | ((value>>(x*8)) & (mask>>(x*8))));
211 }
212 return(0);
213 }
214
215 /**
216 * @}
217 */
218
219 /**
220 * @}
221 */
222
223 /**
224 * @}
225 */
226
227 /**
228 * @}
229 */
230
231
232 /**
233 * @}
234 */
235 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/