/* **MEMBER**=NONE **CMS**=CKOBS ============================================================================== Abs: Top level routines to process PC <--> Knob box messages. Name: drvKnobCtrlBox.c Module. No routine by this name. CKmsg_Init_Line CKmsg_Clear_Count CKmsg_Rqst_Count CKmsg_Reply_Count CKmsg_Legend_Update CKmsg_Rqst_Reset static CKmsg_Send static CKmsg_Recv static CKmsg_Checksum Proto: drvKnobCtrlBox.h 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. 16-Jan-2014, Changes to communicate with knob boxes over ethernet instead of RS485. Incorporated turns Rply_Cnt into Rqst_Cnt in order to verify reply is for correct CPU before passing turns onto VAX. 27-Sep-1996, Robert C. Sass (RCS): Call CKUtil_Errline before error message printouts. ============================================================================== */ #include /* strncpy */ #include /* abs */ #include /* printf & friends */ #include /* NULL */ #include /* time, ctime */ #include /* abs */ #include #include #include #include "knobCtrl.h" #include "knobCtrlDef.h" #include "knobCtrlUserCommDef.h" #include "knobCtrlBoxCommDef.h" /* ** Limit of turn counts we'll accept as valid. */ #define TURNLIMIT 175 /* ** Local prototypes. Not referenced outside of this module. */ static uint32_t CKmsg_Send (line_ts *line_ps, box_ts *box_ps, unsigned char *bufo_p, int count, unsigned *osstatus_p, short dbg); static uint32_t CKmsg_Recv (box_ts *box_ps, unsigned char *inmsgbuf, int elen, unsigned *osstatus_p, unsigned char *recvknobcpu, short dbg); static unsigned char CKmsg_Checksum (unsigned char *buf_p, int count); /*============================================================================= Abs: Initialize a line connection Name: CKmsg_Init_Box Args: line_ps - line data structure lineio - line communication handle Rem: Caller must lock line_ps->mtx (if not called from single thread at startup) Ret: 0 on success, -1 on failure =============================================================================*/ int CKmsg_Init_Line (line_ts *line_ps, void *lineio) { if ( lineio ) { if ( knobCommCallbacks.knob_init_line ) { if ( knobCommCallbacks.knob_init_line (line_ps, lineio) ) return -1; } return 0; } else return -1; } /*============================================================================= Abs: Initialize a knob box connection Name: CKmsg_Init_Box Args: line_ps - line data structure box_ps - knob box data structure Rem: Caller must lock box_ps->mtx (if not called from single thread at startup) Ret: 0 on success, -1 on failure =============================================================================*/ int CKmsg_Init_Box (line_ts *line_ps, box_ts *box_ps) { int rval = 0; /* Return status */ /* ** Assume we won't connect to box */ box_ps->connected = 0; if ( knobCommCallbacks.knob_init_box ) rval = knobCommCallbacks.knob_init_box( line_ps, box_ps ); return rval; } /*============================================================================= Abs: Check if message out of order; if so, reset Name: CKmsg_Out_Of_Order Args: knobcpu Destination knob cpu recknobcpu Reply knob cpu iss Pointer to user status word box_ps Pointer to box struct fkreply_count_ts Message char array osstatus_p Pointer to communication status. 1 if success, else 0 dbg Debug message verbosity Rem: Check if receive out of order message (detected by comparing expected and actual reply CPU), read and discard remaining incoming messages to reset Caller must lock box_ps->mtx Side: None Ret: -1 if received message out of order, else 0 ============================================================================= */ static int CKmsg_Out_Of_Order(unsigned char knobcpu, unsigned char recvknobcpu, uint32_t *iss, box_ts *box_ps, fkreply_count_ts *fkreply_count_s, unsigned *osstatus_p, short dbg) { int i = 0; if ( knobcpu != recvknobcpu ) { printf("Box %i, Turns rqst reply out of order, expected cpu 0x%02x but received 0x%02x\n", box_ps->box, knobcpu, recvknobcpu); *iss = CK_BADPARAM; /* To break out of condition of out-of-order messages */ while( (*iss != CK_READ_TIMEOUT) && (i++ < MAX_KNOBS_BOX) ) { *iss = CKmsg_Recv ( box_ps, (unsigned char *) fkreply_count_s, sizeof(*fkreply_count_s), osstatus_p, &recvknobcpu, dbg ); if ( dbg >= MED ) printf("Box %i knobcpu %i Received knob CPU 0x%02x iss %p\n", box_ps->box, knobcpu, recvknobcpu, iss); } return -1; } else return 0; } /*============================================================================= Abs: Request counter on specified knob and optionally get reply Name: CKmsg_Rqst_Count Args: line_ps Pointer to line struct box_ps Pointer to box struct knob Knob number (0-3) osstatus_p Pointer to communication status. 1 if success, else 0 ocount_p Pointer to knob turns count. If 0, do not return turns count to caller kstatus Status byte from knob box dbg Debug message verbosity Rem: Build knobs message to request a readout of the counter value for a knob, then read the count reply message. The count in the message is 2 unsigned char so insert the 2 bytes into caller's int16_t. If ocount_p is NULL, still read reply to clear input queue, but then return. If receive out of order message (detected by comparing expected and actual reply CPU), read and discard remaining incoming messages to reset Caller must lock box_ps->mtx Side: None Ret: Status from CKmsg_Send. ============================================================================= */ uint32_t CKmsg_Rqst_Count (line_ts *line_ps, box_ts *box_ps, int knob, unsigned *osstatus_p, int16_t *ocount_p, unsigned char *kstatus, short dbg) { tkrqst_ts tkrqst_count_s = {{CTRL_B, 0, TKRQST_COUNT}, {0, CR}}; fkreply_count_ts fkreply_count_s = {{CTRL_B, 0, FKREPLY_COUNT}, 0, 0, 0, {0, CR}}; unsigned char knobcpu = box_ps->knobs_as[knob].ledcpu; /* ledcpu set at startup in single thread, so no need to lock */ unsigned char recvknobcpu; uint32_t iss; /* ** Get count from knob. */ tkrqst_count_s.ckhead.knobcpu = knobcpu; /* Insert knobcpu */ if ( dbg >= HIGH ) printf("CKmsg_Rqst_Count: before send msg to box %i knob %i\n", box_ps->box, knob); iss = CKmsg_Send (line_ps, box_ps, (unsigned char *) &tkrqst_count_s, sizeof(tkrqst_count_s), osstatus_p, dbg); if ( !SUCCESS(iss) ) goto egress; if (SUCCESS(iss = CKmsg_Recv ( box_ps, (unsigned char *) &fkreply_count_s, sizeof(fkreply_count_s), osstatus_p, &recvknobcpu, dbg ))) { if ( CKmsg_Out_Of_Order( knobcpu, recvknobcpu, &iss, box_ps, &fkreply_count_s, osstatus_p, dbg ) ) goto egress; if ( kstatus ) *kstatus = fkreply_count_s.status; if ( !ocount_p ) goto egress; *ocount_p = fkreply_count_s.low | (fkreply_count_s.hi << 8); if ( dbg >= HIGH ) printf("CKmsg_Rqst_Count box %i knob %i turns count %i kstatus 0x%02x\n", box_ps->box, knob, *ocount_p, *kstatus); if (abs (*ocount_p) > TURNLIMIT) { printf ("Box %i knob %i, Turn count %d too large box %i knob %i\n", box_ps->box, knob, *ocount_p, box_ps->box, knob); *ocount_p = 0; } } else { iss = CK_READ_TIMEOUT; } egress: return iss; } /*============================================================================= Abs: Request box info Name: CKmsg_Rqst_Info Args: line_ps Pointer to line struct box_ps Pointer to box struct knob Knob number (0-3) osstatus_p Pointer to communication status. 1 if success, else 0 dbg Debug message verbosity Rem: Build knobs message to request box info. If successfully receive reply, copy message data into box struct. Else clear box struct info. Side: None Ret: Status from CKmsg_Send. ============================================================================= */ uint32_t CKmsg_Rqst_Info (line_ts *line_ps, box_ts *box_ps, unsigned *osstatus_p, short dbg) { tkrqst_ts tkrqst_info_s = {{CTRL_B, 0, TKRQST_INFO}, {0, CR}}; fkreply_info_ts fkreply_info_s = {{CTRL_B, 0, FKREPLY_INFO}, 0, 0, 0, 0, {0, CR}}; unsigned char knobcpu = box_ps->firstcpu; /* ledcpu set at startup in single thread, so no need to lock */ unsigned char recvknobcpu; uint32_t iss; uint32_t ltype = box_ps->type; tkrqst_info_s.ckhead.knobcpu = knobcpu; /* Insert knobcpu */ if ( dbg >= MED ) printf("CKmsg_Rqst_Info: before send msg to box %i knobcpu %i\n", box_ps->box, knobcpu); iss = CKmsg_Send (line_ps, box_ps, (unsigned char *) &tkrqst_info_s, sizeof(tkrqst_info_s), osstatus_p, dbg); if ( !SUCCESS(iss) ) goto egress; if (SUCCESS( CKmsg_Recv ( box_ps, (unsigned char *) &fkreply_info_s, sizeof(fkreply_info_s), osstatus_p, &recvknobcpu, dbg ))) { /* fkreply_info_ts same size as fk_reply_count_ts, so just cast struct */ if ( CKmsg_Out_Of_Order( knobcpu, recvknobcpu, &iss, box_ps, (fkreply_count_ts *)&fkreply_info_s, osstatus_p, dbg ) ) goto egress; box_ps->type = (uint32_t)fkreply_info_s.type; box_ps->versmajor = fkreply_info_s.versmajor; box_ps->versminor = fkreply_info_s.versminor; box_ps->verspatch = fkreply_info_s.verspatch; if ( dbg >= MED ) printf("CKmsg_Rqst_Info type %i vers %i.%i.%i\n", box_ps->type, box_ps->versmajor, box_ps->versminor, box_ps->verspatch); } else { if ( dbg >= MED ) printf("CKmsg_Rqst_Info: no reply or out of order, can't determine box info\n"); /* No response, clear box info * Not all boxes support this message, * so no reply is not an error */ box_ps->type = UNKNOWN_BOX; box_ps->versmajor = 0; box_ps->versminor = 0; box_ps->verspatch = 0; } if ( ltype != box_ps->type ) { if ( varBoxScan[box_ps->box] ) scanIoRequest( varBoxScan[box_ps->box] ); } egress: return iss; } /*============================================================================= Abs: Update legend on specified knob. Name: CKmsg_Legend_Update Args: line_ps Pointer to line struct box_ps Pointer to box struct ledset ledset # (0-3) on box ledindex index (0-15) of starting LED to update ibuf_p Buffer of chars to write to legend osstatus_p Pointer to output final comm status dbg Debug message verbosity Rem: The legend update (led display) message requires the cpuid of the first knob on the box in the cpu part of the message. The ledset to update is given by the ledset # in the message body. The ledindex is the character number at which the update starts. The characters are numbered 0-15 starting from the left. Thus the ledindex determines the starting character and the # of characters in the message. After filling in the cpu, set and index in the message, calculate the length and copy the characters to the message. Insert the final CR and send the resulting message. Side: None. Ret: Status from CKmsg_Send =============================================================================*/ uint32_t CKmsg_Legend_Update (line_ts *line_ps, box_ts *box_ps, char ledset, char row, char ledindex, char *ibuf_p, unsigned *osstatus_p, short dbg) { tklegend_update_ts tklegend_update_s = {{CTRL_B, 0, TKLEGEND_UPDATE}, 0}; tklegend_update_miranova_ts tklegend_update_miranova_s = {{CTRL_B, 0, TKLEGEND_UPDATE}, 0}; uint32_t iss; int numchars; /* # LED chars to update */ int msgsize; /* Actual msg size sent */ int i; /* ** Due to oversight, different messages for Miranova and Knob Box Pro ** Below is the hasty workaround */ if ( box_ps->type == KNOB_BOX_PRO ) { tklegend_update_s.ckhead.knobcpu = 4*box_ps->box; /* For update use firstcpu */ tklegend_update_s.ledset = ledset; /* Which set of LEDs */ tklegend_update_s.ledrow = row; /* Which set of LEDs */ tklegend_update_s.ledindex = ledindex; /* Index of starting LED */ /* ** Each LED in a 16 character legend is numbered 0 based from the left-most ** i.e. 0-15. All characters to the right of the indexed LED are updated thus the ** ledindex determines both the starting char and # of chars to send. */ numchars = LEGEND_SIZE - ledindex; strncpy ( (void *) &tklegend_update_s.legend, (void *) ibuf_p, numchars); /* Chars to msg */ /* ** The tklegend_update_s message is variable length depending on the ** # of characters sent. Insert the trailing CR based on the size and ** send just the number required for the update. */ tklegend_update_s.legend[numchars - 1 + sizeof(cktail_ts)] = CR; msgsize = sizeof(tklegend_update_s) - ledindex; iss = CKmsg_Send (line_ps, box_ps, (unsigned char *)&tklegend_update_s, msgsize, osstatus_p, dbg); } else { tklegend_update_miranova_s.ckhead.knobcpu = 4*box_ps->box; /* For update use firstcpu */ tklegend_update_miranova_s.ledset = ledset; /* Which set of LEDs */ tklegend_update_miranova_s.ledindex = ledindex; /* Index of starting LED */ numchars = LEGEND_SIZE - ledindex; for ( i = 0; i < numchars; i++ ) ibuf_p[i] = toupper( ibuf_p[i] ); /* Miranova supports only upper case characters */ strncpy ( (void *) &tklegend_update_miranova_s.legend, (void *) ibuf_p, numchars); /* Chars to msg */ tklegend_update_miranova_s.legend[numchars - 1 + sizeof(cktail_ts)] = CR; msgsize = sizeof(tklegend_update_miranova_s) - ledindex; iss = CKmsg_Send (line_ps, box_ps, (unsigned char *)&tklegend_update_miranova_s, msgsize, osstatus_p, dbg); } if ( dbg >= HIGH ) printf("CKmsg_Legend_Update: box %i before update ledset %i row %i ledindex %i to %s\n", box_ps->box, ledset, row, ledindex, ibuf_p); box_ps->knobs_as[(int)ledset].legendtowrite[(int)row] = 0; epicsThreadSleep( LEGEND_WAIT ); return iss; } /*============================================================================= Abs: Reset a specified knob. Name: CKmsg_Rqst_Reset Args: line_ps Pointer to line struct box_ps Pointer to box struct knob_ps Pointer to knob struct osstatus_p Pointer to communication status. 1 if success, else 0 dbg Debug message verbosity Rem: Build and send reset message to the specified knob. This is usually done when a knob communication error is encountered. Caller must lock box_ps->mtx Ret: status from CKmsg_Send =============================================================================*/ uint32_t CKmsg_Rqst_Reset_Ack (line_ts *line_ps, box_ts *box_ps, knob_ts *knob_ps, unsigned *osstatus_p, unsigned char code, short dbg) { tkrqst_ts tkrqst_reset_s = {{CTRL_B, 0, code}, {0, CR}}; uint32_t iss; tkrqst_reset_s.ckhead.knobcpu = knob_ps->ledcpu; /* ledcpu set at init in single thread, so no need to lock */ iss = CKmsg_Send ( line_ps, box_ps, (unsigned char *) &tkrqst_reset_s, sizeof(tkrqst_reset_s), osstatus_p, dbg ); return iss; } /*============================================================================= Abs: Send a message to a knob. Name: CKmsg_Send Args: line_ps Pointer to line struct box_ps Pointer to box struct bufo_p Message to send to knob count # chars in message osstatus_p Pointer to output final comm status dbg Debug message verbosity Rem: Send the specified message after filling in the checksum. If we're not connected to box, try to connect first. If we succeed, clear knob turn and proceed with message. If we fail, return error. Caller must lock box_ps->mtx Ret: CK_OKOK - All went well CK_WCHAR_TIMEOUT - Exceded MAX_SERIAL_RETRIES trying to send the message CK_RECVON_TIMEOUT - Exceded MAX_SERIAL_RETRIES waiting for the receiver to go ready after message sent. =============================================================================*/ static uint32_t CKmsg_Send (line_ts *line_ps, box_ts *box_ps, unsigned char *bufo_p, int count, unsigned *osstatus_p, short dbg) { uint32_t iss = CK_OKOK; int i; /* Number of bytes sent */ /* If we have not yet successfully connected to the box, try again */ if (!box_ps->connected) { CKmsg_Init_Box (line_ps, box_ps); if (!box_ps->connected) { iss = CK_READ_TIMEOUT; goto egress; } /* Clear the knob counters in case anyone fiddled with them while we were not connected */ for ( i = 0; i < MAX_KNOBS_BOX; i++ ) { iss = CKmsg_Rqst_Count (line_ps, box_ps, i, osstatus_p, 0, 0, dbg); if ( !SUCCESS(iss) ) goto egress; } } bufo_p[1] |= 0x80; /* CPU address [1] is biased by 0x80 */ bufo_p[count-2] = CKmsg_Checksum(bufo_p, count-2); /* Fill in checksum */ if ( dbg >= HIGH ) { printf("CKmsg_Send: box %i msg to knobs, count %i:\n", box_ps->box, count); for ( i = 0; i < count; i++ ) printf("0x%02x ", bufo_p[i]); printf("\n"); } if ( knobCommCallbacks.knob_write ) knobCommCallbacks.knob_write( box_ps, bufo_p, count, &iss, dbg ); egress: /* Return last known status */ *osstatus_p = 1; /* 1 for successful communication */ return iss; } /*============================================================================= Abs: Receive a message. Name: CKmsg_Recv Args: box_ps Pointer to box struct inmsgbuf Input message received elen Expected # chars in message osstatus_p Pointer to output final comm status recvknobcpu Pointer to reply knob CPU char Rem: Read incoming message. If actual length does not match expected length, return with error. Else calculate and verify checksum of incoming message and copy incoming CPU id to recvknobcpu Caller must lock box_ps->mtx Side: None. Ret: CK_OKOK - Message received OK CK_BADPARAM - Failed to set socket timeout option CK_READ_TIMEOUT - No reply from box CK_CKSUM - Checksum error in received message. =============================================================================*/ static uint32_t CKmsg_Recv (box_ts *box_ps, unsigned char *inmsgbuf, int elen, unsigned *osstatus_p, unsigned char *recvknobcpu, short dbg) { int inmsglen; /* Length of received message */ uint32_t iss = CK_OKOK; /* Start with good status */ int status = 0, i; if ( knobCommCallbacks.knob_read ) knobCommCallbacks.knob_read( box_ps, inmsgbuf, elen, &inmsglen, recvknobcpu, &iss ); if ( dbg >= HIGH ) { printf("CKmsg_Recv: box %i inmsglen %i msg from box:\n", box_ps->box, inmsglen); for (i = 0; i < inmsglen; i++) printf("0x%02x ",inmsgbuf[i]); printf("\n"); } if (inmsglen == elen) /* watch out for this; was not checking exact length before */ status = 1; /* Tell caller we have a msg */ else { iss = CK_READ_TIMEOUT; goto egress; } if ( recvknobcpu ) *recvknobcpu = inmsgbuf[1] & ~0x80; /* If we get here we seem to have read the desired number of * characters so check the checksum. */ if (CKmsg_Checksum (inmsgbuf, elen-2) != inmsgbuf[elen-2]) { if ( dbg >= LOW ) printf ("Checksum error on input message.\n"); iss = CK_CKSUM; } egress: *osstatus_p = status; /* Last communication status */ return iss; } /*============================================================================= Abs: Compute checksum for a message. Name: CKmsg_Checksum Args: buf_p message for which to compute checksum count # chars for which to calc checksum Rem: Return the XOR of the bytes in the message buffer. Ret: Unsigned byte checksum. =============================================================================*/ static unsigned char CKmsg_Checksum (unsigned char *buf_p, int count) { int i; unsigned char cksum; /* Our output */ /* ** Return XOR of all bytes as function output. */ for (i=1, cksum=buf_p[0]; i