diff --git a/core/include/cib.h b/core/include/cib.h index 70702d1b6e..1e9e725c03 100644 --- a/core/include/cib.h +++ b/core/include/cib.h @@ -86,6 +86,22 @@ static inline int cib_get(cib_t *__restrict cib) return -1; } +/** + * @brief Get the index of the next item in buffer without removing it. + * + * @param[in,out] cib corresponding *cib* to buffer. + * Must not be NULL. + * @return index of next item, -1 if the buffer is empty + */ +static inline int cib_peek(cib_t *__restrict cib) +{ + if (cib->write_count > cib->read_count) { + return (int) (cib->read_count & cib->mask); + } + + return -1; +} + /** * @brief Get index for item in buffer to put to. * diff --git a/tests/unittests/tests-core/tests-core-cib.c b/tests/unittests/tests-core/tests-core-cib.c index 7842652506..8991cdaab7 100644 --- a/tests/unittests/tests-core/tests-core-cib.c +++ b/tests/unittests/tests-core/tests-core-cib.c @@ -36,6 +36,20 @@ static void test_cib_get(void) TEST_ASSERT_EQUAL_INT(-1, cib_get(&cib)); } +static void test_cib_peek(void) +{ + cib_init(&cib, TEST_CIB_SIZE); + TEST_ASSERT_EQUAL_INT(-1, cib_peek(&cib)); + TEST_ASSERT_EQUAL_INT(0, cib_put(&cib)); + TEST_ASSERT_EQUAL_INT(0, cib_peek(&cib)); + TEST_ASSERT_EQUAL_INT(1, cib_put(&cib)); + TEST_ASSERT_EQUAL_INT(0, cib_peek(&cib)); + TEST_ASSERT_EQUAL_INT(0, cib_get(&cib)); + TEST_ASSERT_EQUAL_INT(1, cib_peek(&cib)); + TEST_ASSERT_EQUAL_INT(1, cib_get(&cib)); + TEST_ASSERT_EQUAL_INT(-1, cib_peek(&cib)); +} + static void test_cib_avail(void) { TEST_ASSERT_EQUAL_INT(0, cib_avail(&cib)); @@ -83,6 +97,7 @@ Test *tests_core_cib_tests(void) new_TestFixture(test_cib_put_and_get), new_TestFixture(test_empty_cib), new_TestFixture(test_singleton_cib), + new_TestFixture(test_cib_peek), }; EMB_UNIT_TESTCALLER(core_cib_tests, set_up, NULL, fixtures);