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 />

Step 4<br />

Step 5<br />

Step 6<br />

Step 7<br />

Step 8<br />

A simple io_devctl() example<br />

Here you’d do whatever you want <strong>with</strong> the data — I’ve just called a made-up function<br />

called process_data() and passed it the buffer and size.<br />

This step is crucial! Forgetting to do it is easy, and will lead to “memory leaks.”<br />

Notice how we also took care to free the memory in the case of a failure in step 3.<br />

We’re using the macro _IO_SET_WRITE_NBYTES() (see the entry for<br />

iofunc_write_verify() in the <strong>Neutrino</strong> Library Reference) to store the number of bytes<br />

we’ve written, which will then be passed back to the client as the return value from the<br />

client’s write(). It’s important to note that you should return the actual number of<br />

bytes! The client is depending on this.<br />

Now we do similar housekeeping for stat(), lseek(), and further write() functions as we<br />

did for the io_read() routine (and again, we modify the offset in the ocb only in the<br />

case of this not being a _IO_XTYPE_OFFSET type of message). Since we’re writing to<br />

the device, however, we use the IOFUNC_ATTR_MTIME constant instead of the<br />

IOFUNC_ATTR_ATIME constant. The MTIME flag means “modification” time, and a<br />

write() to a resource certainly “modifies” it.<br />

The last step is simple: we return the constant EOK, which tells the resource manager<br />

library that it should reply to the client. This ends our processing. The resource<br />

manager will use the number of bytes that we stashed away <strong>with</strong> the<br />

_IO_SET_WRITE_NBYTES() macro in the reply and the client will unblock; the<br />

client’s C library write() function will return the number of bytes that were written by<br />

our device.<br />

The client’s devctl() call is formally defined as:<br />

#include <br />

#include <br />

#include <br />

int<br />

devctl (int fd,<br />

int dcmd,<br />

void *dev_data_ptr,<br />

size_t nbytes,<br />

int *dev_info_ptr);<br />

We should first understand this function before we look at the resource manager side<br />

of things. The devctl() function is used for “out of band” or “control” operations. For<br />

example, you may be writing data to a sound card (the actual digital audio samples that<br />

April 30, 2009 Chapter 5 • Resource Managers 263

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

Saved successfully!

Ooh no, something went wrong!