/* ============================================================================== Abs: Top level routines for the Knob Controller Much of this is from PC knob files ============================================================================== */ #include /* exit */ #include #include /* strcpy */ #include /* gettimeofday */ #include #include #include #include #include #include #include #include #include #include "knobCtrl.h" #include "knobCtrlUserComm.h" #include "knobCtrlBoxComm.h" /* ** Local prototypes */ static int CKMain_Init(ctrlglobal_ts *ctrlglobal_ps); static void CKProc_User(void); static void CKProc_Sock(void); static void CKProc_Knob(void *arg); static uint32_t CKMain_Get_Pointers (ctrlglobal_ts *ctrlglobal_ps, void *kuserio_p, cuhead_ts *cuhead_ps, line_ts **oline_pps, box_ts **obox_pps, knob_ts **oknob_pps, int *line, int *box, int *knob); /* Top-level global data struture */ ctrlglobal_ts ctrlglobal_s = { 0 }; /* Debug verbosity flags */ short boxdbg[MAX_BOXES_LINE] = { 0 }; /* Per-box debug verbosity, set by PVs, locked with varBoxMutex */ short gbldbg = 0; /* Global debug verbosity, based on per-box flags, locked with ctrlglobal_s.mtx */ knobcb_ts knobCommCallbacks = { 0 }; kusercb_ts knobUserCommCallbacks = { 0 }; const void *knobCtrlBoxCommCbRegistryId = (void*)&knobCtrlBoxCommCbRegistryId; const void *knobCtrlUserCommCbRegistryId = (void*)&knobCtrlUserCommCbRegistryId; /* DevGenVar records and mutexes, for device support */ static DevGenVarRec varBoxDebug[MAX_BOXES_LINE]; static DevGenVarRec varBoxType[MAX_BOXES_LINE]; static DevGenVarRec varKnobTurns[MAX_BOXES_LINE][MAX_KNOBS_BOX]; static DevGenVarRec varKnobHwStat[MAX_BOXES_LINE][MAX_KNOBS_BOX]; static epicsMutexId varBoxMtx[MAX_BOXES_LINE]; /*============================================================================ Abs: Routine to initialize a Knob Controller program. Check for callbacks, start EPICS thread to perform processing. Name: knobInit Args: knobcb_s Pointer to callbacks to communicate with knob boxes kusercb_s Pointer to callbacks to communicate with knob users lineio Handle of communication line mask Mask of supported boxes. 0 = all Rem: No locking as this is called in single thread at startup Ret: None. Exits if initialization problems. Should we return a status value? ==============================================================================*/ static void knobInit (void *knobcb_s, void *kusercb_s, void *lineio, uint32_t mask) { char taskname[50]; int b; line_ts *lline_ps; /* Local line struct pointer */ box_ts *lbox_ps; /* Local box struct pointer */ uint8_t arg; if ( !knobcb_s ) { printf("knobInit: need pointer to knob comm callback info!\n"); goto egress; } if ( !kusercb_s ) { printf("knobInit: need pointer to knob user comm callback info!\n"); goto egress; } knobCommCallbacks = *(knobcb_ts *)knobcb_s; knobUserCommCallbacks = *(kusercb_ts *)kusercb_s; /* ** Populate global structure with arguments for our line */ if ( (CKmsg_Init_Line( &ctrlglobal_s.line_s, lineio )) ) { printf("FATAL: Failed to initialize line\n"); goto egress; } ctrlglobal_s.line_s.exists = 1; ctrlglobal_s.mask = mask; if ( CKMain_Init (&ctrlglobal_s) ) { printf("FATAL: Error during CKMain_Init\n"); goto egress; } lline_ps = &ctrlglobal_s.line_s; if ( !lline_ps->exists ) goto egress; for ( b=0; b < MAX_BOXES_LINE; b++ ) { lbox_ps = &lline_ps->boxes_as[b]; /* If this box not supported, do not launch task for it */ if ( (0 == (mask & 1<line_s.boxes_as[b].mtx; varBoxType[b].data_p = &ctrlglobal_ps->line_s.boxes_as[b].type; varBoxType[b].scan_p = &varBoxScan[b]; } sprintf(str,"BoxDebug"); if ( devGenVarRegister( str, varBoxDebug, MAX_BOXES_LINE ) ) { printf("FATAL: devGenVarRegister failed\n"); return -1; } sprintf(str,"BoxType"); if ( devGenVarRegister( str, varBoxType, MAX_BOXES_LINE ) ) { printf("FATAL: devGenVarRegister failed\n"); return -1; } /* Per knob turns and status variables */ for ( b = 0; b < MAX_BOXES_LINE; b++ ) { devGenVarInit( varKnobTurns[b], MAX_KNOBS_BOX ); devGenVarInit( varKnobHwStat[b], MAX_KNOBS_BOX ); for ( k = 0; k < MAX_KNOBS_BOX; k++ ) { scanIoInit( &varKnobScan[b][k] ); varKnobTurns[b][k].dbr_t = DBR_LONG; varKnobTurns[b][k].data_p = &ctrlglobal_ps->line_s.boxes_as[b].knobs_as[k].counts; varKnobTurns[b][k].mtx = ctrlglobal_ps->line_s.boxes_as[b].knobs_as[k].mtx; varKnobHwStat[b][k].dbr_t = DBR_USHORT; varKnobHwStat[b][k].data_p = &ctrlglobal_ps->line_s.boxes_as[b].knobs_as[k].lkstatus; varKnobHwStat[b][k].mtx = ctrlglobal_ps->line_s.boxes_as[b].knobs_as[k].mtx; varKnobHwStat[b][k].scan_p = &varKnobScan[b][k]; } sprintf(str,"Box%iKnobTurns", b); if ( devGenVarRegister( str, varKnobTurns[b], MAX_KNOBS_BOX ) ) { printf("FATAL: devGenVarRegister failed\n"); return -1; } sprintf(str,"Box%iKnobHwStat", b); if ( devGenVarRegister( str, varKnobHwStat[b], MAX_KNOBS_BOX ) ) { printf("FATAL: devGenVarRegister failed\n"); return -1; } } return 0; } /*============================================================================= Abs: Main initialization routine. Name: CKMain_Init Args: ctrlglobal_ps pointer to global data Rem: Initialize device support, communication with knob users, and data structures. Caller must lock if this runs after startup Ret: 0 on success, -1 on error =============================================================================*/ static int CKMain_Init (ctrlglobal_ts *ctrlglobal_ps) { unsigned char b,k; /* Loopers for 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 rval, i; /* Return status, loop index */ unsigned char lcpuid; /* Keep track of cpu id */ unsigned osstatus_p; char obuf_p[LEGEND_SIZE]; short dbg; for ( i = 0; i < LEGEND_SIZE; i++ ) obuf_p[i] = ' '; /* Initialize communication for receiving/sending messages from/to clients */ if ( (rval = CUmsg_Init( ctrlglobal_ps )) ) goto egress; ctrlglobal_ps->mtx = epicsMutexMustCreate(); /* Line level stuff. Call CKmsg_Init_Line to init knob communication line parameters */ lcpuid = 0; lline_ps = &ctrlglobal_ps->line_s; /* ptr this line */ lline_ps->line = LINE_ID; lline_ps->mtx = epicsMutexMustCreate(); /* Box level stuff. Box 0 not supported, begin at 1. * 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->mtx = epicsMutexMustCreate(); lbox_ps->firstcpu = lcpuid; /* First cpuid this box */ lbox_ps->box = b; lbox_ps->firstuse = 1; /* Not yet initialized */ dbg = boxdbg[b]; /* Only send messages if this box is supported */ if ( (0 != (ctrlglobal_ps->mask & 1<knobs_as[k]; /* ptr this knob */ /* Only do this initialization if this box is supported */ if ( (0 != (ctrlglobal_ps->mask & 1<kuserio_p = CUmsg_Init_Knob(); if ( 0 == lknob_ps->kuserio_p ) { rval = -1; printf("FATAL: CKMain_Init: box %i knob %i failed to create kuserio_p\n", b, k); goto egress; } lknob_ps->mtx = epicsMutexMustCreate(); /* Clear knob legend.*/ if ( lbox_ps->connected ) { CKmsg_Legend_Update ( lline_ps, lbox_ps, k, 0, 0, obuf_p, &osstatus_p, dbg ); if ( KNOB_BOX_PRO == lbox_ps->type ) /* Only knob box pro has second row */ CKmsg_Legend_Update ( lline_ps, lbox_ps, k, 1, 8, obuf_p, &osstatus_p, dbg ); } } lknob_ps->firstuse = 1; /* Never been attached */ lknob_ps->attached = 0; /* Not attached */ lknob_ps->ledset = k; /* led set (0-MAX_KNOBS_BOX) */ lknob_ps->ledcpu = lcpuid; /* This knob's cpuid */ lknob_ps->hwrestore = 0; /* intermittent hw restore flag*/ lknob_ps->type = lbox_ps->type; lknob_ps->laststat = CK_OKOK; /* last error sent to User */ lknob_ps->errors = 0; /* count of knob errors */ lknob_ps->seqnum = 0; /* last User sequence number */ lknob_ps->counts = 0; /* running total of turns counts, for PV */ lcpuid++; /* Next cpuid */ } /* For all knobs this box */ } /* For all boxes this line */ /* Initialize device support */ if ( (rval = CKDevSup_Init( ctrlglobal_ps )) ) goto egress; egress: return rval; } /* Get box debug verbosity level * Caller does not lock */ short CKKnob_Box_Debug(int box) { short dbg; epicsMutexLock( varBoxMtx[box] ); dbg = boxdbg[box]; epicsMutexUnlock( varBoxMtx[box] ); return dbg; } /* Set global debug flag, for messages not specific to one box * Set flag to max of individual box flags */ short CKKnob_Gbl_Debug(void) { short gbldbg = 0, boxdbg; int b; for ( b = 0; b < MAX_BOXES_LINE; b++ ) { boxdbg = CKKnob_Box_Debug( b ); if ( boxdbg > gbldbg ) gbldbg = boxdbg; if ( gbldbg >= HIGH ) break; } return gbldbg; } /*============================================================================= Abs: Decode row and index from ledindex (usually passed in message from knob user). Copy new legend to knob data struct. Will be sent on next loop of CKProc_Knob Name: CKKnob_Queue_Legend Args: type Box hardware type knob_ps Pointer to knob structure ledindex Encodes row and index numbers ibuf_p Characters to write to legend dbg Debug message verbosity Rem: If user sent legend for second row but box type does not support it, copy legend into first row. This is ugly and could be problematic, depending on how user allocates legend space--or if user sends second row first Caller must lock knob_ps mutex Ret: None =============================================================================*/ void CKKnob_Queue_Legend(uint32_t type, knob_ts *knob_ps, unsigned char ledindex, char *ibuf_p, short dbg) { unsigned char row; int i, startchar = 0; row = LEGEND_GET_ROW( ledindex ); ledindex = LEGEND_CLEAR_ROW( ledindex ); if ( dbg >= HIGH ) printf("Enter CKKNob_Queue_Legend box type %i row %i index %i legend %s\n", type, row, ledindex, ibuf_p); /* On row 1, only second half of characters are available to user. * User sends ledindex of 0 and we shift 8 chars to right */ if ( row == 1 ) { if ( KNOB_BOX_PRO == type ) ledindex += 8; else { /* A little kludgey: Miranova has only 1 row; we know that * EPICS knobs only use second row for value, so we can assume this * string is a value and shift it to the right */ row = 0; startchar = ledindex + 9; } } for ( i = 0; i < LEGEND_SIZE - ledindex; i++ ) knob_ps->legend[row][i+startchar] = ibuf_p[i]; knob_ps->legendtowrite[row] = knob_ps->legendvalid[row] = 1; knob_ps->legendindex[row] = ledindex; if ( dbg >= HIGH ) printf("Exit CKKNob_Queue_Legend row %i towrite %i index %i valid %i legend %s\n", row, knob_ps->legendtowrite[row], knob_ps->legendindex[row], knob_ps->legendvalid[row], knob_ps->legend[row]); } /*============================================================================= Abs: Process messages from knob users Name: CKProc_Sock Args: ctrlglobal_ps pointer to global data Rem: While there are messages available, process them. We dispatch on the message code which is always the first char received. CKMain_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 CKProc_Sock (void) { ctrlglobal_ts *ctrlglobal_ps = &ctrlglobal_s; void *kuserio_p = ctrlglobal_ps->kuserio_p; void *ctrlio_p = ctrlglobal_ps->ctrlio_p; unsigned char inmsgbuf[MAX_USER_MSG]; /* A buf to receive a user msg */ int msgavail; /* Logical if there is a message to read */ int inmsglen; /* Length of User message */ line_ts *lline_ps; /* Local line pointer */ box_ts *lbox_ps; /* Local box pointer */ knob_ts *lknob_ps = 0; /* Local knob pointer */ int *l, *b, *k; /* Indices into line, box, knob arrays */ l = malloc( sizeof(int) ); b = malloc( sizeof(int) ); k = malloc( sizeof(int) ); /* ** Read and process all msgs waiting for us from the Users */ do { epicsMutexLock( ctrlglobal_ps->mtx ); msgavail = CUmsg_Recv ( ctrlio_p, kuserio_p, inmsgbuf, &inmsglen ); gbldbg = CKKnob_Gbl_Debug(); epicsMutexUnlock( ctrlglobal_ps->mtx ); if (msgavail) { /* ** Dispatch to the appropriate routine based on the message code in ** the first char. */ switch (inmsgbuf[0]) /* Dispatch on message type code */ { case FUATTACH: /* Attach a knob */ if (!SUCCESS(CKMain_Get_Pointers( ctrlglobal_ps, kuserio_p, (cuhead_ts *) inmsgbuf, &lline_ps, &lbox_ps, &lknob_ps, l, b, k ))) break; if ( lknob_ps->firstuse ) /* This is the only place firstuse is read or written, so we don't lock */ lknob_ps->firstuse = 0; else if ( CUmsg_Check_User( lknob_ps, kuserio_p ) ) CUmsg_TUUsurp( ctrlglobal_ps->ctrlio_p, *l, *b, lknob_ps ); CUmsg_FUAttach( ctrlglobal_ps->ctrlio_p, kuserio_p, lline_ps, lbox_ps, lknob_ps, (fulegend_ts *)&inmsgbuf ); break; case FUDETACH: /* Deattach a knob */ if (!SUCCESS(CKMain_Get_Pointers( ctrlglobal_ps, kuserio_p,(cuhead_ts *) inmsgbuf, &lline_ps, &lbox_ps, &lknob_ps, l, b, k ))) break; if ( CUmsg_Check_User( lknob_ps, kuserio_p ) ) break; CUmsg_FUDetach( ctrlglobal_ps->ctrlio_p, lline_ps, lbox_ps, lknob_ps, (fulegend_ts *)&inmsgbuf ); break; case FULEGEND: /* Update a knob legend */ if (!SUCCESS(CKMain_Get_Pointers( ctrlglobal_ps, kuserio_p,(cuhead_ts *) inmsgbuf, &lline_ps, &lbox_ps, &lknob_ps, l, b, k ))) break; if ( CUmsg_Check_User( lknob_ps, kuserio_p ) ) break; CUmsg_Legend_Update( ctrlglobal_ps->ctrlio_p, lline_ps, lbox_ps, lknob_ps, (fulegend_ts *)&inmsgbuf ); break; case FUHEART: /* Check our heart */ CUmsg_TUHeart( ctrlglobal_ps->ctrlio_p, kuserio_p, gbldbg ); break; default: if ( CUmsg_Check_User( lknob_ps, kuserio_p ) ) break; if ( gbldbg >= MED ) printf("CKProc_Sock: default msg case\n"); CUmsg_TUAck( ctrlglobal_ps->ctrlio_p, kuserio_p, (cuhead_ts *) &inmsgbuf, 0, CK_UNKWN_MSG, gbldbg ); break; } /* End of message dispatch */ } /* If message available */ } while ( msgavail ); /* While msgs left to process */ free ( l ); free ( b ); free ( k ); return; } /*============================================================================= Abs: Recover from box outage. Read box info (type, version). If there were legends before outage, set flags to cause legends to be re-written next time around Name: CKKnob_Box_Recover Args: line_ps Pointer to line structure box_ps Pointer to box structure knob_ps Pointer to knob structure dbg Debug message verbosity Rem: Caller must lock box_ps mutex Ret: None =============================================================================*/ static void CKKnob_Box_Recover(line_ts *line_ps, box_ts *box_ps, knob_ts *knob_ps, short dbg) { unsigned osstatus_p; int i, r, b = box_ps->box; unsigned char kstatus; box_ps->checkinfo = 0; if ( dbg >= MED ) printf("CKProc_Knob: box %i just powered up, let's get info\n", b); knob_ps = &box_ps->knobs_as[0]; /* Any knob will work for check info send command; use 0 */ /* Ignore any errors, not all boxes support all these commands */ CKmsg_Rqst_Info( line_ps, box_ps, &osstatus_p, dbg ); CKmsg_Rqst_Reset_Ack ( line_ps, box_ps, knob_ps, &osstatus_p, TKACK_BOOT, dbg ); for ( i = 0; i < MAX_KNOBS_BOX; i++ ) { knob_ps = &box_ps->knobs_as[i]; epicsMutexLock( knob_ps->mtx ); if ( !knob_ps->attached ) { epicsMutexUnlock( knob_ps->mtx ); continue; } /* Turns request does not need to be within knob mutex lock because ledset set only once at startup */ CKmsg_Rqst_Count( line_ps, box_ps, knob_ps->ledset, &osstatus_p, 0, &kstatus, dbg ); /* Clear turns after power up */ /* Store box type in knob struct to avoid * locking box mutex when processing legend updates */ knob_ps->type = box_ps->type; for ( r = 0; r < MAX_ROWS_LEGEND; r++ ) { if ( knob_ps->legendvalid[r] ) knob_ps->legendtowrite[r] = 1; } epicsMutexUnlock( knob_ps->mtx ); } } /*============================================================================= Abs: If there are legends to be written, send message(s) to knob box to update legend(s) Name: CKKnob_Write_Legend Args: line_ps Pointer to line structure box_ps Pointer to box structure knob_ps Pointer to knob structure dbg Debug message verbosity Rem: Caller must lock box_ps and knob_ps mutexes Ret: Status from sending message to box =============================================================================*/ static uint32_t CKKnob_Write_Legend(line_ts *line_ps, box_ts *box_ps, knob_ts *knob_ps, short dbg) { int r, b = box_ps->box, k = knob_ps->ledset; char index; char legend[LEGEND_SIZE]; unsigned osstatus_p; uint32_t iss = 1; /* Initialize to success */ for ( r = 0; r < MAX_ROWS_LEGEND; r++ ) { if ( knob_ps->legendtowrite[r] ) { if ( dbg >= HIGH ) printf("CKKnob_Write_Legend: write %s to box %i knob %i row %i\n", knob_ps->legend[r], b, k, r); index = knob_ps->legendindex[r]; strncpy( legend, knob_ps->legend[r], LEGEND_SIZE - index ); knob_ps->legendtowrite[r] = 0; if ( !SUCCESS(iss = CKmsg_Legend_Update ( line_ps, box_ps, (char)k, r, index, legend, &osstatus_p, dbg )) ) break; } } return iss; } /*============================================================================= Abs: Scan all attached knobs for changes. Send legend updates to knobs. Handle HW restore, box outage, etc. Name: CKProc_Knob Args: arg_p - box number Rem: Each box has its own thread that runs CKProc_Knob forever Ret: None =============================================================================*/ static void CKProc_Knob (void *arg_p) { ctrlglobal_ts *ctrlglobal_ps = &ctrlglobal_s; uint8_t b = *(uint8_t *)arg_p; int k; line_ts *lline_ps = &ctrlglobal_s.line_s; /* Local line pointer */ box_ts *lbox_ps = &lline_ps->boxes_as[b]; /* Local box pointer */ knob_ts *lknob_ps = { 0 }; /* Local knob pointer */ uint32_t iss; unsigned osstatus_p; cuhead_ts cuhead_s; short dbg; while ( 1 ) { epicsMutexLock( lbox_ps->mtx ); if ( lbox_ps->exists && lbox_ps->connected ) { dbg = CKKnob_Box_Debug( b ); /* If box just powered up, read box info and set flags to re-write legend */ if ( lbox_ps->checkinfo ) CKKnob_Box_Recover( lline_ps, lbox_ps, lknob_ps, dbg ); for ( k = 0; k < MAX_KNOBS_BOX; k++ ) { lknob_ps = &lbox_ps->knobs_as[k]; epicsMutexLock( lknob_ps->mtx ); /* Check if new legends to write. Do this before checking if knob attached * because we may need to clear legend after recent detach */ if ( lknob_ps->legendtowrite[0] || lknob_ps->legendtowrite[1] ) { iss = CKKnob_Write_Legend( lline_ps, lbox_ps, lknob_ps, dbg ); /* If we had an error and the last time was OK then tell the user via * a TUACK message. No ack if all OK. */ if (!SUCCESS(iss) && SUCCESS(lknob_ps->laststat) ) { if ( dbg >= MED ) printf("CKmsg_Legend_Update b %i k %i error and last stat ok\n", b, k); cuhead_s.code = FULEGEND; cuhead_s.line = LINE_ID; cuhead_s.box = b; cuhead_s.knob = k; cuhead_s.seqnum = lknob_ps->legendseqnum; CUmsg_TUAck (ctrlglobal_ps->ctrlio_p, lknob_ps->kuserio_p, &cuhead_s, osstatus_p, iss, dbg); lknob_ps->errors++; /* Bump error count */ } lknob_ps->laststat = iss; /* Regardless, save the last status */ } if ( !lknob_ps->attached ) { epicsMutexUnlock( lknob_ps->mtx ); continue; } else if ( lknob_ps->hwrestore ) { if ( dbg >= MED ) printf("CKProc_Knob: Ack hardware restore box %i knob %i\n", b, k); lknob_ps->hwrestore = 0; CKmsg_Rqst_Reset_Ack ( lline_ps, lbox_ps, lknob_ps, &osstatus_p, TKACK_RESTORE, dbg ); } epicsMutexUnlock( lknob_ps->mtx ); if ( dbg >= HIGH ) printf("CKProc_Knob: before calling tuturns box %i knob %i\n", b, k); CUmsg_TUTurns_Status ( ctrlglobal_s.ctrlio_p, lline_ps, lbox_ps, lknob_ps ); epicsThreadSleep(.1); } /* For each knob */ } epicsMutexUnlock( lbox_ps->mtx ); epicsThreadSleep(.001); /* Break to prevent fast-as-possible loop */ } } /*============================================================================= Abs: Return line, box and knob pointers from indexes in message header. Name: CKMain_Get_Pointers Args: ctrlglobal_ps pointer to global data kuserio_p handle of knob user cuhead_ps pointer to message header oline_pps returned pointer line structure obox_pps returned pointer box structure oknob_pps returned pointer to knob structure socknum our socket number outsock_ps socket struct to send to Rem: Get line pointer from global struct. Get box pointer in line & knob point in box. Side: None. Ret: Knob controller->user error code (see knobCtrlDef.h) CK_OKOK If all is well. CK_BADPARAM If bad line, box, or knob number in message. ============================================================================= */ static uint32_t CKMain_Get_Pointers(ctrlglobal_ts *ctrlglobal_ps, void *kuserio_p, cuhead_ts *cuhead_ps, line_ts **oline_pps, box_ts **obox_pps, knob_ts **oknob_pps, int *line, int *box, int *knob) { uint32_t iss = CK_OKOK; /* Assume all will be well */ unsigned dumstatus = 0; /* ** If input parameters are OK return pointers else send error to User and ** return error. */ *line = cuhead_ps->line; *box = cuhead_ps->box; *knob = cuhead_ps->knob; /* line.exists is set at startup in single thread, so no need to lock */ if ( *line != LINE_ID ) { iss = CK_BADPARAM; goto egress; } if ( (*box >= 0) && (*box < MAX_BOXES_LINE) && (*knob >= 0) && (*knob < MAX_KNOBS_BOX) ) { *oline_pps = &(ctrlglobal_ps->line_s); *obox_pps = &((*oline_pps)->boxes_as[(unsigned)*box]); *oknob_pps = &((*obox_pps)->knobs_as[(unsigned)*knob]); } else { iss = CK_BADPARAM; goto egress; } if ( (0 == (ctrlglobal_ps->mask & 1<<*box)) ) { printf("Request for unsupported box %i (box mask is 0x%x. Update st.cmd to include this box.)\n", *box, ctrlglobal_ps->mask); goto egress; } return (iss); egress: iss = CK_BADPARAM; CUmsg_TUAck( ctrlglobal_ps->ctrlio_p, kuserio_p, (cuhead_ts *) &cuhead_ps, dumstatus, iss, CKKnob_Box_Debug( *box ) ); return (iss); } static long drvKnobCtrlReport(int level) { printf("Knob support\n"); return 0; } static long drvKnobCtrlInit(void) { return 0; } static struct { long number; DRVSUPFUN report; DRVSUPFUN init; } drvKnobCtrl={ 2, drvKnobCtrlReport, drvKnobCtrlInit }; epicsExportAddress(drvet,drvKnobCtrl); /* * IOC shell command registration * * Main knob program. Arguments: * * arg0 - Callback struct for comm with knob boxes * arg1 - Callback struct for comm with clients * arg2 - Handle for comm line i/o (eg terminal server node name) * arg3 - Mask of boxes in use: Example for boxes 1 and 5 * mask is binary 100010 = 0x11; 0 for all boxes * */ static const iocshArg arg0 = { "Knob box comm cbs", iocshArgString}; static const iocshArg arg1 = { "Knob user comm cbs", iocshArgString}; static const iocshArg arg2 = { "I/O device 1", iocshArgString}; static const iocshArg arg3 = { "Mask of boxes in use", iocshArgInt}; static const iocshArg *args[] = { &arg0, &arg1, &arg2, &arg3 }; static const iocshFuncDef knobFuncDef = { "knobCtrlInit", 4, args }; static void knobFunc(const iocshArgBuf *args) { void *knobcb; void *kusercb; uint32_t mask; if ( args[0].sval ) { if ( !( knobcb = registryFind( (void *)knobCtrlBoxCommCbRegistryId, args[0].sval) ) ) { printf("knobInit: Knob comm cb '%s' not found!\n", args[0].sval); return; } else printf("Found box communication callbacks %s\n", args[0].sval); } else { printf("knobInit: Arg 0 missing: need knob comm cb!\n"); return; } if ( args[1].sval ) { if ( !( kusercb = registryFind( (void *)knobCtrlUserCommCbRegistryId, args[1].sval) ) ) { printf("knobInit: Client comm cb '%s' not found!\n", args[1].sval); return; } else printf("Found user communication callbacks %s\n", args[1].sval); } else { printf("knobInit: Arg 0 missing: need client comm cb!\n"); return; } if ( !(strcmp(args[2].sval, "0")) ) { printf("FATAL -- need handle for first comm line (typically terminal server)\n"); return; } if ( args[3].ival == 0 ) { printf("knobInit: mask value is zero, assume all boxes supported\n"); mask = 0xFFFF; } else mask = args[3].ival; knobInit( knobcb, kusercb, args[2].sval, mask ); } static void drvKnobCtrlRegistrar(void) { static int firstTime = 1; if ( firstTime ) { iocshRegister(&knobFuncDef, knobFunc); firstTime = 0; } } epicsExportRegistrar(drvKnobCtrlRegistrar);