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.

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

Step 3<br />

A simple io_read() example<br />

Data area size considerations<br />

io_func.io_read = my_io_read;<br />

This will override the POSIX-layer default function that got put into the table by<br />

iofunc_func_init() <strong>with</strong> a pointer to your function, my_io_read().<br />

You probably don’t want your resource manager called /dev/whatever, so you<br />

should select an appropriate name. Note that the resmgr_attach() function is where<br />

you bind the attributes structure (the attr parameter) to the name — if you wish to<br />

have multiple devices handled by your resource manager, you’d call resmgr_attach()<br />

multiple times, <strong>with</strong> different attributes structures (so that you could tell the different<br />

registered names apart at runtime).<br />

To illustrate how your resource manager might return data to a client, consider a<br />

simple resource manager that always returns the constant string "Hello,<br />

world!\n". There are a number of issues involved, even in this very simple case:<br />

• matching of client’s data area size to data being returned<br />

• handling of EOF case<br />

• maintenance of context information (the lseek() index)<br />

• updating of POSIX stat() information<br />

In our case, the resource manager is returning a fixed string of 14 bytes — there is<br />

exactly that much data available. This is identical to a read-only file on a disk that<br />

contains the string in question; the only real difference is that this “file” is maintained<br />

in our C program via the statement:<br />

char<br />

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

Handling of EOF case<br />

The client, on the other hand, can issue a read() request of any size — the client could<br />

ask for one byte, 14 bytes, or more. The impact of this on the io_read() functionality<br />

you’re going to provide is that you must be able to match the client’s requested data<br />

size <strong>with</strong> what’s available.<br />

A natural fallout of the way you handle the client’s data area size considerations is the<br />

corner case of dealing <strong>with</strong> the End-Of-File (EOF) on the fixed string. Once the client<br />

has read the final “\n” character, further attempts by the client to read more data<br />

should return EOF.<br />

April 30, 2009 Chapter 5 • Resource Managers 255

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

Saved successfully!

Ooh no, something went wrong!