Skip to content

Commit 2d9c703

Browse files
committed
enhance neopixel service
hardcode pixel to 10 even for CLUE to prevent invalid len write to data
1 parent 7c70043 commit 2d9c703

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t buf
109109
// Add Characteristic
110110
_data.setProperties(CHR_PROPS_WRITE);
111111
_data.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN);
112-
_data.setMaxLen(3 + bufsize); // start (2 byte) + flags (1 byte)
112+
// start (2 byte) + flags (1 byte)
113+
// TODO: for backward compatible with current CPB app, force to at least 10 pixels
114+
_data.setMaxLen(3 + 30 /* bufsize */);
113115
VERIFY_STATUS( _data.begin() );
114116

115117
// Add Characteristic
@@ -124,28 +126,36 @@ err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t buf
124126
return ERROR_NONE;
125127
}
126128

127-
//--------------------------------------------------------------------+
128-
// Static callbacks
129-
//--------------------------------------------------------------------+
130-
void BLEAdafruitAddressablePixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len)
129+
void BLEAdafruitAddressablePixel::_pixel_write_handler(uint16_t conn_hdl, uint8_t* data, uint16_t len)
131130
{
132131
(void) conn_hdl;
133132

134133
if (len < 3) return;
135134

136-
BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService();
137-
138135
uint16_t index;
139136
memcpy(&index, data, 2);
140137
uint8_t flag = data[2];
141138

142-
uint8_t* buffer = svc._neo->getPixels() + index;
139+
// pixel staring buffer
140+
uint8_t* pixbuf = _neo->getPixels() + index;
141+
142+
// limit copied bytes up to strip's boundary
143+
uint16_t copied_count = max16(_bufsize.read16(), index) - index;
144+
copied_count = min16(len-3, copied_count);
145+
146+
PRINT_INT(copied_count);
143147

144-
memcpy(buffer, data+3, len-3);
148+
if (copied_count) memcpy(pixbuf, data+3, copied_count);
145149

146150
// show flag
147-
if ( flag & 0x01 )
148-
{
149-
svc._neo->show();
150-
}
151+
if ( flag & 0x01 ) _neo->show();
152+
}
153+
154+
//--------------------------------------------------------------------+
155+
// Static callbacks
156+
//--------------------------------------------------------------------+
157+
void BLEAdafruitAddressablePixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len)
158+
{
159+
BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService();
160+
svc._pixel_write_handler(conn_hdl, data, len);
151161
}

libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class BLEAdafruitAddressablePixel : public BLEService
4949

5050
err_t begin(uint8_t pin, uint8_t type, uint16_t bufsize);
5151

52+
void _pixel_write_handler(uint16_t conn_hdl, uint8_t* data, uint16_t len);
53+
5254
static void pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len);
5355
};
5456

variants/clue_nrf52840/variant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ extern "C"
4242

4343
// LEDs
4444
#define PIN_LED1 (17)
45-
#define PIN_NEOPIXEL (18)
4645
#define PIN_LED2 (43) // dual white LEDs
46+
#define PIN_NEOPIXEL (18)
4747

4848
#define LED_BUILTIN PIN_LED1
4949

0 commit comments

Comments
 (0)