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.

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

int chid; // channel ID (global)<br />

int debug = 1; // set debug value, 1=on, 0=off<br />

char *progname = "time1.c";<br />

// forward prototypes<br />

static void setupPulseAndTimer (void);<br />

static void gotAPulse (void);<br />

static void gotAMessage (int rcvid, ClientMessageT *msg);<br />

main()<br />

This next section of code is the mainline. It’s responsible for:<br />

• creating the channel (via ChannelCreate()),<br />

• calling the setupPulseAndTimer() routine (to set up a once-per-second timer, <strong>with</strong> a<br />

pulse as the event delivery method), and then<br />

• sitting in a “do-forever” loop waiting for pulses or messages and processing them.<br />

Notice the check against the return value from MsgReceive() — a zero indicates it’s a<br />

pulse (and we don’t do any strong checking to ensure that it’s our pulse), a non-zero<br />

indicates it’s a message. The processing of the pulse or message is done by<br />

gotAPulse() and gotAMessage().<br />

int<br />

main (void)<br />

{<br />

int rcvid;<br />

MessageT msg;<br />

// ignore command-line arguments<br />

// process ID of the sender<br />

// the message itself<br />

if ((chid = ChannelCreate (0)) == -1) {<br />

fprintf (stderr, "%s: couldn’t create channel!\n",<br />

progname);<br />

perror (NULL);<br />

exit (EXIT_FAILURE);<br />

}<br />

// set up the pulse and timer<br />

setupPulseAndTimer ();<br />

// receive messages<br />

for (;;) {<br />

rcvid = MsgReceive (chid, &msg, sizeof (msg), NULL);<br />

}<br />

// determine who the message came from<br />

if (rcvid == 0) {<br />

// production code should check "code" field...<br />

gotAPulse ();<br />

} else {<br />

gotAMessage (rcvid, &msg.msg);<br />

}<br />

}<br />

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

return (EXIT_SUCCESS);<br />

April 30, 2009 Chapter 3 • Clocks, Timers, and <strong>Getting</strong> a Kick Every So Often 151

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

Saved successfully!

Ooh no, something went wrong!