Skip to content

Commit 5fa8980

Browse files
zyczkapi-no
authored andcommitted
applications: nrf_desktop: switch to 64bit timestamp in eventq
Switch to 64bit timestamp using k_uptime_get API to calculate timestamp differences in eventq utility. We do it in order to prevent overflows. Jira: NCSDK-34009 Signed-off-by: Jan Zyczkowski <[email protected]>
1 parent 1b20c06 commit 5fa8980

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

applications/nrf_desktop/src/modules/hid_state.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,10 @@ static void connect(const void *subscriber_id, uint8_t report_id)
765765

766766
if (!hid_eventq_is_empty(&rd->eventq)) {
767767
/* Remove all stale events from the queue. */
768-
uint32_t uptime = k_uptime_get_32();
768+
int64_t uptime = k_uptime_get();
769769

770770
if (uptime > CONFIG_DESKTOP_HID_REPORT_EXPIRATION) {
771-
uint32_t min_ts = uptime - CONFIG_DESKTOP_HID_REPORT_EXPIRATION;
771+
int64_t min_ts = uptime - CONFIG_DESKTOP_HID_REPORT_EXPIRATION;
772772

773773
hid_eventq_cleanup(&rd->eventq, min_ts);
774774
}
@@ -911,10 +911,10 @@ static void update_key(const struct hid_keymap *map, bool pressed)
911911
__ASSERT_NO_MSG(hid_eventq_is_full(&rd->eventq));
912912

913913
/* Queue is full. If not yet connected, try to drop old events and retry. */
914-
uint32_t uptime = k_uptime_get_32();
914+
int64_t uptime = k_uptime_get();
915915

916916
if (uptime > CONFIG_DESKTOP_HID_REPORT_EXPIRATION) {
917-
uint32_t min_ts = uptime - CONFIG_DESKTOP_HID_REPORT_EXPIRATION;
917+
int64_t min_ts = uptime - CONFIG_DESKTOP_HID_REPORT_EXPIRATION;
918918

919919
hid_eventq_cleanup(&rd->eventq, min_ts);
920920
}

applications/nrf_desktop/src/util/hid_eventq.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct hid_eventq_data {
2121
struct hid_eventq_event {
2222
sys_snode_t node;
2323
struct hid_eventq_data data;
24-
uint32_t timestamp;
24+
int64_t timestamp;
2525
};
2626

2727

@@ -73,7 +73,7 @@ static void drop_oldest_hid_events(struct hid_eventq *q)
7373
/* Try to remove events but only if key release was generated for each removed key
7474
* press.
7575
*/
76-
uint32_t node_timestamp = CONTAINER_OF(i, struct hid_eventq_event, node)->timestamp;
76+
int64_t node_timestamp = CONTAINER_OF(i, struct hid_eventq_event, node)->timestamp;
7777

7878
/* Use incremented event timestamp to drop the event. */
7979
hid_eventq_cleanup(q, node_timestamp + 1);
@@ -107,11 +107,11 @@ int hid_eventq_keypress_enqueue(struct hid_eventq *q, uint16_t id, bool pressed,
107107
return -ENOMEM;
108108
}
109109

110-
evt->timestamp = k_uptime_get_32();
110+
evt->timestamp = k_uptime_get();
111111
evt->data.key_id = id;
112112
evt->data.pressed = pressed;
113113

114-
LOG_DBG("q:%p, ts:%u, id:%" PRIu16 ", %s",
114+
LOG_DBG("q:%p, ts:%" PRId64 ", id:%" PRIu16 ", %s",
115115
(void *)q, evt->timestamp, id, pressed ? "press" : "release");
116116

117117
/* Add a new event to the queue. */
@@ -138,7 +138,7 @@ int hid_eventq_keypress_dequeue(struct hid_eventq *q, uint16_t *id, bool *presse
138138
*id = evt->data.key_id;
139139
*pressed = evt->data.pressed;
140140

141-
LOG_DBG("q:%p, ts:%u, id:%" PRIu16 ", %s",
141+
LOG_DBG("q:%p, ts:%" PRId64 ", id:%" PRIu16 ", %s",
142142
(void *)q, evt->timestamp, *id, *pressed ? "press" : "release");
143143

144144
k_free(evt);
@@ -185,7 +185,7 @@ void hid_eventq_reset(struct hid_eventq *q)
185185
__ASSERT_NO_MSG(sys_slist_is_empty(&q->root));
186186
}
187187

188-
static sys_snode_t *get_first_valid_node(struct hid_eventq *q, uint32_t min_timestamp)
188+
static sys_snode_t *get_first_valid_node(struct hid_eventq *q, int64_t min_timestamp)
189189
{
190190
sys_snode_t *i;
191191

@@ -234,11 +234,11 @@ static sys_snode_t *get_keypress_release_node(struct hid_eventq *q, struct hid_e
234234
return NULL;
235235
}
236236

237-
void hid_eventq_cleanup(struct hid_eventq *q, uint32_t min_timestamp)
237+
void hid_eventq_cleanup(struct hid_eventq *q, int64_t min_timestamp)
238238
{
239239
__ASSERT_NO_MSG(hid_eventq_is_initialized(q));
240240

241-
LOG_DBG("q:%p, min_timestamp:%" PRIu32, (void *)q, min_timestamp);
241+
LOG_DBG("q:%p, min_timestamp:%" PRId64, (void *)q, min_timestamp);
242242

243243
sys_snode_t *first_valid = get_first_valid_node(q, min_timestamp);
244244

applications/nrf_desktop/src/util/hid_eventq.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void hid_eventq_reset(struct hid_eventq *q);
119119
* @param[in] q HID event queue object.
120120
* @param[in] min_timestamp Minimal valid timestamp.
121121
*/
122-
void hid_eventq_cleanup(struct hid_eventq *q, uint32_t min_timestamp);
122+
void hid_eventq_cleanup(struct hid_eventq *q, int64_t min_timestamp);
123123

124124
#ifdef __cplusplus
125125
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ nRF Desktop
261261

262262
* The :ref:`nrf_desktop_hid_eventq`.
263263
The utility can be used by an application module to temporarily queue HID events related to keypresses (button press or release) to handle them later.
264+
The utility uses 64-bit timestamps to prevent overflow issues.
264265
* The :ref:`nrf_desktop_hid_keymap`.
265266
The utility can be used by an application module to map an application-specific key ID to a HID report ID and HID usage ID pair according to statically defined user configuration.
266267
The :file:`hid_keymap.h` file was moved from the :file:`configuration/common` directory to the :file:`src/util` directory.

0 commit comments

Comments
 (0)