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.

time1.c<br />

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

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

int debug = 1; // set debug value, 1 == enabled, 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 />

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

/*<br />

* setupPulseAndTimer<br />

*<br />

* This routine is responsible for setting up a pulse so it<br />

* sends a message <strong>with</strong> code MT_TIMER. It then sets up a periodic<br />

* timer that fires once per second.<br />

*/<br />

void<br />

setupPulseAndTimer (void)<br />

{<br />

timer_t timerid; // timer ID for timer<br />

struct sigevent event; // event to deliver<br />

struct itimerspec timer; // the timer data structure<br />

int coid; // connection back to ourselves<br />

// create a connection back to ourselves<br />

coid = ConnectAttach (0, 0, chid, 0, 0);<br />

if (coid == -1) {<br />

fprintf (stderr, "%s: couldn’t ConnectAttach to self!\n", progname);<br />

perror (NULL);<br />

exit (EXIT_FAILURE);<br />

318 Appendix: C • Sample Programs April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!