Skip to content

Commit

Permalink
Remove sigio implementation from FileHandle
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hasnain Virk committed May 31, 2017
1 parent 1afc7bf commit c65f81b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
26 changes: 23 additions & 3 deletions platform/BufferedSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ BufferedSerial::~BufferedSerial()

void BufferedSerial::DCD_IRQ()
{
_poll_change(this);
wake();
}

void BufferedSerial::set_data_carrier_detect(PinName DCD_pin, bool active_high)
Expand Down Expand Up @@ -94,6 +94,18 @@ int BufferedSerial::sync()
return 0;
}

void BufferedSerial::sigio(Callback<void()> func) {
core_util_critical_section_enter();
_sigio_cb = func;
if (_sigio_cb) {
short current_events = poll(0x7FFF);
if (current_events) {
_sigio_cb();
}
}
core_util_critical_section_exit();
}

ssize_t BufferedSerial::write(const void* buffer, size_t length)
{
size_t data_written = 0;
Expand Down Expand Up @@ -164,6 +176,14 @@ bool BufferedSerial::hup() const
return _dcd && _dcd->read() != 0;
}

void BufferedSerial::wake()
{
_poll_change(this);
if (_sigio_cb) {
_sigio_cb();
}
}

short BufferedSerial::poll(short events) const {

short revents = 0;
Expand Down Expand Up @@ -213,7 +233,7 @@ void BufferedSerial::rx_irq(void)

/* Report the File handler that data is ready to be read from the buffer. */
if (was_empty && !_rxbuf.empty()) {
_poll_change(this);
wake();
}
}

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

/* Report the File handler that data can be written to peripheral. */
if (was_full && !_txbuf.full() && !hup()) {
_poll_change(this);
wake();
}
}

Expand Down
7 changes: 7 additions & 0 deletions platform/BufferedSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ class BufferedSerial : private SerialBase, public FileHandle {

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

virtual void sigio(Callback<void()> func);

void set_data_carrier_detect(PinName DCD_pin, bool active_high=false);


private:

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

PlatformMutex _mutex;

Callback<void()> _sigio_cb;

bool _blocking;
bool _tx_irq_enabled;
InterruptIn *_dcd;
Expand All @@ -95,6 +100,8 @@ class BufferedSerial : private SerialBase, public FileHandle {
void tx_irq(void);
void rx_irq(void);

void wake(void);

void DCD_IRQ(void);
};
} //namespace mbed
Expand Down
13 changes: 0 additions & 13 deletions platform/FileHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ off_t FileHandle::size()
seek(off, SEEK_SET);
return size;
}

void FileHandle::sigio(Callback<void()> func) {
core_util_critical_section_enter();
_callback = func;
if (_callback) {
short current_events = poll(0x7FFF);
if (current_events) {
_callback();
}
}
core_util_critical_section_exit();
}

std::FILE *fdopen(FileHandle *fh, const char *mode)
{
return mbed_fdopen(fh, mode);
Expand Down
16 changes: 5 additions & 11 deletions platform/FileHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ class FileHandle {
* The input parameter can be used or ignored - the could always return all events,
* or could check just the events listed in events.
* Call is non-blocking - returns instantaneous state of events.
* Whenever an event occurs, the derived class must call _poll_change().
* Whenever an event occurs, the derived class must call _poll_change() (as well as
* the sigio() callback).
*
* @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc.
*
* @returns
Expand Down Expand Up @@ -230,18 +232,10 @@ class FileHandle {
*
* @param func Function to call on state change
*/
void sigio(Callback<void()> func);

/** Issue sigio to user - used by mbed::_poll_change */
void _send_sigio()
virtual void sigio(Callback<void()> func)
{
if (_callback) {
_callback();
}
//Default for real files. Do nothing for real files.
}

private:
Callback<void()> _callback;
};

/** Not a member function
Expand Down
3 changes: 0 additions & 3 deletions platform/mbed_poll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ int poll(pollfh fhs[], unsigned nfhs, int timeout)
void _poll_change(FileHandle *fh)
{
// TODO, will depend on how we implement poll

// Also, do the user callback
fh->_send_sigio();
}

} // namespace mbed
4 changes: 3 additions & 1 deletion platform/mbed_poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ struct pollfh {
*/
int poll(pollfh fhs[], unsigned nfhs, int timeout);

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

Expand Down

0 comments on commit c65f81b

Please sign in to comment.