/* **MEMBER**=NONE **CMS**=PCKNOBS ============================================================================== Abs: Top level routines for the PC in the PCknobs system. Name: drvKnobScp.c main The big main dude! static PKMain_Init Initialize structs and communication static PKMain_Proc_Sock Process input socket messages static PKMain_Proc_Knobs Process any attached knobs static PKMain_Get_Pointers Get global structure pointers Proto: Defined locally since they're not referenced by anyone. Auth: 18-Oct-1995, Robert C. Sass (RCS) Rev: 07-Dec-1995, Ron Chestnut (RON) -------------------------------------------------------------------------------- Mod: (newest to oldest) 29-Feb-2014, Sonya Hoobler (SONYA): Modify to communicate with knobs over network instead of serial. Many associated changes. Modify to run in context of EPICS IOC and added support for EPICS records. 27-Sep-1996, Robert C. Sass (RCS): Print Verion on Startup. Allow 2 spaces for the "I'm alive" rotating character. Add global line counter so we can scroll error messages from other modules. 04-Apr-1996, Robert C. Sass (RCS): Because of the Pathworks buffering limit of 12 for UDP Datagrams, scan the socket more rapidly than the knobs. ============================================================================== */ #include /* exit */ #include #include /* strcpy */ #include #include #include #include #include #include #include /* close */ #include "slc_macr.h" /* vmsstat_t etc. */ #include "drvKnobScp.h" #include "drvKnobScpAlpha.h" #include "drvKnobScpBox.h" #include #include #include #include #include #include /* ** Local prototypes */ static void PKArgs_Init (pcglobal_ts *pcglobal_ps, const char *node, uint32_t mask, int line); static int PKMain_Init (pcglobal_ts *pcglobal_ps); static void PKMain_Proc (pcglobal_ts *pcglobal_ps); static void PKMain_Proc_Sock (pcglobal_ts *pcglobal_ps); static void PKMain_Proc_Knobs (pcglobal_ts *pcglobal_ps); static vmsstat_t PKMain_Get_Pointers (pcglobal_ts *pcglobal_ps, pvhead_ts *pvhead_ps, line_ts **oline_pps, box_ts **obox_pps, knob_ts **oknob_pps, int socknum, struct sockaddr_in *outsock_ps); static void PKMain_Exit_Handler (void); /* ** The top level static global structure. It has everything we need to know. ** We pass around pointers to it so it's not referenced as an extern by ** other modules. */ static pcglobal_ts PCglobal_s = { 0 }; static epicsFloat64 poll_period = 100; // milliseconds epicsFloat64 box_timeout = 500; // milliseconds unsigned short debug = 0; /* Diagnostic counters*/ volatile epicsUInt32 knobAttach = 0; volatile epicsUInt32 knobDetach = 0; /*============================================================================ Abs: Main routine Name: knob_main Args: node0 node name of device that comm with knobs Use: string Type: const char * Acc: read only mask0 mask of which boxes' hardware to initialize Use: integer value of 0 indicates all boxes Type: uint32_t Acc: read/write node1 node name of device that comm with knobs Use: string Type: const char * Acc: read only mask1 mask of which boxes' hardware to initialize Use: integer value of 0 indicates all boxes Type: uint32_t Acc: read/write Rem: Main control for the PC knobs program. After initialization, it forever scans for VAX socket input and changes on attached knobs. It then delays until the next time interval. This program supports 2 "lines", nowadays that means 2 terminal servers, each to service half of the knobs. The input arguments are for these 2 devices. Side: None. Ret: None. Exits if initialization problems. ============================================================================== */ void knob_main (const char *node0, uint32_t mask0, const char *node1, uint32_t mask1) { char taskName[50]; /*--------------------------- Code ------------------------------------------*/ /* ** Do some initialization stuff. */ if ( debug >= LOW ) printf("Enter knob_main node %s mask 0x%04x and node %s mask 0x%04x\n", node0, mask0, node1, mask1); /* ** Populate global structure with arguments for our possible lines */ PKArgs_Init(&PCglobal_s, node0, mask0, 0); PKArgs_Init(&PCglobal_s, node1, mask1, 1); if (!PKMain_Init (&PCglobal_s)) // Init everything { printf("FATAL: error during pkmain_init\n"); exit (EXIT_FAILURE); goto egress; } /* Start task to poll knobs and input from Alpha */ sprintf( taskName, "KnobMainPoll" ); epicsThreadMustCreate( taskName, epicsThreadPriorityHigh, epicsThreadGetStackSize(epicsThreadStackMedium), PKMain_Proc, &PCglobal_s ); egress: return; } void PKMain_Proc (pcglobal_ts *pcglobal_ps) { for (;;) { int i; for ( i = 0; i < 2; i++ ) PKMain_Proc_Sock (&PCglobal_s); // Process socket message PKMain_Proc_Knobs (&PCglobal_s); // Poll/process the knobs epicsThreadSleep(poll_period/1000); // epicsThreadSleep expects seconds } } /*============================================================================= Abs: Process some initialization parameters Name: PKArgs_Init Args: pcglobal_ps pointer to global data Use: pointer Type: pcglobal_ts * Acc: read only Mech: by reference node node name of device that comm with knobs Use: string Type: const char * Acc: read only mask mask of which boxes' hardware to initialize Use: integer Type: uint32_t Acc: read only line line number Use: integer Type: int Acc: read only Rem: If node arg is present, store it in global structure and set line "exists" to 1. Mask of 0 indicates all boxes should be initialized. Set this and also store in line structure. Ret: None ============================================================================= */ static void PKArgs_Init (pcglobal_ts *pcglobal_ps, const char *node, uint32_t mask, int line) { /*--------------------------- Code ---------------------------------------*/ if ( node ) { memcpy((void *)pcglobal_ps->lines_as[line].node, (void *)node, NODE_NAME_MAX); pcglobal_ps->lines_as[line].exists = 1; } if ( mask == 0 ) mask = 0xFFFF; pcglobal_ps->lines_as[line].mask = mask; } /*============================================================================= Abs: Initialize EPICS record device support Name: PKDevsup_Init Args: pcglobal_ps pointer to global data Use: pointer Type: pcglobal_ts * Acc: read only Mech: by reference Ret: C logical true/false: 1 on success, 0 on error ============================================================================= */ static int PKDevSup_Init (pcglobal_ts *pcglobal_ps) { int i, l, b; char str[20]; DevGenVarRec *rec_p = boxStat; /*--------------------------- Code ---------------------------------------*/ /* ** Init device support -- need to update! */ sprintf(str,"BoxStat"); devGenVarInit( boxStat, BOX_VAR_CNT ); scanIoInit( &boxVarScan ); for ( i = 0; i < BOX_VAR_CNT; i++ ) rec_p[i].dbr_t = DBR_CHAR; for ( l = 0; l < MAX_LINES_PC; l++ ) { for ( b = 0; b < MAX_BOXES_LINE; b++, rec_p++ ) { rec_p->data_p = &pcglobal_ps->lines_as[l].boxes_as[b].connected; rec_p->scan_p = &boxVarScan; } } if ( devGenVarRegister( str, boxStat, BOX_VAR_CNT ) ) { epicsPrintf("FATAL: devGenVarRegister failed\n"); return 0; } sprintf(str,"BoxAvail"); devGenVarInit( boxAvail, BOX_VAR_CNT ); rec_p = boxAvail; for ( i = 0; i < BOX_VAR_CNT; i++ ) rec_p[i].dbr_t = DBR_CHAR; for ( l = 0; l < MAX_LINES_PC; l++ ) { for ( b = 0; b < MAX_BOXES_LINE; b++, rec_p++ ) { rec_p->data_p = &pcglobal_ps->lines_as[l].boxes_as[b].avail; } } if ( devGenVarRegister( str, boxAvail, BOX_VAR_CNT ) ) { epicsPrintf("FATAL: devGenVarRegister failed\n"); return 0; } sprintf(str,"IocVar"); devGenVarInit( iocVar, IOC_VAR_CNT ); rec_p = iocVar; for ( i = 0; i < IOC_VAR_CNT; i++ ) rec_p[i].dbr_t = DBR_DOUBLE; rec_p->data_p = &poll_period; rec_p++; rec_p->data_p = &box_timeout; rec_p++; rec_p->dbr_t = DBR_USHORT; rec_p->data_p = &debug; if ( devGenVarRegister( str, iocVar, IOC_VAR_CNT ) ) { epicsPrintf("FATAL: devGenVarRegister failed\n"); return 0; } return 1; } /*============================================================================= Abs: Main initialization routine. Name: PKMain_Init Args: pcglobal_ps pointer to global data Use: pointer Type: pcglobal_ts * Acc: read only Mech: by reference Rem: We first call PKDevSup_Init to initialize EPICS records, then PKVmsg_Init to create and bind the socket used for our VAX communication. Then we loop through our global structure by line (RS485) box and knob, initializing all variables. Call PKNmsg_Init_Box to init each box indicated by mask. Side: Global structure, VAX socket and RS485 lines initialized. Ret: C logical true/false. ============================================================================= */ static int PKMain_Init (pcglobal_ts *pcglobal_ps) { charu l,b,k; /* Loopers for lines, boxes & knobs */ line_ts *lline_ps; /* Local line struct pointer */ box_ts *lbox_ps; /* Local box struct pointer */ knob_ts *lknob_ps; /* Local knob struct pointer */ int ostatus; /* Return logical status */ charu lcpuid; /* Keep track of cpu id */ unsigned osstatus_p; char obuf_p[LEGEND_SIZE] = {' '}; /*--------------------------- Code ---------------------------------------*/ /* ** Init device support. */ if (!(ostatus = PKDevSup_Init (pcglobal_ps))) goto egress; /* ** Init socket for UDP message processing. */ if (!(ostatus = PKVmsg_Init (&pcglobal_ps->socknum, &pcglobal_ps->oursocket_s))) goto egress; /* ** Line level stuff. Call PKNmsg_Init_Line to init RS485 line parameters. */ for (l = 0; l < MAX_LINES_PC; l++) { lcpuid = 0; lline_ps = &pcglobal_ps->lines_as[l]; // ptr this line if ( !lline_ps->exists ) break; /* ** Box level stuff. We save the first cpu in the box because the legend ** update message doesn't follow the rules; the cpu is always the first ** one in the box and the LED to update is specified in the message. */ for (b=0; b < MAX_BOXES_LINE; b++) { lbox_ps = &lline_ps->boxes_as[b]; // ptr this box lbox_ps->exists = 0; // Assume it's not there lbox_ps->avail = 1; // Assume we want to use it if it's there lbox_ps->firstcpu = lcpuid; // First cpuid this box lbox_ps->box = b; if ( lline_ps->mask & (1 << b) ) { if (!(ostatus = PKNmsg_Init_Box (lline_ps->node, b, &lbox_ps->socknum, &lbox_ps->socket_s, &lbox_ps->boxsocket_s, &lbox_ps->connected))) goto egress; } if ( debug >= MED ) printf("PKMain_Init: line %i box %i lcpuid %i\n", l, b, lcpuid); /* ** Knobs level stuff. Set not attached, init set number, cpu, status ** error count & VAX sequence number. Last we increment the cpuid. */ for (k = 0; k < MAX_KNOBS_BOX; k++) { /* Clear knob legend */ if ( (lline_ps->mask & (1 << b)) && lbox_ps->connected ) PKNmsg_Legend_Update (lline_ps->node, b*4, k, 0, obuf_p, &osstatus_p, lbox_ps); lknob_ps = &lbox_ps->knobs_as[k];// ptr this knob */ lknob_ps->attached = 0; // Not attached lknob_ps->ledset = k; // led set (0-MAX_KNOBS_BOX) lknob_ps->ledcpu = lcpuid; // this knobs cpuid lknob_ps->laststat = PK_OKOK; // last error sent to VAX lknob_ps->errors = 0; // count of knob errors lknob_ps->seqnum = 0; // last VAX sequence number lcpuid++; // next cpuid } /* For all knobs this box */ } /* For all boxes this line */ } /* For all lines this PC */ egress: return (ostatus); } /*============================================================================= Abs: Process input from VAX on our IP socket. Name: PKMain_Proc_Sock Args: pcglobal_ps pointer to global data Use: pointer Type: pcglobal_ts * Acc: read only Mech: by reference Rem: While there are messages available, process them. We dispatch on the message code which is always the first char received. PKMain_Get_Pointers returns pointers to the line, box & knob structs from the line, box and knob numbers in the message header. Only the heartbeat message doesn't have this knob info. Side: None. Ret: Void. ============================================================================= */ static void PKMain_Proc_Sock (pcglobal_ts *pcglobal_ps) { int msgavail; /* Logical if there is a message to read */ struct sockaddr_in insocket_s; /* Socket info of sender */ charu inmsgbuf[MAX_VAX_MSG]; /* A buf to receive a VAX msg */ int inmsglen; /* Length of VAX message */ line_ts *lline_ps; /* Local line pointer */ box_ts *lbox_ps; /* Local box pointer */ knob_ts *lknob_ps; /* Local knob pointer */ int i; /*--------------------------- Code ---------------------------------------*/ /* ** Read and process all msgs waiting for us from the VAX. */ do { msgavail = PKVmsg_Recv (pcglobal_ps->socknum, &insocket_s, inmsgbuf, &inmsglen); if (msgavail) { if ( debug >= HIGH ) { printf("\nPKMain_Proc_Sock: msg avail; type %i; contents:\n", inmsgbuf[0]); for ( i = 0; i < inmsglen; i++ ) printf("%c ", inmsgbuf[i]); printf("\n"); printf("socket addr %i\n", insocket_s.sin_addr.s_addr); } /* ** Dispatch to the appropriate routine based on the message code in ** the first char. */ switch (inmsgbuf[0]) // Dispatch on message type code { case FVATTACH: // Attach a knob if ( debug >= HIGH ) printf("PKMain_Proc_Sock: process FVATTACH msg\n"); if (!SUCCESS(PKMain_Get_Pointers (pcglobal_ps, (pvhead_ts *) inmsgbuf, &lline_ps, &lbox_ps, &lknob_ps, pcglobal_ps->socknum, &insocket_s))) break; lbox_ps->exists = 1; // This box exists knobAttach++; PKVmsg_FVAttach(lknob_ps, lbox_ps, (fvlegend_ts *) &inmsgbuf, lline_ps->node, pcglobal_ps->socknum, &insocket_s); break; case FVDETACH: // Deattach a knob if ( debug >= HIGH ) printf("PKMain_Proc_Sock: process FVDETACH msg\n"); if (!SUCCESS(PKMain_Get_Pointers (pcglobal_ps, (pvhead_ts *) inmsgbuf, &lline_ps, &lbox_ps, &lknob_ps, pcglobal_ps->socknum, &insocket_s))) break; knobDetach++; PKVmsg_FVDetach(lknob_ps, lbox_ps, (fvlegend_ts *) &inmsgbuf, lline_ps->node, pcglobal_ps->socknum, &insocket_s); break; case FVLEGEND: // Update a knob legend if ( debug >= HIGH ) printf("PKMain_Proc_Sock: process FVLEGEND msg\n"); if (!SUCCESS(PKMain_Get_Pointers (pcglobal_ps, (pvhead_ts *) inmsgbuf, &lline_ps, &lbox_ps, &lknob_ps, pcglobal_ps->socknum, &insocket_s))) break; PKVmsg_Legend_Update (lknob_ps, lbox_ps,//->firstcpu, (fvlegend_ts *) &inmsgbuf, lline_ps->node, pcglobal_ps->socknum, &insocket_s); break; case FVHEART: // Check our heart if ( debug >= HIGH ) printf("PKMain_Proc_Sock: process FVHEART msg\n"); PKVmsg_TVHeart (pcglobal_ps->socknum, &insocket_s); break; default: if ( debug >= HIGH ) printf("PKMain_Proc_Sock: default msg case\n"); PKVmsg_TVAck (pcglobal_ps->socknum, &insocket_s, (pvhead_ts *) &inmsgbuf, 0, PK_UNKWN_MSG); break; } // End of message dispatch } // If message available } while (msgavail); // While msgs left to process return; } /*============================================================================= Abs: Scan all attached knobs for changes. Name: PKMain_Proc_Knobs Args: pcglobal_ps pointer to global data Use: pointer Type: pcglobal_ts * Acc: read only Mech: by reference Rem: Loop through each line, box and knob. For each line & box that exists, check if any knob is attached. If it is, request the knob's current delta count. If the request was good and the count is non-zero, send the result in a TVTURNS message to the SCP that assigned the knob. If there was an error talking to the knob and the last status was good, send a TVTURNS with a 0 count and the error status. Side: None. Ret: Void. ============================================================================= */ static void PKMain_Proc_Knobs (pcglobal_ts *pcglobal_ps) { charu l,b,k; /* Loopers for lines, boxes & knobs */ line_ts *lline_ps; /* Local line pointer */ box_ts *lbox_ps; /* Local box pointer */ knob_ts *lknob_ps; /* Local knob pointer */ /*--------------------------- Code ---------------------------------------*/ for (l = 0; l < MAX_LINES_PC; l++) { lline_ps = &pcglobal_ps->lines_as[l]; // ptr this line if (lline_ps->exists) { for (b=0; b < MAX_BOXES_LINE; b++) { lbox_ps = &lline_ps->boxes_as[b]; // ptr this box */ if (lbox_ps->exists && lbox_ps->avail) { for (k = 0; k < MAX_KNOBS_BOX; k++) { lknob_ps = &lbox_ps->knobs_as[k];// ptr this knob */ /* ** If knob is attached, send VAX a TVTURNS msg if changed. */ if (lknob_ps->attached) { if ( debug >= HIGH) printf("PKMain_Proc_Knobs: before calling tvturns line %i box %i knob %i\n", l, b, k); PKVmsg_TVTurns (lknob_ps, lbox_ps, lline_ps->node, l, b, pcglobal_ps->socknum); } } // For each knob } // If box exists } // For all boxes this line } // If this line exists } // For all lines return; } /*============================================================================= Abs: Return line, box and knob pointers from indexes in message header. Name: PKMain_Get_Pointers Args: pcglobal_ps pointer to global data Use: pointer Type: pcglobal_ts * Acc: read only Mech: by reference pvhead_ps pointer to message header Use: pointer Type: pvhead_ts * Acc: read only Mech: by reference oline_pps returned pointer line structure Use: pointer Type: line_ts ** Acc: write only Mech: by reference obox_pps returned pointer box structure Use: pointer Type: box_ts ** Acc: write only Mech: by reference oknob_pps returned pointer to knob structure Use: pointer Type: knob_ts ** Acc: write only Mech: by reference socknum our socket number Use: integer Type: int Acc: read Mech: by value outsock_ps socket struct to send to Use: pointer Type: struct sockaddr_in * Acc: read Mech: by reference Rem: Get line pointer by indexing lines from line # in msg header. Get box pointer in line & knob point in box. Side: None. Ret: PK_OKOK If all is well. PK_BADPARAM If bad line, box or knob number in message. ============================================================================= */ static vmsstat_t PKMain_Get_Pointers (pcglobal_ts *pcglobal_ps, pvhead_ts *pvhead_ps, line_ts **oline_pps, box_ts **obox_pps, knob_ts **oknob_pps, int socknum, struct sockaddr_in *outsock_ps) { vmsstat_t iss = PK_OKOK; // Assume all will be well unsigned dumsstatus = 0; // A dummy serial status /*--------------------------- Code ---------------------------------------*/ /* ** If input parameters are OK return pointers else send error to VAX and ** return error. */ if (!pcglobal_ps->lines_as[(int)pvhead_ps->line].exists) { iss = PK_BADPARAM; PKVmsg_TVAck (socknum, outsock_ps, pvhead_ps, dumsstatus, iss); goto egress; } if ( (pvhead_ps->line >= 0) && (pvhead_ps->line < MAX_LINES_PC) && (pvhead_ps->box >= 0) && (pvhead_ps->box < MAX_BOXES_LINE) && (pvhead_ps->knob >= 0) && (pvhead_ps->knob < MAX_KNOBS_BOX) ) { if ( debug >= HIGH ) printf("PKMain_Get_Pointers: input parameters ok, line %i, box %i, knob %i\n", pvhead_ps->line, pvhead_ps->box, pvhead_ps->knob); *oline_pps = &(pcglobal_ps->lines_as[(unsigned)pvhead_ps->line]); *obox_pps = &((*oline_pps)->boxes_as[(unsigned)pvhead_ps->box]); *oknob_pps = &((*obox_pps)->knobs_as[(unsigned)pvhead_ps->knob]); } else { iss = PK_BADPARAM; PKVmsg_TVAck (socknum, outsock_ps, pvhead_ps, dumsstatus, iss); } egress: return (iss); } /*============================================================================= Abs: Exit handler for us. (not currently in use) Name: PKMain_Exit_Handler Args: None. Rem: Close our socket. This is to cover the possibility of someone doing a while we're running. We must close the socket else if we're started up again we'll get a bind error at initialize time. Side: None. Ret: Void. ============================================================================= */ static void PKMain_Exit_Handler (void) { int status; char blocking = 0; /*--------------------------- Code ---------------------------------------*/ /* ** Set the socket to blocking so the sclose is synchronous. */ status = ioctl(PCglobal_s.socknum, FIONBIO, &blocking); status = close (PCglobal_s.socknum); return; } static long drvKnobScpReport(int level) { printf("Knob support\n"); return 0; } static long drvKnobScpInit(void) { return 0; } static struct { long number; DRVSUPFUN report; DRVSUPFUN init; } drvKnobScp={ 2, drvKnobScpReport, drvKnobScpInit }; epicsExportAddress(drvet,drvKnobScp); /* * IOC shell command registration * * Node name and mask of active boxes for each device, typically terminal server (2 supported) * First node name is required but second is optional. Set second to 0 if not in use */ static const iocshArg arg0 = { "terminal server 1 name", iocshArgString}; static const iocshArg arg1 = { "mask of active boxes on ts1", iocshArgInt}; static const iocshArg arg2 = { "terminal server 2 name", iocshArgString}; static const iocshArg arg3 = { "mask of active boxes on ts2", iocshArgInt}; static const iocshArg *args[] = { &arg0, &arg1, &arg2, &arg3 }; static const iocshFuncDef knobFuncDef = { "knob_main", 4, args }; static void knobFunc(const iocshArgBuf *args) { if ( !(strcmp(args[0].sval, "0")) ) { epicsPrintf("FATAL -- need first I/O node name (typically terminal server)\n"); return; } if ( !(strcmp(args[2].sval, "0")) ) { knob_main( args[0].sval, args[1].ival, NULL, args[3].ival ); return; } knob_main( args[0].sval, args[1].ival, args[2].sval, args[3].ival ); } static void drvKnobScpRegisterCommands(void) { static int firstTime = 1; if ( firstTime ) { iocshRegister(&knobFuncDef, knobFunc); firstTime = 0; } } epicsExportRegistrar(drvKnobScpRegisterCommands);