NetBurner 3.5.0
PDF Version
 
FdToNBString

Many of NetBurner's libraries utilize file descriptors(fd) for data input/output(I/O) transmission. To name a few, some libraries that utilize fd's include TCP, WebSockets, HTTP, FTP, serial, and the config system.

The user may find themselves wanting to capture I/O data from an fd to a local variable instead of using the fd as originally intended by a library. This may helpful for logging, debugging, or printing to serial. The intent of this example is to demonstrate capturing file descriptor I/O data to an NBString.

This example utilizes the NBStringBuilder class which associates a buffer with a file descriptor which can then be used with API's that utilize fd's. The NBStringBuilder class is defined in nbstring.h. The NBStringBuilder class supports the use of file descriptor functions defined in iosys.h, such as read(), write(), and close().

This example will demonstrate the use of an NBStringBuilder object to read the system diagnostics data set. With the use of EnableSystemDiagnostics(), the system maintains a diagnostics data set as a JSON object. The diagnostics API exposes functions to write the JSON object to an fd, which can be used to write the JSON to a TCP socket or to serial. Instead, we will use the NBStringBuilder to write the diagnostics data to an NBString. The NBString can be utilized to store the diagnostics to persistent storage (flash, SD card, etc), or transmitted remotely (email, syslog, FTP, etc). For the simplicity of an example, this application will print the NBString to serial.