/*============================================================================= Abs: Top level routines to process Knob Controller <--> Knob User messages. Much of this copied from MCC PC knob software Name: knobCtrlUserComm.c =============================================================================*/ #include /* memcpy */ #include #include #include #include "knobCtrl.h" #include "knobCtrlUserComm.h" #include "knobCtrlBoxComm.h" static void CUmsg_Send (void *ctrlio_p, void *kuserio_p, void *msg_p, int length); static inline uint32_t CUmsg_TUuint32( uint32_t val ) { if ( knobUserCommCallbacks.write_uint32 ) return knobUserCommCallbacks.write_uint32( val ); else return val; } static inline uint16_t CUmsg_TUuint16( uint16_t val ) { if ( knobUserCommCallbacks.write_uint16 ) return knobUserCommCallbacks.write_uint16( val ); else return val; } static inline uint32_t CUmsg_FUUint32( uint32_t val ) { if ( knobUserCommCallbacks.read_uint32 ) return knobUserCommCallbacks.read_uint32( val ); else return val; } static inline uint16_t CUmsg_FUUint16( uint16_t val ) { if ( knobUserCommCallbacks.read_uint16 ) return knobUserCommCallbacks.read_uint16( val ); else return val; } /*=========================================================================== Abs: Initialize for Knob Controller <--> Knob User processing. Name: CUmsg_Init Args: ctrlglobal_ps Global data structure Rem: Caller user callbacks to initialize communication with knob users Caller must lock ctrlglobal_ps->mtx if called outside of single thread at startup. Ret: 0 on success, -1 on error =============================================================================*/ int CUmsg_Init (ctrlglobal_ts *ctrlglobal_ps) { if ( knobUserCommCallbacks.init_ctrlio ) ctrlglobal_ps->ctrlio_p = knobUserCommCallbacks.init_ctrlio(); if ( knobUserCommCallbacks.init_userio ) ctrlglobal_ps->kuserio_p = knobUserCommCallbacks.init_userio(); if ( knobUserCommCallbacks.init_comm ) return knobUserCommCallbacks.init_comm( ctrlglobal_ps->ctrlio_p, ctrlglobal_ps->kuserio_p ); return 0; } void *CUmsg_Init_Knob (void) { if ( knobUserCommCallbacks.init_knob ) return knobUserCommCallbacks.init_knob(); else return 0; } /*=========================================================================== Abs: Check for and read any waiting knob user input msgs. Name: CUmsg_Recv Args: ctrlio_p Knob controller communication handle kuserio_p Knob user communication handle omsg_p Message received omsglen_p Pointer to length of message received Rem: Call user callback to see if there's a message. If data is available, return data and length to caller. Caller does not need to lock Ret: C logical true = a message is available, false = no message =============================================================================*/ int CUmsg_Recv (void *ctrlio_p, void *kuserio_p, unsigned char *omsg_p, int *omsglen_p) { int ostatus = 0; /* Output Status. Assume no msgs */ /* Check if a message awaits. */ epicsMutexLock( ctrlglobal_s.mtx ); if ( knobUserCommCallbacks.read ) { knobUserCommCallbacks.read( ctrlio_p, kuserio_p, omsg_p, omsglen_p ); } epicsMutexUnlock( ctrlglobal_s.mtx ); if (*omsglen_p > 0) ostatus = 1; /* Tell caller we have a msg */ return ostatus; } /*=========================================================================== Abs: Check to see if user that sent recent request matches the user that currently 'owns' this knob Name: CUmsg_Check_User Args: knob_ps Data structure for knob, contains stored info for knob owner kuserio_p Info of user that sent last request Rem: Call user callback to compare kuserio_p with knob_ps->kuserio_p Caller does not need to lock Ret: -1 if users do not match, 0 if they do match =============================================================================*/ int CUmsg_Check_User (knob_ts *knob_ps, void *kuserio_p) { int rval = 0; if ( knobUserCommCallbacks.knob_check_owner ) { epicsMutexLock( knob_ps->mtx ); rval = knobUserCommCallbacks.knob_check_owner( knob_ps->kuserio_p, kuserio_p ); epicsMutexUnlock( knob_ps->mtx ); } return rval; } /*=========================================================================== Abs: Attach the requested knob. Name: CUmsg_FUAttach Args: ctrlio_p Communication handle for knob users kuserio_p Info for user requesting the attach line_ps Data structure for line box_ps Data structure for box knob_ps Data structure for knob fvattach_ps Attach message from user Rem: Set knob attached, save user sequence number and copy senders socket struct to the knob struct for our reply. Clear the knobs counter, update the legend and send ack msg back to user. Caller does not need to lock Ret: None =============================================================================*/ void CUmsg_FUAttach (void *ctrlio_p, void *kuserio_p, line_ts *line_ps, box_ts *box_ps, knob_ts *knob_ps, fulegend_ts *fuattach_ps) { uint32_t iss; /* Ye olde VAX status */ unsigned lsstatus; /* Local serial status */ uint8_t kstatus; /* Knob status byte from turns read */ int r; short dbg = CKKnob_Box_Debug( box_ps->box ); if ( dbg >= MED ) printf("CUmsg_FUAttach box %i knob %i %s ledcpu %i firstcpu %i\n", box_ps->box, knob_ps->ledset, fuattach_ps->legend, knob_ps->ledcpu, box_ps->firstcpu); /* ** Clear the counter in case anyone fiddled with the knob while it ** wasn't officially attached, update the legend and send the ack ** to the VAX with the appropriate status. (We do not need the turns ** count, so pass 0 as 5th argument to Rqst_Count.) */ epicsMutexLock( box_ps->mtx ); box_ps->exists = 1; /* This box exists */ iss = CKmsg_Rqst_Count( line_ps, box_ps, knob_ps->ledset, &lsstatus, 0, &kstatus, dbg ); epicsMutexUnlock( box_ps->mtx ); /* ** Save i/o info of sender, last status and sequence number. If all went well, ** set knob attached; else, bump error count. */ epicsMutexLock( knob_ps->mtx ); knobUserCommCallbacks.knob_assoc( knob_ps->kuserio_p, kuserio_p ); CUmsg_TUAck ( ctrlio_p, knob_ps->kuserio_p, &fuattach_ps->cuhead_s, lsstatus, iss, dbg ); knob_ps->seqnum = CUmsg_FUUint32( fuattach_ps->cuhead_s.seqnum ); /* Latest sequence number */ knob_ps->laststat = iss; /* Save last status */ if (SUCCESS(iss)) { /* Reset valid legend flag, then queue new legend */ for ( r = 0; r < MAX_ROWS_LEGEND; r++ ) knob_ps->legendvalid[r] = 0; /* ** On attach, we manually clear row 1 in case user does not write to it ** (don't want leftover text from previous attach) */ CKKnob_Queue_Legend( knob_ps->type, knob_ps, LEGEND_SET_ROW(0,1), " ", dbg ); CKKnob_Queue_Legend( knob_ps->type, knob_ps, fuattach_ps->offset, fuattach_ps->legend, dbg ); knob_ps->attached = 1; /* Set knob attached so we'll poll it */ } else { knob_ps->errors++; /* Bump error count if problem */ knob_ps->attached = 0; /* Set knob attached so we'll poll it */ } /* Reset running turns count total to 0 */ knob_ps->counts = 0; epicsMutexUnlock( knob_ps->mtx ); return; } /*=========================================================================== Abs: Detach the requested knob. Name: CUmsg_FUDetach Args: ctrlio_p Communication handle for knob users line_ps Data structure for line box_ps Data structure for box knob_ps Data structure for knob fvdetach_ps Detach message from user Rem: Set the knob detached, save the last sequence number and the socket struct of the sender. Update the legend and send the TUACK with VAX and serial status. Save the last VMS status and if there was a problem, bump the error count for this knob. Caller does not need to lock Ret: None =============================================================================*/ void CUmsg_FUDetach (void *ctrlio_p, line_ts *line_ps, box_ts *box_ps, knob_ts *knob_ps, fulegend_ts *fudetach_ps) { uint32_t iss = 1; /* Send user success */ unsigned lsstatus = 1; /* Local knob comm status, initialize to success */ short dbg = CKKnob_Box_Debug( box_ps->box ); if ( dbg >= MED ) printf("CUmsg_FUDetach box %i knob %i\n", box_ps->box, knob_ps->ledset); /* ** Queue legend for later write ** Send ack message to user. Status always good because we haven't sent a message to box ** Set knob detached regardless of error since that's what the user wanted. */ epicsMutexLock( knob_ps->mtx ); CUmsg_TUAck ( ctrlio_p, knob_ps->kuserio_p, &fudetach_ps->cuhead_s, lsstatus, iss, dbg ); knob_ps->seqnum = CUmsg_FUUint32( fudetach_ps->cuhead_s.seqnum ); // Latest sequence number knob_ps->laststat = iss; // Save the last status /* ** Bump error count if problems. */ if (!SUCCESS(iss)) knob_ps->errors++; CKKnob_Queue_Legend( knob_ps->type, knob_ps, fudetach_ps->offset, fudetach_ps->legend, dbg ); knob_ps->attached = 0; // Set knob detached epicsMutexUnlock( knob_ps->mtx ); return; } /*=========================================================================== Abs: Save the latest legend request. Is then written to knob by box's processing thread Name: CUmsg_Legend_Update Args: ctrlio_p Communication handle for knob users line_ps Data structure for line box_ps Data structure for box knob_ps Data structure for knob fulegend_ps Legend message from user Rem: Since IP messages can arrive out of order, check the sequence number. If <= last one just return. Else, save the new number and write the new legend data to the knob structure, to be written to knob later. Caller does not need to lock Ret: None =============================================================================*/ void CUmsg_Legend_Update (void *ctrlio_p, line_ts *line_ps, box_ts *box_ps, knob_ts *knob_ps, fulegend_ts *fulegend_ps) { uint32_t nseqnum = CUmsg_FUUint32( fulegend_ps->cuhead_s.seqnum ); /* New sequence number from knob user */ short dbg = CKKnob_Box_Debug( box_ps->box ); if ( dbg >= HIGH ) printf("CUmsg_Legend_Update box %i knob %i %s\n", box_ps->box, knob_ps->ledset, fulegend_ps->legend); epicsMutexLock( knob_ps->mtx ); /* ** If this sequence # is <= the last one then skip the update. */ if ( nseqnum <= knob_ps->seqnum ) { if ( dbg >= HIGH ) printf("CUMsg_Legend_Update: seq # %u <= previous %u\n", nseqnum, (unsigned)knob_ps->seqnum); goto egress; } /* ** Save new sequence #, update the legend. */ knob_ps->seqnum = nseqnum; /* Latest sequence number */ /* TODO: Figure out if we need both seqnums; do not lock box mutex as box member set once at startup */ CKKnob_Queue_Legend( knob_ps->type, knob_ps, fulegend_ps->offset, fulegend_ps->legend, dbg ); knob_ps->legendseqnum = nseqnum; egress: epicsMutexUnlock( knob_ps->mtx ); return; } /*=========================================================================== Abs: Send acknowledge to knob user Name: CUmsg_TUAck Args: ctrlio_p Communication handle for knob users kuserio_p Communication handle for this specific user cuhead_ps Pointer to user msg header sstatus Knob comm status iss User status Rem: Build the tuack message from the header and user status and send to the user in kuserio_p If kuserio_p argument is a knob's kuserio_p, then caller must lock knob_ps.mtx. If kuserio_p is just copied from a new incoming message, then caller does not need to lock Ret: None =============================================================================*/ void CUmsg_TUAck (void *ctrlio_p, void *kuserio_p, cuhead_ts *cuhead_ps, unsigned sstatus, uint32_t iss, short dbg ) { tuack_ts tuack_s; /* The ack msg */ /* ** Copy header & status & send to knob user */ memcpy (&tuack_s.cuhead_s, cuhead_ps, sizeof(cuhead_ts)); tuack_s.acode = tuack_s.cuhead_s.code; /* Return function code */ tuack_s.cuhead_s.code = TUACK; /* The ack function */ tuack_s.userstat = CUmsg_TUuint32( iss ); tuack_s.sstatus = CUmsg_TUuint16( sstatus ); if ( dbg >= HIGH ) printf("CUmsg_TUAck: ack msg to user 0x%04x sstatus %i\n", (int)iss, sstatus); CUmsg_Send ( ctrlio_p, kuserio_p, &tuack_s, sizeof(tuack_ts) ); return; } /*=========================================================================== Abs: Get latest turns from knob and if non-zero, send to knob user. Depending on knob's return status byte, take other action Name: CUmsg_TUTurns_Status Args: ctrlio_p Communication handle for knob users line_ps Pointer to data structure for line box_ps Pointer to data structure for box knob_ps Pointer to data structure for knob Rem: Send a count request msg to the knob and get its reply. Format and send the TUTURNS message if condition is met (see comments below). Save the last status and bump error count if there was an error communicating with the knob. Caller does not need to lock Ret: None =============================================================================*/ void CUmsg_TUTurns_Status (void *ctrlio_p, line_ts *line_ps, box_ts *box_ps, knob_ts *knob_ps) { uint32_t iss; /* Ye olde VAX status */ int16_t lcount = 0; /* Knob counter value */ unsigned lsstatus; /* Knob communication status */ tuturns_ts tuturns_s; /* The turns msg */ uint32_t type; /* Local box type */ unsigned char kstatus; /* Status byte returned with turns count */ int b = box_ps->box; int k = knob_ps->ledset; short dbg = CKKnob_Box_Debug( box_ps->box ); /* ** Get count from knob. */ epicsMutexLock( box_ps->mtx ); type = box_ps->type; iss = CKmsg_Rqst_Count (line_ps, box_ps, k, &lsstatus, &lcount, &kstatus, dbg); if ( dbg >= HIGH ) printf("CUmsg_TUTurns_Status: Box %i knob %i Rqst_Count return status %i kstatus %i SUCCESS %i\n", b, k, iss, kstatus, SUCCESS( iss ) ); /* Determine if box just booted up */ if ( SUCCESS( iss ) && (type == KNOB_BOX_PRO) && (kstatus & FK_BOOT) ) { box_ps->checkinfo = 1; if ( dbg > MED ) printf("CUmsg_Turns_Status: Box %i knob %i box just powered up, kstatus %i FK_BOOT 0x%02x\n", b, k, kstatus, FK_BOOT); } epicsMutexUnlock( box_ps->mtx ); /* ** We send a message to the knob user under 3 conditions: ** ** 1) If the status is good and the count is non-zero, send a turns msg ** 2) If the status is bad but it was good last time, send a turns msg ** 3) If the status is good, it is a knob box pro, and the knob status byte indicates a hardware restore, send a restore msg */ epicsMutexLock( knob_ps->mtx ); if ( ((lcount != 0) && SUCCESS(iss)) || (!SUCCESS(iss) && SUCCESS(knob_ps->laststat)) ) { tuturns_s.cuhead_s.code = TUTURNS; tuturns_s.turns = (int16_t)CUmsg_TUuint16( (uint16_t)lcount ); if ( dbg >= MED ) printf("CUmsg_TUTurns_Status: Box %i knob %i before sending %i lcount %i turns to user success %i\n", b, k, lcount, tuturns_s.turns, SUCCESS( iss )); /* Update running total; set to 0 if rolled over */ knob_ps->counts = ( abs(knob_ps->counts - lcount) > (long)(pow((double)2,(double)(8*sizeof(knob_ps->counts)))/2) ) ? 0 : knob_ps->counts - lcount; if ( dbg >= HIGH ) printf("CUmsg_TUTurns_Status: Box %i knob %i new counts total %i delta %i\n", b, k, knob_ps->counts, lcount); } else if ( (kstatus & FK_RESTORE) && (type == KNOB_BOX_PRO) && SUCCESS(iss) ) { tuturns_s.cuhead_s.code = TURESTORE; if ( dbg >= MED ) printf("CUmsg_TUTurns_Status: Box %i knob %i before sending restore msg to user kstatus 0x%02x\n", b, k, kstatus); knob_ps->hwrestore = 1; } else goto egress; /* ** Fill in all the fields and send the message. */ tuturns_s.cuhead_s.line = line_ps->line; tuturns_s.cuhead_s.box = box_ps->box; tuturns_s.cuhead_s.knob = k; tuturns_s.cuhead_s.seqnum = CUmsg_TUuint32( knob_ps->seqnum ); tuturns_s.sstatus = CUmsg_TUuint16( lsstatus ); tuturns_s.userstat = CUmsg_TUuint32( iss ); CUmsg_Send ( ctrlio_p, knob_ps->kuserio_p, &tuturns_s, sizeof(tuturns_ts) ); egress: knob_ps->laststat = iss; /* Save the last status */ if ( (kstatus != knob_ps->lkstatus) ) { knob_ps->lkstatus = kstatus; if ( varKnobScan[b][k] ) scanIoRequest( varKnobScan[b][k] ); } epicsMutexUnlock( knob_ps->mtx ); return; } /*=========================================================================== Abs: Send usurp message to user: other user has taken control of this knob Name: CUmsg_TUUsurp Args: ctrlio_p Communication handle for knob users line Line number box Box number knob_ps Knob data strucutre Rem: Build message to inform user that other user has taken control of knob. Send message to user. Caller does not need to lock Ret: None =============================================================================*/ void CUmsg_TUUsurp (void *ctrlio_p, int line, int box, knob_ts *knob_ps) { tuturns_ts tuturns_s; /* The usurp msg */ short dbg = CKKnob_Box_Debug( box ); /* ** Fill in all the fields and send the message. */ tuturns_s.cuhead_s.code = TUUSURP; tuturns_s.cuhead_s.line = line; tuturns_s.cuhead_s.box = box; tuturns_s.cuhead_s.knob = knob_ps->ledset; tuturns_s.sstatus = CUmsg_TUuint16( 1 ); /* Set status to OK */ tuturns_s.userstat = CUmsg_TUuint32( CK_OKOK ); if ( dbg >= MED ) printf("CUmsg_TUTurns_Status: box %i knob %i before sending usurped msg to user\n", box, knob_ps->ledset); epicsMutexLock( knob_ps->mtx ); tuturns_s.cuhead_s.seqnum = CUmsg_TUuint32( knob_ps->seqnum ); CUmsg_Send ( ctrlio_p, knob_ps->kuserio_p, &tuturns_s, sizeof(tuturns_ts) ); epicsMutexUnlock( knob_ps->mtx ); return; } /*=========================================================================== Abs: Send hearbeat ack message to knob user Name: CUmsg_TUHeart Args: ctrlio_p Communication handle for knob users kuserio_p Communication handle for this specific user Rem: Fill in the message code and a good status and send the heartbeat to the user in kuserio_p Caller does not need to lock Ret: None =============================================================================*/ void CUmsg_TUHeart (void *ctrlio_p, void *kuserio_p, short dbg) { tuheart_ts tuheart_s; /* The heartbeat ack msg */ /* ** Fill in all the fields and send the message. */ tuheart_s.code = TUHEART; tuheart_s.userstat = CUmsg_TUuint32( CK_OKOK ); /* ** Send the message to the VAX */ CUmsg_Send ( ctrlio_p, kuserio_p, &tuheart_s, sizeof(tuheart_ts) ); return; } /*=========================================================================== Abs: Internal routine to send a message to the Knob User Name: CUmsg_Send Args: ctrlio_p Communication handle for knob users kuserio_p Communication handle for this specific user omsg_p Outgoing message length Length of outgoing message Rem: Send message to knob user If kuserio_p argument is a knob's kuserio_p, then caller must lock knob_ps.mtx. If kuserio_p is just copied from a new incoming message, then caller does not need to lock Ret: Void. =============================================================================*/ static void CUmsg_Send (void *ctrlio_p, void *kuserio_p, void *omsg_p, int length) { epicsMutexLock( ctrlglobal_s.mtx ); knobUserCommCallbacks.write( ctrlio_p, kuserio_p, omsg_p, length ); epicsMutexUnlock( ctrlglobal_s.mtx ); }