Skip to content

Commit

Permalink
Fix long notifications not being shown and parsed correctly.
Browse files Browse the repository at this point in the history
Due to 8 bit variable overflow.
Due to parse buffer not being large enough.

Added warning and proper handling in case payload
from GadgetBridge is too big.
  • Loading branch information
jakkra committed Aug 19, 2023
1 parent 8600ed4 commit c30d0e9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ CONFIG_DISPLAY_FAST_WAKEUP=y
CONFIG_DEMO_BUILD=n

# Choose one or many of below
CONFIG_WATCHFACE_ANALOG=y
CONFIG_WATCHFACE_ANALOG=n
CONFIG_WATCHFACE_DIGITAL=y
CONFIG_WATCHFACE_MINIMAL=y

Expand Down
11 changes: 9 additions & 2 deletions app/src/ble_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ LOG_MODULE_REGISTER(ble_comm, LOG_LEVEL_DBG);

#define BLE_COMM_CONN_INT_UPDATE_TIMEOUT_MS 5000

#define MAX_GB_PACKET_LENGTH 1000

ZBUS_CHAN_DECLARE(ble_comm_data_chan);

typedef enum parse_state {
Expand Down Expand Up @@ -65,9 +67,9 @@ K_WORK_DELAYABLE_DEFINE(conn_interval_work, update_conn_interval_handler);

static struct bt_conn *current_conn;
static uint32_t max_send_len;
static uint8_t receive_buf[300];
static uint8_t receive_buf[MAX_GB_PACKET_LENGTH];
static uint8_t num_parsed_brackets;
static uint8_t parsed_data_index = 0;
static uint16_t parsed_data_index = 0;
static parse_state_t parse_state = WAIT_GB;

static on_data_cb_t data_parsed_cb;
Expand Down Expand Up @@ -368,6 +370,11 @@ static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data, uint1
for (int i = 0; i < len; i++) {
receive_buf[parsed_data_index] = data[i];
parsed_data_index++;
if (parsed_data_index >= MAX_GB_PACKET_LENGTH) {
LOG_ERR("Data from Gadgetbridge does not fit in MAX_GB_PACKET_LENGTH (%d)", MAX_GB_PACKET_LENGTH);
parse_state = WAIT_GB;
break;
}
if (data[i] == '{') {
num_parsed_brackets++;
} else if (data[i] == '}') {
Expand Down
4 changes: 2 additions & 2 deletions app/src/ble_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ static ssize_t on_receive(struct bt_conn *conn,
uint16_t offset,
uint8_t flags)
{
LOG_DBG("Received data, handle %d, conn %p",
attr->handle, (void *)conn);
LOG_DBG("Received data, handle %d, conn %p, len: %d",
attr->handle, (void *)conn, len);

if (callbacks->data_receive) {
callbacks->data_receive(conn, buf, len);
Expand Down

0 comments on commit c30d0e9

Please sign in to comment.