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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

// 5) if returning data, write it to client<br />

if (nbytes) {<br />

MsgReply (ctp -> rcvid, nbytes, data_string + off, nbytes);<br />

// 6) set up POSIX stat() "atime" data<br />

ocb -> attr -> flags |= IOFUNC_ATTR_ATIME |<br />

IOFUNC_ATTR_DIRTY_TIME;<br />

// 7) advance the lseek() index by the number of bytes<br />

// read if not _IO_XTYPE_OFFSET<br />

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

ocb -> offset += nbytes;<br />

}<br />

} else {<br />

// 8) not returning data, just unblock client<br />

MsgReply (ctp -> rcvid, EOK, NULL, 0);<br />

}<br />

}<br />

// 9) indicate we already did the MsgReply to the library<br />

return (_RESMGR_NOREPLY);<br />

Step 1<br />

Here we ensured that the client’s open() call had in fact specified that the device was to<br />

be opened for reading. If the client opened the device for writing only, and then<br />

attempted to perform a read from it, it would be considered an error. In that case, the<br />

helper function iofunc_read_verify() would return EBADF, and not EOK, so we’d<br />

return that value to the library, which would then pass it along to the client.<br />

Step 2<br />

Here we checked to see if the client had specified an xtype-override — a per-message<br />

override (e.g., because while the device had been opened in non-blocking mode, this<br />

specifies for this one request that we’d like blocking behavior). Note that the blocking<br />

aspect of the “xtype” override can be noted by the iofunc_read_verify() function’s last<br />

parameter — since we’re illustrating a very simple example, we just passed in a NULL<br />

indicating that we don’t care about this aspect.<br />

More important, however, is to see how particular “xtype” modifiers are handled. An<br />

interesting one is the _IO_XTYPE_OFFSET modifier, which, if present, indicates that<br />

the message passed from the client contains an offset and that the read operation<br />

should not modify the “current file position” of the file descriptor (this is used by the<br />

function pread(), for example). If the _IO_XTYPE_OFFSET modifier is not present,<br />

then the read operation can go ahead and modify the “current file position.” We use the<br />

variable xtype to store the “xtype” that we received in the message, and the variable off<br />

to represent the current offset that we should be using during processing. You’ll see<br />

some additional handling of the _IO_XTYPE_OFFSET modifier below, in step 7.<br />

If there is a different “xtype override” than _IO_XTYPE_OFFSET (and not the no-op<br />

one of _IO_XTYPE_NONE), we fail the request <strong>with</strong> ENOSYS. This simply means that<br />

we don’t know how to handle it, and we therefore return the error up to the client.<br />

April 30, 2009 Chapter 5 • Resource Managers 257

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

Saved successfully!

Ooh no, something went wrong!