/* **MEMBER**=NONE **CMS**=PCKNOBS ============================================================================== Abs: Top level routines to process PC <--> VAX messages. Name: drvKnobScpAlpha.c Module. No routine by this name. PKVmsg_Init Initialize socket. PKVmsg_Recv Receive input message. PKVmsg_FVAttach Attach a knob. PKVmsg_FVDetach Detach a knob. PKVmsg_Legend_Update Update a legend. PKVmsg_TVAck Ack to VAX. PKVmsg_TVTurns Turns to VAX. PKVmsg_TVHeart Heartbeat to VAX. static PKVmsg_Send Low level message send. Proto: drvKnobScpAlpha.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. 04-Apr-1996, Robert C. Sass (RCS): Correct calls to PKNmsg_Legend_update to pass knob_ps->ledset. 27-Sep-1996, Robert C. Sass (RCS): Eliminate UDP error routines. Use PKUtil_Errline instead. ============================================================================= */ #include /* memcpy */ #include #include #include #include #include #include "slc_macr.h" /* vmsstat_t etc. */ #include "drvKnobScp.h" #include "drvKnobScpAlpha.h" #include "drvKnobScpBox.h" /* ** Global error returned for socket calls. */ extern int errno; /* ** Local prototypes not referenced outside of this module. */ static void PKVmsg_Send (int socknum, struct sockaddr_in *outsock_ps, void *msg_p, int length); /*=========================================================================== Abs: Initialize for PC <--> VAX IP processing. Name: PKVmsg_Init Args: socknum_p pointer to created socket number Use: pointer Type: int * Acc: write only Mech: by reference sockaddr_in_ps pointer to filled in socket Use: pointer Type: struct sockaddr_in * Acc: write only Mech: by reference Rem: Create the socket, bind it with our 'known' port and set it to non-blocking so we can poll it for input. Side: None. Ret: int C logical true (OK) false (error) ============================================================================= */ int PKVmsg_Init (int *socknum_p, struct sockaddr_in *sockaddr_in_ps) { int ostatus = 1; /* Output C logical status; assume OK */ int status; /* Status from bind & sioctl */ char nonblocking = 1; /* Nonblocking I/O */ int reuseon = 1; /* For socket reuse option */ /*--------------------------- Code -----------------------------------------*/ /* ** Create a UDP/IP datagram socket to send and receive VAX msgs */ *socknum_p = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(*socknum_p < 0) { printf("\nAaack!! Error %d. Can''t create a socket!! All is lost!!\n", errno); ostatus = 0; // Bad news bears goto egress; } /* ** Bind to our IP address at the 'known' port so the VAX can talk to us. */ sockaddr_in_ps->sin_addr.s_addr = INADDR_ANY; // Our IP is known to the system sockaddr_in_ps->sin_family = AF_INET; // Ye olde standard Internet sockaddr_in_ps->sin_port = htons(PCPORT); // Our ``known'' port status = bind(*socknum_p, (struct sockaddr *)sockaddr_in_ps, sizeof(struct sockaddr_in) ); if(status < 0) { printf ("PKVmsg_Init: Aaack!! Error %d. Can't bind to the socket!! I Give up!!\n", errno); ostatus = 0; // Tell caller we failed goto egress; } /* ** Set the socket to reuse the addresss in case we're ** aborted by a scurrilous operator. */ status = setsockopt (*socknum_p, SOL_SOCKET, SO_REUSEADDR, &reuseon , sizeof(reuseon)); if(status < 0) { printf ("Aaack!! Error %d. Can''t set socket to reuse!! I Give up!!\n", errno); ostatus = 0; // Tell caller we failed //goto egress; - look into this } /* ** Set the socket to nonblocking so we can "poll" it. */ status = ioctl(*socknum_p, FIONBIO, &nonblocking); if(status < 0) { printf ("Aaack!! Error %d. Can't set non-blocking control!! Abandon ship!!\n", errno); ostatus = 0; // Tell caller it is the end goto egress; } egress: return(ostatus); } /*=========================================================================== Abs: Check for and read any waiting VAX input msgs. Name: PKVmsg_Recv Args: socknum socket number for recv Use: integer Type: int Acc: read only Mech: by value ofromsock_ps socket struct from sender Use: struct * Type: struct sockaddr_in * Acc: write only Mech: by reference omsg_p message received Use: pointer Type: charu * Acc: write only Mech: by reference omsglen_p length of message received Use: pointer Type: int * Acc: write only Mech: by reference Rem: Call recvfrom to see if there's a message. If data is available, return data and length to caller. If not, check for the normal error code and scream if it's not. Side: None. Ret: C logical true = a message is available, false = no message ============================================================================= */ int PKVmsg_Recv (int socknum, struct sockaddr_in *ofromsock_ps, charu *omsg_p, int *omsglen_p) { int ostatus = 0; /* Output Status. Assume no msgs */ static unsigned fromlen = sizeof(struct sockaddr_in); /* For recvfrom */ /*--------------------------- Code -----------------------------------------*/ /* ** Check if a message awaits. */ *omsglen_p = recvfrom (socknum, omsg_p, MAX_VAX_MSG, 0, (struct sockaddr *)ofromsock_ps, &fromlen); if (*omsglen_p > 0) ostatus = 1; // Tell caller ve haf a msg else if (errno != EWOULDBLOCK) // If no msg, check if not std error printf ("From Alpha UDP Receive Error %d!! Actual length = %d\n", errno, *omsglen_p); return (ostatus); } /*=========================================================================== Abs: Attach the requested knob. Name: PKVmsg_FVAttach Args: knob_ps knob structure pointer Use: pointer Type: knob_ts * Acc: read/write Mech: by reference firstcpu id of first cpu on this knob's box Use: integer Type: charu Acc: read Mech: by value fvattach_ps attach message from VAX Use: pointer Type: fvlegend_ts * 3 messages use same struct Acc: read Mech: by reference com com port for RS485 line of knob Use: integer Type: int Acc: read Mech: by value socknum our socket number Use: integer Type: int Acc: read Mech: by value insock_ps socket struct of sender Use: pointer Type: struct sockaddr_in * Acc: read Mech: by reference Rem: Set knob attached, save VAX 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 VAX. Side: None. Ret: Void. ============================================================================= */ void PKVmsg_FVAttach (knob_ts *knob_ps, box_ts *box_ps, fvlegend_ts *fvattach_ps, const char *node, int socknum, struct sockaddr_in *insock_ps) { vmsstat_t iss; /* Ye olde VAX status */ // int16_t lcount; /* Knob counter value */ unsigned lsstatus; /* Local serial status */ charu firstcpu = box_ps->firstcpu; /*--------------------------- Code -----------------------------------------*/ /* ** 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.) */ if ( debug >= HIGH ) printf("PKVmsg_FVAttach ledcpu %i firstcpu %i\n", knob_ps->ledcpu, firstcpu); iss = PKNmsg_Rqst_Count (node, knob_ps->ledcpu, &lsstatus, box_ps, 0); if (SUCCESS(iss)) { iss = PKNmsg_Legend_Update (node, firstcpu, knob_ps->ledset, fvattach_ps->offset, fvattach_ps->legend, &lsstatus, box_ps); } PKVmsg_TVAck (socknum, insock_ps, &fvattach_ps->pvhead_s, lsstatus, iss); /* ** Save socket of sender, last status and sequence number. If all went well, ** set knob attached else, bump error count. */ memcpy (&knob_ps->vaxsock_s, insock_ps, sizeof(struct sockaddr_in)); knob_ps->seqnum = fvattach_ps->pvhead_s.seqnum; // Latest sequence number knob_ps->laststat = iss; // Save last status if (SUCCESS(iss)) knob_ps->attached = 1; // Set knob attached so we'll poll it else knob_ps->errors++; // Bump error count if problem return; } /*=========================================================================== Abs: Detach the requested knob. Name: PKVmsg_FVDetach Args: knob_ps knob structure pointer Use: pointer Type: knob_ts * Acc: read/write Mech: by reference pointer to box struct firstcpu id of first cpu on this knob's box Use: integer Type: charu Acc: read Mech: by value fvdetach_ps detach message from VAX Use: pointer Type: fvlegend_ts * 3 messages use same struct Acc: read Mech: by reference com com port for RS485 line of knob Use: integer Type: int Acc: read Mech: by value socknum our socket number Use: integer Type: int Acc: read Mech: by value insock_ps socket struct of sender Use: pointer Type: struct sockaddr_in * Acc: read Mech: by reference Rem: Set the knob detached, save the last sequence number and the socket struct of the sender. Update the legend and send the TVACK with VAX and serial status. Save the last VMS status and if there was a problem, bump the error count for this knob. Side: None. Ret: Void. ============================================================================= */ //update argument list details everywhere! void PKVmsg_FVDetach (knob_ts *knob_ps, box_ts *box_ps, fvlegend_ts *fvdetach_ps, const char *node, int socknum, struct sockaddr_in *insock_ps) { vmsstat_t iss; // Ye olde VAX status unsigned lsstatus; // Local serial status charu firstcpu = box_ps->firstcpu; /*--------------------------- Code -----------------------------------------*/ /* ** Uupdate the legend and send the ack to the VAX with the ** appropriate status. */ iss = PKNmsg_Legend_Update (node, firstcpu, knob_ps->ledset, fvdetach_ps->offset, fvdetach_ps->legend, &lsstatus, box_ps); PKVmsg_TVAck (socknum, insock_ps, &fvdetach_ps->pvhead_s, lsstatus, iss); /* ** Save socket and sequence number of sender and last iss. Set knob detached ** regardless of error since that's what the VAX wanted. */ memcpy (&knob_ps->vaxsock_s, insock_ps, sizeof(struct sockaddr_in)); knob_ps->seqnum = fvdetach_ps->pvhead_s.seqnum; // Latest sequence number knob_ps->laststat = iss; // Save the last status knob_ps->attached = 0; // Set knob detached /* ** Bump error count if problems. */ if (!SUCCESS(iss)) knob_ps->errors++; return; } /*=========================================================================== Abs: Update the legend on the knob. Name: PKVmsg_Legend_Update Args: knob_ps knob structure pointer Use: pointer Type: knob_ts * Acc: read/write Mech: by reference firstcpu id of first cpu on this knob's box Use: integer Type: charu Acc: read Mech: by value fvlegend_ps attach message from VAX Use: pointer Type: fvlegend_ts * 3 messages use same struct Acc: read Mech: by reference com com port for RS485 line of knob Use: integer Type: int Acc: read Mech: by value socknum our socket number Use: integer Type: int Acc: read Mech: by value insock_ps socket struct of sender Use: pointer Type: struct sockaddr_in * Acc: read Mech: by reference Rem: Since IP messages can arrive out of order, check the sequnce number. If <= last one just return. Else, save the new number and update the legend. If the legend update had an error and there was no error last time, send a TVACK to the VAX with the bad status. Side: None. Ret: Void. ============================================================================= */ void PKVmsg_Legend_Update (knob_ts *knob_ps, box_ts *box_ps, //charu firstcpu, fvlegend_ts *fvlegend_ps, const char *node, int socknum, struct sockaddr_in *insock_ps) { vmsstat_t iss = PK_OKOK; /* Ye olde VAX status */ unsigned lsstatus; /* Local serial status */ charu firstcpu = box_ps->firstcpu; /*--------------------------- Code -----------------------------------------*/ /* ** If this sequence # is <= the last one then skip the update. */ if (fvlegend_ps->pvhead_s.seqnum <= knob_ps->seqnum) { if ( debug >= HIGH ) printf("PKVMsg_Legend_Update: seq # %u <= previous %u\n", (unsigned)fvlegend_ps->pvhead_s.seqnum, (unsigned)knob_ps->seqnum); goto egress; } if ( debug >= HIGH ) printf("Entered PKVmsg_Legend_Update %s\n", fvlegend_ps->legend); /* ** Save new sequence #, update the legend. */ knob_ps->seqnum = fvlegend_ps->pvhead_s.seqnum; // Latest sequence number iss = PKNmsg_Legend_Update (node, firstcpu, knob_ps->ledset, fvlegend_ps->offset, fvlegend_ps->legend, &lsstatus, box_ps); /* ** If we had an error and the last time was OK then tell the VAX via ** a TVACK message. No ack if all OK. */ if (!SUCCESS(iss) && SUCCESS(knob_ps->laststat) ) { if ( debug >= HIGH ) printf("PKVmsg_Legend_Update error and last stat ok\n"); PKVmsg_TVAck (socknum, insock_ps, &fvlegend_ps->pvhead_s, lsstatus, iss); knob_ps->errors++; // Update error count (add rollover) } knob_ps->laststat = iss; // Regardless, save the last status egress: return; } /*=========================================================================== Abs: Send acknowledge to VAX. Name: PKVmsg_TVAck Args: 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 pvhead_ps pointer to VAX msg header Use: pointer Type: pvhead_ts * Acc: read Mech: by reference sstatus serial status Use: integer Type: unsigned Acc: read Mech: by value iss VMS status Use: integer Type: vmsstat_t Acc: read Mech: by value Rem: Build the tvack message from the header and VMS status and send to the socket pointed to by outsock_ps. Side: None. Ret: Void. ============================================================================= */ void PKVmsg_TVAck (int socknum, struct sockaddr_in *outsock_ps, pvhead_ts *pvhead_ps, unsigned sstatus, vmsstat_t iss ) { tvack_ts tvack_s; /* The ack msg */ /*--------------------------- Code -----------------------------------------*/ /* ** Copy header & status & send to VAX. */ memcpy (&tvack_s.pvhead_s, pvhead_ps, sizeof(pvhead_ts)); tvack_s.acode = tvack_s.pvhead_s.code; // Return original code tvack_s.pvhead_s.code = TVACK; // The ack function tvack_s.vmsstat = iss; tvack_s.sstatus = sstatus; if ( debug >= HIGH ) printf("PKVmsg_TVAck: ack msg to vax iss %i sstatus %i\n", (int)iss, sstatus); PKVmsg_Send (socknum, outsock_ps, &tvack_s, sizeof(tvack_ts)); return; } /*=========================================================================== Abs: Send turns message to VAX. Name: PKVmsg_TVTurns Args: knob_ps knob structure pointer Use: pointer Type: knob_ts * Acc: read/write Mech: by reference com com port for RS485 line of knob Use: integer Type: int Acc: read Mech: by value iline line number of knob Use: integer Type: char Acc: read Mech: by value ibox box number of knob Use: integer Type: char Acc: read Mech: by value socknum our socket number Use: integer Type: int Acc: read Mech: by value Rem: Send a count request msg to the knob and get its reply. We send a TVTURNS message to the VAX under 2 conditions: 1) If the status is good and the count is non-zero. 2) If the status is bad but it was good last time. Format and send the TVTURNS message if either condition is met. Save the last status and bump error count if there was an error communicating with the knob. Side: None. Ret: Void. ============================================================================= */ void PKVmsg_TVTurns (knob_ts *knob_ps, box_ts *box_ps, const char *node, char iline, char ibox, int socknum) { vmsstat_t iss; // Ye olde VAX status int16_t lcount = 0; // Knob counter value unsigned lsstatus; // Serial status tvturns_ts tvturns_s; // The turns msg /*--------------------------- Code -----------------------------------------*/ if ( debug >= HIGH ) printf("PKVmsg_TVTurns node %s ledcpu %i\n", node, knob_ps->ledcpu); /* ** Get count from knob. */ iss = PKNmsg_Rqst_Count (node, knob_ps->ledcpu, &lsstatus, box_ps, &lcount); /* ** We send a message to the VAX under 2 conditions: ** ** 1) If the status is good and the count is non-zero. ** 2) If the status is bad but it was good last time. */ if ( (SUCCESS(iss) && (lcount != 0)) || (!SUCCESS(iss) && SUCCESS(knob_ps->laststat)) ) /* ** Fill in all the fields and send the message. */ { tvturns_s.pvhead_s.code = TVTURNS; tvturns_s.pvhead_s.line = iline; tvturns_s.pvhead_s.box = ibox; tvturns_s.pvhead_s.knob = knob_ps->ledset; tvturns_s.pvhead_s.seqnum = knob_ps->seqnum; tvturns_s.sstatus = lsstatus; tvturns_s.turns = lcount; tvturns_s.vmsstat = iss; if ( debug >= MED ) printf("PKVmsg_TVTurns: before sending %i turns to VAX\n", tvturns_s.turns); PKVmsg_Send (socknum, &knob_ps->vaxsock_s, &tvturns_s, sizeof(tvturns_ts)); } knob_ps->laststat = iss; // Always save last status if (!SUCCESS(iss)) knob_ps->errors++; // Bump error count if problem return; } /*=========================================================================== Abs: Send hearbeat ack message to VAX. Name: PKVmsg_TVHeart Args: 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: Fill in the message code and a good status and send the heartbeat to the socket pointed to by outsock_ps. Side: None. Ret: Void. ============================================================================= */ void PKVmsg_TVHeart (int socknum, struct sockaddr_in *outsock_ps) { tvheart_ts tvheart_s; // The heartbeat ack msg /*--------------------------- Code -----------------------------------------*/ /* ** Fill in all the fields and send the message. */ tvheart_s.code = TVHEART; tvheart_s.vmsstat = PK_OKOK; /* ** Send the message to the VAX */ if ( debug >= HIGH ) printf("PKVmsg_TVHeart: heartbeat msg to vax\n"); PKVmsg_Send (socknum, outsock_ps, &tvheart_s, sizeof(tvheart_ts)); return; } /*=========================================================================== Abs: Internal routine to send a message to the VAX. Name: PKVmsg_Send Args: 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 omsg_p the message to send Use: pointer Type: void * Acc: read Mech: by reference length # bytes to send Use: integer Type: int Acc: read Mech: by value Rem: Call sendto to send the IP datagram to the specified socket. Side: None. Ret: Void. ============================================================================= */ static void PKVmsg_Send (int socknum, struct sockaddr_in *outsock_ps, void *omsg_p, int length) { int outlen; // Actual length sent /*--------------------------- Code -----------------------------------------*/ if ( debug >= HIGH ) { printf("PKVmsg_Send: before sending message to vax\n"); printf("outsock_ps->sin_addr %i\n", outsock_ps->sin_addr.s_addr); } outlen = sendto (socknum, omsg_p, length, 0, (struct sockaddr *)outsock_ps, sizeof(struct sockaddr_in)); if (outlen != length) { printf ("UDP to Alpha Send Error %d!! Actual length = %d expected length = %d\n", errno, outlen, length); } else { if ( debug >= HIGH ) printf("success sending message to vax actual %d expected %d\n", outlen, length); } return; }