#include /* strncpy */ #include /* abs */ #include /* printf & friends */ #include /* NULL */ #include /* time, ctime */ #include /* abs */ #include #include #include #include #include "knobCtrlDef.h" #include "knobCtrlBoxCommDef.h" #include "knobCtrlUserCommDef.h" #include "commUdpTcp.h" #include "knobUserEpics.h" #define UDP 0 #define TCP 1 static int PROTO = UDP; /* for historical reasons (VMS uses it) */ static void CKN_Exit_Handler (int socknum_p); #undef DEBUG static int init_comm(void *node_p, int *port) { sockdesc_ts *ctrlio_ps; int connected; /* Local dummy variable to keep commUdpTcpClntInit happy */ /* global communication data structure */ if ( !(kuserglobal_s.ctrlio_p = calloc( 1, sizeof( sockdesc_ts ) )) ) { printf("knobDeviceCommUdp: FATAL error, cannot allocate memory\n"); return -1; } ctrlio_ps = kuserglobal_s.ctrlio_p; memcpy( (void *)ctrlio_ps->node_a, node_p, NODE_NAME_MAX ); ctrlio_ps->port = *port; return commUdpTcpClntInit( &ctrlio_ps->socknum, &ctrlio_ps->socket_s, (char *)node_p, PROTO, *port, &connected ); } static void user_write(unsigned char *bufo_p, int count, uint32_t *iss, int dbg) { sockdesc_ts *ctrlio_ps = kuserglobal_s.ctrlio_p; int outlen, i; if ( dbg >= HIGH ) { printf("knobDeviceCommUdp: write msg:\n"); for (i = 0; i < count; i++ ) printf("%c", bufo_p[i]); printf("\n"); for (i = 0; i < count; i++ ) printf("0x%02x ", bufo_p[i]); printf("\n"); } commUdpTcpWrite( &ctrlio_ps->socknum, &ctrlio_ps->socket_s, bufo_p, count, &outlen ); if ( outlen != count ) *iss = 0; if (errno == EPIPE) { if ( dbg > LOW ) printf("Broken pipe. Not connected to knob box\n"); } } static void user_read(unsigned char *inmsgbuf, int *inmsglen_p) { sockdesc_ts *ctrlio_ps = kuserglobal_s.ctrlio_p; struct timeval timeout; int i; /* Set timeout for receiving messages */ timeout.tv_sec = 0; timeout.tv_usec = 1e5; commUdpTcpRead( &ctrlio_ps->socknum, &ctrlio_ps->socket_s, inmsgbuf, MAX_USER_MSG, inmsglen_p, timeout ); #ifdef DEBUG if ( *inmsglen_p > 0 ) { printf("knobDeviceCommUdp: read msg length %i:\n", *inmsglen_p); for (i = 0; i < *inmsglen_p; i ++) printf("%c", inmsgbuf[i]); printf("\n"); } #endif } /*============================================================================= Abs: Exit handler for us. Name: CKMain_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 CKN_Exit_Handler (int socknum_p) { int status; char blocking = 0; /* Set the socket to blocking so the sclose is synchronous. */ status = ioctl(socknum_p, FIONBIO, &blocking); status = close(socknum_p); return; } kuserctrlcb_ts knobUserCtrlCommUdpCb = { init_comm: init_comm, init_line: 0, init_box: 0, write: user_write, read: user_read, write_uint32: htonl, write_uint16: htons, read_uint32: ntohl, read_uint16: ntohs }; static void drvKnobUserCtrlCommUdpRegistrar(void) { registryAdd( (void*)knobUserCtrlCommCbRegistryId, "knobUserCtrlCommUdpCb", &knobUserCtrlCommUdpCb ); } epicsExportRegistrar(drvKnobUserCtrlCommUdpRegistrar);