Skip to content

Commit c65f81b

Browse files
author
Hasnain Virk
committed
Remove sigio implementation from FileHandle
Make FileHandle more of an interface class, by requiring implementers to provide the sigio() functionality themselves. sigio() and poll() remain parallel independent mechanisms, so FileHandle implementations must trigger both on state changes.
1 parent 1afc7bf commit c65f81b

File tree

6 files changed

+38
-31
lines changed

6 files changed

+38
-31
lines changed

platform/BufferedSerial.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ BufferedSerial::~BufferedSerial()
4040

4141
void BufferedSerial::DCD_IRQ()
4242
{
43-
_poll_change(this);
43+
wake();
4444
}
4545

4646
void BufferedSerial::set_data_carrier_detect(PinName DCD_pin, bool active_high)
@@ -94,6 +94,18 @@ int BufferedSerial::sync()
9494
return 0;
9595
}
9696

97+
void BufferedSerial::sigio(Callback<void()> func) {
98+
core_util_critical_section_enter();
99+
_sigio_cb = func;
100+
if (_sigio_cb) {
101+
short current_events = poll(0x7FFF);
102+
if (current_events) {
103+
_sigio_cb();
104+
}
105+
}
106+
core_util_critical_section_exit();
107+
}
108+
97109
ssize_t BufferedSerial::write(const void* buffer, size_t length)
98110
{
99111
size_t data_written = 0;
@@ -164,6 +176,14 @@ bool BufferedSerial::hup() const
164176
return _dcd && _dcd->read() != 0;
165177
}
166178

179+
void BufferedSerial::wake()
180+
{
181+
_poll_change(this);
182+
if (_sigio_cb) {
183+
_sigio_cb();
184+
}
185+
}
186+
167187
short BufferedSerial::poll(short events) const {
168188

169189
short revents = 0;
@@ -213,7 +233,7 @@ void BufferedSerial::rx_irq(void)
213233

214234
/* Report the File handler that data is ready to be read from the buffer. */
215235
if (was_empty && !_rxbuf.empty()) {
216-
_poll_change(this);
236+
wake();
217237
}
218238
}
219239

@@ -237,7 +257,7 @@ void BufferedSerial::tx_irq(void)
237257

238258
/* Report the File handler that data can be written to peripheral. */
239259
if (was_full && !_txbuf.full() && !hup()) {
240-
_poll_change(this);
260+
wake();
241261
}
242262
}
243263

platform/BufferedSerial.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ class BufferedSerial : private SerialBase, public FileHandle {
6464

6565
virtual int set_blocking(bool blocking) { _blocking = blocking; return 0; }
6666

67+
virtual void sigio(Callback<void()> func);
68+
6769
void set_data_carrier_detect(PinName DCD_pin, bool active_high=false);
6870

71+
6972
private:
7073

7174
/** Software serial buffers
@@ -76,6 +79,8 @@ class BufferedSerial : private SerialBase, public FileHandle {
7679

7780
PlatformMutex _mutex;
7881

82+
Callback<void()> _sigio_cb;
83+
7984
bool _blocking;
8085
bool _tx_irq_enabled;
8186
InterruptIn *_dcd;
@@ -95,6 +100,8 @@ class BufferedSerial : private SerialBase, public FileHandle {
95100
void tx_irq(void);
96101
void rx_irq(void);
97102

103+
void wake(void);
104+
98105
void DCD_IRQ(void);
99106
};
100107
} //namespace mbed

platform/FileHandle.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,6 @@ off_t FileHandle::size()
3232
seek(off, SEEK_SET);
3333
return size;
3434
}
35-
36-
void FileHandle::sigio(Callback<void()> func) {
37-
core_util_critical_section_enter();
38-
_callback = func;
39-
if (_callback) {
40-
short current_events = poll(0x7FFF);
41-
if (current_events) {
42-
_callback();
43-
}
44-
}
45-
core_util_critical_section_exit();
46-
}
47-
4835
std::FILE *fdopen(FileHandle *fh, const char *mode)
4936
{
5037
return mbed_fdopen(fh, mode);

platform/FileHandle.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ class FileHandle {
181181
* The input parameter can be used or ignored - the could always return all events,
182182
* or could check just the events listed in events.
183183
* Call is non-blocking - returns instantaneous state of events.
184-
* Whenever an event occurs, the derived class must call _poll_change().
184+
* Whenever an event occurs, the derived class must call _poll_change() (as well as
185+
* the sigio() callback).
186+
*
185187
* @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
186188
*
187189
* @returns
@@ -230,18 +232,10 @@ class FileHandle {
230232
*
231233
* @param func Function to call on state change
232234
*/
233-
void sigio(Callback<void()> func);
234-
235-
/** Issue sigio to user - used by mbed::_poll_change */
236-
void _send_sigio()
235+
virtual void sigio(Callback<void()> func)
237236
{
238-
if (_callback) {
239-
_callback();
240-
}
237+
//Default for real files. Do nothing for real files.
241238
}
242-
243-
private:
244-
Callback<void()> _callback;
245239
};
246240

247241
/** Not a member function

platform/mbed_poll.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ int poll(pollfh fhs[], unsigned nfhs, int timeout)
7575
void _poll_change(FileHandle *fh)
7676
{
7777
// TODO, will depend on how we implement poll
78-
79-
// Also, do the user callback
80-
fh->_send_sigio();
8178
}
8279

8380
} // namespace mbed

platform/mbed_poll.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ struct pollfh {
4949
*/
5050
int poll(pollfh fhs[], unsigned nfhs, int timeout);
5151

52-
/** To be called by device when poll state changes - must be called for poll() and sigio() to work
52+
/** To be called by device when poll state changes - must be called for poll() to work
53+
* This must be called in addition to the user-provided callback.
54+
*
5355
* @param fh A pointer to the file handle*/
5456
void _poll_change(FileHandle *fh);
5557

0 commit comments

Comments
 (0)