Skip to content

Commit 2ada172

Browse files
bugobliteratortridge
authored andcommitted
canard: split common code into cpp files
1 parent 0cba715 commit 2ada172

File tree

9 files changed

+230
-137
lines changed

9 files changed

+230
-137
lines changed

canard/handler_list.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "handler_list.h"
2+
3+
using namespace Canard;
4+
5+
HandlerList::HandlerList(CanardTransferType _transfer_type, uint16_t _msgid, uint64_t _signature, uint8_t _index) :
6+
index(_index) {
7+
if (index >= CANARD_NUM_HANDLERS) {
8+
return;
9+
}
10+
#ifdef CANARD_MUTEX_ENABLED
11+
WITH_SEMAPHORE(sem[index]);
12+
#endif
13+
next = head[index];
14+
head[index] = this;
15+
msgid = _msgid;
16+
signature = _signature;
17+
transfer_type = _transfer_type;
18+
}
19+
20+
HandlerList::~HandlerList()
21+
{
22+
#ifdef CANARD_MUTEX_ENABLED
23+
WITH_SEMAPHORE(sem[index]);
24+
#endif
25+
HandlerList* entry = head[index];
26+
if (entry == this) {
27+
head[index] = next;
28+
return;
29+
}
30+
while (entry != nullptr) {
31+
if (entry->next == this) {
32+
entry->next = next;
33+
return;
34+
}
35+
entry = entry->next;
36+
}
37+
}
38+
39+
bool HandlerList::accept_message(uint8_t index, uint16_t msgid, uint64_t &signature)
40+
{
41+
#ifdef CANARD_MUTEX_ENABLED
42+
WITH_SEMAPHORE(sem[index]);
43+
#endif
44+
HandlerList* entry = head[index];
45+
while (entry != nullptr) {
46+
if (entry->msgid == msgid) {
47+
signature = entry->signature;
48+
return true;
49+
}
50+
entry = entry->next;
51+
}
52+
return false;
53+
}
54+
55+
void HandlerList::handle_message(uint8_t index, const CanardRxTransfer& transfer)
56+
{
57+
#ifdef CANARD_MUTEX_ENABLED
58+
WITH_SEMAPHORE(sem[index]);
59+
#endif
60+
HandlerList* entry = head[index];
61+
while (entry != nullptr) {
62+
if (transfer.data_type_id == entry->msgid &&
63+
entry->transfer_type == transfer.transfer_type) {
64+
entry->handle_message(transfer);
65+
return;
66+
}
67+
entry = entry->next;
68+
}
69+
}

canard/handler_list.h

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -41,82 +41,25 @@ class HandlerList {
4141
/// @param _msgid ID of the message/service
4242
/// @param _signature Signature of the message/service
4343
/// @param _index Index of the handler list
44-
HandlerList(CanardTransferType _transfer_type, uint16_t _msgid, uint64_t _signature, uint8_t _index) NOINLINE_FUNC :
45-
index(_index) {
46-
if (index >= CANARD_NUM_HANDLERS) {
47-
return;
48-
}
49-
#ifdef WITH_SEMAPHORE
50-
WITH_SEMAPHORE(sem[index]);
51-
#endif
52-
next = head[index];
53-
head[index] = this;
54-
msgid = _msgid;
55-
signature = _signature;
56-
transfer_type = _transfer_type;
57-
}
44+
HandlerList(CanardTransferType _transfer_type, uint16_t _msgid, uint64_t _signature, uint8_t _index) NOINLINE_FUNC;
5845

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

6249
// destructor, remove the entry from the singly-linked list
63-
virtual ~HandlerList() NOINLINE_FUNC {
64-
#ifdef WITH_SEMAPHORE
65-
WITH_SEMAPHORE(sem[index]);
66-
#endif
67-
HandlerList* entry = head[index];
68-
if (entry == this) {
69-
head[index] = next;
70-
return;
71-
}
72-
while (entry != nullptr) {
73-
if (entry->next == this) {
74-
entry->next = next;
75-
return;
76-
}
77-
entry = entry->next;
78-
}
79-
}
50+
virtual ~HandlerList() NOINLINE_FUNC;
8051

8152
/// @brief accept a message if it is handled by this handler list
8253
/// @param index Index of the handler list
8354
/// @param msgid ID of the message/service
8455
/// @param[out] signature Signature of the message/service
8556
/// @return true if the message is handled by this handler list
86-
static bool accept_message(uint8_t index, uint16_t msgid, uint64_t &signature) NOINLINE_FUNC
87-
{
88-
#ifdef WITH_SEMAPHORE
89-
WITH_SEMAPHORE(sem[index]);
90-
#endif
91-
HandlerList* entry = head[index];
92-
while (entry != nullptr) {
93-
if (entry->msgid == msgid) {
94-
signature = entry->signature;
95-
return true;
96-
}
97-
entry = entry->next;
98-
}
99-
return false;
100-
}
57+
static bool accept_message(uint8_t index, uint16_t msgid, uint64_t &signature) NOINLINE_FUNC;
10158

10259
/// @brief handle a message if it is handled by this handler list
10360
/// @param index Index of the handler list
10461
/// @param transfer transfer object of the request
105-
static void handle_message(uint8_t index, const CanardRxTransfer& transfer) NOINLINE_FUNC
106-
{
107-
#ifdef WITH_SEMAPHORE
108-
WITH_SEMAPHORE(sem[index]);
109-
#endif
110-
HandlerList* entry = head[index];
111-
while (entry != nullptr) {
112-
if (transfer.data_type_id == entry->msgid &&
113-
entry->transfer_type == transfer.transfer_type) {
114-
entry->handle_message(transfer);
115-
return;
116-
}
117-
entry = entry->next;
118-
}
119-
}
62+
static void handle_message(uint8_t index, const CanardRxTransfer& transfer) NOINLINE_FUNC;
12063

12164
/// @brief Method to handle a message implemented by the derived class
12265
/// @param transfer transfer object of the request
@@ -128,7 +71,7 @@ class HandlerList {
12871

12972
private:
13073
static HandlerList* head[CANARD_NUM_HANDLERS];
131-
#ifdef WITH_SEMAPHORE
74+
#ifdef CANARD_MUTEX_ENABLED
13275
static Canard::Semaphore sem[CANARD_NUM_HANDLERS];
13376
#endif
13477
uint16_t msgid;

canard/interface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ struct Transfer {
4040
uint8_t priority; ///< Priority of the transfer
4141
const void* payload; ///< Pointer to the payload
4242
uint32_t payload_len; ///< Length of the payload
43+
#if CANARD_MULTI_IFACE
4344
uint8_t iface_mask; ///< Bitmask of interfaces to send the transfer on
45+
#endif
46+
#if CANARD_ENABLE_CANFD
4447
bool canfd; ///< true if the transfer is CAN FD
48+
#endif
4549
uint32_t timeout_ms; ///< timeout in ms
4650
};
4751

canard/publisher.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "publisher.h"
2+
3+
using namespace Canard;
4+
5+
bool PublisherBase::send(uint16_t data_type_id,
6+
uint64_t data_type_signature,
7+
uint8_t* msg_buf,
8+
uint32_t len
9+
#if CANARD_ENABLE_CANFD
10+
, bool canfd
11+
#endif
12+
) {
13+
if (len == 0) {
14+
return false;
15+
}
16+
Transfer msg_transfer {};
17+
msg_transfer.transfer_type = CanardTransferTypeBroadcast;
18+
msg_transfer.data_type_id = data_type_id;
19+
msg_transfer.data_type_signature = data_type_signature;
20+
msg_transfer.payload = msg_buf;
21+
msg_transfer.payload_len = len;
22+
#if CANARD_ENABLE_CANFD
23+
msg_transfer.canfd = canfd;
24+
#endif
25+
#if CANARD_MULTI_IFACE
26+
msg_transfer.iface_mask = CANARD_IFACE_ALL;
27+
#endif
28+
return Sender::send(msg_transfer);
29+
}
30+
31+
bool Sender::send(Transfer& transfer, uint8_t destination_node_id) {
32+
switch (transfer.transfer_type)
33+
{
34+
case CanardTransferTypeBroadcast:
35+
transfer.inout_transfer_id = TransferObject::get_tid_ptr(interface.get_index(),transfer.data_type_id, CanardTransferTypeBroadcast, interface.get_node_id(), destination_node_id);
36+
transfer.priority = priority;
37+
transfer.timeout_ms = timeout;
38+
return interface.broadcast(transfer);
39+
case CanardTransferTypeRequest:
40+
transfer.inout_transfer_id = TransferObject::get_tid_ptr(interface.get_index(),transfer.data_type_id, CanardTransferTypeRequest, interface.get_node_id(), destination_node_id);
41+
transfer.priority = priority;
42+
transfer.timeout_ms = timeout;
43+
return interface.request(destination_node_id, transfer);
44+
case CanardTransferTypeResponse:
45+
default:
46+
return false;
47+
}
48+
}

canard/publisher.h

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,34 @@ class Sender {
5656
/// @brief Send a message
5757
/// @param Transfer message to send
5858
/// @return true if the message was put into the queue successfully
59-
bool send(Transfer& transfer, uint8_t destination_node_id = CANARD_BROADCAST_NODE_ID) NOINLINE_FUNC {
60-
switch (transfer.transfer_type)
61-
{
62-
case CanardTransferTypeBroadcast:
63-
transfer.inout_transfer_id = TransferObject::get_tid_ptr(interface.get_index(),transfer.data_type_id, CanardTransferTypeBroadcast, interface.get_node_id(), destination_node_id);
64-
transfer.priority = priority;
65-
transfer.timeout_ms = timeout;
66-
return interface.broadcast(transfer);
67-
case CanardTransferTypeRequest:
68-
transfer.inout_transfer_id = TransferObject::get_tid_ptr(interface.get_index(),transfer.data_type_id, CanardTransferTypeRequest, interface.get_node_id(), destination_node_id);
69-
transfer.priority = priority;
70-
transfer.timeout_ms = timeout;
71-
return interface.request(destination_node_id, transfer);
72-
case CanardTransferTypeResponse:
73-
default:
74-
return false;
75-
}
76-
}
59+
bool send(Transfer& transfer, uint8_t destination_node_id = CANARD_BROADCAST_NODE_ID) NOINLINE_FUNC;
7760
private:
7861
uint8_t priority = CANARD_TRANSFER_PRIORITY_MEDIUM; ///< Priority of the message
7962
uint32_t timeout = 1000; ///< Timeout of the message in ms
8063
};
8164

65+
class PublisherBase : public Sender {
66+
public:
67+
PublisherBase(Interface &_interface) :
68+
Sender(_interface)
69+
{}
70+
71+
protected:
72+
bool send(uint16_t data_type_id,
73+
uint64_t data_type_signature,
74+
uint8_t* msg_buf,
75+
uint32_t len
76+
#if CANARD_ENABLE_CANFD
77+
, bool canfd
78+
#endif
79+
) NOINLINE_FUNC;
80+
};
81+
8282
template <typename msgtype>
83-
class Publisher : public Sender {
83+
class Publisher : public PublisherBase {
8484
public:
8585
Publisher(Interface &_interface) :
86-
Sender(_interface)
86+
PublisherBase(_interface)
8787
{}
8888

8989
// delete copy constructor and assignment operator
@@ -115,22 +115,14 @@ class Publisher : public Sender {
115115
#endif
116116
);
117117
// send the message if encoded successfully
118-
if (len > 0) {
119-
Transfer msg_transfer {};
120-
msg_transfer.transfer_type = CanardTransferTypeBroadcast;
121-
msg_transfer.data_type_id = msgtype::cxx_iface::ID;
122-
msg_transfer.data_type_signature = msgtype::cxx_iface::SIGNATURE;
123-
msg_transfer.payload = msg_buf;
124-
msg_transfer.payload_len = len;
118+
return PublisherBase::send(msgtype::cxx_iface::ID,
119+
msgtype::cxx_iface::SIGNATURE,
120+
(uint8_t*)msg_buf,
121+
len
125122
#if CANARD_ENABLE_CANFD
126-
msg_transfer.canfd = canfd;
127-
#endif
128-
#if CANARD_MULTI_IFACE
129-
msg_transfer.iface_mask = CANARD_IFACE_ALL;
123+
,canfd
130124
#endif
131-
return send(msg_transfer);
132-
}
133-
return false;
125+
);
134126
}
135127
private:
136128
uint8_t msg_buf[msgtype::cxx_iface::MAX_SIZE]; ///< Buffer to store the encoded message

canard/service_server.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "service_server.h"
2+
3+
using namespace Canard;
4+
5+
ServerBase::ServerBase(Interface &_interface, uint16_t _msgid, uint64_t _signature) :
6+
HandlerList(CanardTransferTypeRequest, _msgid, _signature, _interface.get_index()),
7+
interface(_interface)
8+
{}
9+
10+
bool ServerBase::respond(const CanardRxTransfer& transfer,
11+
uint16_t data_type_id,
12+
uint64_t data_type_signature,
13+
uint8_t* rsp_buf,
14+
uint32_t len)
15+
{
16+
// send the message if encoded successfully
17+
if (len == 0) {
18+
return false;
19+
}
20+
Transfer rsp_transfer = {
21+
.transfer_type = CanardTransferTypeResponse,
22+
.data_type_signature = data_type_signature,
23+
.data_type_id = data_type_id,
24+
.inout_transfer_id = &transfer_id,
25+
.priority = transfer.priority,
26+
.payload = rsp_buf,
27+
.payload_len = len,
28+
#if CANARD_MULTI_IFACE
29+
.iface_mask = iface_mask,
30+
#endif
31+
#if CANARD_ENABLE_CANFD
32+
.canfd = transfer.canfd,
33+
#endif
34+
.timeout_ms = timeout,
35+
};
36+
return interface.respond(transfer.source_node_id, rsp_transfer);
37+
}

0 commit comments

Comments
 (0)