summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Johanson <peter@peterjohanson.com>2022-02-01 03:29:50 +0000
committerPete Johanson <peter@peterjohanson.com>2022-01-31 23:03:34 -0500
commit74307504280573aae5819afad29aceaf97b8344e (patch)
tree6b9face3d9f539d4b5b4f5f41f377f6ebe402123
parentb8700eaaa1e24837d6b9ff60430d706c555f9a2f (diff)
refactor(splits): Minor cleanups to periph invocation
* Add strlcpy from public domain version. * Leverage strlcpy to detect truncation of behavior dev strs, and log. * Use `offsetof` for cleaner detection on peripheral side.
-rw-r--r--app/CMakeLists.txt1
-rw-r--r--app/include/zmk/stdlib.h19
-rw-r--r--app/src/split/bluetooth/central.c9
-rw-r--r--app/src/split/bluetooth/service.c4
-rw-r--r--app/src/stdlib.c25
5 files changed, 55 insertions, 3 deletions
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 1e153fb..7681efa 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -23,6 +23,7 @@ zephyr_linker_sources(RODATA include/linker/zmk-events.ld)
# find_package(Zephyr) which defines the target.
target_include_directories(app PRIVATE include)
target_sources_ifdef(CONFIG_ZMK_SLEEP app PRIVATE src/power.c)
+target_sources(app PRIVATE src/stdlib.c)
target_sources(app PRIVATE src/activity.c)
target_sources(app PRIVATE src/kscan.c)
target_sources(app PRIVATE src/matrix_transform.c)
diff --git a/app/include/zmk/stdlib.h b/app/include/zmk/stdlib.h
new file mode 100644
index 0000000..fa8fe67
--- /dev/null
+++ b/app/include/zmk/stdlib.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2022 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#pragma once
+
+#include <stdlib.h> /* for size_t */
+
+/*
+ * ANSI C version of strlcpy
+ * Based on the NetBSD strlcpy man page.
+ *
+ * Nathan Myers <ncm-nospam@cantrip.org>, 2003/06/03
+ * Placed in the public domain.
+ */
+
+size_t strlcpy(char *dst, const char *src, size_t size); \ No newline at end of file
diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c
index 0d52d05..8a0e79e 100644
--- a/app/src/split/bluetooth/central.c
+++ b/app/src/split/bluetooth/central.c
@@ -17,6 +17,7 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
+#include <zmk/stdlib.h>
#include <zmk/ble.h>
#include <zmk/behavior.h>
#include <zmk/split/bluetooth/uuid.h>
@@ -549,8 +550,12 @@ int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *bi
.position = event.position,
.state = state ? 1 : 0,
}};
- strncpy(payload.behavior_dev, binding->behavior_dev, ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN - 1);
- payload.behavior_dev[ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN - 1] = '\0';
+ const size_t payload_dev_size = sizeof(payload.behavior_dev);
+ if (strlcpy(payload.behavior_dev, binding->behavior_dev, payload_dev_size) >=
+ payload_dev_size) {
+ LOG_ERR("Truncated behavior label %s to %s before invoking peripheral behavior",
+ log_strdup(binding->behavior_dev), log_strdup(payload.behavior_dev));
+ }
struct zmk_split_run_behavior_payload_wrapper wrapper = {.source = source, .payload = payload};
return split_bt_invoke_behavior_payload(wrapper);
diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c
index ca192d7..7de7850 100644
--- a/app/src/split/bluetooth/service.c
+++ b/app/src/split/bluetooth/service.c
@@ -51,8 +51,10 @@ static ssize_t split_svc_run_behavior(struct bt_conn *conn, const struct bt_gatt
// We run if:
// 1: We've gotten all the position/state/param data.
// 2: We have a null terminated string for the behavior device label.
+ const size_t behavior_dev_offset =
+ offsetof(struct zmk_split_run_behavior_payload, behavior_dev);
if ((end_addr > sizeof(struct zmk_split_run_behavior_data)) &&
- payload->behavior_dev[end_addr - sizeof(struct zmk_split_run_behavior_data) - 1] == '\0') {
+ payload->behavior_dev[end_addr - behavior_dev_offset - 1] == '\0') {
struct zmk_behavior_binding binding = {
.param1 = payload->data.param1,
.param2 = payload->data.param2,
diff --git a/app/src/stdlib.c b/app/src/stdlib.c
new file mode 100644
index 0000000..5bca169
--- /dev/null
+++ b/app/src/stdlib.c
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2022 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <zmk/stdlib.h>
+#include <string.h>
+
+/*
+ * ANSI C version of strlcpy
+ * Based on the NetBSD strlcpy man page.
+ *
+ * Nathan Myers <ncm-nospam@cantrip.org>, 2003/06/03
+ * Placed in the public domain.
+ */
+
+size_t strlcpy(char *dst, const char *src, size_t size) {
+ const size_t len = strlen(src);
+ if (size != 0) {
+ memcpy(dst, src, (len > size - 1) ? size - 1 : len);
+ dst[size - 1] = 0;
+ }
+ return len;
+}