Skip to content

Commit 4731c9b

Browse files
MarekPietakapi-no
authored andcommitted
applications: nrf_desktop: Use bsearch from C library
This simplifies maintenance and allows to use Picolibc. Jira: NCSDK-32238 Signed-off-by: Marek Pieta <[email protected]> Signed-off-by: Divya Pillai <[email protected]>
1 parent 5a72d78 commit 4731c9b

File tree

3 files changed

+10
-75
lines changed

3 files changed

+10
-75
lines changed

applications/nrf_desktop/src/modules/fn_keys.c

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <sys/types.h>
88
#include <zephyr/kernel.h>
99
#include <zephyr/sys/util.h>
10+
#include <stdlib.h>
1011
#include <zephyr/settings/settings.h>
1112

1213
#define MODULE fn_keys
@@ -72,38 +73,6 @@ static void validate_enabled_fn_keys(void)
7273
}
7374
}
7475

75-
static void *bsearch(const void *key, const uint8_t *base,
76-
size_t elem_num, size_t elem_size,
77-
int (*compare)(const void *, const void *))
78-
{
79-
__ASSERT_NO_MSG(base);
80-
__ASSERT_NO_MSG(compare);
81-
__ASSERT_NO_MSG(elem_num <= SSIZE_MAX);
82-
83-
if (!elem_num) {
84-
return NULL;
85-
}
86-
87-
ssize_t lower = 0;
88-
ssize_t upper = elem_num - 1;
89-
90-
while (upper >= lower) {
91-
ssize_t m = (lower + upper) / 2;
92-
int cmp = compare(key, base + (elem_size * m));
93-
94-
if (cmp == 0) {
95-
return (void *)(base + (elem_size * m));
96-
} else if (cmp < 0) {
97-
upper = m - 1;
98-
} else {
99-
lower = m + 1;
100-
}
101-
}
102-
103-
/* key not found */
104-
return NULL;
105-
}
106-
10776
static int key_id_compare(const void *a, const void *b)
10877
{
10978
const uint16_t *p_a = a;
@@ -117,10 +86,10 @@ static bool fn_key_enabled(uint16_t key_id)
11786
validate_enabled_fn_keys();
11887

11988
uint16_t *p = bsearch(&key_id,
120-
(uint8_t *)fn_keys,
121-
ARRAY_SIZE(fn_keys),
122-
sizeof(fn_keys[0]),
123-
key_id_compare);
89+
fn_keys,
90+
ARRAY_SIZE(fn_keys),
91+
sizeof(fn_keys[0]),
92+
key_id_compare);
12493

12594
return (p != NULL);
12695
}

applications/nrf_desktop/src/modules/hid_state.c

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <zephyr/sys/slist.h>
1818
#include <zephyr/sys/util.h>
1919
#include <zephyr/sys/byteorder.h>
20+
#include <stdlib.h>
2021

2122
#include <caf/events/led_event.h>
2223
#include <caf/events/button_event.h>
@@ -149,43 +150,6 @@ static bool report_send(struct report_state *rs,
149150
bool send_always);
150151

151152

152-
/**@brief Binary search. Input array must be already sorted.
153-
*
154-
* bsearch is also available from newlib libc, but including
155-
* the library takes around 10K of FLASH.
156-
*/
157-
static void *bsearch(const void *key, const uint8_t *base,
158-
size_t elem_num, size_t elem_size,
159-
int (*compare)(const void *, const void *))
160-
{
161-
__ASSERT_NO_MSG(base);
162-
__ASSERT_NO_MSG(compare);
163-
__ASSERT_NO_MSG(elem_num <= SSIZE_MAX);
164-
165-
if (!elem_num) {
166-
return NULL;
167-
}
168-
169-
ssize_t lower = 0;
170-
ssize_t upper = elem_num - 1;
171-
172-
while (upper >= lower) {
173-
ssize_t m = (lower + upper) / 2;
174-
int cmp = compare(key, base + (elem_size * m));
175-
176-
if (cmp == 0) {
177-
return (void *)(base + (elem_size * m));
178-
} else if (cmp < 0) {
179-
upper = m - 1;
180-
} else {
181-
lower = m + 1;
182-
}
183-
}
184-
185-
/* key not found */
186-
return NULL;
187-
}
188-
189153
/**@brief Compare Key ID in HID Keymap entries. */
190154
static int hid_keymap_compare(const void *a, const void *b)
191155
{
@@ -203,7 +167,7 @@ static struct hid_keymap *hid_keymap_get(uint16_t key_id)
203167
};
204168

205169
struct hid_keymap *map = bsearch(&key,
206-
(uint8_t *)hid_keymap,
170+
hid_keymap,
207171
ARRAY_SIZE(hid_keymap),
208172
sizeof(key),
209173
hid_keymap_compare);
@@ -516,7 +480,7 @@ static bool key_value_set(struct items *items, uint16_t usage_id, int16_t value)
516480
};
517481

518482
p_item = bsearch(&i,
519-
(uint8_t *)items->item,
483+
items->item,
520484
ARRAY_SIZE(items->item),
521485
sizeof(items->item[0]),
522486
usage_id_compare);

doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ nRF Desktop
259259
Supporting Bluetooth LE legacy pairing makes devices vulnerable for a downgrade attack.
260260
The :kconfig:option:`CONFIG_BT_SMP_SC_PAIR_ONLY` Kconfig option is enabled by default in Zephyr.
261261
If you still need to support the Bluetooth LE legacy pairing, you need to disable the option in the configuration.
262+
* :ref:`nrf_desktop_hid_state` and :ref:`nrf_desktop_fn_keys` to use :c:func:`bsearch` implementation from C library.
263+
This simplifies maintenance and allows you to use Picolibc (:kconfig:option:`CONFIG_PICOLIBC`).
262264

263265
* Added:
264266

0 commit comments

Comments
 (0)