Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
BenchmarkCan.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_COMMON_BENCHMARKCAN_HXX_
35#define _FREERTOS_DRIVERS_COMMON_BENCHMARKCAN_HXX_
36
38#include "utils/Atomic.hxx"
39
42class BenchmarkCan : public Can, private Atomic
43{
44public:
50 BenchmarkCan(const char *name)
51 : Can(name)
52 , readTimeLast_(0)
53 , readCount_(0)
54 {
55 }
56
60 {
61 }
62
68 void start_benchmark(const struct can_frame *frame, unsigned count)
69 {
70 bool need_signal = false;
71 {
72 AtomicHolder h(this);
73 packet_ = *frame;
74 readCount_ = count;
75 need_signal = refill_locked();
76 }
77 if (need_signal)
78 {
80 }
81 }
82
88 long long get_timestamp(unsigned *count)
89 {
90 *count = readCount_;
91 return readTimeLast_;
92 }
93
94private:
102 ssize_t read(File *file, void *buf, size_t count) override
103 {
104 ssize_t result = Can::read(file, buf, count);
105
106 bool need_signal = false;
107 if (result > 0)
108 {
109 AtomicHolder lock(this);
111 unsigned num_frames = result / sizeof(struct can_frame);
112 if (readCount_ > num_frames)
113 {
114 readCount_ -= num_frames;
115 }
116 else
117 {
118 readCount_ = 0;
119 }
120 need_signal = refill_locked();
121 }
122 if (need_signal)
123 {
125 }
126 return result;
127 }
128
136 ssize_t write(File *file, const void *buf, size_t count) override
137 {
138 /* drop all the written data on the floor */
139 return count;
140 }
141
146 {
147 bool need_signal = false;
148 while ((rxBuf->pending() < readCount_) && (rxBuf->space()))
149 {
150 if (rxBuf->pending() == 0)
151 {
152 need_signal = true;
153 }
154 rxBuf->put(&packet_, 1);
155 }
156 return need_signal;
157 }
158
159 void enable() override
160 {
161 }
162 void disable() override
163 {
164 }
165 void tx_msg() override
166 {
167 }
168
169 struct can_frame packet_;
170 long long readTimeLast_;
171 size_t readCount_;
174};
175
176#endif /* _FREERTOS_DRIVERS_COMMON_BENCHMARKCAN_HXX_ */
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Lightweight locking class for protecting small critical sections.
Definition Atomic.hxx:130
Generic CAN driver for throughput testing purposes.
ssize_t write(File *file, const void *buf, size_t count) override
Write to a file or device.
~BenchmarkCan()
Destructor.
void tx_msg() override
function to try and transmit a message
long long readTimeLast_
timestamp of last read in OS time
bool refill_locked()
Refills the rxBuf from the packet_ and readCount.
void disable() override
function to disable device
void start_benchmark(const struct can_frame *frame, unsigned count)
Start a benchmarking run.
size_t readCount_
count of packets still to inject
long long get_timestamp(unsigned *count)
Get the latest performance time stamp.
struct can_frame packet_
packet to inject.
ssize_t read(File *file, void *buf, size_t count) override
Read from a file or device.
BenchmarkCan(const char *name)
Constructor.
void enable() override
function to enable device
Base class for a CAN device for the Arduino environment.
int read(struct can_frame *frame)
Read a frame if there is one available.
DeviceBuffer< struct can_frame > * rxBuf
receive buffer
size_t space()
Return the number of items for which space is available.
void signal_condition()
Signal the wakeup condition.
size_t pending()
Return the number of items in the queue.
size_t put(const T *buf, size_t items)
Insert a number of items to the buffer.
const char * name
device name
Definition Devtab.hxx:266
static long long get_monotonic()
Get the monotonic time since the system started.
Definition OS.hxx:560
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
File information.
Definition Devtab.hxx:52