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.

atoz.c<br />

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

{<br />

dispatch_t<br />

*dpp;<br />

resmgr_attr_t<br />

resmgr_attr;<br />

dispatch_context_t *ctp;<br />

resmgr_connect_funcs_t connect_func;<br />

resmgr_io_funcs_t io_func;<br />

iofunc_attr_t<br />

attr;<br />

int i;<br />

// create the dispatch structure<br />

if ((dpp = dispatch_create ()) == NULL) {<br />

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

exit (EXIT_FAILURE);<br />

}<br />

// initialize the various data structures<br />

memset (&resmgr_attr, 0, sizeof (resmgr_attr));<br />

resmgr_attr.nparts_max = 1;<br />

resmgr_attr.msg_max_size = 2048;<br />

// bind default functions into the outcall tables<br />

iofunc_func_init (_RESMGR_CONNECT_NFUNCS, &connect_func,<br />

_RESMGR_IO_NFUNCS, &io_func);<br />

// create and initialize the attributes structure for the directory<br />

iofunc_attr_init (&attr, S_IFDIR | 0555, 0, 0);<br />

attr.inode = NUM_ENTS + 1; // 1-26 are reserved for ’a’ through ’z’ files<br />

attr.nbytes = NUM_ENTS; // 26 entries contained in this directory<br />

// and for the "a" through "z" names<br />

for (i = 0; i < NUM_ENTS; i++) {<br />

iofunc_attr_init (&atoz_attrs [i], S_IFREG | 0444, 0, 0);<br />

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

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

}<br />

// add our functions; we’re only interested in 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", _FTYPE_ANY,<br />

_RESMGR_FLAG_DIR, &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 />

316 Appendix: C • Sample Programs April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!