|
NetBurner 3.5.6
PDF Version |
This application demonstrates how to implement a custom I/O device using NetBurner's Extra File Descriptor system. The example creates a circular buffer that can be accessed using standard file operations like read() and write(), making it behave like any other file descriptor in the system. The Extra File Descriptors are defined in the 'iointernal.h' header file.
read(), write(), and fdprintf()CircularBufferRead(): Reads data from the circular bufferCircularBufferWrite(): Writes data to the circular bufferCircularBufferClose(): Cleanup when closing the file descriptorCircularBufferInit(): Initializes the buffer and returns a file descriptorCircularBufferFlushBytes(): Flushes data up to a terminator byteCircularBufferRawPrint(): Debug function to display raw buffer contentsThe circular buffer uses two indices:
Write Index: Points to the next position for writing
Buffer States**:
readIndex == writeIndex(writeIndex + 1) % bufferSize == readIndexWhen overwrite mode is enabled, you can specify a terminator byte (e.g., '
' for line-based data). When the buffer becomes full, data is flushed up to this terminator instead of byte-by-byte, making it ideal for line-oriented protocols.
The application includes two test functions:
Demonstrates buffer overflow behavior by writing more data than the buffer can hold, then reading it back to show how the system handles full buffer conditions.
Tests the terminator-based flushing feature by sending line-terminated messages and displaying the raw buffer contents to show how data is managed.
The implementation uses NetBurner's file descriptor status functions:
SetDataAvail(fd) / ClrDataAvail(fd): Indicates data ready for readingSetWriteAvail(fd) / ClrWriteAvail(fd): Indicates space available for writingSetHaveError(fd) / ClrHaveError(fd): Error status managementThese functions are interrupt-safe and integrate with NetBurner's I/O system.
All buffer operations are protected by critical sections using NetBurner's OS_CRIT class, ensuring safe operation in multi-task environments.
circBufDebug) for detailed operation loggingKey configuration parameters in main.cpp:
dataBufferSize: Buffer size (set to 10 for testing, increase for production)enableOverwrite: Set to true for automatic overwrite modeoverwriteTermByte: Terminator character for intelligent flushingThis example demonstrates the NetBurner Extra File Descriptor system defined in iointernal.h. The same pattern can be used to create custom I/O devices for:
main.cpp: Main application and test functionsExtraFdCircBuffer.h: Header file with structure definitionsExtraFdCircBuffer.cpp: Implementation of circular buffer operations