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.

Advanced topics<br />

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

}<br />

atoz_attrs [i].inode = i + 1;<br />

atoz_attrs [i].nbytes = 1;<br />

// add our functions; we’re interested only in<br />

// io_open and io_read<br />

connect_func.open = my_open;<br />

io_func.read = my_read;<br />

// establish a name in the pathname space<br />

if (resmgr_attach (dpp, &resmgr_attr, "/dev/atoz",<br />

_FTYPE_ANY, _RESMGR_FLAG_DIR,<br />

&connect_func, &io_func,<br />

&attr) == -1) {<br />

perror ("Unable to resmgr_attach\n");<br />

exit (EXIT_FAILURE);<br />

}<br />

// allocate a context<br />

ctp = dispatch_context_alloc (dpp);<br />

// wait here forever, handling messages<br />

while (1) {<br />

if ((ctp = dispatch_block (ctp)) == NULL) {<br />

perror ("Unable to dispatch_block\n");<br />

exit (EXIT_FAILURE);<br />

}<br />

dispatch_handler (ctp);<br />

}<br />

}<br />

// you’ll never get here<br />

return (EXIT_SUCCESS);<br />

my_open()<br />

While my_open() is very short, it has a number of crucial points. Notice how we<br />

decide if the resource being opened is a “file” or a “directory” based only on the<br />

pathname length. We can do this “trick” because we know that there are no other<br />

directories in this resource manager apart from the main one. If you want to have<br />

multiple directories below the mountpoint, you have to do more complicated analysis<br />

of the path member of the msg structure. For our simple example, if there’s nothing in<br />

the pathname, we know it’s the directory. Also, notice the extremely simplified<br />

pathname validation checking: we simply compare to make sure that there’s only one<br />

character passed to us, and that the character lies <strong>with</strong>in the range “a” through “z”<br />

inclusive. Again, for more complex resource managers, you’d be responsible for<br />

parsing the name past the registered mountpoint.<br />

Now, the most important feature! Notice how we used the POSIX-layer default<br />

functions to do all the work for us! The iofunc_open_default() function is usually<br />

installed in the connect functions table at the same spot that our new my_open()<br />

function is now occupying. This means that it takes the identical set of arguments! All<br />

we have to do is decide which attributes structure we want to have bound <strong>with</strong> the<br />

OCB that the default function is going to create: either the directory one (in which<br />

case we pass attr), or one of the 26 different ones for the 26 different files (in which<br />

278 Chapter 5 • Resource Managers April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!