Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split common code into separate cpp files #60

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions canard/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* SOFTWARE.
*
*/
#pragma once

#include <stdint.h>

Expand Down
69 changes: 69 additions & 0 deletions canard/handler_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "handler_list.h"

using namespace Canard;

HandlerList::HandlerList(CanardTransferType _transfer_type, uint16_t _msgid, uint64_t _signature, uint8_t _index) :
index(_index) {
if (index >= CANARD_NUM_HANDLERS) {
return;
}
#ifdef CANARD_MUTEX_ENABLED
WITH_SEMAPHORE(sem[index]);
#endif
next = head[index];
head[index] = this;
msgid = _msgid;
signature = _signature;
transfer_type = _transfer_type;
}

HandlerList::~HandlerList()
{
#ifdef CANARD_MUTEX_ENABLED
WITH_SEMAPHORE(sem[index]);
#endif
HandlerList* entry = head[index];
if (entry == this) {
head[index] = next;
return;
}
while (entry != nullptr) {
if (entry->next == this) {
entry->next = next;
return;
}
entry = entry->next;
}
}

bool HandlerList::accept_message(uint8_t index, uint16_t msgid, uint64_t &signature)
{
#ifdef CANARD_MUTEX_ENABLED
WITH_SEMAPHORE(sem[index]);
#endif
HandlerList* entry = head[index];
while (entry != nullptr) {
if (entry->msgid == msgid) {
signature = entry->signature;
return true;
}
entry = entry->next;
}
return false;
}

void HandlerList::handle_message(uint8_t index, const CanardRxTransfer& transfer)
{
#ifdef CANARD_MUTEX_ENABLED
WITH_SEMAPHORE(sem[index]);
#endif
HandlerList* entry = head[index];
while (entry != nullptr) {
if (transfer.data_type_id == entry->msgid &&
entry->transfer_type == transfer.transfer_type) {
entry->handle_message(transfer);
return;
}
entry = entry->next;
}
}
71 changes: 8 additions & 63 deletions canard/handler_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,82 +41,25 @@ class HandlerList {
/// @param _msgid ID of the message/service
/// @param _signature Signature of the message/service
/// @param _index Index of the handler list
HandlerList(CanardTransferType _transfer_type, uint16_t _msgid, uint64_t _signature, uint8_t _index) NOINLINE_FUNC :
index(_index) {
if (index >= CANARD_NUM_HANDLERS) {
return;
}
#ifdef WITH_SEMAPHORE
WITH_SEMAPHORE(sem[index]);
#endif
next = head[index];
head[index] = this;
msgid = _msgid;
signature = _signature;
transfer_type = _transfer_type;
}
HandlerList(CanardTransferType _transfer_type, uint16_t _msgid, uint64_t _signature, uint8_t _index) NOINLINE_FUNC;

/// @brief delete copy constructor and assignment operator
HandlerList(const HandlerList&) = delete;

// destructor, remove the entry from the singly-linked list
virtual ~HandlerList() NOINLINE_FUNC {
#ifdef WITH_SEMAPHORE
WITH_SEMAPHORE(sem[index]);
#endif
HandlerList* entry = head[index];
if (entry == this) {
head[index] = next;
return;
}
while (entry != nullptr) {
if (entry->next == this) {
entry->next = next;
return;
}
entry = entry->next;
}
}
virtual ~HandlerList() NOINLINE_FUNC;

/// @brief accept a message if it is handled by this handler list
/// @param index Index of the handler list
/// @param msgid ID of the message/service
/// @param[out] signature Signature of the message/service
/// @return true if the message is handled by this handler list
static bool accept_message(uint8_t index, uint16_t msgid, uint64_t &signature) NOINLINE_FUNC
{
#ifdef WITH_SEMAPHORE
WITH_SEMAPHORE(sem[index]);
#endif
HandlerList* entry = head[index];
while (entry != nullptr) {
if (entry->msgid == msgid) {
signature = entry->signature;
return true;
}
entry = entry->next;
}
return false;
}
static bool accept_message(uint8_t index, uint16_t msgid, uint64_t &signature) NOINLINE_FUNC;

/// @brief handle a message if it is handled by this handler list
/// @param index Index of the handler list
/// @param transfer transfer object of the request
static void handle_message(uint8_t index, const CanardRxTransfer& transfer) NOINLINE_FUNC
{
#ifdef WITH_SEMAPHORE
WITH_SEMAPHORE(sem[index]);
#endif
HandlerList* entry = head[index];
while (entry != nullptr) {
if (transfer.data_type_id == entry->msgid &&
entry->transfer_type == transfer.transfer_type) {
entry->handle_message(transfer);
return;
}
entry = entry->next;
}
}
static void handle_message(uint8_t index, const CanardRxTransfer& transfer) NOINLINE_FUNC;

/// @brief Method to handle a message implemented by the derived class
/// @param transfer transfer object of the request
Expand All @@ -125,10 +68,12 @@ class HandlerList {
protected:
uint8_t index;
HandlerList* next;

#ifdef CANARD_MUTEX_ENABLED
Canard::Semaphore& get_sem() { return sem[index]; }
#endif
private:
static HandlerList* head[CANARD_NUM_HANDLERS];
#ifdef WITH_SEMAPHORE
#ifdef CANARD_MUTEX_ENABLED
static Canard::Semaphore sem[CANARD_NUM_HANDLERS];
#endif
uint16_t msgid;
Expand Down
4 changes: 4 additions & 0 deletions canard/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ struct Transfer {
uint8_t priority; ///< Priority of the transfer
const void* payload; ///< Pointer to the payload
uint32_t payload_len; ///< Length of the payload
#if CANARD_MULTI_IFACE
uint8_t iface_mask; ///< Bitmask of interfaces to send the transfer on
#endif
#if CANARD_ENABLE_CANFD
bool canfd; ///< true if the transfer is CAN FD
#endif
uint32_t timeout_ms; ///< timeout in ms
};

Expand Down
48 changes: 48 additions & 0 deletions canard/publisher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "publisher.h"

using namespace Canard;

bool PublisherBase::send(uint16_t data_type_id,
uint64_t data_type_signature,
uint8_t* msg_buf,
uint32_t len
#if CANARD_ENABLE_CANFD
, bool canfd
#endif
) {
if (len == 0) {
return false;
}
Transfer msg_transfer {};
msg_transfer.transfer_type = CanardTransferTypeBroadcast;
msg_transfer.data_type_id = data_type_id;
msg_transfer.data_type_signature = data_type_signature;
msg_transfer.payload = msg_buf;
msg_transfer.payload_len = len;
#if CANARD_ENABLE_CANFD
msg_transfer.canfd = canfd;
#endif
#if CANARD_MULTI_IFACE
msg_transfer.iface_mask = CANARD_IFACE_ALL;
#endif
return Sender::send(msg_transfer);
}

bool Sender::send(Transfer& transfer, uint8_t destination_node_id) {
switch (transfer.transfer_type)
{
case CanardTransferTypeBroadcast:
transfer.inout_transfer_id = TransferObject::get_tid_ptr(interface.get_index(),transfer.data_type_id, CanardTransferTypeBroadcast, interface.get_node_id(), destination_node_id);
transfer.priority = priority;
transfer.timeout_ms = timeout;
return interface.broadcast(transfer);
case CanardTransferTypeRequest:
transfer.inout_transfer_id = TransferObject::get_tid_ptr(interface.get_index(),transfer.data_type_id, CanardTransferTypeRequest, interface.get_node_id(), destination_node_id);
transfer.priority = priority;
transfer.timeout_ms = timeout;
return interface.request(destination_node_id, transfer);
case CanardTransferTypeResponse:
default:
return false;
}
}
60 changes: 26 additions & 34 deletions canard/publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,34 @@ class Sender {
/// @brief Send a message
/// @param Transfer message to send
/// @return true if the message was put into the queue successfully
bool send(Transfer& transfer, uint8_t destination_node_id = CANARD_BROADCAST_NODE_ID) NOINLINE_FUNC {
switch (transfer.transfer_type)
{
case CanardTransferTypeBroadcast:
transfer.inout_transfer_id = TransferObject::get_tid_ptr(interface.get_index(),transfer.data_type_id, CanardTransferTypeBroadcast, interface.get_node_id(), destination_node_id);
transfer.priority = priority;
transfer.timeout_ms = timeout;
return interface.broadcast(transfer);
case CanardTransferTypeRequest:
transfer.inout_transfer_id = TransferObject::get_tid_ptr(interface.get_index(),transfer.data_type_id, CanardTransferTypeRequest, interface.get_node_id(), destination_node_id);
transfer.priority = priority;
transfer.timeout_ms = timeout;
return interface.request(destination_node_id, transfer);
case CanardTransferTypeResponse:
default:
return false;
}
}
bool send(Transfer& transfer, uint8_t destination_node_id = CANARD_BROADCAST_NODE_ID) NOINLINE_FUNC;
private:
uint8_t priority = CANARD_TRANSFER_PRIORITY_MEDIUM; ///< Priority of the message
uint32_t timeout = 1000; ///< Timeout of the message in ms
};

class PublisherBase : public Sender {
public:
PublisherBase(Interface &_interface) :
Sender(_interface)
{}

protected:
bool send(uint16_t data_type_id,
uint64_t data_type_signature,
uint8_t* msg_buf,
uint32_t len
#if CANARD_ENABLE_CANFD
, bool canfd
#endif
) NOINLINE_FUNC;
};

template <typename msgtype>
class Publisher : public Sender {
class Publisher : public PublisherBase {
public:
Publisher(Interface &_interface) :
Sender(_interface)
PublisherBase(_interface)
{}

// delete copy constructor and assignment operator
Expand Down Expand Up @@ -115,22 +115,14 @@ class Publisher : public Sender {
#endif
);
// send the message if encoded successfully
if (len > 0) {
Transfer msg_transfer {};
msg_transfer.transfer_type = CanardTransferTypeBroadcast;
msg_transfer.data_type_id = msgtype::cxx_iface::ID;
msg_transfer.data_type_signature = msgtype::cxx_iface::SIGNATURE;
msg_transfer.payload = msg_buf;
msg_transfer.payload_len = len;
return PublisherBase::send(msgtype::cxx_iface::ID,
msgtype::cxx_iface::SIGNATURE,
(uint8_t*)msg_buf,
len
#if CANARD_ENABLE_CANFD
msg_transfer.canfd = canfd;
#endif
#if CANARD_MULTI_IFACE
msg_transfer.iface_mask = CANARD_IFACE_ALL;
,canfd
#endif
return send(msg_transfer);
}
return false;
);
}
private:
uint8_t msg_buf[msgtype::cxx_iface::MAX_SIZE]; ///< Buffer to store the encoded message
Expand Down
37 changes: 37 additions & 0 deletions canard/service_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "service_server.h"

using namespace Canard;

ServerBase::ServerBase(Interface &_interface, uint16_t _msgid, uint64_t _signature) :
HandlerList(CanardTransferTypeRequest, _msgid, _signature, _interface.get_index()),
interface(_interface)
{}

bool ServerBase::respond(const CanardRxTransfer& transfer,
uint16_t data_type_id,
uint64_t data_type_signature,
uint8_t* rsp_buf,
uint32_t len)
{
// send the message if encoded successfully
if (len == 0) {
return false;
}
Transfer rsp_transfer = {
.transfer_type = CanardTransferTypeResponse,
.data_type_signature = data_type_signature,
.data_type_id = data_type_id,
.inout_transfer_id = &transfer_id,
.priority = transfer.priority,
.payload = rsp_buf,
.payload_len = len,
#if CANARD_MULTI_IFACE
.iface_mask = iface_mask,
#endif
#if CANARD_ENABLE_CANFD
.canfd = transfer.canfd,
#endif
.timeout_ms = timeout,
};
return interface.respond(transfer.source_node_id, rsp_transfer);
}
Loading
Loading