26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Examples<br />

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG.<br />

Maintenance of context information<br />

Updating POSIX information<br />

The code<br />

Both the “Data area size considerations” and the “Handling of EOF case” scenarios<br />

will require that context be maintained in the OCB passed to your io_read() function,<br />

specifically the offset member.<br />

One final consideration: when data is read from a resource manager, the POSIX<br />

access time (atime) variable needs to be updated. This is so that a client stat() function<br />

will show that someone has indeed accessed the device.<br />

Here’s the code that addresses all the above points. We’ll go through it step-by-step in<br />

the discussion that follows:<br />

/*<br />

* io_read1.c<br />

*/<br />

#include <br />

#include <br />

#include <br />

#include <br />

// our data string<br />

char *data_string = "Hello, world!\n";<br />

int<br />

io_read (resmgr_context_t *ctp, io_read_t *msg, iofunc_ocb_t *ocb)<br />

{<br />

int sts;<br />

int nbytes;<br />

int nleft;<br />

int off;<br />

int xtype;<br />

struct _xtype_offset *xoffset;<br />

// 1) verify that the device is opened for read<br />

if ((sts = iofunc_read_verify (ctp, msg, ocb, NULL)) != EOK) {<br />

return (sts);<br />

}<br />

// 2) check for and handle an XTYPE override<br />

xtype = msg -> i.xtype & _IO_XTYPE_MASK;<br />

if (xtype == _IO_XTYPE_OFFSET) {<br />

xoffset = (struct _xtype_offset *) (&msg -> i + 1);<br />

off = xoffset -> offset;<br />

} else if (xtype == _IO_XTYPE_NONE) {<br />

off = ocb -> offset;<br />

} else { // unknown, fail it<br />

return (ENOSYS);<br />

}<br />

// 3) how many bytes are left?<br />

nleft = ocb -> attr -> nbytes - off;<br />

// 4) how many bytes can we return to the client?<br />

nbytes = min (nleft, msg -> i.nbytes);<br />

256 Chapter 5 • Resource Managers April 30, 2009

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!