Skip to content

Commit 16518dc

Browse files
author
Ben Avison
committed
Refactor pointer arithmetic on void*
This is a GCC extension, illegal in ISO C. IAR generates errors Pa152 'these operand types cannot be used here' and Pe852 'expression must be a pointer to a complete object type'. Replace with uint8_t*.
1 parent 6dfc857 commit 16518dc

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

src/class/video/video_device.c

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ typedef struct TU_ATTR_PACKED {
8080

8181
/* video control interface */
8282
typedef struct TU_ATTR_PACKED {
83-
void const *beg; /* The head of the first video control interface descriptor */
83+
uint8_t const *beg; /* The head of the first video control interface descriptor */
8484
uint16_t len; /* Byte length of the descriptors */
8585
uint16_t cur; /* offset for current video control interface */
8686
uint8_t stm[CFG_TUD_VIDEO_STREAMING]; /* Indices of streaming interface */
@@ -108,19 +108,19 @@ static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */
108108
* @param[in] desc interface descriptor
109109
*
110110
* @return bInterfaceNumber */
111-
static inline uint8_t _desc_itfnum(void const *desc)
111+
static inline uint8_t _desc_itfnum(uint8_t const *desc)
112112
{
113-
return ((uint8_t const*)desc)[2];
113+
return desc[2];
114114
}
115115

116116
/** Get endpoint address from the endpoint descriptor
117117
*
118118
* @param[in] desc endpoint descriptor
119119
*
120120
* @return bEndpointAddress */
121-
static inline uint8_t _desc_ep_addr(void const *desc)
121+
static inline uint8_t _desc_ep_addr(uint8_t const *desc)
122122
{
123-
return ((uint8_t const*)desc)[2];
123+
return desc[2];
124124
}
125125

126126
/** Get instance of streaming interface
@@ -146,7 +146,7 @@ static tusb_desc_vc_itf_t const* _get_desc_vc(videod_interface_t const *self)
146146
static tusb_desc_vs_itf_t const* _get_desc_vs(videod_streaming_interface_t const *self)
147147
{
148148
if (!self->desc.cur) return NULL;
149-
void const *desc = _videod_itf[self->index_vc].beg;
149+
uint8_t const *desc = _videod_itf[self->index_vc].beg;
150150
return (tusb_desc_vs_itf_t const*)(desc + self->desc.cur);
151151
}
152152

@@ -158,9 +158,9 @@ static tusb_desc_vs_itf_t const* _get_desc_vs(videod_streaming_interface_t const
158158
*
159159
* @return The pointer for interface descriptor.
160160
* @retval end did not found interface descriptor */
161-
static void const* _find_desc(void const *beg, void const *end, uint_fast8_t desc_type)
161+
static uint8_t const* _find_desc(uint8_t const *beg, uint8_t const *end, uint_fast8_t desc_type)
162162
{
163-
void const *cur = beg;
163+
uint8_t const *cur = beg;
164164
while ((cur < end) && (desc_type != tu_desc_type(cur))) {
165165
cur = tu_desc_next(cur);
166166
}
@@ -177,14 +177,13 @@ static void const* _find_desc(void const *beg, void const *end, uint_fast8_t des
177177
*
178178
* @return The pointer for interface descriptor.
179179
* @retval end did not found interface descriptor */
180-
static void const* _find_desc_3(void const *beg, void const *end,
181-
uint_fast8_t desc_type,
182-
uint_fast8_t element_0,
183-
uint_fast8_t element_1)
180+
static uint8_t const* _find_desc_3(uint8_t const *beg, uint8_t const *end,
181+
uint_fast8_t desc_type,
182+
uint_fast8_t element_0,
183+
uint_fast8_t element_1)
184184
{
185-
for (void const *cur = beg; cur < end; cur = _find_desc(cur, end, desc_type)) {
186-
uint8_t const *p = (uint8_t const *)cur;
187-
if ((p[2] == element_0) && (p[3] == element_1)) {
185+
for (uint8_t const *cur = beg; cur < end; cur = _find_desc(cur, end, desc_type)) {
186+
if ((cur[2] == element_0) && (cur[3] == element_1)) {
188187
return cur;
189188
}
190189
cur = tu_desc_next(cur);
@@ -199,9 +198,9 @@ static void const* _find_desc_3(void const *beg, void const *end,
199198
*
200199
* @return The pointer for interface descriptor.
201200
* @retval end did not found interface descriptor */
202-
static void const* _next_desc_itf(void const *beg, void const *end)
201+
static uint8_t const* _next_desc_itf(void const *beg, uint8_t const *end)
203202
{
204-
void const *cur = beg;
203+
uint8_t const *cur = beg;
205204
uint_fast8_t itfnum = ((tusb_desc_interface_t const*)cur)->bInterfaceNumber;
206205
while ((cur < end) &&
207206
(itfnum == ((tusb_desc_interface_t const*)cur)->bInterfaceNumber)) {
@@ -219,7 +218,7 @@ static void const* _next_desc_itf(void const *beg, void const *end)
219218
*
220219
* @return The pointer for interface descriptor.
221220
* @retval end did not found interface descriptor */
222-
static inline void const* _find_desc_itf(void const *beg, void const *end, uint_fast8_t itfnum, uint_fast8_t altnum)
221+
static inline uint8_t const* _find_desc_itf(uint8_t const *beg, uint8_t const *end, uint_fast8_t itfnum, uint_fast8_t altnum)
223222
{
224223
return _find_desc_3(beg, end, TUSB_DESC_INTERFACE, itfnum, altnum);
225224
}
@@ -233,9 +232,9 @@ static inline void const* _find_desc_itf(void const *beg, void const *end, uint_
233232
*
234233
* @return The pointer for endpoint descriptor.
235234
* @retval end did not found endpoint descriptor */
236-
static void const* _find_desc_ep(void const *beg, void const *end)
235+
static uint8_t const* _find_desc_ep(uint8_t const *beg, uint8_t const *end)
237236
{
238-
for (void const *cur = beg; cur < end; cur = tu_desc_next(cur)) {
237+
for (uint8_t const *cur = beg; cur < end; cur = tu_desc_next(cur)) {
239238
uint_fast8_t desc_type = tu_desc_type(cur);
240239
if (TUSB_DESC_ENDPOINT == desc_type) return cur;
241240
if (TUSB_DESC_INTERFACE == desc_type) break;
@@ -251,17 +250,17 @@ static void const* _find_desc_ep(void const *beg, void const *end)
251250
*
252251
* @return The pointer for interface descriptor.
253252
* @retval end did not found interface descriptor */
254-
static void const* _find_desc_entity(void const *desc, uint_fast8_t entityid)
253+
static uint8_t const* _find_desc_entity(void const *desc, uint_fast8_t entityid)
255254
{
256255
tusb_desc_vc_itf_t const *vc = (tusb_desc_vc_itf_t const*)desc;
257-
void const *beg = vc;
258-
void const *end = beg + vc->std.bLength + vc->ctl.wTotalLength;
259-
for (void const *cur = beg; cur < end; cur = _find_desc(cur, end, TUSB_DESC_CS_INTERFACE)) {
256+
uint8_t const *beg = desc;
257+
uint8_t const *end = beg + vc->std.bLength + vc->ctl.wTotalLength;
258+
for (uint8_t const *cur = beg; cur < end; cur = _find_desc(cur, end, TUSB_DESC_CS_INTERFACE)) {
260259
tusb_desc_cs_video_entity_itf_t const *itf = (tusb_desc_cs_video_entity_itf_t const *)cur;
261260
if ((VIDEO_CS_ITF_VC_INPUT_TERMINAL <= itf->bDescriptorSubtype
262261
&& itf->bDescriptorSubtype < VIDEO_CS_ITF_VC_MAX)
263262
&& itf->bEntityId == entityid) {
264-
return itf;
263+
return cur;
265264
}
266265
cur = tu_desc_next(cur);
267266
}
@@ -272,18 +271,18 @@ static void const* _find_desc_entity(void const *desc, uint_fast8_t entityid)
272271
static inline void const* _end_of_streaming_descriptor(void const *desc)
273272
{
274273
tusb_desc_vs_itf_t const *vs = (tusb_desc_vs_itf_t const *)desc;
275-
return desc + vs->std.bLength + vs->stm.wTotalLength;
274+
return (uint8_t const *)desc + vs->std.bLength + vs->stm.wTotalLength;
276275
}
277276

278277
/** Find the first format descriptor with the specified format number. */
279-
static inline tusb_desc_cs_video_fmt_uncompressed_t const *_find_desc_format(void const *beg, void const *end, uint_fast8_t fmtnum)
278+
static inline tusb_desc_cs_video_fmt_uncompressed_t const *_find_desc_format(uint8_t const *beg, uint8_t const *end, uint_fast8_t fmtnum)
280279
{
281280
return (tusb_desc_cs_video_fmt_uncompressed_t const*)
282281
_find_desc_3(beg, end, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED, fmtnum);
283282
}
284283

285284
/** Find the first frame descriptor with the specified format number. */
286-
static inline tusb_desc_cs_video_frm_uncompressed_t const *_find_desc_frame(void const *beg, void const *end, uint_fast8_t frmnum)
285+
static inline tusb_desc_cs_video_frm_uncompressed_t const *_find_desc_frame(uint8_t const *beg, uint8_t const *end, uint_fast8_t frmnum)
287286
{
288287
return (tusb_desc_cs_video_frm_uncompressed_t const*)
289288
_find_desc_3(beg, end, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VS_FRAME_UNCOMPRESSED, frmnum);
@@ -485,9 +484,9 @@ static bool _close_vc_itf(uint8_t rhport, videod_interface_t *self)
485484
{
486485
tusb_desc_vc_itf_t const *vc = _get_desc_vc(self);
487486
/* The next descriptor after the class-specific VC interface header descriptor. */
488-
void const *cur = (void const*)vc + vc->std.bLength + vc->ctl.bLength;
487+
uint8_t const *cur = (uint8_t const*)vc + vc->std.bLength + vc->ctl.bLength;
489488
/* The end of the video control interface descriptor. */
490-
void const *end = (void const*)vc + vc->std.bLength + vc->ctl.wTotalLength;
489+
uint8_t const *end = (uint8_t const*)vc + vc->std.bLength + vc->ctl.wTotalLength;
491490
if (vc->std.bNumEndpoints) {
492491
/* Find the notification endpoint descriptor. */
493492
cur = _find_desc(cur, end, TUSB_DESC_ENDPOINT);
@@ -506,10 +505,10 @@ static bool _close_vc_itf(uint8_t rhport, videod_interface_t *self)
506505
static bool _open_vc_itf(uint8_t rhport, videod_interface_t *self, uint_fast8_t altnum)
507506
{
508507
TU_LOG2(" open VC %d\n", altnum);
509-
void const *beg = self->beg;
510-
void const *end = beg + self->len;
508+
uint8_t const *beg = self->beg;
509+
uint8_t const *end = beg + self->len;
511510
/* The first descriptor is a video control interface descriptor. */
512-
void const *cur = _find_desc_itf(beg, end, _desc_itfnum(beg), altnum);
511+
uint8_t const *cur = _find_desc_itf(beg, end, _desc_itfnum(beg), altnum);
513512
TU_LOG2(" cur %d\n", cur - beg);
514513
TU_VERIFY(cur < end);
515514

@@ -534,7 +533,7 @@ static bool _open_vc_itf(uint8_t rhport, videod_interface_t *self, uint_fast8_t
534533
/* Open the notification endpoint */
535534
TU_ASSERT(usbd_edpt_open(rhport, notif));
536535
}
537-
self->cur = (uint16_t) ((void const*)vc - beg);
536+
self->cur = (uint16_t) ((uint8_t const*)vc - beg);
538537
return true;
539538
}
540539

@@ -546,7 +545,7 @@ static bool _open_vs_itf(uint8_t rhport, videod_streaming_interface_t *stm, uint
546545
{
547546
uint_fast8_t i;
548547
TU_LOG2(" reopen VS %d\n", altnum);
549-
void const *desc = _videod_itf[stm->index_vc].beg;
548+
uint8_t const *desc = _videod_itf[stm->index_vc].beg;
550549

551550
/* Close endpoints of previous settings. */
552551
for (i = 0; i < TU_ARRAY_SIZE(stm->desc.ep); ++i) {
@@ -563,9 +562,9 @@ static bool _open_vs_itf(uint8_t rhport, videod_streaming_interface_t *stm, uint
563562
stm->offset = 0;
564563

565564
/* Find a alternate interface */
566-
void const *beg = desc + stm->desc.beg;
567-
void const *end = desc + stm->desc.end;
568-
void const *cur = _find_desc_itf(beg, end, _desc_itfnum(beg), altnum);
565+
uint8_t const *beg = desc + stm->desc.beg;
566+
uint8_t const *end = desc + stm->desc.end;
567+
uint8_t const *cur = _find_desc_itf(beg, end, _desc_itfnum(beg), altnum);
569568
TU_VERIFY(cur < end);
570569
uint_fast8_t numeps = ((tusb_desc_interface_t const *)cur)->bNumEndpoints;
571570
TU_ASSERT(numeps <= TU_ARRAY_SIZE(stm->desc.ep));
@@ -977,7 +976,7 @@ bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *bu
977976
if (!stm || !stm->desc.ep[0] || stm->buffer) return false;
978977

979978
/* Find EP address */
980-
void const *desc = _videod_itf[stm->index_vc].beg;
979+
uint8_t const *desc = _videod_itf[stm->index_vc].beg;
981980
uint8_t ep_addr = 0;
982981
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
983982
uint_fast16_t ofs_ep = stm->desc.ep[i];
@@ -1044,15 +1043,15 @@ uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
10441043
}
10451044
TU_ASSERT(ctl_idx < CFG_TUD_VIDEO, 0);
10461045

1047-
void const *end = (void const*)itf_desc + max_len;
1048-
self->beg = itf_desc;
1046+
uint8_t const *end = (uint8_t const*)itf_desc + max_len;
1047+
self->beg = (uint8_t const *)itf_desc;
10491048
self->len = max_len;
10501049
/*------------- Video Control Interface -------------*/
10511050
TU_VERIFY(_open_vc_itf(rhport, self, 0), 0);
10521051
tusb_desc_vc_itf_t const *vc = _get_desc_vc(self);
10531052
uint_fast8_t bInCollection = vc->ctl.bInCollection;
10541053
/* Find the end of the video interface descriptor */
1055-
void const *cur = _next_desc_itf(itf_desc, end);
1054+
uint8_t const *cur = _next_desc_itf(itf_desc, end);
10561055
for (uint8_t stm_idx = 0; stm_idx < bInCollection; ++stm_idx) {
10571056
videod_streaming_interface_t *stm = NULL;
10581057
/* find free streaming interface handle */
@@ -1085,7 +1084,7 @@ bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_
10851084
/* Identify which control interface to use */
10861085
uint_fast8_t itf;
10871086
for (itf = 0; itf < CFG_TUD_VIDEO; ++itf) {
1088-
void const *desc = _videod_itf[itf].beg;
1087+
uint8_t const *desc = _videod_itf[itf].beg;
10891088
if (!desc) continue;
10901089
if (itfnum == _desc_itfnum(desc)) break;
10911090
}
@@ -1101,7 +1100,7 @@ bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_
11011100
for (itf = 0; itf < CFG_TUD_VIDEO_STREAMING; ++itf) {
11021101
videod_streaming_interface_t *stm = &_videod_streaming_itf[itf];
11031102
if (!stm->desc.beg) continue;
1104-
void const *desc = _videod_itf[stm->index_vc].beg;
1103+
uint8_t const *desc = _videod_itf[stm->index_vc].beg;
11051104
if (itfnum == _desc_itfnum(desc + stm->desc.beg)) break;
11061105
}
11071106

@@ -1127,7 +1126,7 @@ bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
11271126
uint_fast16_t const ep_ofs = stm->desc.ep[0];
11281127
if (!ep_ofs) continue;
11291128
ctl = &_videod_itf[stm->index_vc];
1130-
void const *desc = ctl->beg;
1129+
uint8_t const *desc = ctl->beg;
11311130
if (ep_addr == _desc_ep_addr(desc + ep_ofs)) break;
11321131
}
11331132

0 commit comments

Comments
 (0)