Skip to content

Commit aa6d2a7

Browse files
committed
Tidy USBSerial::attach() methods
Martino previously updated the attach() methods on the USBSerial object to handle more than one callback to be called when data is received. I updated that code to: * Check that the _rx callback array doesn't overflow. * Prepended a "_" prefix to the _rx and _howManyCallbacks members to make them consistent with the other members in this class and its parent USBCDC class.
1 parent b17d263 commit aa6d2a7

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

cores/arduino/USB/PluggableUSBSerial.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
157157
{
158158
USBCDC::lock();
159159

160-
if ((mptr != NULL) && (tptr != NULL)) {
161-
rx[howManyCallbacks++] = ::mbed::Callback<void()>(mptr, tptr);
160+
if ((mptr != NULL) && (tptr != NULL) && (_howManyCallbacks < sizeof(_rx)/sizeof(_rx[0]))) {
161+
_rx[_howManyCallbacks++] = ::mbed::Callback<void()>(mptr, tptr);
162162
}
163163

164164
USBCDC::unlock();
@@ -173,8 +173,8 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
173173
{
174174
USBCDC::lock();
175175

176-
if (fptr != NULL) {
177-
rx[howManyCallbacks++] = ::mbed::Callback<void()>(fptr);
176+
if ((fptr != NULL) && (_howManyCallbacks < sizeof(_rx)/sizeof(_rx[0]))) {
177+
_rx[_howManyCallbacks++] = ::mbed::Callback<void()>(fptr);
178178
}
179179

180180
USBCDC::unlock();
@@ -189,7 +189,9 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
189189
{
190190
USBCDC::lock();
191191

192-
rx[howManyCallbacks++] = cb;
192+
if (_howManyCallbacks < sizeof(_rx)/sizeof(_rx[0])) {
193+
_rx[_howManyCallbacks++] = cb;
194+
}
193195

194196
USBCDC::unlock();
195197
}
@@ -273,9 +275,9 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
273275
}
274276

275277
private:
276-
::mbed::Callback<void()> rx[MAX_CALLBACKS_ON_IRQ];
277-
int howManyCallbacks = 0;
278278
void (*_settings_changed_callback)(int baud, int bits, int parity, int stop);
279+
::mbed::Callback<void()> _rx[MAX_CALLBACKS_ON_IRQ];
280+
size_t _howManyCallbacks = 0;
279281
};
280282
}
281283

cores/arduino/USB/USBSerial.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,12 @@ void USBSerial::data_rx()
9595
USBCDC::assert_locked();
9696

9797
//call a potential handler
98-
int i = 0;
99-
while (i < howManyCallbacks) {
100-
if (rx[i]) {
101-
rx[i].call();
98+
for (size_t i = 0 ; i < _howManyCallbacks ; i++) {
99+
if (_rx[i]) {
100+
_rx[i].call();
102101
} else {
103102
break;
104103
}
105-
i++;
106104
}
107105
}
108106

0 commit comments

Comments
 (0)