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.

Examples<br />

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

“What if I wanted to set the sample rate to some arbitrary number n, how would I do<br />

that?” That will be answered in the next io_devctl() example below.<br />

Step 3<br />

This step is simply good defensive programming. We return an error code of ENOSYS<br />

to tell the client that we didn’t understand their request.<br />

Step 4<br />

Finally, we clear out the return structure and set up a one-part IOV to point to it. Then<br />

we return a value to the resource manager library encoded by the macro<br />

_RESMGR_NPARTS() telling it that we’re returning a one part IOV. This is then<br />

returned to the client. We could alternatively have used the _RESMGR_PTR() macro:<br />

// instead of this<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 />

// we could have done this<br />

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

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

return (_RESMGR_PTR (ctp, &msg -> o, sizeof (msg -> o)));<br />

The reason we cleared out the return structure here (and not in the io_read() or<br />

io_write() examples) is because in this case, the return structure has actual contents!<br />

(In the io_read() case, the only data returned was the data itself and the number of<br />

bytes read — there was no “return data structure,” and in the io_write() case, the only<br />

data returned was the number of bytes written.)<br />

An io_devctl() example that deals <strong>with</strong> data<br />

In the previous io_devctl() example, above, we raised the question of how to set<br />

arbitrary sampling rates. Obviously, it’s not a good solution to create a large number<br />

of DCMD_AUDIO_SET_SAMPLE_RATE_* constants — we’d rapidly use up the<br />

available bits in the dcmd member.<br />

From the client side, we’ll use the dev_data_ptr pointer to point to the sample rate,<br />

which we’ll simply pass as an integer. Therefore, the nbytes member will simply be<br />

the number of bytes in an integer (4 on a 32-bit machine). We’ll assume that the<br />

constant DCMD_AUDIO_SET_SAMPLE_RATE is defined for this purpose.<br />

Also, we’d like to be able to read the current sampling rate. We’ll also use the<br />

dev_data_ptr and nbytes as described above, but in the reverse direction — the<br />

resource manager will return data into the memory location pointed to by<br />

dev_data_ptr (for nbytes) instead of getting data from that memory location. Let’s<br />

assume that the constant DCMD_AUDIO_GET_SAMPLE_RATE is defined for this<br />

purpose.<br />

Let’s see what happens in the resource manager’s io_devctl(), as shown here (we<br />

won’t discuss things that have already been discussed in the previous example):<br />

266 Chapter 5 • Resource Managers April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!