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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Porting philosophy<br />

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

}<br />

return (sts);<br />

This is an example of using io_msg() (which we’ll define shortly — it’s not a standard<br />

QSS supplied library call!). The io_msg() function does the magic of assembling the<br />

_IO_MSG message. To get around the problems that we discussed about devctl()<br />

having only one “size” parameter, we’ve given io_msg() two size parameters, one for<br />

the input (to the resource manager, sizeof (msg.i)) and one for the output (from<br />

the resource manager, sizeof (msg.o)). Notice how we update the values of *odr<br />

and *ms only if the io_msg() function returns an EOK.<br />

This is a common trick, and is useful in this case because the passed arguments don’t<br />

get modified unless the actual command succeeded. (This prevents the client program<br />

from having to maintain copies of its passed data, just in case the function fails.)<br />

One last thing that I’ve done in the adjust_xyz() function, is that I depend on the<br />

global_fd variable containing the file descriptor of the resource manager. Again, there<br />

are a number of ways that you could handle it:<br />

• Bury the file descriptor <strong>with</strong>in the io_msg() function (this would be useful if you<br />

wanted to avoid having to pass around the file descriptor on each and every call;<br />

useful if you’re ever going to talk to only the one resource manager, and thus most<br />

likely not suitable as a general purpose solution).<br />

Or:<br />

• Pass the file descriptor from the client itself to each function in the API library<br />

(useful if the client’s going to be responsible for talking to the resource manager in<br />

other ways, such as the standard POSIX file descriptor calls like read(), orifthe<br />

client may be talking to multiple resource managers).<br />

Here’s the source for io_msg():<br />

int<br />

io_msg (int fd, int cmd, void *msg, int isize, int osize)<br />

{<br />

io_msg_t io_message;<br />

iov_t rx_iov [2];<br />

iov_t tx_iov [2];<br />

int<br />

sts;<br />

// set up the transmit IOV<br />

SETIOV (tx_iov + 0, &io_msg.o, sizeof (io_msg.o));<br />

SETIOV (tx_iov + 1, msg, osize);<br />

// set up the receive IOV<br />

SETIOV (rx_iov + 0, &io_msg.i, sizeof (io_msg.i));<br />

SETIOV (rx_iov + 1, msg, isize);<br />

// set up the _IO_MSG itself<br />

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

io_message.type = _IO_MSG;<br />

io_message.mgrid = cmd;<br />

}<br />

return (MsgSendv (fd, tx_iov, 2, rx_iov, 2));<br />

298 Appendix: A • <strong>QNX</strong> 4 to <strong>Neutrino</strong> April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!