From 8f8a086eee160c548751336e8195f588a31f5ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Fri, 15 Apr 2016 11:59:14 +0200 Subject: [PATCH] use LENGTH instead of sizeof for array indexes --- lib/usb_cdcacm.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/usb_cdcacm.c b/lib/usb_cdcacm.c index 4d01ed2..9314fc0 100644 --- a/lib/usb_cdcacm.c +++ b/lib/usb_cdcacm.c @@ -34,6 +34,7 @@ #include // USB CDC library #include // synchronisation utilities +#include "global.h" // global utilities #include "usb_cdcacm.h" // USB CDC ACM header and definitions /** @brief USB CDC ACM device descriptor @@ -257,17 +258,17 @@ static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data * * even though it's optional in the CDC spec, and we don't * advertise it in the ACM functional descriptor. */ - char local_buf[10]; - struct usb_cdc_notification *notif = (void *)local_buf; + uint8_t reply[10] = {0}; + struct usb_cdc_notification *notif = (void *)reply; /* we echo signals back to host as notification. */ notif->bmRequestType = 0xA1; notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; notif->wValue = 0; notif->wIndex = 0; notif->wLength = 2; - local_buf[8] = req->wValue & 3; - local_buf[9] = 0; - usbd_ep_write_packet(usbd_dev, 0x83, local_buf, 10); + reply[8] = req->wValue & 3; + reply[9] = 0; + usbd_ep_write_packet(usbd_dev, 0x83, reply, LENGTH(reply)); break; case USB_CDC_REQ_SET_LINE_CODING: // ignore if length is wrong @@ -306,8 +307,8 @@ static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep) /* receive data */ usb_length = usbd_ep_read_packet(usbd_dev, 0x01, usb_data, sizeof(usb_data)); if (usb_length) { // copy received data - for (uint16_t i=0; i 64 ? 64 : tx_used); // length of data to be transmitted (respect max packet size) - usb_length = (usb_length > (sizeof(tx_buffer)-tx_i) ? sizeof(tx_buffer)-tx_i : usb_length); // since here we use the source array not as ring buffer, only go up to the end + usb_length = (usb_length > (LENGTH(tx_buffer)-tx_i) ? LENGTH(tx_buffer)-tx_i : usb_length); // since here we use the source array not as ring buffer, only go up to the end while (usb_length != usbd_ep_write_packet(usb_device, 0x82, (void*)(&tx_buffer[tx_i]), usb_length)); // ensure data is written into transmit buffer - tx_i = (tx_i+usb_length)%sizeof(tx_buffer); // update location on buffer + tx_i = (tx_i+usb_length)%LENGTH(tx_buffer); // update location on buffer tx_used -= usb_length; // update used size mutex_unlock(&tx_lock); // release lock } else { @@ -382,7 +383,7 @@ char cdcacm_getchar(void) __WFI(); // sleep until interrupt (not sure if it's a good idea here) } char to_return = rx_buffer[rx_i]; // get the next available character - rx_i = (rx_i+1)%sizeof(rx_buffer); // update used buffer + rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer rx_used--; // update used buffer cdcacm_received = rx_used; // update available data return to_return; @@ -394,12 +395,12 @@ void cdcacm_putchar(char c) return; } mutex_lock(&tx_lock); // get lock to prevent race condition - if (tx_used