1:/*--------------------------------------------------------------------------- 2: 3: FILENAME: 4: templateStates.c 5: 6: PURPOSE: 7: Provide the template process state machine handlers. 8: 9: REVISION HISTORY: 10: Date Engineer Revision Remarks 11: 01/01/2004 Your Name 0 Original 12: 13: NOTES: 14: All references to "template" should be replaced by a meaningful 15: process name, including the file names and data structures. 16: 17:----------------------------------------------------------------------------*/ 18: 19:// System include files 20:#include <termios.h> 21: 22:// Library include files 23: 24:// Local include files 25:#include "template.h" 26: 27: 28:// global memory declarations 29: 30:// global memory referenced 31: 32:// static (local) memory declarations 33: 34:static void sendRequest (int number) 35:{ 36: TEMPLATE_MSG_USER_REQUEST request; 37: 38: request.number = number; 39: 40: radMsgRouterMessageSend (TEMPLATE_MSGID_USER_REQUEST, &request, sizeof(request)); 41: 42: return; 43:} 44: 45:static void processResponse (TEMPLATE_MSG_USER_RESPONSE *response) 46:{ 47: printf ("Number of primes < %d is %d\n", 48: response->number, 49: response->numberOfPrimes); 50: 51: return; 52:} 53: 54: 55:// methods 56: 57:int templateIdleState (int state, void *stimulus, void *data) 58:{ 59: STIM *stim = (STIM *)stimulus; 60: TEMPLATE_WORK *work = (TEMPLATE_WORK *)data; 61: time_t ntime; 62: struct tm *locTime; 63: 64: switch (stim->type) 65: { 66: case STIM_DUMMY: 67: // this one starts this state machine; 68: // do any initialization, communication, etc. here 69: 70: // start timer 1 71: radTimerStart (work->timerNum1, TEMPLATE_TIMER1_PERIOD); 72: 73: // start timer 2 74: radTimerStart (work->timerNum2, TEMPLATE_TIMER2_PERIOD); 75: 76: // return the new state for the machine 77: return TEMPLATE_STATE_RUN; 78: 79: case STIM_QMSG: 80: case STIM_EVENT: 81: case STIM_TIMER: 82: break; 83: } 84: 85: return state; 86:} 87: 88:int templateRunState (int state, void *stimulus, void *data) 89:{ 90: STIM *stim = (STIM *)stimulus; 91: TEMPLATE_WORK *work = (TEMPLATE_WORK *)data; 92: char temp[256]; 93: ssize_t retVal; 94: int value; 95: 96: switch (stim->type) 97: { 98: case STIM_QMSG: 99: if (stim->msgType == TEMPLATE_MSGID_USER_RESPONSE) 100: { 101: // print out the results 102: processResponse ((TEMPLATE_MSG_USER_RESPONSE *)stim->msg); 103: } 104: 105: break; 106: 107: case STIM_TIMER: 108: // process a timer expiry 109: if (stim->timerNumber == TEMPLATE_TIMER_NUM1) 110: { 111: radMsgLog (PRI_STATUS, "Timer 1 Expiry"); 112: radTimerStart (work->timerNum1, TEMPLATE_TIMER1_PERIOD); 113: return state; 114: } 115: else if (stim->timerNumber == TEMPLATE_TIMER_NUM2) 116: { 117: radMsgLog (PRI_STATUS, "Timer 2 Expiry"); 118: radTimerStart (work->timerNum2, TEMPLATE_TIMER2_PERIOD); 119: return state; 120: } 121: break; 122: 123: case STIM_IO: 124: // read data from stdin 125: memset (temp, 0, 256); 126: retVal = read (stim->iofd, temp, 256); 127: if (retVal == (ssize_t)-1) 128: { 129: radMsgLog (PRI_HIGH, "read error on stdin!"); 130: return TEMPLATE_STATE_ERROR; 131: } 132: else if (retVal == 0) 133: { 134: return state; 135: } 136: 137: // send a message to the template2d process to compute the primes 138: value = atoi (temp); 139: if (value <= 0) 140: { 141: printf ("Integer must be positive!!!\n"); 142: } 143: else 144: { 145: sendRequest (value); 146: } 147: 148: // return the same state that got us here (RUN) 149: return state; 150: 151: case STIM_EVENT: 152: break; 153: } 154: 155: return state; 156:} 157: 158:int templateErrorState (int state, void *stimulus, void *data) 159:{ 160: STIM *stim = (STIM *)stimulus; 161: TEMPLATE_WORK *work = (TEMPLATE_WORK *)data; 162: 163: 164: switch (stim->type) 165: { 166: case STIM_IO: 167: case STIM_QMSG: 168: case STIM_EVENT: 169: case STIM_TIMER: 170: radMsgLog (PRI_STATUS, 171: "%s:templateErrorState: received stimulus %d", 172: PROC_NAME_TEMPLATE, 173: stim->type); 174: break; 175: } 176: 177: return state; 178:}