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

if ((sts = iofunc_devctl_default (ctp, msg, ocb)) !=<br />

_RESMGR_DEFAULT)<br />

{<br />

return (sts);<br />

}<br />

// 2) see which command it was, and act on it<br />

switch (msg -> i.dcmd) {<br />

case DCMD_AUDIO_SET_CHANNEL_MONO:<br />

audio_set_nchannels (1);<br />

break;<br />

case DCMD_AUDIO_SET_CHANNEL_STEREO:<br />

audio_set_nchannels (2);<br />

break;<br />

case DCMD_AUDIO_SET_SAMPLE_RATE_CD:<br />

audio_set_samplerate (44100);<br />

break;<br />

case DCMD_AUDIO_SET_SAMPLE_RATE_DAT:<br />

audio_set_samplerate (48000);<br />

break;<br />

// 3) in case it’s a command that we don’t recognize, fail it<br />

default:<br />

return (ENOSYS);<br />

}<br />

}<br />

// 4) tell the client it worked<br />

memset (&msg -> o, 0, sizeof (msg -> o));<br />

SETIOV (ctp -> iov, &msg -> o, sizeof (msg -> o));<br />

return (_RESMGR_NPARTS (1));<br />

Step 1<br />

In the first step, we see again the use of a helper function, this time<br />

iofunc_devctl_default(), which is used to perform all default processing for the<br />

devctl() function. If you didn’t supply your own io_devctl(), and just let<br />

iofunc_func_init() initialize the I/O and connect functions tables for you, the<br />

iofunc_devctl_default() function is what would get called. We include it in our<br />

io_devctl() function because we want it to handle all the regular POSIX devctl() cases<br />

for us. We examine the return value; if it’s not _RESMGR_DEFAULT, then this means<br />

that the iofunc_devctl_default() function “handled” the request, so we just pass along<br />

its return value as our return value.<br />

If the constant _RESMGR_DEFAULT is the return value, then we know that the helper<br />

function didn’t handle the request and that we should check to see if it’s one of ours.<br />

Step 2<br />

This checking is done in step 2 via the switch/case statement. We simply compare<br />

the dcmd values that the client code would have stuffed into the second argument to<br />

devctl() to see if there’s a match. Note that we call the fictitious functions<br />

audio_set_nchannels() and audio_set_samplerate() to accomplish the actual “work”<br />

for the client. An important note that should be mentioned here is that we’ve<br />

specifically avoided touching the data area aspects of devctl() — you may be thinking,<br />

April 30, 2009 Chapter 5 • Resource Managers 265

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

Saved successfully!

Ooh no, something went wrong!