-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc5e5ae
commit df3e7a3
Showing
5 changed files
with
272 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
target_sources(${PROJECT_NAME} | ||
PUBLIC | ||
udc_mac.hpp | ||
message_queue.hpp | ||
PRIVATE | ||
udc_mac.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/// @author Benedek Kupper | ||
/// @date 2024 | ||
/// | ||
/// @copyright | ||
/// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. | ||
/// If a copy of the MPL was not distributed with this file, You can obtain one at | ||
/// https://mozilla.org/MPL/2.0/. | ||
/// | ||
#ifndef __PORT_ZEPHYR_MESSAGE_QUEUE_HPP | ||
#define __PORT_ZEPHYR_MESSAGE_QUEUE_HPP | ||
|
||
#include <cstddef> | ||
#include <optional> | ||
#include <zephyr/kernel.h> | ||
|
||
namespace os::zephyr | ||
{ | ||
template <typename T, std::size_t SIZE> | ||
class message_queue | ||
{ | ||
public: | ||
message_queue() { ::k_msgq_init(&msgq_, msgq_buffer_, sizeof(T), max_size()); } | ||
|
||
bool post(const T& msg, ::k_timeout_t timeout = K_FOREVER) | ||
{ | ||
return ::k_msgq_put(&msgq_, &msg, timeout) == 0; | ||
} | ||
|
||
std::optional<T> get(::k_timeout_t timeout = K_FOREVER) | ||
{ | ||
T msg; | ||
if (::k_msgq_get(&msgq_, reinterpret_cast<void*>(&msg), timeout) == 0) | ||
{ | ||
return msg; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
void flush() { ::k_msgq_purge(&msgq_); } | ||
|
||
std::optional<T> peek() const | ||
{ | ||
T msg; | ||
if (::k_msgq_peek(const_cast<::k_msgq*>(&msgq_), reinterpret_cast<void*>(&msg)) == 0) | ||
{ | ||
return msg; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
std::size_t size() const { return ::k_msgq_num_used_get(const_cast<::k_msgq*>(&msgq_)); } | ||
static constexpr std::size_t max_size() { return SIZE; } | ||
bool empty() const { return ::k_msgq_num_used_get(const_cast<::k_msgq*>(&msgq_)) == 0; } | ||
bool full() const { return ::k_msgq_num_free_get(const_cast<::k_msgq*>(&msgq_)) == 0; } | ||
|
||
private: | ||
::k_msgq msgq_; | ||
char msgq_buffer_[max_size() * sizeof(T)]; | ||
}; | ||
|
||
} // namespace os::zephyr | ||
|
||
#endif // __PORT_ZEPHYR_MESSAGE_QUEUE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.