Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
🔥 Remove can_router from util
Browse files Browse the repository at this point in the history
can_router is misplaced in this library. Utilities, at their core, are
optional constructs. They provide helper functions for developers.
can_router is an interface requirement for drivers which does not fit
the design of util. Thus it will be decoupled from libhal-util and
brought into libhal-canrouter.

Add unit tests for can message comparison
  • Loading branch information
Khalil Estell authored and kammce committed Aug 6, 2023
1 parent f2d1318 commit 7ab331d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 543 deletions.
15 changes: 2 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: ✅ Checks
on:
workflow_dispatch:
pull_request:
release:
types: [published]
push:
tags:
- "*"
branches:
- main
schedule:
Expand All @@ -19,14 +19,3 @@ jobs:
devices:
uses: libhal/ci/.github/workflows/[email protected]
secrets: inherit

release:
needs: [ci, devices]
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-22.04
steps:
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
generate_release_notes: true
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class libhal_util_conan(ConanFile):
name = "libhal-util"
version = "2.0.0"
version = "3.0.0"
license = "Apache-2.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/libhal/libhal-util"
Expand Down
150 changes: 0 additions & 150 deletions include/libhal-util/can.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
#pragma once

#include <cmath>
#include <functional>
#include <optional>

#include <libhal/can.hpp>

#include "comparison.hpp"
#include "math.hpp"
#include "static_callable.hpp"
#include "static_list.hpp"

namespace hal {
[[nodiscard]] constexpr auto operator==(const can::settings& p_lhs,
Expand Down Expand Up @@ -119,150 +115,4 @@ namespace hal {
p_lhs.length == p_rhs.length &&
p_lhs.is_remote_request == p_rhs.is_remote_request;
}

/**
* @brief Route CAN messages received on the can bus to callbacks based on ID.
*
*/
class can_router
{
public:
static constexpr auto noop =
[]([[maybe_unused]] const can::message_t& p_message) {};

using message_handler = hal::callback<hal::can::handler>;

struct route
{
hal::can::id_t id = 0;
message_handler handler = noop;
};

using route_item = static_list<route>::item;

static result<can_router> create(hal::can& p_can)
{
can_router new_can_router(p_can);
return new_can_router;
}

/**
* @brief Construct a new can message router
*
* @param p_can - can peripheral to route messages for
*/
explicit can_router(hal::can& p_can)
: m_can(&p_can)
{
(void)m_can->on_receive(std::ref((*this)));
}

can_router() = delete;
can_router(can_router& p_other) = delete;
can_router& operator=(can_router& p_other) = delete;
can_router& operator=(can_router&& p_other)
{
m_handlers = std::move(p_other.m_handlers);
m_can = std::move(p_other.m_can);
(void)m_can->on_receive(std::ref(*this));

p_other.m_can = nullptr;
return *this;
}

can_router(can_router&& p_other)
{
*this = std::move(p_other);
}

~can_router()
{
if (m_can) {
// Assume that if this succeeded in the create factory function, that it
// will work this time
(void)m_can->on_receive(noop);
}
}

/**
* @brief Get a reference to the can peripheral driver
*
* Used to send can messages through the same port that the can_router is
* using.
*
* @return can& reference to the can peripheral driver
*/
[[nodiscard]] hal::can& bus()
{
return *m_can;
}

/**
* @brief Add a message route without setting the callback
*
* The default callback will do nothing and will drop the message.
*
* @param p_id - Associated ID of messages to be stored.
* @return auto - route item from the linked list that must be stored stored
* in a variable
*/
[[nodiscard]] auto add_message_callback(hal::can::id_t p_id)
{
return m_handlers.push_back(route{
.id = p_id,
});
}

/**
* @brief Set a callback for when messages with a specific ID is received
*
* @param p_id - Associated ID of messages to be stored.
* @param p_handler - callback to be executed when a p_id message is received.
* @return auto - route item from the linked list that must be stored stored
* in a variable
*/
[[nodiscard]] auto add_message_callback(hal::can::id_t p_id,
message_handler p_handler)
{
return m_handlers.push_back(route{
.id = p_id,
.handler = p_handler,
});
}

/**
* @brief Get the list of handlers
*
* Meant for testing purposes or when direct inspection of the map is useful
* in userspace. Should not be used in by libraries.
*
* @return const auto& map of all of the can message handlers.
*/
[[nodiscard]] const auto& handlers()
{
return m_handlers;
}

/**
* @brief Message routing interrupt service handler
*
* Searches the static list and finds the first ID associated with the message
* and run's that route's callback.
*
* @param p_message - message received from the bus
*/
void operator()(const can::message_t& p_message)
{
for (auto& list_handler : m_handlers) {
if (p_message.id == list_handler.id) {
list_handler.handler(p_message);
return;
}
}
}

private:
static_list<route> m_handlers{};
hal::can* m_can = nullptr;
};
} // namespace hal
11 changes: 8 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ find_package(ut REQUIRED CONFIG)
find_package(libhal REQUIRED CONFIG)

add_executable(${PROJECT_NAME}

# Source
../src/steady_clock.cpp
../src/streams.cpp

# Tests
as_bytes.test.cpp
bit.test.cpp
can.test.cpp
bit.test.cpp
enum.test.cpp
i2c.test.cpp
input_pin.test.cpp
Expand Down Expand Up @@ -86,5 +92,4 @@ target_link_options(${PROJECT_NAME} PRIVATE
${ASAN_FLAG})
target_link_libraries(${PROJECT_NAME} PRIVATE
boost-ext-ut::ut
libhal::libhal
libhal-util)
libhal::libhal)
Loading

0 comments on commit 7ab331d

Please sign in to comment.