CanProtobuf.hpp
Go to the documentation of this file.
1 // Copyright (C) 2021 COGIP Robotics association <cogip35@gmail.com>
2 // This file is subject to the terms and conditions of the GNU Lesser
3 // General Public License v2.1. See the file LICENSE in the top level
4 // directory for more details.
5 
18 
19 #pragma once
20 
21 #include "etl/delegate.h"
22 
23 // RIOT includes
24 #include <mutex.h>
25 #include "can/conn/raw.h"
26 #include "periph/can.h"
27 #include "ringbuffer.h"
28 #include "thread.h"
29 #include "etl/map.h"
30 
31 #include "canpb/canpb.hpp"
32 #include "canpb/ReadBuffer.hpp"
33 #include "canpb/WriteBuffer.hpp"
34 
35 #include "MessageInterface.h"
36 
37 // Double the size of the ringbuffer receiving input bytes,
38 // so we can continue receiving data even if the thread decoding
39 // the message has not consumed previous message immediately.
40 #define CAN_BUFFER_SIZE CANPB_INPUT_MESSAGE_LENGTH_MAX*2
41 
42 namespace cogip {
43 
44 namespace canpb {
45 
47 using uuid_t = uint32_t;
48 
50 using message_handler_t = etl::delegate<void(cogip::canpb::ReadBuffer &)>;
51 
56  void *arg
57  );
58 
60 class CanProtobuf {
61 public:
62 
64  explicit CanProtobuf(
65  uint8_t can_interface_number
66  );
67 
70  bool init(struct can_filter *filter);
71 
73  void start_reader();
74 
76  void can_rx_cb(
77  uint8_t data
78  );
79 
81  void message_reader();
82 
85  bool send_message(
86  uuid_t uuid,
87  const EmbeddedProto::MessageInterface *message = nullptr
89  );
90 
93  uuid_t uuid,
94  message_handler_t handler
95  );
96 
97 private:
98  conn_can_raw_t conn_can_raw_;
99  uint8_t can_interface_number_;
100  kernel_pid_t reader_pid_;
101  ringbuffer_t rx_buf_;
102  uint32_t msg_length_ = 0;
103  mutex_t mutex_ = MUTEX_INIT;
104  etl::map<uuid_t, message_handler_t, CANPB_MAX_HANDLERS> message_handlers_;
106  char reader_stack_[CANPB_READER_STACKSIZE];
107  char rx_mem_[CAN_BUFFER_SIZE];
108  ReadBuffer read_buffer_;
109  WriteBuffer write_buffer_;
110 };
111 
112 } // namespace canpb
113 
114 } // namespace cogip
115 
CANPB_READER_STACKSIZE
#define CANPB_READER_STACKSIZE
message reader thread stask size
Definition: canpb.hpp:21
cogip::canpb::uuid_t
uint32_t uuid_t
Custom type for uuids.
Definition: CanProtobuf.hpp:47
cogip::canpb::CanProtobuf::message_reader
void message_reader()
Wait and decode incoming Protobuf messages.
cogip::canpb::CanProtobuf::register_message_handler
void register_message_handler(uuid_t uuid, message_handler_t handler)
Associate a message handle to a specific uuid.
cogip::canpb::CanProtobuf::CanProtobuf
CanProtobuf(uint8_t can_interface_number)
Class constructor.
cogip::canpb::ReadBuffer
ReadBuffer class used to decode Protobuf messages.
Definition: ReadBuffer.hpp:28
cogip::canpb::WriteBuffer
WriteBuffer class used to encode Protobuf messages.
Definition: WriteBuffer.hpp:27
cogip::canpb::CanProtobuf::init
bool init(struct can_filter *filter)
Initialize CAN connection.
cogip
Differential drive controller.
Definition: Coords.hpp:16
canpb.hpp
cogip::canpb::CanProtobuf
Generic CAN Protobuf communication class.
Definition: CanProtobuf.hpp:60
cogip::canpb::CanProtobuf::send_message
bool send_message(uuid_t uuid, const EmbeddedProto::MessageInterface *message=nullptr)
Send CAN message.
WriteBuffer.hpp
Write buffer for EmbeddedProto.
ReadBuffer.hpp
Read buffer for EmbeddedProto.
cogip::canpb::message_reader_wrapper
void * message_reader_wrapper(void *arg)
Thread function decoding incoming Protobuf messages.
cogip::canpb::message_handler_t
etl::delegate< void(cogip::canpb::ReadBuffer &)> message_handler_t
Prototype for incoming Protobuf message handlers.
Definition: CanProtobuf.hpp:50
cogip::canpb::CanProtobuf::can_rx_cb
void can_rx_cb(uint8_t data)
Function call for each incomming frame on CAN port.
cogip::canpb::CanProtobuf::start_reader
void start_reader()
Start thread waiting for incoming messages.