-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSerialDispatcher.h
105 lines (81 loc) · 3.53 KB
/
SerialDispatcher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* This file is part of the Arduino_ThreadsafeIO library.
* Copyright (c) 2021 Arduino SA.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SERIAL_DISPATCHER_H_
#define SERIAL_DISPATCHER_H_
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include "api/HardwareSerial.h"
#include <mbed.h>
#include <list>
#include <functional>
#include <SharedPtr.h>
/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/
class SerialDispatcher : public arduino::HardwareSerial
{
public:
SerialDispatcher(arduino::HardwareSerial & serial);
virtual void begin(unsigned long baudrate) override;
virtual void begin(unsigned long baudrate, uint16_t config) override;
virtual void end() override;
virtual int available() override;
virtual int peek() override;
virtual int read() override;
virtual void flush() override;
virtual size_t write(uint8_t const b) override;
virtual size_t write(const uint8_t * data, size_t len) override;
using Print::write;
virtual operator bool() override { return _serial; }
void block();
void unblock();
typedef std::function<String(String const &)> PrefixInjectorCallbackFunc;
typedef std::function<String(String const &, String const &)> SuffixInjectorCallbackFunc;
void prefix(PrefixInjectorCallbackFunc func);
void suffix(SuffixInjectorCallbackFunc func);
void global_prefix(PrefixInjectorCallbackFunc func);
void global_suffix(SuffixInjectorCallbackFunc func);
private:
bool _is_initialized;
rtos::Mutex _mutex;
rtos::ConditionVariable _cond;
arduino::HardwareSerial & _serial;
rtos::Thread _thread;
bool _has_tread_started;
bool _terminate_thread;
PrefixInjectorCallbackFunc _global_prefix_callback;
SuffixInjectorCallbackFunc _global_suffix_callback;
static int constexpr THREADSAFE_SERIAL_TRANSMIT_RINGBUFFER_SIZE = 128;
typedef arduino::RingBufferN<THREADSAFE_SERIAL_TRANSMIT_RINGBUFFER_SIZE> SerialTransmitRingbuffer;
typedef struct
{
osThreadId_t thread_id;
SerialTransmitRingbuffer tx_buffer;
bool block_tx_buffer;
mbed::SharedPtr<arduino::RingBuffer> rx_buffer; /* Only when a thread has expressed interested to read from serial a receive ringbuffer is allocated. */
PrefixInjectorCallbackFunc prefix_func;
SuffixInjectorCallbackFunc suffix_func;
} ThreadCustomerData;
std::list<ThreadCustomerData> _thread_customer_list;
void threadFunc();
std::list<ThreadCustomerData>::iterator findThreadCustomerDataById(osThreadId_t const thread_id);
void prepareSerialReader(std::list<ThreadCustomerData>::iterator & iter);
void handleSerialReader();
};
#endif /* SERIAL_DISPATCHER_H_ */