Sie sind auf Seite 1von 14

Appendix

A
Example Code

Tornado Training Workshop

Copyright Wind River Systems Wind River Systems

A-1

Synchronization

Message Queues: Data Collection

Message Queues: Client - Server

Exception Handling

select ( )

UDP

TCP

Demo code

Synchronization Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include vxWorks.h #include semLib.h #include intLib.h LOCAL SEM_ID mySemId; void myInit( ) { /* Initialize software/hardware for device */ ... mySemId = semBCreate (SEM_Q_FIFO, SEM_EMPTY); intConnect (MY_INT_VEC, myIsr, 0); } void myGetData( ) { /* Tweak device registers to start I/O */ ... /* Wait for interrupt indicating data is ready */ semTake (mySemId, WAIT_FOREVER); /* use the data */ ... } LOCAL void myIsr( ) { /* Interrupt generated from external device */ ... semGive (mySemId); }

Synchronization Wind River Systems

A-2

Message Queues: Data Collection


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include #include vxWorks.h taskLib.h msgQLib.h myLib.h

LOCAL MSG_Q_ID myMsgQId; STATUS myInit (void) { /* Software/hardware initialization done here */ ... myMsgQId = msgQCreate (MAX_MSGS, MAX_SIZE, MSG_Q_PRIORITY); if (myMsgQId == NULL) return (ERROR); if (taskSpawn (..., myPollingCode, ...) == ERROR) { msgQDelete (myMsgQId); return (ERROR); } return (OK); } LOCAL void myPollingCode (void) { for (;;) { taskDelay (DELAY_PERIOD); /* Read data from device & */ /* place into message queue*/ ... if (msgQSend (myMsgQId, ...) == ERROR) handleTheError( ); } } int myGetData (char * pBuf) { return (msgQReceive (myMsgQId, pBuf, ...)); }

Message Queues: Data Collection Wind River Systems

A-3

Message Queues: Client - Server


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include #include vxWorks.h taskLib.h msgQLib.h myLib.h

LOCAL MSG_Q_ID myMsgQId; STATUS myInit (void) { /* Server initialization done here */ ... myMsgQId = msgQCreate (MAX_MSGS, MSG_SIZE, MSG_Q_PRIORITY); if (myMsgQId == NULL) return (ERROR); if (taskSpawn (..., myServerCode, ...) == ERROR) { msgQDelete (myMsgQId); return (ERROR); } return (OK); } LOCAL void myServerCode (void) { for (;;) { /* Wait to receive next request */ if (msgQReceive (myMsgQId, pBuf, ...) == ERROR) handleTheError(); /* Service the request */ ... } } STATUS mySendRequest (MY_REQUEST * pRequest) { return (msgQSend (myMsgQId, pRequest, ...)); }

Message Queues: Client - Server Wind River Systems

A-4

Exception Handling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include #include #include #include vxWorks.h taskLib.h setjmp.h signal.h fooLib.h /* Destination for jump */

jmp_buf location;

STATUS fooInit (void) { fooTid = taskSpawn (tFooPoll, FOO_PRIORITY, FOO_OPTIONS, FOO_STACK_SIZE, (FUNCPTR) fooPoll, 0, 0, 0, 0 0, 0, 0, 0, 0, 0); if (fooTid == ERROR) return ERROR; return OK; } LOCAL void fooPoll (void) { signal (SIGBUS, fooHandler); signal (SIGSEGV, fooHandler); FOREVER { if (setjmp (location) != 0) fooLogDevError ( ); fooAccessDev ( ); fooLogData ( ); } } LOCAL void fooHandler (int sigNum) { fooResetDev ( ); longjmp (location, 100); }

setjmp( )s return value

Exception Handling Wind River Systems

A-5

select( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include #include vxWorks.h fioLib.h selectLib.h fooLib.h

LOCAL struct fd_set readFds, tmpFds; LOCAL int fds[NUM_FDS]; LOCAL int width = 0; char channelName[20]; /************************************************ * fooReadInit - Attempt to open channels /dev/0 * through /dev/N, N == NUM_FDS-1. For each * channel opened, set the bit of the corresponding * file descriptor in readFds. * RETURNS: The number of channels opened. */ int fooReadInit(void) { int i; /* channel number */ int j; /* index into fds */ /* Initialize read fd_set structure */ FD_ZERO (&readFds); for (i=0, j=0; i < NUM_FDS; i++) { int fd; sprintf (channelName, %s%d, /dev/, i); if ((fd = open(channelName, O_RDWR, 0))>=0) { fds[j++] = fd; FD_SET (fd, &readFds); width = max (fd, width); } } width++; return j; /* Number successfully opened */ }

select( ) Wind River Systems

A-6

select( ) Example, continued.


41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 /************************************************* * fooRead - Service requests arriving over the * nFds file descriptors in the fds[] array. */ void fooRead ( int nFds ) { int i;

/* # file descriptors in fds[] */

if (nFds <= 0) exit (errno = S_fooLib_BAD_NFDS); /* Call select and wait for read activity */ FOREVER { tmpFds = readFds; if (select (width, &tmpFds, NULL,NULL,NULL) == ERROR) { for (i=0; i < nFds; i++) close (fds[i]); exit (ERROR); } else for (i=0; i < nFds; i++) if (FD_ISSET (fds[i], &tmpFds)) fooDoRequest (fds[i]); } } /************************************************ * fooDoRequest - Read the null-terminated * request,and send back the string * Youve got to be kidding!\n */

select( ) Wind River Systems

A-7

Demo Code - WindView Demo


demoTransfer.c
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 /* demoTransfer.c - pass blocks between two tasks */ /* sloppy test program */ #include #include #include #include #include #include vxWorks.h msgQLib.h stdlib.h stdio.h taskLib.h wvUtilLib.h

/* globals */ int senderDelayTicks = 1; int receiverDelayTicks = 1; int sendTask = ERROR; int receiveTask = ERROR; int taskOptions = 0; int taskStack = 10000; int senderPriority = 80; int receiverPriority = 90; MSG_Q_ID transferQ = NULL; int blk_size = 4096; int QBlocks = 16; int QOptions = MSG_Q_FIFO; BOOL transferStarted = FALSE; /* forward declarations */ void send1 (MSG_Q_ID mq); void receive1 (MSG_Q_ID mq); void dataWrite (char * pBlock, size_t nBytes);

select( ) Wind River Systems

A-8

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

void dataProcess (char * pBlock, size_t nBytes); STATUS transferStart (void) { if (transferStarted) return ERROR; transferQ = msgQCreate (QBlocks, sizeof (char *), QOptions); receiveTask = taskSpawn (tRecv1, receiverPriority, taskOptions, taskStack, (FUNCPTR) receive1, (int) transferQ, 0,0,0,0,0,0,0,0,0); sendTask = taskSpawn (tSend1, senderPriority, taskOptions, taskStack, (FUNCPTR) send1, (int) transferQ, 0,0,0,0,0,0,0,0,0); transferStarted = TRUE; return OK; } STATUS transferStop (void) { if (!transferStarted) return ERROR; msgQDelete (transferQ); transferStarted = FALSE; return OK; } void send1 ( MSG_Q_ID mq ) { char * pCh; FOREVER {

select( ) Wind River Systems

A-9

164 if ( (pCh = (char *) malloc (blk_size)) 165 == NULL) 166 { 167 printf (Out of memory in task %s.\n, 168 taskName(0)); 169 return; 170 } 171 172 dataWrite (pCh, blk_size); 173 174 if (msgQSend (mq, (char *)&pCh, sizeof (pCh), 175 WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR) 176 { 177 printf (msgQSend failed.\n); 178 free (pCh); 179 return; 180 } 181 182 taskDelay (senderDelayTicks); 183 } 184 } 185 186 VOID receive1 187 ( 188 MSG_Q_ID mq 189 ) 190 { 191 char * pCh; 192 193 FOREVER 194 { 195 if (msgQReceive (mq, (char *) &pCh, 196 sizeof (pCh), WAIT_FOREVER) 197 != sizeof (pCh)) 198 { 199 printf (msgQReceive failed.\n); 200 return; 201 } 202 203 dataProcess (pCh, blk_size); 204 free (pCh); 205 } 206 } 207 208

select( ) Wind River Systems

A-10

209 void dataWrite 210 ( 211 char * pBlock, 212 size_t nBytes 213 ) 214 { 215 216 int * pI = (int *) pBlock; 217 size_t nInts = nBytes / 4; 218 int x = 0; 219 WV_EVT (1); 220 while (pI - (int *) pBlock < nInts) 221 *pI++ = x++; 222 WV_EVT (2); 223 } 224 225 void dataProcess 226 ( 227 char* pBlock, 228 size_t nBytes 229 ) 230 { 231 size_t nInts = nBytes / 4; 232 int * pBeg = (int *) pBlock; 233 int * pEnd = pBeg + (nInts - 1); 234 int x; 235 WV_EVT (3); 236 while (pEnd > pBeg) 237 { 238 x = *pEnd; 239 *pEnd-- = *pBeg; 240 *pBeg++ = x; 241 } 242 WV_EVT (4); 243 } 244 1

select( ) Wind River Systems

A-11

Demo Code - WindView Demo


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
select( ) Wind River Systems

wvUtilLib./* wvUtilLib.h - WindView utility macros */ /* Copyright 1998 Wind River Systems, Inc. */ /* modification history -------------------01a,03nov98,dlk written. */ #ifndef __INCwvUtilLibh #define __INCwvUtilLibh #include #include #include #include #include #include vxWorks.h wvLib.h private/wvBufferP.h private/wvUploadPathP.h private/wvSockUploadPathLibP.h private/eventP.h

#ifdef __cplusplus extern C { #endif /* __cplusplus */ /* defines */

#ifdef WV_INSTR /************************************************** * The default upload path depends on the components * that are included : * * if (upload file is included) * default upload path = file * else if (TSFS socket is included) * default upload path = TSFS socket * else * default upload path = socket *
A-12

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

* That is, the default upload path will be the LAST * of the following which is included in the VxWorks * configuration: * * TCP/IP socket upload path * TSFS socket upload path * File upload path * * (See usrWindviewInit() in prjConfig.c) If none of * these are included, there is a configuration * error. * * You can use the WV_START macro to start event * log upload from your application. You can use the * macro as follows, assuming that the default * path is socket and that you want to upload to the * host from which the target boots, and evtRecv or a * graph is listening on the default port 6164: * * WV_START(WV_CLASS_3, sysBootHost, 6164 ); */ #define WV_START(evtClass, path, arg)\ wvOn(evtClass, path, arg, 0) #define WV_STOP\ do {\ if (wvEvtBufferGet())\ wvOff();\ } while (FALSE) #define WV_EVT(id) \ wvEvent ((id), (char *)NULL, 0) #define WV_EVT_STRING(id, str) \ wvEvent ((id), (char *)(str), strlen(str) + 1) #define WV_EVT_OBJ(id, obj) \ wvEvent ((id), (char *)&(obj), sizeof (obj)) #define WV_EVT_ARRAY(id, array) \ wvEvent ((id), (char *)(array), sizeof (array)) #define WV_INST_SEM(semId) \ wvObjInst (OBJ_SEM, (semId), INSTRUMENT_ON)

select( ) Wind River Systems

A-13

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1 1

#define WV_INST_MSGQ(msgQId) \ wvObjInst (OBJ_MSG, (msgQId), INSTRUMENT_ON) #define WV_INST_WDOG(wdogId) \ wvObjInst (OBJ_WD, (wdogId), INSTRUMENT_ON) #define WV_INST_TASK(taskId) \ wvObjINST (OBJ_TASK, (void *) (taskId), \ INSTRUMENT_ON) #define WV_OBJ_INST_ON \ wvObjInstModeSet (INSTRUMENT_ON) #define WV_OBJ_INST_OFF \ wvObjInstModeSet (INSTRUMENT_OFF) #else /* WV_INSTR not defined */ #define #define #define #define #define #define #define #define #define #define #define #define WV_EVT(id) (void) OK WV_EVT_STRING(id, str) (void) OK WV_EVT_OBJ(id, obj) (void) OK WV_EVT_ARRAY(id, array) (void) OK WV_INST_SEM(semId) (void) OK WV_INST_MSGQ(msgQId) (void) OK WV_INST_WDOG(sdogId) (void) OK WV_INST_TASK(taskId) (void) OK WV_OBJ_INST_ON (void) OK WV_OBJ_INST_OFF (void) OK WV_START(evtClass, path, arg) (void) OK WV_STOP

#endif /* WV_INSTR */ /* typedefs */ /* function declarations */ #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* __INCwvUtilLibh */

select( ) Wind River Systems

A-14

Das könnte Ihnen auch gefallen