diff --git a/hardsup/StrMatch.c b/hardsup/StrMatch.c index 6134a05..4a1af05 100755 --- a/hardsup/StrMatch.c +++ b/hardsup/StrMatch.c @@ -81,16 +81,18 @@ **==================================================================== ** StrMatch - compare two strings. */ - int StrMatch ( +int StrMatch( /* ======== -*/ char *str_a, - char *str_b, - int min_len) { +*/ char *str_a, + char *str_b, int min_len) +{ - int i = 0; + int i = 0; - while ((tolower(str_a[i]) == tolower(str_b[i])) && (str_a[i] != '\0')) i++; + while ((tolower(str_a[i]) == tolower(str_b[i])) && (str_a[i] != '\0')) + i++; + + return ((str_a[i] == '\0') && (i >= min_len)); +} - return ((str_a[i] == '\0') && (i >= min_len)); - } /*-------------------------------------------------- End of StrMatch.C =======*/ diff --git a/hardsup/asynsrv_def.h b/hardsup/asynsrv_def.h index ccaed19..d25e864 100644 --- a/hardsup/asynsrv_def.h +++ b/hardsup/asynsrv_def.h @@ -12,40 +12,40 @@ #endif #define AsynSrv_MAX_LINK 8 - /* - ** Structures needed by AsynSrv_Utility. - */ - struct AsynSrv__info { - int skt; /* The socket number of the connection */ - char host[20]; /* The name of RS-232-C server */ - int port; /* The TCP/IP port number of server */ - int chan; /* The RS-232-C channel number on server */ - int msg_id; - int protocol_code; /* Flag to identify the server's protocol level */ - char protocol_id[4]; /* ASCII version of server's protocol level */ - int cmnd_hdr_len; /* Header length for command strings */ - char cmnd_fmt[8]; /* "sprintf" format for cmnd header conversion */ - int rply_hdr_len; /* Header length for response strings */ - char rply_fmt[8]; /* "sscanf" format for rply header conversion */ - char chan_char[4]; /* ASCII encoded version of chan */ - char tmo[4]; /* ASCII encoded time-out (deci-secs) */ - char eot[4]; /* Expected terminators */ - int max_replies; /* Binary version of #replies in response */ - int n_replies; /* # of last response returned to caller */ - void (*idleHandler) (int, int); /* MZ. handler called when waiting .. - ** .. on a response */ - }; + /* + ** Structures needed by AsynSrv_Utility. + */ +struct AsynSrv__info { + int skt; /* The socket number of the connection */ + char host[20]; /* The name of RS-232-C server */ + int port; /* The TCP/IP port number of server */ + int chan; /* The RS-232-C channel number on server */ + int msg_id; + int protocol_code; /* Flag to identify the server's protocol level */ + char protocol_id[4]; /* ASCII version of server's protocol level */ + int cmnd_hdr_len; /* Header length for command strings */ + char cmnd_fmt[8]; /* "sprintf" format for cmnd header conversion */ + int rply_hdr_len; /* Header length for response strings */ + char rply_fmt[8]; /* "sscanf" format for rply header conversion */ + char chan_char[4]; /* ASCII encoded version of chan */ + char tmo[4]; /* ASCII encoded time-out (deci-secs) */ + char eot[4]; /* Expected terminators */ + int max_replies; /* Binary version of #replies in response */ + int n_replies; /* # of last response returned to caller */ + void (*idleHandler) (int, int); /* MZ. handler called when waiting .. + ** .. on a response */ +}; - struct AsynSrv_HostPortSkt { - char host[30]; - int port; - int skt; - int protocol_code; - char protocol_id[4]; - int cmnd_hdr_len; - int rply_hdr_len; - int usage_cnt; - int status; - }; +struct AsynSrv_HostPortSkt { + char host[30]; + int port; + int skt; + int protocol_code; + char protocol_id[4]; + int cmnd_hdr_len; + int rply_hdr_len; + int usage_cnt; + int status; +}; /*------------------------------------------------ End of AsynSrv_DEF.H --*/ -#endif /* _asynsrv_def_ */ +#endif /* _asynsrv_def_ */ diff --git a/hardsup/asynsrv_mark.c b/hardsup/asynsrv_mark.c index 5927350..5e81e22 100644 --- a/hardsup/asynsrv_mark.c +++ b/hardsup/asynsrv_mark.c @@ -383,1083 +383,1176 @@ /*-------------------------------------------------------------------------- ** Global Variables */ - static int AsynSrv_call_depth = 0; - static char AsynSrv_routine[5][64]; - static int AsynSrv_errcode = 0; - static int AsynSrv_errno, AsynSrv_vaxc_errno; - static int AsynSrv_connect_tmo = 5; /* Time-out on "connect" */ - static int AsynSrv_msec_tmo = 10000; /* Time-out for responses */ - static char AsynSrv_eot[] = {'1', '\r', '\0','\0'}; /* Terminators */ +static int AsynSrv_call_depth = 0; +static char AsynSrv_routine[5][64]; +static int AsynSrv_errcode = 0; +static int AsynSrv_errno, AsynSrv_vaxc_errno; +static int AsynSrv_connect_tmo = 5; /* Time-out on "connect" */ +static int AsynSrv_msec_tmo = 10000; /* Time-out for responses */ +static char AsynSrv_eot[] = { '1', '\r', '\0', '\0' }; /* Terminators */ + /* ** The following is the list of open connections (= number of ** active sockets). */ - static int AsynSrv_n_cnct = 0; - static struct AsynSrv_HostPortSkt AsynSrv_HPS_list[AsynSrv_MAX_LINK]; +static int AsynSrv_n_cnct = 0; +static struct AsynSrv_HostPortSkt AsynSrv_HPS_list[AsynSrv_MAX_LINK]; /* ** The following is the list of active calls to AsynSrv_Open. */ - static int AsynSrv_n_active = 0; - static struct AsynSrv__info *AsynSrv_active[MAX_OPEN]; +static int AsynSrv_n_active = 0; +static struct AsynSrv__info *AsynSrv_active[MAX_OPEN]; /* **--------------------------------------------------------------------------- ** AsynSrv_Close: Close a connection to an RS-232-C server. */ - int AsynSrv_Close ( +int AsynSrv_Close( /* ============= -*/ struct AsynSrv__info *asyn_info, - int force_flag) { +*/ struct AsynSrv__info *asyn_info, + int force_flag) +{ - int i, j, k, my_skt; - char buff[4]; - /*----------------------------------------------- + int i, j, k, my_skt; + char buff[4]; + /*----------------------------------------------- */ - if (asyn_info == NULL) return True; /* Just return if nothing to do! */ - my_skt = asyn_info->skt; - if (my_skt <= 0) return True; /* Just return if nothing to do! */ - /*----------------------------------------------- + if (asyn_info == NULL) + return True; /* Just return if nothing to do! */ + my_skt = asyn_info->skt; + if (my_skt <= 0) + return True; /* Just return if nothing to do! */ + /*----------------------------------------------- ** Pre-set the routinename (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Close"); - AsynSrv_call_depth++; - } - /*------------------------------------------------------ + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Close"); + AsynSrv_call_depth++; + } + /*------------------------------------------------------ ** Start by finding the table entry for this connection */ - for (i = 0; i < AsynSrv_n_cnct; i++) { - if (AsynSrv_HPS_list[i].skt != my_skt) continue; - if (AsynSrv_HPS_list[i].port != asyn_info->port) continue; - if (strcmp (AsynSrv_HPS_list[i].host, asyn_info->host) == 0) break; - } - if (i >= AsynSrv_n_cnct) { /* Did we find the entry? */ - AsynSrv_errcode = AsynSrv__BAD_PAR; /* No! */ - return False; - } - /*------------------------------------------------------ + for (i = 0; i < AsynSrv_n_cnct; i++) { + if (AsynSrv_HPS_list[i].skt != my_skt) + continue; + if (AsynSrv_HPS_list[i].port != asyn_info->port) + continue; + if (strcmp(AsynSrv_HPS_list[i].host, asyn_info->host) == 0) + break; + } + if (i >= AsynSrv_n_cnct) { /* Did we find the entry? */ + AsynSrv_errcode = AsynSrv__BAD_PAR; /* No! */ + return False; + } + /*------------------------------------------------------ ** Now find the table entry for the AsynSrvOpen call. - */ - for (j = 0; j < AsynSrv_n_active; j++) { - if ((AsynSrv_active[j] == asyn_info) && - (AsynSrv_active[j]->skt == my_skt)) { - break; - } + */ + for (j = 0; j < AsynSrv_n_active; j++) { + if ((AsynSrv_active[j] == asyn_info) && + (AsynSrv_active[j]->skt == my_skt)) { + break; } - if (j >= AsynSrv_n_active) { /* Did we find the entry? */ - AsynSrv_errcode = AsynSrv__BAD_PAR; /* No! */ - return False; - } - /*------------------------------------------------------ + } + if (j >= AsynSrv_n_active) { /* Did we find the entry? */ + AsynSrv_errcode = AsynSrv__BAD_PAR; /* No! */ + return False; + } + /*------------------------------------------------------ ** i is the index for the connection table entry. ** j is the index for the caller's AsynSrvOpen call entry. */ - if (AsynSrv_HPS_list[i].usage_cnt <= 0) { /* Is the connection active? */ - AsynSrv_errcode = AsynSrv__BAD_PAR; /* No */ - return False; - } - /*------------------------------------------------------ + if (AsynSrv_HPS_list[i].usage_cnt <= 0) { /* Is the connection active? */ + AsynSrv_errcode = AsynSrv__BAD_PAR; /* No */ + return False; + } + /*------------------------------------------------------ ** For the caller, simply set his socket number to zero, ** mark the AsynSrvOpen entry as free and decrease the ** usage count (the entries will be compressed later). */ - AsynSrv_active[j]->skt = 0; /* Mark the close .. */ - AsynSrv_active[j] = NULL; /* .. and flag entry to be removed. */ - AsynSrv_HPS_list[i].usage_cnt--; /* Decrease usage count */ - /*------------------------------------------------------ + AsynSrv_active[j]->skt = 0; /* Mark the close .. */ + AsynSrv_active[j] = NULL; /* .. and flag entry to be removed. */ + AsynSrv_HPS_list[i].usage_cnt--; /* Decrease usage count */ + /*------------------------------------------------------ ** If this is a force-close, go through all AsynSrv_Open ** entries looking for a socket match, mark them as ** free and decrease usage count. */ - if (force_flag) { - for (k = 0; k < AsynSrv_n_active; k++) { - if (AsynSrv_active[k] != NULL) { - if (AsynSrv_active[k]->skt == my_skt) { - AsynSrv_active[k]->skt = -1; /* Mark the force-close */ - AsynSrv_active[k] = NULL; /* Mark entry to be removed */ - AsynSrv_HPS_list[i].usage_cnt--; /* Decrease usage count */ - } - } - } - if (AsynSrv_HPS_list[i].usage_cnt != 0) { /* Use count should now be .. */ - AsynSrv_errcode = AsynSrv__BAD_PAR; /* .. zero or there's a bug. */ - return False; - } - } - /*------------------------------------------------------ - ** Compress the list of AsynSrv_Open entries - */ - j = 0; + if (force_flag) { for (k = 0; k < AsynSrv_n_active; k++) { if (AsynSrv_active[k] != NULL) { - AsynSrv_active[j] = AsynSrv_active[k]; - j++; + if (AsynSrv_active[k]->skt == my_skt) { + AsynSrv_active[k]->skt = -1; /* Mark the force-close */ + AsynSrv_active[k] = NULL; /* Mark entry to be removed */ + AsynSrv_HPS_list[i].usage_cnt--; /* Decrease usage count */ + } } } - for (k = j; k < AsynSrv_n_active; k++) AsynSrv_active[k] = NULL; - AsynSrv_n_active = j; - /*------------------------------------------------------ + if (AsynSrv_HPS_list[i].usage_cnt != 0) { /* Use count should now be .. */ + AsynSrv_errcode = AsynSrv__BAD_PAR; /* .. zero or there's a bug. */ + return False; + } + } + /*------------------------------------------------------ + ** Compress the list of AsynSrv_Open entries + */ + j = 0; + for (k = 0; k < AsynSrv_n_active; k++) { + if (AsynSrv_active[k] != NULL) { + AsynSrv_active[j] = AsynSrv_active[k]; + j++; + } + } + for (k = j; k < AsynSrv_n_active; k++) + AsynSrv_active[k] = NULL; + AsynSrv_n_active = j; + /*------------------------------------------------------ ** If the link is now idle, really close it and compress ** the connection table entry out of the list. */ - if (AsynSrv_HPS_list[i].usage_cnt == 0) { - send (my_skt, "-001", 4, 0); /* Tell the TCP/IP server that .. - ** .. we are about to quit. - */ - recv (my_skt, buff, sizeof (buff), 0); /* And wait for his ack */ - close (my_skt); - for (j = i; j < AsynSrv_n_cnct; j++) { - memcpy ((char *) &AsynSrv_HPS_list[j], (char *) &AsynSrv_HPS_list[j+1], - sizeof (AsynSrv_HPS_list[0])); - } - AsynSrv_HPS_list[AsynSrv_n_cnct].skt = 0; /* Invalidate the free entry */ - AsynSrv_n_cnct--; + if (AsynSrv_HPS_list[i].usage_cnt == 0) { + send(my_skt, "-001", 4, 0); /* Tell the TCP/IP server that .. + ** .. we are about to quit. + */ + recv(my_skt, buff, sizeof(buff), 0); /* And wait for his ack */ + close(my_skt); + for (j = i; j < AsynSrv_n_cnct; j++) { + memcpy((char *) &AsynSrv_HPS_list[j], + (char *) &AsynSrv_HPS_list[j + 1], + sizeof(AsynSrv_HPS_list[0])); } - AsynSrv_call_depth--; - return True; + AsynSrv_HPS_list[AsynSrv_n_cnct].skt = 0; /* Invalidate the free entry */ + AsynSrv_n_cnct--; } + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_Config: Configure an open connection. */ - int AsynSrv_Config ( +int AsynSrv_Config( /* ============== -*/ struct AsynSrv__info *asyn_info, - ...) { +*/ struct AsynSrv__info *asyn_info, + ...) +{ - char buff[16]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - int intval; - /* - ** Pre-set the routinename (in case of error) - */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Config"); - AsynSrv_call_depth++; - } - - va_start (ap, asyn_info); /* Set up var arg machinery */ - txt_ptr = va_arg (ap, char *); /* Get pntr to first parameter ident */ - while (txt_ptr != NULL) { - if (strcmp (txt_ptr, "msecTmo") == 0) { - intval = va_arg (ap, int); - if ((intval < 100) || (intval > 999999)) { - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - sprintf (buff, "%04d", intval/100); /* Convert to ASCII as .. - ** .. deci-secs */ - memcpy (asyn_info->tmo, buff, 4); - }else if (strcmp (txt_ptr, "eot") == 0) { - txt_ptr = va_arg (ap, char *); - if (txt_ptr == NULL) { - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - memcpy (asyn_info->eot, "\0\0\0\0", 4); - switch (txt_ptr[0]) { - case '3': asyn_info->eot[3] = txt_ptr[3]; - case '2': asyn_info->eot[2] = txt_ptr[2]; - case '1': asyn_info->eot[1] = txt_ptr[1]; - case '0': - asyn_info->eot[0] = txt_ptr[0]; - break; - default: - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - }else { - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - txt_ptr = va_arg (ap, char *); /* Get pntr to next parameter ident */ - } - - AsynSrv_call_depth--; - return True; + char buff[16]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + int intval; + /* + ** Pre-set the routinename (in case of error) + */ + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Config"); + AsynSrv_call_depth++; } + + va_start(ap, asyn_info); /* Set up var arg machinery */ + txt_ptr = va_arg(ap, char *); /* Get pntr to first parameter ident */ + while (txt_ptr != NULL) { + if (strcmp(txt_ptr, "msecTmo") == 0) { + intval = va_arg(ap, int); + if ((intval < 100) || (intval > 999999)) { + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + sprintf(buff, "%04d", intval / 100); /* Convert to ASCII as .. + ** .. deci-secs */ + memcpy(asyn_info->tmo, buff, 4); + } else if (strcmp(txt_ptr, "eot") == 0) { + txt_ptr = va_arg(ap, char *); + if (txt_ptr == NULL) { + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + memcpy(asyn_info->eot, "\0\0\0\0", 4); + switch (txt_ptr[0]) { + case '3': + asyn_info->eot[3] = txt_ptr[3]; + case '2': + asyn_info->eot[2] = txt_ptr[2]; + case '1': + asyn_info->eot[1] = txt_ptr[1]; + case '0': + asyn_info->eot[0] = txt_ptr[0]; + break; + default: + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + } else { + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + txt_ptr = va_arg(ap, char *); /* Get pntr to next parameter ident */ + } + + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_ConfigDflt: Set default values in AsynSrv_Utility ** which will be used to initialise ** structures in AsynSrv_Open. */ - int AsynSrv_ConfigDflt ( +int AsynSrv_ConfigDflt( /* ================== -*/ char *par_id, - ...) { - int i; - char buff[4]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - int intval; - /* - ** Pre-set the routinename (in case of error) - */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_ConfigDflt"); - AsynSrv_call_depth++; - } - - va_start (ap, par_id); /* Set up var arg machinery */ - txt_ptr = par_id; /* Point to first arg */ - while (txt_ptr != NULL) { - if (strcmp (txt_ptr, "tmoC") == 0) { - intval = va_arg (ap, int); - if ((intval < 1) || (intval > 3600)) { - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - AsynSrv_connect_tmo = intval; - }else if (strcmp (txt_ptr, "msecTmo") == 0) { - intval = va_arg (ap, int); - if ((intval < 100) || (intval > 999900)) { - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - AsynSrv_msec_tmo = intval; - }else if (strcmp (txt_ptr, "eot") == 0) { - txt_ptr = va_arg (ap, char *); - if (txt_ptr == NULL) { - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - switch (txt_ptr[0]) { - case '3': AsynSrv_eot[3] = txt_ptr[3]; - case '2': AsynSrv_eot[2] = txt_ptr[2]; - case '1': AsynSrv_eot[1] = txt_ptr[1]; - case '0': - AsynSrv_eot[0] = txt_ptr[0]; - break; - default: - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - switch (txt_ptr[0]) { - case '0': AsynSrv_eot[1] = '\0'; - case '1': AsynSrv_eot[2] = '\0'; - case '2': AsynSrv_eot[3] = '\0'; - } - }else { - AsynSrv_errcode = AsynSrv__BAD_PAR; - return False; - } - txt_ptr = va_arg (ap, char *); /* Get pntr to next parameter ident */ - } - - AsynSrv_call_depth--; - return True; +*/ char *par_id, + ...) +{ + int i; + char buff[4]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + int intval; + /* + ** Pre-set the routinename (in case of error) + */ + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_ConfigDflt"); + AsynSrv_call_depth++; } + + va_start(ap, par_id); /* Set up var arg machinery */ + txt_ptr = par_id; /* Point to first arg */ + while (txt_ptr != NULL) { + if (strcmp(txt_ptr, "tmoC") == 0) { + intval = va_arg(ap, int); + if ((intval < 1) || (intval > 3600)) { + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + AsynSrv_connect_tmo = intval; + } else if (strcmp(txt_ptr, "msecTmo") == 0) { + intval = va_arg(ap, int); + if ((intval < 100) || (intval > 999900)) { + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + AsynSrv_msec_tmo = intval; + } else if (strcmp(txt_ptr, "eot") == 0) { + txt_ptr = va_arg(ap, char *); + if (txt_ptr == NULL) { + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + switch (txt_ptr[0]) { + case '3': + AsynSrv_eot[3] = txt_ptr[3]; + case '2': + AsynSrv_eot[2] = txt_ptr[2]; + case '1': + AsynSrv_eot[1] = txt_ptr[1]; + case '0': + AsynSrv_eot[0] = txt_ptr[0]; + break; + default: + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + switch (txt_ptr[0]) { + case '0': + AsynSrv_eot[1] = '\0'; + case '1': + AsynSrv_eot[2] = '\0'; + case '2': + AsynSrv_eot[3] = '\0'; + } + } else { + AsynSrv_errcode = AsynSrv__BAD_PAR; + return False; + } + txt_ptr = va_arg(ap, char *); /* Get pntr to next parameter ident */ + } + + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_ErrInfo: Return detailed status from last operation. */ - void AsynSrv_ErrInfo ( +void AsynSrv_ErrInfo( /* =============== -*/ char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno) { +*/ char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno) +{ - int i, j, k; - char buff[80]; + int i, j, k; + char buff[80]; - if (AsynSrv_call_depth <= 0) { - strcpy (AsynSrv_routine[0], "AsynSrv_no_error_detected"); - *errcode = 0; - *my_errno = 0; - *vaxc_errno = 0; - }else { - if (AsynSrv_call_depth > 1) { /* Concatenate the names */ - for (i = 1; i < AsynSrv_call_depth; i++) { - strcat (AsynSrv_routine[0], "/"); - StrJoin (AsynSrv_routine[0], sizeof (AsynSrv_routine), - AsynSrv_routine[0], AsynSrv_routine[i]); - } + if (AsynSrv_call_depth <= 0) { + strcpy(AsynSrv_routine[0], "AsynSrv_no_error_detected"); + *errcode = 0; + *my_errno = 0; + *vaxc_errno = 0; + } else { + if (AsynSrv_call_depth > 1) { /* Concatenate the names */ + for (i = 1; i < AsynSrv_call_depth; i++) { + strcat(AsynSrv_routine[0], "/"); + StrJoin(AsynSrv_routine[0], sizeof(AsynSrv_routine), + AsynSrv_routine[0], AsynSrv_routine[i]); } - *errcode = AsynSrv_errcode; - *my_errno = AsynSrv_errno; - *vaxc_errno = AsynSrv_vaxc_errno; - switch (AsynSrv_errcode) { - case AsynSrv__BAD_HOST: strcpy (buff, "/AsynSrv__BAD_HOST"); break; - case AsynSrv__BAD_SOCKET: strcpy (buff, "/AsynSrv__BAD_SOCKET"); break; - case AsynSrv__BAD_BIND: strcpy (buff, "/AsynSrv__BAD_BIND"); break; - case AsynSrv__BAD_CONNECT: strcpy (buff, "/AsynSrv__BAD_CONNECT"); break; - case AsynSrv__BAD_SENDLEN: strcpy (buff, "/AsynSrv__BAD_SENDLEN"); break; - case AsynSrv__BAD_SEND: strcpy (buff, "/AsynSrv__BAD_SEND"); break; - case AsynSrv__BAD_SEND_PIPE: strcpy (buff, "/AsynSrv__BAD_SEND_PIPE"); break; - case AsynSrv__BAD_SEND_NET: strcpy (buff, "/AsynSrv__BAD_SEND_NET"); break; - case AsynSrv__BAD_SEND_UNKN: strcpy (buff, "/AsynSrv__BAD_SEND_UNKN"); break; - case AsynSrv__BAD_RECV: strcpy (buff, "/AsynSrv__BAD_RECV"); break; - case AsynSrv__BAD_RECV_PIPE: strcpy (buff, "/AsynSrv__BAD_RECV_PIPE"); break; - case AsynSrv__BAD_RECV_NET: strcpy (buff, "/AsynSrv__BAD_RECV_NET"); break; - case AsynSrv__BAD_RECV_UNKN: strcpy (buff, "/AsynSrv__BAD_RECV_UNKN"); break; - case AsynSrv__BAD_NOT_BCD: strcpy (buff, "/AsynSrv__BAD_NOT_BCD"); break; - case AsynSrv__BAD_RECVLEN: strcpy (buff, "/AsynSrv__BAD_RECVLEN"); break; - case AsynSrv__BAD_FLUSH: strcpy (buff, "/AsynSrv__BAD_FLUSH"); break; - case AsynSrv__BAD_RECV1: strcpy (buff, "/AsynSrv__BAD_RECV1"); break; - case AsynSrv__BAD_RECV1_PIPE:strcpy (buff, "/AsynSrv__BAD_RECV1_PIPE"); break; - case AsynSrv__BAD_RECV1_NET: strcpy (buff, "/AsynSrv__BAD_RECV1_NET"); break; - case AsynSrv__BAD_PAR: strcpy (buff, "/AsynSrv__BAD_PAR"); break; - case AsynSrv__FORCED_CLOSED: strcpy (buff, "/AsynSrv__FORCED_CLOSED"); break; - case AsynSrv__BAD_REPLY: strcpy (buff, "/AsynSrv__BAD_REPLY"); break; - case AsynSrv__BAD_CMND_LEN: strcpy (buff, "/AsynSrv__BAD_CMND_LEN"); break; - case AsynSrv__BAD_PROT_LVL: strcpy (buff, "/AsynSrv__BAD_PROT_LVL"); break; - case AsynSrv__NO_ROOM: strcpy (buff, "/AsynSrv__NO_ROOM"); break; - default: sprintf (buff, "/AsynSrv__unkn_err_code: %d", AsynSrv_errcode); - } - StrJoin (AsynSrv_routine[0], sizeof(AsynSrv_routine), - AsynSrv_routine[0], buff); } - *entry_txt = AsynSrv_routine[0]; - AsynSrv_call_depth = 0; - AsynSrv_errcode = 0; + *errcode = AsynSrv_errcode; + *my_errno = AsynSrv_errno; + *vaxc_errno = AsynSrv_vaxc_errno; + switch (AsynSrv_errcode) { + case AsynSrv__BAD_HOST: + strcpy(buff, "/AsynSrv__BAD_HOST"); + break; + case AsynSrv__BAD_SOCKET: + strcpy(buff, "/AsynSrv__BAD_SOCKET"); + break; + case AsynSrv__BAD_BIND: + strcpy(buff, "/AsynSrv__BAD_BIND"); + break; + case AsynSrv__BAD_CONNECT: + strcpy(buff, "/AsynSrv__BAD_CONNECT"); + break; + case AsynSrv__BAD_SENDLEN: + strcpy(buff, "/AsynSrv__BAD_SENDLEN"); + break; + case AsynSrv__BAD_SEND: + strcpy(buff, "/AsynSrv__BAD_SEND"); + break; + case AsynSrv__BAD_SEND_PIPE: + strcpy(buff, "/AsynSrv__BAD_SEND_PIPE"); + break; + case AsynSrv__BAD_SEND_NET: + strcpy(buff, "/AsynSrv__BAD_SEND_NET"); + break; + case AsynSrv__BAD_SEND_UNKN: + strcpy(buff, "/AsynSrv__BAD_SEND_UNKN"); + break; + case AsynSrv__BAD_RECV: + strcpy(buff, "/AsynSrv__BAD_RECV"); + break; + case AsynSrv__BAD_RECV_PIPE: + strcpy(buff, "/AsynSrv__BAD_RECV_PIPE"); + break; + case AsynSrv__BAD_RECV_NET: + strcpy(buff, "/AsynSrv__BAD_RECV_NET"); + break; + case AsynSrv__BAD_RECV_UNKN: + strcpy(buff, "/AsynSrv__BAD_RECV_UNKN"); + break; + case AsynSrv__BAD_NOT_BCD: + strcpy(buff, "/AsynSrv__BAD_NOT_BCD"); + break; + case AsynSrv__BAD_RECVLEN: + strcpy(buff, "/AsynSrv__BAD_RECVLEN"); + break; + case AsynSrv__BAD_FLUSH: + strcpy(buff, "/AsynSrv__BAD_FLUSH"); + break; + case AsynSrv__BAD_RECV1: + strcpy(buff, "/AsynSrv__BAD_RECV1"); + break; + case AsynSrv__BAD_RECV1_PIPE: + strcpy(buff, "/AsynSrv__BAD_RECV1_PIPE"); + break; + case AsynSrv__BAD_RECV1_NET: + strcpy(buff, "/AsynSrv__BAD_RECV1_NET"); + break; + case AsynSrv__BAD_PAR: + strcpy(buff, "/AsynSrv__BAD_PAR"); + break; + case AsynSrv__FORCED_CLOSED: + strcpy(buff, "/AsynSrv__FORCED_CLOSED"); + break; + case AsynSrv__BAD_REPLY: + strcpy(buff, "/AsynSrv__BAD_REPLY"); + break; + case AsynSrv__BAD_CMND_LEN: + strcpy(buff, "/AsynSrv__BAD_CMND_LEN"); + break; + case AsynSrv__BAD_PROT_LVL: + strcpy(buff, "/AsynSrv__BAD_PROT_LVL"); + break; + case AsynSrv__NO_ROOM: + strcpy(buff, "/AsynSrv__NO_ROOM"); + break; + default: + sprintf(buff, "/AsynSrv__unkn_err_code: %d", AsynSrv_errcode); + } + StrJoin(AsynSrv_routine[0], sizeof(AsynSrv_routine), + AsynSrv_routine[0], buff); } + *entry_txt = AsynSrv_routine[0]; + AsynSrv_call_depth = 0; + AsynSrv_errcode = 0; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_GetReply: Get next reply from a reply buffer. */ - char *AsynSrv_GetReply ( +char *AsynSrv_GetReply( /* ================ -*/ struct AsynSrv__info *asyn_info, - struct RS__RespStruct *rcve_buff, - char *last_rply) { +*/ struct AsynSrv__info *asyn_info, + struct RS__RespStruct *rcve_buff, char *last_rply) +{ - char *pntr = NULL; - int i, rply_len; + char *pntr = NULL; + int i, rply_len; - if (last_rply == NULL) { /* Start with first reply? */ - /* Yes */ - asyn_info->n_replies = 1; - if (asyn_info->max_replies > 0) { - pntr = rcve_buff->u.rplys; - pntr = pntr + 1 + asyn_info->rply_hdr_len; - } - }else { /* No - get next reply */ - if (asyn_info->n_replies < asyn_info->max_replies) { /* If there is one */ - i = sscanf ((last_rply - asyn_info->rply_hdr_len - 1), - asyn_info->rply_fmt, &rply_len); - if ((i == 1) && (rply_len >= 0)) { - pntr = last_rply + rply_len + asyn_info->rply_hdr_len; - } + if (last_rply == NULL) { /* Start with first reply? */ + /* Yes */ + asyn_info->n_replies = 1; + if (asyn_info->max_replies > 0) { + pntr = rcve_buff->u.rplys; + pntr = pntr + 1 + asyn_info->rply_hdr_len; + } + } else { /* No - get next reply */ + if (asyn_info->n_replies < asyn_info->max_replies) { /* If there is one */ + i = sscanf((last_rply - asyn_info->rply_hdr_len - 1), + asyn_info->rply_fmt, &rply_len); + if ((i == 1) && (rply_len >= 0)) { + pntr = last_rply + rply_len + asyn_info->rply_hdr_len; } } - return pntr; } + return pntr; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_Open: Open a connection to an RS-232-C Server. */ - int AsynSrv_Open ( +int AsynSrv_Open( /* ============ -*/ struct AsynSrv__info *asyn_info) { +*/ struct AsynSrv__info *asyn_info) +{ - int i, status; - int my_skt; - char old_time_out[4]; - union { - char chars[4]; - int val; - } time_out; - char buff[128]; - struct RS__MsgStruct s_buff; - struct RS__RespStruct r_buff; - unsigned int oto_len, oto_status; - struct hostent *rmt_hostent; - struct in_addr *rmt_inet_addr_pntr; - struct in_addr rmt_inet_addr; - int rmt_sockname_len; - struct sockaddr_in lcl_sockname; - struct sockaddr_in rmt_sockname; + int i, status; + int my_skt; + char old_time_out[4]; + union { + char chars[4]; + int val; + } time_out; + char buff[128]; + struct RS__MsgStruct s_buff; + struct RS__RespStruct r_buff; + unsigned int oto_len, oto_status; + struct hostent *rmt_hostent; + struct in_addr *rmt_inet_addr_pntr; + struct in_addr rmt_inet_addr; + int rmt_sockname_len; + struct sockaddr_in lcl_sockname; + struct sockaddr_in rmt_sockname; - char *errtxt_ptr; - int errcode, my_errno, my_vaxc_errno; + char *errtxt_ptr; + int errcode, my_errno, my_vaxc_errno; /*-------------------------------------------------------- */ - asyn_info->skt = 0; + asyn_info->skt = 0; /*-------------------------------------------------------- ** Initialise the error info stack and pre-set the ** routine name (in case of error). */ - AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; - strcpy (AsynSrv_routine[0], "AsynSrv_Open"); - AsynSrv_call_depth = 1; + AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; + strcpy(AsynSrv_routine[0], "AsynSrv_Open"); + AsynSrv_call_depth = 1; /*-------------------------------------------------------- ** Is there room for a new AsynSrv_Open table entry? */ - if (AsynSrv_n_active >= MAX_OPEN) { - AsynSrv_errcode = AsynSrv__NO_ROOM; /* There isn't! */ - return False; - } + if (AsynSrv_n_active >= MAX_OPEN) { + AsynSrv_errcode = AsynSrv__NO_ROOM; /* There isn't! */ + return False; + } /*-------------------------------------------------------- ** See if a table entry for this connection already exists. */ - for (i = 0; i < AsynSrv_n_cnct; i++) { - if (AsynSrv_HPS_list[i].port != asyn_info->port) continue; - if (strcmp (AsynSrv_HPS_list[i].host, asyn_info->host) == 0) break; - } - if (i < AsynSrv_n_cnct) { /* Did we find an entry? */ - AsynSrv_call_depth--; /* Yes */ - AsynSrv_HPS_list[i].usage_cnt++; /* Up the usage count and .. */ - AsynSrv_active[AsynSrv_n_active] = /* .. remember the open and .. */ - asyn_info; - AsynSrv_n_active++; - asyn_info->skt = /* .. return the socket. */ - AsynSrv_HPS_list[i].skt; - if (asyn_info->chan < 0) asyn_info->chan = 0; - if (asyn_info->chan > 255) asyn_info->chan = 0; - sprintf (buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ - memcpy (asyn_info->chan_char, buff, sizeof (asyn_info->chan_char)); + for (i = 0; i < AsynSrv_n_cnct; i++) { + if (AsynSrv_HPS_list[i].port != asyn_info->port) + continue; + if (strcmp(AsynSrv_HPS_list[i].host, asyn_info->host) == 0) + break; + } + if (i < AsynSrv_n_cnct) { /* Did we find an entry? */ + AsynSrv_call_depth--; /* Yes */ + AsynSrv_HPS_list[i].usage_cnt++; /* Up the usage count and .. */ + AsynSrv_active[AsynSrv_n_active] = /* .. remember the open and .. */ + asyn_info; + AsynSrv_n_active++; + asyn_info->skt = /* .. return the socket. */ + AsynSrv_HPS_list[i].skt; + if (asyn_info->chan < 0) + asyn_info->chan = 0; + if (asyn_info->chan > 255) + asyn_info->chan = 0; + sprintf(buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ + memcpy(asyn_info->chan_char, buff, sizeof(asyn_info->chan_char)); - asyn_info->protocol_code = AsynSrv_HPS_list[i].protocol_code; - memcpy (asyn_info->protocol_id, - AsynSrv_HPS_list[i].protocol_id, - sizeof (asyn_info->protocol_id)); - asyn_info->cmnd_hdr_len = AsynSrv_HPS_list[i].cmnd_hdr_len; - sprintf (asyn_info->cmnd_fmt, "%%0%dd", asyn_info->cmnd_hdr_len); - asyn_info->rply_hdr_len = AsynSrv_HPS_list[i].rply_hdr_len; - sprintf (asyn_info->rply_fmt, "%%%dd", asyn_info->rply_hdr_len); + asyn_info->protocol_code = AsynSrv_HPS_list[i].protocol_code; + memcpy(asyn_info->protocol_id, + AsynSrv_HPS_list[i].protocol_id, + sizeof(asyn_info->protocol_id)); + asyn_info->cmnd_hdr_len = AsynSrv_HPS_list[i].cmnd_hdr_len; + sprintf(asyn_info->cmnd_fmt, "%%0%dd", asyn_info->cmnd_hdr_len); + asyn_info->rply_hdr_len = AsynSrv_HPS_list[i].rply_hdr_len; + sprintf(asyn_info->rply_fmt, "%%%dd", asyn_info->rply_hdr_len); - sprintf (buff, "%04d", AsynSrv_msec_tmo/100); /* Set dflt time-out .. - ** ..(deci-secs) */ - memcpy (asyn_info->tmo, buff, sizeof (asyn_info->tmo)); + sprintf(buff, "%04d", AsynSrv_msec_tmo / 100); /* Set dflt time-out .. + ** ..(deci-secs) */ + memcpy(asyn_info->tmo, buff, sizeof(asyn_info->tmo)); - memcpy (asyn_info->eot, /* Set dflt terminator(s) */ - AsynSrv_eot, sizeof (asyn_info->eot)); + memcpy(asyn_info->eot, /* Set dflt terminator(s) */ + AsynSrv_eot, sizeof(asyn_info->eot)); - asyn_info->max_replies = asyn_info->n_replies = 0; - return True; - } + asyn_info->max_replies = asyn_info->n_replies = 0; + return True; + } /*-------------------------------------------------------- ** There is no existing connection. Is there room for ** a new connection entry? */ - if (AsynSrv_n_cnct >= AsynSrv_MAX_LINK) { - AsynSrv_errcode = AsynSrv__NO_ROOM; /* There isn't! */ - return False; - } + if (AsynSrv_n_cnct >= AsynSrv_MAX_LINK) { + AsynSrv_errcode = AsynSrv__NO_ROOM; /* There isn't! */ + return False; + } /*-------------------------------------------------------- ** But, before going any further, do some quick checks on ** values in asyn_info. */ - if ((asyn_info->port <= 0) || - (asyn_info->port > 65535)) { - AsynSrv_errcode = AsynSrv__BAD_PAR; /* Something is bad! */ - return False; - } - if (asyn_info->chan < 0) asyn_info->chan = 0; - if (asyn_info->chan > 255) asyn_info->chan = 0; + if ((asyn_info->port <= 0) || (asyn_info->port > 65535)) { + AsynSrv_errcode = AsynSrv__BAD_PAR; /* Something is bad! */ + return False; + } + if (asyn_info->chan < 0) + asyn_info->chan = 0; + if (asyn_info->chan > 255) + asyn_info->chan = 0; /*-------------------------------------------------------- ** Set up a new connection. */ - StrJoin (AsynSrv_HPS_list[AsynSrv_n_cnct].host, - sizeof (AsynSrv_HPS_list[AsynSrv_n_cnct].host), - asyn_info->host, ""); - AsynSrv_HPS_list[AsynSrv_n_cnct].port = asyn_info->port; - /*--------------------------- + StrJoin(AsynSrv_HPS_list[AsynSrv_n_cnct].host, + sizeof(AsynSrv_HPS_list[AsynSrv_n_cnct].host), + asyn_info->host, ""); + AsynSrv_HPS_list[AsynSrv_n_cnct].port = asyn_info->port; + /*--------------------------- ** Get the Internet address of the server. */ - rmt_inet_addr.s_addr = inet_addr (asyn_info->host); - if (rmt_inet_addr.s_addr != -1) { - rmt_inet_addr_pntr = &rmt_inet_addr; - }else { - rmt_hostent = gethostbyname (asyn_info->host); - if (rmt_hostent == NULL) { - AsynSrv_errcode = AsynSrv__BAD_HOST; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save errno info */ - fprintf (stderr, "\nAsynSrv_Open/gethostbyname: Failed to get Internet " - "address of \"%s\".\n", asyn_info->host); - return False; - } - rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; + rmt_inet_addr.s_addr = inet_addr(asyn_info->host); + if (rmt_inet_addr.s_addr != -1) { + rmt_inet_addr_pntr = &rmt_inet_addr; + } else { + rmt_hostent = gethostbyname(asyn_info->host); + if (rmt_hostent == NULL) { + AsynSrv_errcode = AsynSrv__BAD_HOST; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save errno info */ + fprintf(stderr, + "\nAsynSrv_Open/gethostbyname: Failed to get Internet " + "address of \"%s\".\n", asyn_info->host); + return False; } - /*--------------------------- + rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; + } + /*--------------------------- ** Create a TCP/IP socket for connecting to server and bind it. */ - my_skt = socket (AF_INET, SOCK_STREAM, 0); - if (my_skt <= 0) { - AsynSrv_errcode = AsynSrv__BAD_SOCKET; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, "\nAsynSrv_Open/socket: Failed to create a socket.\n"); - return False; - } - lcl_sockname.sin_family = AF_INET; - lcl_sockname.sin_port = htons (0); - lcl_sockname.sin_addr.s_addr = 0; - status = bind (my_skt, (struct sockaddr *) &lcl_sockname, - sizeof (lcl_sockname)); - if (status == -1) { - close (my_skt); - AsynSrv_errcode = AsynSrv__BAD_BIND; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, "\nAsynSrv_Open/bind: Failed to bind socket.\n"); - return False; - } - /*--------------------------- + my_skt = socket(AF_INET, SOCK_STREAM, 0); + if (my_skt <= 0) { + AsynSrv_errcode = AsynSrv__BAD_SOCKET; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, "\nAsynSrv_Open/socket: Failed to create a socket.\n"); + return False; + } + lcl_sockname.sin_family = AF_INET; + lcl_sockname.sin_port = htons(0); + lcl_sockname.sin_addr.s_addr = 0; + status = bind(my_skt, (struct sockaddr *) &lcl_sockname, + sizeof(lcl_sockname)); + if (status == -1) { + close(my_skt); + AsynSrv_errcode = AsynSrv__BAD_BIND; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, "\nAsynSrv_Open/bind: Failed to bind socket.\n"); + return False; + } + /*--------------------------- ** Set short time-out (VMS systems only) */ #ifdef __VMS - oto_len = sizeof (old_time_out); /* Save current time-out first */ - oto_status = getsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - old_time_out, &oto_len); + oto_len = sizeof(old_time_out); /* Save current time-out first */ + oto_status = getsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + old_time_out, &oto_len); - if (oto_status == 0) { - time_out.val = AsynSrv_connect_tmo; /* Set new time-out */ - status = setsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - time_out.chars, sizeof (time_out)); - } + if (oto_status == 0) { + time_out.val = AsynSrv_connect_tmo; /* Set new time-out */ + status = setsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + time_out.chars, sizeof(time_out)); + } #endif - /*--------------------------- + /*--------------------------- ** Connect to RS-232-C Server. */ - rmt_sockname_len = sizeof (rmt_sockname); - rmt_sockname.sin_family = AF_INET; - rmt_sockname.sin_port = htons (asyn_info->port); - rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; - status = connect (my_skt, (struct sockaddr *) &rmt_sockname, - sizeof (rmt_sockname)); - if (status != 0) { - close (my_skt); - AsynSrv_errcode = AsynSrv__BAD_CONNECT; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, - "\nAsynSrv_Open/connect: Failed to connect to server.\n"); - perror ("AsynSrv_Open"); - return False; - } - /*--------------------------- + rmt_sockname_len = sizeof(rmt_sockname); + rmt_sockname.sin_family = AF_INET; + rmt_sockname.sin_port = htons(asyn_info->port); + rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; + status = connect(my_skt, (struct sockaddr *) &rmt_sockname, + sizeof(rmt_sockname)); + if (status != 0) { + close(my_skt); + AsynSrv_errcode = AsynSrv__BAD_CONNECT; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, + "\nAsynSrv_Open/connect: Failed to connect to server.\n"); + perror("AsynSrv_Open"); + return False; + } + /*--------------------------- ** Restore time-out (VMS only) */ #ifdef __VMS - if (oto_status == 0) { - setsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - old_time_out, oto_len); - } + if (oto_status == 0) { + setsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + old_time_out, oto_len); + } #endif - /*--------------------------------------------------- + /*--------------------------------------------------- ** Setup the defaults in the AsynSrv__info data structure. */ - asyn_info->skt = my_skt; /* Return socket number to caller */ + asyn_info->skt = my_skt; /* Return socket number to caller */ - asyn_info->protocol_code = 0; /* Ensure protocol_code set to "unknown" */ - memcpy (asyn_info->protocol_id, "\0\0\0\0", - sizeof (asyn_info->protocol_id)); - asyn_info->cmnd_hdr_len = 4; - strcpy (asyn_info->cmnd_fmt, "%04d"); - asyn_info->rply_hdr_len = 4; - strcpy (asyn_info->rply_fmt, "%4d"); + asyn_info->protocol_code = 0; /* Ensure protocol_code set to "unknown" */ + memcpy(asyn_info->protocol_id, "\0\0\0\0", + sizeof(asyn_info->protocol_id)); + asyn_info->cmnd_hdr_len = 4; + strcpy(asyn_info->cmnd_fmt, "%04d"); + asyn_info->rply_hdr_len = 4; + strcpy(asyn_info->rply_fmt, "%4d"); - sprintf (buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ - memcpy (asyn_info->chan_char, buff, sizeof (asyn_info->chan_char)); + sprintf(buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ + memcpy(asyn_info->chan_char, buff, sizeof(asyn_info->chan_char)); - sprintf (buff, "%04d", AsynSrv_msec_tmo/100); /* Set dflt time-out .. - ** .. (deci-secs) */ - memcpy (asyn_info->tmo, buff, sizeof (asyn_info->tmo)); + sprintf(buff, "%04d", AsynSrv_msec_tmo / 100); /* Set dflt time-out .. + ** .. (deci-secs) */ + memcpy(asyn_info->tmo, buff, sizeof(asyn_info->tmo)); - memcpy (asyn_info->eot, AsynSrv_eot, sizeof (asyn_info->eot)); /* Set .. - ** .. dflt terminator(s) */ - asyn_info->max_replies = 0; - asyn_info->n_replies = 0; - /* - ** Send a null command buffer to the server. This should give - ** a "protocol mismatch" error response and from this we can get - ** the actual protocol level supported by the server. - */ - status = AsynSrv_SendCmnds (asyn_info, &s_buff, &r_buff, NULL); - if (!status && (AsynSrv_errcode == AsynSrv__BAD_PROT_LVL)) { - /* - ** As expected, we got a "protocol mismatch" error. - ** Save the server's protocol level for future use. - */ - memcpy (asyn_info->protocol_id, r_buff.s_pcol_lvl, - sizeof (r_buff.s_pcol_lvl)); - if (strncmp (r_buff.s_pcol_lvl, RS__PROTOCOL_ID_V01B, - strlen (RS__PROTOCOL_ID_V01B)) == 0) { - asyn_info->protocol_code = RS__PROTOCOL_CODE_V01B; - asyn_info->cmnd_hdr_len = 4; - strcpy (asyn_info->cmnd_fmt, "%04d"); - asyn_info->rply_hdr_len = 4; - strcpy (asyn_info->rply_fmt, "%4d"); - }else if (strncmp (r_buff.s_pcol_lvl, RS__PROTOCOL_ID, - strlen (RS__PROTOCOL_ID)) == 0) { - asyn_info->protocol_code = RS__PROTOCOL_CODE; - asyn_info->cmnd_hdr_len = 2; - strcpy (asyn_info->cmnd_fmt, "%02d"); - asyn_info->rply_hdr_len = 2; - strcpy (asyn_info->rply_fmt, "%2d"); - }else { - close (my_skt); - asyn_info->skt = 0; - fprintf (stderr, - "\nAsynSrv_Open: Server protocol level is unrecognised.\n" - " Server level is \"%4s\"\n", r_buff.s_pcol_lvl); - return False; - } - }else { - close (my_skt); + memcpy(asyn_info->eot, AsynSrv_eot, sizeof(asyn_info->eot)); /* Set .. + ** .. dflt terminator(s) */ + asyn_info->max_replies = 0; + asyn_info->n_replies = 0; + /* + ** Send a null command buffer to the server. This should give + ** a "protocol mismatch" error response and from this we can get + ** the actual protocol level supported by the server. + */ + status = AsynSrv_SendCmnds(asyn_info, &s_buff, &r_buff, NULL); + if (!status && (AsynSrv_errcode == AsynSrv__BAD_PROT_LVL)) { + /* + ** As expected, we got a "protocol mismatch" error. + ** Save the server's protocol level for future use. + */ + memcpy(asyn_info->protocol_id, r_buff.s_pcol_lvl, + sizeof(r_buff.s_pcol_lvl)); + if (strncmp(r_buff.s_pcol_lvl, RS__PROTOCOL_ID_V01B, + strlen(RS__PROTOCOL_ID_V01B)) == 0) { + asyn_info->protocol_code = RS__PROTOCOL_CODE_V01B; + asyn_info->cmnd_hdr_len = 4; + strcpy(asyn_info->cmnd_fmt, "%04d"); + asyn_info->rply_hdr_len = 4; + strcpy(asyn_info->rply_fmt, "%4d"); + } else if (strncmp(r_buff.s_pcol_lvl, RS__PROTOCOL_ID, + strlen(RS__PROTOCOL_ID)) == 0) { + asyn_info->protocol_code = RS__PROTOCOL_CODE; + asyn_info->cmnd_hdr_len = 2; + strcpy(asyn_info->cmnd_fmt, "%02d"); + asyn_info->rply_hdr_len = 2; + strcpy(asyn_info->rply_fmt, "%2d"); + } else { + close(my_skt); asyn_info->skt = 0; - AsynSrv_errcode = AsynSrv__BAD_PROT_LVL; - fprintf (stderr, - "\nAsynSrv_Open: Problem getting protocol level of Server!\n"); + fprintf(stderr, + "\nAsynSrv_Open: Server protocol level is unrecognised.\n" + " Server level is \"%4s\"\n", r_buff.s_pcol_lvl); return False; } - /*--------------------------------------------------- + } else { + close(my_skt); + asyn_info->skt = 0; + AsynSrv_errcode = AsynSrv__BAD_PROT_LVL; + fprintf(stderr, + "\nAsynSrv_Open: Problem getting protocol level of Server!\n"); + return False; + } + /*--------------------------------------------------- ** Complete the setup of the connection table entry */ - AsynSrv_HPS_list[AsynSrv_n_cnct].skt = my_skt; - AsynSrv_HPS_list[AsynSrv_n_cnct].protocol_code = asyn_info->protocol_code; - memcpy (AsynSrv_HPS_list[AsynSrv_n_cnct].protocol_id, - asyn_info->protocol_id, sizeof (asyn_info->protocol_id)); - AsynSrv_HPS_list[AsynSrv_n_cnct].cmnd_hdr_len = asyn_info->cmnd_hdr_len; - AsynSrv_HPS_list[AsynSrv_n_cnct].rply_hdr_len = asyn_info->rply_hdr_len; - AsynSrv_HPS_list[AsynSrv_n_cnct].usage_cnt = 1; - AsynSrv_n_cnct++; + AsynSrv_HPS_list[AsynSrv_n_cnct].skt = my_skt; + AsynSrv_HPS_list[AsynSrv_n_cnct].protocol_code = + asyn_info->protocol_code; + memcpy(AsynSrv_HPS_list[AsynSrv_n_cnct].protocol_id, + asyn_info->protocol_id, sizeof(asyn_info->protocol_id)); + AsynSrv_HPS_list[AsynSrv_n_cnct].cmnd_hdr_len = asyn_info->cmnd_hdr_len; + AsynSrv_HPS_list[AsynSrv_n_cnct].rply_hdr_len = asyn_info->rply_hdr_len; + AsynSrv_HPS_list[AsynSrv_n_cnct].usage_cnt = 1; + AsynSrv_n_cnct++; - AsynSrv_active[AsynSrv_n_active] = /* Remember the open in case .. */ - asyn_info; /* .. there's a force-exit */ - AsynSrv_n_active++; + AsynSrv_active[AsynSrv_n_active] = /* Remember the open in case .. */ + asyn_info; /* .. there's a force-exit */ + AsynSrv_n_active++; + + AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; + AsynSrv_call_depth = 0; + return True; +} - AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; - AsynSrv_call_depth = 0; - return True; - } /* **--------------------------------------------------------------------------- ** AsynSrv_Force: Open a connection to an RS-232-C Server. ** Thereby insisting on an own socket. */ - int AsynSrv_Force ( +int AsynSrv_Force( /* ============ -*/ struct AsynSrv__info *asyn_info) { +*/ struct AsynSrv__info *asyn_info) +{ - int i, status; - int my_skt; - char old_time_out[4]; - union { - char chars[4]; - int val; - } time_out; - char buff[128]; - struct RS__MsgStruct s_buff; - struct RS__RespStruct r_buff; - unsigned int oto_len, oto_status; - struct hostent *rmt_hostent; - struct in_addr *rmt_inet_addr_pntr; - struct in_addr rmt_inet_addr; - int rmt_sockname_len; - struct sockaddr_in lcl_sockname; - struct sockaddr_in rmt_sockname; + int i, status; + int my_skt; + char old_time_out[4]; + union { + char chars[4]; + int val; + } time_out; + char buff[128]; + struct RS__MsgStruct s_buff; + struct RS__RespStruct r_buff; + unsigned int oto_len, oto_status; + struct hostent *rmt_hostent; + struct in_addr *rmt_inet_addr_pntr; + struct in_addr rmt_inet_addr; + int rmt_sockname_len; + struct sockaddr_in lcl_sockname; + struct sockaddr_in rmt_sockname; - char *errtxt_ptr; - int errcode, my_errno, my_vaxc_errno; + char *errtxt_ptr; + int errcode, my_errno, my_vaxc_errno; /*-------------------------------------------------------- */ - asyn_info->skt = 0; + asyn_info->skt = 0; /*-------------------------------------------------------- ** Initialise the error info stack and pre-set the ** routine name (in case of error). */ - AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; - strcpy (AsynSrv_routine[0], "AsynSrv_Open"); - AsynSrv_call_depth = 1; + AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; + strcpy(AsynSrv_routine[0], "AsynSrv_Open"); + AsynSrv_call_depth = 1; /*-------------------------------------------------------- ** But, before going any further, do some quick checks on ** values in asyn_info. */ - if ((asyn_info->port <= 0) || - (asyn_info->port > 65535)) { - AsynSrv_errcode = AsynSrv__BAD_PAR; /* Something is bad! */ - return False; - } - if (asyn_info->chan < 0) asyn_info->chan = 0; - if (asyn_info->chan > 255) asyn_info->chan = 0; + if ((asyn_info->port <= 0) || (asyn_info->port > 65535)) { + AsynSrv_errcode = AsynSrv__BAD_PAR; /* Something is bad! */ + return False; + } + if (asyn_info->chan < 0) + asyn_info->chan = 0; + if (asyn_info->chan > 255) + asyn_info->chan = 0; /*-------------------------------------------------------- ** Set up a new connection. */ - /*--------------------------- + /*--------------------------- ** Get the Internet address of the server. */ - rmt_inet_addr.s_addr = inet_addr (asyn_info->host); - if (rmt_inet_addr.s_addr != -1) { - rmt_inet_addr_pntr = &rmt_inet_addr; - }else { - rmt_hostent = gethostbyname (asyn_info->host); - if (rmt_hostent == NULL) { - AsynSrv_errcode = AsynSrv__BAD_HOST; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save errno info */ - fprintf (stderr, "\nAsynSrv_Open/gethostbyname: Failed to get Internet " - "address of \"%s\".\n", asyn_info->host); - return False; - } - rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; + rmt_inet_addr.s_addr = inet_addr(asyn_info->host); + if (rmt_inet_addr.s_addr != -1) { + rmt_inet_addr_pntr = &rmt_inet_addr; + } else { + rmt_hostent = gethostbyname(asyn_info->host); + if (rmt_hostent == NULL) { + AsynSrv_errcode = AsynSrv__BAD_HOST; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save errno info */ + fprintf(stderr, + "\nAsynSrv_Open/gethostbyname: Failed to get Internet " + "address of \"%s\".\n", asyn_info->host); + return False; } - /*--------------------------- + rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; + } + /*--------------------------- ** Create a TCP/IP socket for connecting to server and bind it. */ - my_skt = socket (AF_INET, SOCK_STREAM, 0); - if (my_skt <= 0) { - AsynSrv_errcode = AsynSrv__BAD_SOCKET; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, "\nAsynSrv_Open/socket: Failed to create a socket.\n"); - return False; - } - lcl_sockname.sin_family = AF_INET; - lcl_sockname.sin_port = htons (0); - lcl_sockname.sin_addr.s_addr = 0; - status = bind (my_skt, (struct sockaddr *) &lcl_sockname, - sizeof (lcl_sockname)); - if (status == -1) { - close (my_skt); - AsynSrv_errcode = AsynSrv__BAD_BIND; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, "\nAsynSrv_Open/bind: Failed to bind socket.\n"); - return False; - } - /*--------------------------- + my_skt = socket(AF_INET, SOCK_STREAM, 0); + if (my_skt <= 0) { + AsynSrv_errcode = AsynSrv__BAD_SOCKET; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, "\nAsynSrv_Open/socket: Failed to create a socket.\n"); + return False; + } + lcl_sockname.sin_family = AF_INET; + lcl_sockname.sin_port = htons(0); + lcl_sockname.sin_addr.s_addr = 0; + status = bind(my_skt, (struct sockaddr *) &lcl_sockname, + sizeof(lcl_sockname)); + if (status == -1) { + close(my_skt); + AsynSrv_errcode = AsynSrv__BAD_BIND; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, "\nAsynSrv_Open/bind: Failed to bind socket.\n"); + return False; + } + /*--------------------------- ** Set short time-out (VMS systems only) */ #ifdef __VMS - oto_len = sizeof (old_time_out); /* Save current time-out first */ - oto_status = getsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - old_time_out, &oto_len); + oto_len = sizeof(old_time_out); /* Save current time-out first */ + oto_status = getsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + old_time_out, &oto_len); - if (oto_status == 0) { - time_out.val = AsynSrv_connect_tmo; /* Set new time-out */ - status = setsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - time_out.chars, sizeof (time_out)); - } + if (oto_status == 0) { + time_out.val = AsynSrv_connect_tmo; /* Set new time-out */ + status = setsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + time_out.chars, sizeof(time_out)); + } #endif - /*--------------------------- + /*--------------------------- ** Connect to RS-232-C Server. */ - rmt_sockname_len = sizeof (rmt_sockname); - rmt_sockname.sin_family = AF_INET; - rmt_sockname.sin_port = htons (asyn_info->port); - rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; - status = connect (my_skt, (struct sockaddr *) &rmt_sockname, - sizeof (rmt_sockname)); - if (status != 0) { - close (my_skt); - AsynSrv_errcode = AsynSrv__BAD_CONNECT; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, - "\nAsynSrv_Open/connect: Failed to connect to server.\n"); - perror ("AsynSrv_Open"); - return False; - } - /*--------------------------- + rmt_sockname_len = sizeof(rmt_sockname); + rmt_sockname.sin_family = AF_INET; + rmt_sockname.sin_port = htons(asyn_info->port); + rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; + status = connect(my_skt, (struct sockaddr *) &rmt_sockname, + sizeof(rmt_sockname)); + if (status != 0) { + close(my_skt); + AsynSrv_errcode = AsynSrv__BAD_CONNECT; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, + "\nAsynSrv_Open/connect: Failed to connect to server.\n"); + perror("AsynSrv_Open"); + return False; + } + /*--------------------------- ** Restore time-out (VMS only) */ #ifdef __VMS - if (oto_status == 0) { - setsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - old_time_out, oto_len); - } + if (oto_status == 0) { + setsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + old_time_out, oto_len); + } #endif - /*--------------------------------------------------- + /*--------------------------------------------------- ** Setup the defaults in the AsynSrv__info data structure. */ - asyn_info->skt = my_skt; /* Return socket number to caller */ + asyn_info->skt = my_skt; /* Return socket number to caller */ - asyn_info->protocol_code = 0; /* Ensure protocol_code set to "unknown" */ - memcpy (asyn_info->protocol_id, "\0\0\0\0", - sizeof (asyn_info->protocol_id)); - asyn_info->cmnd_hdr_len = 4; - strcpy (asyn_info->cmnd_fmt, "%04d"); - asyn_info->rply_hdr_len = 4; - strcpy (asyn_info->rply_fmt, "%4d"); + asyn_info->protocol_code = 0; /* Ensure protocol_code set to "unknown" */ + memcpy(asyn_info->protocol_id, "\0\0\0\0", + sizeof(asyn_info->protocol_id)); + asyn_info->cmnd_hdr_len = 4; + strcpy(asyn_info->cmnd_fmt, "%04d"); + asyn_info->rply_hdr_len = 4; + strcpy(asyn_info->rply_fmt, "%4d"); - sprintf (buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ - memcpy (asyn_info->chan_char, buff, sizeof (asyn_info->chan_char)); + sprintf(buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ + memcpy(asyn_info->chan_char, buff, sizeof(asyn_info->chan_char)); - sprintf (buff, "%04d", AsynSrv_msec_tmo/100); /* Set dflt time-out .. - ** .. (deci-secs) */ - memcpy (asyn_info->tmo, buff, sizeof (asyn_info->tmo)); + sprintf(buff, "%04d", AsynSrv_msec_tmo / 100); /* Set dflt time-out .. + ** .. (deci-secs) */ + memcpy(asyn_info->tmo, buff, sizeof(asyn_info->tmo)); - memcpy (asyn_info->eot, AsynSrv_eot, sizeof (asyn_info->eot)); /* Set .. - ** .. dflt terminator(s) */ - asyn_info->max_replies = 0; - asyn_info->n_replies = 0; - /* - ** Send a null command buffer to the server. This should give - ** a "protocol mismatch" error response and from this we can get - ** the actual protocol level supported by the server. - */ - status = AsynSrv_SendCmnds (asyn_info, &s_buff, &r_buff, NULL); - if (!status && (AsynSrv_errcode == AsynSrv__BAD_PROT_LVL)) { - /* - ** As expected, we got a "protocol mismatch" error. - ** Save the server's protocol level for future use. - */ - memcpy (asyn_info->protocol_id, r_buff.s_pcol_lvl, - sizeof (r_buff.s_pcol_lvl)); - if (strncmp (r_buff.s_pcol_lvl, RS__PROTOCOL_ID_V01B, - strlen (RS__PROTOCOL_ID_V01B)) == 0) { - asyn_info->protocol_code = RS__PROTOCOL_CODE_V01B; - asyn_info->cmnd_hdr_len = 4; - strcpy (asyn_info->cmnd_fmt, "%04d"); - asyn_info->rply_hdr_len = 4; - strcpy (asyn_info->rply_fmt, "%4d"); - }else if (strncmp (r_buff.s_pcol_lvl, RS__PROTOCOL_ID, - strlen (RS__PROTOCOL_ID)) == 0) { - asyn_info->protocol_code = RS__PROTOCOL_CODE; - asyn_info->cmnd_hdr_len = 2; - strcpy (asyn_info->cmnd_fmt, "%02d"); - asyn_info->rply_hdr_len = 2; - strcpy (asyn_info->rply_fmt, "%2d"); - }else { - close (my_skt); - asyn_info->skt = 0; - fprintf (stderr, - "\nAsynSrv_Open: Server protocol level is unrecognised.\n" - " Server level is \"%4s\"\n", r_buff.s_pcol_lvl); - return False; - } - }else { - close (my_skt); + memcpy(asyn_info->eot, AsynSrv_eot, sizeof(asyn_info->eot)); /* Set .. + ** .. dflt terminator(s) */ + asyn_info->max_replies = 0; + asyn_info->n_replies = 0; + /* + ** Send a null command buffer to the server. This should give + ** a "protocol mismatch" error response and from this we can get + ** the actual protocol level supported by the server. + */ + status = AsynSrv_SendCmnds(asyn_info, &s_buff, &r_buff, NULL); + if (!status && (AsynSrv_errcode == AsynSrv__BAD_PROT_LVL)) { + /* + ** As expected, we got a "protocol mismatch" error. + ** Save the server's protocol level for future use. + */ + memcpy(asyn_info->protocol_id, r_buff.s_pcol_lvl, + sizeof(r_buff.s_pcol_lvl)); + if (strncmp(r_buff.s_pcol_lvl, RS__PROTOCOL_ID_V01B, + strlen(RS__PROTOCOL_ID_V01B)) == 0) { + asyn_info->protocol_code = RS__PROTOCOL_CODE_V01B; + asyn_info->cmnd_hdr_len = 4; + strcpy(asyn_info->cmnd_fmt, "%04d"); + asyn_info->rply_hdr_len = 4; + strcpy(asyn_info->rply_fmt, "%4d"); + } else if (strncmp(r_buff.s_pcol_lvl, RS__PROTOCOL_ID, + strlen(RS__PROTOCOL_ID)) == 0) { + asyn_info->protocol_code = RS__PROTOCOL_CODE; + asyn_info->cmnd_hdr_len = 2; + strcpy(asyn_info->cmnd_fmt, "%02d"); + asyn_info->rply_hdr_len = 2; + strcpy(asyn_info->rply_fmt, "%2d"); + } else { + close(my_skt); asyn_info->skt = 0; - AsynSrv_errcode = AsynSrv__BAD_PROT_LVL; - fprintf (stderr, - "\nAsynSrv_Open: Problem getting protocol level of Server!\n"); + fprintf(stderr, + "\nAsynSrv_Open: Server protocol level is unrecognised.\n" + " Server level is \"%4s\"\n", r_buff.s_pcol_lvl); return False; } - AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; - AsynSrv_call_depth = 0; - return True; + } else { + close(my_skt); + asyn_info->skt = 0; + AsynSrv_errcode = AsynSrv__BAD_PROT_LVL; + fprintf(stderr, + "\nAsynSrv_Open: Problem getting protocol level of Server!\n"); + return False; } + AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; + AsynSrv_call_depth = 0; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_SendCmnds: Send commands to RS232C server. */ - int AsynSrv_SendCmnds ( +int AsynSrv_SendCmnds( /* ================= -*/ struct AsynSrv__info *asyn_info, - struct RS__MsgStruct *send_buff, - struct RS__RespStruct *rcve_buff, - ...) { /* Now we have list of commands - - ** char *txt = pntr to cmnd strng - ** Terminate list with *txt = NULL. - */ - int i, status, c_len, size, max_size, ncmnds; - int bytes_to_come, bytes_left; - char *nxt_byte_ptr; - char err_text[80]; - char text[20]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - char *cmnd_lst_ptr; - /*---------------------------------------------- +*/ struct AsynSrv__info *asyn_info, + struct RS__MsgStruct *send_buff, + struct RS__RespStruct *rcve_buff, ...) +{ /* Now we have list of commands - + ** char *txt = pntr to cmnd strng + ** Terminate list with *txt = NULL. + */ + int i, status, c_len, size, max_size, ncmnds; + int bytes_to_come, bytes_left; + char *nxt_byte_ptr; + char err_text[80]; + char text[20]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + char *cmnd_lst_ptr; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_SendCmnds"); - AsynSrv_call_depth++; - } - /*---------------------------------------------- + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_SendCmnds"); + AsynSrv_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - if (asyn_info->skt <= 0) { - memset (rcve_buff->msg_size, - '0', sizeof (rcve_buff->msg_size)); - if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { - AsynSrv_errcode = AsynSrv__FORCED_CLOSED; - } - return False; + if (asyn_info->skt <= 0) { + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { + AsynSrv_errcode = AsynSrv__FORCED_CLOSED; } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Build message for server from the list of commands. */ - asyn_info->max_replies = asyn_info->n_replies = 0; + asyn_info->max_replies = asyn_info->n_replies = 0; - asyn_info->msg_id++; /* Set up an incrementing message id */ - if (asyn_info->msg_id > 9999) asyn_info->msg_id = 1; - sprintf (send_buff->msg_id, "%04d", asyn_info->msg_id); + asyn_info->msg_id++; /* Set up an incrementing message id */ + if (asyn_info->msg_id > 9999) + asyn_info->msg_id = 1; + sprintf(send_buff->msg_id, "%04d", asyn_info->msg_id); - memcpy (send_buff->c_pcol_lvl, asyn_info->protocol_id, - sizeof (send_buff->c_pcol_lvl)); + memcpy(send_buff->c_pcol_lvl, asyn_info->protocol_id, + sizeof(send_buff->c_pcol_lvl)); - memcpy (send_buff->serial_port, asyn_info->chan_char, - sizeof (send_buff->serial_port)); + memcpy(send_buff->serial_port, asyn_info->chan_char, + sizeof(send_buff->serial_port)); - memcpy (send_buff->tmo, asyn_info->tmo, sizeof (send_buff->tmo)); + memcpy(send_buff->tmo, asyn_info->tmo, sizeof(send_buff->tmo)); - memcpy (send_buff->terms, asyn_info->eot, sizeof (send_buff->terms)); + memcpy(send_buff->terms, asyn_info->eot, sizeof(send_buff->terms)); - memcpy (send_buff->n_cmnds, "0000", sizeof (send_buff->n_cmnds)); + memcpy(send_buff->n_cmnds, "0000", sizeof(send_buff->n_cmnds)); - va_start (ap, rcve_buff); /* Set up var arg machinery */ + va_start(ap, rcve_buff); /* Set up var arg machinery */ - txt_ptr = va_arg (ap, char *); /* Get pntr to next cmnd string */ - ncmnds = 0; - cmnd_lst_ptr = &send_buff->cmnds[0]; - bytes_left = sizeof (*send_buff) - - OffsetOf (struct RS__MsgStruct, cmnds[0]); + txt_ptr = va_arg(ap, char *); /* Get pntr to next cmnd string */ + ncmnds = 0; + cmnd_lst_ptr = &send_buff->cmnds[0]; + bytes_left = sizeof(*send_buff) - + OffsetOf(struct RS__MsgStruct, cmnds[0]); - while (txt_ptr != NULL) { - c_len = strlen (txt_ptr); - size = asyn_info->cmnd_hdr_len + c_len; - if (size > bytes_left) { - AsynSrv_errcode = AsynSrv__BAD_SENDLEN; /* Too much to send */ - fprintf (stderr, "\nAsynSrv_SendCmnds/send: too much to send" - " - request ignored.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - }else { - sprintf (cmnd_lst_ptr, asyn_info->cmnd_fmt, c_len); - if (cmnd_lst_ptr[asyn_info->cmnd_hdr_len] != '\0') { - AsynSrv_errcode = AsynSrv__BAD_CMND_LEN; - fprintf (stderr, "\nAsynSrv_SendCmnds/send: command too long -" - " - request ignored.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - } - cmnd_lst_ptr += asyn_info->cmnd_hdr_len; - strcpy (cmnd_lst_ptr, txt_ptr); - cmnd_lst_ptr += c_len; - ncmnds++; - bytes_left = bytes_left - size; - txt_ptr = va_arg (ap, char *); - } - } - sprintf (text, "%04d", ncmnds); - memcpy (send_buff->n_cmnds, text, sizeof (send_buff->n_cmnds)); - - size = cmnd_lst_ptr - send_buff->msg_id; - size = (size + 3) & (~3); /* Round up to multiple of 4 */ - sprintf (text, "%04d", size); - memcpy (send_buff->msg_size, text, sizeof (send_buff->msg_size)); - - size += sizeof (send_buff->msg_size); - status = send (asyn_info->skt, - (char *) send_buff, size, 0); - if (status != size) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = AsynSrv__BAD_SEND; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/send: probable network problem"); - }else if (status == -1) { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = AsynSrv__BAD_SEND_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/send: broken network pipe"); - }else { - AsynSrv_errcode = AsynSrv__BAD_SEND_NET; /* It's some other net problem */ - perror ("AsynSrv_SendCmnds/send"); - } - }else { - AsynSrv_errcode = AsynSrv__BAD_SEND_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nAsynSrv_SendCmnds/send: probable TCP/IP problem"); - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); + while (txt_ptr != NULL) { + c_len = strlen(txt_ptr); + size = asyn_info->cmnd_hdr_len + c_len; + if (size > bytes_left) { + AsynSrv_errcode = AsynSrv__BAD_SENDLEN; /* Too much to send */ + fprintf(stderr, "\nAsynSrv_SendCmnds/send: too much to send" + " - request ignored.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); return False; + } else { + sprintf(cmnd_lst_ptr, asyn_info->cmnd_fmt, c_len); + if (cmnd_lst_ptr[asyn_info->cmnd_hdr_len] != '\0') { + AsynSrv_errcode = AsynSrv__BAD_CMND_LEN; + fprintf(stderr, "\nAsynSrv_SendCmnds/send: command too long -" + " - request ignored.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + return False; + } + cmnd_lst_ptr += asyn_info->cmnd_hdr_len; + strcpy(cmnd_lst_ptr, txt_ptr); + cmnd_lst_ptr += c_len; + ncmnds++; + bytes_left = bytes_left - size; + txt_ptr = va_arg(ap, char *); } - - size = sizeof (rcve_buff->msg_size); - status = recv (asyn_info->skt, rcve_buff->msg_size, size, 0); - if (status != size) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = AsynSrv__BAD_RECV; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: probable network problem"); - }else if (status == -1) { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = AsynSrv__BAD_RECV_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: broken network pipe"); - }else { - AsynSrv_errcode = AsynSrv__BAD_RECV_NET; /* It's some other net problem */ - perror ("AsynSrv_SendCmnds/recv"); - } - }else { - AsynSrv_errcode = AsynSrv__BAD_RECV_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: probable TCP/IP problem"); - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - if (sscanf (rcve_buff->msg_size, "%4d", &bytes_to_come) != 1) { - AsynSrv_errcode = AsynSrv__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: non-BCD byte count" - " - link to server force-closed.\n"); - return False; - } - max_size = sizeof (*rcve_buff) - size; - if (bytes_to_come > max_size) { - AsynSrv_errcode = AsynSrv__BAD_RECVLEN; - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: pending message length too big" - " - flushing ...\n"); - nxt_byte_ptr = &rcve_buff->msg_size[size]; - while (bytes_to_come > 0) { /* Flush out the incoming message */ - bytes_left = bytes_to_come; - if (bytes_left > max_size) bytes_left = max_size; - status = recv (asyn_info->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - AsynSrv_errcode = AsynSrv__BAD_FLUSH; /* TCP/IP problem during flush */ - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: network problem during" - " flush.\nLink to server force-closed.\n"); - return False; - } - bytes_to_come = bytes_to_come - status; - } - fprintf (stderr, "\n flushed OK.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - }else { - nxt_byte_ptr = &rcve_buff->msg_size[size]; - bytes_left = bytes_to_come; - while (bytes_left > 0) { /* Read the rest of the response */ - status = recv (asyn_info->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = AsynSrv__BAD_RECV1; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv/1: probable network " - "problem"); - }else { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = AsynSrv__BAD_RECV1_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv/1: broken network pipe"); - }else { - AsynSrv_errcode = AsynSrv__BAD_RECV1_NET; /* It's some other net fault */ - perror ("AsynSrv_SendCmnds/recv/1"); - } - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - bytes_left = bytes_left - status; - nxt_byte_ptr = nxt_byte_ptr + status; - } - if (strncmp (asyn_info->protocol_id, rcve_buff->s_pcol_lvl, - sizeof (rcve_buff->s_pcol_lvl)) != 0) { - AsynSrv_errcode = AsynSrv__BAD_PROT_LVL; /* Protocol level is bad */ - return False; - } - if ((sscanf (rcve_buff->n_rply, "%4d", &asyn_info->max_replies) != 1) || - (asyn_info->max_replies < 0)) { - AsynSrv_errcode = AsynSrv__BAD_REPLY; /* Reply is bad */ - if (AsynSrv_call_depth < 5) { /* Add reply to routine stack */ - bytes_to_come = bytes_to_come + 4; - if (bytes_to_come >= sizeof (AsynSrv_routine[0])) - bytes_to_come = sizeof (AsynSrv_routine[0]) - 1; - for (i=0; imsg_size[i] == '\0') - rcve_buff->msg_size[i] = '.'; - } - rcve_buff->msg_size[bytes_to_come] = '\0'; - strcpy (AsynSrv_routine[AsynSrv_call_depth], rcve_buff->msg_size); - AsynSrv_call_depth++; - } - return False; - } - } - AsynSrv_call_depth--; - return True; } + sprintf(text, "%04d", ncmnds); + memcpy(send_buff->n_cmnds, text, sizeof(send_buff->n_cmnds)); + + size = cmnd_lst_ptr - send_buff->msg_id; + size = (size + 3) & (~3); /* Round up to multiple of 4 */ + sprintf(text, "%04d", size); + memcpy(send_buff->msg_size, text, sizeof(send_buff->msg_size)); + + size += sizeof(send_buff->msg_size); + status = send(asyn_info->skt, (char *) send_buff, size, 0); + if (status != size) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = AsynSrv__BAD_SEND; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmnds/send: probable network problem"); + } else if (status == -1) { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = AsynSrv__BAD_SEND_PIPE; /* Server exited (probably) */ + fprintf(stderr, "\nAsynSrv_SendCmnds/send: broken network pipe"); + } else { + AsynSrv_errcode = AsynSrv__BAD_SEND_NET; /* It's some other net problem */ + perror("AsynSrv_SendCmnds/send"); + } + } else { + AsynSrv_errcode = AsynSrv__BAD_SEND_UNKN; /* TCP/IP problems */ + fprintf(stderr, "\nAsynSrv_SendCmnds/send: probable TCP/IP problem"); + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + + size = sizeof(rcve_buff->msg_size); + status = recv(asyn_info->skt, rcve_buff->msg_size, size, 0); + if (status != size) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = AsynSrv__BAD_RECV; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmnds/recv: probable network problem"); + } else if (status == -1) { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = AsynSrv__BAD_RECV_PIPE; /* Server exited (probably) */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv: broken network pipe"); + } else { + AsynSrv_errcode = AsynSrv__BAD_RECV_NET; /* It's some other net problem */ + perror("AsynSrv_SendCmnds/recv"); + } + } else { + AsynSrv_errcode = AsynSrv__BAD_RECV_UNKN; /* TCP/IP problems */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv: probable TCP/IP problem"); + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + if (sscanf(rcve_buff->msg_size, "%4d", &bytes_to_come) != 1) { + AsynSrv_errcode = AsynSrv__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv: non-BCD byte count" + " - link to server force-closed.\n"); + return False; + } + max_size = sizeof(*rcve_buff) - size; + if (bytes_to_come > max_size) { + AsynSrv_errcode = AsynSrv__BAD_RECVLEN; + fprintf(stderr, + "\nAsynSrv_SendCmnds/recv: pending message length too big" + " - flushing ...\n"); + nxt_byte_ptr = &rcve_buff->msg_size[size]; + while (bytes_to_come > 0) { /* Flush out the incoming message */ + bytes_left = bytes_to_come; + if (bytes_left > max_size) + bytes_left = max_size; + status = recv(asyn_info->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + AsynSrv_errcode = AsynSrv__BAD_FLUSH; /* TCP/IP problem during flush */ + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv: network problem during" + " flush.\nLink to server force-closed.\n"); + return False; + } + bytes_to_come = bytes_to_come - status; + } + fprintf(stderr, "\n flushed OK.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + return False; + } else { + nxt_byte_ptr = &rcve_buff->msg_size[size]; + bytes_left = bytes_to_come; + while (bytes_left > 0) { /* Read the rest of the response */ + status = recv(asyn_info->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = AsynSrv__BAD_RECV1; /* Server exited (probably) */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv/1: probable network " + "problem"); + } else { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = AsynSrv__BAD_RECV1_PIPE; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmnds/recv/1: broken network pipe"); + } else { + AsynSrv_errcode = AsynSrv__BAD_RECV1_NET; /* It's some other net fault */ + perror("AsynSrv_SendCmnds/recv/1"); + } + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + bytes_left = bytes_left - status; + nxt_byte_ptr = nxt_byte_ptr + status; + } + if (strncmp(asyn_info->protocol_id, rcve_buff->s_pcol_lvl, + sizeof(rcve_buff->s_pcol_lvl)) != 0) { + AsynSrv_errcode = AsynSrv__BAD_PROT_LVL; /* Protocol level is bad */ + return False; + } + if ((sscanf(rcve_buff->n_rply, "%4d", &asyn_info->max_replies) != 1) || + (asyn_info->max_replies < 0)) { + AsynSrv_errcode = AsynSrv__BAD_REPLY; /* Reply is bad */ + if (AsynSrv_call_depth < 5) { /* Add reply to routine stack */ + bytes_to_come = bytes_to_come + 4; + if (bytes_to_come >= sizeof(AsynSrv_routine[0])) + bytes_to_come = sizeof(AsynSrv_routine[0]) - 1; + for (i = 0; i < bytes_to_come; i++) { + if (rcve_buff->msg_size[i] == '\0') + rcve_buff->msg_size[i] = '.'; + } + rcve_buff->msg_size[bytes_to_come] = '\0'; + strcpy(AsynSrv_routine[AsynSrv_call_depth], rcve_buff->msg_size); + AsynSrv_call_depth++; + } + return False; + } + } + AsynSrv_call_depth--; + return True; +} + /*-------------------------------------------- End of AsynSrv_Utility.C -----*/ diff --git a/hardsup/asynsrv_utility.c b/hardsup/asynsrv_utility.c index 57a28cc..60b86fd 100644 --- a/hardsup/asynsrv_utility.c +++ b/hardsup/asynsrv_utility.c @@ -630,7 +630,7 @@ #include #ifdef __VMS -#pragma nostandard /* The "$" characters in ucx$inetdef.h give trouble! */ +#pragma nostandard /* The "$" characters in ucx$inetdef.h give trouble! */ #include #include #else @@ -646,1480 +646,1613 @@ #define MAX_OPEN 64 - int AsynSrv_SendSpecCmnd ( /* A prototype for a local routine */ - struct AsynSrv__info *asyn_info, - char *cmnd); +int AsynSrv_SendSpecCmnd( /* A prototype for a local routine */ + struct AsynSrv__info *asyn_info, char *cmnd); /*-------------------------------------------------------------------------- ** Global Variables */ - static int AsynSrv_call_depth = 0; - static char AsynSrv_routine[5][64]; - static int AsynSrv_errcode = 0; - static int AsynSrv_errno, AsynSrv_vaxc_errno; - static int AsynSrv_connect_tmo = 5; /* Time-out on "connect" */ - static int AsynSrv_msec_tmo = 10000; /* Time-out for responses */ - static char AsynSrv_eot[] = {'1', '\r', '\0','\0'}; /* Terminators */ +static int AsynSrv_call_depth = 0; +static char AsynSrv_routine[5][64]; +static int AsynSrv_errcode = 0; +static int AsynSrv_errno, AsynSrv_vaxc_errno; +static int AsynSrv_connect_tmo = 5; /* Time-out on "connect" */ +static int AsynSrv_msec_tmo = 10000; /* Time-out for responses */ +static char AsynSrv_eot[] = { '1', '\r', '\0', '\0' }; /* Terminators */ + /* ** The following is the list of open connections (= number of ** active sockets). */ - static int AsynSrv_n_cnct = 0; - static struct AsynSrv_HostPortSkt AsynSrv_HPS_list[AsynSrv_MAX_LINK]; +static int AsynSrv_n_cnct = 0; +static struct AsynSrv_HostPortSkt AsynSrv_HPS_list[AsynSrv_MAX_LINK]; /* ** The following is the list of active calls to AsynSrv_Open. */ - static int AsynSrv_n_active = 0; - static struct AsynSrv__info *AsynSrv_active[MAX_OPEN]; +static int AsynSrv_n_active = 0; +static struct AsynSrv__info *AsynSrv_active[MAX_OPEN]; /* **--------------------------------------------------------------------------- ** AsynSrv_ChanClose: Send a "CLOSE CHAN" request to ** RS232C server. */ - int AsynSrv_ChanClose ( +int AsynSrv_ChanClose( /* ================= -*/ struct AsynSrv__info *asyn_info) { +*/ struct AsynSrv__info *asyn_info) +{ - int status; - char cmnd[8], rply[8]; - /*---------------------------------------------- + int status; + char cmnd[8], rply[8]; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_ChanClose"); - AsynSrv_call_depth++; - } - /*---------------------------------------------- + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_ChanClose"); + AsynSrv_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - if (asyn_info->skt <= 0) { - if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { - AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; - } - return False; + if (asyn_info->skt <= 0) { + if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { + AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Send message and get reply. */ - status = AsynSrv_SendSpecCmnd (asyn_info, "-006"); + status = AsynSrv_SendSpecCmnd(asyn_info, "-006"); + + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return status; +} - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return status; - } /* **--------------------------------------------------------------------------- ** AsynSrv_Close: Close a connection to an RS-232-C server. */ - int AsynSrv_Close ( +int AsynSrv_Close( /* ============= -*/ struct AsynSrv__info *asyn_info, - int force_flag) { +*/ struct AsynSrv__info *asyn_info, + int force_flag) +{ - int i, j, k, my_skt; - char buff[4]; - /*----------------------------------------------- + int i, j, k, my_skt; + char buff[4]; + /*----------------------------------------------- */ - if (asyn_info == NULL) return True; /* Just return if nothing to do! */ - my_skt = asyn_info->skt; - if (my_skt <= 0) return True; /* Just return if nothing to do! */ - /*----------------------------------------------- + if (asyn_info == NULL) + return True; /* Just return if nothing to do! */ + my_skt = asyn_info->skt; + if (my_skt <= 0) + return True; /* Just return if nothing to do! */ + /*----------------------------------------------- ** Pre-set the routinename (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Close"); - AsynSrv_call_depth++; - } - /*------------------------------------------------------ + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Close"); + AsynSrv_call_depth++; + } + /*------------------------------------------------------ ** Start by finding the table entry for this connection */ - for (i = 0; i < AsynSrv_n_cnct; i++) { - if (AsynSrv_HPS_list[i].skt != my_skt) continue; - if (AsynSrv_HPS_list[i].port != asyn_info->port) continue; - if (strcmp (AsynSrv_HPS_list[i].host, asyn_info->host) == 0) break; - } - if (i >= AsynSrv_n_cnct) { /* Did we find the entry? */ - AsynSrv_errcode = ASYNSRV__BAD_PAR; /* No! */ - return False; - } - /*------------------------------------------------------ + for (i = 0; i < AsynSrv_n_cnct; i++) { + if (AsynSrv_HPS_list[i].skt != my_skt) + continue; + if (AsynSrv_HPS_list[i].port != asyn_info->port) + continue; + if (strcmp(AsynSrv_HPS_list[i].host, asyn_info->host) == 0) + break; + } + if (i >= AsynSrv_n_cnct) { /* Did we find the entry? */ + AsynSrv_errcode = ASYNSRV__BAD_PAR; /* No! */ + return False; + } + /*------------------------------------------------------ ** Now find the table entry for the AsynSrvOpen call. - */ - for (j = 0; j < AsynSrv_n_active; j++) { - if ((AsynSrv_active[j] == asyn_info) && - (AsynSrv_active[j]->skt == my_skt)) { - break; - } + */ + for (j = 0; j < AsynSrv_n_active; j++) { + if ((AsynSrv_active[j] == asyn_info) && + (AsynSrv_active[j]->skt == my_skt)) { + break; } - if (j >= AsynSrv_n_active) { /* Did we find the entry? */ - AsynSrv_errcode = ASYNSRV__BAD_PAR; /* No! */ - return False; - } - /*------------------------------------------------------ + } + if (j >= AsynSrv_n_active) { /* Did we find the entry? */ + AsynSrv_errcode = ASYNSRV__BAD_PAR; /* No! */ + return False; + } + /*------------------------------------------------------ ** i is the index for the connection table entry. ** j is the index for the caller's AsynSrvOpen call entry. */ - if (AsynSrv_HPS_list[i].usage_cnt <= 0) { /* Is the connection active? */ - AsynSrv_errcode = ASYNSRV__BAD_PAR; /* No */ - return False; - } - /*------------------------------------------------------ + if (AsynSrv_HPS_list[i].usage_cnt <= 0) { /* Is the connection active? */ + AsynSrv_errcode = ASYNSRV__BAD_PAR; /* No */ + return False; + } + /*------------------------------------------------------ ** For the caller, simply set his socket number to zero, ** mark the AsynSrvOpen entry as free and decrease the ** usage count (the entries will be compressed later). */ - AsynSrv_active[j]->skt = 0; /* Mark the close .. */ - AsynSrv_active[j] = NULL; /* .. and flag entry to be removed. */ - AsynSrv_HPS_list[i].usage_cnt--; /* Decrease usage count */ - /*------------------------------------------------------ + AsynSrv_active[j]->skt = 0; /* Mark the close .. */ + AsynSrv_active[j] = NULL; /* .. and flag entry to be removed. */ + AsynSrv_HPS_list[i].usage_cnt--; /* Decrease usage count */ + /*------------------------------------------------------ ** If this is a force-close, go through all AsynSrv_Open ** entries looking for a socket match, mark them as ** free and decrease usage count. */ - if (force_flag != 0) { - for (k = 0; k < AsynSrv_n_active; k++) { - if (AsynSrv_active[k] != NULL) { - if (AsynSrv_active[k]->skt == my_skt) { - AsynSrv_active[k]->skt = -1; /* Mark the force-close */ - AsynSrv_active[k] = NULL; /* Mark entry to be removed */ - AsynSrv_HPS_list[i].usage_cnt--; /* Decrease usage count */ - } - } - } - if (AsynSrv_HPS_list[i].usage_cnt != 0) { /* Use count should now be .. */ - AsynSrv_errcode = ASYNSRV__BAD_PAR; /* .. zero or there's a bug. */ - return False; - } - } - /*------------------------------------------------------ - ** Compress the list of AsynSrv_Open entries - */ - j = 0; + if (force_flag != 0) { for (k = 0; k < AsynSrv_n_active; k++) { if (AsynSrv_active[k] != NULL) { - AsynSrv_active[j] = AsynSrv_active[k]; - j++; + if (AsynSrv_active[k]->skt == my_skt) { + AsynSrv_active[k]->skt = -1; /* Mark the force-close */ + AsynSrv_active[k] = NULL; /* Mark entry to be removed */ + AsynSrv_HPS_list[i].usage_cnt--; /* Decrease usage count */ + } } } - for (k = j; k < AsynSrv_n_active; k++) AsynSrv_active[k] = NULL; - AsynSrv_n_active = j; - /*------------------------------------------------------ + if (AsynSrv_HPS_list[i].usage_cnt != 0) { /* Use count should now be .. */ + AsynSrv_errcode = ASYNSRV__BAD_PAR; /* .. zero or there's a bug. */ + return False; + } + } + /*------------------------------------------------------ + ** Compress the list of AsynSrv_Open entries + */ + j = 0; + for (k = 0; k < AsynSrv_n_active; k++) { + if (AsynSrv_active[k] != NULL) { + AsynSrv_active[j] = AsynSrv_active[k]; + j++; + } + } + for (k = j; k < AsynSrv_n_active; k++) + AsynSrv_active[k] = NULL; + AsynSrv_n_active = j; + /*------------------------------------------------------ ** If the link is now idle, really close it and compress ** the connection table entry out of the list. */ - if (AsynSrv_HPS_list[i].usage_cnt == 0) { - send (my_skt, "-001", 4, 0); /* Tell the TCP/IP server that .. - ** .. we are about to quit. - */ - recv (my_skt, buff, sizeof (buff), 0); /* And wait for his ack */ - close (my_skt); - for (j = i; j < AsynSrv_n_cnct; j++) { - memcpy ((char *) &AsynSrv_HPS_list[j], (char *) &AsynSrv_HPS_list[j+1], - sizeof (AsynSrv_HPS_list[0])); - } - AsynSrv_HPS_list[AsynSrv_n_cnct].skt = 0; /* Invalidate the free entry */ - AsynSrv_n_cnct--; + if (AsynSrv_HPS_list[i].usage_cnt == 0) { + send(my_skt, "-001", 4, 0); /* Tell the TCP/IP server that .. + ** .. we are about to quit. + */ + recv(my_skt, buff, sizeof(buff), 0); /* And wait for his ack */ + close(my_skt); + for (j = i; j < AsynSrv_n_cnct; j++) { + memcpy((char *) &AsynSrv_HPS_list[j], + (char *) &AsynSrv_HPS_list[j + 1], + sizeof(AsynSrv_HPS_list[0])); } - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return True; + AsynSrv_HPS_list[AsynSrv_n_cnct].skt = 0; /* Invalidate the free entry */ + AsynSrv_n_cnct--; } + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_Config: Configure an open connection. */ - int AsynSrv_Config ( +int AsynSrv_Config( /* ============== -*/ struct AsynSrv__info *asyn_info, - ...) { +*/ struct AsynSrv__info *asyn_info, + ...) +{ - char buff[16], my_eot[4]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - int intval; - typedef void (*IdleHandler)(int,int); - /* - ** Pre-set the routinename (in case of error) - */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Config"); - AsynSrv_call_depth++; - } - - va_start (ap, asyn_info); /* Set up var arg machinery */ - txt_ptr = va_arg (ap, char *); /* Get pntr to first parameter ident */ - while (txt_ptr != NULL) { - if (strcmp (txt_ptr, "msecTmo") == 0) { - intval = va_arg (ap, int); - if ((intval < 0) || (intval > 999999)) { - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - sprintf (buff, "%04d", intval/100); /* Convert to ASCII as .. - ** .. deci-secs */ - memcpy (asyn_info->tmo, buff, 4); - }else if (strcmp (txt_ptr, "eot") == 0) { - txt_ptr = va_arg (ap, char *); - if (txt_ptr == NULL) { - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - memcpy (my_eot, "\0\0\0\0", 4); - switch (txt_ptr[0]) { - case '3': my_eot[3] = txt_ptr[3]; - case '2': my_eot[2] = txt_ptr[2]; - case '1': my_eot[1] = txt_ptr[1]; - case '0': - my_eot[0] = txt_ptr[0]; - break; - default: - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - memcpy (asyn_info->eot, my_eot, 4); - }else if (strcmp (txt_ptr, "idleHdl") == 0) { /* MZ. */ - asyn_info->idleHandler = va_arg (ap, IdleHandler); - }else { - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - txt_ptr = va_arg (ap, char *); /* Get pntr to next parameter ident */ - } - - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return True; + char buff[16], my_eot[4]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + int intval; + typedef void (*IdleHandler) (int, int); + /* + ** Pre-set the routinename (in case of error) + */ + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Config"); + AsynSrv_call_depth++; } + + va_start(ap, asyn_info); /* Set up var arg machinery */ + txt_ptr = va_arg(ap, char *); /* Get pntr to first parameter ident */ + while (txt_ptr != NULL) { + if (strcmp(txt_ptr, "msecTmo") == 0) { + intval = va_arg(ap, int); + if ((intval < 0) || (intval > 999999)) { + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + sprintf(buff, "%04d", intval / 100); /* Convert to ASCII as .. + ** .. deci-secs */ + memcpy(asyn_info->tmo, buff, 4); + } else if (strcmp(txt_ptr, "eot") == 0) { + txt_ptr = va_arg(ap, char *); + if (txt_ptr == NULL) { + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + memcpy(my_eot, "\0\0\0\0", 4); + switch (txt_ptr[0]) { + case '3': + my_eot[3] = txt_ptr[3]; + case '2': + my_eot[2] = txt_ptr[2]; + case '1': + my_eot[1] = txt_ptr[1]; + case '0': + my_eot[0] = txt_ptr[0]; + break; + default: + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + memcpy(asyn_info->eot, my_eot, 4); + } else if (strcmp(txt_ptr, "idleHdl") == 0) { /* MZ. */ + asyn_info->idleHandler = va_arg(ap, IdleHandler); + } else { + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + txt_ptr = va_arg(ap, char *); /* Get pntr to next parameter ident */ + } + + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_ConfigDflt: Set default values in AsynSrv_Utility ** which will be used to initialise ** structures in AsynSrv_Open. */ - int AsynSrv_ConfigDflt ( +int AsynSrv_ConfigDflt( /* ================== -*/ char *par_id, - ...) { - int i; - char buff[4]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - int intval; - /* - ** Pre-set the routinename (in case of error) - */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_ConfigDflt"); - AsynSrv_call_depth++; - } - - va_start (ap, par_id); /* Set up var arg machinery */ - txt_ptr = par_id; /* Point to first arg */ - while (txt_ptr != NULL) { - if (strcmp (txt_ptr, "tmoC") == 0) { - intval = va_arg (ap, int); - if ((intval < 1) || (intval > 3600)) { - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - AsynSrv_connect_tmo = intval; - }else if (strcmp (txt_ptr, "msecTmo") == 0) { - intval = va_arg (ap, int); - if ((intval < 0) || (intval > 999999)) { - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - AsynSrv_msec_tmo = intval; - }else if (strcmp (txt_ptr, "eot") == 0) { - txt_ptr = va_arg (ap, char *); - if (txt_ptr == NULL) { - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - switch (txt_ptr[0]) { - case '3': AsynSrv_eot[3] = txt_ptr[3]; - case '2': AsynSrv_eot[2] = txt_ptr[2]; - case '1': AsynSrv_eot[1] = txt_ptr[1]; - case '0': - AsynSrv_eot[0] = txt_ptr[0]; - break; - default: - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - switch (txt_ptr[0]) { - case '0': AsynSrv_eot[1] = '\0'; - case '1': AsynSrv_eot[2] = '\0'; - case '2': AsynSrv_eot[3] = '\0'; - } - }else { - AsynSrv_errcode = ASYNSRV__BAD_PAR; - return False; - } - txt_ptr = va_arg (ap, char *); /* Get pntr to next parameter ident */ - } - - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return True; +*/ char *par_id, + ...) +{ + int i; + char buff[4]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + int intval; + /* + ** Pre-set the routinename (in case of error) + */ + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_ConfigDflt"); + AsynSrv_call_depth++; } + + va_start(ap, par_id); /* Set up var arg machinery */ + txt_ptr = par_id; /* Point to first arg */ + while (txt_ptr != NULL) { + if (strcmp(txt_ptr, "tmoC") == 0) { + intval = va_arg(ap, int); + if ((intval < 1) || (intval > 3600)) { + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + AsynSrv_connect_tmo = intval; + } else if (strcmp(txt_ptr, "msecTmo") == 0) { + intval = va_arg(ap, int); + if ((intval < 0) || (intval > 999999)) { + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + AsynSrv_msec_tmo = intval; + } else if (strcmp(txt_ptr, "eot") == 0) { + txt_ptr = va_arg(ap, char *); + if (txt_ptr == NULL) { + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + switch (txt_ptr[0]) { + case '3': + AsynSrv_eot[3] = txt_ptr[3]; + case '2': + AsynSrv_eot[2] = txt_ptr[2]; + case '1': + AsynSrv_eot[1] = txt_ptr[1]; + case '0': + AsynSrv_eot[0] = txt_ptr[0]; + break; + default: + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + switch (txt_ptr[0]) { + case '0': + AsynSrv_eot[1] = '\0'; + case '1': + AsynSrv_eot[2] = '\0'; + case '2': + AsynSrv_eot[3] = '\0'; + } + } else { + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; + } + txt_ptr = va_arg(ap, char *); /* Get pntr to next parameter ident */ + } + + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_ErrInfo: Return detailed status from last operation. */ - void AsynSrv_ErrInfo ( +void AsynSrv_ErrInfo( /* =============== -*/ char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno) { +*/ char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno) +{ - int i, j, k; - char buff[80]; + int i, j, k; + char buff[80]; - if (AsynSrv_call_depth <= 0) { - strcpy (AsynSrv_routine[0], "AsynSrv_no_error_detected"); - *errcode = 0; - *my_errno = 0; - *vaxc_errno = 0; - }else { - if (AsynSrv_call_depth > 1) { /* Concatenate the names */ - for (i = 1; i < AsynSrv_call_depth; i++) { - strcat (AsynSrv_routine[0], "/"); - StrJoin (AsynSrv_routine[0], sizeof (AsynSrv_routine), - AsynSrv_routine[0], AsynSrv_routine[i]); - } + if (AsynSrv_call_depth <= 0) { + strcpy(AsynSrv_routine[0], "AsynSrv_no_error_detected"); + *errcode = 0; + *my_errno = 0; + *vaxc_errno = 0; + } else { + if (AsynSrv_call_depth > 1) { /* Concatenate the names */ + for (i = 1; i < AsynSrv_call_depth; i++) { + strcat(AsynSrv_routine[0], "/"); + StrJoin(AsynSrv_routine[0], sizeof(AsynSrv_routine), + AsynSrv_routine[0], AsynSrv_routine[i]); } - *errcode = AsynSrv_errcode; - *my_errno = AsynSrv_errno; - *vaxc_errno = AsynSrv_vaxc_errno; - switch (AsynSrv_errcode) { - case ASYNSRV__BAD_BIND: strcpy (buff, "/ASYNSRV__BAD_BIND"); break; - case ASYNSRV__BAD_CMND_LEN: strcpy (buff, "/ASYNSRV__BAD_CMND_LEN"); break; - case ASYNSRV__BAD_CONNECT: strcpy (buff, "/ASYNSRV__BAD_CONNECT"); break; - case ASYNSRV__BAD_FLUSH: strcpy (buff, "/ASYNSRV__BAD_FLUSH"); break; - case ASYNSRV__BAD_HOST: strcpy (buff, "/ASYNSRV__BAD_HOST"); break; - case ASYNSRV__BAD_NOT_BCD: strcpy (buff, "/ASYNSRV__BAD_NOT_BCD"); break; - case ASYNSRV__BAD_PAR: strcpy (buff, "/ASYNSRV__BAD_PAR"); break; - case ASYNSRV__BAD_PROT_LVL: strcpy (buff, "/ASYNSRV__BAD_PROT_LVL"); break; - case ASYNSRV__BAD_RECV: strcpy (buff, "/ASYNSRV__BAD_RECV"); break; - case ASYNSRV__BAD_RECV_LEN: strcpy (buff, "/ASYNSRV__BAD_RECV_LEN"); break; - case ASYNSRV__BAD_RECV_NET: strcpy (buff, "/ASYNSRV__BAD_RECV_NET"); break; - case ASYNSRV__BAD_RECV_PIPE: strcpy (buff, "/ASYNSRV__BAD_RECV_PIPE"); break; - case ASYNSRV__BAD_RECV_UNKN: strcpy (buff, "/ASYNSRV__BAD_RECV_UNKN"); break; - case ASYNSRV__BAD_RECV1: strcpy (buff, "/ASYNSRV__BAD_RECV1"); break; - case ASYNSRV__BAD_RECV1_NET: strcpy (buff, "/ASYNSRV__BAD_RECV1_NET"); break; - case ASYNSRV__BAD_RECV1_PIPE:strcpy (buff, "/ASYNSRV__BAD_RECV1_PIPE"); break; - case ASYNSRV__BAD_REPLY: strcpy (buff, "/ASYNSRV__BAD_REPLY"); break; - case ASYNSRV__BAD_SEND: strcpy (buff, "/ASYNSRV__BAD_SEND"); break; - case ASYNSRV__BAD_SEND_LEN: strcpy (buff, "/ASYNSRV__BAD_SEND_LEN"); break; - case ASYNSRV__BAD_SEND_NET: strcpy (buff, "/ASYNSRV__BAD_SEND_NET"); break; - case ASYNSRV__BAD_SEND_PIPE: strcpy (buff, "/ASYNSRV__BAD_SEND_PIPE"); break; - case ASYNSRV__BAD_SEND_UNKN: strcpy (buff, "/ASYNSRV__BAD_SEND_UNKN"); break; - case ASYNSRV__BAD_SOCKET: strcpy (buff, "/ASYNSRV__BAD_SOCKET"); break; - case ASYNSRV__FORCED_CLOSED: strcpy (buff, "/ASYNSRV__FORCED_CLOSED"); break; - case ASYNSRV__NO_ROOM: strcpy (buff, "/ASYNSRV__NO_ROOM"); break; - default: sprintf (buff, "/ASYNSRV__unkn_err_code: %d", AsynSrv_errcode); - } - StrJoin (AsynSrv_routine[0], sizeof(AsynSrv_routine), - AsynSrv_routine[0], buff); } - *entry_txt = AsynSrv_routine[0]; - AsynSrv_call_depth = 0; - AsynSrv_errcode = 0; + *errcode = AsynSrv_errcode; + *my_errno = AsynSrv_errno; + *vaxc_errno = AsynSrv_vaxc_errno; + switch (AsynSrv_errcode) { + case ASYNSRV__BAD_BIND: + strcpy(buff, "/ASYNSRV__BAD_BIND"); + break; + case ASYNSRV__BAD_CMND_LEN: + strcpy(buff, "/ASYNSRV__BAD_CMND_LEN"); + break; + case ASYNSRV__BAD_CONNECT: + strcpy(buff, "/ASYNSRV__BAD_CONNECT"); + break; + case ASYNSRV__BAD_FLUSH: + strcpy(buff, "/ASYNSRV__BAD_FLUSH"); + break; + case ASYNSRV__BAD_HOST: + strcpy(buff, "/ASYNSRV__BAD_HOST"); + break; + case ASYNSRV__BAD_NOT_BCD: + strcpy(buff, "/ASYNSRV__BAD_NOT_BCD"); + break; + case ASYNSRV__BAD_PAR: + strcpy(buff, "/ASYNSRV__BAD_PAR"); + break; + case ASYNSRV__BAD_PROT_LVL: + strcpy(buff, "/ASYNSRV__BAD_PROT_LVL"); + break; + case ASYNSRV__BAD_RECV: + strcpy(buff, "/ASYNSRV__BAD_RECV"); + break; + case ASYNSRV__BAD_RECV_LEN: + strcpy(buff, "/ASYNSRV__BAD_RECV_LEN"); + break; + case ASYNSRV__BAD_RECV_NET: + strcpy(buff, "/ASYNSRV__BAD_RECV_NET"); + break; + case ASYNSRV__BAD_RECV_PIPE: + strcpy(buff, "/ASYNSRV__BAD_RECV_PIPE"); + break; + case ASYNSRV__BAD_RECV_UNKN: + strcpy(buff, "/ASYNSRV__BAD_RECV_UNKN"); + break; + case ASYNSRV__BAD_RECV1: + strcpy(buff, "/ASYNSRV__BAD_RECV1"); + break; + case ASYNSRV__BAD_RECV1_NET: + strcpy(buff, "/ASYNSRV__BAD_RECV1_NET"); + break; + case ASYNSRV__BAD_RECV1_PIPE: + strcpy(buff, "/ASYNSRV__BAD_RECV1_PIPE"); + break; + case ASYNSRV__BAD_REPLY: + strcpy(buff, "/ASYNSRV__BAD_REPLY"); + break; + case ASYNSRV__BAD_SEND: + strcpy(buff, "/ASYNSRV__BAD_SEND"); + break; + case ASYNSRV__BAD_SEND_LEN: + strcpy(buff, "/ASYNSRV__BAD_SEND_LEN"); + break; + case ASYNSRV__BAD_SEND_NET: + strcpy(buff, "/ASYNSRV__BAD_SEND_NET"); + break; + case ASYNSRV__BAD_SEND_PIPE: + strcpy(buff, "/ASYNSRV__BAD_SEND_PIPE"); + break; + case ASYNSRV__BAD_SEND_UNKN: + strcpy(buff, "/ASYNSRV__BAD_SEND_UNKN"); + break; + case ASYNSRV__BAD_SOCKET: + strcpy(buff, "/ASYNSRV__BAD_SOCKET"); + break; + case ASYNSRV__FORCED_CLOSED: + strcpy(buff, "/ASYNSRV__FORCED_CLOSED"); + break; + case ASYNSRV__NO_ROOM: + strcpy(buff, "/ASYNSRV__NO_ROOM"); + break; + default: + sprintf(buff, "/ASYNSRV__unkn_err_code: %d", AsynSrv_errcode); + } + StrJoin(AsynSrv_routine[0], sizeof(AsynSrv_routine), + AsynSrv_routine[0], buff); } + *entry_txt = AsynSrv_routine[0]; + AsynSrv_call_depth = 0; + AsynSrv_errcode = 0; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_Flush: Send a Flush command to RS232C server. */ - int AsynSrv_Flush ( +int AsynSrv_Flush( /* ============= -*/ struct AsynSrv__info *asyn_info) { +*/ struct AsynSrv__info *asyn_info) +{ - int status; - char cmnd[8], rply[8]; - /*---------------------------------------------- + int status; + char cmnd[8], rply[8]; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Flush"); - AsynSrv_call_depth++; - } - /*---------------------------------------------- + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Flush"); + AsynSrv_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - if (asyn_info->skt <= 0) { - if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { - AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; - } - return False; + if (asyn_info->skt <= 0) { + if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { + AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Send message and get reply. */ - status = AsynSrv_SendSpecCmnd (asyn_info, "-004"); + status = AsynSrv_SendSpecCmnd(asyn_info, "-004"); + + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return status; +} - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return status; - } /* **--------------------------------------------------------------------------- ** AsynSrv_GetLenTerm: Get length and terminator of given ** reply from reply buffer. */ - int AsynSrv_GetLenTerm ( +int AsynSrv_GetLenTerm( /* ================== -*/ struct AsynSrv__info *asyn_info, - struct RS__RespStruct *rcve_buff, - char *rply, /* In: Addr of a reply as got .. - ** .. got from _GetReply */ - int *len, /* Out: The returned length */ - char *term) { /* Out: The returned t'nator */ +*/ struct AsynSrv__info *asyn_info, + struct RS__RespStruct *rcve_buff, char *rply, /* In: Addr of a reply as got .. + ** .. got from _GetReply */ + int *len, /* Out: The returned length */ + char *term) +{ /* Out: The returned t'nator */ - int i; + int i; - i = sscanf ((rply - asyn_info->rply_hdr_len - 1), asyn_info->rply_fmt, len); - *len = (i == 1) ? (*len - 2) : 0; - *term = *(rply - 1); + i = sscanf((rply - asyn_info->rply_hdr_len - 1), asyn_info->rply_fmt, + len); + *len = (i == 1) ? (*len - 2) : 0; + *term = *(rply - 1); + + if (i != 1) + return False; + return True; +} - if (i != 1) return False; - return True; - } /* **--------------------------------------------------------------------------- ** AsynSrv_GetReply: Get next reply from a reply buffer. */ - char *AsynSrv_GetReply ( +char *AsynSrv_GetReply( /* ================ -*/ struct AsynSrv__info *asyn_info, - struct RS__RespStruct *rcve_buff, - char *last_rply) { +*/ struct AsynSrv__info *asyn_info, + struct RS__RespStruct *rcve_buff, char *last_rply) +{ - char *pntr = NULL; - int i, rply_len; + char *pntr = NULL; + int i, rply_len; - if (last_rply == NULL) { /* Start with first reply? */ - /* Yes */ - asyn_info->n_replies = 1; - if (asyn_info->max_replies > 0) { - pntr = rcve_buff->u.rplys; - pntr = pntr + 1 + asyn_info->rply_hdr_len; - } - }else { /* No - get next reply */ - if (asyn_info->n_replies < asyn_info->max_replies) { /* If there is one */ - i = sscanf ((last_rply - asyn_info->rply_hdr_len - 1), - asyn_info->rply_fmt, &rply_len); - if ((i == 1) && (rply_len >= 0)) { - pntr = last_rply + rply_len + asyn_info->rply_hdr_len; - } + if (last_rply == NULL) { /* Start with first reply? */ + /* Yes */ + asyn_info->n_replies = 1; + if (asyn_info->max_replies > 0) { + pntr = rcve_buff->u.rplys; + pntr = pntr + 1 + asyn_info->rply_hdr_len; + } + } else { /* No - get next reply */ + if (asyn_info->n_replies < asyn_info->max_replies) { /* If there is one */ + i = sscanf((last_rply - asyn_info->rply_hdr_len - 1), + asyn_info->rply_fmt, &rply_len); + if ((i == 1) && (rply_len >= 0)) { + pntr = last_rply + rply_len + asyn_info->rply_hdr_len; } } - return pntr; } + return pntr; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_Open: Open a connection to an RS-232-C Server. */ - int AsynSrv_Open ( +int AsynSrv_Open( /* ============ -*/ struct AsynSrv__info *asyn_info) { +*/ struct AsynSrv__info *asyn_info) +{ - int i, status; - int my_skt; - char old_time_out[4]; - union { - char chars[4]; - int val; - } time_out; - char buff[128]; - struct RS__MsgStruct s_buff; - struct RS__RespStruct r_buff; - unsigned int oto_len, oto_status; - struct hostent *rmt_hostent; - struct in_addr *rmt_inet_addr_pntr; - struct in_addr rmt_inet_addr; - int rmt_sockname_len; - struct sockaddr_in lcl_sockname; - struct sockaddr_in rmt_sockname; + int i, status; + int my_skt; + char old_time_out[4]; + union { + char chars[4]; + int val; + } time_out; + char buff[128]; + struct RS__MsgStruct s_buff; + struct RS__RespStruct r_buff; + unsigned int oto_len, oto_status; + struct hostent *rmt_hostent; + struct in_addr *rmt_inet_addr_pntr; + struct in_addr rmt_inet_addr; + int rmt_sockname_len; + struct sockaddr_in lcl_sockname; + struct sockaddr_in rmt_sockname; - char *errtxt_ptr; - int errcode, my_errno, my_vaxc_errno; + char *errtxt_ptr; + int errcode, my_errno, my_vaxc_errno; /*-------------------------------------------------------- */ - asyn_info->skt = 0; - asyn_info->msg_id = 0; + asyn_info->skt = 0; + asyn_info->msg_id = 0; /*-------------------------------------------------------- ** Initialise the error info stack and pre-set the ** routine name (in case of error). */ - AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; - strcpy (AsynSrv_routine[0], "AsynSrv_Open"); - AsynSrv_call_depth = 1; + AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; + strcpy(AsynSrv_routine[0], "AsynSrv_Open"); + AsynSrv_call_depth = 1; /*-------------------------------------------------------- ** Is there room for a new AsynSrv_Open table entry? */ - if (AsynSrv_n_active >= MAX_OPEN) { - AsynSrv_errcode = ASYNSRV__NO_ROOM; /* There isn't! */ - return False; - } + if (AsynSrv_n_active >= MAX_OPEN) { + AsynSrv_errcode = ASYNSRV__NO_ROOM; /* There isn't! */ + return False; + } /*-------------------------------------------------------- ** See if a table entry for this connection already exists. */ - for (i = 0; i < AsynSrv_n_cnct; i++) { - if (AsynSrv_HPS_list[i].status != 0) continue; - if (AsynSrv_HPS_list[i].port != asyn_info->port) continue; - if (strcmp (AsynSrv_HPS_list[i].host, asyn_info->host) == 0) break; - } - if (i < AsynSrv_n_cnct) { /* Did we find an entry? */ - /* Yes */ - AsynSrv_HPS_list[i].usage_cnt++; /* Up the usage count and .. */ - AsynSrv_active[AsynSrv_n_active] = /* .. remember the open and .. */ - asyn_info; - AsynSrv_n_active++; - asyn_info->skt = /* .. return the socket. */ - AsynSrv_HPS_list[i].skt; - if (asyn_info->chan < 0) asyn_info->chan = 0; - if (asyn_info->chan > 255) asyn_info->chan = 0; - sprintf (buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ - memcpy (asyn_info->chan_char, buff, sizeof (asyn_info->chan_char)); + for (i = 0; i < AsynSrv_n_cnct; i++) { + if (AsynSrv_HPS_list[i].status != 0) + continue; + if (AsynSrv_HPS_list[i].port != asyn_info->port) + continue; + if (strcmp(AsynSrv_HPS_list[i].host, asyn_info->host) == 0) + break; + } + if (i < AsynSrv_n_cnct) { /* Did we find an entry? */ + /* Yes */ + AsynSrv_HPS_list[i].usage_cnt++; /* Up the usage count and .. */ + AsynSrv_active[AsynSrv_n_active] = /* .. remember the open and .. */ + asyn_info; + AsynSrv_n_active++; + asyn_info->skt = /* .. return the socket. */ + AsynSrv_HPS_list[i].skt; + if (asyn_info->chan < 0) + asyn_info->chan = 0; + if (asyn_info->chan > 255) + asyn_info->chan = 0; + sprintf(buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ + memcpy(asyn_info->chan_char, buff, sizeof(asyn_info->chan_char)); - asyn_info->protocol_code = AsynSrv_HPS_list[i].protocol_code; - memcpy (asyn_info->protocol_id, - AsynSrv_HPS_list[i].protocol_id, - sizeof (asyn_info->protocol_id)); - asyn_info->cmnd_hdr_len = AsynSrv_HPS_list[i].cmnd_hdr_len; - sprintf (asyn_info->cmnd_fmt, "%%0%dd", asyn_info->cmnd_hdr_len); - asyn_info->rply_hdr_len = AsynSrv_HPS_list[i].rply_hdr_len; - sprintf (asyn_info->rply_fmt, "%%%dd", asyn_info->rply_hdr_len); + asyn_info->protocol_code = AsynSrv_HPS_list[i].protocol_code; + memcpy(asyn_info->protocol_id, + AsynSrv_HPS_list[i].protocol_id, + sizeof(asyn_info->protocol_id)); + asyn_info->cmnd_hdr_len = AsynSrv_HPS_list[i].cmnd_hdr_len; + sprintf(asyn_info->cmnd_fmt, "%%0%dd", asyn_info->cmnd_hdr_len); + asyn_info->rply_hdr_len = AsynSrv_HPS_list[i].rply_hdr_len; + sprintf(asyn_info->rply_fmt, "%%%dd", asyn_info->rply_hdr_len); - sprintf (buff, "%04d", AsynSrv_msec_tmo/100); /* Set dflt time-out .. - ** ..(deci-secs) */ - memcpy (asyn_info->tmo, buff, sizeof (asyn_info->tmo)); + sprintf(buff, "%04d", AsynSrv_msec_tmo / 100); /* Set dflt time-out .. + ** ..(deci-secs) */ + memcpy(asyn_info->tmo, buff, sizeof(asyn_info->tmo)); - memcpy (asyn_info->eot, /* Set dflt terminator(s) */ - AsynSrv_eot, sizeof (asyn_info->eot)); + memcpy(asyn_info->eot, /* Set dflt terminator(s) */ + AsynSrv_eot, sizeof(asyn_info->eot)); - asyn_info->max_replies = asyn_info->n_replies = 0; - asyn_info->idleHandler = NULL; - AsynSrv_call_depth--; - return True; - } + asyn_info->max_replies = asyn_info->n_replies = 0; + asyn_info->idleHandler = NULL; + AsynSrv_call_depth--; + return True; + } /*-------------------------------------------------------- ** There is no existing connection. Open a new one. */ - status = AsynSrv_OpenNew (asyn_info); - if (!status) return False; + status = AsynSrv_OpenNew(asyn_info); + if (!status) + return False; /*-------------------------------------------------------- ** Allow the entry to be shared (i.e. status = 0) */ - AsynSrv_HPS_list[AsynSrv_n_cnct-1].status = 0; + AsynSrv_HPS_list[AsynSrv_n_cnct - 1].status = 0; /*-------------------------------------------------------- */ - AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; - AsynSrv_call_depth = 0; - return True; - } + AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; + AsynSrv_call_depth = 0; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_OpenNew: Open a new connection to an RS-232-C Server. */ - int AsynSrv_OpenNew ( +int AsynSrv_OpenNew( /* =============== -*/ struct AsynSrv__info *asyn_info) { +*/ struct AsynSrv__info *asyn_info) +{ - int i, status; - int my_skt; - char old_time_out[4]; - union { - char chars[4]; - int val; - } time_out; - char buff[128]; - struct RS__MsgStruct s_buff; - struct RS__RespStruct r_buff; - unsigned int oto_len, oto_status; - struct hostent *rmt_hostent; - struct in_addr *rmt_inet_addr_pntr; - struct in_addr rmt_inet_addr; - int rmt_sockname_len; - struct sockaddr_in lcl_sockname; - struct sockaddr_in rmt_sockname; + int i, status; + int my_skt; + char old_time_out[4]; + union { + char chars[4]; + int val; + } time_out; + char buff[128]; + struct RS__MsgStruct s_buff; + struct RS__RespStruct r_buff; + unsigned int oto_len, oto_status; + struct hostent *rmt_hostent; + struct in_addr *rmt_inet_addr_pntr; + struct in_addr rmt_inet_addr; + int rmt_sockname_len; + struct sockaddr_in lcl_sockname; + struct sockaddr_in rmt_sockname; - char *errtxt_ptr; - int errcode, my_errno, my_vaxc_errno; + char *errtxt_ptr; + int errcode, my_errno, my_vaxc_errno; /*-------------------------------------------------------- */ - asyn_info->skt = 0; - asyn_info->msg_id = 0; + asyn_info->skt = 0; + asyn_info->msg_id = 0; /*-------------------------------------------------------- ** Initialise the error info stack and pre-set the ** routine name (in case of error). */ - AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; - if ((AsynSrv_call_depth == 1) && - (strcmp (AsynSrv_routine[0], "AsynSrv_Open") == 0)) { - strcpy (AsynSrv_routine[1], "AsynSrv_OpenNew"); - AsynSrv_call_depth = 2; - }else { - strcpy (AsynSrv_routine[0], "AsynSrv_OpenNew"); - AsynSrv_call_depth = 1; - } + AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; + if ((AsynSrv_call_depth == 1) && + (strcmp(AsynSrv_routine[0], "AsynSrv_Open") == 0)) { + strcpy(AsynSrv_routine[1], "AsynSrv_OpenNew"); + AsynSrv_call_depth = 2; + } else { + strcpy(AsynSrv_routine[0], "AsynSrv_OpenNew"); + AsynSrv_call_depth = 1; + } /*-------------------------------------------------------- ** Is there room for a new AsynSrv_Open table entry? */ - if (AsynSrv_n_active >= MAX_OPEN) { - AsynSrv_errcode = ASYNSRV__NO_ROOM; /* There isn't! */ - return False; - } + if (AsynSrv_n_active >= MAX_OPEN) { + AsynSrv_errcode = ASYNSRV__NO_ROOM; /* There isn't! */ + return False; + } /*-------------------------------------------------------- ** Is there room for a new connection entry? */ - if (AsynSrv_n_cnct >= AsynSrv_MAX_LINK) { - AsynSrv_errcode = ASYNSRV__NO_ROOM; /* There isn't! */ - return False; - } + if (AsynSrv_n_cnct >= AsynSrv_MAX_LINK) { + AsynSrv_errcode = ASYNSRV__NO_ROOM; /* There isn't! */ + return False; + } /*-------------------------------------------------------- ** There's room for a new connection but, before going any ** further, do some quick checks on values in asyn_info. */ - if ((asyn_info->port <= 0) || - (asyn_info->port > 65535) || - (asyn_info->chan < 0) || - (asyn_info->chan > 255)) { - AsynSrv_errcode = ASYNSRV__BAD_PAR; /* Something is bad! */ - return False; - } + if ((asyn_info->port <= 0) || + (asyn_info->port > 65535) || + (asyn_info->chan < 0) || (asyn_info->chan > 255)) { + AsynSrv_errcode = ASYNSRV__BAD_PAR; /* Something is bad! */ + return False; + } /*-------------------------------------------------------- ** Set up a new connection. */ - StrJoin (AsynSrv_HPS_list[AsynSrv_n_cnct].host, - sizeof (AsynSrv_HPS_list[AsynSrv_n_cnct].host), - asyn_info->host, ""); - AsynSrv_HPS_list[AsynSrv_n_cnct].port = asyn_info->port; - /*--------------------------- + StrJoin(AsynSrv_HPS_list[AsynSrv_n_cnct].host, + sizeof(AsynSrv_HPS_list[AsynSrv_n_cnct].host), + asyn_info->host, ""); + AsynSrv_HPS_list[AsynSrv_n_cnct].port = asyn_info->port; + /*--------------------------- ** Get the Internet address of the server. */ - rmt_inet_addr.s_addr = inet_addr (asyn_info->host); - if (rmt_inet_addr.s_addr != -1) { - rmt_inet_addr_pntr = &rmt_inet_addr; - }else { - rmt_hostent = gethostbyname (asyn_info->host); - if (rmt_hostent == NULL) { - AsynSrv_errcode = ASYNSRV__BAD_HOST; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save errno info */ - fprintf (stderr, "\nAsynSrv_OpenNew/gethostbyname: Failed to get Internet " - "address of \"%s\".\n", asyn_info->host); - return False; - } - rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; + rmt_inet_addr.s_addr = inet_addr(asyn_info->host); + if (rmt_inet_addr.s_addr != -1) { + rmt_inet_addr_pntr = &rmt_inet_addr; + } else { + rmt_hostent = gethostbyname(asyn_info->host); + if (rmt_hostent == NULL) { + AsynSrv_errcode = ASYNSRV__BAD_HOST; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save errno info */ + fprintf(stderr, + "\nAsynSrv_OpenNew/gethostbyname: Failed to get Internet " + "address of \"%s\".\n", asyn_info->host); + return False; } - /*--------------------------- + rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; + } + /*--------------------------- ** Create a TCP/IP socket for connecting to server and bind it. */ - my_skt = socket (AF_INET, SOCK_STREAM, 0); - if (my_skt <= 0) { - AsynSrv_errcode = ASYNSRV__BAD_SOCKET; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, "\nAsynSrv_OpenNew/socket: Failed to create a socket.\n"); - return False; - } - lcl_sockname.sin_family = AF_INET; - lcl_sockname.sin_port = htons (0); - lcl_sockname.sin_addr.s_addr = 0; - status = bind (my_skt, (struct sockaddr *) &lcl_sockname, - sizeof (lcl_sockname)); - if (status == -1) { - close (my_skt); - AsynSrv_errcode = ASYNSRV__BAD_BIND; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, "\nAsynSrv_OpenNew/bind: Failed to bind socket.\n"); - return False; - } - /*--------------------------- + my_skt = socket(AF_INET, SOCK_STREAM, 0); + if (my_skt <= 0) { + AsynSrv_errcode = ASYNSRV__BAD_SOCKET; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, + "\nAsynSrv_OpenNew/socket: Failed to create a socket.\n"); + return False; + } + lcl_sockname.sin_family = AF_INET; + lcl_sockname.sin_port = htons(0); + lcl_sockname.sin_addr.s_addr = 0; + status = bind(my_skt, (struct sockaddr *) &lcl_sockname, + sizeof(lcl_sockname)); + if (status == -1) { + close(my_skt); + AsynSrv_errcode = ASYNSRV__BAD_BIND; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, "\nAsynSrv_OpenNew/bind: Failed to bind socket.\n"); + return False; + } + /*--------------------------- ** Set short time-out (VMS systems only) */ #ifdef __VMS - oto_len = sizeof (old_time_out); /* Save current time-out first */ - oto_status = getsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - old_time_out, &oto_len); + oto_len = sizeof(old_time_out); /* Save current time-out first */ + oto_status = getsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + old_time_out, &oto_len); - if (oto_status == 0) { - time_out.val = AsynSrv_connect_tmo; /* Set new time-out */ - status = setsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - time_out.chars, sizeof (time_out)); - } + if (oto_status == 0) { + time_out.val = AsynSrv_connect_tmo; /* Set new time-out */ + status = setsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + time_out.chars, sizeof(time_out)); + } #endif - /*--------------------------- + /*--------------------------- ** Connect to RS-232-C Server. */ - rmt_sockname_len = sizeof (rmt_sockname); - rmt_sockname.sin_family = AF_INET; - rmt_sockname.sin_port = htons (asyn_info->port); - rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; - status = connect (my_skt, (struct sockaddr *) &rmt_sockname, - sizeof (rmt_sockname)); - if (status != 0) { - close (my_skt); - AsynSrv_errcode = ASYNSRV__BAD_CONNECT; - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ - fprintf (stderr, - "\nAsynSrv_OpenNew/connect: Failed to connect to server.\n"); - perror ("AsynSrv_OpenNew"); - return False; - } - /*--------------------------- + rmt_sockname_len = sizeof(rmt_sockname); + rmt_sockname.sin_family = AF_INET; + rmt_sockname.sin_port = htons(asyn_info->port); + rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; + status = connect(my_skt, (struct sockaddr *) &rmt_sockname, + sizeof(rmt_sockname)); + if (status != 0) { + close(my_skt); + AsynSrv_errcode = ASYNSRV__BAD_CONNECT; + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); /* Save the errno info */ + fprintf(stderr, + "\nAsynSrv_OpenNew/connect: Failed to connect to server.\n"); + perror("AsynSrv_OpenNew"); + return False; + } + /*--------------------------- ** Restore time-out (VMS only) */ #ifdef __VMS - if (oto_status == 0) { - setsockopt (my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, - old_time_out, oto_len); - } + if (oto_status == 0) { + setsockopt(my_skt, IPPROTO_TCP, UCX$C_TCP_PROBE_IDLE, + old_time_out, oto_len); + } #endif - /*--------------------------------------------------- + /*--------------------------------------------------- ** Setup the defaults in the AsynSrv__info data structure. */ - asyn_info->skt = my_skt; /* Return socket number to caller */ + asyn_info->skt = my_skt; /* Return socket number to caller */ - asyn_info->protocol_code = 0; /* Ensure protocol_code set to "unknown" */ - memcpy (asyn_info->protocol_id, "\0\0\0\0", - sizeof (asyn_info->protocol_id)); - asyn_info->cmnd_hdr_len = 4; - strcpy (asyn_info->cmnd_fmt, "%04d"); - asyn_info->rply_hdr_len = 4; - strcpy (asyn_info->rply_fmt, "%4d"); + asyn_info->protocol_code = 0; /* Ensure protocol_code set to "unknown" */ + memcpy(asyn_info->protocol_id, "\0\0\0\0", + sizeof(asyn_info->protocol_id)); + asyn_info->cmnd_hdr_len = 4; + strcpy(asyn_info->cmnd_fmt, "%04d"); + asyn_info->rply_hdr_len = 4; + strcpy(asyn_info->rply_fmt, "%4d"); - sprintf (buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ - memcpy (asyn_info->chan_char, buff, sizeof (asyn_info->chan_char)); + sprintf(buff, "%04d", asyn_info->chan); /* Convert channel # to ASCII */ + memcpy(asyn_info->chan_char, buff, sizeof(asyn_info->chan_char)); - sprintf (buff, "%04d", AsynSrv_msec_tmo/100); /* Set dflt time-out .. - ** .. (deci-secs) */ - memcpy (asyn_info->tmo, buff, sizeof (asyn_info->tmo)); + sprintf(buff, "%04d", AsynSrv_msec_tmo / 100); /* Set dflt time-out .. + ** .. (deci-secs) */ + memcpy(asyn_info->tmo, buff, sizeof(asyn_info->tmo)); - memcpy (asyn_info->eot, AsynSrv_eot, sizeof (asyn_info->eot)); /* Set .. - ** .. dflt terminator(s) */ - asyn_info->max_replies = 0; - asyn_info->n_replies = 0; - asyn_info->idleHandler = NULL; - /* - ** Send a null command buffer to the server. This should give - ** a "protocol mismatch" error response and from this we can get - ** the actual protocol level supported by the server. - */ - status = AsynSrv_SendCmnds (asyn_info, &s_buff, &r_buff, NULL); - if (!status && (AsynSrv_errcode == ASYNSRV__BAD_PROT_LVL)) { - /* - ** As expected, we got a "protocol mismatch" error. - ** Save the server's protocol level for future use. - */ - memcpy (asyn_info->protocol_id, r_buff.s_pcol_lvl, - sizeof (r_buff.s_pcol_lvl)); - if (strncmp (r_buff.s_pcol_lvl, RS__PROTOCOL_ID_V01B, - strlen (RS__PROTOCOL_ID_V01B)) == 0) { - asyn_info->protocol_code = RS__PROTOCOL_CODE_V01B; - asyn_info->cmnd_hdr_len = 4; - strcpy (asyn_info->cmnd_fmt, "%04d"); - asyn_info->rply_hdr_len = 4; - strcpy (asyn_info->rply_fmt, "%4d"); - }else if (strncmp (r_buff.s_pcol_lvl, RS__PROTOCOL_ID, - strlen (RS__PROTOCOL_ID)) == 0) { - asyn_info->protocol_code = RS__PROTOCOL_CODE; - asyn_info->cmnd_hdr_len = 2; - strcpy (asyn_info->cmnd_fmt, "%02d"); - asyn_info->rply_hdr_len = 2; - strcpy (asyn_info->rply_fmt, "%2d"); - }else { - close (my_skt); - asyn_info->skt = 0; - fprintf (stderr, - "\nAsynSrv_OpenNew: Server protocol level is unrecognised.\n" - " Server level is \"%4s\"\n", r_buff.s_pcol_lvl); - return False; - } - }else { - close (my_skt); + memcpy(asyn_info->eot, AsynSrv_eot, sizeof(asyn_info->eot)); /* Set .. + ** .. dflt terminator(s) */ + asyn_info->max_replies = 0; + asyn_info->n_replies = 0; + asyn_info->idleHandler = NULL; + /* + ** Send a null command buffer to the server. This should give + ** a "protocol mismatch" error response and from this we can get + ** the actual protocol level supported by the server. + */ + status = AsynSrv_SendCmnds(asyn_info, &s_buff, &r_buff, NULL); + if (!status && (AsynSrv_errcode == ASYNSRV__BAD_PROT_LVL)) { + /* + ** As expected, we got a "protocol mismatch" error. + ** Save the server's protocol level for future use. + */ + memcpy(asyn_info->protocol_id, r_buff.s_pcol_lvl, + sizeof(r_buff.s_pcol_lvl)); + if (strncmp(r_buff.s_pcol_lvl, RS__PROTOCOL_ID_V01B, + strlen(RS__PROTOCOL_ID_V01B)) == 0) { + asyn_info->protocol_code = RS__PROTOCOL_CODE_V01B; + asyn_info->cmnd_hdr_len = 4; + strcpy(asyn_info->cmnd_fmt, "%04d"); + asyn_info->rply_hdr_len = 4; + strcpy(asyn_info->rply_fmt, "%4d"); + } else if (strncmp(r_buff.s_pcol_lvl, RS__PROTOCOL_ID, + strlen(RS__PROTOCOL_ID)) == 0) { + asyn_info->protocol_code = RS__PROTOCOL_CODE; + asyn_info->cmnd_hdr_len = 2; + strcpy(asyn_info->cmnd_fmt, "%02d"); + asyn_info->rply_hdr_len = 2; + strcpy(asyn_info->rply_fmt, "%2d"); + } else { + close(my_skt); asyn_info->skt = 0; - AsynSrv_errcode = ASYNSRV__BAD_PROT_LVL; - fprintf (stderr, - "\nAsynSrv_OpenNew: Problem getting protocol level of Server!\n"); + fprintf(stderr, + "\nAsynSrv_OpenNew: Server protocol level is unrecognised.\n" + " Server level is \"%4s\"\n", r_buff.s_pcol_lvl); return False; } - /*--------------------------------------------------- + } else { + close(my_skt); + asyn_info->skt = 0; + AsynSrv_errcode = ASYNSRV__BAD_PROT_LVL; + fprintf(stderr, + "\nAsynSrv_OpenNew: Problem getting protocol level of Server!\n"); + return False; + } + /*--------------------------------------------------- ** Complete the setup of the connection table entry */ - AsynSrv_HPS_list[AsynSrv_n_cnct].skt = my_skt; - AsynSrv_HPS_list[AsynSrv_n_cnct].protocol_code = asyn_info->protocol_code; - memcpy (AsynSrv_HPS_list[AsynSrv_n_cnct].protocol_id, - asyn_info->protocol_id, sizeof (asyn_info->protocol_id)); - AsynSrv_HPS_list[AsynSrv_n_cnct].cmnd_hdr_len = asyn_info->cmnd_hdr_len; - AsynSrv_HPS_list[AsynSrv_n_cnct].rply_hdr_len = asyn_info->rply_hdr_len; - AsynSrv_HPS_list[AsynSrv_n_cnct].usage_cnt = 1; - AsynSrv_HPS_list[AsynSrv_n_cnct].status = 1; - AsynSrv_n_cnct++; + AsynSrv_HPS_list[AsynSrv_n_cnct].skt = my_skt; + AsynSrv_HPS_list[AsynSrv_n_cnct].protocol_code = + asyn_info->protocol_code; + memcpy(AsynSrv_HPS_list[AsynSrv_n_cnct].protocol_id, + asyn_info->protocol_id, sizeof(asyn_info->protocol_id)); + AsynSrv_HPS_list[AsynSrv_n_cnct].cmnd_hdr_len = asyn_info->cmnd_hdr_len; + AsynSrv_HPS_list[AsynSrv_n_cnct].rply_hdr_len = asyn_info->rply_hdr_len; + AsynSrv_HPS_list[AsynSrv_n_cnct].usage_cnt = 1; + AsynSrv_HPS_list[AsynSrv_n_cnct].status = 1; + AsynSrv_n_cnct++; - AsynSrv_active[AsynSrv_n_active] = /* Remember the open in case .. */ - asyn_info; /* .. there's a force-exit */ - AsynSrv_n_active++; + AsynSrv_active[AsynSrv_n_active] = /* Remember the open in case .. */ + asyn_info; /* .. there's a force-exit */ + AsynSrv_n_active++; + + AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; + AsynSrv_call_depth--; + return True; +} - AsynSrv_errcode = AsynSrv_errno = AsynSrv_vaxc_errno = 0; - AsynSrv_call_depth--; - return True; - } /* **--------------------------------------------------------------------------- ** AsynSrv_SendCmnds: Send commands to RS232C server. */ - int AsynSrv_SendCmnds ( +int AsynSrv_SendCmnds( /* ================= -*/ struct AsynSrv__info *asyn_info, - struct RS__MsgStruct *send_buff, - struct RS__RespStruct *rcve_buff, - ...) { /* Now we have list of commands - - ** char *txt = pntr to cmnd strng - ** Terminate list with *txt = NULL. - */ - int i, status, c_len, size, max_size, ncmnds; - int bytes_to_come = 1, bytes_left; - char *nxt_byte_ptr; - char err_text[80]; - char text[20]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - char *cmnd_lst_ptr; - /*---------------------------------------------- +*/ struct AsynSrv__info *asyn_info, + struct RS__MsgStruct *send_buff, + struct RS__RespStruct *rcve_buff, ...) +{ /* Now we have list of commands - + ** char *txt = pntr to cmnd strng + ** Terminate list with *txt = NULL. + */ + int i, status, c_len, size, max_size, ncmnds; + int bytes_to_come = 1, bytes_left; + char *nxt_byte_ptr; + char err_text[80]; + char text[20]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + char *cmnd_lst_ptr; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_SendCmnds"); - AsynSrv_call_depth++; - } - /*---------------------------------------------- + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_SendCmnds"); + AsynSrv_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - if (asyn_info->skt <= 0) { - memset (rcve_buff, - '0', sizeof (*rcve_buff)); - if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { - AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; - } - return False; + if (asyn_info->skt <= 0) { + memset(rcve_buff, '0', sizeof(*rcve_buff)); + if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { + AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Build message for server from the list of commands. */ - asyn_info->max_replies = asyn_info->n_replies = 0; + asyn_info->max_replies = asyn_info->n_replies = 0; - asyn_info->msg_id++; /* Set up an incrementing message id */ - if (asyn_info->msg_id > 9999) asyn_info->msg_id = 1; - sprintf (send_buff->msg_id, "%04d", asyn_info->msg_id); + asyn_info->msg_id++; /* Set up an incrementing message id */ + if (asyn_info->msg_id > 9999) + asyn_info->msg_id = 1; + sprintf(send_buff->msg_id, "%04d", asyn_info->msg_id); - memcpy (send_buff->c_pcol_lvl, asyn_info->protocol_id, - sizeof (send_buff->c_pcol_lvl)); + memcpy(send_buff->c_pcol_lvl, asyn_info->protocol_id, + sizeof(send_buff->c_pcol_lvl)); - memcpy (send_buff->serial_port, asyn_info->chan_char, - sizeof (send_buff->serial_port)); + memcpy(send_buff->serial_port, asyn_info->chan_char, + sizeof(send_buff->serial_port)); - memcpy (send_buff->tmo, asyn_info->tmo, sizeof (send_buff->tmo)); + memcpy(send_buff->tmo, asyn_info->tmo, sizeof(send_buff->tmo)); - memcpy (send_buff->terms, asyn_info->eot, sizeof (send_buff->terms)); + memcpy(send_buff->terms, asyn_info->eot, sizeof(send_buff->terms)); - memcpy (send_buff->n_cmnds, "0000", sizeof (send_buff->n_cmnds)); + memcpy(send_buff->n_cmnds, "0000", sizeof(send_buff->n_cmnds)); - va_start (ap, rcve_buff); /* Set up var arg machinery */ + va_start(ap, rcve_buff); /* Set up var arg machinery */ - txt_ptr = va_arg (ap, char *); /* Get pntr to next cmnd string */ - ncmnds = 0; - cmnd_lst_ptr = &send_buff->cmnds[0]; - bytes_left = sizeof (*send_buff) - - OffsetOf (struct RS__MsgStruct, cmnds[0]); + txt_ptr = va_arg(ap, char *); /* Get pntr to next cmnd string */ + ncmnds = 0; + cmnd_lst_ptr = &send_buff->cmnds[0]; + bytes_left = sizeof(*send_buff) - + OffsetOf(struct RS__MsgStruct, cmnds[0]); - while (txt_ptr != NULL) { - c_len = strlen (txt_ptr); - size = asyn_info->cmnd_hdr_len + c_len; - if (size > bytes_left) { - AsynSrv_errcode = ASYNSRV__BAD_SEND_LEN; /* Too much to send */ - fprintf (stderr, "\nAsynSrv_SendCmnds/send: too much to send" - " - request ignored.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - }else { - sprintf (cmnd_lst_ptr, asyn_info->cmnd_fmt, c_len); - if (cmnd_lst_ptr[asyn_info->cmnd_hdr_len] != '\0') { - AsynSrv_errcode = ASYNSRV__BAD_CMND_LEN; - fprintf (stderr, "\nAsynSrv_SendCmnds/send: command too long -" - " - request ignored.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - } - cmnd_lst_ptr += asyn_info->cmnd_hdr_len; - strcpy (cmnd_lst_ptr, txt_ptr); - cmnd_lst_ptr += c_len; - ncmnds++; - bytes_left = bytes_left - size; - txt_ptr = va_arg (ap, char *); - } - } - sprintf (text, "%04d", ncmnds); - memcpy (send_buff->n_cmnds, text, sizeof (send_buff->n_cmnds)); - - size = cmnd_lst_ptr - send_buff->msg_id; - size = (size + 3) & (~3); /* Round up to multiple of 4 */ - sprintf (text, "%04d", size); - memcpy (send_buff->msg_size, text, sizeof (send_buff->msg_size)); - - size += sizeof (send_buff->msg_size); - status = send (asyn_info->skt, - (char *) send_buff, size, 0); - if (status != size) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = ASYNSRV__BAD_SEND; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/send: probable network problem"); - }else if (status == -1) { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = ASYNSRV__BAD_SEND_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/send: broken network pipe"); - }else { - AsynSrv_errcode = ASYNSRV__BAD_SEND_NET; /* It's some other net problem */ - perror ("AsynSrv_SendCmnds/send"); - } - }else { - AsynSrv_errcode = ASYNSRV__BAD_SEND_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nAsynSrv_SendCmnds/send: probable TCP/IP problem"); - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); + while (txt_ptr != NULL) { + c_len = strlen(txt_ptr); + size = asyn_info->cmnd_hdr_len + c_len; + if (size > bytes_left) { + AsynSrv_errcode = ASYNSRV__BAD_SEND_LEN; /* Too much to send */ + fprintf(stderr, "\nAsynSrv_SendCmnds/send: too much to send" + " - request ignored.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); return False; - } - - if (asyn_info->idleHandler != NULL) { /* MZ. */ - sscanf(asyn_info->tmo, "%4d", &i); /* Decode timeout from ASCII .. - ** .. encoded deci-sec */ - asyn_info->idleHandler (i*150, asyn_info->skt); /* Wait for an event .. - ** .. on asyn_info->skt or a .. - ** .. timeout of 1.5*tmo */ - } - - memset (rcve_buff,0, sizeof (*rcve_buff)); - size = sizeof (rcve_buff->msg_size); - memset(rcve_buff->msg_size,0,size); - status = recv (asyn_info->skt, rcve_buff->msg_size, size, 0); - if (status != size) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = ASYNSRV__BAD_RECV; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: probable network problem"); - }else if (status == -1) { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = ASYNSRV__BAD_RECV_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: broken network pipe"); - }else { - AsynSrv_errcode = ASYNSRV__BAD_RECV_NET; /* It's some other net problem */ - perror ("AsynSrv_SendCmnds/recv"); - } - }else { - AsynSrv_errcode = ASYNSRV__BAD_RECV_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: probable TCP/IP problem"); + } else { + sprintf(cmnd_lst_ptr, asyn_info->cmnd_fmt, c_len); + if (cmnd_lst_ptr[asyn_info->cmnd_hdr_len] != '\0') { + AsynSrv_errcode = ASYNSRV__BAD_CMND_LEN; + fprintf(stderr, "\nAsynSrv_SendCmnds/send: command too long -" + " - request ignored.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + return False; } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; + cmnd_lst_ptr += asyn_info->cmnd_hdr_len; + strcpy(cmnd_lst_ptr, txt_ptr); + cmnd_lst_ptr += c_len; + ncmnds++; + bytes_left = bytes_left - size; + txt_ptr = va_arg(ap, char *); } - if (sscanf (rcve_buff->msg_size, "%4d", &bytes_to_come) != 1) { - AsynSrv_errcode = ASYNSRV__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: non-BCD byte count" - " - link to server force-closed.\n"); - return False; - } - max_size = sizeof (*rcve_buff) - size; - if (bytes_to_come > max_size) { - AsynSrv_errcode = ASYNSRV__BAD_RECV_LEN; - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: pending message length too big" - " - flushing ...\n"); - nxt_byte_ptr = &rcve_buff->msg_size[size]; - while (bytes_to_come > 0) { /* Flush out the incoming message */ - bytes_left = bytes_to_come; - if (bytes_left > max_size) bytes_left = max_size; - status = recv (asyn_info->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - AsynSrv_errcode = ASYNSRV__BAD_FLUSH; /* TCP/IP problem during flush */ - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv: network problem during" - " flush.\nLink to server force-closed.\n"); - return False; - } - bytes_to_come = bytes_to_come - status; - } - fprintf (stderr, "\n flushed OK.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - }else { - nxt_byte_ptr = &rcve_buff->msg_size[size]; - bytes_left = bytes_to_come; - while (bytes_left > 0) { /* Read the rest of the response */ - status = recv (asyn_info->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = ASYNSRV__BAD_RECV1; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv/1: probable network " - "problem"); - }else { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = ASYNSRV__BAD_RECV1_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmnds/recv/1: broken network pipe"); - }else { - AsynSrv_errcode = ASYNSRV__BAD_RECV1_NET; /* It's some other net fault */ - perror ("AsynSrv_SendCmnds/recv/1"); - } - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - bytes_left = bytes_left - status; - nxt_byte_ptr = nxt_byte_ptr + status; - } - if (strncmp (asyn_info->protocol_id, rcve_buff->s_pcol_lvl, - sizeof (rcve_buff->s_pcol_lvl)) != 0) { - AsynSrv_errcode = ASYNSRV__BAD_PROT_LVL; /* Protocol level is bad */ - return False; - } - if ((sscanf (rcve_buff->n_rply, "%4d", &asyn_info->max_replies) != 1) || - (asyn_info->max_replies < 0)) { - AsynSrv_errcode = ASYNSRV__BAD_REPLY; /* Reply is bad */ - if (AsynSrv_call_depth < 5) { /* Add reply to routine stack */ - bytes_to_come = bytes_to_come + 4; - if (bytes_to_come >= sizeof (AsynSrv_routine[0])) - bytes_to_come = sizeof (AsynSrv_routine[0]) - 1; - for (i=0; imsg_size[i] == '\0') - rcve_buff->msg_size[i] = '.'; - } - rcve_buff->msg_size[bytes_to_come] = '\0'; - strcpy (AsynSrv_routine[AsynSrv_call_depth], rcve_buff->msg_size); - AsynSrv_call_depth++; - } - return False; - } - } - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return True; } + sprintf(text, "%04d", ncmnds); + memcpy(send_buff->n_cmnds, text, sizeof(send_buff->n_cmnds)); + + size = cmnd_lst_ptr - send_buff->msg_id; + size = (size + 3) & (~3); /* Round up to multiple of 4 */ + sprintf(text, "%04d", size); + memcpy(send_buff->msg_size, text, sizeof(send_buff->msg_size)); + + size += sizeof(send_buff->msg_size); + status = send(asyn_info->skt, (char *) send_buff, size, 0); + if (status != size) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = ASYNSRV__BAD_SEND; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmnds/send: probable network problem"); + } else if (status == -1) { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = ASYNSRV__BAD_SEND_PIPE; /* Server exited (probably) */ + fprintf(stderr, "\nAsynSrv_SendCmnds/send: broken network pipe"); + } else { + AsynSrv_errcode = ASYNSRV__BAD_SEND_NET; /* It's some other net problem */ + perror("AsynSrv_SendCmnds/send"); + } + } else { + AsynSrv_errcode = ASYNSRV__BAD_SEND_UNKN; /* TCP/IP problems */ + fprintf(stderr, "\nAsynSrv_SendCmnds/send: probable TCP/IP problem"); + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + + if (asyn_info->idleHandler != NULL) { /* MZ. */ + sscanf(asyn_info->tmo, "%4d", &i); /* Decode timeout from ASCII .. + ** .. encoded deci-sec */ + asyn_info->idleHandler(i * 150, asyn_info->skt); /* Wait for an event .. + ** .. on asyn_info->skt or a .. + ** .. timeout of 1.5*tmo */ + } + + memset(rcve_buff, 0, sizeof(*rcve_buff)); + size = sizeof(rcve_buff->msg_size); + memset(rcve_buff->msg_size, 0, size); + status = recv(asyn_info->skt, rcve_buff->msg_size, size, 0); + if (status != size) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = ASYNSRV__BAD_RECV; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmnds/recv: probable network problem"); + } else if (status == -1) { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = ASYNSRV__BAD_RECV_PIPE; /* Server exited (probably) */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv: broken network pipe"); + } else { + AsynSrv_errcode = ASYNSRV__BAD_RECV_NET; /* It's some other net problem */ + perror("AsynSrv_SendCmnds/recv"); + } + } else { + AsynSrv_errcode = ASYNSRV__BAD_RECV_UNKN; /* TCP/IP problems */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv: probable TCP/IP problem"); + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + if (sscanf(rcve_buff->msg_size, "%4d", &bytes_to_come) != 1) { + AsynSrv_errcode = ASYNSRV__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv: non-BCD byte count" + " - link to server force-closed.\n"); + return False; + } + max_size = sizeof(*rcve_buff) - size; + if (bytes_to_come > max_size) { + AsynSrv_errcode = ASYNSRV__BAD_RECV_LEN; + fprintf(stderr, + "\nAsynSrv_SendCmnds/recv: pending message length too big" + " - flushing ...\n"); + nxt_byte_ptr = &rcve_buff->msg_size[size]; + while (bytes_to_come > 0) { /* Flush out the incoming message */ + bytes_left = bytes_to_come; + if (bytes_left > max_size) + bytes_left = max_size; + status = recv(asyn_info->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + AsynSrv_errcode = ASYNSRV__BAD_FLUSH; /* TCP/IP problem during flush */ + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv: network problem during" + " flush.\nLink to server force-closed.\n"); + return False; + } + bytes_to_come = bytes_to_come - status; + } + fprintf(stderr, "\n flushed OK.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + return False; + } else { + nxt_byte_ptr = &rcve_buff->msg_size[size]; + bytes_left = bytes_to_come; + while (bytes_left > 0) { /* Read the rest of the response */ + status = recv(asyn_info->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = ASYNSRV__BAD_RECV1; /* Server exited (probably) */ + fprintf(stderr, "\nAsynSrv_SendCmnds/recv/1: probable network " + "problem"); + } else { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = ASYNSRV__BAD_RECV1_PIPE; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmnds/recv/1: broken network pipe"); + } else { + AsynSrv_errcode = ASYNSRV__BAD_RECV1_NET; /* It's some other net fault */ + perror("AsynSrv_SendCmnds/recv/1"); + } + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + bytes_left = bytes_left - status; + nxt_byte_ptr = nxt_byte_ptr + status; + } + if (strncmp(asyn_info->protocol_id, rcve_buff->s_pcol_lvl, + sizeof(rcve_buff->s_pcol_lvl)) != 0) { + AsynSrv_errcode = ASYNSRV__BAD_PROT_LVL; /* Protocol level is bad */ + return False; + } + if ((sscanf(rcve_buff->n_rply, "%4d", &asyn_info->max_replies) != 1) || + (asyn_info->max_replies < 0)) { + AsynSrv_errcode = ASYNSRV__BAD_REPLY; /* Reply is bad */ + if (AsynSrv_call_depth < 5) { /* Add reply to routine stack */ + bytes_to_come = bytes_to_come + 4; + if (bytes_to_come >= sizeof(AsynSrv_routine[0])) + bytes_to_come = sizeof(AsynSrv_routine[0]) - 1; + for (i = 0; i < bytes_to_come; i++) { + if (rcve_buff->msg_size[i] == '\0') + rcve_buff->msg_size[i] = '.'; + } + rcve_buff->msg_size[bytes_to_come] = '\0'; + strcpy(AsynSrv_routine[AsynSrv_call_depth], rcve_buff->msg_size); + AsynSrv_call_depth++; + } + return False; + } + } + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_SendCmndsBig: Same as AsynSrv_SendCmnds but with ** user defined buffer sizes. */ - int AsynSrv_SendCmndsBig ( +int AsynSrv_SendCmndsBig( /* ==================== -*/ struct AsynSrv__info *asyn_info, - struct RS__MsgStruct *send_buff, - int send_buff_size, - struct RS__RespStruct *rcve_buff, - int rcve_buff_size, - ...) { /* Now we have list of commands - - ** char *txt = pntr to cmnd strng - ** Terminate list with *txt = NULL. - */ - int i, status, size, max_size, ncmnds; - int bytes_to_come, bytes_left; - char *nxt_byte_ptr; - char err_text[80]; - char text[20]; - va_list ap; /* Pointer to variable args */ - int *c_len, s_len; - char *txt_ptr; - char *cmnd_lst_ptr; - /*---------------------------------------------- +*/ struct AsynSrv__info *asyn_info, + struct RS__MsgStruct *send_buff, + int send_buff_size, + struct RS__RespStruct *rcve_buff, + int rcve_buff_size, ...) +{ /* Now we have list of commands - + ** char *txt = pntr to cmnd strng + ** Terminate list with *txt = NULL. + */ + int i, status, size, max_size, ncmnds; + int bytes_to_come, bytes_left; + char *nxt_byte_ptr; + char err_text[80]; + char text[20]; + va_list ap; /* Pointer to variable args */ + int *c_len, s_len; + char *txt_ptr; + char *cmnd_lst_ptr; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_SendCmndsBig"); - AsynSrv_call_depth++; - } - /*---------------------------------------------- + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_SendCmndsBig"); + AsynSrv_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - if (asyn_info->skt <= 0) { - memset (rcve_buff->msg_size, - '0', sizeof (rcve_buff->msg_size)); - if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { - AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; - } - return False; + if (asyn_info->skt <= 0) { + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { + AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Build message for server from the list of commands. */ - if (send_buff_size < 64 || rcve_buff_size < 64) { - AsynSrv_errcode = ASYNSRV__BAD_PAR; return False;} - - asyn_info->max_replies = asyn_info->n_replies = 0; - - asyn_info->msg_id++; /* Set up an incrementing message id */ - if (asyn_info->msg_id > 9999) asyn_info->msg_id = 1; - sprintf (send_buff->msg_id, "%04d", asyn_info->msg_id); - - memcpy (send_buff->c_pcol_lvl, asyn_info->protocol_id, - sizeof (send_buff->c_pcol_lvl)); - - memcpy (send_buff->serial_port, asyn_info->chan_char, - sizeof (send_buff->serial_port)); - - memcpy (send_buff->tmo, asyn_info->tmo, sizeof (send_buff->tmo)); - - memcpy (send_buff->terms, asyn_info->eot, sizeof (send_buff->terms)); - - memcpy (send_buff->n_cmnds, "0000", sizeof (send_buff->n_cmnds)); - - va_start (ap, rcve_buff_size); /* Set up var arg machinery */ - - c_len = va_arg (ap, int *); /* Get pntr to length of next cmnd string */ - ncmnds = 0; - cmnd_lst_ptr = &send_buff->cmnds[0]; - bytes_left = send_buff_size - - OffsetOf (struct RS__MsgStruct, cmnds[0]); - - while (c_len != NULL) { - txt_ptr = va_arg (ap, char *); - s_len = *c_len; - if (s_len <= 0) s_len = strlen (txt_ptr); - size = asyn_info->cmnd_hdr_len + s_len; - if (size > bytes_left) { - AsynSrv_errcode = ASYNSRV__BAD_SEND_LEN; /* Too much to send */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/send: too much to send" - " - request ignored.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - } - sprintf (cmnd_lst_ptr, asyn_info->cmnd_fmt, s_len); - if (cmnd_lst_ptr[asyn_info->cmnd_hdr_len] != '\0') { - AsynSrv_errcode = ASYNSRV__BAD_CMND_LEN; - fprintf (stderr, "\nAsynSrv_SendCmndsBig/send: command too long -" - " - request ignored.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - } - cmnd_lst_ptr += asyn_info->cmnd_hdr_len; - memcpy (cmnd_lst_ptr, txt_ptr, s_len); - cmnd_lst_ptr += s_len; - ncmnds++; - bytes_left = bytes_left - size; - c_len = va_arg (ap, int *); - } - sprintf (text, "%04d", ncmnds); - memcpy (send_buff->n_cmnds, text, sizeof (send_buff->n_cmnds)); - - size = cmnd_lst_ptr - send_buff->msg_id; - size = (size + 3) & (~3); /* Round up to multiple of 4 */ - sprintf (text, "%04d", size); - memcpy (send_buff->msg_size, text, sizeof (send_buff->msg_size)); - - size += sizeof (send_buff->msg_size); - status = send (asyn_info->skt, - (char *) send_buff, size, 0); - if (status != size) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = ASYNSRV__BAD_SEND; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/send: probable network problem"); - }else if (status == -1) { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = ASYNSRV__BAD_SEND_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/send: broken network pipe"); - }else { - AsynSrv_errcode = ASYNSRV__BAD_SEND_NET; /* It's some other net problem */ - perror ("AsynSrv_SendCmndsBig/send"); - } - }else { - AsynSrv_errcode = ASYNSRV__BAD_SEND_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/send: probable TCP/IP problem"); - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - - size = sizeof (rcve_buff->msg_size); - status = recv (asyn_info->skt, rcve_buff->msg_size, size, 0); - if (status != size) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = ASYNSRV__BAD_RECV; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/recv: probable network problem"); - }else if (status == -1) { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = ASYNSRV__BAD_RECV_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/recv: broken network pipe"); - }else { - AsynSrv_errcode = ASYNSRV__BAD_RECV_NET; /* It's some other net problem */ - perror ("AsynSrv_SendCmndsBig/recv"); - } - }else { - AsynSrv_errcode = ASYNSRV__BAD_RECV_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/recv: probable TCP/IP problem"); - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - if (sscanf (rcve_buff->msg_size, "%4d", &bytes_to_come) != 1) { - AsynSrv_errcode = ASYNSRV__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/recv: non-BCD byte count" - " - link to server force-closed.\n"); - return False; - } - max_size = rcve_buff_size - size; - if (bytes_to_come > max_size) { - AsynSrv_errcode = ASYNSRV__BAD_RECV_LEN; - fprintf (stderr, "\nAsynSrv_SendCmndsBig/recv: pending message length " - "too big - flushing ...\n"); - nxt_byte_ptr = &rcve_buff->msg_size[size]; - while (bytes_to_come > 0) { /* Flush out the incoming message */ - bytes_left = bytes_to_come; - if (bytes_left > max_size) bytes_left = max_size; - status = recv (asyn_info->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - AsynSrv_errcode = ASYNSRV__BAD_FLUSH; /* TCP/IP problem during flush */ - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/recv: network problem during" - " flush.\nLink to server force-closed.\n"); - return False; - } - bytes_to_come = bytes_to_come - status; - } - fprintf (stderr, "\n flushed OK.\n"); - memset (rcve_buff->msg_size, '0', sizeof (rcve_buff->msg_size)); - return False; - }else { - nxt_byte_ptr = &rcve_buff->msg_size[size]; - bytes_left = bytes_to_come; - while (bytes_left > 0) { /* Read the rest of the response */ - status = recv (asyn_info->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = ASYNSRV__BAD_RECV1; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/recv/1: probable network " - "problem"); - }else { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = ASYNSRV__BAD_RECV1_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendCmndsBig/recv/1: broken network pipe"); - }else { - AsynSrv_errcode = ASYNSRV__BAD_RECV1_NET; /* It's some other net fault */ - perror ("AsynSrv_SendCmndsBig/recv/1"); - } - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - bytes_left = bytes_left - status; - nxt_byte_ptr = nxt_byte_ptr + status; - } - if (strncmp (asyn_info->protocol_id, rcve_buff->s_pcol_lvl, - sizeof (rcve_buff->s_pcol_lvl)) != 0) { - AsynSrv_errcode = ASYNSRV__BAD_PROT_LVL; /* Protocol level is bad */ - return False; - } - if ((sscanf (rcve_buff->n_rply, "%4d", &asyn_info->max_replies) != 1) || - (asyn_info->max_replies < 0)) { - AsynSrv_errcode = ASYNSRV__BAD_REPLY; /* Reply is bad */ - if (AsynSrv_call_depth < 5) { /* Add reply to routine stack */ - bytes_to_come = bytes_to_come + 4; - if (bytes_to_come >= sizeof (AsynSrv_routine[0])) - bytes_to_come = sizeof (AsynSrv_routine[0]) - 1; - for (i=0; imsg_size[i] == '\0') - rcve_buff->msg_size[i] = '.'; - } - rcve_buff->msg_size[bytes_to_come] = '\0'; - strcpy (AsynSrv_routine[AsynSrv_call_depth], rcve_buff->msg_size); - AsynSrv_call_depth++; - } - return False; - } - } - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return True; + if (send_buff_size < 64 || rcve_buff_size < 64) { + AsynSrv_errcode = ASYNSRV__BAD_PAR; + return False; } + + asyn_info->max_replies = asyn_info->n_replies = 0; + + asyn_info->msg_id++; /* Set up an incrementing message id */ + if (asyn_info->msg_id > 9999) + asyn_info->msg_id = 1; + sprintf(send_buff->msg_id, "%04d", asyn_info->msg_id); + + memcpy(send_buff->c_pcol_lvl, asyn_info->protocol_id, + sizeof(send_buff->c_pcol_lvl)); + + memcpy(send_buff->serial_port, asyn_info->chan_char, + sizeof(send_buff->serial_port)); + + memcpy(send_buff->tmo, asyn_info->tmo, sizeof(send_buff->tmo)); + + memcpy(send_buff->terms, asyn_info->eot, sizeof(send_buff->terms)); + + memcpy(send_buff->n_cmnds, "0000", sizeof(send_buff->n_cmnds)); + + va_start(ap, rcve_buff_size); /* Set up var arg machinery */ + + c_len = va_arg(ap, int *); /* Get pntr to length of next cmnd string */ + ncmnds = 0; + cmnd_lst_ptr = &send_buff->cmnds[0]; + bytes_left = send_buff_size - OffsetOf(struct RS__MsgStruct, cmnds[0]); + + while (c_len != NULL) { + txt_ptr = va_arg(ap, char *); + s_len = *c_len; + if (s_len <= 0) + s_len = strlen(txt_ptr); + size = asyn_info->cmnd_hdr_len + s_len; + if (size > bytes_left) { + AsynSrv_errcode = ASYNSRV__BAD_SEND_LEN; /* Too much to send */ + fprintf(stderr, "\nAsynSrv_SendCmndsBig/send: too much to send" + " - request ignored.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + return False; + } + sprintf(cmnd_lst_ptr, asyn_info->cmnd_fmt, s_len); + if (cmnd_lst_ptr[asyn_info->cmnd_hdr_len] != '\0') { + AsynSrv_errcode = ASYNSRV__BAD_CMND_LEN; + fprintf(stderr, "\nAsynSrv_SendCmndsBig/send: command too long -" + " - request ignored.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + return False; + } + cmnd_lst_ptr += asyn_info->cmnd_hdr_len; + memcpy(cmnd_lst_ptr, txt_ptr, s_len); + cmnd_lst_ptr += s_len; + ncmnds++; + bytes_left = bytes_left - size; + c_len = va_arg(ap, int *); + } + sprintf(text, "%04d", ncmnds); + memcpy(send_buff->n_cmnds, text, sizeof(send_buff->n_cmnds)); + + size = cmnd_lst_ptr - send_buff->msg_id; + size = (size + 3) & (~3); /* Round up to multiple of 4 */ + sprintf(text, "%04d", size); + memcpy(send_buff->msg_size, text, sizeof(send_buff->msg_size)); + + size += sizeof(send_buff->msg_size); + status = send(asyn_info->skt, (char *) send_buff, size, 0); + if (status != size) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = ASYNSRV__BAD_SEND; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/send: probable network problem"); + } else if (status == -1) { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = ASYNSRV__BAD_SEND_PIPE; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/send: broken network pipe"); + } else { + AsynSrv_errcode = ASYNSRV__BAD_SEND_NET; /* It's some other net problem */ + perror("AsynSrv_SendCmndsBig/send"); + } + } else { + AsynSrv_errcode = ASYNSRV__BAD_SEND_UNKN; /* TCP/IP problems */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/send: probable TCP/IP problem"); + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + + size = sizeof(rcve_buff->msg_size); + status = recv(asyn_info->skt, rcve_buff->msg_size, size, 0); + if (status != size) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = ASYNSRV__BAD_RECV; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/recv: probable network problem"); + } else if (status == -1) { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = ASYNSRV__BAD_RECV_PIPE; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/recv: broken network pipe"); + } else { + AsynSrv_errcode = ASYNSRV__BAD_RECV_NET; /* It's some other net problem */ + perror("AsynSrv_SendCmndsBig/recv"); + } + } else { + AsynSrv_errcode = ASYNSRV__BAD_RECV_UNKN; /* TCP/IP problems */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/recv: probable TCP/IP problem"); + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + if (sscanf(rcve_buff->msg_size, "%4d", &bytes_to_come) != 1) { + AsynSrv_errcode = ASYNSRV__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, "\nAsynSrv_SendCmndsBig/recv: non-BCD byte count" + " - link to server force-closed.\n"); + return False; + } + max_size = rcve_buff_size - size; + if (bytes_to_come > max_size) { + AsynSrv_errcode = ASYNSRV__BAD_RECV_LEN; + fprintf(stderr, "\nAsynSrv_SendCmndsBig/recv: pending message length " + "too big - flushing ...\n"); + nxt_byte_ptr = &rcve_buff->msg_size[size]; + while (bytes_to_come > 0) { /* Flush out the incoming message */ + bytes_left = bytes_to_come; + if (bytes_left > max_size) + bytes_left = max_size; + status = recv(asyn_info->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + AsynSrv_errcode = ASYNSRV__BAD_FLUSH; /* TCP/IP problem during flush */ + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/recv: network problem during" + " flush.\nLink to server force-closed.\n"); + return False; + } + bytes_to_come = bytes_to_come - status; + } + fprintf(stderr, "\n flushed OK.\n"); + memset(rcve_buff->msg_size, '0', sizeof(rcve_buff->msg_size)); + return False; + } else { + nxt_byte_ptr = &rcve_buff->msg_size[size]; + bytes_left = bytes_to_come; + while (bytes_left > 0) { /* Read the rest of the response */ + status = recv(asyn_info->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = ASYNSRV__BAD_RECV1; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/recv/1: probable network " + "problem"); + } else { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = ASYNSRV__BAD_RECV1_PIPE; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendCmndsBig/recv/1: broken network pipe"); + } else { + AsynSrv_errcode = ASYNSRV__BAD_RECV1_NET; /* It's some other net fault */ + perror("AsynSrv_SendCmndsBig/recv/1"); + } + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + bytes_left = bytes_left - status; + nxt_byte_ptr = nxt_byte_ptr + status; + } + if (strncmp(asyn_info->protocol_id, rcve_buff->s_pcol_lvl, + sizeof(rcve_buff->s_pcol_lvl)) != 0) { + AsynSrv_errcode = ASYNSRV__BAD_PROT_LVL; /* Protocol level is bad */ + return False; + } + if ((sscanf(rcve_buff->n_rply, "%4d", &asyn_info->max_replies) != 1) || + (asyn_info->max_replies < 0)) { + AsynSrv_errcode = ASYNSRV__BAD_REPLY; /* Reply is bad */ + if (AsynSrv_call_depth < 5) { /* Add reply to routine stack */ + bytes_to_come = bytes_to_come + 4; + if (bytes_to_come >= sizeof(AsynSrv_routine[0])) + bytes_to_come = sizeof(AsynSrv_routine[0]) - 1; + for (i = 0; i < bytes_to_come; i++) { + if (rcve_buff->msg_size[i] == '\0') + rcve_buff->msg_size[i] = '.'; + } + rcve_buff->msg_size[bytes_to_come] = '\0'; + strcpy(AsynSrv_routine[AsynSrv_call_depth], rcve_buff->msg_size); + AsynSrv_call_depth++; + } + return False; + } + } + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_SendSpecCmnd: Send a "special" command to an ** RS232C server. */ - int AsynSrv_SendSpecCmnd ( +int AsynSrv_SendSpecCmnd( /* ==================== -*/ struct AsynSrv__info *asyn_info, - char *cmnd) { +*/ struct AsynSrv__info *asyn_info, + char *cmnd) +{ - int status; - char rply[8]; - /*---------------------------------------------- + int status; + char rply[8]; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_SendSpecCmnd"); - AsynSrv_call_depth++; - } - /*---------------------------------------------- + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_SendSpecCmnd"); + AsynSrv_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - if (asyn_info->skt <= 0) { - if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { - AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; - } - return False; + if (asyn_info->skt <= 0) { + if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { + AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Send the message to the server. */ - status = send (asyn_info->skt, cmnd, 4, 0); - if (status != 4) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = ASYNSRV__BAD_SEND; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendSpecCmnd/send: probable network problem"); - }else if (status == -1) { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = ASYNSRV__BAD_SEND_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendSpecCmnd/send: broken network pipe"); - }else { - AsynSrv_errcode = ASYNSRV__BAD_SEND_NET; /* It's some other net problem */ - perror ("AsynSrv_SendSpecCmnd/send"); - } - }else { - AsynSrv_errcode = ASYNSRV__BAD_SEND_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nAsynSrv_SendSpecCmnd/send: probable TCP/IP problem"); + status = send(asyn_info->skt, cmnd, 4, 0); + if (status != 4) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = ASYNSRV__BAD_SEND; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendSpecCmnd/send: probable network problem"); + } else if (status == -1) { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = ASYNSRV__BAD_SEND_PIPE; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendSpecCmnd/send: broken network pipe"); + } else { + AsynSrv_errcode = ASYNSRV__BAD_SEND_NET; /* It's some other net problem */ + perror("AsynSrv_SendSpecCmnd/send"); } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; + } else { + AsynSrv_errcode = ASYNSRV__BAD_SEND_UNKN; /* TCP/IP problems */ + fprintf(stderr, + "\nAsynSrv_SendSpecCmnd/send: probable TCP/IP problem"); } - - status = recv (asyn_info->skt, rply, 4, 0); - if (status != 4) { - GetErrno (&AsynSrv_errno, &AsynSrv_vaxc_errno); - if (status == 0) { - AsynSrv_errcode = ASYNSRV__BAD_RECV; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendSpecCmnd/recv: probable network problem"); - }else if (status == -1) { - if (AsynSrv_errno == EPIPE) { - AsynSrv_errcode = ASYNSRV__BAD_RECV_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nAsynSrv_SendSpecCmnd/recv: broken network pipe"); - }else { - AsynSrv_errcode = ASYNSRV__BAD_RECV_NET; /* It's some other net problem */ - perror ("AsynSrv_SendSpecCmnd/recv"); - } - }else { - AsynSrv_errcode = ASYNSRV__BAD_RECV_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nAsynSrv_SendSpecCmnd/recv: probable TCP/IP problem"); - } - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - if (memcmp (cmnd, rply, 4) != 0) { - AsynSrv_errcode = ASYNSRV__BAD_NOT_BCD; /* Message not echoed OK */ - AsynSrv_Close (asyn_info, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nAsynSrv_SendSpecCmnd/recv: command not echoed correctly" - " - link to server force-closed.\n"); - return False; - } - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return True; + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; } + + status = recv(asyn_info->skt, rply, 4, 0); + if (status != 4) { + GetErrno(&AsynSrv_errno, &AsynSrv_vaxc_errno); + if (status == 0) { + AsynSrv_errcode = ASYNSRV__BAD_RECV; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendSpecCmnd/recv: probable network problem"); + } else if (status == -1) { + if (AsynSrv_errno == EPIPE) { + AsynSrv_errcode = ASYNSRV__BAD_RECV_PIPE; /* Server exited (probably) */ + fprintf(stderr, + "\nAsynSrv_SendSpecCmnd/recv: broken network pipe"); + } else { + AsynSrv_errcode = ASYNSRV__BAD_RECV_NET; /* It's some other net problem */ + perror("AsynSrv_SendSpecCmnd/recv"); + } + } else { + AsynSrv_errcode = ASYNSRV__BAD_RECV_UNKN; /* TCP/IP problems */ + fprintf(stderr, + "\nAsynSrv_SendSpecCmnd/recv: probable TCP/IP problem"); + } + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + if (memcmp(cmnd, rply, 4) != 0) { + AsynSrv_errcode = ASYNSRV__BAD_NOT_BCD; /* Message not echoed OK */ + AsynSrv_Close(asyn_info, True); /* Force close TCP/IP connection */ + fprintf(stderr, + "\nAsynSrv_SendSpecCmnd/recv: command not echoed correctly" + " - link to server force-closed.\n"); + return False; + } + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** AsynSrv_Trace: Send a "TRACE" request to RS232C server. */ - int AsynSrv_Trace ( +int AsynSrv_Trace( /* ============= -*/ struct AsynSrv__info *asyn_info, - int state) { +*/ struct AsynSrv__info *asyn_info, + int state) +{ - int status; - char cmnd[8], rply[8]; - /*---------------------------------------------- + int status; + char cmnd[8], rply[8]; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Trace"); - AsynSrv_call_depth++; - } - /*---------------------------------------------- + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Trace"); + AsynSrv_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - if (asyn_info->skt <= 0) { - if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { - AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; - } - return False; + if (asyn_info->skt <= 0) { + if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { + AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Select message for server according to value of state. */ - if (state) { - strcpy (cmnd, "-002"); - }else { - strcpy (cmnd, "-003"); - } - /*---------------------------------------------- + if (state) { + strcpy(cmnd, "-002"); + } else { + strcpy(cmnd, "-003"); + } + /*---------------------------------------------- ** Send message and get reply. */ - status = AsynSrv_SendSpecCmnd (asyn_info, cmnd); + status = AsynSrv_SendSpecCmnd(asyn_info, cmnd); + + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return status; +} - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return status; - } /* **--------------------------------------------------------------------------- ** AsynSrv_Trace_Write: Send a Trace_Write command to ** RS232C server. */ - int AsynSrv_Trace_Write ( +int AsynSrv_Trace_Write( /* =================== -*/ struct AsynSrv__info *asyn_info) { +*/ struct AsynSrv__info *asyn_info) +{ - int status; - char cmnd[8], rply[8]; - /*---------------------------------------------- + int status; + char cmnd[8], rply[8]; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { - strcpy (AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Trace_Write"); - AsynSrv_call_depth++; - } - /*---------------------------------------------- + if (AsynSrv_errcode == 0 && AsynSrv_call_depth < 5) { + strcpy(AsynSrv_routine[AsynSrv_call_depth], "AsynSrv_Trace_Write"); + AsynSrv_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - if (asyn_info->skt <= 0) { - if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { - AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; - } - return False; + if (asyn_info->skt <= 0) { + if ((AsynSrv_errcode == 0) && (asyn_info->skt < 0)) { + AsynSrv_errcode = ASYNSRV__FORCED_CLOSED; } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Send message and get reply. */ - status = AsynSrv_SendSpecCmnd (asyn_info, "-005"); + status = AsynSrv_SendSpecCmnd(asyn_info, "-005"); + + if (AsynSrv_errcode == 0) + AsynSrv_call_depth--; + return status; +} - if (AsynSrv_errcode == 0) AsynSrv_call_depth--; - return status; - } /*-------------------------------------------- End of AsynSrv_Utility.C -----*/ diff --git a/hardsup/c_interfaces.c b/hardsup/c_interfaces.c index a514c64..55634f8 100644 --- a/hardsup/c_interfaces.c +++ b/hardsup/c_interfaces.c @@ -69,28 +69,31 @@ /*-------------------------------------------------------------------------- ** Global Variables */ - extern int C_gbl_status = 0; - extern struct dsc$descriptor_s C_name_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; +extern int C_gbl_status = 0; +extern struct dsc$descriptor_s C_name_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 +}; + /*-------------------------------------------------------------------------- ** Old-style prototypes of routines which we are ** bridging to. */ - int log_arr_get (); - int log_int_get (); - int log_flt_get (); - int log_str_get (); +int log_arr_get(); +int log_int_get(); +int log_flt_get(); +int log_str_get(); - int str_edit (); +int str_edit(); - int tt_port_connect (); - int tt_port_disconnect (); - int tt_port_io (); - int tt_port_config (); +int tt_port_connect(); +int tt_port_disconnect(); +int tt_port_io(); +int tt_port_config(); /* --------------------------------------------------------------------------*/ - int C_log_arr_get (char *name, int arr_size, int *value, int indx) { +int C_log_arr_get(char *name, int arr_size, int *value, int indx) +{ /* ============= ** ** This routine is useful for calling LOG_ARR_GET from a C program. @@ -110,15 +113,17 @@ ** C_name_desc - set up as a string descriptor for name. It can ** be used to generate an error message if return status == 0. */ - C_name_desc.dsc$w_length = strlen (name); - C_name_desc.dsc$a_pointer = name; + C_name_desc.dsc$w_length = strlen(name); + C_name_desc.dsc$a_pointer = name; - C_gbl_status = log_arr_get (&C_name_desc, &arr_size, value, &indx); + C_gbl_status = log_arr_get(&C_name_desc, &arr_size, value, &indx); + + return (C_gbl_status & 1); +} - return (C_gbl_status & 1); - } /*--------------------------------------------------------------------------*/ - int C_log_int_get (char *name, long int *value, int indx) { +int C_log_int_get(char *name, long int *value, int indx) +{ /* ============= ** ** This routine is useful for calling LOG_INT_GET from a C program. @@ -136,15 +141,17 @@ ** C_name_desc - set up as a string descriptor for name. It can ** be used to generate an error message if return status == 0. */ - C_name_desc.dsc$w_length = strlen (name); - C_name_desc.dsc$a_pointer = name; + C_name_desc.dsc$w_length = strlen(name); + C_name_desc.dsc$a_pointer = name; - C_gbl_status = log_int_get (&C_name_desc, value, &indx); + C_gbl_status = log_int_get(&C_name_desc, value, &indx); + + return (C_gbl_status & 1); +} - return (C_gbl_status & 1); - } /*--------------------------------------------------------------------------*/ - int C_log_flt_get (char *name, float *value, int indx) { +int C_log_flt_get(char *name, float *value, int indx) +{ /* ============= ** ** This routine is useful for calling LOG_FLT_GET from a C program. @@ -162,15 +169,17 @@ ** C_name_desc - set up as a string descriptor for name. It can ** be used to generate an error message if return status == 0. */ - C_name_desc.dsc$w_length = strlen (name); - C_name_desc.dsc$a_pointer = name; + C_name_desc.dsc$w_length = strlen(name); + C_name_desc.dsc$a_pointer = name; - C_gbl_status = log_flt_get (&C_name_desc, value, &indx); + C_gbl_status = log_flt_get(&C_name_desc, value, &indx); + + return (C_gbl_status & 1); +} - return (C_gbl_status & 1); - } /*--------------------------------------------------------------------------*/ - int C_log_str_get (char *name, char *value, int val_size, int indx) { +int C_log_str_get(char *name, char *value, int val_size, int indx) +{ /* ============= ** ** This routine is useful for calling LOG_STR_GET from a C program. @@ -190,29 +199,32 @@ ** C_name_desc - set up as a string descriptor for name. It can ** be used to generate an error message if return status == 0. */ - struct dsc$descriptor_s my_val_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; + struct dsc$descriptor_s my_val_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; - C_name_desc.dsc$w_length = strlen (name); - C_name_desc.dsc$a_pointer = name; + C_name_desc.dsc$w_length = strlen(name); + C_name_desc.dsc$a_pointer = name; - my_val_desc.dsc$w_length = val_size - 1; - my_val_desc.dsc$a_pointer = value; + my_val_desc.dsc$w_length = val_size - 1; + my_val_desc.dsc$a_pointer = value; - C_gbl_status = log_str_get (&C_name_desc, &my_val_desc, &indx); - value[val_size - 1] = 0; /* Zero-terminate the string */ + C_gbl_status = log_str_get(&C_name_desc, &my_val_desc, &indx); + value[val_size - 1] = 0; /* Zero-terminate the string */ - if (C_gbl_status & 1) { /* If success, strip trailing spaces */ - while ((strlen (value) > 0) && (value[strlen (value) - 1] == ' ')) { - value[strlen (value) - 1] = 0; - } + if (C_gbl_status & 1) { /* If success, strip trailing spaces */ + while ((strlen(value) > 0) && (value[strlen(value) - 1] == ' ')) { + value[strlen(value) - 1] = 0; } - return (C_gbl_status & 1); } + return (C_gbl_status & 1); +} + /*--------------------------------------------------------------------------*/ - int C_str_edit (char *out, char *in, char *ctrl, int *length) { +int C_str_edit(char *out, char *in, char *ctrl, int *length) +{ /* ========== ** ** This routine is useful for calling STR_EDIT from a C program. @@ -235,39 +247,44 @@ ** Global variables: ** C_gbl_status - set to the VAX/VMS return status of STR_EDIT. */ - struct dsc$descriptor_s out_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; - struct dsc$descriptor_s in_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; - struct dsc$descriptor_s ctrl_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; + struct dsc$descriptor_s out_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; + struct dsc$descriptor_s in_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; + struct dsc$descriptor_s ctrl_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; - out_desc.dsc$w_length = *length - 1; - out_desc.dsc$a_pointer = out; + out_desc.dsc$w_length = *length - 1; + out_desc.dsc$a_pointer = out; - in_desc.dsc$w_length = strlen (in); - in_desc.dsc$a_pointer = in; + in_desc.dsc$w_length = strlen(in); + in_desc.dsc$a_pointer = in; - ctrl_desc.dsc$w_length = strlen (ctrl); - ctrl_desc.dsc$a_pointer = ctrl; + ctrl_desc.dsc$w_length = strlen(ctrl); + ctrl_desc.dsc$a_pointer = ctrl; - C_gbl_status = str_edit (&out_desc, &in_desc, &ctrl_desc, length); - if (*length >= 0) { /* zero-terminate the output string */ - out[*length] = '\0'; - }else { - out[0] = '\0'; - } - - return (C_gbl_status & 1); + C_gbl_status = str_edit(&out_desc, &in_desc, &ctrl_desc, length); + if (*length >= 0) { /* zero-terminate the output string */ + out[*length] = '\0'; + } else { + out[0] = '\0'; } + + return (C_gbl_status & 1); +} + /*--------------------------------------------------------------------------*/ - int C_tt_port_connect (int *hndl, int *chan, char *lognam, char *pwd) { +int C_tt_port_connect(int *hndl, int *chan, char *lognam, char *pwd) +{ /* ================= ** ** This routine is useful for calling TT_PORT_CONNECT from a C program. @@ -292,30 +309,33 @@ ** Global variables: ** C_gbl_status - set to the VAX/VMS return status of TT_PORT_CONNECT. */ - struct dsc$descriptor_s lognam_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; - struct dsc$descriptor_s pwd_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; - lognam_desc.dsc$w_length = strlen (lognam); - lognam_desc.dsc$a_pointer = lognam; + struct dsc$descriptor_s lognam_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; + struct dsc$descriptor_s pwd_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; + lognam_desc.dsc$w_length = strlen(lognam); + lognam_desc.dsc$a_pointer = lognam; - if (pwd != NULL) { - pwd_desc.dsc$w_length = strlen (pwd); - pwd_desc.dsc$a_pointer = pwd; - C_gbl_status = tt_port_connect ( - hndl, chan, &lognam_desc, &pwd_desc); - }else { - C_gbl_status = tt_port_connect (hndl, chan, &lognam_desc, NULL); - } - - return (C_gbl_status & 1); + if (pwd != NULL) { + pwd_desc.dsc$w_length = strlen(pwd); + pwd_desc.dsc$a_pointer = pwd; + C_gbl_status = tt_port_connect(hndl, chan, &lognam_desc, &pwd_desc); + } else { + C_gbl_status = tt_port_connect(hndl, chan, &lognam_desc, NULL); } + + return (C_gbl_status & 1); +} + /*--------------------------------------------------------------------------*/ - int C_tt_port_disconnect (int *hndl) { +int C_tt_port_disconnect(int *hndl) +{ /* ==================== ** ** This routine is useful for calling TT_PORT_DISCONNECT from a C program. @@ -331,20 +351,18 @@ ** Global variables: ** C_gbl_status - set to the VAX/VMS return status of TT_PORT_DISCONNECT. */ - C_gbl_status = tt_port_disconnect (hndl); + C_gbl_status = tt_port_disconnect(hndl); + + return (C_gbl_status & 1); +} - return (C_gbl_status & 1); - } /*--------------------------------------------------------------------------*/ - int C_tt_port_io ( +int C_tt_port_io( /* ============ -*/ int *hndl, - char *rqst, - char *term, - char *answ, - int *answ_len, /* Attention -- Read/Write argument!! */ - int flush, - int tmo) { +*/ int *hndl, + char *rqst, char *term, char *answ, int *answ_len, /* Attention -- Read/Write argument!! */ + int flush, int tmo) +{ /* ** This routine is useful for calling TT_PORT_IO from a C program. ** Refer to the DELTAT.OLB description of TT_PORT_IO to clarify any @@ -386,66 +404,73 @@ ** Global variables: ** C_gbl_status - set to the VAX/VMS return status of TT_PORT_IO. */ - struct dsc$descriptor_s rqst_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; - struct dsc$descriptor_s term_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; - struct dsc$descriptor_s answ_desc = {0, - DSC$K_DTYPE_T, - DSC$K_CLASS_S, - 0}; - char *my_rqst = NULL; - char *my_term = NULL; - char *my_answ = NULL; + struct dsc$descriptor_s rqst_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; + struct dsc$descriptor_s term_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; + struct dsc$descriptor_s answ_desc = { 0, + DSC$K_DTYPE_T, + DSC$K_CLASS_S, + 0 + }; + char *my_rqst = NULL; + char *my_term = NULL; + char *my_answ = NULL; - int my_answ_len = 0; - int my_flush = 1; - int my_tmo = 2; + int my_answ_len = 0; + int my_flush = 1; + int my_tmo = 2; - my_tmo = tmo; - if (my_tmo < 0) my_tmo = 0; - my_flush = flush; - if (my_flush != 0) my_flush = 1; - if (answ != NULL) { - if (answ_len == 0) { - printf ("C_tt_port_io -- argument error.\n"); - printf (" %s\n", - "answ_len must be present if answ is present."); - C_gbl_status = FALSE; - return FALSE; - } - answ_desc.dsc$w_length = *answ_len - 1; - answ_desc.dsc$a_pointer = answ; + my_tmo = tmo; + if (my_tmo < 0) + my_tmo = 0; + my_flush = flush; + if (my_flush != 0) + my_flush = 1; + if (answ != NULL) { + if (answ_len == 0) { + printf("C_tt_port_io -- argument error.\n"); + printf(" %s\n", + "answ_len must be present if answ is present."); + C_gbl_status = FALSE; + return FALSE; } - if (term != NULL) { - term_desc.dsc$w_length = strlen (term); - term_desc.dsc$a_pointer = term; - } - if (rqst != NULL) { - rqst_desc.dsc$w_length = strlen (rqst); - rqst_desc.dsc$a_pointer = rqst; - } - C_gbl_status = tt_port_io (hndl, &rqst_desc, &term_desc, - &answ_desc, &my_answ_len, &my_flush, &my_tmo); - if (answ_desc.dsc$w_length > 0) { /* Process any input string */ - if (answ_desc.dsc$w_length > my_answ_len) { /* Room for terminator? */ - answ[my_answ_len+1] = answ[my_answ_len]; /* Yes, so move it. */ - } - answ[my_answ_len] = '\0'; /* Put in null terminator */ - *answ_len = my_answ_len; /* Return value to caller */ - } - - return (C_gbl_status & 1); + answ_desc.dsc$w_length = *answ_len - 1; + answ_desc.dsc$a_pointer = answ; } + if (term != NULL) { + term_desc.dsc$w_length = strlen(term); + term_desc.dsc$a_pointer = term; + } + if (rqst != NULL) { + rqst_desc.dsc$w_length = strlen(rqst); + rqst_desc.dsc$a_pointer = rqst; + } + C_gbl_status = tt_port_io(hndl, &rqst_desc, &term_desc, + &answ_desc, &my_answ_len, &my_flush, &my_tmo); + if (answ_desc.dsc$w_length > 0) { /* Process any input string */ + if (answ_desc.dsc$w_length > my_answ_len) { /* Room for terminator? */ + answ[my_answ_len + 1] = answ[my_answ_len]; /* Yes, so move it. */ + } + answ[my_answ_len] = '\0'; /* Put in null terminator */ + *answ_len = my_answ_len; /* Return value to caller */ + } + + return (C_gbl_status & 1); +} + /*--------------------------------------------------------------------------*/ - int C_tt_port_config ( +int C_tt_port_config( /* ================ -*/ int *hndl, - int mask) { +*/ int *hndl, + int mask) +{ /* ** This routine is useful for calling TT_PORT_CONFIG from a C program. ** Refer to the DELTAT.OLB description of TT_PORT_CONFIG to clarify any @@ -465,8 +490,9 @@ ** Global variables: ** C_gbl_status - set to the VAX/VMS return status of TT_PORT_CONFIG. */ - C_gbl_status = tt_port_config (hndl, &mask); + C_gbl_status = tt_port_config(hndl, &mask); + + return (C_gbl_status & 1); +} - return (C_gbl_status & 1); - } /*=========================================== End of C_INTERFACES.C ========*/ diff --git a/hardsup/dillutil.c b/hardsup/dillutil.c index 38b4ad0..f64cd99 100644 --- a/hardsup/dillutil.c +++ b/hardsup/dillutil.c @@ -26,456 +26,416 @@ */ /*-------------------------------------------------------------------------*/ - int DILLU_Open(pDILLU *pData, char *pHost, int iPort, int iChannel, - int iMode, char *pTransFile) - { - int iRet; - char pCommand[80]; - char pReply[132]; - pDILLU self = NULL; - pSTable pTable = NULL; - FILE *fd = NULL; - - /* check translation file first */ - fd = fopen(pTransFile,"r"); - if(!fd) - { - return DILLU__FILENOTFOUND; - } - fgets(pReply, 131,fd); - if(strstr(pReply,"DILLUTION") == NULL) - { - fclose(fd); - return DILLU__NODILLFILE; - } - - pTable = CreateTable(fd); - fclose(fd); - if(!pTable) - { - return DILLU__ERRORTABLE; - } - - /* allocate a new data structure */ - self = (pDILLU)malloc(sizeof(DILLU)); - if(self == NULL) - { - return DILLU__BADMALLOC; - } - - *pData = self; - self->pTranstable = pTable; - - iRet = SerialOpen(&self->pData, pHost, iPort, iChannel); - if(iRet != 1) - { - return iRet; - } +int DILLU_Open(pDILLU * pData, char *pHost, int iPort, int iChannel, + int iMode, char *pTransFile) +{ + int iRet; + char pCommand[80]; + char pReply[132]; + pDILLU self = NULL; + pSTable pTable = NULL; + FILE *fd = NULL; - /* set an lengthy timeout for the configuration in order to - prevent problems. - */ - iRet = SerialConfig(&self->pData, 100); - if(iRet != 1) - { - return iRet; - } - - self->iReadOnly = iMode; - if(!self->iReadOnly) - { - /* switch to remote operation */ + /* check translation file first */ + fd = fopen(pTransFile, "r"); + if (!fd) { + return DILLU__FILENOTFOUND; + } + fgets(pReply, 131, fd); + if (strstr(pReply, "DILLUTION") == NULL) { + fclose(fd); + return DILLU__NODILLFILE; + } + + pTable = CreateTable(fd); + fclose(fd); + if (!pTable) { + return DILLU__ERRORTABLE; + } + + /* allocate a new data structure */ + self = (pDILLU) malloc(sizeof(DILLU)); + if (self == NULL) { + return DILLU__BADMALLOC; + } + + *pData = self; + self->pTranstable = pTable; + + iRet = SerialOpen(&self->pData, pHost, iPort, iChannel); + if (iRet != 1) { + return iRet; + } + + /* set an lengthy timeout for the configuration in order to + prevent problems. + */ + iRet = SerialConfig(&self->pData, 100); + if (iRet != 1) { + return iRet; + } + + self->iReadOnly = iMode; + if (!self->iReadOnly) { + /* switch to remote operation */ /* iRet = SerialWriteRead(&self->pData,"C1\r\n",pReply,131); if(iRet != 1) { return iRet; } -*/ - } - return 1; - } -/* --------------------------------------------------------------------------*/ - void DILLU_Close(pDILLU *pData) - { - char pReply[132]; - int iRet; - pDILLU self; - - self = *pData; - - if(!self) - return; - - /* switch to local operation */ - iRet = SerialWriteRead(&self->pData,"C0\r\n",pReply,131); - /* ignore errors on this one, the thing may be down */ - - /* close connection */ - SerialClose(&self->pData); - - /* free memory */ - free(self); - *pData = NULL; - } -/* --------------------------------------------------------------------------*/ - int DILLU_Config(pDILLU *pData, int iTmo) - { - int iRet; - char pReply[132]; - char pCommand[10]; - pDILLU self; - - self = *pData; - - /* first timeout */ - if(iTmo > 0) - { - iRet = SerialConfig(&self->pData, iTmo); - if(iRet < 0) - { - return iRet; - } - } - return 1; - } -/* --------------------------------------------------------------------------*/ - int DILLU_Send(pDILLU *pData, char *pCommand, char *pReply, int iLen) - { - pDILLU self; - - self = *pData; - - /* make sure, that there is a \r at the end of the command */ - if(strchr(pCommand,(int)'\r') == NULL) - { - strcat(pCommand,"\r\n"); - } - return SerialWriteRead(&self->pData,pCommand,pReply,iLen); - } -/* --------------------------------------------------------------------------*/ - int DILLU_Read(pDILLU *pData, float *fVal) - { - char pCommand[10], pReply[132]; - int iRet; - float fRead = -9999.; - float fOhm; - pDILLU self; - - self = *pData; - - - /* send D command */ - sprintf(pCommand,"D\r\n"); - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - - /* read ohms */ - iRet = sscanf(pReply,"%f",&fOhm); - if(iRet != 1) - { - return DILLU__BADREAD; - } - if(fOhm > 9999890.) - { - return DILLU__SILLYANSWER; - } - - /* convert to K */ - iRet = InterpolateVal2(self->pTranstable,fOhm,&fRead); - *fVal = fRead; - return 1; +*/ } + return 1; +} + +/* --------------------------------------------------------------------------*/ +void DILLU_Close(pDILLU * pData) +{ + char pReply[132]; + int iRet; + pDILLU self; + + self = *pData; + + if (!self) + return; + + /* switch to local operation */ + iRet = SerialWriteRead(&self->pData, "C0\r\n", pReply, 131); + /* ignore errors on this one, the thing may be down */ + + /* close connection */ + SerialClose(&self->pData); + + /* free memory */ + free(self); + *pData = NULL; +} + +/* --------------------------------------------------------------------------*/ +int DILLU_Config(pDILLU * pData, int iTmo) +{ + int iRet; + char pReply[132]; + char pCommand[10]; + pDILLU self; + + self = *pData; + + /* first timeout */ + if (iTmo > 0) { + iRet = SerialConfig(&self->pData, iTmo); + if (iRet < 0) { + return iRet; + } + } + return 1; +} + +/* --------------------------------------------------------------------------*/ +int DILLU_Send(pDILLU * pData, char *pCommand, char *pReply, int iLen) +{ + pDILLU self; + + self = *pData; + + /* make sure, that there is a \r at the end of the command */ + if (strchr(pCommand, (int) '\r') == NULL) { + strcat(pCommand, "\r\n"); + } + return SerialWriteRead(&self->pData, pCommand, pReply, iLen); +} + +/* --------------------------------------------------------------------------*/ +int DILLU_Read(pDILLU * pData, float *fVal) +{ + char pCommand[10], pReply[132]; + int iRet; + float fRead = -9999.; + float fOhm; + pDILLU self; + + self = *pData; + + + /* send D command */ + sprintf(pCommand, "D\r\n"); + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + + /* read ohms */ + iRet = sscanf(pReply, "%f", &fOhm); + if (iRet != 1) { + return DILLU__BADREAD; + } + if (fOhm > 9999890.) { + return DILLU__SILLYANSWER; + } + + /* convert to K */ + iRet = InterpolateVal2(self->pTranstable, fOhm, &fRead); + *fVal = fRead; + return 1; +} + /*-------------------------------------------------------------------------*/ - int DILLU_Set(pDILLU *pData, float fVal) - { - char pCommand[50], pReply[132]; - int iRet, i,iRange, iExec; - const float fPrecision = 0.0001; - float fSet, fRead, fOhms, tmax, fTemp; - pDILLU self; +int DILLU_Set(pDILLU * pData, float fVal) +{ + char pCommand[50], pReply[132]; + int iRet, i, iRange, iExec; + const float fPrecision = 0.0001; + float fSet, fRead, fOhms, tmax, fTemp; + pDILLU self; - self = *pData; + self = *pData; - if(self->iReadOnly) - { - return DILLU__READONLY; - } - - /* send D command to read current value*/ - sprintf(pCommand,"D\r\n"); - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - - /* read ohms */ - iRet = sscanf(pReply,"%f",&fRead); - if(iRet != 1) - { - return DILLU__BADREAD; - } - if(fRead > 9999890.) - { - return DILLU__SILLYANSWER; - } - - - /* convert new set value to ohms */ - iRet = InterpolateVal1(self->pTranstable,fVal,&fOhms); - if(!iRet) - { - return DILLU__OUTOFRANGE; - } - - /* set to remote operation */ -#ifdef debug - printf("C1\n"); -#endif - iRet = SerialWriteRead(&self->pData,"C1\r\n",pReply,131); - if(iRet != 1) - { - return iRet; - } - - /* set heater power */ - strcpy(pCommand,"G3\r"); - if(fOhms > 1125) - { - strcpy(pCommand,"G2\r"); - } - if(fOhms > 4000) - strcpy(pCommand,"G1\r"); -#ifdef debug - printf("A9\n"); -#endif - iRet = SerialWriteRead(&self->pData,"A9\r",pReply,131); - if(iRet != 1) - { - return iRet; - } -#ifdef debug - printf("%s\n",pCommand); -#endif - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - - /* Integrator time constant */ - strcpy(pCommand,"G2\r"); - if(fOhms > 200) - strcpy(pCommand,"G1\r"); - if(fOhms > 2000) - strcpy(pCommand,"G0\r"); - strcpy(pCommand,"G7\r"); - if(fOhms > 400.) - { - strcpy(pCommand,"G6\r"); - } -#ifdef debug - printf("A4\n"); -#endif - iRet = SerialWriteRead(&self->pData,"A4\r",pReply,131); - if(iRet != 1) - { - return iRet; - } -#ifdef debug - printf("%s\n",pCommand); -#endif - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - - /* derivator time constant */ - if(fOhms > 1000.) - { - strcpy(pCommand,"G1\r"); - } - else - { - strcpy(pCommand,"G2\r"); - } -#ifdef debug - printf("A5\n"); -#endif - iRet = SerialWriteRead(&self->pData,"A5\r",pReply,131); - if(iRet != 1) - { - return iRet; - } -#ifdef debug - printf("%s\n",pCommand); - iRet = 1; -#endif - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - - /* proportional gain */ - if(fOhms > 500.) - { - strcpy(pCommand,"G3\r"); - } - if(fOhms > 1000) - { - strcpy(pCommand,"G2\r"); - } - if(fOhms > 2000) - { - strcpy(pCommand,"G1\r"); - } -#ifdef debug - printf("A6\n"); -#endif - iRet = SerialWriteRead(&self->pData,"A6\r",pReply,131); - if(iRet != 1) - { - return iRet; - } -#ifdef debug - printf("%s\n",pCommand); -#endif - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - - /* range calculation a la Elsenhans */ - iRange = 1; - fTemp = fOhms*10000.; - if( (fRead > 1.9) || (fOhms > 1.9) ) - { - iRange = 2; - fTemp = fOhms*1000.; - } - if( (fRead > 19) || (fOhms > 19) ) - { - iRange = 3; - fTemp = fOhms*100.; - } - if( (fRead > 190) || (fOhms > 190) ) - { - iRange = 4; - fTemp = fOhms*10.; - } - if( (fRead > 750) || (fOhms > 750) ) - { - iRange = 5; - fTemp = fOhms; - } - if( (fRead > 19000) || (fOhms > 19000) ) - { - iRange = 6; - fTemp = fOhms/10.; - } - if( (fRead > 190000) || (fOhms > 190000) ) - { - iRange = 7; - fTemp = fOhms/100.; - } - - sprintf(pCommand,"R%1.1d\r",iRange); -#ifdef debug - printf("%s\n",pCommand); -#endif - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - - - /* finally set temperature */ -#ifdef debug - printf("Set Val befor hex: %d\n",(int)fTemp); -#endif - sprintf(pCommand,"G%4.4X\r",(int)fTemp); -#ifdef debug - printf("A3\n"); -#endif - iRet = SerialWriteRead(&self->pData,"A3\r",pReply,131); - if(iRet != 1) - { - return iRet; - } -#ifdef debug - printf("%s\n",pCommand); -#endif - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - - /* unset remote operation, so that users may mess everything up - from the panel - */ -#ifdef debug - printf("C1\n"); -#endif - iRet = SerialWriteRead(&self->pData,"C0\r\n",pReply,131); - if(iRet != 1) - { - return iRet; - } - - return 1; + if (self->iReadOnly) { + return DILLU__READONLY; } + + /* send D command to read current value */ + sprintf(pCommand, "D\r\n"); + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + + /* read ohms */ + iRet = sscanf(pReply, "%f", &fRead); + if (iRet != 1) { + return DILLU__BADREAD; + } + if (fRead > 9999890.) { + return DILLU__SILLYANSWER; + } + + + /* convert new set value to ohms */ + iRet = InterpolateVal1(self->pTranstable, fVal, &fOhms); + if (!iRet) { + return DILLU__OUTOFRANGE; + } + + /* set to remote operation */ +#ifdef debug + printf("C1\n"); +#endif + iRet = SerialWriteRead(&self->pData, "C1\r\n", pReply, 131); + if (iRet != 1) { + return iRet; + } + + /* set heater power */ + strcpy(pCommand, "G3\r"); + if (fOhms > 1125) { + strcpy(pCommand, "G2\r"); + } + if (fOhms > 4000) + strcpy(pCommand, "G1\r"); +#ifdef debug + printf("A9\n"); +#endif + iRet = SerialWriteRead(&self->pData, "A9\r", pReply, 131); + if (iRet != 1) { + return iRet; + } +#ifdef debug + printf("%s\n", pCommand); +#endif + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + + /* Integrator time constant */ + strcpy(pCommand, "G2\r"); + if (fOhms > 200) + strcpy(pCommand, "G1\r"); + if (fOhms > 2000) + strcpy(pCommand, "G0\r"); + strcpy(pCommand, "G7\r"); + if (fOhms > 400.) { + strcpy(pCommand, "G6\r"); + } +#ifdef debug + printf("A4\n"); +#endif + iRet = SerialWriteRead(&self->pData, "A4\r", pReply, 131); + if (iRet != 1) { + return iRet; + } +#ifdef debug + printf("%s\n", pCommand); +#endif + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + + /* derivator time constant */ + if (fOhms > 1000.) { + strcpy(pCommand, "G1\r"); + } else { + strcpy(pCommand, "G2\r"); + } +#ifdef debug + printf("A5\n"); +#endif + iRet = SerialWriteRead(&self->pData, "A5\r", pReply, 131); + if (iRet != 1) { + return iRet; + } +#ifdef debug + printf("%s\n", pCommand); + iRet = 1; +#endif + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + + /* proportional gain */ + if (fOhms > 500.) { + strcpy(pCommand, "G3\r"); + } + if (fOhms > 1000) { + strcpy(pCommand, "G2\r"); + } + if (fOhms > 2000) { + strcpy(pCommand, "G1\r"); + } +#ifdef debug + printf("A6\n"); +#endif + iRet = SerialWriteRead(&self->pData, "A6\r", pReply, 131); + if (iRet != 1) { + return iRet; + } +#ifdef debug + printf("%s\n", pCommand); +#endif + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + + /* range calculation a la Elsenhans */ + iRange = 1; + fTemp = fOhms * 10000.; + if ((fRead > 1.9) || (fOhms > 1.9)) { + iRange = 2; + fTemp = fOhms * 1000.; + } + if ((fRead > 19) || (fOhms > 19)) { + iRange = 3; + fTemp = fOhms * 100.; + } + if ((fRead > 190) || (fOhms > 190)) { + iRange = 4; + fTemp = fOhms * 10.; + } + if ((fRead > 750) || (fOhms > 750)) { + iRange = 5; + fTemp = fOhms; + } + if ((fRead > 19000) || (fOhms > 19000)) { + iRange = 6; + fTemp = fOhms / 10.; + } + if ((fRead > 190000) || (fOhms > 190000)) { + iRange = 7; + fTemp = fOhms / 100.; + } + + sprintf(pCommand, "R%1.1d\r", iRange); +#ifdef debug + printf("%s\n", pCommand); +#endif + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + + + /* finally set temperature */ +#ifdef debug + printf("Set Val befor hex: %d\n", (int) fTemp); +#endif + sprintf(pCommand, "G%4.4X\r", (int) fTemp); +#ifdef debug + printf("A3\n"); +#endif + iRet = SerialWriteRead(&self->pData, "A3\r", pReply, 131); + if (iRet != 1) { + return iRet; + } +#ifdef debug + printf("%s\n", pCommand); +#endif + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + + /* unset remote operation, so that users may mess everything up + from the panel + */ +#ifdef debug + printf("C1\n"); +#endif + iRet = SerialWriteRead(&self->pData, "C0\r\n", pReply, 131); + if (iRet != 1) { + return iRet; + } + + return 1; +} + /*-------------------------------------------------------------------------*/ - void DILLU_Error2Text(pDILLU *pData,int iCode, char *pError, int iLen) - { - char pBueffel[512]; - pDILLU self; +void DILLU_Error2Text(pDILLU * pData, int iCode, char *pError, int iLen) +{ + char pBueffel[512]; + pDILLU self; - self = *pData; - - switch(iCode) - { - case DILLU__FILENOTFOUND: - strncpy(pError,"Translation Table file not found",iLen); - return; - break; - case DILLU__NODILLFILE: - strncpy(pError,"Translation Table file is not DILLU",iLen); - return; - break; - case DILLU__ERRORTABLE: - strncpy(pError,"Translation Table could not be created",iLen); - return; - break; - case DILLU__BADREAD: - strncpy(pError,"Message corrupted",iLen); - return; - break; - case DILLU__SILLYANSWER: - strncpy(pError,"Message corrupted",iLen); - return; - break; - case DILLU__BADMALLOC: - strncpy(pError,"Out of memory in Open_DILLU",iLen); - return; - break; - case DILLU__READONLY: - strncpy(pError,"DILLU is read-only",iLen); - return; - break; - case DILLU__OUTOFRANGE: - strncpy(pError,"Requested value is out of range",iLen); - return; - break; - default: - SerialError(iCode,pError,iLen); - break; - } + self = *pData; + + switch (iCode) { + case DILLU__FILENOTFOUND: + strncpy(pError, "Translation Table file not found", iLen); + return; + break; + case DILLU__NODILLFILE: + strncpy(pError, "Translation Table file is not DILLU", iLen); + return; + break; + case DILLU__ERRORTABLE: + strncpy(pError, "Translation Table could not be created", iLen); + return; + break; + case DILLU__BADREAD: + strncpy(pError, "Message corrupted", iLen); + return; + break; + case DILLU__SILLYANSWER: + strncpy(pError, "Message corrupted", iLen); + return; + break; + case DILLU__BADMALLOC: + strncpy(pError, "Out of memory in Open_DILLU", iLen); + return; + break; + case DILLU__READONLY: + strncpy(pError, "DILLU is read-only", iLen); + return; + break; + case DILLU__OUTOFRANGE: + strncpy(pError, "Requested value is out of range", iLen); + return; + break; + default: + SerialError(iCode, pError, iLen); + break; } +} diff --git a/hardsup/dillutil.h b/hardsup/dillutil.h index b1ac9c3..668769f 100644 --- a/hardsup/dillutil.h +++ b/hardsup/dillutil.h @@ -30,17 +30,17 @@ #define DILLU__BADMALLOC -717 #define DILLU__NODILLUFOUND -711 /*------------------------------------------------------------------------*/ - typedef struct __DILLU { - void *pData; - pSTable pTranstable; - int iReadOnly; - } DILLU; +typedef struct __DILLU { + void *pData; + pSTable pTranstable; + int iReadOnly; +} DILLU; - typedef struct __DILLU *pDILLU; +typedef struct __DILLU *pDILLU; /*-----------------------------------------------------------------------*/ - int DILLU_Open(pDILLU *pData,char *pHost, int iPort, int iChannel, - int iMode, char *pTransFile); +int DILLU_Open(pDILLU * pData, char *pHost, int iPort, int iChannel, + int iMode, char *pTransFile); /***** creates an DILLU datastructure and opens a connection to the ITCL4 controller. Input Parameters are: the hostname @@ -53,16 +53,16 @@ Return values are 1 for success, a negative error code on failure. - */ - - void DILLU_Close(pDILLU *pData); + */ + +void DILLU_Close(pDILLU * pData); /****** close a connection to an DILLU controller and frees its data structure. The only parameter is a pointer to the data structure for this controller. This pointer will be invalid after this call. - */ + */ - int DILLU_Config(pDILLU *pData, int iTmo); +int DILLU_Config(pDILLU * pData, int iTmo); /***** configure some aspects of a DILLU temperature controller. The parameter are: - a pointer to the data structure for the controller as @@ -71,8 +71,8 @@ The function returns 1 on success, a negative error code on failure. */ - - int DILLU_Send(pDILLU *pData, char *pCommand, char *pReply, int iLen); + +int DILLU_Send(pDILLU * pData, char *pCommand, char *pReply, int iLen); /******* send a the command in pCommand to the DILLU controller. A possible reply is returned in the buffer pReply. Maximum iLen characters are copied to pReply. @@ -82,8 +82,8 @@ Return values are 1 for success, a negative error code on failure. */ - - int DILLU_Read(pDILLU *pData, float *fVal); + +int DILLU_Read(pDILLU * pData, float *fVal); /****** Reads the current temperature at the controller @@ -91,7 +91,7 @@ failure. */ - int DILLU_Set(pDILLU *pData, float fVal); +int DILLU_Set(pDILLU * pData, float fVal); /****** sets a new preset temperature in the DILL temperature controller. Parameters are: - a pointer to a DILLU data structure as returned by Open_DILLU. @@ -101,8 +101,6 @@ failure. */ - void DILLU_Error2Text(pDILLU *pData, int iCode, char *pError, int iLen); - -#endif - - +void DILLU_Error2Text(pDILLU * pData, int iCode, char *pError, int iLen); + +#endif diff --git a/hardsup/el734_def.h b/hardsup/el734_def.h index f3a6437..d5a1bc0 100644 --- a/hardsup/el734_def.h +++ b/hardsup/el734_def.h @@ -17,57 +17,58 @@ #define MAX_MOT 12 -enum EL734_Requests {FULL__STATUS, - SHORT__STATUS}; +enum EL734_Requests { FULL__STATUS, + SHORT__STATUS +}; /* ** Structure to which the EL734_Open handle points. */ - struct EL734info { - struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */ - int motor; - int ored_msr, fp_cntr, fr_cntr; - struct RS__MsgStruct to_host; - struct RS__RespStruct from_host; - }; +struct EL734info { + struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */ + int motor; + int ored_msr, fp_cntr, fr_cntr; + struct RS__MsgStruct to_host; + struct RS__RespStruct from_host; +}; /* ** Structure holding everything that is known about a VME Motor Controller. ** It is also the structure of replies from the Server. */ - struct Motor_State { - int motor; /* Motor number */ - int exists; /* True if Motor exists */ - int msr; /* MSR - Motor Status Register */ - int ored_msr; /* Cumulated MSR */ - int fp_cntr; /* Counter for *FP reports */ - int fr_cntr; /* Counter for *FR reports */ - int ss; /* SS - Status Flags Register */ - char pos_real[16]; /* U - Position as read (degrees) */ - char name[16]; /* MN */ - int dec_pt; /* A - # of decimal places */ - int enc_factor[2]; /* FD - Encoder scaling factors (numer/denom) */ - int mot_factor[2]; /* FM - Motor scaling factors (numer/denom) */ - char inertia_tol[16];/* D - Inertia tol'nce (sec) (Schleppfehler) */ - int ramp; /* E - Start/stop ramp (kHz/sec) */ - int loop_mode; /* F - Open loop/Closed loop (0/1) */ - int slow_hz; /* G - Start/stop frequency (Mot-S/sec) */ - char lims[2][16]; /* H - Lower/Upper limits */ - int fast_hz; /* J - Top speed (Mot-S/sec) */ - int ref_mode; /* K - Reference mode */ - int backlash; /* L - Backlash par (Mot-S) (Spielausgleich) */ - int pos_tol; /* M - Position tolerance (Enc-Steps) */ - char ref_param[16]; /* Q - Parameter for "Goto Reference" */ - int is_sided; /* T - One-sided operation flag (0 = no) */ - char null_pt[16]; /* V - Null point */ - int ac_par; /* W - Air-cushion dependency */ - int enc_circ; /* Z - circumference of encoder (Enc-Steps) */ - int stat_pos; /* SP - # of positionings */ - int stat_pos_flt; /* ST - # of positioning faults (recovered) */ - int stat_pos_fail; /* SR - # of positioning fails (abandoned) */ - int stat_cush_fail; /* SA - # of air-cushion fails */ - char set_real[16]; /* P - Position as set (degrees) */ - int ac_state; /* AC - Air-cushion state (0 = down) */ - int out; /* SO - State of Output Signal */ - int in; /* RI - State of Input Signal */ - }; +struct Motor_State { + int motor; /* Motor number */ + int exists; /* True if Motor exists */ + int msr; /* MSR - Motor Status Register */ + int ored_msr; /* Cumulated MSR */ + int fp_cntr; /* Counter for *FP reports */ + int fr_cntr; /* Counter for *FR reports */ + int ss; /* SS - Status Flags Register */ + char pos_real[16]; /* U - Position as read (degrees) */ + char name[16]; /* MN */ + int dec_pt; /* A - # of decimal places */ + int enc_factor[2]; /* FD - Encoder scaling factors (numer/denom) */ + int mot_factor[2]; /* FM - Motor scaling factors (numer/denom) */ + char inertia_tol[16]; /* D - Inertia tol'nce (sec) (Schleppfehler) */ + int ramp; /* E - Start/stop ramp (kHz/sec) */ + int loop_mode; /* F - Open loop/Closed loop (0/1) */ + int slow_hz; /* G - Start/stop frequency (Mot-S/sec) */ + char lims[2][16]; /* H - Lower/Upper limits */ + int fast_hz; /* J - Top speed (Mot-S/sec) */ + int ref_mode; /* K - Reference mode */ + int backlash; /* L - Backlash par (Mot-S) (Spielausgleich) */ + int pos_tol; /* M - Position tolerance (Enc-Steps) */ + char ref_param[16]; /* Q - Parameter for "Goto Reference" */ + int is_sided; /* T - One-sided operation flag (0 = no) */ + char null_pt[16]; /* V - Null point */ + int ac_par; /* W - Air-cushion dependency */ + int enc_circ; /* Z - circumference of encoder (Enc-Steps) */ + int stat_pos; /* SP - # of positionings */ + int stat_pos_flt; /* ST - # of positioning faults (recovered) */ + int stat_pos_fail; /* SR - # of positioning fails (abandoned) */ + int stat_cush_fail; /* SA - # of air-cushion fails */ + char set_real[16]; /* P - Position as set (degrees) */ + int ac_state; /* AC - Air-cushion state (0 = down) */ + int out; /* SO - State of Output Signal */ + int in; /* RI - State of Input Signal */ +}; /*------------------------------------------------ End of EL734_DEF.H --*/ -#endif /* _el734_def_ */ +#endif /* _el734_def_ */ diff --git a/hardsup/el734_utility.c b/hardsup/el734_utility.c index 3ecf57d..b037785 100644 --- a/hardsup/el734_utility.c +++ b/hardsup/el734_utility.c @@ -822,1827 +822,2057 @@ /*-------------------------------------------------------------------------- ** Global Variables */ - static int EL734_call_depth = 0; - static char EL734_routine[5][64]; - static int EL734_errcode = 0; - static int EL734_errno, EL734_vaxc_errno; - char EL734_IllgText[256]; +static int EL734_call_depth = 0; +static char EL734_routine[5][64]; +static int EL734_errcode = 0; +static int EL734_errno, EL734_vaxc_errno; +char EL734_IllgText[256]; /* **--------------------------------------------------------------------------- ** EL734_AddCallStack: Add a routine name to the call stack. ** This allows EL734_ErrInfo to generate a ** trace-back in case of error. */ - int EL734_AddCallStack ( +int EL734_AddCallStack( /* ================== -*/ struct EL734info *pntr, - char *name) { +*/ struct EL734info *pntr, + char *name) +{ - if (EL734_errcode != 0) return False; + if (EL734_errcode != 0) + return False; - if (EL734_call_depth < 5) { - strcpy (EL734_routine[EL734_call_depth], name); - EL734_call_depth++; - } - - if (pntr == NULL) {EL734_errcode = EL734__NOT_OPEN; return False;} - - if (pntr->asyn_info.skt <= 0) { - memset (pntr->from_host.msg_size, - '0', sizeof (pntr->from_host.msg_size)); - EL734_errcode = (pntr->asyn_info.skt < 0) ? EL734__FORCED_CLOSED - : EL734__NO_SOCKET; - return False; - } - return True; + if (EL734_call_depth < 5) { + strcpy(EL734_routine[EL734_call_depth], name); + EL734_call_depth++; } + + if (pntr == NULL) { + EL734_errcode = EL734__NOT_OPEN; + return False; + } + + if (pntr->asyn_info.skt <= 0) { + memset(pntr->from_host.msg_size, + '0', sizeof(pntr->from_host.msg_size)); + EL734_errcode = (pntr->asyn_info.skt < 0) ? EL734__FORCED_CLOSED + : EL734__NO_SOCKET; + return False; + } + return True; +} + /* **--------------------------------------------------------------------------- ** EL734_Close: Close a connection to a motor. */ - int EL734_Close ( +int EL734_Close( /* =========== -*/ void **handle, - int force_flag) { +*/ void **handle, + int force_flag) +{ - struct EL734info *info_ptr; - char buff[4]; - - info_ptr = (struct EL734info *) *handle; - if (info_ptr == NULL) return True; - - if (info_ptr->asyn_info.skt != 0) { - if (info_ptr->asyn_info.skt > 0) { - AsynSrv_Close (*handle, force_flag); - } - } - free (*handle); - *handle = NULL; + struct EL734info *info_ptr; + char buff[4]; + info_ptr = (struct EL734info *) *handle; + if (info_ptr == NULL) return True; + + if (info_ptr->asyn_info.skt != 0) { + if (info_ptr->asyn_info.skt > 0) { + AsynSrv_Close(*handle, force_flag); + } } + free(*handle); + *handle = NULL; + + return True; +} + /* **--------------------------------------------------------------------------- ** EL734_Config: Configure a connection to a motor. */ - int EL734_Config ( +int EL734_Config( /* ============ -*/ void **handle, - ...) { +*/ void **handle, + ...) +{ - char buff[16]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - int intval; - struct EL734info *info_ptr; - /*---------------------------------------------- + char buff[16]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + int intval; + struct EL734info *info_ptr; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_Config")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_Config")) + return False; + /*---------------------------------------------- */ - va_start (ap, handle); /* Set up var arg machinery */ - txt_ptr = va_arg (ap, char *); /* Get pntr to first parameter ident */ - while (txt_ptr != NULL) { - if (strcmp (txt_ptr, "msecTmo") == 0) { - intval = va_arg (ap, int); - if ((intval < 100) || (intval > 999999)) { - EL734_errcode = EL734__BAD_PAR; - return False; - } - sprintf (buff, "%04d", intval/100); /* Convert to ASCII as .. - ** .. deci-secs */ - memcpy (info_ptr->asyn_info.tmo, buff, 4); - }else if (strcmp (txt_ptr, "eot") == 0) { - txt_ptr = va_arg (ap, char *); - if (txt_ptr == NULL) { - EL734_errcode = EL734__BAD_PAR; - return False; - } - memcpy (info_ptr->asyn_info.eot, "\0\0\0\0", 4); - switch (txt_ptr[0]) { - case '3': info_ptr->asyn_info.eot[3] = txt_ptr[3]; - case '2': info_ptr->asyn_info.eot[2] = txt_ptr[2]; - case '1': info_ptr->asyn_info.eot[1] = txt_ptr[1]; - case '0': - info_ptr->asyn_info.eot[0] = txt_ptr[0]; - break; - default: - EL734_errcode = EL734__BAD_PAR; - return False; - } - }else if (strcmp (txt_ptr, "motor") == 0) { - intval = va_arg (ap, int); - if ((intval < 1) || (intval > 12)) { - EL734_errcode = EL734__BAD_PAR; - return False; - } - info_ptr->motor = intval; - }else if (strcmp (txt_ptr, "chan") == 0) { - intval = va_arg (ap, int); - if ((intval < 0) || (intval > 255)) { - EL734_errcode = EL734__BAD_PAR; - return False; - } - info_ptr->asyn_info.chan = intval; - sprintf (buff, "%04d", intval); /* Convert to ASCII */ - memcpy (info_ptr->asyn_info.chan_char, buff, 4); - }else { - EL734_errcode = EL734__BAD_PAR; - return False; + va_start(ap, handle); /* Set up var arg machinery */ + txt_ptr = va_arg(ap, char *); /* Get pntr to first parameter ident */ + while (txt_ptr != NULL) { + if (strcmp(txt_ptr, "msecTmo") == 0) { + intval = va_arg(ap, int); + if ((intval < 100) || (intval > 999999)) { + EL734_errcode = EL734__BAD_PAR; + return False; } - txt_ptr = va_arg (ap, char *); /* Get pntr to next parameter ident */ + sprintf(buff, "%04d", intval / 100); /* Convert to ASCII as .. + ** .. deci-secs */ + memcpy(info_ptr->asyn_info.tmo, buff, 4); + } else if (strcmp(txt_ptr, "eot") == 0) { + txt_ptr = va_arg(ap, char *); + if (txt_ptr == NULL) { + EL734_errcode = EL734__BAD_PAR; + return False; + } + memcpy(info_ptr->asyn_info.eot, "\0\0\0\0", 4); + switch (txt_ptr[0]) { + case '3': + info_ptr->asyn_info.eot[3] = txt_ptr[3]; + case '2': + info_ptr->asyn_info.eot[2] = txt_ptr[2]; + case '1': + info_ptr->asyn_info.eot[1] = txt_ptr[1]; + case '0': + info_ptr->asyn_info.eot[0] = txt_ptr[0]; + break; + default: + EL734_errcode = EL734__BAD_PAR; + return False; + } + } else if (strcmp(txt_ptr, "motor") == 0) { + intval = va_arg(ap, int); + if ((intval < 1) || (intval > 12)) { + EL734_errcode = EL734__BAD_PAR; + return False; + } + info_ptr->motor = intval; + } else if (strcmp(txt_ptr, "chan") == 0) { + intval = va_arg(ap, int); + if ((intval < 0) || (intval > 255)) { + EL734_errcode = EL734__BAD_PAR; + return False; + } + info_ptr->asyn_info.chan = intval; + sprintf(buff, "%04d", intval); /* Convert to ASCII */ + memcpy(info_ptr->asyn_info.chan_char, buff, 4); + } else { + EL734_errcode = EL734__BAD_PAR; + return False; } - - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; + txt_ptr = va_arg(ap, char *); /* Get pntr to next parameter ident */ } + + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL734_EncodeMSR: Encode the MSR status into text. */ - char *EL734_EncodeMSR (char *text, int text_len, +char *EL734_EncodeMSR(char *text, int text_len, /* =============== -*/ int msr, - int ored_msr, - int fp_cntr, - int fr_cntr) { - int len; - char my_text[132]; - char my_text_0[32]; +*/ int msr, + int ored_msr, int fp_cntr, int fr_cntr) +{ + int len; + char my_text[132]; + char my_text_0[32]; - if (msr == 0) { - ored_msr = ored_msr & ~(MSR__BUSY); /* Zero "Busy" bit */ - if (ored_msr == MSR__OK) { - StrJoin (text, text_len, "Status, MSR = Idle. Positioned OK.", ""); - }else { - if ((ored_msr & MSR__OK) != 0) { - StrJoin (text, text_len, "Status, MSR = Idle. Positioned OK. ", ""); - }else { - StrJoin (text, text_len, "Status, MSR = Idle. ", ""); - } - if ((ored_msr & MSR__REF_OK) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Ref. Pos'n OK. "); - } - if ((ored_msr & MSR__LIM_ERR) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Limit Switch Problem. "); - } - if ((ored_msr & MSR__AC_FAIL) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Air-Cushion Error. "); - } - if ((ored_msr & MSR__REF_FAIL) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Ref. Pos'n Fail. "); - } - if ((ored_msr & MSR__POS_FAIL) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Pos'n Fail. "); - } - if ((ored_msr & MSR__POS_FAULT) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - if (fp_cntr == 1) { - StrJoin (text, text_len, my_text, "1 Pos'n Fault. "); - }else { - sprintf (my_text_0, "%d Pos'n Faults. ", fp_cntr); - StrJoin (text, text_len, my_text, my_text_0); - } - } - if ((ored_msr & MSR__RUN_FAIL) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Run Fail. "); - } - if ((ored_msr & MSR__RUN_FAULT) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - if (fr_cntr == 1) { - StrJoin (text, text_len, my_text, "1 Run Fault. "); - }else { - sprintf (my_text_0, "%d Run Faults. ", fr_cntr); - StrJoin (text, text_len, my_text, my_text_0); - } - } - if ((ored_msr & MSR__HALT) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Halt. "); - } - if ((ored_msr & MSR__HI_LIM) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Hit HiLim. "); - } - if ((ored_msr & MSR__LO_LIM) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Hit LoLim. "); - } - if ((ored_msr & MSR__STOPPED) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Stopped. "); - } + if (msr == 0) { + ored_msr = ored_msr & ~(MSR__BUSY); /* Zero "Busy" bit */ + if (ored_msr == MSR__OK) { + StrJoin(text, text_len, "Status, MSR = Idle. Positioned OK.", ""); + } else { + if ((ored_msr & MSR__OK) != 0) { + StrJoin(text, text_len, "Status, MSR = Idle. Positioned OK. ", ""); + } else { + StrJoin(text, text_len, "Status, MSR = Idle. ", ""); } - }else if ((msr & ~(0x2fff)) != 0) { - StrJoin (text, text_len, "Status, MSR = ??", ""); - }else { - sprintf (my_text, "%#x ", msr); - StrJoin (text, text_len, "Status, MSR = ", my_text); - if ((msr & MSR__LIM_ERR) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Limit Switch Problem/"); + if ((ored_msr & MSR__REF_OK) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Ref. Pos'n OK. "); } - if ((msr & MSR__AC_FAIL) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Air-Cushion Error/"); + if ((ored_msr & MSR__LIM_ERR) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Limit Switch Problem. "); } - if ((msr & MSR__REF_FAIL) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Ref. Pos'n Fail/"); + if ((ored_msr & MSR__AC_FAIL) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Air-Cushion Error. "); } - if ((msr & MSR__POS_FAIL) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Pos'n Fail/"); + if ((ored_msr & MSR__REF_FAIL) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Ref. Pos'n Fail. "); } - if ((msr & MSR__POS_FAULT) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Pos'n Fault/"); + if ((ored_msr & MSR__POS_FAIL) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Pos'n Fail. "); } - if ((msr & MSR__RUN_FAIL) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Run Fail/"); + if ((ored_msr & MSR__POS_FAULT) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + if (fp_cntr == 1) { + StrJoin(text, text_len, my_text, "1 Pos'n Fault. "); + } else { + sprintf(my_text_0, "%d Pos'n Faults. ", fp_cntr); + StrJoin(text, text_len, my_text, my_text_0); + } } - if ((msr & MSR__RUN_FAULT) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Run Fault/"); + if ((ored_msr & MSR__RUN_FAIL) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Run Fail. "); } - if ((msr & MSR__HALT) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Halt/"); + if ((ored_msr & MSR__RUN_FAULT) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + if (fr_cntr == 1) { + StrJoin(text, text_len, my_text, "1 Run Fault. "); + } else { + sprintf(my_text_0, "%d Run Faults. ", fr_cntr); + StrJoin(text, text_len, my_text, my_text_0); + } } - if ((msr & MSR__HI_LIM) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Hit HiLim/"); + if ((ored_msr & MSR__HALT) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Halt. "); } - if ((msr & MSR__LO_LIM) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Hit LoLim/"); + if ((ored_msr & MSR__HI_LIM) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Hit HiLim. "); } - if ((msr & MSR__STOPPED) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Stopped/"); + if ((ored_msr & MSR__LO_LIM) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Hit LoLim. "); } - if ((msr & MSR__REF_OK) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Ref. Pos'n OK/"); + if ((ored_msr & MSR__STOPPED) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Stopped. "); } - if ((msr & MSR__OK) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "OK/"); - } - if ((msr & MSR__BUSY) != 0) { - StrJoin (my_text, sizeof (my_text), text, ""); - StrJoin (text, text_len, my_text, "Busy/"); - } - len = strlen (text); - text[len-1] = '\0'; } - return text; + } else if ((msr & ~(0x2fff)) != 0) { + StrJoin(text, text_len, "Status, MSR = ??", ""); + } else { + sprintf(my_text, "%#x ", msr); + StrJoin(text, text_len, "Status, MSR = ", my_text); + if ((msr & MSR__LIM_ERR) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Limit Switch Problem/"); + } + if ((msr & MSR__AC_FAIL) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Air-Cushion Error/"); + } + if ((msr & MSR__REF_FAIL) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Ref. Pos'n Fail/"); + } + if ((msr & MSR__POS_FAIL) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Pos'n Fail/"); + } + if ((msr & MSR__POS_FAULT) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Pos'n Fault/"); + } + if ((msr & MSR__RUN_FAIL) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Run Fail/"); + } + if ((msr & MSR__RUN_FAULT) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Run Fault/"); + } + if ((msr & MSR__HALT) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Halt/"); + } + if ((msr & MSR__HI_LIM) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Hit HiLim/"); + } + if ((msr & MSR__LO_LIM) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Hit LoLim/"); + } + if ((msr & MSR__STOPPED) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Stopped/"); + } + if ((msr & MSR__REF_OK) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Ref. Pos'n OK/"); + } + if ((msr & MSR__OK) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "OK/"); + } + if ((msr & MSR__BUSY) != 0) { + StrJoin(my_text, sizeof(my_text), text, ""); + StrJoin(text, text_len, my_text, "Busy/"); + } + len = strlen(text); + text[len - 1] = '\0'; } + return text; +} + /* **--------------------------------------------------------------------------- ** EL734_EncodeSS: Encode the SS flags into text. */ - char *EL734_EncodeSS (char *text, int text_len, int ss) { +char *EL734_EncodeSS(char *text, int text_len, int ss) +{ /* ============== */ - int len; - char my_text[132]; - char my_text_0[32]; + int len; + char my_text[132]; + char my_text_0[32]; - if (ss == 0) { - StrJoin (text, text_len, "Flags, SS = 0", ""); - }else if ((ss & ~(0x3f)) != 0) { - StrJoin (text, text_len, "Flags, SS = ??", ""); - }else { - sprintf (my_text, "Flags, SS = 0x%02X ", ss); - my_text_0[0] = '\0'; - if ((ss & 0x20) != 0) strcat (my_text_0, "LSX/"); - if ((ss & 0x10) != 0) strcat (my_text_0, "LS2/"); - if ((ss & 0x08) != 0) strcat (my_text_0, "LS1/"); - if ((ss & 0x04) != 0) strcat (my_text_0, "STP/"); - if ((ss & 0x02) != 0) strcat (my_text_0, "CCW/"); - if ((ss & 0x01) != 0) strcat (my_text_0, "HLT/"); - len = strlen (my_text_0); - my_text_0[len-1] = '\0'; - StrJoin (text, text_len, my_text, my_text_0); - } - return text; + if (ss == 0) { + StrJoin(text, text_len, "Flags, SS = 0", ""); + } else if ((ss & ~(0x3f)) != 0) { + StrJoin(text, text_len, "Flags, SS = ??", ""); + } else { + sprintf(my_text, "Flags, SS = 0x%02X ", ss); + my_text_0[0] = '\0'; + if ((ss & 0x20) != 0) + strcat(my_text_0, "LSX/"); + if ((ss & 0x10) != 0) + strcat(my_text_0, "LS2/"); + if ((ss & 0x08) != 0) + strcat(my_text_0, "LS1/"); + if ((ss & 0x04) != 0) + strcat(my_text_0, "STP/"); + if ((ss & 0x02) != 0) + strcat(my_text_0, "CCW/"); + if ((ss & 0x01) != 0) + strcat(my_text_0, "HLT/"); + len = strlen(my_text_0); + my_text_0[len - 1] = '\0'; + StrJoin(text, text_len, my_text, my_text_0); } + return text; +} + /* **------------------------------------------------------------------------- ** EL734_ErrInfo: Return detailed status from last operation. */ - void EL734_ErrInfo ( +void EL734_ErrInfo( /* ============= -*/ char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno) { +*/ char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno) +{ - int i; - char buff[80], *txt; - int asyn_errcode, asyn_errno, asyn_vaxerrno; - char* asyn_errtxt; + int i; + char buff[80], *txt; + int asyn_errcode, asyn_errno, asyn_vaxerrno; + char *asyn_errtxt; - if (EL734_call_depth <= 0) { - strcpy (EL734_routine[0], "EL734_no_error_detected"); - *errcode = 0; - *my_errno = 0; - *vaxc_errno = 0; - }else { - if (EL734_call_depth > 1) { /* Concatenate the names */ - for (i = 1; i < EL734_call_depth; i++) { - strcat (EL734_routine[0], "/"); - StrJoin (EL734_routine[0], sizeof (EL734_routine), - EL734_routine[0], EL734_routine[i]); - } + if (EL734_call_depth <= 0) { + strcpy(EL734_routine[0], "EL734_no_error_detected"); + *errcode = 0; + *my_errno = 0; + *vaxc_errno = 0; + } else { + if (EL734_call_depth > 1) { /* Concatenate the names */ + for (i = 1; i < EL734_call_depth; i++) { + strcat(EL734_routine[0], "/"); + StrJoin(EL734_routine[0], sizeof(EL734_routine), + EL734_routine[0], EL734_routine[i]); } - *errcode = EL734_errcode; - *my_errno = EL734_errno; - *vaxc_errno = EL734_vaxc_errno; - switch (EL734_errcode) { - case EL734__BAD_ADR: txt = "/EL734__BAD_ADR"; break; - case EL734__BAD_ASYNSRV: txt = "/EL734__BAD_ASYNSRV"; break; - case EL734__BAD_CMD: txt = "/EL734__BAD_CMD"; break; - case EL734__BAD_DEV: txt = "/EL734__BAD_DEV"; break; - case EL734__BAD_ILLG: txt = "/EL734__BAD_ILLG"; break; - case EL734__BAD_LOC: txt = "/EL734__BAD_LOC"; break; - case EL734__BAD_MALLOC: txt = "/EL734__BAD_MALLOC"; break; - case EL734__BAD_OFL: txt = "/EL734__BAD_OFL"; break; - case EL734__BAD_OVFL: txt = "/EL734__BAD_OVFL"; break; - case EL734__BAD_PAR: txt = "/EL734__BAD_PAR"; break; - case EL734__BAD_RNG: txt = "/EL734__BAD_RNG"; break; - case EL734__BAD_SOCKET: txt = "/EL734__BAD_SOCKET"; break; - case EL734__BAD_STP: txt = "/EL734__BAD_STP"; break; - case EL734__BAD_TMO: txt = "/EL734__BAD_TMO"; break; - case EL734__EMERG_STOP: txt = "/EL734__EMERG_STOP"; break; - case EL734__FORCED_CLOSED: txt = "/EL734__FORCED_CLOSED"; break; - case EL734__NOT_OPEN: txt = "/EL734__NOT_OPEN"; break; - case EL734__NO_SOCKET: txt = "/EL734__NO_SOCKET"; break; - default: - sprintf (buff, "/EL734__unknown_err_code: %d", EL734_errcode); - txt = buff; - } - StrJoin (EL734_routine[0], sizeof(EL734_routine), EL734_routine[0], txt); } - AsynSrv_ErrInfo (&asyn_errtxt, &asyn_errcode, &asyn_errno, &asyn_vaxerrno); - if (asyn_errcode != 0) { - strcat (EL734_routine[0], "/"); - StrJoin (EL734_routine[0], sizeof(EL734_routine), - EL734_routine[0], asyn_errtxt); + *errcode = EL734_errcode; + *my_errno = EL734_errno; + *vaxc_errno = EL734_vaxc_errno; + switch (EL734_errcode) { + case EL734__BAD_ADR: + txt = "/EL734__BAD_ADR"; + break; + case EL734__BAD_ASYNSRV: + txt = "/EL734__BAD_ASYNSRV"; + break; + case EL734__BAD_CMD: + txt = "/EL734__BAD_CMD"; + break; + case EL734__BAD_DEV: + txt = "/EL734__BAD_DEV"; + break; + case EL734__BAD_ILLG: + txt = "/EL734__BAD_ILLG"; + break; + case EL734__BAD_LOC: + txt = "/EL734__BAD_LOC"; + break; + case EL734__BAD_MALLOC: + txt = "/EL734__BAD_MALLOC"; + break; + case EL734__BAD_OFL: + txt = "/EL734__BAD_OFL"; + break; + case EL734__BAD_OVFL: + txt = "/EL734__BAD_OVFL"; + break; + case EL734__BAD_PAR: + txt = "/EL734__BAD_PAR"; + break; + case EL734__BAD_RNG: + txt = "/EL734__BAD_RNG"; + break; + case EL734__BAD_SOCKET: + txt = "/EL734__BAD_SOCKET"; + break; + case EL734__BAD_STP: + txt = "/EL734__BAD_STP"; + break; + case EL734__BAD_TMO: + txt = "/EL734__BAD_TMO"; + break; + case EL734__EMERG_STOP: + txt = "/EL734__EMERG_STOP"; + break; + case EL734__FORCED_CLOSED: + txt = "/EL734__FORCED_CLOSED"; + break; + case EL734__NOT_OPEN: + txt = "/EL734__NOT_OPEN"; + break; + case EL734__NO_SOCKET: + txt = "/EL734__NO_SOCKET"; + break; + default: + sprintf(buff, "/EL734__unknown_err_code: %d", EL734_errcode); + txt = buff; } - *entry_txt = EL734_routine[0]; - EL734_call_depth = 0; - EL734_errcode = 0; + StrJoin(EL734_routine[0], sizeof(EL734_routine), EL734_routine[0], + txt); } + AsynSrv_ErrInfo(&asyn_errtxt, &asyn_errcode, &asyn_errno, + &asyn_vaxerrno); + if (asyn_errcode != 0) { + strcat(EL734_routine[0], "/"); + StrJoin(EL734_routine[0], sizeof(EL734_routine), + EL734_routine[0], asyn_errtxt); + } + *entry_txt = EL734_routine[0]; + EL734_call_depth = 0; + EL734_errcode = 0; +} + /* **--------------------------------------------------------------------------- ** EL734_GetAirCush: Get W and AC register values. */ - int EL734_GetAirCush ( +int EL734_GetAirCush( /* ================ -*/ void **handle, - int *present, - int *state) { +*/ void **handle, + int *present, int *state) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10], cmnd1[10]; - char *rply_ptr, *rply_ptr0, *rply_ptr1; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10], cmnd1[10]; + char *rply_ptr, *rply_ptr0, *rply_ptr1; + /*---------------------------------------------- */ - *present = *state = 0; - info_ptr = (struct EL734info *) *handle; + *present = *state = 0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetAirCush")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetAirCush")) + return False; + /*---------------------------------------------- ** Send W and AC cmnds to EL734 */ - sprintf (cmnd0, "w %d\r", info_ptr->motor); - sprintf (cmnd1, "ac %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, cmnd1, NULL); - if (!status) { - *present = *state = 0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr1 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (rply_ptr1 == NULL) rply_ptr1 = "?no_response"; + sprintf(cmnd0, "w %d\r", info_ptr->motor); + sprintf(cmnd1, "ac %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, cmnd1, NULL); + if (!status) { + *present = *state = 0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr1 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?no_response"; - if ((sscanf (rply_ptr0, "%d", present) != 1) || - (sscanf (rply_ptr1, "%d", state) != 1)) { - if (*rply_ptr0 == '?') { - rply_ptr = rply_ptr0; - }else if (*rply_ptr1 == '?') { - rply_ptr = rply_ptr1; - }else { - rply_ptr = "?funny_response"; - } - *present = *state = 0; - EL734_SetErrcode (info_ptr, rply_ptr, "W\" or \"AC"); - return False; + if ((sscanf(rply_ptr0, "%d", present) != 1) || + (sscanf(rply_ptr1, "%d", state) != 1)) { + if (*rply_ptr0 == '?') { + rply_ptr = rply_ptr0; + } else if (*rply_ptr1 == '?') { + rply_ptr = rply_ptr1; + } else { + rply_ptr = "?funny_response"; } + *present = *state = 0; + EL734_SetErrcode(info_ptr, rply_ptr, "W\" or \"AC"); + return False; } - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; } + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL734_GetEncGearing: Get FD register values. */ - int EL734_GetEncGearing ( +int EL734_GetEncGearing( /* =================== -*/ void **handle, - int *nominator, - int *denominator) { +*/ void **handle, + int *nominator, int *denominator) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *nominator = *denominator = 0; - info_ptr = (struct EL734info *) *handle; + *nominator = *denominator = 0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetEncGearing")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetEncGearing")) + return False; + /*---------------------------------------------- ** Send FD cmnd to EL734 */ - sprintf (cmnd0, "fd %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - *nominator = *denominator = 0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (sscanf (rply_ptr0, "%d %d", nominator, denominator) == 2) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - *nominator = *denominator = 0; - EL734_SetErrcode (info_ptr, rply_ptr0, "FD"); - return False; + sprintf(cmnd0, "fd %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + *nominator = *denominator = 0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (sscanf(rply_ptr0, "%d %d", nominator, denominator) == 2) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + *nominator = *denominator = 0; + EL734_SetErrcode(info_ptr, rply_ptr0, "FD"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetId: Get ID register value. */ - int EL734_GetId ( +int EL734_GetId( /* =========== -*/ void **handle, - char *id_txt, - int id_len) { +*/ void **handle, + char *id_txt, int id_len) +{ - int status; - struct EL734info *info_ptr; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char *rply_ptr0; + /*---------------------------------------------- */ - *id_txt = '\0'; - info_ptr = (struct EL734info *) *handle; + *id_txt = '\0'; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetId")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetId")) + return False; + /*---------------------------------------------- ** Send ID cmnd to EL734 */ - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "id\r", NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if ((rply_ptr0 != NULL) && - (*rply_ptr0 != '\0') && - (*rply_ptr0 != '?')) { - StrJoin (id_txt, id_len, rply_ptr0, ""); - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - EL734_SetErrcode (info_ptr, rply_ptr0, "ID"); - return False; + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "id\r", NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if ((rply_ptr0 != NULL) && (*rply_ptr0 != '\0') && (*rply_ptr0 != '?')) { + StrJoin(id_txt, id_len, rply_ptr0, ""); + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + EL734_SetErrcode(info_ptr, rply_ptr0, "ID"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetLimits: Get H register values. */ - int EL734_GetLimits ( +int EL734_GetLimits( /* =============== -*/ void **handle, - float *lo, - float *hi) { +*/ void **handle, + float *lo, float *hi) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *lo = *hi = 0.0; - info_ptr = (struct EL734info *) *handle; + *lo = *hi = 0.0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetLimits")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetLimits")) + return False; + /*---------------------------------------------- ** Send H cmnd to EL734 */ - sprintf (cmnd0, "h %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - *lo = *hi = 0.0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (sscanf (rply_ptr0, "%f %f", lo, hi) == 2) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - *lo = *hi = 0.0; - EL734_SetErrcode (info_ptr, rply_ptr0, "H"); - return False; + sprintf(cmnd0, "h %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + *lo = *hi = 0.0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (sscanf(rply_ptr0, "%f %f", lo, hi) == 2) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + *lo = *hi = 0.0; + EL734_SetErrcode(info_ptr, rply_ptr0, "H"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetMotorGearing: Get FM register values. */ - int EL734_GetMotorGearing ( +int EL734_GetMotorGearing( /* ===================== -*/ void **handle, - int *nominator, - int *denominator) { +*/ void **handle, + int *nominator, int *denominator) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *nominator = *denominator = 0; - info_ptr = (struct EL734info *) *handle; + *nominator = *denominator = 0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetMotorGearing")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetMotorGearing")) + return False; + /*---------------------------------------------- ** Send FM cmnd to EL734 */ - sprintf (cmnd0, "fm %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - *nominator = *denominator = 0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (sscanf (rply_ptr0, "%d %d", nominator, denominator) == 2) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - *nominator = *denominator = 0; - EL734_SetErrcode (info_ptr, rply_ptr0, "FM"); - return False; + sprintf(cmnd0, "fm %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + *nominator = *denominator = 0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (sscanf(rply_ptr0, "%d %d", nominator, denominator) == 2) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + *nominator = *denominator = 0; + EL734_SetErrcode(info_ptr, rply_ptr0, "FM"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetNullPoint: Get V register value. */ - int EL734_GetNullPoint ( +int EL734_GetNullPoint( /* ================== -*/ void **handle, - int *null_pt) { +*/ void **handle, + int *null_pt) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *null_pt = 0; - info_ptr = (struct EL734info *) *handle; + *null_pt = 0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetNullPoint")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetNullPoint")) + return False; + /*---------------------------------------------- ** Send V cmnd to EL734 */ - sprintf (cmnd0, "v %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - *null_pt = 0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (sscanf (rply_ptr0, "%d", null_pt) == 1) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - *null_pt = 0; - EL734_SetErrcode (info_ptr, rply_ptr0, "V"); - return False; + sprintf(cmnd0, "v %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + *null_pt = 0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (sscanf(rply_ptr0, "%d", null_pt) == 1) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + *null_pt = 0; + EL734_SetErrcode(info_ptr, rply_ptr0, "V"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetPosition: Get U register value, the current position. */ - int EL734_GetPosition ( +int EL734_GetPosition( /* ================= -*/ void **handle, - float *ist_posit) { +*/ void **handle, + float *ist_posit) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *ist_posit = 0.0; - info_ptr = (struct EL734info *) *handle; + *ist_posit = 0.0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetPosition")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetPosition")) + return False; + /*---------------------------------------------- ** Send U cmnd to EL734 */ - sprintf (cmnd0, "u %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - *ist_posit = 0.0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (sscanf (rply_ptr0, "%f", ist_posit) == 1) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - *ist_posit = 0.0; - EL734_SetErrcode (info_ptr, rply_ptr0, "U"); - return False; + sprintf(cmnd0, "u %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + *ist_posit = 0.0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (sscanf(rply_ptr0, "%f", ist_posit) == 1) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + *ist_posit = 0.0; + EL734_SetErrcode(info_ptr, rply_ptr0, "U"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetPrecision: Get A register value. */ - int EL734_GetPrecision ( +int EL734_GetPrecision( /* ================== -*/ void **handle, - int *n_dec) { +*/ void **handle, + int *n_dec) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *n_dec = 3; - info_ptr = (struct EL734info *) *handle; + *n_dec = 3; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetPrecision")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetPrecision")) + return False; + /*---------------------------------------------- ** Send A cmnd to EL734 */ - sprintf (cmnd0, "a %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - *n_dec = 3; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (sscanf (rply_ptr0, "%d", n_dec) == 1) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - *n_dec = 3; - EL734_SetErrcode (info_ptr, rply_ptr0, "A"); - return False; + sprintf(cmnd0, "a %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + *n_dec = 3; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (sscanf(rply_ptr0, "%d", n_dec) == 1) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + *n_dec = 3; + EL734_SetErrcode(info_ptr, rply_ptr0, "A"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetRefMode: Get K register value. */ - int EL734_GetRefMode ( +int EL734_GetRefMode( /* ================ -*/ void **handle, - int *mode) { +*/ void **handle, + int *mode) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *mode = 0; - info_ptr = (struct EL734info *) *handle; + *mode = 0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetRefMode")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetRefMode")) + return False; + /*---------------------------------------------- ** Send K cmnd to EL734 */ - sprintf (cmnd0, "k %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - *mode = 0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (sscanf (rply_ptr0, "%d", mode) == 1) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - *mode = 0; - EL734_SetErrcode (info_ptr, rply_ptr0, "K"); - return False; + sprintf(cmnd0, "k %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + *mode = 0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (sscanf(rply_ptr0, "%d", mode) == 1) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + *mode = 0; + EL734_SetErrcode(info_ptr, rply_ptr0, "K"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetRefParam: Get Q register value. */ - int EL734_GetRefParam ( +int EL734_GetRefParam( /* ================= -*/ void **handle, - float *param) { +*/ void **handle, + float *param) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *param = 0.0; - info_ptr = (struct EL734info *) *handle; + *param = 0.0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetRefParam")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetRefParam")) + return False; + /*---------------------------------------------- ** Send Q cmnd to EL734 */ - sprintf (cmnd0, "q %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - *param = 0.0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (sscanf (rply_ptr0, "%f", param) == 1) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - *param = 0.0; - EL734_SetErrcode (info_ptr, rply_ptr0, "Q"); - return False; + sprintf(cmnd0, "q %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + *param = 0.0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (sscanf(rply_ptr0, "%f", param) == 1) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + *param = 0.0; + EL734_SetErrcode(info_ptr, rply_ptr0, "Q"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_GetSpeeds: Get G/J/E register values. */ - int EL734_GetSpeeds ( +int EL734_GetSpeeds( /* =============== -*/ void **handle, - int *lo, - int *hi, - int *ramp) { +*/ void **handle, + int *lo, int *hi, int *ramp) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char cmnd1[10]; - char cmnd2[10]; - char *rply_ptr; - char *rply_ptr0; - char *rply_ptr1; - char *rply_ptr2; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char cmnd1[10]; + char cmnd2[10]; + char *rply_ptr; + char *rply_ptr0; + char *rply_ptr1; + char *rply_ptr2; + /*---------------------------------------------- */ - *lo = *hi = *ramp = 0; - info_ptr = (struct EL734info *) *handle; + *lo = *hi = *ramp = 0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetSpeeds")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetSpeeds")) + return False; + /*---------------------------------------------- ** Send G, J and E cmnds to EL734 */ - sprintf (cmnd0, "g %d\r", info_ptr->motor); - sprintf (cmnd1, "j %d\r", info_ptr->motor); - sprintf (cmnd2, "e %d\r", info_ptr->motor); + sprintf(cmnd0, "g %d\r", info_ptr->motor); + sprintf(cmnd1, "j %d\r", info_ptr->motor); + sprintf(cmnd2, "e %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, cmnd1, cmnd2, NULL); - if (!status) { - *lo = *hi = *ramp = 0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr1 = rply_ptr2 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); - if (rply_ptr1 != NULL) rply_ptr2 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr1); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, cmnd1, cmnd2, NULL); + if (!status) { + *lo = *hi = *ramp = 0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr1 = rply_ptr2 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); + if (rply_ptr1 != NULL) + rply_ptr2 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr1); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (rply_ptr1 == NULL) rply_ptr1 = "?no_response"; - if (rply_ptr2 == NULL) rply_ptr2 = "?no_response"; + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?no_response"; + if (rply_ptr2 == NULL) + rply_ptr2 = "?no_response"; - if ((sscanf (rply_ptr0, "%d", lo) != 1) || - (sscanf (rply_ptr1, "%d", hi) != 1) || - (sscanf (rply_ptr2, "%d", ramp) != 1)) { - if (*rply_ptr0 == '?') { - rply_ptr = rply_ptr0; - }else if (*rply_ptr1 == '?') { - rply_ptr = rply_ptr1; - }else if (*rply_ptr2 == '?') { - rply_ptr = rply_ptr2; - }else { - rply_ptr = "?funny_response"; - } - *lo = *hi = *ramp = 0; - EL734_SetErrcode (info_ptr, rply_ptr, "G\", \"J\" or \"E"); - return False; + if ((sscanf(rply_ptr0, "%d", lo) != 1) || + (sscanf(rply_ptr1, "%d", hi) != 1) || + (sscanf(rply_ptr2, "%d", ramp) != 1)) { + if (*rply_ptr0 == '?') { + rply_ptr = rply_ptr0; + } else if (*rply_ptr1 == '?') { + rply_ptr = rply_ptr1; + } else if (*rply_ptr2 == '?') { + rply_ptr = rply_ptr2; + } else { + rply_ptr = "?funny_response"; } + *lo = *hi = *ramp = 0; + EL734_SetErrcode(info_ptr, rply_ptr, "G\", \"J\" or \"E"); + return False; } - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; } + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL734_GetStatus: Get MSR/SS/U register values. */ - int EL734_GetStatus ( +int EL734_GetStatus( /* =============== -*/ void **handle, - int *msr, - int *ored_msr, - int *fp_cntr, - int *fr_cntr, - int *ss, - float *ist_posit) { +*/ void **handle, + int *msr, + int *ored_msr, + int *fp_cntr, int *fr_cntr, int *ss, float *ist_posit) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char cmnd1[10]; - char cmnd2[10]; - char *rply_ptr; - char *rply_ptr0; - char *rply_ptr1; - char *rply_ptr2; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char cmnd1[10]; + char cmnd2[10]; + char *rply_ptr; + char *rply_ptr0; + char *rply_ptr1; + char *rply_ptr2; + /*---------------------------------------------- */ - *msr = *ored_msr = *fp_cntr = *fr_cntr = *ss = -1; *ist_posit = 0.0; - info_ptr = (struct EL734info *) *handle; + *msr = *ored_msr = *fp_cntr = *fr_cntr = *ss = -1; + *ist_posit = 0.0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetStatus")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetStatus")) + return False; + /*---------------------------------------------- ** Send MSR, SS and U cmnds to EL734 */ - sprintf (cmnd0, "msr %d\r", info_ptr->motor); - sprintf (cmnd1, "ss %d\r", info_ptr->motor); - sprintf (cmnd2, "u %d\r", info_ptr->motor); + sprintf(cmnd0, "msr %d\r", info_ptr->motor); + sprintf(cmnd1, "ss %d\r", info_ptr->motor); + sprintf(cmnd2, "u %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, cmnd1, cmnd2, NULL); - if (!status) { - *msr = *ored_msr = *fp_cntr = *fr_cntr = *ss = -1; *ist_posit = 0.0; - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr1 = rply_ptr2 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); - if (rply_ptr1 != NULL) rply_ptr2 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr1); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, cmnd1, cmnd2, NULL); + if (!status) { + *msr = *ored_msr = *fp_cntr = *fr_cntr = *ss = -1; + *ist_posit = 0.0; + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr1 = rply_ptr2 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); + if (rply_ptr1 != NULL) + rply_ptr2 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr1); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (rply_ptr1 == NULL) rply_ptr1 = "?no_response"; - if (rply_ptr2 == NULL) rply_ptr2 = "?no_response"; + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?no_response"; + if (rply_ptr2 == NULL) + rply_ptr2 = "?no_response"; - if ((sscanf (rply_ptr0, "%x", msr) == 1) && - (sscanf (rply_ptr2, "%f", ist_posit) == 1)) { - info_ptr->ored_msr = info_ptr->ored_msr | *msr; - if ((*msr & MSR__POS_FAULT) != 0) info_ptr->fp_cntr++; - if ((*msr & MSR__RUN_FAULT) != 0) info_ptr->fr_cntr++; - *ored_msr = info_ptr->ored_msr; - *fp_cntr = info_ptr->fp_cntr; - *fr_cntr = info_ptr->fr_cntr; - /* Remember: we may get "?BSY" for SS and - ** this should not be treated as an error! - */ - if (sscanf (rply_ptr1, "%x", ss) != 1) *ss = -1; - }else { - if (*rply_ptr0 == '?') { - rply_ptr = rply_ptr0; - }else if (*rply_ptr1 == '?') { - rply_ptr = rply_ptr1; - }else if (*rply_ptr2 == '?') { - rply_ptr = rply_ptr2; - }else { - rply_ptr = "?funny_response"; - } - *msr = *ored_msr = *fp_cntr = *fr_cntr = *ss = -1; *ist_posit = 0.0; - EL734_SetErrcode (info_ptr, rply_ptr, "MSR\", \"SS\" or \"U"); - return False; + if ((sscanf(rply_ptr0, "%x", msr) == 1) && + (sscanf(rply_ptr2, "%f", ist_posit) == 1)) { + info_ptr->ored_msr = info_ptr->ored_msr | *msr; + if ((*msr & MSR__POS_FAULT) != 0) + info_ptr->fp_cntr++; + if ((*msr & MSR__RUN_FAULT) != 0) + info_ptr->fr_cntr++; + *ored_msr = info_ptr->ored_msr; + *fp_cntr = info_ptr->fp_cntr; + *fr_cntr = info_ptr->fr_cntr; + /* Remember: we may get "?BSY" for SS and + ** this should not be treated as an error! + */ + if (sscanf(rply_ptr1, "%x", ss) != 1) + *ss = -1; + } else { + if (*rply_ptr0 == '?') { + rply_ptr = rply_ptr0; + } else if (*rply_ptr1 == '?') { + rply_ptr = rply_ptr1; + } else if (*rply_ptr2 == '?') { + rply_ptr = rply_ptr2; + } else { + rply_ptr = "?funny_response"; } + *msr = *ored_msr = *fp_cntr = *fr_cntr = *ss = -1; + *ist_posit = 0.0; + EL734_SetErrcode(info_ptr, rply_ptr, "MSR\", \"SS\" or \"U"); + return False; } - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; } + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL734_GetZeroPoint: Get zero point (= converted V register value) */ - int EL734_GetZeroPoint ( +int EL734_GetZeroPoint( /* ================== -*/ void **handle, - float *zero_pt) { +*/ void **handle, + float *zero_pt) +{ - int status, null_pt, nom, denom; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status, null_pt, nom, denom; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - *zero_pt = 0.0; - info_ptr = (struct EL734info *) *handle; + *zero_pt = 0.0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_GetZeroPoint")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_GetZeroPoint")) + return False; + /*---------------------------------------------- ** Get V register value. */ - status = EL734_GetNullPoint (handle, &null_pt); - if (!status) return False; + status = EL734_GetNullPoint(handle, &null_pt); + if (!status) + return False; - /*---------------------------------------------- + /*---------------------------------------------- ** FD register values. */ - status = EL734_GetEncGearing (handle, &nom, &denom); - if (!status) return False; + status = EL734_GetEncGearing(handle, &nom, &denom); + if (!status) + return False; - if (nom == 0) { - EL734_errcode = EL734__BAD_OVFL; /* Encoder gearing ratio is zero */ - return False; - } - - *zero_pt = ((float) denom)/((float) nom); - *zero_pt *= (float) null_pt; - - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; + if (nom == 0) { + EL734_errcode = EL734__BAD_OVFL; /* Encoder gearing ratio is zero */ + return False; } + + *zero_pt = ((float) denom) / ((float) nom); + *zero_pt *= (float) null_pt; + + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL734_MoveNoWait: Move motor and don't wait for completion. */ - int EL734_MoveNoWait ( +int EL734_MoveNoWait( /* ================ -*/ void **handle, - float soll_posit) { +*/ void **handle, + float soll_posit) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[32]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[32]; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_MoveNoWait")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_MoveNoWait")) + return False; + /*---------------------------------------------- ** Send P cmnd to EL734 */ - sprintf (cmnd0, "p %d %.3f\r", info_ptr->motor, soll_posit); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (*rply_ptr0 == '\0' || *rply_ptr0 == '\r' ) { - /* - ** The command was accepted - so zero the statistics - ** fields in the handle and return to caller. - */ - info_ptr->ored_msr = info_ptr->fp_cntr = info_ptr->fr_cntr = 0; - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - EL734_SetErrcode (info_ptr, rply_ptr0, "P"); - return False; + sprintf(cmnd0, "p %d %.3f\r", info_ptr->motor, soll_posit); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (*rply_ptr0 == '\0' || *rply_ptr0 == '\r') { + /* + ** The command was accepted - so zero the statistics + ** fields in the handle and return to caller. + */ + info_ptr->ored_msr = info_ptr->fp_cntr = info_ptr->fr_cntr = 0; + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + EL734_SetErrcode(info_ptr, rply_ptr0, "P"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_MoveWait: Move motor and wait for completion. */ - int EL734_MoveWait ( +int EL734_MoveWait( /* ============== -*/ void **handle, - float soll_posit, - int *ored_msr, - int *fp_cntr, - int *fr_cntr, - float *ist_posit) { +*/ void **handle, + float soll_posit, + int *ored_msr, + int *fp_cntr, int *fr_cntr, float *ist_posit) +{ - int status; - struct EL734info *info_ptr; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + /*---------------------------------------------- */ - *ored_msr = *fp_cntr = *fr_cntr = -1; *ist_posit = 0.0; - info_ptr = (struct EL734info *) *handle; + *ored_msr = *fp_cntr = *fr_cntr = -1; + *ist_posit = 0.0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_MoveWait")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_MoveWait")) + return False; + /*---------------------------------------------- ** Start the movement. */ - status = EL734_MoveNoWait (handle, soll_posit); - if (status) { - status = EL734_WaitIdle (handle, ored_msr, fp_cntr, fr_cntr, ist_posit); - } - if (status && (EL734_errcode == 0)) EL734_call_depth--; - if (EL734_errcode != 0) return False; - return status; + status = EL734_MoveNoWait(handle, soll_posit); + if (status) { + status = EL734_WaitIdle(handle, ored_msr, fp_cntr, fr_cntr, ist_posit); } + if (status && (EL734_errcode == 0)) + EL734_call_depth--; + if (EL734_errcode != 0) + return False; + return status; +} + /* **--------------------------------------------------------------------------- ** EL734_Open: Open a connection to a motor. */ - int EL734_Open ( +int EL734_Open( /* ========== -*/ void **handle, - char *host, - int port, - int chan, - int motor, - char *device_id) { +*/ void **handle, + char *host, int port, int chan, int motor, char *device_id) +{ - int my_msr, status; - struct EL734info *my_handle; - char tmo_save[4]; - char msr_cmnd[20]; - char *rply_ptr; - char *rply_ptr0; - char *rply_ptr1; - char *rply_ptr2; - char *rply_ptr3; + int my_msr, status; + struct EL734info *my_handle; + char tmo_save[4]; + char msr_cmnd[20]; + char *rply_ptr; + char *rply_ptr0; + char *rply_ptr1; + char *rply_ptr2; + char *rply_ptr3; /*-------------------------------------------------------- ** Initialise the error info stack and pre-set the ** routine name (in case of error). */ - EL734_errcode = EL734_errno = EL734_vaxc_errno = 0; - strcpy (EL734_routine[0], "EL734_Open"); - EL734_call_depth = 1; + EL734_errcode = EL734_errno = EL734_vaxc_errno = 0; + strcpy(EL734_routine[0], "EL734_Open"); + EL734_call_depth = 1; /*-------------------------------------------------------- ** Assume trouble */ - *handle = NULL; + *handle = NULL; /*-------------------------------------------------------- ** Reserve space for the data we need to store. */ - my_handle = (struct EL734info *) malloc (sizeof (*my_handle)); - if (my_handle == NULL) { - EL734_errcode = EL734__BAD_MALLOC; /* malloc failed!! */ - return False; - } - memset(my_handle,0,sizeof(*my_handle)); + my_handle = (struct EL734info *) malloc(sizeof(*my_handle)); + if (my_handle == NULL) { + EL734_errcode = EL734__BAD_MALLOC; /* malloc failed!! */ + return False; + } + memset(my_handle, 0, sizeof(*my_handle)); /*-------------------------------------------------------- ** Set up the connection */ - StrJoin (my_handle->asyn_info.host, sizeof (my_handle->asyn_info.host), - host, ""); - my_handle->asyn_info.port = port; - my_handle->asyn_info.chan = chan; - status = AsynSrv_Open (&my_handle->asyn_info); - if (!status) { - EL734_errcode = EL734__BAD_SOCKET; - GetErrno (&EL734_errno, &EL734_vaxc_errno); /* Save errno info */ - fprintf (stderr, "\nEL734_Open/AsynSrv_Open: " - "Failed to make connection.\n"); - free (my_handle); - return False; - } - - memcpy (tmo_save, my_handle->asyn_info.tmo, 4); /* Save time-out */ - EL734_Config ((void *) &my_handle, - "msecTmo", 500, /* Set a short time-out initially since - ** there should be no reason for the RMT, - ** ECHO or ID commands to take very long. - */ - "eot", "1\r", - "motor", motor, - NULL); - my_handle->ored_msr = 0; - my_handle->fp_cntr = 0; - my_handle->fr_cntr = 0; - /* - ** Now ensure the EL734 is on-line. The first "RMT 1" command can - ** fail due to pending characters in the EL734 input buffer causing - ** the "RMT 1" to be corrupted. The response of the EL734 to this - ** command is ignored for this reason (but the AsynSrv_SendCmnds - ** status must be OK otherwise it indicates a network problem). - */ - status = AsynSrv_SendCmnds (&my_handle->asyn_info, - &my_handle->to_host, &my_handle->from_host, - "rmt 1\r", NULL); - sprintf (msr_cmnd, "msr %d\r", motor); - if (status) { - if (device_id != NULL) { - status = AsynSrv_SendCmnds (&my_handle->asyn_info, - &my_handle->to_host, &my_handle->from_host, - "rmt 1\r", "echo 0\r", "id\r", msr_cmnd, NULL); - }else { - status = AsynSrv_SendCmnds (&my_handle->asyn_info, - &my_handle->to_host, &my_handle->from_host, - "rmt 1\r", "echo 0\r", "echo 0\r", msr_cmnd, NULL); - } - } - memcpy (my_handle->asyn_info.tmo, tmo_save, 4); /* Restore time-out */ - if (!status) { - /* Some error occurred in AsynSrv_SendCmnds */ - EL734_errcode = EL734__BAD_ASYNSRV; - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - }else { - /* Check the responses carefully. The 3rd response should - ** be the device identifier (if to be checked). The 4th - ** response should be a hex integer. - ** MK: But first we check for *ES, emergency stop. - ** The desired behaviour is that we continue in this - ** case and hope for the best. - */ - rply_ptr1 = rply_ptr2 = rply_ptr3 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &my_handle->asyn_info, &my_handle->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &my_handle->asyn_info, &my_handle->from_host, rply_ptr0); - if (rply_ptr1 != NULL) rply_ptr2 = AsynSrv_GetReply ( - &my_handle->asyn_info, &my_handle->from_host, rply_ptr1); - if (rply_ptr2 != NULL) rply_ptr3 = AsynSrv_GetReply ( - &my_handle->asyn_info, &my_handle->from_host, rply_ptr2); - - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (rply_ptr1 == NULL) rply_ptr1 = "?no_response"; - if (rply_ptr2 == NULL) rply_ptr2 = "?no_response"; - if (rply_ptr3 == NULL) rply_ptr3 = "?no_response"; - - if(strstr(rply_ptr0,"*ES") != NULL){ - *handle = my_handle; - EL734_call_depth--; - return True; - } - - if (*rply_ptr1 == '?') rply_ptr0 = rply_ptr1; - if (*rply_ptr2 == '?') rply_ptr0 = rply_ptr2; - if (*rply_ptr3 == '?') rply_ptr0 = rply_ptr3; - if (*rply_ptr0 != '?') { - if (device_id != NULL) { /* Check device ID? */ - if (*rply_ptr2 == '\0') { /* Yes. But if response is blank, it - ** may be because Emergency Stop is set. - */ - EL734_SetErrcode (my_handle, rply_ptr2, "ID"); - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - }else { - if (strncmp (rply_ptr2, device_id, strlen (device_id)) != 0) { - EL734_errcode = EL734__BAD_DEV; /* Device has wrong ID */ - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - } - } - } - if (sscanf (rply_ptr3, "%x", &my_msr) != 1) { - /* MSR response is bad */ - EL734_SetErrcode (my_handle, rply_ptr3, msr_cmnd); /* Check for *ES */ - if (EL734_errcode != EL734__EMERG_STOP) - EL734_errcode = EL734__BAD_DEV; - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - } - /* - ** The connection is complete. Pass the data structure - ** back to the caller as a handle. - */ - *handle = my_handle; - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - }else { - EL734_errcode = EL734__BAD_DEV; - if (strcmp (rply_ptr0, "?OFL") == 0) EL734_errcode = EL734__BAD_OFL; - if (strcmp (rply_ptr0, "?CMD") == 0) EL734_errcode = EL734__BAD_CMD; - if (strcmp (rply_ptr0, "?LOC") == 0) EL734_errcode = EL734__BAD_LOC; - if (strcmp (rply_ptr0, "?ADR") == 0) EL734_errcode = EL734__BAD_ADR; - if (strcmp (rply_ptr0, "*ES") == 0) EL734_errcode = EL734__EMERG_STOP; - if (strncmp (rply_ptr0, "?TMO", 4) == 0) EL734_errcode = EL734__BAD_TMO; - if (EL734_errcode == EL734__BAD_DEV) - fprintf (stderr, " Unrecognised initial response: \"%s\"\n", - rply_ptr0); - } - } - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); + StrJoin(my_handle->asyn_info.host, sizeof(my_handle->asyn_info.host), + host, ""); + my_handle->asyn_info.port = port; + my_handle->asyn_info.chan = chan; + status = AsynSrv_Open(&my_handle->asyn_info); + if (!status) { + EL734_errcode = EL734__BAD_SOCKET; + GetErrno(&EL734_errno, &EL734_vaxc_errno); /* Save errno info */ + fprintf(stderr, "\nEL734_Open/AsynSrv_Open: " + "Failed to make connection.\n"); + free(my_handle); return False; } + + memcpy(tmo_save, my_handle->asyn_info.tmo, 4); /* Save time-out */ + EL734_Config((void *) &my_handle, "msecTmo", 500, /* Set a short time-out initially since + ** there should be no reason for the RMT, + ** ECHO or ID commands to take very long. + */ + "eot", "1\r", "motor", motor, NULL); + my_handle->ored_msr = 0; + my_handle->fp_cntr = 0; + my_handle->fr_cntr = 0; + /* + ** Now ensure the EL734 is on-line. The first "RMT 1" command can + ** fail due to pending characters in the EL734 input buffer causing + ** the "RMT 1" to be corrupted. The response of the EL734 to this + ** command is ignored for this reason (but the AsynSrv_SendCmnds + ** status must be OK otherwise it indicates a network problem). + */ + status = AsynSrv_SendCmnds(&my_handle->asyn_info, + &my_handle->to_host, &my_handle->from_host, + "rmt 1\r", NULL); + sprintf(msr_cmnd, "msr %d\r", motor); + if (status) { + if (device_id != NULL) { + status = AsynSrv_SendCmnds(&my_handle->asyn_info, + &my_handle->to_host, + &my_handle->from_host, "rmt 1\r", + "echo 0\r", "id\r", msr_cmnd, NULL); + } else { + status = AsynSrv_SendCmnds(&my_handle->asyn_info, + &my_handle->to_host, + &my_handle->from_host, "rmt 1\r", + "echo 0\r", "echo 0\r", msr_cmnd, NULL); + } + } + memcpy(my_handle->asyn_info.tmo, tmo_save, 4); /* Restore time-out */ + if (!status) { + /* Some error occurred in AsynSrv_SendCmnds */ + EL734_errcode = EL734__BAD_ASYNSRV; + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } else { + /* Check the responses carefully. The 3rd response should + ** be the device identifier (if to be checked). The 4th + ** response should be a hex integer. + ** MK: But first we check for *ES, emergency stop. + ** The desired behaviour is that we continue in this + ** case and hope for the best. + */ + rply_ptr1 = rply_ptr2 = rply_ptr3 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&my_handle->asyn_info, &my_handle->from_host, + NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&my_handle->asyn_info, &my_handle->from_host, + rply_ptr0); + if (rply_ptr1 != NULL) + rply_ptr2 = + AsynSrv_GetReply(&my_handle->asyn_info, &my_handle->from_host, + rply_ptr1); + if (rply_ptr2 != NULL) + rply_ptr3 = + AsynSrv_GetReply(&my_handle->asyn_info, &my_handle->from_host, + rply_ptr2); + + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?no_response"; + if (rply_ptr2 == NULL) + rply_ptr2 = "?no_response"; + if (rply_ptr3 == NULL) + rply_ptr3 = "?no_response"; + + if (strstr(rply_ptr0, "*ES") != NULL) { + *handle = my_handle; + EL734_call_depth--; + return True; + } + + if (*rply_ptr1 == '?') + rply_ptr0 = rply_ptr1; + if (*rply_ptr2 == '?') + rply_ptr0 = rply_ptr2; + if (*rply_ptr3 == '?') + rply_ptr0 = rply_ptr3; + if (*rply_ptr0 != '?') { + if (device_id != NULL) { /* Check device ID? */ + if (*rply_ptr2 == '\0') { /* Yes. But if response is blank, it + ** may be because Emergency Stop is set. + */ + EL734_SetErrcode(my_handle, rply_ptr2, "ID"); + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } else { + if (strncmp(rply_ptr2, device_id, strlen(device_id)) != 0) { + EL734_errcode = EL734__BAD_DEV; /* Device has wrong ID */ + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } + } + } + if (sscanf(rply_ptr3, "%x", &my_msr) != 1) { + /* MSR response is bad */ + EL734_SetErrcode(my_handle, rply_ptr3, msr_cmnd); /* Check for *ES */ + if (EL734_errcode != EL734__EMERG_STOP) + EL734_errcode = EL734__BAD_DEV; + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } + /* + ** The connection is complete. Pass the data structure + ** back to the caller as a handle. + */ + *handle = my_handle; + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; + } else { + EL734_errcode = EL734__BAD_DEV; + if (strcmp(rply_ptr0, "?OFL") == 0) + EL734_errcode = EL734__BAD_OFL; + if (strcmp(rply_ptr0, "?CMD") == 0) + EL734_errcode = EL734__BAD_CMD; + if (strcmp(rply_ptr0, "?LOC") == 0) + EL734_errcode = EL734__BAD_LOC; + if (strcmp(rply_ptr0, "?ADR") == 0) + EL734_errcode = EL734__BAD_ADR; + if (strcmp(rply_ptr0, "*ES") == 0) + EL734_errcode = EL734__EMERG_STOP; + if (strncmp(rply_ptr0, "?TMO", 4) == 0) + EL734_errcode = EL734__BAD_TMO; + if (EL734_errcode == EL734__BAD_DEV) + fprintf(stderr, " Unrecognised initial response: \"%s\"\n", + rply_ptr0); + } + } + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; +} + /* **--------------------------------------------------------------------------- ** EL734_PutOffline: put the EL734 off-line */ - int EL734_PutOffline ( +int EL734_PutOffline( /* ================ -*/ void **handle) { +*/ void **handle) +{ - int status; - struct EL734info *info_ptr; - char *rply_ptr0, *rply_ptr1, *rply_ptr2; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char *rply_ptr0, *rply_ptr1, *rply_ptr2; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_PutOffline")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_PutOffline")) + return False; + /*---------------------------------------------- ** The problem which this routine has is that the EL734 ** may already be off-line. The following is, therefore, ** rather pedantic for most cases which occur in practice. */ - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "\r", "rmt 1\r", "echo 1\r", "rmt 0\r", NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; return False;} - - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "RMT\r", "", NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; return False;} - - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (rply_ptr1 == NULL) rply_ptr1 = "?no_response"; - if ((strcmp (rply_ptr0, "RMT") == 0) && - (strcmp (rply_ptr1, "\n0") == 0)) { - EL734_call_depth--; - return True; - } - - EL734_SetErrcode (info_ptr, rply_ptr0, "RMT"); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "\r", "rmt 1\r", "echo 1\r", "rmt 0\r", NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; return False; } + + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "RMT\r", "", NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } + + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?no_response"; + if ((strcmp(rply_ptr0, "RMT") == 0) && (strcmp(rply_ptr1, "\n0") == 0)) { + EL734_call_depth--; + return True; + } + + EL734_SetErrcode(info_ptr, rply_ptr0, "RMT"); + return False; +} + /* **--------------------------------------------------------------------------- ** EL734_PutOnline: put the EL734 on-line */ - int EL734_PutOnline ( +int EL734_PutOnline( /* =============== -*/ void **handle, - int echo) { +*/ void **handle, + int echo) +{ - int status, my_echo; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status, my_echo; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_PutOnline")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_PutOnline")) + return False; + /*---------------------------------------------- */ - if ((echo != 0) && (echo != 1) && (echo != 2)) { - EL734_errcode = EL734__BAD_PAR; return False; - } - /*---------------------------------------------- + if ((echo != 0) && (echo != 1) && (echo != 2)) { + EL734_errcode = EL734__BAD_PAR; + return False; + } + /*---------------------------------------------- ** The problem which this routine has is that the state ** of the EL734 is not known. The following is, therefore, ** rather pedantic for most cases which occur in practice. */ - sprintf (cmnd0, "echo %d\r", echo); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "\r", "rmt 1\r", cmnd0, NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - } - - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "echo\r", NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; return False;} - - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (strcmp (rply_ptr0, "ECHO") == 0) { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - } - if ((sscanf (rply_ptr0, "%d", &my_echo) == 1) && (my_echo == echo)) { - EL734_call_depth--; - return True; - } - - EL734_SetErrcode (info_ptr, rply_ptr0, "ECHO"); + sprintf(cmnd0, "echo %d\r", echo); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "\r", "rmt 1\r", cmnd0, NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; return False; } + + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "echo\r", NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } + + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (strcmp(rply_ptr0, "ECHO") == 0) { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + } + if ((sscanf(rply_ptr0, "%d", &my_echo) == 1) && (my_echo == echo)) { + EL734_call_depth--; + return True; + } + + EL734_SetErrcode(info_ptr, rply_ptr0, "ECHO"); + return False; +} + /* **--------------------------------------------------------------------------- ** EL734_SendCmnd - Send a command to RS232C server. */ - int EL734_SendCmnd ( +int EL734_SendCmnd( /* ============== -*/ void **handle, - char *cmnd, - char *rply, - int rply_size) { +*/ void **handle, + char *cmnd, char *rply, int rply_size) +{ - struct EL734info *info_ptr; - int my_status; - char *rply_ptr; - /*---------------------------------------------- + struct EL734info *info_ptr; + int my_status; + char *rply_ptr; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_SendCmnd")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_SendCmnd")) + return False; + /*---------------------------------------------- ** Send command to EL734. */ - my_status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!my_status) { - EL734_errcode = EL734__BAD_ASYNSRV; return False; - }else { - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr == NULL) rply_ptr = "?no_response"; - StrJoin (rply, rply_size, rply_ptr, ""); - } - - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; + my_status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!my_status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr == NULL) + rply_ptr = "?no_response"; + StrJoin(rply, rply_size, rply_ptr, ""); } + + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL734_SetAirCush: Set the air-cushion register (AC register) */ - int EL734_SetAirCush ( +int EL734_SetAirCush( /* ================ -*/ void **handle, - int state) { +*/ void **handle, + int state) +{ - int status, dum1, my_state; - struct EL734info *info_ptr; - char cmnd0[32]; - char *rply_ptr0; - /*---------------------------------------------- + int status, dum1, my_state; + struct EL734info *info_ptr; + char cmnd0[32]; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_SetAirCush")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_SetAirCush")) + return False; + /*---------------------------------------------- ** Send AC cmnd to EL734 */ - if (state != 0) state = 1; - sprintf (cmnd0, "ac %d %d\r", info_ptr->motor, state); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (*rply_ptr0 == '\0') { - /* - ** The command was accepted - check value is set OK. - */ - status = EL734_GetAirCush (handle, &dum1, &my_state); - if (!status) return False; - if (state != my_state) { - EL734_errcode = EL734__VFY_ERR; - return False; - } - EL734_call_depth--; - return True; + if (state != 0) + state = 1; + sprintf(cmnd0, "ac %d %d\r", info_ptr->motor, state); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (*rply_ptr0 == '\0') { + /* + ** The command was accepted - check value is set OK. + */ + status = EL734_GetAirCush(handle, &dum1, &my_state); + if (!status) + return False; + if (state != my_state) { + EL734_errcode = EL734__VFY_ERR; + return False; } - EL734_SetErrcode (info_ptr, rply_ptr0, "AC"); - return False; + EL734_call_depth--; + return True; } + EL734_SetErrcode(info_ptr, rply_ptr0, "AC"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_SetErrcode - Set up EL734_errcode */ - int EL734_SetErrcode ( +int EL734_SetErrcode( /* ================ -*/ struct EL734info *info_ptr, - char *response, - char *cmnd) { +*/ struct EL734info *info_ptr, + char *response, char *cmnd) +{ - int status; - char *rply; - char tmo_save[4]; - char eot_save[4]; + int status; + char *rply; + char tmo_save[4]; + char eot_save[4]; - EL734_errcode = EL734__BAD_ILLG; - if (strcmp (response, "?OFL") == 0) EL734_errcode = EL734__BAD_OFL; - if (strcmp (response, "?CMD") == 0) EL734_errcode = EL734__BAD_CMD; - if (strcmp (response, "?LOC") == 0) EL734_errcode = EL734__BAD_LOC; - if (strcmp (response, "?ADR") == 0) EL734_errcode = EL734__BAD_ADR; - if (strcmp (response, "?RNG") == 0) EL734_errcode = EL734__BAD_RNG; - if (strcmp (response, "*ES") == 0) EL734_errcode = EL734__EMERG_STOP; - if (strcmp (response, "*MS" ) == 0) EL734_errcode = EL734__BAD_STP; - if (strncmp (response, "?TMO", 4) == 0) EL734_errcode = EL734__BAD_TMO; - if (EL734_errcode != EL734__BAD_ILLG) return EL734_errcode; - /* - ** The response is not recognised. Perhaps the emergency stop - ** signal is set. To check this, it is necessary to turn off - ** terminator checking since the EL734 prefixes its "*ES" - ** response with a character. We also therefore set - ** a very short time-out. - */ - memcpy (tmo_save, info_ptr->asyn_info.tmo, 4); /* Save time-out */ - memcpy (eot_save, info_ptr->asyn_info.eot, 4); /* Save terminators */ - AsynSrv_Config (&info_ptr->asyn_info, - "msecTmo", 100, /* Set short time-out */ - "eot", "0", /* Set no terminator */ - NULL); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "ID\r", NULL); - memcpy (info_ptr->asyn_info.eot, eot_save, 4); /* Restore terminators */ - memcpy (info_ptr->asyn_info.tmo, tmo_save, 4); /* Restore time-out */ - if (status) { - rply = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply == NULL) rply = "?no_response"; - }else { - rply = "?no_response"; - } - if (strstr (rply, "*ES") != NULL) EL734_errcode = EL734__EMERG_STOP; - - if ((EL734_errcode == EL734__BAD_ILLG) && (cmnd != NULL)) { - fprintf (stderr, " Unrecognised response to \"%s\" command: \"%s\"\n", - cmnd, response); - strcpy (EL734_IllgText, cmnd); - strcat (EL734_IllgText, " : "); - strcat (EL734_IllgText, response); - } + EL734_errcode = EL734__BAD_ILLG; + if (strcmp(response, "?OFL") == 0) + EL734_errcode = EL734__BAD_OFL; + if (strcmp(response, "?CMD") == 0) + EL734_errcode = EL734__BAD_CMD; + if (strcmp(response, "?LOC") == 0) + EL734_errcode = EL734__BAD_LOC; + if (strcmp(response, "?ADR") == 0) + EL734_errcode = EL734__BAD_ADR; + if (strcmp(response, "?RNG") == 0) + EL734_errcode = EL734__BAD_RNG; + if (strcmp(response, "*ES") == 0) + EL734_errcode = EL734__EMERG_STOP; + if (strcmp(response, "*MS") == 0) + EL734_errcode = EL734__BAD_STP; + if (strncmp(response, "?TMO", 4) == 0) + EL734_errcode = EL734__BAD_TMO; + if (EL734_errcode != EL734__BAD_ILLG) return EL734_errcode; + /* + ** The response is not recognised. Perhaps the emergency stop + ** signal is set. To check this, it is necessary to turn off + ** terminator checking since the EL734 prefixes its "*ES" + ** response with a character. We also therefore set + ** a very short time-out. + */ + memcpy(tmo_save, info_ptr->asyn_info.tmo, 4); /* Save time-out */ + memcpy(eot_save, info_ptr->asyn_info.eot, 4); /* Save terminators */ + AsynSrv_Config(&info_ptr->asyn_info, "msecTmo", 100, /* Set short time-out */ + "eot", "0", /* Set no terminator */ + NULL); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "ID\r", NULL); + memcpy(info_ptr->asyn_info.eot, eot_save, 4); /* Restore terminators */ + memcpy(info_ptr->asyn_info.tmo, tmo_save, 4); /* Restore time-out */ + if (status) { + rply = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply == NULL) + rply = "?no_response"; + } else { + rply = "?no_response"; } + if (strstr(rply, "*ES") != NULL) + EL734_errcode = EL734__EMERG_STOP; + + if ((EL734_errcode == EL734__BAD_ILLG) && (cmnd != NULL)) { + fprintf(stderr, " Unrecognised response to \"%s\" command: \"%s\"\n", + cmnd, response); + strcpy(EL734_IllgText, cmnd); + strcat(EL734_IllgText, " : "); + strcat(EL734_IllgText, response); + } + return EL734_errcode; +} + /* **--------------------------------------------------------------------------- ** EL734_SetHighSpeed: Set the max speed (J register) */ - int EL734_SetHighSpeed ( +int EL734_SetHighSpeed( /* ================== -*/ void **handle, - int hi) { +*/ void **handle, + int hi) +{ - int status, my_lo, my_hi, my_ramp; - struct EL734info *info_ptr; - char cmnd0[32]; - char *rply_ptr0; - /*---------------------------------------------- + int status, my_lo, my_hi, my_ramp; + struct EL734info *info_ptr; + char cmnd0[32]; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_SetHighSpeed")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_SetHighSpeed")) + return False; + /*---------------------------------------------- ** Send J cmnd to EL734 */ - sprintf (cmnd0, "j %d %d\r", info_ptr->motor, hi); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (*rply_ptr0 == '\0') { - /* - ** The command was accepted - check value is set OK. - */ - status = EL734_GetSpeeds (handle, &my_lo, &my_hi, &my_ramp); - if (!status) return False; - if (hi != my_hi) { - EL734_errcode = EL734__VFY_ERR; - return False; - } - EL734_call_depth--; - return True; + sprintf(cmnd0, "j %d %d\r", info_ptr->motor, hi); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (*rply_ptr0 == '\0') { + /* + ** The command was accepted - check value is set OK. + */ + status = EL734_GetSpeeds(handle, &my_lo, &my_hi, &my_ramp); + if (!status) + return False; + if (hi != my_hi) { + EL734_errcode = EL734__VFY_ERR; + return False; } - EL734_SetErrcode (info_ptr, rply_ptr0, "J"); - return False; + EL734_call_depth--; + return True; } + EL734_SetErrcode(info_ptr, rply_ptr0, "J"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_SetLowSpeed: Set the start/stop speed (G register) */ - int EL734_SetLowSpeed ( +int EL734_SetLowSpeed( /* ================= -*/ void **handle, - int lo) { +*/ void **handle, + int lo) +{ - int status, my_lo, my_hi, my_ramp; - struct EL734info *info_ptr; - char cmnd0[32]; - char *rply_ptr0; - /*---------------------------------------------- + int status, my_lo, my_hi, my_ramp; + struct EL734info *info_ptr; + char cmnd0[32]; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_SetLowSpeed")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_SetLowSpeed")) + return False; + /*---------------------------------------------- ** Send G cmnd to EL734 */ - sprintf (cmnd0, "g %d %d\r", info_ptr->motor, lo); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (*rply_ptr0 == '\0') { - /* - ** The command was accepted - check value is set OK. - */ - status = EL734_GetSpeeds (handle, &my_lo, &my_hi, &my_ramp); - if (!status) return False; - if (lo != my_lo) { - EL734_errcode = EL734__VFY_ERR; - return False; - } - EL734_call_depth--; - return True; + sprintf(cmnd0, "g %d %d\r", info_ptr->motor, lo); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (*rply_ptr0 == '\0') { + /* + ** The command was accepted - check value is set OK. + */ + status = EL734_GetSpeeds(handle, &my_lo, &my_hi, &my_ramp); + if (!status) + return False; + if (lo != my_lo) { + EL734_errcode = EL734__VFY_ERR; + return False; } - EL734_SetErrcode (info_ptr, rply_ptr0, "G"); - return False; + EL734_call_depth--; + return True; } + EL734_SetErrcode(info_ptr, rply_ptr0, "G"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_SetRamp: Set the start/stop ramp (E register) */ - int EL734_SetRamp ( +int EL734_SetRamp( /* ============= -*/ void **handle, - int ramp) { +*/ void **handle, + int ramp) +{ - int status, my_lo, my_hi, my_ramp; - struct EL734info *info_ptr; - char cmnd0[32]; - char *rply_ptr0; - /*---------------------------------------------- + int status, my_lo, my_hi, my_ramp; + struct EL734info *info_ptr; + char cmnd0[32]; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_SetRamp")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_SetRamp")) + return False; + /*---------------------------------------------- ** Send E cmnd to EL734 */ - sprintf (cmnd0, "e %d %d\r", info_ptr->motor, ramp); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (*rply_ptr0 == '\0') { - /* - ** The command was accepted - check value is set OK. - */ - status = EL734_GetSpeeds (handle, &my_lo, &my_hi, &my_ramp); - if (!status) return False; - if (ramp != my_ramp) { - EL734_errcode = EL734__VFY_ERR; - return False; - } - EL734_call_depth--; - return True; + sprintf(cmnd0, "e %d %d\r", info_ptr->motor, ramp); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (*rply_ptr0 == '\0') { + /* + ** The command was accepted - check value is set OK. + */ + status = EL734_GetSpeeds(handle, &my_lo, &my_hi, &my_ramp); + if (!status) + return False; + if (ramp != my_ramp) { + EL734_errcode = EL734__VFY_ERR; + return False; } - EL734_SetErrcode (info_ptr, rply_ptr0, "E"); - return False; + EL734_call_depth--; + return True; } + EL734_SetErrcode(info_ptr, rply_ptr0, "E"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_Stop: Send a stop command to motor. */ - int EL734_Stop ( +int EL734_Stop( /* ========== -*/ void **handle) { +*/ void **handle) +{ - int status; - struct EL734info *info_ptr; - char cmnd0[10]; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL734info *info_ptr; + char cmnd0[10]; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL734info *) *handle; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_Stop")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_Stop")) + return False; + /*---------------------------------------------- ** Send S cmnd to EL734 */ - sprintf (cmnd0, "s %d\r", info_ptr->motor); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd0, NULL); - if (!status) { - EL734_errcode = EL734__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?no_response"; - if (rply_ptr0[0] == '\0') { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - EL734_SetErrcode (info_ptr, rply_ptr0, "S"); - return False; + sprintf(cmnd0, "s %d\r", info_ptr->motor); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd0, NULL); + if (!status) { + EL734_errcode = EL734__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?no_response"; + if (rply_ptr0[0] == '\0') { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } + EL734_SetErrcode(info_ptr, rply_ptr0, "S"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL734_WaitIdle: Wait till MSR goes to zero. */ - int EL734_WaitIdle ( +int EL734_WaitIdle( /* ============== -*/ void **handle, - int *ored_msr, - int *fp_cntr, - int *fr_cntr, - float *ist_posit) { +*/ void **handle, + int *ored_msr, + int *fp_cntr, int *fr_cntr, float *ist_posit) +{ #ifdef __VMS #include #define hibernate lib$wait (0.25) #else #include #include - struct timespec delay = {0, 250000000}; - struct timespec delay_left; - -#ifdef LINUX + struct timespec delay = { 0, 250000000 }; + struct timespec delay_left; + +#ifdef LINUX #define hibernate nanosleep(&delay, &delay_left) #else #define hibernate nanosleep_d9 (&delay, &delay_left) #endif #endif - int msr, ss; - struct EL734info *info_ptr; - /*---------------------------------------------- + int msr, ss; + struct EL734info *info_ptr; + /*---------------------------------------------- */ - *ored_msr = *fp_cntr = *fr_cntr = -1; *ist_posit = 0.0; - info_ptr = (struct EL734info *) *handle; + *ored_msr = *fp_cntr = *fr_cntr = -1; + *ist_posit = 0.0; + info_ptr = (struct EL734info *) *handle; - if (!EL734_AddCallStack (info_ptr, "EL734_WaitIdle")) return False; - /*---------------------------------------------- + if (!EL734_AddCallStack(info_ptr, "EL734_WaitIdle")) + return False; + /*---------------------------------------------- ** Poll the motor status till not moving. */ - while (EL734_GetStatus (handle, - &msr, ored_msr, fp_cntr, fr_cntr, &ss, ist_posit)) { - if ((msr & MSR__BUSY) == 0) { - if (EL734_errcode != 0) return False; - EL734_call_depth--; - return True; - } - hibernate; + while (EL734_GetStatus(handle, + &msr, ored_msr, fp_cntr, fr_cntr, &ss, + ist_posit)) { + if ((msr & MSR__BUSY) == 0) { + if (EL734_errcode != 0) + return False; + EL734_call_depth--; + return True; } - return False; /* Error detected in EL734_GetStatus */ + hibernate; } + return False; /* Error detected in EL734_GetStatus */ +} + /* **--------------------------------------------------------------------------- ** EL734_ZeroStatus: Zero the "ored-MSR" and fault counters. */ - void EL734_ZeroStatus ( +void EL734_ZeroStatus( /* ================ -*/ void **handle) { +*/ void **handle) +{ - struct EL734info *info_ptr; - /* - ** Do nothing if no handle! - */ - info_ptr = (struct EL734info *) *handle; - if (info_ptr == NULL) return; - /* - ** Zero the data structure items. - */ - info_ptr->ored_msr = 0; - info_ptr->fp_cntr = 0; - info_ptr->fr_cntr = 0; - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); + struct EL734info *info_ptr; + /* + ** Do nothing if no handle! + */ + info_ptr = (struct EL734info *) *handle; + if (info_ptr == NULL) return; - } + /* + ** Zero the data structure items. + */ + info_ptr->ored_msr = 0; + info_ptr->fp_cntr = 0; + info_ptr->fr_cntr = 0; + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return; +} + /*-------------------------------------------- End of EL734_Utility.C =======*/ diff --git a/hardsup/el734fix.h b/hardsup/el734fix.h index e14df3a..e05a1f5 100644 --- a/hardsup/el734fix.h +++ b/hardsup/el734fix.h @@ -9,7 +9,7 @@ #define EL734__BAD_HOST ASYNSRV__BAD_HOST #define EL734__BAD_BIND ASYNSRV__BAD_BIND -#define EL734__BAD_SENDLEN ASYNSRV__BAD_SEND_LEN +#define EL734__BAD_SENDLEN ASYNSRV__BAD_SEND_LEN #define EL734__BAD_SEND ASYNSRV__BAD_SEND #define EL734__BAD_SEND_PIPE ASYNSRV__BAD_SEND_PIPE #define EL734__BAD_SEND_UNKN ASYNSRV__BAD_SEND_UNKN @@ -26,4 +26,4 @@ #define EL734__BAD_RECV1_NET ASYNSRV__BAD_RECV1_NET #define EL734__BAD_CONNECT ASYNSRV__BAD_CONNECT #define EL734__BAD_ID EL734__BAD_DEV -#endif /* el734fix */ +#endif /* el734fix */ diff --git a/hardsup/el734tcl.c b/hardsup/el734tcl.c index c4dc11a..4c97953 100644 --- a/hardsup/el734tcl.c +++ b/hardsup/el734tcl.c @@ -24,20 +24,20 @@ #define False 0 #define True 1 - typedef struct - { - float fUpper; /* upper limit */ - float fLower; /* Lower Limit */ - int iFix; /* fixed, unfixed flag */ - float fSoftZero; /* SW-zero point */ - float fSoftUpper; /* software upper boundary*/ - float fSoftLower; /* " lower " */ - int iLowFlag, iUpFlag, iZeroFlag; /*activity flags */ - void *pData; /* EL734 open struct */ - } EL734st; +typedef struct { + float fUpper; /* upper limit */ + float fLower; /* Lower Limit */ + int iFix; /* fixed, unfixed flag */ + float fSoftZero; /* SW-zero point */ + float fSoftUpper; /* software upper boundary */ + float fSoftLower; /* " lower " */ + int iLowFlag, iUpFlag, iZeroFlag; /*activity flags */ + void *pData; /* EL734 open struct */ +} EL734st; - EXTERN int EL734Action(ClientData pDat, Tcl_Interp *i, int a, char *argv[]); - static void EL734Error2Text(char *pBuffer, int errcode); +EXTERN int EL734Action(ClientData pDat, Tcl_Interp * i, int a, + char *argv[]); +static void EL734Error2Text(char *pBuffer, int errcode); /*--------------------------------------------------------------------------- Tcl has a high niceness level. It deletes a command properly when @@ -46,173 +46,160 @@ ---------------------------------------------------------------------------*/ EXTERN void EL734Murder(ClientData pData) { - EL734st *pTa = (EL734st *)pData; - EL734_Close(&(pTa->pData)); - free(pData); + EL734st *pTa = (EL734st *) pData; + EL734_Close(&(pTa->pData)); + free(pData); } + /*---------------------------------------------------------------------------- EL734 is the main entry point for this stuff. It connects to a motor and, on success, creates a new command with the name of the motor. Syntax: EL734 name host port channel index ----------------------------------------------------------------------------*/ +---------------------------------------------------------------------------*/ -int EL734(ClientData clientData, Tcl_Interp *interp, - int argc, char *argv[]) +int EL734(ClientData clientData, Tcl_Interp * interp, + int argc, char *argv[]) { - int iRet; - EL734st *pEL734 = NULL; - int iPort, iChannel, iMotor; - char *pErr = NULL; - char pBueffel[80]; - - /* check arguments */ - if(argc < 6) - { - Tcl_AppendResult(interp, - " Insufficient arguments: EL734 name host port channel index" - , (char *) NULL); - return TCL_ERROR; - } - - /* convert arguments */ - iRet = Tcl_GetInt(interp,argv[3],&iPort); - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need integer value for port", - (char *)NULL); - return iRet; - } - - iRet = Tcl_GetInt(interp,argv[4],&iChannel); - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need integer value for channel", - (char *)NULL); - return iRet; - } + int iRet; + EL734st *pEL734 = NULL; + int iPort, iChannel, iMotor; + char *pErr = NULL; + char pBueffel[80]; - iRet = Tcl_GetInt(interp,argv[5],&iMotor); - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need integer value for motor", - (char *)NULL); - return iRet; - } - - /* make a new pointer, initialise EL734st */ - pEL734 = (EL734st *)malloc(sizeof(EL734st)); - if(pEL734 ==NULL) - { - Tcl_AppendResult(interp,"No memory in EL734",NULL); - return TCL_ERROR; - } - pEL734->iFix = False; - pEL734->fSoftZero = INACTIVE+1; - pEL734->fSoftUpper = INACTIVE+1.; - pEL734->fSoftLower = -INACTIVE-1.; - pEL734->iZeroFlag = False; - pEL734->iLowFlag = False; - pEL734->iUpFlag = False; + /* check arguments */ + if (argc < 6) { + Tcl_AppendResult(interp, + " Insufficient arguments: EL734 name host port channel index", + (char *) NULL); + return TCL_ERROR; + } - /* open the motor, finally */ - iRet = EL734_Open(&(pEL734->pData), argv[2],iPort,iChannel,iMotor,"STPMC EL734"); - if(iRet) /* success */ - { - /* figure out motor limits */ - EL734_GetLimits(&(pEL734->pData),&(pEL734->fLower), - &(pEL734->fUpper)); - /* handle TCL, create new command: the motor */ - Tcl_CreateCommand(interp,strdup(argv[1]),EL734Action, - (ClientData)pEL734,EL734Murder); - Tcl_AppendResult(interp,strdup(argv[1]),(char *)NULL); - return TCL_OK; - } - else - { - EL734_ErrInfo(&pErr,&iPort,&iChannel, &iMotor); - EL734Error2Text(pBueffel,iPort); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - free(pEL734); - return TCL_ERROR; - } + /* convert arguments */ + iRet = Tcl_GetInt(interp, argv[3], &iPort); + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need integer value for port", (char *) NULL); + return iRet; + } + + iRet = Tcl_GetInt(interp, argv[4], &iChannel); + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need integer value for channel", + (char *) NULL); + return iRet; + } + + iRet = Tcl_GetInt(interp, argv[5], &iMotor); + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need integer value for motor", + (char *) NULL); + return iRet; + } + + /* make a new pointer, initialise EL734st */ + pEL734 = (EL734st *) malloc(sizeof(EL734st)); + if (pEL734 == NULL) { + Tcl_AppendResult(interp, "No memory in EL734", NULL); + return TCL_ERROR; + } + pEL734->iFix = False; + pEL734->fSoftZero = INACTIVE + 1; + pEL734->fSoftUpper = INACTIVE + 1.; + pEL734->fSoftLower = -INACTIVE - 1.; + pEL734->iZeroFlag = False; + pEL734->iLowFlag = False; + pEL734->iUpFlag = False; + + /* open the motor, finally */ + iRet = + EL734_Open(&(pEL734->pData), argv[2], iPort, iChannel, iMotor, + "STPMC EL734"); + if (iRet) { /* success */ + /* figure out motor limits */ + EL734_GetLimits(&(pEL734->pData), &(pEL734->fLower), + &(pEL734->fUpper)); + /* handle TCL, create new command: the motor */ + Tcl_CreateCommand(interp, strdup(argv[1]), EL734Action, + (ClientData) pEL734, EL734Murder); + Tcl_AppendResult(interp, strdup(argv[1]), (char *) NULL); + return TCL_OK; + } else { + EL734_ErrInfo(&pErr, &iPort, &iChannel, &iMotor); + EL734Error2Text(pBueffel, iPort); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + free(pEL734); + return TCL_ERROR; + } } + /*--------------------------------------------------------------------------- CheckPos checks a position and converts it to a real position. Returns TCL_ERROR on mistake, TCL_OK else ---------------------------------------------------------------------------*/ - static int CheckPos(Tcl_Interp *interp, EL734st *pData, - float fRequested, float *fDrive) - { - float fPos; - char pBueffel[132]; - - /* fixed ? */ - if(pData->iFix) - { - Tcl_AppendResult(interp,"Motor fixed",NULL); - return TCL_ERROR; - } - - /* check against SW-boundaries */ - if(pData->iUpFlag) - { - if(fRequested > pData->fSoftUpper) - { - sprintf(pBueffel, - "Requested position: %f violates SW-boundary %f", - fRequested, pData->fSoftUpper); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - } - if(pData->iLowFlag) - { - if(fRequested < pData->fSoftLower) - { - sprintf(pBueffel, - "Requested position: %f violates SW-boundary %f", - fRequested, pData->fSoftLower); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - } - - /* correct for zero point */ - if(pData->iZeroFlag) - { - fPos = fRequested - pData->fSoftZero; - } - else - { - fPos = fRequested; - } - - /* check HW-boundaries */ - if( (fPos < pData->fLower) || (fPos > pData->fUpper) ) - { - sprintf(pBueffel," %f outside limits %f %f", - fPos,pData->fLower, pData->fUpper); - Tcl_AppendResult(interp,"Requested position: ", - pBueffel,(char *)NULL); - return TCL_ERROR; - } - - *fDrive = fPos; - return TCL_OK; - - } +static int CheckPos(Tcl_Interp * interp, EL734st * pData, + float fRequested, float *fDrive) +{ + float fPos; + char pBueffel[132]; + + /* fixed ? */ + if (pData->iFix) { + Tcl_AppendResult(interp, "Motor fixed", NULL); + return TCL_ERROR; + } + + /* check against SW-boundaries */ + if (pData->iUpFlag) { + if (fRequested > pData->fSoftUpper) { + sprintf(pBueffel, + "Requested position: %f violates SW-boundary %f", + fRequested, pData->fSoftUpper); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + } + if (pData->iLowFlag) { + if (fRequested < pData->fSoftLower) { + sprintf(pBueffel, + "Requested position: %f violates SW-boundary %f", + fRequested, pData->fSoftLower); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + } + + /* correct for zero point */ + if (pData->iZeroFlag) { + fPos = fRequested - pData->fSoftZero; + } else { + fPos = fRequested; + } + + /* check HW-boundaries */ + if ((fPos < pData->fLower) || (fPos > pData->fUpper)) { + sprintf(pBueffel, " %f outside limits %f %f", + fPos, pData->fLower, pData->fUpper); + Tcl_AppendResult(interp, "Requested position: ", + pBueffel, (char *) NULL); + return TCL_ERROR; + } + + *fDrive = fPos; + return TCL_OK; + +} + /* ------------------------------------------------------------------------- fucking standard library missing functionality!!!!!!!!!!!!!!!! ---------------------------------------------------------------------------*/ - static float absf(float x) - { - if(x < .0) - return -x; - else - return x; - } - +static float absf(float x) +{ + if (x < .0) + return -x; + else + return x; +} + /*-------------------------------------------------------------------------- EL734 Action is the routine where commands send to the motor will @@ -230,415 +217,367 @@ int EL734(ClientData clientData, Tcl_Interp *interp, motor upper val sets a software upper limit motor lower val sets a software lower limit ----------------------------------------------------------------------------*/ -EXTERN int EL734Action(ClientData clientData, Tcl_Interp *interp, - int argc, char *argv[]) +EXTERN int EL734Action(ClientData clientData, Tcl_Interp * interp, + int argc, char *argv[]) { - EL734st *pData = (EL734st *)clientData; - char pBueffel[80]; - char *pErr = NULL; - int iRet, iMSR, iOMSR, iFPC, iFRC, iSS; - float fPos, fNpos; - double dPos; - int i; - struct RS__RplyStruct *pReply = NULL; + EL734st *pData = (EL734st *) clientData; + char pBueffel[80]; + char *pErr = NULL; + int iRet, iMSR, iOMSR, iFPC, iFRC, iSS; + float fPos, fNpos; + double dPos; + int i; + struct RS__RplyStruct *pReply = NULL; - /* check parameters */ - if(argc < 2) - { - Tcl_AppendResult(interp, - "Usage: motor and either dr, pos, hlim slim run zero up lo",(char *)NULL); - return TCL_ERROR; - } - if(pData == NULL) - { - Tcl_AppendResult(interp, - "Motor data lost!!!!!!!!",(char *)NULL); - return TCL_ERROR; - } - - /* check for HW-lim */ - if(strcmp(argv[1],"hlim") == 0) - { - sprintf(pBueffel," %f %f",pData->fLower,pData->fUpper); - Tcl_AppendResult(interp,pBueffel,(char *)NULL); - return TCL_OK; - } + /* check parameters */ + if (argc < 2) { + Tcl_AppendResult(interp, + "Usage: motor and either dr, pos, hlim slim run zero up lo", + (char *) NULL); + return TCL_ERROR; + } + if (pData == NULL) { + Tcl_AppendResult(interp, "Motor data lost!!!!!!!!", (char *) NULL); + return TCL_ERROR; + } - /* check for SW-lim */ - if(strcmp(argv[1],"slim") == 0) - { - sprintf(pBueffel," %f %f",pData->fSoftLower,pData->fSoftUpper); - Tcl_AppendResult(interp,pBueffel,(char *)NULL); - return TCL_OK; - } - - /* fix & unfix */ - if(strcmp(argv[1],"fix") == 0) - { - pData->iFix = True; - return TCL_OK; - } - if(strcmp(argv[1],"unfix") == 0) - { - pData->iFix = False; - return TCL_OK; - } - - /* reset */ - if(strcmp(argv[1],"reset")== 0) - { - pData->iFix = False; - pData->iLowFlag = False; - pData->iUpFlag = False; - pData->iZeroFlag = False; - return TCL_OK; - } - - /* check for pos */ - if(strcmp(argv[1],"pos") == 0) - { - iRet = EL734_GetStatus(&(pData->pData), - &iMSR, - &iOMSR, - &iFPC, - &iFRC, - &iSS, - &fPos); - if(!iRet) - { - EL734_ErrInfo(&pErr,&iMSR,&iOMSR, &iSS); - EL734Error2Text(pBueffel,iMSR); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - else - { - if(pData->iZeroFlag) - { - fPos += pData->fSoftZero; - } - sprintf(pBueffel," %f",fPos); - Tcl_AppendResult(interp,pBueffel,NULL); - return TCL_OK; - } - } + /* check for HW-lim */ + if (strcmp(argv[1], "hlim") == 0) { + sprintf(pBueffel, " %f %f", pData->fLower, pData->fUpper); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_OK; + } - /* zero point */ - if(strcmp(argv[1],"zero") == 0) - { - /* check for zero already been defined */ - if(pData->iZeroFlag) - { - Tcl_AppendResult(interp, - "Request to set new zero point rejected.", - " Use reset before new definition. ", - " I'll get confused otherwise ", - NULL); - return TCL_ERROR; - } - - /* get the new position */ - if(argc < 3) - { - Tcl_AppendResult(interp, - "Usage: motor zero val",NULL); - return TCL_ERROR; - } + /* check for SW-lim */ + if (strcmp(argv[1], "slim") == 0) { + sprintf(pBueffel, " %f %f", pData->fSoftLower, pData->fSoftUpper); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_OK; + } - iRet = Tcl_GetDouble(interp,argv[2],&dPos); - fNpos = dPos; - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need float value for new zeropint", - (char *)NULL); - return iRet; - } - pData->fSoftZero = -fNpos; - pData->iZeroFlag = True; - return TCL_OK; - } - - /* upper SW-limit */ - if(strcmp(argv[1],"up") == 0) - { - /* get the new position */ - if(argc < 3) - { - Tcl_AppendResult(interp, - "Usage: motor up val",NULL); - return TCL_ERROR; - } + /* fix & unfix */ + if (strcmp(argv[1], "fix") == 0) { + pData->iFix = True; + return TCL_OK; + } + if (strcmp(argv[1], "unfix") == 0) { + pData->iFix = False; + return TCL_OK; + } - iRet = Tcl_GetDouble(interp,argv[2],&dPos); - fNpos = dPos; - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need float value for new upper limit", - (char *)NULL); - return iRet; - } - pData->fSoftUpper = fNpos; - pData->iUpFlag = True; - return TCL_OK; - } + /* reset */ + if (strcmp(argv[1], "reset") == 0) { + pData->iFix = False; + pData->iLowFlag = False; + pData->iUpFlag = False; + pData->iZeroFlag = False; + return TCL_OK; + } - /* lower SW-limit */ - if(strcmp(argv[1],"lo") == 0) - { - /* get the new position */ - if(argc < 3) - { - Tcl_AppendResult(interp, - "Usage: motor lo val",NULL); - return TCL_ERROR; - } + /* check for pos */ + if (strcmp(argv[1], "pos") == 0) { + iRet = EL734_GetStatus(&(pData->pData), + &iMSR, &iOMSR, &iFPC, &iFRC, &iSS, &fPos); + if (!iRet) { + EL734_ErrInfo(&pErr, &iMSR, &iOMSR, &iSS); + EL734Error2Text(pBueffel, iMSR); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } else { + if (pData->iZeroFlag) { + fPos += pData->fSoftZero; + } + sprintf(pBueffel, " %f", fPos); + Tcl_AppendResult(interp, pBueffel, NULL); + return TCL_OK; + } + } - iRet = Tcl_GetDouble(interp,argv[2],&dPos); - fNpos = dPos; - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need float value for new lower limit", - (char *)NULL); - return iRet; - } - pData->fSoftLower = fNpos; - pData->iLowFlag = True; - return TCL_OK; - } - + /* zero point */ + if (strcmp(argv[1], "zero") == 0) { + /* check for zero already been defined */ + if (pData->iZeroFlag) { + Tcl_AppendResult(interp, + "Request to set new zero point rejected.", + " Use reset before new definition. ", + " I'll get confused otherwise ", NULL); + return TCL_ERROR; + } - - /* this is most important: dr for Drive */ - if(strcmp(argv[1],"dr") == 0) - { - /* get the new position */ - if(argc < 3) - { - Tcl_AppendResult(interp, - "Usage: motor dr val",NULL); - return TCL_ERROR; - } + /* get the new position */ + if (argc < 3) { + Tcl_AppendResult(interp, "Usage: motor zero val", NULL); + return TCL_ERROR; + } - iRet = Tcl_GetDouble(interp,argv[2],&dPos); - fNpos = dPos; - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need value to drive to", - (char *)NULL); - return iRet; - } - - /* check if compatible with limits */ - if(CheckPos(interp,pData,fNpos,&fPos) == TCL_ERROR) - return TCL_ERROR; + iRet = Tcl_GetDouble(interp, argv[2], &dPos); + fNpos = dPos; + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need float value for new zeropint", + (char *) NULL); + return iRet; + } + pData->fSoftZero = -fNpos; + pData->iZeroFlag = True; + return TCL_OK; + } - /* finally move */ - iRet = EL734_MoveWait(&(pData->pData), fPos, &iOMSR, - &iFPC, &iFRC,&fNpos); - /* 99.99999999999% of all code is error checking */ - if(!iRet) - { - EL734_ErrInfo(&pErr,&iMSR,&iOMSR, &iSS); - EL734Error2Text(pBueffel,iMSR); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - - /* check if driving has been done */ - if(absf(fPos-fNpos) > MOTACURRACY) - { - Tcl_AppendResult(interp, - " Motor error: inacurate driving!", - " Probably something serious is wrong ", - " Check the fucking hardware ", - NULL); - return TCL_ERROR; - } - return TCL_OK; - } - - /* this is most important: run for Driving without waiting */ - if(strcmp(argv[1],"run") == 0) - { - /* get the new position */ - if(argc < 3) - { - Tcl_AppendResult(interp, - "Usage: motor run val",NULL); - return TCL_ERROR; - } + /* upper SW-limit */ + if (strcmp(argv[1], "up") == 0) { + /* get the new position */ + if (argc < 3) { + Tcl_AppendResult(interp, "Usage: motor up val", NULL); + return TCL_ERROR; + } - iRet = Tcl_GetDouble(interp,argv[2],&dPos); - fNpos = dPos; - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need value to run for", - (char *)NULL); - return iRet; - } - - /* check if compatible with limits */ - if(CheckPos(interp,pData,fNpos,&fPos) == TCL_ERROR) - return TCL_ERROR; + iRet = Tcl_GetDouble(interp, argv[2], &dPos); + fNpos = dPos; + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need float value for new upper limit", + (char *) NULL); + return iRet; + } + pData->fSoftUpper = fNpos; + pData->iUpFlag = True; + return TCL_OK; + } - /* finally move */ - iRet = EL734_MoveNoWait (&(pData->pData), fPos); + /* lower SW-limit */ + if (strcmp(argv[1], "lo") == 0) { + /* get the new position */ + if (argc < 3) { + Tcl_AppendResult(interp, "Usage: motor lo val", NULL); + return TCL_ERROR; + } - /* 99.99999999999% of all code is error checking */ - if(!iRet) - { - EL734_ErrInfo(&pErr,&iMSR,&iOMSR, &iSS); - EL734Error2Text(pBueffel,iMSR); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - return TCL_OK; - } + iRet = Tcl_GetDouble(interp, argv[2], &dPos); + fNpos = dPos; + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need float value for new lower limit", + (char *) NULL); + return iRet; + } + pData->fSoftLower = fNpos; + pData->iLowFlag = True; + return TCL_OK; + } - /* the dangerous, undocumented expert command: com: - sends something directly to the motor. All args following - com will be concatenated in one string, closed with \r - and send to the motor . A GetReply will be invoked in order - to yield a return value. Usage by normal motor users strictly - discouraged. - */ - if(strcmp(argv[1],"com") == 0) - { - strcpy(pBueffel,argv[2]); - for(i = 3; i < argc; i++) - { - strcat(pBueffel," "); - strcat(pBueffel,argv[i]); - } - sprintf(pBueffel,"%s\r",pBueffel); - iRet = EL734_SendCmnds(&(pData->pData),pBueffel,NULL); - if(!iRet) - { - EL734_ErrInfo(&pErr,&iMSR,&iOMSR, &iSS); - EL734Error2Text(pBueffel,iMSR); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - /* fetch reply */ - pReply = (struct RS__RplyStruct *)EL734_GetReply( - &(pData->pData),NULL); - while(pReply != NULL) - { - Tcl_AppendElement(interp,pReply->rply); - pReply = (struct RS__RplyStruct *)EL734_GetReply( - &(pData->pData),pReply); - } - return TCL_OK; - } - - /* if we end here an unknown command has been sent */ - Tcl_AppendResult(interp, - "Usage: motor and either dr, run,zero, pos, hlim" - "slim up low reset fix unfix",(char *)NULL); - return TCL_ERROR; + + + /* this is most important: dr for Drive */ + if (strcmp(argv[1], "dr") == 0) { + /* get the new position */ + if (argc < 3) { + Tcl_AppendResult(interp, "Usage: motor dr val", NULL); + return TCL_ERROR; + } + + iRet = Tcl_GetDouble(interp, argv[2], &dPos); + fNpos = dPos; + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need value to drive to", (char *) NULL); + return iRet; + } + + /* check if compatible with limits */ + if (CheckPos(interp, pData, fNpos, &fPos) == TCL_ERROR) + return TCL_ERROR; + + /* finally move */ + iRet = EL734_MoveWait(&(pData->pData), fPos, &iOMSR, + &iFPC, &iFRC, &fNpos); + /* 99.99999999999% of all code is error checking */ + if (!iRet) { + EL734_ErrInfo(&pErr, &iMSR, &iOMSR, &iSS); + EL734Error2Text(pBueffel, iMSR); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + + /* check if driving has been done */ + if (absf(fPos - fNpos) > MOTACURRACY) { + Tcl_AppendResult(interp, + " Motor error: inacurate driving!", + " Probably something serious is wrong ", + " Check the fucking hardware ", NULL); + return TCL_ERROR; + } + return TCL_OK; + } + + /* this is most important: run for Driving without waiting */ + if (strcmp(argv[1], "run") == 0) { + /* get the new position */ + if (argc < 3) { + Tcl_AppendResult(interp, "Usage: motor run val", NULL); + return TCL_ERROR; + } + + iRet = Tcl_GetDouble(interp, argv[2], &dPos); + fNpos = dPos; + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need value to run for", (char *) NULL); + return iRet; + } + + /* check if compatible with limits */ + if (CheckPos(interp, pData, fNpos, &fPos) == TCL_ERROR) + return TCL_ERROR; + + /* finally move */ + iRet = EL734_MoveNoWait(&(pData->pData), fPos); + + /* 99.99999999999% of all code is error checking */ + if (!iRet) { + EL734_ErrInfo(&pErr, &iMSR, &iOMSR, &iSS); + EL734Error2Text(pBueffel, iMSR); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + return TCL_OK; + } + + /* the dangerous, undocumented expert command: com: + sends something directly to the motor. All args following + com will be concatenated in one string, closed with \r + and send to the motor . A GetReply will be invoked in order + to yield a return value. Usage by normal motor users strictly + discouraged. + */ + if (strcmp(argv[1], "com") == 0) { + strcpy(pBueffel, argv[2]); + for (i = 3; i < argc; i++) { + strcat(pBueffel, " "); + strcat(pBueffel, argv[i]); + } + sprintf(pBueffel, "%s\r", pBueffel); + iRet = EL734_SendCmnds(&(pData->pData), pBueffel, NULL); + if (!iRet) { + EL734_ErrInfo(&pErr, &iMSR, &iOMSR, &iSS); + EL734Error2Text(pBueffel, iMSR); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + /* fetch reply */ + pReply = + (struct RS__RplyStruct *) EL734_GetReply(&(pData->pData), NULL); + while (pReply != NULL) { + Tcl_AppendElement(interp, pReply->rply); + pReply = + (struct RS__RplyStruct *) EL734_GetReply(&(pData->pData), + pReply); + } + return TCL_OK; + } + + /* if we end here an unknown command has been sent */ + Tcl_AppendResult(interp, + "Usage: motor and either dr, run,zero, pos, hlim" + "slim up low reset fix unfix", (char *) NULL); + return TCL_ERROR; } + /*--------------------------------------------------------------------------- EL734Error2Text converts between an EL734 error code to text -----------------------------------------------------------------------------*/ - void EL734Error2Text(char *pBuffer, int iErr) - { - switch(iErr) - { - case -28: - strcpy(pBuffer,"EL734__BAD_ADR"); - break; - case -8: - strcpy(pBuffer,"EL734__BAD_BIND"); - break; - case -30: - strcpy(pBuffer,"EL734__BAD_BSY"); - break; - case -3: - strcpy(pBuffer,"EL734__BAD_CMD"); - break; - case -9: - strcpy(pBuffer,"EL734__BAD_CONNECT"); - break; - case -23: - strcpy(pBuffer,"EL734__BAD_FLUSH"); - break; - case -6: - strcpy(pBuffer,"EL734__BAD_HOST"); - break; - case -10: - strcpy(pBuffer,"EL734__BAD_ID"); - break; - case -5: - strcpy(pBuffer,"EL734__BAD_ILLG"); - break; - case -2: - strcpy(pBuffer,"EL734__BAD_LOC"); - break; - case -11: - strcpy(pBuffer,"EL734__BAD_MALLOC"); - break; - case -21: - strcpy(pBuffer,"EL734__BAD_NOT_BCD"); - break; - case -4: - strcpy(pBuffer,"EL734__BAD_OFL"); - break; - case -29: - strcpy(pBuffer,"EL734__BAD_PAR"); - break; - - case -17: - strcpy(pBuffer,"EL734__BAD_RECV"); - break; - case -19: - strcpy(pBuffer,"EL734__BAD_RECV_NET"); - break; - case -18: - strcpy(pBuffer,"EL734__BAD_RECV_PIPE"); - break; - case -20: - strcpy(pBuffer,"EL734__BAD_RECV_UNKN"); - break; - case -22: - strcpy(pBuffer,"EL734__BAD_RECVLEN"); - break; - case -24: - strcpy(pBuffer,"EL734__BAD_RECV1"); - break; - case -26: - strcpy(pBuffer,"EL734__BAD_RECV1_NET"); - break; - case -25: - strcpy(pBuffer,"EL734__BAD_RECV1_PIPE"); - break; - case -27: - strcpy(pBuffer,"EL734__BAD_RNG"); - break; - case -13: - strcpy(pBuffer,"EL734__BAD_SEND"); - break; - case -14: - strcpy(pBuffer,"EL734__BAD_SEND_PIPE"); - break; - case -15: - strcpy(pBuffer,"EL734__BAD_SEND_NET"); - break; - case -16: - strcpy(pBuffer,"EL734__BAD_SEND_UNKN"); - break; - case -12: - strcpy(pBuffer,"EL734__BAD_SENDLEN"); - break; - case -7: - strcpy(pBuffer,"EL734__BAD_SOCKET"); - break; - case -1: - strcpy(pBuffer,"EL734__BAD_TMO"); - break; - default: - strcpy(pBuffer,"Unknown EL734 error"); - break; - } - } +void EL734Error2Text(char *pBuffer, int iErr) +{ + switch (iErr) { + case -28: + strcpy(pBuffer, "EL734__BAD_ADR"); + break; + case -8: + strcpy(pBuffer, "EL734__BAD_BIND"); + break; + case -30: + strcpy(pBuffer, "EL734__BAD_BSY"); + break; + case -3: + strcpy(pBuffer, "EL734__BAD_CMD"); + break; + case -9: + strcpy(pBuffer, "EL734__BAD_CONNECT"); + break; + case -23: + strcpy(pBuffer, "EL734__BAD_FLUSH"); + break; + case -6: + strcpy(pBuffer, "EL734__BAD_HOST"); + break; + case -10: + strcpy(pBuffer, "EL734__BAD_ID"); + break; + case -5: + strcpy(pBuffer, "EL734__BAD_ILLG"); + break; + case -2: + strcpy(pBuffer, "EL734__BAD_LOC"); + break; + case -11: + strcpy(pBuffer, "EL734__BAD_MALLOC"); + break; + case -21: + strcpy(pBuffer, "EL734__BAD_NOT_BCD"); + break; + case -4: + strcpy(pBuffer, "EL734__BAD_OFL"); + break; + case -29: + strcpy(pBuffer, "EL734__BAD_PAR"); + break; + + case -17: + strcpy(pBuffer, "EL734__BAD_RECV"); + break; + case -19: + strcpy(pBuffer, "EL734__BAD_RECV_NET"); + break; + case -18: + strcpy(pBuffer, "EL734__BAD_RECV_PIPE"); + break; + case -20: + strcpy(pBuffer, "EL734__BAD_RECV_UNKN"); + break; + case -22: + strcpy(pBuffer, "EL734__BAD_RECVLEN"); + break; + case -24: + strcpy(pBuffer, "EL734__BAD_RECV1"); + break; + case -26: + strcpy(pBuffer, "EL734__BAD_RECV1_NET"); + break; + case -25: + strcpy(pBuffer, "EL734__BAD_RECV1_PIPE"); + break; + case -27: + strcpy(pBuffer, "EL734__BAD_RNG"); + break; + case -13: + strcpy(pBuffer, "EL734__BAD_SEND"); + break; + case -14: + strcpy(pBuffer, "EL734__BAD_SEND_PIPE"); + break; + case -15: + strcpy(pBuffer, "EL734__BAD_SEND_NET"); + break; + case -16: + strcpy(pBuffer, "EL734__BAD_SEND_UNKN"); + break; + case -12: + strcpy(pBuffer, "EL734__BAD_SENDLEN"); + break; + case -7: + strcpy(pBuffer, "EL734__BAD_SOCKET"); + break; + case -1: + strcpy(pBuffer, "EL734__BAD_TMO"); + break; + default: + strcpy(pBuffer, "Unknown EL734 error"); + break; + } +} diff --git a/hardsup/el737_def.h b/hardsup/el737_def.h index 997e6f3..ce87b1f 100644 --- a/hardsup/el737_def.h +++ b/hardsup/el737_def.h @@ -18,50 +18,52 @@ #include #endif -enum EL737_States { - UNKNOWN = -2, - OFFLINE = -1, - MS = 0x0, - PTS = 0x1, - PCS = 0x2, - LRTS = 0x5, - LRCS = 0x6, - PTSP = 0x9, - PCSP = 0xA, - LRTSP = 0xD, - LRCSP = 0xE}; +enum EL737_States { + UNKNOWN = -2, + OFFLINE = -1, + MS = 0x0, + PTS = 0x1, + PCS = 0x2, + LRTS = 0x5, + LRCS = 0x6, + PTSP = 0x9, + PCSP = 0xA, + LRTSP = 0xD, + LRCSP = 0xE +}; -enum EL737_Consts { - VMECNT__PRESET_COUNT, - VMECNT__PRESET_TIME, +enum EL737_Consts { + VMECNT__PRESET_COUNT, + VMECNT__PRESET_TIME, - VMECNT__FULL, - VMECNT__SHORT, - VMECNT__INCR}; + VMECNT__FULL, + VMECNT__SHORT, + VMECNT__INCR +}; /* ** Structure to which the EL737_Open handle points. */ - struct EL737info { - struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */ - int c5, c6, c7, c8; - struct RS__MsgStruct to_host; - struct RS__RespStruct from_host; - }; +struct EL737info { + struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */ + int c5, c6, c7, c8; + struct RS__MsgStruct to_host; + struct RS__RespStruct from_host; +}; /* ** Structure holding everything that is known about a VME Neutron Counter. */ - struct Counter_State { - int state; /* RS */ - char timer[16]; /* RT \ RA */ - int cntrs[8]; /* RC 1 ... RC 8 / */ - char rates[8][16]; /* RR 1 ... RR 8 */ - char thresh_integ_time[8][16]; /* DI 1 ... DI 8 */ - char rate_integ_time[16]; /* DT */ - int analog_indx; /* DA */ - int thresh_indx; /* DR */ - char threshes[8][16]; /* DL 1 ... DL 8 */ - int mon_preset; /* MP */ - char timer_preset[16]; /* TP */ - }; +struct Counter_State { + int state; /* RS */ + char timer[16]; /* RT \ RA */ + int cntrs[8]; /* RC 1 ... RC 8 / */ + char rates[8][16]; /* RR 1 ... RR 8 */ + char thresh_integ_time[8][16]; /* DI 1 ... DI 8 */ + char rate_integ_time[16]; /* DT */ + int analog_indx; /* DA */ + int thresh_indx; /* DR */ + char threshes[8][16]; /* DL 1 ... DL 8 */ + int mon_preset; /* MP */ + char timer_preset[16]; /* TP */ +}; /*----------------------------------------------------- End of EL737_DEF.H --*/ -#endif /* _el737_def_ */ +#endif /* _el737_def_ */ diff --git a/hardsup/el737_utility.c b/hardsup/el737_utility.c index fdb3742..faa09f2 100644 --- a/hardsup/el737_utility.c +++ b/hardsup/el737_utility.c @@ -636,12 +636,12 @@ #include #ifdef __VMS - #include +#include #else - #include - #ifdef FORTIFY - #include - #endif +#include +#ifdef FORTIFY +#include +#endif #endif /*-----------------------------------------------------------------*/ #include @@ -653,1114 +653,1253 @@ /*-------------------------------------------------------------------------- ** Global Variables */ - static int EL737_call_depth = 0; - static char EL737_routine[5][64]; - static int EL737_errcode = 0; - static int EL737_errno, EL737_vaxc_errno; +static int EL737_call_depth = 0; +static char EL737_routine[5][64]; +static int EL737_errcode = 0; +static int EL737_errno, EL737_vaxc_errno; /* **--------------------------------------------------------------------------- ** EL737_AddCallStack: Add a routine name to the call stack. ** This allows EL737_ErrInfo to generate a ** trace-back in case of error. */ - int EL737_AddCallStack ( +int EL737_AddCallStack( /* ================== -*/ struct EL737info *pntr, - char *name) { +*/ struct EL737info *pntr, + char *name) +{ - if (EL737_errcode != 0) return False; + if (EL737_errcode != 0) + return False; - if (EL737_call_depth < 5) { - strcpy (EL737_routine[EL737_call_depth], name); - EL737_call_depth++; - } - - if (pntr == NULL) {EL737_errcode = EL737__NOT_OPEN; return False;} - - if (pntr->asyn_info.skt <= 0) { - memset (pntr->from_host.msg_size, - '0', sizeof (pntr->from_host.msg_size)); - EL737_errcode = (pntr->asyn_info.skt < 0) ? EL737__FORCED_CLOSED - : EL737__NO_SOCKET; - return False; - } - return True; + if (EL737_call_depth < 5) { + strcpy(EL737_routine[EL737_call_depth], name); + EL737_call_depth++; } + + if (pntr == NULL) { + EL737_errcode = EL737__NOT_OPEN; + return False; + } + + if (pntr->asyn_info.skt <= 0) { + memset(pntr->from_host.msg_size, + '0', sizeof(pntr->from_host.msg_size)); + EL737_errcode = (pntr->asyn_info.skt < 0) ? EL737__FORCED_CLOSED + : EL737__NO_SOCKET; + return False; + } + return True; +} + /* **--------------------------------------------------------------------------- ** EL737_Close: Close a connection to an EL737 counter. */ - int EL737_Close ( +int EL737_Close( /* =========== -*/ void **handle, - int force_flag) { +*/ void **handle, + int force_flag) +{ - struct EL737info *info_ptr; - char buff[4]; - - info_ptr = (struct EL737info *) *handle; - if (info_ptr == NULL) return True; - - if (info_ptr->asyn_info.skt != 0) { - if (info_ptr->asyn_info.skt > 0) { - AsynSrv_Close (*handle, force_flag); - } - } - free (*handle); - *handle = NULL; + struct EL737info *info_ptr; + char buff[4]; + info_ptr = (struct EL737info *) *handle; + if (info_ptr == NULL) return True; + + if (info_ptr->asyn_info.skt != 0) { + if (info_ptr->asyn_info.skt > 0) { + AsynSrv_Close(*handle, force_flag); + } } + free(*handle); + *handle = NULL; + + return True; +} + /* **--------------------------------------------------------------------------- ** EL737_Config: Configure a connection to an EL737 counter. */ - int EL737_Config ( +int EL737_Config( /* ============ -*/ void **handle, - ...) { +*/ void **handle, + ...) +{ - char buff[16]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - int intval; - struct EL737info *info_ptr; - /*---------------------------------------------- + char buff[16]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + int intval; + struct EL737info *info_ptr; + /*---------------------------------------------- */ - info_ptr = (struct EL737info *) *handle; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_Config")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_Config")) + return False; + /*---------------------------------------------- */ - va_start (ap, handle); /* Set up var arg machinery */ - txt_ptr = va_arg (ap, char *); /* Get pntr to first parameter ident */ - while (txt_ptr != NULL) { - if (strcmp (txt_ptr, "msecTmo") == 0) { - intval = va_arg (ap, int); - if ((intval < 100) || (intval > 999999)) { - EL737_errcode = EL737__BAD_PAR; return False; - } - sprintf (buff, "%04d", intval/100); /* Convert to ASCII as .. - ** .. deci-secs */ - memcpy (info_ptr->asyn_info.tmo, buff, 4); - }else if (strcmp (txt_ptr, "eot") == 0) { - txt_ptr = va_arg (ap, char *); - if (txt_ptr == NULL) { - EL737_errcode = EL737__BAD_PAR; return False; - } - memcpy (info_ptr->asyn_info.eot, "\0\0\0\0", 4); - switch (txt_ptr[0]) { - case '3': info_ptr->asyn_info.eot[3] = txt_ptr[3]; - case '2': info_ptr->asyn_info.eot[2] = txt_ptr[2]; - case '1': info_ptr->asyn_info.eot[1] = txt_ptr[1]; - case '0': - info_ptr->asyn_info.eot[0] = txt_ptr[0]; - break; - default: - EL737_errcode = EL737__BAD_PAR; return False; - } - }else { - EL737_errcode = EL737__BAD_PAR; return False; + va_start(ap, handle); /* Set up var arg machinery */ + txt_ptr = va_arg(ap, char *); /* Get pntr to first parameter ident */ + while (txt_ptr != NULL) { + if (strcmp(txt_ptr, "msecTmo") == 0) { + intval = va_arg(ap, int); + if ((intval < 100) || (intval > 999999)) { + EL737_errcode = EL737__BAD_PAR; + return False; } - txt_ptr = va_arg (ap, char *); /* Get pntr to next parameter ident */ + sprintf(buff, "%04d", intval / 100); /* Convert to ASCII as .. + ** .. deci-secs */ + memcpy(info_ptr->asyn_info.tmo, buff, 4); + } else if (strcmp(txt_ptr, "eot") == 0) { + txt_ptr = va_arg(ap, char *); + if (txt_ptr == NULL) { + EL737_errcode = EL737__BAD_PAR; + return False; + } + memcpy(info_ptr->asyn_info.eot, "\0\0\0\0", 4); + switch (txt_ptr[0]) { + case '3': + info_ptr->asyn_info.eot[3] = txt_ptr[3]; + case '2': + info_ptr->asyn_info.eot[2] = txt_ptr[2]; + case '1': + info_ptr->asyn_info.eot[1] = txt_ptr[1]; + case '0': + info_ptr->asyn_info.eot[0] = txt_ptr[0]; + break; + default: + EL737_errcode = EL737__BAD_PAR; + return False; + } + } else { + EL737_errcode = EL737__BAD_PAR; + return False; } - - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; + txt_ptr = va_arg(ap, char *); /* Get pntr to next parameter ident */ } + + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL737_Continue: Continue a measurement with an EL737 counter. */ - int EL737_Continue ( +int EL737_Continue( /* ============== -*/ void **handle, - int *status) { +*/ void **handle, + int *status) +{ - int my_status; - struct EL737info *info_ptr; - char *rply_ptr0, *rply_ptr1; - /*---------------------------------------------- + int my_status; + struct EL737info *info_ptr; + char *rply_ptr0, *rply_ptr1; + /*---------------------------------------------- */ - *status = 0; - info_ptr = (struct EL737info *) *handle; + *status = 0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_Continue")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_Continue")) + return False; + /*---------------------------------------------- ** Send CO and RS cmnds to EL737 */ - my_status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "CO\r", "RS\r", NULL); - if (!my_status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - } - rply_ptr1 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); - - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (rply_ptr1 == NULL) rply_ptr1 = "?"; - if ( ((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r') ) && - (sscanf (rply_ptr1, "%d", status) == 1)) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - *status = 0; - EL737_SetErrcode (info_ptr, rply_ptr0, "CO\" or \"RS"); + my_status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "CO\r", "RS\r", NULL); + if (!my_status) { + EL737_errcode = EL737__BAD_ASYNSRV; return False; } + rply_ptr1 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); + + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?"; + if (((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r')) && + (sscanf(rply_ptr1, "%d", status) == 1)) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; + } + *status = 0; + EL737_SetErrcode(info_ptr, rply_ptr0, "CO\" or \"RS"); + return False; +} + /* **--------------------------------------------------------------------------- ** EL737_EnableThresh: Enable/disable Threshold Monitoring. */ - int EL737_EnableThresh ( +int EL737_EnableThresh( /* ================== -*/ void **handle, - int indx) { +*/ void **handle, + int indx) +{ - int status; - char cmnd[20]; - struct EL737info *info_ptr; - char *rply_ptr0; - /*---------------------------------------------- + int status; + char cmnd[20]; + struct EL737info *info_ptr; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL737info *) *handle; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_EnableThresh")) return False; + if (!EL737_AddCallStack(info_ptr, "EL737_EnableThresh")) + return False; - if ((indx < 0) || (indx > 8)) { - EL737_errcode = EL737__BAD_PAR; return False; - } - /*---------------------------------------------- + if ((indx < 0) || (indx > 8)) { + EL737_errcode = EL737__BAD_PAR; + return False; + } + /*---------------------------------------------- ** Send "DR " cmnd to EL737 to select the ** "active" threshold rate counter. */ - sprintf (cmnd, "DR %d\r", indx); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!status) { - EL737_errcode = EL737__BAD_ASYNSRV; return False; - } - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if ( (*rply_ptr0 == '\0') || (*rply_ptr0 == '\r') ) { - EL737_call_depth--; - return True; - } - EL737_SetErrcode (info_ptr, rply_ptr0, cmnd); + sprintf(cmnd, "DR %d\r", indx); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!status) { + EL737_errcode = EL737__BAD_ASYNSRV; return False; } + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if ((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r')) { + EL737_call_depth--; + return True; + } + EL737_SetErrcode(info_ptr, rply_ptr0, cmnd); + return False; +} + /* ** ------------------------------------------------------------------------- ** EL737_ErrInfo: Return detailed status from last operation. */ - void EL737_ErrInfo ( +void EL737_ErrInfo( /* ============= -*/ char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno) { +*/ char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno) +{ - int i; - char buff[80]; - int asyn_errcode, asyn_errno, asyn_vaxerrno; - char* asyn_errtxt; + int i; + char buff[80]; + int asyn_errcode, asyn_errno, asyn_vaxerrno; + char *asyn_errtxt; - if (EL737_call_depth <= 0) { - strcpy (EL737_routine[0], "EL737_no_error_detected"); - *errcode = 0; - *my_errno = 0; - *vaxc_errno = 0; - }else { - if (EL737_call_depth > 1) { /* Concatenate the names */ - for (i = 1; i < EL737_call_depth; i++) { - strcat (EL737_routine[0], "/"); - StrJoin (EL737_routine[0], sizeof (EL737_routine), - EL737_routine[0], EL737_routine[i]); - } + if (EL737_call_depth <= 0) { + strcpy(EL737_routine[0], "EL737_no_error_detected"); + *errcode = 0; + *my_errno = 0; + *vaxc_errno = 0; + } else { + if (EL737_call_depth > 1) { /* Concatenate the names */ + for (i = 1; i < EL737_call_depth; i++) { + strcat(EL737_routine[0], "/"); + StrJoin(EL737_routine[0], sizeof(EL737_routine), + EL737_routine[0], EL737_routine[i]); } - *errcode = EL737_errcode; - *my_errno = EL737_errno; - *vaxc_errno = EL737_vaxc_errno; - switch (EL737_errcode) { - case EL737__BAD_ASYNSRV: strcpy (buff, "/EL737__BAD_ASYNSRV"); break; - case EL737__BAD_BSY: strcpy (buff, "/EL737__BAD_BSY"); break; - case EL737__BAD_CMD: strcpy (buff, "/EL737__BAD_CMD"); break; - case EL737__BAD_CNTR: strcpy (buff, "/EL737__BAD_CNTR"); break; - case EL737__BAD_DEV: strcpy (buff, "/EL737__BAD_DEV"); break; - case EL737__BAD_ILLG: strcpy (buff, "/EL737__BAD_ILLG"); break; - case EL737__BAD_LOC: strcpy (buff, "/EL737__BAD_LOC"); break; - case EL737__BAD_MALLOC: strcpy (buff, "/EL737__BAD_MALLOC"); break; - case EL737__BAD_OFL: strcpy (buff, "/EL737__BAD_OFL"); break; - case EL737__BAD_OVFL: strcpy (buff, "/EL737__BAD_OVFL"); break; - case EL737__BAD_PAR: strcpy (buff, "/EL737__BAD_PAR"); break; - case EL737__BAD_SOCKET: strcpy (buff, "/EL737__BAD_SOCKET"); break; - case EL737__BAD_TMO: strcpy (buff, "/EL737__BAD_TMO"); break; - case EL737__CNTR_OVFL: strcpy (buff, "/EL737__CNTR_OVFL"); break; - case EL737__FORCED_CLOSED: strcpy (buff, "/EL737__FORCED_CLOSED"); break; - case EL737__NOT_OPEN: strcpy (buff, "/EL737__NOT_OPEN"); break; - case EL737__NO_SOCKET: strcpy (buff, "/EL737__NO_SOCKET"); break; - case EL737__NO_VALUE: strcpy (buff, "/EL737__NO_VALUE"); break; - default: sprintf (buff, "/EL737__unknown_err_code: %d", EL737_errcode); - } - StrJoin (EL737_routine[0], sizeof(EL737_routine), EL737_routine[0], buff); } - AsynSrv_ErrInfo (&asyn_errtxt, &asyn_errcode, &asyn_errno, &asyn_vaxerrno); - if (asyn_errcode != 0) { - strcat (EL737_routine[0], "/"); - StrJoin (EL737_routine[0], sizeof(EL737_routine), - EL737_routine[0], asyn_errtxt); + *errcode = EL737_errcode; + *my_errno = EL737_errno; + *vaxc_errno = EL737_vaxc_errno; + switch (EL737_errcode) { + case EL737__BAD_ASYNSRV: + strcpy(buff, "/EL737__BAD_ASYNSRV"); + break; + case EL737__BAD_BSY: + strcpy(buff, "/EL737__BAD_BSY"); + break; + case EL737__BAD_CMD: + strcpy(buff, "/EL737__BAD_CMD"); + break; + case EL737__BAD_CNTR: + strcpy(buff, "/EL737__BAD_CNTR"); + break; + case EL737__BAD_DEV: + strcpy(buff, "/EL737__BAD_DEV"); + break; + case EL737__BAD_ILLG: + strcpy(buff, "/EL737__BAD_ILLG"); + break; + case EL737__BAD_LOC: + strcpy(buff, "/EL737__BAD_LOC"); + break; + case EL737__BAD_MALLOC: + strcpy(buff, "/EL737__BAD_MALLOC"); + break; + case EL737__BAD_OFL: + strcpy(buff, "/EL737__BAD_OFL"); + break; + case EL737__BAD_OVFL: + strcpy(buff, "/EL737__BAD_OVFL"); + break; + case EL737__BAD_PAR: + strcpy(buff, "/EL737__BAD_PAR"); + break; + case EL737__BAD_SOCKET: + strcpy(buff, "/EL737__BAD_SOCKET"); + break; + case EL737__BAD_TMO: + strcpy(buff, "/EL737__BAD_TMO"); + break; + case EL737__CNTR_OVFL: + strcpy(buff, "/EL737__CNTR_OVFL"); + break; + case EL737__FORCED_CLOSED: + strcpy(buff, "/EL737__FORCED_CLOSED"); + break; + case EL737__NOT_OPEN: + strcpy(buff, "/EL737__NOT_OPEN"); + break; + case EL737__NO_SOCKET: + strcpy(buff, "/EL737__NO_SOCKET"); + break; + case EL737__NO_VALUE: + strcpy(buff, "/EL737__NO_VALUE"); + break; + default: + sprintf(buff, "/EL737__unknown_err_code: %d", EL737_errcode); } - *entry_txt = EL737_routine[0]; - EL737_call_depth = 0; - EL737_errcode = 0; + StrJoin(EL737_routine[0], sizeof(EL737_routine), EL737_routine[0], + buff); } + AsynSrv_ErrInfo(&asyn_errtxt, &asyn_errcode, &asyn_errno, + &asyn_vaxerrno); + if (asyn_errcode != 0) { + strcat(EL737_routine[0], "/"); + StrJoin(EL737_routine[0], sizeof(EL737_routine), + EL737_routine[0], asyn_errtxt); + } + *entry_txt = EL737_routine[0]; + EL737_call_depth = 0; + EL737_errcode = 0; +} + /* **--------------------------------------------------------------------------- ** EL737_GetMonIntegTime: Get DI register value for a counter. */ - int EL737_GetMonIntegTime ( +int EL737_GetMonIntegTime( /* ===================== -*/ void **handle, - int indx, - float *mon_integ_time) { +*/ void **handle, + int indx, float *mon_integ_time) +{ - int status; - char cmnd[20]; - struct EL737info *info_ptr; - char *rply_ptr0; - /*---------------------------------------------- + int status; + char cmnd[20]; + struct EL737info *info_ptr; + char *rply_ptr0; + /*---------------------------------------------- */ - *mon_integ_time = 0.1; - info_ptr = (struct EL737info *) *handle; + *mon_integ_time = 0.1; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_GetMonIntegTime")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_GetMonIntegTime")) + return False; + /*---------------------------------------------- ** Send "DI " cmnd to EL737 */ - sprintf (cmnd, "DI %d\r", indx); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (sscanf (rply_ptr0, "%f", mon_integ_time) == 1) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - *mon_integ_time = 0.1; - EL737_SetErrcode (info_ptr, rply_ptr0, cmnd); - return False; + sprintf(cmnd, "DI %d\r", indx); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (sscanf(rply_ptr0, "%f", mon_integ_time) == 1) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; } + *mon_integ_time = 0.1; + EL737_SetErrcode(info_ptr, rply_ptr0, cmnd); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL737_GetRateIntegTime: Get DT register value. */ - int EL737_GetRateIntegTime ( +int EL737_GetRateIntegTime( /* ====================== -*/ void **handle, - float *rate_integ_time) { +*/ void **handle, + float *rate_integ_time) +{ - int status; - struct EL737info *info_ptr; - char *rply_ptr0; - /*---------------------------------------------- + int status; + struct EL737info *info_ptr; + char *rply_ptr0; + /*---------------------------------------------- */ - *rate_integ_time = 0.1; - info_ptr = (struct EL737info *) *handle; + *rate_integ_time = 0.1; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_GetRateIntegTime")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_GetRateIntegTime")) + return False; + /*---------------------------------------------- ** Send DT cmnd to EL737 */ - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "DT\r", NULL); - if (!status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (sscanf (rply_ptr0, "%f", rate_integ_time) == 1) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - EL737_SetErrcode (info_ptr, rply_ptr0, "DT"); - *rate_integ_time = 0.1; - return False; + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "DT\r", NULL); + if (!status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (sscanf(rply_ptr0, "%f", rate_integ_time) == 1) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; } + EL737_SetErrcode(info_ptr, rply_ptr0, "DT"); + *rate_integ_time = 0.1; + return False; } +} + /* **--------------------------------------------------------------------------- ** EL737_GetStatus: Get RA/RS register values. */ - int EL737_GetStatus ( +int EL737_GetStatus( /* =============== -*/ void **handle, - int *c1, - int *c2, - int *c3, - int *c4, - float *timer, - int *rs) { +*/ void **handle, + int *c1, + int *c2, int *c3, int *c4, float *timer, int *rs) +{ - int i, status, nvals; - struct EL737info *info_ptr; - char *rply_ptr, *p_cmnd; - /*---------------------------------------------- + int i, status, nvals; + struct EL737info *info_ptr; + char *rply_ptr, *p_cmnd; + /*---------------------------------------------- */ - *c1 = *c2 = *c3 = *c4 = *rs = 0; *timer = 0.0; - info_ptr = (struct EL737info *) *handle; + *c1 = *c2 = *c3 = *c4 = *rs = 0; + *timer = 0.0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_GetStatus")) return False; - info_ptr->c5 = info_ptr->c6 = info_ptr->c7 = info_ptr->c8 = 0; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_GetStatus")) + return False; + info_ptr->c5 = info_ptr->c6 = info_ptr->c7 = info_ptr->c8 = 0; + /*---------------------------------------------- ** Send RA and RS cmnds to EL737. Since this routine gets ** used such a lot, try up to 3 times if a syntax error in ** the reply is detected. */ - for (i = 0; i < 3; i++) { - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "RA\r", "RS\r", NULL); - if (!status) {EL737_errcode = EL737__BAD_ASYNSRV; return False;} - p_cmnd = "RA"; - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr != NULL) { - nvals = sscanf (rply_ptr, "%f %d %d %d %d %d %d %d %d", - timer, c1, c2, c3, c4, - &info_ptr->c5, &info_ptr->c6, - &info_ptr->c7, &info_ptr->c8); - if (nvals != 9) nvals = sscanf (rply_ptr, "%d %d %d %d %f", - c1, c2, c3, c4, timer); - if (nvals == 5) { - info_ptr->c5 = info_ptr->c6 = info_ptr->c7 = info_ptr->c8 = 0; - nvals = 9; - } - if (nvals == 9) { - p_cmnd = "RS"; - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr); - if (rply_ptr != NULL) { - if (sscanf (rply_ptr, "%d", rs) == 1) { - EL737_call_depth--; - return True; - } - } - } + for (i = 0; i < 3; i++) { + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "RA\r", "RS\r", NULL); + if (!status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } + p_cmnd = "RA"; + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr != NULL) { + nvals = sscanf(rply_ptr, "%f %d %d %d %d %d %d %d %d", + timer, c1, c2, c3, c4, + &info_ptr->c5, &info_ptr->c6, + &info_ptr->c7, &info_ptr->c8); + if (nvals != 9) + nvals = sscanf(rply_ptr, "%d %d %d %d %f", c1, c2, c3, c4, timer); + if (nvals == 5) { + info_ptr->c5 = info_ptr->c6 = info_ptr->c7 = info_ptr->c8 = 0; + nvals = 9; + } + if (nvals == 9) { + p_cmnd = "RS"; + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr); + if (rply_ptr != NULL) { + if (sscanf(rply_ptr, "%d", rs) == 1) { + EL737_call_depth--; + return True; + } + } } } - if (rply_ptr == NULL) rply_ptr = "?"; - EL737_SetErrcode (info_ptr, rply_ptr, p_cmnd); - *c1 = *c2 = *c3 = *c4 = *rs = 0; *timer = 0.0; - return False; } + if (rply_ptr == NULL) + rply_ptr = "?"; + EL737_SetErrcode(info_ptr, rply_ptr, p_cmnd); + *c1 = *c2 = *c3 = *c4 = *rs = 0; + *timer = 0.0; + return False; +} + /* **--------------------------------------------------------------------------- ** EL737_GetStatusExtra: Get values of extra counters. */ - int EL737_GetStatusExtra ( +int EL737_GetStatusExtra( /* ==================== -*/ void **handle, - int *c5, - int *c6, - int *c7, - int *c8) { +*/ void **handle, + int *c5, int *c6, int *c7, int *c8) +{ - struct EL737info *info_ptr; - /*---------------------------------------------- + struct EL737info *info_ptr; + /*---------------------------------------------- */ - *c5 = *c6 = *c7 = *c8 = 0; - info_ptr = (struct EL737info *) *handle; + *c5 = *c6 = *c7 = *c8 = 0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_GetStatusExtra")) return False; + if (!EL737_AddCallStack(info_ptr, "EL737_GetStatusExtra")) + return False; - *c5 = info_ptr->c5; - *c6 = info_ptr->c6; - *c7 = info_ptr->c7; - *c8 = info_ptr->c8; + *c5 = info_ptr->c5; + *c6 = info_ptr->c6; + *c7 = info_ptr->c7; + *c8 = info_ptr->c8; + + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; +} - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } /* **--------------------------------------------------------------------------- ** EL737_GetThresh: Get threshold monitoring status. */ - int EL737_GetThresh ( +int EL737_GetThresh( /* =============== -*/ void **handle, - int *indx, - float *val) { +*/ void **handle, + int *indx, float *val) +{ - int status, my_indx; - float my_val; - char cmnd[20]; - struct EL737info *info_ptr; - char *rply_ptr0; - /*---------------------------------------------- + int status, my_indx; + float my_val; + char cmnd[20]; + struct EL737info *info_ptr; + char *rply_ptr0; + /*---------------------------------------------- */ - *indx = 0; - *val = 0.0; - info_ptr = (struct EL737info *) *handle; + *indx = 0; + *val = 0.0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_GetThresh")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_GetThresh")) + return False; + /*---------------------------------------------- ** Send DR cmnd to EL737 to get the number of the ** "active" threshold rate counter. */ - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "DR\r", NULL); - if (!status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - } - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if ((sscanf (rply_ptr0, "%d", &my_indx) == 1) && - (my_indx >= 0) && - (my_indx <= 8)) { - *indx = my_indx; - if (my_indx != 0) { - /*---------------------------------------------- - ** Now send DL cmnd to EL737 to get the threshold value. - */ - sprintf (cmnd, "DL %d\r", my_indx); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - } - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (sscanf (rply_ptr0, "%f", &my_val) == 1) { - *val = my_val; - EL737_call_depth--; - return True; - } - }else { - *val = 0.0; - EL737_call_depth--; - return True; - } - } - EL737_SetErrcode (info_ptr, rply_ptr0, cmnd); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "DR\r", NULL); + if (!status) { + EL737_errcode = EL737__BAD_ASYNSRV; return False; } + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if ((sscanf(rply_ptr0, "%d", &my_indx) == 1) && + (my_indx >= 0) && (my_indx <= 8)) { + *indx = my_indx; + if (my_indx != 0) { + /*---------------------------------------------- + ** Now send DL cmnd to EL737 to get the threshold value. + */ + sprintf(cmnd, "DL %d\r", my_indx); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (sscanf(rply_ptr0, "%f", &my_val) == 1) { + *val = my_val; + EL737_call_depth--; + return True; + } + } else { + *val = 0.0; + EL737_call_depth--; + return True; + } + } + EL737_SetErrcode(info_ptr, rply_ptr0, cmnd); + return False; +} + /* **--------------------------------------------------------------------------- ** EL737_Open: Open a connection to an EL737 counter. */ - int EL737_Open ( +int EL737_Open( /* ========== -*/ void **handle, - char *host, - int port, - int chan) { +*/ void **handle, + char *host, int port, int chan) +{ - int status, c1, c2, c3, c4, nvals; - float timer; - struct EL737info *my_handle; - char tmo_save[4]; - char *rply_ptr; - char *rply_ptr0; - char *rply_ptr1; - char *rply_ptr2; + int status, c1, c2, c3, c4, nvals; + float timer; + struct EL737info *my_handle; + char tmo_save[4]; + char *rply_ptr; + char *rply_ptr0; + char *rply_ptr1; + char *rply_ptr2; /*-------------------------------------------------------- ** Initialise the error info stack and pre-set the ** routine name (in case of error). */ - EL737_errcode = EL737_errno = EL737_vaxc_errno = 0; - strcpy (EL737_routine[0], "EL737_Open"); - EL737_call_depth = 1; + EL737_errcode = EL737_errno = EL737_vaxc_errno = 0; + strcpy(EL737_routine[0], "EL737_Open"); + EL737_call_depth = 1; /*-------------------------------------------------------- ** Assume trouble */ - *handle = NULL; + *handle = NULL; /*-------------------------------------------------------- ** Reserve space for the data we need to store. */ - my_handle = (struct EL737info *) malloc (sizeof (*my_handle)); - if (my_handle == NULL) { - EL737_errcode = EL737__BAD_MALLOC; /* malloc failed!! */ - return False; - } - memset(my_handle,0,sizeof(*my_handle)); + my_handle = (struct EL737info *) malloc(sizeof(*my_handle)); + if (my_handle == NULL) { + EL737_errcode = EL737__BAD_MALLOC; /* malloc failed!! */ + return False; + } + memset(my_handle, 0, sizeof(*my_handle)); /*-------------------------------------------------------- ** Set up the connection */ - StrJoin (my_handle->asyn_info.host, sizeof (my_handle->asyn_info.host), - host, ""); - my_handle->asyn_info.port = port; - my_handle->asyn_info.chan = chan; - status = AsynSrv_Open (&my_handle->asyn_info); - if (!status) { - EL737_errcode = EL737__BAD_SOCKET; - GetErrno (&EL737_errno, &EL737_vaxc_errno); /* Save errno info */ - fprintf (stderr, "\nEL737_Open/AsynSrv_Open: " - "Failed to make connection.\n"); - free (my_handle); - return False; - } - - memcpy (tmo_save, my_handle->asyn_info.tmo, 4); - EL737_Config ((void *) &my_handle, - "msecTmo", 500, /* Set a short time-out initially since - ** there should be no reason for the RMT, - ** ECHO or RA commands to take very long - */ - "eot", "1\r", - NULL); - /* - ** Now ensure the EL737 is on-line. The first "RMT 1" command can - ** fail due to pending characters in the EL737 input buffer causing - ** the "RMT 1" to be corrupted. The response of the EL737 to this - ** command is ignored for this reason (but the AsynSrv_SendCmnds - ** status must be OK otherwise it indicates a network problem). - */ - status = AsynSrv_SendCmnds (&my_handle->asyn_info, - &my_handle->to_host, &my_handle->from_host, - "RMT 1\r", NULL); - if (status) { - status = AsynSrv_SendCmnds (&my_handle->asyn_info, - &my_handle->to_host, &my_handle->from_host, - "RMT 1\r", "ECHO 2\r", "RA\r", NULL); - } - if (!status) { - /* Some error occurred in AsynSrv_SendCmnds */ - EL737_errcode = EL737__BAD_ASYNSRV; - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - }else { - /* Check the responses carefully. - */ - rply_ptr1 = rply_ptr2 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &my_handle->asyn_info, &my_handle->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &my_handle->asyn_info, &my_handle->from_host, rply_ptr0); - if (rply_ptr1 != NULL) rply_ptr2 = AsynSrv_GetReply ( - &my_handle->asyn_info, &my_handle->from_host, rply_ptr1); - - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (rply_ptr1 == NULL) rply_ptr1 = "?"; - if (rply_ptr2 == NULL) rply_ptr2 = "?"; - - if (*rply_ptr1 == '?') rply_ptr0 = rply_ptr1; - if (*rply_ptr2 == '?') rply_ptr0 = rply_ptr2; - if (*rply_ptr0 != '?') { - nvals = sscanf (rply_ptr2, "%f %d %d %d %d %d %d %d %d", - &timer, &c1, &c2, &c3, &c4, - &my_handle->c5, &my_handle->c6, - &my_handle->c7, &my_handle->c8); - if (nvals != 9) nvals = sscanf (rply_ptr2, "%d %d %d %d %f", - &c1, &c2, &c3, &c4, &timer); - if (nvals == 5) { - my_handle->c5 = my_handle->c6 = my_handle->c7 = my_handle->c8 = 0; - nvals = 9; - } - if (nvals != 9) { - EL737_errcode = EL737__BAD_DEV; /* Device is not EL737 */ - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - } - memcpy (my_handle->asyn_info.tmo, tmo_save, 4); /* Restore time-out */ - /* - ** The connection is complete. Pass the data structure - ** back to the caller as a handle. - */ - *handle = my_handle; - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - }else { - EL737_SetErrcode (my_handle, rply_ptr0, "RMT\", \"ECHO\" or \"RA"); - } - } - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); + StrJoin(my_handle->asyn_info.host, sizeof(my_handle->asyn_info.host), + host, ""); + my_handle->asyn_info.port = port; + my_handle->asyn_info.chan = chan; + status = AsynSrv_Open(&my_handle->asyn_info); + if (!status) { + EL737_errcode = EL737__BAD_SOCKET; + GetErrno(&EL737_errno, &EL737_vaxc_errno); /* Save errno info */ + fprintf(stderr, "\nEL737_Open/AsynSrv_Open: " + "Failed to make connection.\n"); + free(my_handle); return False; } + + memcpy(tmo_save, my_handle->asyn_info.tmo, 4); + EL737_Config((void *) &my_handle, "msecTmo", 500, /* Set a short time-out initially since + ** there should be no reason for the RMT, + ** ECHO or RA commands to take very long + */ + "eot", "1\r", NULL); + /* + ** Now ensure the EL737 is on-line. The first "RMT 1" command can + ** fail due to pending characters in the EL737 input buffer causing + ** the "RMT 1" to be corrupted. The response of the EL737 to this + ** command is ignored for this reason (but the AsynSrv_SendCmnds + ** status must be OK otherwise it indicates a network problem). + */ + status = AsynSrv_SendCmnds(&my_handle->asyn_info, + &my_handle->to_host, &my_handle->from_host, + "RMT 1\r", NULL); + if (status) { + status = AsynSrv_SendCmnds(&my_handle->asyn_info, + &my_handle->to_host, &my_handle->from_host, + "RMT 1\r", "ECHO 2\r", "RA\r", NULL); + } + if (!status) { + /* Some error occurred in AsynSrv_SendCmnds */ + EL737_errcode = EL737__BAD_ASYNSRV; + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } else { + /* Check the responses carefully. + */ + rply_ptr1 = rply_ptr2 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&my_handle->asyn_info, &my_handle->from_host, + NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&my_handle->asyn_info, &my_handle->from_host, + rply_ptr0); + if (rply_ptr1 != NULL) + rply_ptr2 = + AsynSrv_GetReply(&my_handle->asyn_info, &my_handle->from_host, + rply_ptr1); + + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?"; + if (rply_ptr2 == NULL) + rply_ptr2 = "?"; + + if (*rply_ptr1 == '?') + rply_ptr0 = rply_ptr1; + if (*rply_ptr2 == '?') + rply_ptr0 = rply_ptr2; + if (*rply_ptr0 != '?') { + nvals = sscanf(rply_ptr2, "%f %d %d %d %d %d %d %d %d", + &timer, &c1, &c2, &c3, &c4, + &my_handle->c5, &my_handle->c6, + &my_handle->c7, &my_handle->c8); + if (nvals != 9) + nvals = sscanf(rply_ptr2, "%d %d %d %d %f", + &c1, &c2, &c3, &c4, &timer); + if (nvals == 5) { + my_handle->c5 = my_handle->c6 = my_handle->c7 = my_handle->c8 = 0; + nvals = 9; + } + if (nvals != 9) { + EL737_errcode = EL737__BAD_DEV; /* Device is not EL737 */ + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } + memcpy(my_handle->asyn_info.tmo, tmo_save, 4); /* Restore time-out */ + /* + ** The connection is complete. Pass the data structure + ** back to the caller as a handle. + */ + *handle = my_handle; + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; + } else { + EL737_SetErrcode(my_handle, rply_ptr0, "RMT\", \"ECHO\" or \"RA"); + } + } + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; +} + /* **--------------------------------------------------------------------------- ** EL737_Pause: Pause a measurement with an EL737 counter. */ - int EL737_Pause ( +int EL737_Pause( /* =========== -*/ void **handle, - int *status) { +*/ void **handle, + int *status) +{ - int my_status; - struct EL737info *info_ptr; - char *rply_ptr0, *rply_ptr1; - /*---------------------------------------------- + int my_status; + struct EL737info *info_ptr; + char *rply_ptr0, *rply_ptr1; + /*---------------------------------------------- */ - *status = 0; - info_ptr = (struct EL737info *) *handle; + *status = 0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_Pause")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_Pause")) + return False; + /*---------------------------------------------- ** Send PS and RS cmnds to EL737 */ - my_status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "PS\r", "RS\r", NULL); - if (!my_status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - }else { - rply_ptr1 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); + my_status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "PS\r", "RS\r", NULL); + if (!my_status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } else { + rply_ptr1 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (rply_ptr1 == NULL) rply_ptr1 = "?"; - if ( ((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r') ) && - (sscanf (rply_ptr1, "%d", status) == 1)) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - EL737_SetErrcode (info_ptr, rply_ptr0, "PS\" or \"RS"); - *status = 0; - return False; + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?"; + if (((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r')) && + (sscanf(rply_ptr1, "%d", status) == 1)) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; } + EL737_SetErrcode(info_ptr, rply_ptr0, "PS\" or \"RS"); + *status = 0; + return False; } +} + /* **--------------------------------------------------------------------------- ** EL737_SendCmnd - Send a command to RS232C server. */ - int EL737_SendCmnd ( +int EL737_SendCmnd( /* ============== -*/ void **handle, - char *cmnd, - char *rply, - int rply_size) { +*/ void **handle, + char *cmnd, char *rply, int rply_size) +{ - struct EL737info *info_ptr; - int my_status; - char *rply_ptr; - /*---------------------------------------------- + struct EL737info *info_ptr; + int my_status; + char *rply_ptr; + /*---------------------------------------------- */ - info_ptr = (struct EL737info *) *handle; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_SendCmnd")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_SendCmnd")) + return False; + /*---------------------------------------------- ** Send command to EL737. */ - my_status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!my_status) { - EL737_errcode = EL737__BAD_ASYNSRV; return False; - }else { - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr == NULL) rply_ptr = "?"; - StrJoin (rply, rply_size, rply_ptr, ""); - } - - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; + my_status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!my_status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } else { + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr == NULL) + rply_ptr = "?"; + StrJoin(rply, rply_size, rply_ptr, ""); } + + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL737_SetErrcode - Set up EL737_errcode */ - int EL737_SetErrcode ( +int EL737_SetErrcode( /* ================ -*/ struct EL737info *info_ptr, - char *response, - char *cmnd) { +*/ struct EL737info *info_ptr, + char *response, char *cmnd) +{ - int status, s_len; - char *rply; - char tmo_save[4]; - char eot_save[4]; + int status, s_len; + char *rply; + char tmo_save[4]; + char eot_save[4]; - EL737_errcode = EL737__BAD_ILLG; - if (strcmp (response, "?OF" ) == 0) EL737_errcode = EL737__BAD_LOC; - if (strcmp (response, "?OFL") == 0) EL737_errcode = EL737__BAD_OFL; - if (strcmp (response, "?OV" ) == 0) EL737_errcode = EL737__BAD_OVFL; - if (strcmp (response, "?1" ) == 0) EL737_errcode = EL737__BAD_CMD; - if (strcmp (response, "?2" ) == 0) EL737_errcode = EL737__BAD_BSY; - if (strcmp (response, "?3" ) == 0) EL737_errcode = EL737__BAD_PAR; - if (strcmp (response, "?4" ) == 0) EL737_errcode = EL737__BAD_CNTR; - if (strcmp (response, "?5" ) == 0) EL737_errcode = EL737__NO_VALUE; - if (strcmp (response, "?6" ) == 0) EL737_errcode = EL737__CNTR_OVFL; - if (strncmp (response, "?TMO", 4) == 0) EL737_errcode = EL737__BAD_TMO; + EL737_errcode = EL737__BAD_ILLG; + if (strcmp(response, "?OF") == 0) + EL737_errcode = EL737__BAD_LOC; + if (strcmp(response, "?OFL") == 0) + EL737_errcode = EL737__BAD_OFL; + if (strcmp(response, "?OV") == 0) + EL737_errcode = EL737__BAD_OVFL; + if (strcmp(response, "?1") == 0) + EL737_errcode = EL737__BAD_CMD; + if (strcmp(response, "?2") == 0) + EL737_errcode = EL737__BAD_BSY; + if (strcmp(response, "?3") == 0) + EL737_errcode = EL737__BAD_PAR; + if (strcmp(response, "?4") == 0) + EL737_errcode = EL737__BAD_CNTR; + if (strcmp(response, "?5") == 0) + EL737_errcode = EL737__NO_VALUE; + if (strcmp(response, "?6") == 0) + EL737_errcode = EL737__CNTR_OVFL; + if (strncmp(response, "?TMO", 4) == 0) + EL737_errcode = EL737__BAD_TMO; - if ((EL737_errcode == EL737__BAD_ILLG) && (cmnd != NULL)) { - s_len = strlen (cmnd); - if (cmnd[s_len-1] == '\r') s_len--; - fprintf (stderr, " Unrecognised response to \"%.*s\" command: \"%s\"\n", - s_len, cmnd, response); - } - - return EL737_errcode; + if ((EL737_errcode == EL737__BAD_ILLG) && (cmnd != NULL)) { + s_len = strlen(cmnd); + if (cmnd[s_len - 1] == '\r') + s_len--; + fprintf(stderr, " Unrecognised response to \"%.*s\" command: \"%s\"\n", + s_len, cmnd, response); } + + return EL737_errcode; +} + /* **--------------------------------------------------------------------------- ** EL737_SetThresh: Set threshold monitoring level. */ - int EL737_SetThresh ( +int EL737_SetThresh( /* =============== -*/ void **handle, - int indx, - float val) { +*/ void **handle, + int indx, float val) +{ - int status; - char cmnd[32]; - struct EL737info *info_ptr; - char *rply_ptr0; - /*---------------------------------------------- + int status; + char cmnd[32]; + struct EL737info *info_ptr; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL737info *) *handle; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_SetThresh")) return False; + if (!EL737_AddCallStack(info_ptr, "EL737_SetThresh")) + return False; - if ((indx < 0) || (indx > 8)) { - EL737_errcode = EL737__BAD_PAR; return False; - } - /*---------------------------------------------- + if ((indx < 0) || (indx > 8)) { + EL737_errcode = EL737__BAD_PAR; + return False; + } + /*---------------------------------------------- ** If is zero, simply call EL737_EnableThresh to ** disable threshold monitoring by the counter. */ - if (indx == 0) { - return EL737_EnableThresh (handle, 0); - } - /*---------------------------------------------- + if (indx == 0) { + return EL737_EnableThresh(handle, 0); + } + /*---------------------------------------------- ** Send "DR ||" cmnd to EL737 to set the ** threshold for counter . */ - sprintf (cmnd, "DL %d %.3f\r", indx, fabs (val)); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!status) { - EL737_errcode = EL737__BAD_ASYNSRV; return False; - } - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if ( (*rply_ptr0 == '\0') || (*rply_ptr0 == '\r') ) { - if (val >= 0) return EL737_EnableThresh (handle, indx); - EL737_call_depth--; - return True; - } - EL737_SetErrcode (info_ptr, rply_ptr0, cmnd); + sprintf(cmnd, "DL %d %.3f\r", indx, fabs(val)); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!status) { + EL737_errcode = EL737__BAD_ASYNSRV; return False; } + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if ((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r')) { + if (val >= 0) + return EL737_EnableThresh(handle, indx); + EL737_call_depth--; + return True; + } + EL737_SetErrcode(info_ptr, rply_ptr0, cmnd); + return False; +} + /* **--------------------------------------------------------------------------- ** EL737_StartCnt: Start a preset cnt measurement with an EL737. */ - int EL737_StartCnt ( +int EL737_StartCnt( /* ============== -*/ void **handle, - int count, - int *status) { +*/ void **handle, + int count, int *status) +{ - int my_status; - char cmnd[20]; - struct EL737info *info_ptr; - char *rply_ptr0, *rply_ptr1; - /*---------------------------------------------- + int my_status; + char cmnd[20]; + struct EL737info *info_ptr; + char *rply_ptr0, *rply_ptr1; + /*---------------------------------------------- */ - *status = 0; - info_ptr = (struct EL737info *) *handle; + *status = 0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_StartCnt")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_StartCnt")) + return False; + /*---------------------------------------------- ** Send MP and RS cmnds to EL737 */ - sprintf (cmnd, "MP %d\r", count); /* Encode an appropriate command */ - my_status = AsynSrv_SendCmnds (&info_ptr->asyn_info, /* Send it */ - &info_ptr->to_host, &info_ptr->from_host, - cmnd, "RS\r", NULL); - if (!my_status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - }else { - rply_ptr1 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); + sprintf(cmnd, "MP %d\r", count); /* Encode an appropriate command */ + my_status = AsynSrv_SendCmnds(&info_ptr->asyn_info, /* Send it */ + &info_ptr->to_host, &info_ptr->from_host, + cmnd, "RS\r", NULL); + if (!my_status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } else { + rply_ptr1 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (rply_ptr1 == NULL) rply_ptr1 = "?"; - if ( ((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r') ) && - (sscanf (rply_ptr1, "%d", status) == 1)) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - EL737_SetErrcode (info_ptr, rply_ptr0, cmnd); - *status = 0; - return False; + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?"; + if (((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r')) && + (sscanf(rply_ptr1, "%d", status) == 1)) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; } + EL737_SetErrcode(info_ptr, rply_ptr0, cmnd); + *status = 0; + return False; } +} + /* **--------------------------------------------------------------------------- ** EL737_StartTime: Start a preset time measurement with an EL737. */ - int EL737_StartTime ( +int EL737_StartTime( /* =============== -*/ void **handle, - float timer, - int *status) { +*/ void **handle, + float timer, int *status) +{ - int my_status; - char cmnd[20]; - struct EL737info *info_ptr; - char *rply_ptr0, *rply_ptr1; - /*---------------------------------------------- + int my_status; + char cmnd[20]; + struct EL737info *info_ptr; + char *rply_ptr0, *rply_ptr1; + /*---------------------------------------------- */ - *status = 0; - info_ptr = (struct EL737info *) *handle; + *status = 0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_StartTime")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_StartTime")) + return False; + /*---------------------------------------------- ** Send TP and RS cmnds to EL737 */ - sprintf (cmnd, "TP %.2f\r", timer); /* Encode an appropriate command */ - my_status = AsynSrv_SendCmnds (&info_ptr->asyn_info, /* Send it */ - &info_ptr->to_host, &info_ptr->from_host, - cmnd, "RS\r", NULL); - if (!my_status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - }else { - rply_ptr1 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); + sprintf(cmnd, "TP %.2f\r", timer); /* Encode an appropriate command */ + my_status = AsynSrv_SendCmnds(&info_ptr->asyn_info, /* Send it */ + &info_ptr->to_host, &info_ptr->from_host, + cmnd, "RS\r", NULL); + if (!my_status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } else { + rply_ptr1 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (rply_ptr1 == NULL) rply_ptr1 = "?"; - if ( ((*rply_ptr0 == '\0') || (*rply_ptr0 == 'r') ) && - (sscanf (rply_ptr1, "%d", status) == 1)) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - EL737_SetErrcode (info_ptr, rply_ptr0, cmnd); - *status = 0; - return False; + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?"; + if (((*rply_ptr0 == '\0') || (*rply_ptr0 == 'r')) && + (sscanf(rply_ptr1, "%d", status) == 1)) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; } + EL737_SetErrcode(info_ptr, rply_ptr0, cmnd); + *status = 0; + return False; } +} + /* **--------------------------------------------------------------------------- ** EL737_Stop: stop a measurement with an EL737 counter. */ - int EL737_Stop ( +int EL737_Stop( /* ========== -*/ void **handle, - int *c1, - int *c2, - int *c3, - int *c4, - float *timer, - int *rs) { +*/ void **handle, + int *c1, int *c2, int *c3, int *c4, float *timer, int *rs) +{ - int my_status, nvals; - struct EL737info *info_ptr; - char *rply_ptr0, *rply_ptr1, *rply_ptr2; - /*---------------------------------------------- + int my_status, nvals; + struct EL737info *info_ptr; + char *rply_ptr0, *rply_ptr1, *rply_ptr2; + /*---------------------------------------------- */ - *c1 = *c2 = *c3 = *c4 = *rs = 0; *timer = 0.0; - info_ptr = (struct EL737info *) *handle; + *c1 = *c2 = *c3 = *c4 = *rs = 0; + *timer = 0.0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_Stop")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_Stop")) + return False; + /*---------------------------------------------- ** Send S, RS and RA cmnds to EL737 */ - my_status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "S\r", "RS\r", "RA\r", NULL); - if (!my_status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - }else { - rply_ptr1 = rply_ptr2 = NULL; - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 != NULL) rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); - if (rply_ptr1 != NULL) rply_ptr2 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr1); + my_status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "S\r", "RS\r", "RA\r", NULL); + if (!my_status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } else { + rply_ptr1 = rply_ptr2 = NULL; + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr0 != NULL) + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); + if (rply_ptr1 != NULL) + rply_ptr2 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr1); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; - if (rply_ptr1 == NULL) rply_ptr1 = "?"; - if (rply_ptr2 == NULL) rply_ptr2 = "?"; + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; + if (rply_ptr1 == NULL) + rply_ptr1 = "?"; + if (rply_ptr2 == NULL) + rply_ptr2 = "?"; - nvals = sscanf (rply_ptr2, "%f %d %d %d %d %d %d %d %d", - timer, c1, c2, c3, c4, - &info_ptr->c5, &info_ptr->c6, - &info_ptr->c7, &info_ptr->c8); - if (nvals != 9) nvals = sscanf (rply_ptr2, "%d %d %d %d %f", - c1, c2, c3, c4, timer); - if (nvals == 5) { - info_ptr->c5 = info_ptr->c6 = info_ptr->c7 = info_ptr->c8 = 0; - nvals = 9; - } - if ( ((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r') )&& - (sscanf (rply_ptr1, "%d", rs) == 1) && - (nvals == 9)) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - if (*rply_ptr0 != '?') { - if (*rply_ptr1 == '?') rply_ptr0 = rply_ptr1; - if (*rply_ptr0 == '?') rply_ptr0 = rply_ptr2; - } - EL737_SetErrcode (info_ptr, rply_ptr0, "S\", \"RS\" or \"RA"); - *c1 = *c2 = *c3 = *c4 = *rs = 0; *timer = 0.0; - return False; + nvals = sscanf(rply_ptr2, "%f %d %d %d %d %d %d %d %d", + timer, c1, c2, c3, c4, + &info_ptr->c5, &info_ptr->c6, + &info_ptr->c7, &info_ptr->c8); + if (nvals != 9) + nvals = sscanf(rply_ptr2, "%d %d %d %d %f", c1, c2, c3, c4, timer); + if (nvals == 5) { + info_ptr->c5 = info_ptr->c6 = info_ptr->c7 = info_ptr->c8 = 0; + nvals = 9; } + if (((*rply_ptr0 == '\0') || (*rply_ptr0 == '\r')) && + (sscanf(rply_ptr1, "%d", rs) == 1) && (nvals == 9)) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; + } + if (*rply_ptr0 != '?') { + if (*rply_ptr1 == '?') + rply_ptr0 = rply_ptr1; + if (*rply_ptr0 == '?') + rply_ptr0 = rply_ptr2; + } + EL737_SetErrcode(info_ptr, rply_ptr0, "S\", \"RS\" or \"RA"); + *c1 = *c2 = *c3 = *c4 = *rs = 0; + *timer = 0.0; + return False; } +} + /* **--------------------------------------------------------------------------- ** EL737_StopFast: stop a measurement with an EL737 counter. */ - int EL737_StopFast ( +int EL737_StopFast( /* ============== -*/ void **handle) { +*/ void **handle) +{ - int my_status, nvals; - struct EL737info *info_ptr; - char *rply_ptr0; - /*---------------------------------------------- + int my_status, nvals; + struct EL737info *info_ptr; + char *rply_ptr0; + /*---------------------------------------------- */ - info_ptr = (struct EL737info *) *handle; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_StopFast")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_StopFast")) + return False; + /*---------------------------------------------- ** Send S cmnd to EL737 */ - my_status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "S\r", NULL); - if (!my_status) { - EL737_errcode = EL737__BAD_ASYNSRV; - return False; - }else { - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); + my_status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "S\r", NULL); + if (!my_status) { + EL737_errcode = EL737__BAD_ASYNSRV; + return False; + } else { + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = "?"; + if (rply_ptr0 == NULL) + rply_ptr0 = "?"; - if ( (*rply_ptr0 == '\0' || (*rply_ptr0 == '\r') ) ) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - EL737_SetErrcode (info_ptr, rply_ptr0, "S"); - return False; + if ((*rply_ptr0 == '\0' || (*rply_ptr0 == '\r'))) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; } + EL737_SetErrcode(info_ptr, rply_ptr0, "S"); + return False; } +} + /* **--------------------------------------------------------------------------- ** EL737_WaitIdle: Wait till RS goes to zero. */ - int EL737_WaitIdle ( +int EL737_WaitIdle( /* ============== -*/ void **handle, - int *c1, - int *c2, - int *c3, - int *c4, - float *timer) { +*/ void **handle, + int *c1, int *c2, int *c3, int *c4, float *timer) +{ #ifdef __VMS #include #define hibernate lib$wait (0.25) #else #include #include - struct timespec delay = {0, 250000000}; - struct timespec delay_left; -#ifdef LINUX + struct timespec delay = { 0, 250000000 }; + struct timespec delay_left; +#ifdef LINUX #define hibernate nanosleep(&delay, &delay_left) #else #define hibernate nanosleep_d9 (&delay, &delay_left) #endif #endif - int my_rs; - struct EL737info *info_ptr; - /*---------------------------------------------- + int my_rs; + struct EL737info *info_ptr; + /*---------------------------------------------- */ - *c1 = *c2 = *c3 = *c4 = 0; *timer = 0.0; - info_ptr = (struct EL737info *) *handle; + *c1 = *c2 = *c3 = *c4 = 0; + *timer = 0.0; + info_ptr = (struct EL737info *) *handle; - if (!EL737_AddCallStack (info_ptr, "EL737_WaitIdle")) return False; - /*---------------------------------------------- + if (!EL737_AddCallStack(info_ptr, "EL737_WaitIdle")) + return False; + /*---------------------------------------------- ** Keep reading status till idle. */ - while (EL737_GetStatus (handle, c1, c2, c3, c4, timer, &my_rs)) { - if (my_rs == 0) { - if (EL737_errcode != 0) return False; - EL737_call_depth--; - return True; - } - hibernate; + while (EL737_GetStatus(handle, c1, c2, c3, c4, timer, &my_rs)) { + if (my_rs == 0) { + if (EL737_errcode != 0) + return False; + EL737_call_depth--; + return True; } - return False; /* Error detected in EL737_GetStatus */ + hibernate; } + return False; /* Error detected in EL737_GetStatus */ +} + /*-------------------------------------------- End of EL737_Utility.C =======*/ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/hardsup/el737fix.h b/hardsup/el737fix.h index 4c7d0a1..3dada12 100644 --- a/hardsup/el737fix.h +++ b/hardsup/el737fix.h @@ -9,7 +9,7 @@ #define EL737__BAD_HOST ASYNSRV__BAD_HOST #define EL737__BAD_BIND ASYNSRV__BAD_BIND -#define EL737__BAD_SENDLEN ASYNSRV__BAD_SEND_LEN +#define EL737__BAD_SENDLEN ASYNSRV__BAD_SEND_LEN #define EL737__BAD_SEND ASYNSRV__BAD_SEND #define EL737__BAD_SEND_PIPE ASYNSRV__BAD_SEND_PIPE #define EL737__BAD_SEND_UNKN ASYNSRV__BAD_SEND_UNKN @@ -25,9 +25,9 @@ #define EL737__BAD_RECV1_PIPE ASYNSRV__BAD_RECV1_PIPE #define EL737__BAD_RECV1_NET ASYNSRV__BAD_RECV1_NET #define EL737__BAD_CONNECT ASYNSRV__BAD_CONNECT -#define EL737__BAD_ID -99995 +#define EL737__BAD_ID -99995 #define EL737__BAD_SNTX -99991 #define EL737__BAD_REPLY -99992 #define EL737__BAD_ADR -99993 #define EL737__BAD_RNG -99994 -#endif /* el734fix */ +#endif /* el734fix */ diff --git a/hardsup/el737tcl.c b/hardsup/el737tcl.c index 510b6c9..629af13 100644 --- a/hardsup/el737tcl.c +++ b/hardsup/el737tcl.c @@ -23,13 +23,13 @@ #define True 1 #define False 0 - typedef struct - { - void *pData; /* EL737 open struct */ - } EL737st; +typedef struct { + void *pData; /* EL737 open struct */ +} EL737st; - EXTERN int EL737Action(ClientData pDat, Tcl_Interp *i, int a, char *argv[]); - static void EL737Error2Text(char *pBuffer, int errcode); +EXTERN int EL737Action(ClientData pDat, Tcl_Interp * i, int a, + char *argv[]); +static void EL737Error2Text(char *pBuffer, int errcode); /*--------------------------------------------------------------------------- Tcl has a high niceness level. It deletes a command properly when @@ -38,79 +38,73 @@ ---------------------------------------------------------------------------*/ EXTERN void EL737Murder(ClientData pData) { - EL737st *pTa = (EL737st *)pData; - EL737_Close(&(pTa->pData)); - free(pData); + EL737st *pTa = (EL737st *) pData; + EL737_Close(&(pTa->pData)); + free(pData); } + /*---------------------------------------------------------------------------- CterEL737 is the main entry point for this stuff. It connects to a counter and, on success, creates a new command with the name of the counter. Syntax: EL737 name host port channel ----------------------------------------------------------------------------*/ +---------------------------------------------------------------------------*/ -int CterEL737(ClientData clientData, Tcl_Interp *interp, - int argc, char *argv[]) +int CterEL737(ClientData clientData, Tcl_Interp * interp, + int argc, char *argv[]) { - int iRet; - EL737st *pEL737 = NULL; - int iPort, iChannel, iMotor; - char *pErr = NULL; - char pBueffel[80]; - - /* check arguments */ - if(argc < 5) - { - Tcl_AppendResult(interp, - " Insufficient arguments: CterEL737 name host port channel" - , (char *) NULL); - return TCL_ERROR; - } - - /* convert arguments */ - iRet = Tcl_GetInt(interp,argv[3],&iPort); - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need integer value for port", - (char *)NULL); - return iRet; - } - - iRet = Tcl_GetInt(interp,argv[4],&iChannel); - if(iRet == TCL_ERROR) - { - Tcl_AppendResult(interp,"Need integer value for channel", - (char *)NULL); - return iRet; - } + int iRet; + EL737st *pEL737 = NULL; + int iPort, iChannel, iMotor; + char *pErr = NULL; + char pBueffel[80]; - /* make a new pointer, initialise EL737st */ - pEL737 = (EL737st *)malloc(sizeof(EL737st)); - if(pEL737 ==NULL) - { - Tcl_AppendResult(interp,"No memory in EL734",NULL); - return TCL_ERROR; - } + /* check arguments */ + if (argc < 5) { + Tcl_AppendResult(interp, + " Insufficient arguments: CterEL737 name host port channel", + (char *) NULL); + return TCL_ERROR; + } - /* open the rotten Counter, finally */ - iRet = EL737_Open(&(pEL737->pData), argv[2],iPort,iChannel); - if(iRet) /* success */ - { - /* handle TCL, create new command: the Counter */ - Tcl_CreateCommand(interp,strdup(argv[1]),EL737Action, - (ClientData)pEL737,EL737Murder); - Tcl_AppendResult(interp,strdup(argv[1]),(char *)NULL); - return TCL_OK; - } - else - { - EL737_ErrInfo(&pErr,&iPort,&iChannel, &iMotor); - EL737Error2Text(pBueffel,iPort); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - free(pEL737); - return TCL_ERROR; - } + /* convert arguments */ + iRet = Tcl_GetInt(interp, argv[3], &iPort); + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need integer value for port", (char *) NULL); + return iRet; + } + + iRet = Tcl_GetInt(interp, argv[4], &iChannel); + if (iRet == TCL_ERROR) { + Tcl_AppendResult(interp, "Need integer value for channel", + (char *) NULL); + return iRet; + } + + /* make a new pointer, initialise EL737st */ + pEL737 = (EL737st *) malloc(sizeof(EL737st)); + if (pEL737 == NULL) { + Tcl_AppendResult(interp, "No memory in EL734", NULL); + return TCL_ERROR; + } + + /* open the rotten Counter, finally */ + iRet = EL737_Open(&(pEL737->pData), argv[2], iPort, iChannel); + if (iRet) { /* success */ + /* handle TCL, create new command: the Counter */ + Tcl_CreateCommand(interp, strdup(argv[1]), EL737Action, + (ClientData) pEL737, EL737Murder); + Tcl_AppendResult(interp, strdup(argv[1]), (char *) NULL); + return TCL_OK; + } else { + EL737_ErrInfo(&pErr, &iPort, &iChannel, &iMotor); + EL737Error2Text(pBueffel, iPort); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + free(pEL737); + return TCL_ERROR; + } } + /*-------------------------------------------------------------------------- EL737 Action is the routine where commands send to the conter will @@ -130,271 +124,241 @@ int CterEL737(ClientData clientData, Tcl_Interp *interp, { counts monitor time } counter Stop forces counter to stop ----------------------------------------------------------------------------*/ -EXTERN int EL737Action(ClientData clientData, Tcl_Interp *interp, - int argc, char *argv[]) +EXTERN int EL737Action(ClientData clientData, Tcl_Interp * interp, + int argc, char *argv[]) { - EL737st *pData = (EL737st *)clientData; - char pBueffel[132]; - char pNumBuf[20]; - char *pErr = NULL; - int iC1, iC2, iC3, iC4, iRS, iRet; - float fTime; - int iFlag = 0; - int iMode; - double dVal; + EL737st *pData = (EL737st *) clientData; + char pBueffel[132]; + char pNumBuf[20]; + char *pErr = NULL; + int iC1, iC2, iC3, iC4, iRS, iRet; + float fTime; + int iFlag = 0; + int iMode; + double dVal; - /* obviously we need at least a keyword! */ - if(argc < 2) - { - Tcl_AppendResult(interp,"No keyword given",NULL); - return TCL_ERROR; - } - - /* get values out */ - if(strcmp(argv[1],"value") == 0) - { - iRet = EL737_GetStatus(&(pData->pData),&iC1, &iC2, &iC3, - &iC4,&fTime,&iRS); - if(!iRet) - { - EL737_ErrInfo(&pErr,&iC1,&iC2, &iC3); - EL737Error2Text(pBueffel,iC1); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - sprintf(pNumBuf,"%d",iC2); - Tcl_AppendElement(interp,pNumBuf); - sprintf(pNumBuf,"%d",iC1); - Tcl_AppendElement(interp,pNumBuf); - sprintf(pNumBuf,"%f",fTime); - Tcl_AppendElement(interp,pNumBuf); - return TCL_OK; - } + /* obviously we need at least a keyword! */ + if (argc < 2) { + Tcl_AppendResult(interp, "No keyword given", NULL); + return TCL_ERROR; + } - /* isDone ? */ - if(strcmp(argv[1],"isDone") == 0) - { - iRet = EL737_GetStatus(&(pData->pData),&iC1, &iC2, &iC3, - &iC4,&fTime,&iRS); - if(!iRet) - { - EL737_ErrInfo(&pErr,&iC1,&iC2, &iC3); - EL737Error2Text(pBueffel,iC1); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - if(iRS == 0) /* done is true */ - { - sprintf(pNumBuf,"%d",True); - } - else - { - sprintf(pNumBuf,"%d",False); - } - Tcl_AppendResult(interp,pNumBuf,(char *) NULL); - return TCL_OK; - } - - /* actual counting neutrons in two different modes */ - if(strcmp(argv[1],"wait") == 0) - { - iFlag = 2; - } - if(strcmp(argv[1],"start") == 0) - { - iFlag = 1; - } - if(iFlag > 0) /* we need to count */ - { - if(argc < 4) /* not enough arguments */ - { - Tcl_AppendResult(interp,"Usage: ",argv[0],argv[1], - " timer or monitor val",NULL); - return TCL_ERROR; - } - - /* timer or monitor preset ? */ - if(strcmp(argv[2],"timer") == 0) - { - iMode = 1; - } - else if (strcmp(argv[2],"monitor") == 0) - { - iMode = 2; - } - else - { - Tcl_AppendResult(interp,"Usage: ",argv[0],argv[1], - " timer or monitor val",NULL); - return TCL_ERROR; - } - - /* get the preset value */ - iRet = Tcl_GetDouble(interp,argv[3],&dVal); - if(iRet == TCL_ERROR) - { - return TCL_ERROR; - } - - /* actual start collecting neutrons */ - if(iMode == 1) - { - iRet = EL737_StartTime(&(pData->pData),(float)dVal, - &iRS); - } - else - { - iRet = EL737_StartCnt(&(pData->pData),(int)dVal, - &iRS); - } - if(!iRet) - { - EL737_ErrInfo(&pErr,&iC1,&iC2, &iC3); - EL737Error2Text(pBueffel,iC1); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - } /* end of count startup code */ - - /* if apropriate: wait */ - if(iFlag == 2) - { - iRet = EL737_WaitIdle(&(pData->pData),&iC1, &iC2, &iC3, - &iC4,&fTime); - if(!iRet) - { - EL737_ErrInfo(&pErr,&iC1,&iC2, &iC3); - EL737Error2Text(pBueffel,iC1); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - return TCL_OK; - } - else if(iFlag == 1) - { - return TCL_OK; - } - - /* the stop command */ - if(strcmp(argv[1],"stop") == 0) - { - iRet = EL737_Stop(&(pData->pData),&iC1, &iC2, &iC3, - &iC4,&fTime,&iRS); - if(!iRet) - { - EL737_ErrInfo(&pErr,&iC1,&iC2, &iC3); - EL737Error2Text(pBueffel,iC1); - Tcl_AppendResult(interp,pBueffel,(char *) NULL); - return TCL_ERROR; - } - return TCL_OK; - } + /* get values out */ + if (strcmp(argv[1], "value") == 0) { + iRet = EL737_GetStatus(&(pData->pData), &iC1, &iC2, &iC3, + &iC4, &fTime, &iRS); + if (!iRet) { + EL737_ErrInfo(&pErr, &iC1, &iC2, &iC3); + EL737Error2Text(pBueffel, iC1); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + sprintf(pNumBuf, "%d", iC2); + Tcl_AppendElement(interp, pNumBuf); + sprintf(pNumBuf, "%d", iC1); + Tcl_AppendElement(interp, pNumBuf); + sprintf(pNumBuf, "%f", fTime); + Tcl_AppendElement(interp, pNumBuf); + return TCL_OK; + } - Tcl_AppendResult(interp," obscure command: ",argv[1], - " not understood by EL737 counter", NULL); - return TCL_ERROR; + /* isDone ? */ + if (strcmp(argv[1], "isDone") == 0) { + iRet = EL737_GetStatus(&(pData->pData), &iC1, &iC2, &iC3, + &iC4, &fTime, &iRS); + if (!iRet) { + EL737_ErrInfo(&pErr, &iC1, &iC2, &iC3); + EL737Error2Text(pBueffel, iC1); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + if (iRS == 0) { /* done is true */ + sprintf(pNumBuf, "%d", True); + } else { + sprintf(pNumBuf, "%d", False); + } + Tcl_AppendResult(interp, pNumBuf, (char *) NULL); + return TCL_OK; + } + + /* actual counting neutrons in two different modes */ + if (strcmp(argv[1], "wait") == 0) { + iFlag = 2; + } + if (strcmp(argv[1], "start") == 0) { + iFlag = 1; + } + if (iFlag > 0) { /* we need to count */ + if (argc < 4) { /* not enough arguments */ + Tcl_AppendResult(interp, "Usage: ", argv[0], argv[1], + " timer or monitor val", NULL); + return TCL_ERROR; + } + + /* timer or monitor preset ? */ + if (strcmp(argv[2], "timer") == 0) { + iMode = 1; + } else if (strcmp(argv[2], "monitor") == 0) { + iMode = 2; + } else { + Tcl_AppendResult(interp, "Usage: ", argv[0], argv[1], + " timer or monitor val", NULL); + return TCL_ERROR; + } + + /* get the preset value */ + iRet = Tcl_GetDouble(interp, argv[3], &dVal); + if (iRet == TCL_ERROR) { + return TCL_ERROR; + } + + /* actual start collecting neutrons */ + if (iMode == 1) { + iRet = EL737_StartTime(&(pData->pData), (float) dVal, &iRS); + } else { + iRet = EL737_StartCnt(&(pData->pData), (int) dVal, &iRS); + } + if (!iRet) { + EL737_ErrInfo(&pErr, &iC1, &iC2, &iC3); + EL737Error2Text(pBueffel, iC1); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + } + + /* end of count startup code */ + /* if apropriate: wait */ + if (iFlag == 2) { + iRet = EL737_WaitIdle(&(pData->pData), &iC1, &iC2, &iC3, &iC4, &fTime); + if (!iRet) { + EL737_ErrInfo(&pErr, &iC1, &iC2, &iC3); + EL737Error2Text(pBueffel, iC1); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + return TCL_OK; + } else if (iFlag == 1) { + return TCL_OK; + } + + /* the stop command */ + if (strcmp(argv[1], "stop") == 0) { + iRet = EL737_Stop(&(pData->pData), &iC1, &iC2, &iC3, + &iC4, &fTime, &iRS); + if (!iRet) { + EL737_ErrInfo(&pErr, &iC1, &iC2, &iC3); + EL737Error2Text(pBueffel, iC1); + Tcl_AppendResult(interp, pBueffel, (char *) NULL); + return TCL_ERROR; + } + return TCL_OK; + } + + Tcl_AppendResult(interp, " obscure command: ", argv[1], + " not understood by EL737 counter", NULL); + return TCL_ERROR; } + /*--------------------------------------------------------------------------- EL737Error2Text converts between an EL734 error code to text -----------------------------------------------------------------------------*/ - void EL737Error2Text(char *pBuffer, int iErr) - { - switch(iErr) - { - case -28: - strcpy(pBuffer,"EL737__BAD_ADR"); - break; - case -8: - strcpy(pBuffer,"EL737__BAD_OVFL"); - break; - case -30: - strcpy(pBuffer,"EL737__BAD_BSY"); - break; - case -3: - strcpy(pBuffer,"EL737__BAD_SNTX"); - break; - case -9: - strcpy(pBuffer,"EL737__BAD_CONNECT"); - break; - case -23: - strcpy(pBuffer,"EL737__BAD_FLUSH"); - break; - case -6: - strcpy(pBuffer,"EL734__BAD_DEV"); - break; - case -10: - strcpy(pBuffer,"EL737__BAD_ID"); - break; - case -5: - strcpy(pBuffer,"EL737__BAD_ILLG"); - break; - case -2: - strcpy(pBuffer,"EL737__BAD_LOC"); - break; - case -11: - strcpy(pBuffer,"EL737__BAD_MALLOC"); - break; - case -21: - strcpy(pBuffer,"EL737__BAD_NOT_BCD"); - break; - case -4: - strcpy(pBuffer,"EL737__BAD_OFL"); - break; - case -29: - strcpy(pBuffer,"EL737__BAD_PAR"); - break; - - case -17: - strcpy(pBuffer,"EL737__BAD_RECV"); - break; - case -19: - strcpy(pBuffer,"EL737__BAD_RECV_NET"); - break; - case -18: - strcpy(pBuffer,"EL737__BAD_RECV_PIPE"); - break; - case -20: - strcpy(pBuffer,"EL737__BAD_RECV_UNKN"); - break; - case -22: - strcpy(pBuffer,"EL737__BAD_RECVLEN"); - break; - case -24: - strcpy(pBuffer,"EL737__BAD_RECV1"); - break; - case -26: - strcpy(pBuffer,"EL737__BAD_RECV1_NET"); - break; - case -25: - strcpy(pBuffer,"EL737__BAD_RECV1_PIPE"); - break; - case -27: - strcpy(pBuffer,"EL737__BAD_RNG"); - break; - case -13: - strcpy(pBuffer,"EL737__BAD_SEND"); - break; - case -14: - strcpy(pBuffer,"EL737__BAD_SEND_PIPE"); - break; - case -15: - strcpy(pBuffer,"EL737__BAD_SEND_NET"); - break; - case -16: - strcpy(pBuffer,"EL737__BAD_SEND_UNKN"); - break; - case -12: - strcpy(pBuffer,"EL737__BAD_SENDLEN"); - break; - case -7: - strcpy(pBuffer,"EL737__BAD_SOCKET"); - break; - case -1: - strcpy(pBuffer,"EL737__BAD_TMO"); - break; - default: - strcpy(pBuffer,"Unknown EL737 error"); - break; - } - } +void EL737Error2Text(char *pBuffer, int iErr) +{ + switch (iErr) { + case -28: + strcpy(pBuffer, "EL737__BAD_ADR"); + break; + case -8: + strcpy(pBuffer, "EL737__BAD_OVFL"); + break; + case -30: + strcpy(pBuffer, "EL737__BAD_BSY"); + break; + case -3: + strcpy(pBuffer, "EL737__BAD_SNTX"); + break; + case -9: + strcpy(pBuffer, "EL737__BAD_CONNECT"); + break; + case -23: + strcpy(pBuffer, "EL737__BAD_FLUSH"); + break; + case -6: + strcpy(pBuffer, "EL734__BAD_DEV"); + break; + case -10: + strcpy(pBuffer, "EL737__BAD_ID"); + break; + case -5: + strcpy(pBuffer, "EL737__BAD_ILLG"); + break; + case -2: + strcpy(pBuffer, "EL737__BAD_LOC"); + break; + case -11: + strcpy(pBuffer, "EL737__BAD_MALLOC"); + break; + case -21: + strcpy(pBuffer, "EL737__BAD_NOT_BCD"); + break; + case -4: + strcpy(pBuffer, "EL737__BAD_OFL"); + break; + case -29: + strcpy(pBuffer, "EL737__BAD_PAR"); + break; + + case -17: + strcpy(pBuffer, "EL737__BAD_RECV"); + break; + case -19: + strcpy(pBuffer, "EL737__BAD_RECV_NET"); + break; + case -18: + strcpy(pBuffer, "EL737__BAD_RECV_PIPE"); + break; + case -20: + strcpy(pBuffer, "EL737__BAD_RECV_UNKN"); + break; + case -22: + strcpy(pBuffer, "EL737__BAD_RECVLEN"); + break; + case -24: + strcpy(pBuffer, "EL737__BAD_RECV1"); + break; + case -26: + strcpy(pBuffer, "EL737__BAD_RECV1_NET"); + break; + case -25: + strcpy(pBuffer, "EL737__BAD_RECV1_PIPE"); + break; + case -27: + strcpy(pBuffer, "EL737__BAD_RNG"); + break; + case -13: + strcpy(pBuffer, "EL737__BAD_SEND"); + break; + case -14: + strcpy(pBuffer, "EL737__BAD_SEND_PIPE"); + break; + case -15: + strcpy(pBuffer, "EL737__BAD_SEND_NET"); + break; + case -16: + strcpy(pBuffer, "EL737__BAD_SEND_UNKN"); + break; + case -12: + strcpy(pBuffer, "EL737__BAD_SENDLEN"); + break; + case -7: + strcpy(pBuffer, "EL737__BAD_SOCKET"); + break; + case -1: + strcpy(pBuffer, "EL737__BAD_TMO"); + break; + default: + strcpy(pBuffer, "Unknown EL737 error"); + break; + } +} diff --git a/hardsup/el755_def.h b/hardsup/el755_def.h index 8cfe339..b0539ae 100644 --- a/hardsup/el755_def.h +++ b/hardsup/el755_def.h @@ -21,11 +21,11 @@ /* ** Structure to which the EL755_Open handle points. */ - struct EL755info { - struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */ - int index; - struct RS__MsgStruct to_host; - struct RS__RespStruct from_host; - }; +struct EL755info { + struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */ + int index; + struct RS__MsgStruct to_host; + struct RS__RespStruct from_host; +}; /*------------------------------------------------ End of EL755_DEF.H --*/ -#endif /* _el755_def_ */ +#endif /* _el755_def_ */ diff --git a/hardsup/el755_errorlog.c b/hardsup/el755_errorlog.c index 5491d21..b605dd8 100644 --- a/hardsup/el755_errorlog.c +++ b/hardsup/el755_errorlog.c @@ -16,11 +16,13 @@ ** routine if he wishes to log these errors in ** some other way. */ - void EL755_ErrorLog ( +void EL755_ErrorLog( /* ============== -*/ char *routine_name, - char *text) { +*/ char *routine_name, + char *text) +{ + + fprintf(stderr, "%s: %s\n", routine_name, text); +} - fprintf (stderr, "%s: %s\n", routine_name, text); - } /*-------------------------------------------- End of EL755_ErrorLog.C =======*/ diff --git a/hardsup/el755_utility.c b/hardsup/el755_utility.c index 57cb021..04e108f 100644 --- a/hardsup/el755_utility.c +++ b/hardsup/el755_utility.c @@ -476,540 +476,596 @@ /*-------------------------------------------------------------------------- ** Global Variables */ - static int EL755_call_depth = 0; - static char EL755_routine[5][64]; - static int EL755_errcode = 0; - static int EL755_errno, EL755_vaxc_errno; +static int EL755_call_depth = 0; +static char EL755_routine[5][64]; +static int EL755_errcode = 0; +static int EL755_errno, EL755_vaxc_errno; /* **--------------------------------------------------------------------------- ** EL755_AddCallStack: Add a routine name to the call stack. ** This allows EL755_ErrInfo to generate a ** trace-back in case of error. */ - int EL755_AddCallStack ( +int EL755_AddCallStack( /* ================== -*/ struct EL755info *pntr, - char *name) { +*/ struct EL755info *pntr, + char *name) +{ - if (EL755_errcode != 0) return False; + if (EL755_errcode != 0) + return False; - if (EL755_call_depth < 5) { - StrJoin (EL755_routine[EL755_call_depth], sizeof (EL755_routine[0]), - name, ""); - EL755_call_depth++; - } - - if (pntr == NULL) {EL755_errcode = EL755__NOT_OPEN; return False;} - - if (pntr->asyn_info.skt <= 0) { - memset (pntr->from_host.msg_size, - '0', sizeof (pntr->from_host.msg_size)); - EL755_errcode = (pntr->asyn_info.skt < 0) ? EL755__FORCED_CLOSED - : EL755__NO_SOCKET; - return False; - } - return True; + if (EL755_call_depth < 5) { + StrJoin(EL755_routine[EL755_call_depth], sizeof(EL755_routine[0]), + name, ""); + EL755_call_depth++; } + + if (pntr == NULL) { + EL755_errcode = EL755__NOT_OPEN; + return False; + } + + if (pntr->asyn_info.skt <= 0) { + memset(pntr->from_host.msg_size, + '0', sizeof(pntr->from_host.msg_size)); + EL755_errcode = (pntr->asyn_info.skt < 0) ? EL755__FORCED_CLOSED + : EL755__NO_SOCKET; + return False; + } + return True; +} + /* **--------------------------------------------------------------------------- ** EL755_Close: Close a connection to an EL755 controller. */ - int EL755_Close ( +int EL755_Close( /* =========== -*/ void **handle, - int force_flag) { +*/ void **handle, + int force_flag) +{ - struct EL755info *info_ptr; - char buff[4]; - - info_ptr = (struct EL755info *) *handle; - if (info_ptr == NULL) return True; - - if (info_ptr->asyn_info.skt != 0) { - if (info_ptr->asyn_info.skt > 0) { - AsynSrv_Close (*handle, force_flag); - } - } - free (*handle); - *handle = NULL; + struct EL755info *info_ptr; + char buff[4]; + info_ptr = (struct EL755info *) *handle; + if (info_ptr == NULL) return True; + + if (info_ptr->asyn_info.skt != 0) { + if (info_ptr->asyn_info.skt > 0) { + AsynSrv_Close(*handle, force_flag); + } } + free(*handle); + *handle = NULL; + + return True; +} + /* **--------------------------------------------------------------------------- ** EL755_Config: Configure a connection to an EL755 controller. */ - int EL755_Config ( +int EL755_Config( /* ============ -*/ void **handle, - ...) { +*/ void **handle, + ...) +{ - struct EL755info *info_ptr; - char buff[80], rply[256], my_txt[16]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - int intval, my_txt_l; - /*---------------------------------------------- + struct EL755info *info_ptr; + char buff[80], rply[256], my_txt[16]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + int intval, my_txt_l; + /*---------------------------------------------- */ - info_ptr = (struct EL755info *) *handle; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_Config")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_Config")) + return False; + /*---------------------------------------------- */ - va_start (ap, handle); /* Set up var arg machinery */ - txt_ptr = va_arg (ap, char *); /* Get pntr to first parameter ident */ - while (txt_ptr != NULL) { - my_txt_l = sizeof (my_txt); - StrEdit (my_txt, txt_ptr, "lowercase", &my_txt_l); - /*------------------------------------*/ - if (strcmp (my_txt, "msectmo") == 0) { - intval = va_arg (ap, int); - if ((intval < 100) || (intval > 999999)) { - EL755_errcode = EL755__BAD_PAR; return False; - } - sprintf (buff, "%04d", intval/100); /* Convert to ASCII as .. - ** .. deci-secs */ - memcpy (info_ptr->asyn_info.tmo, buff, 4); - /*------------------------------------*/ - }else if (strcmp (my_txt, "eot") == 0) { - txt_ptr = va_arg (ap, char *); - if (txt_ptr == NULL) { - EL755_errcode = EL755__BAD_PAR; - return False; - } - memcpy (info_ptr->asyn_info.eot, "\0\0\0\0", 4); - switch (txt_ptr[0]) { - case '3': info_ptr->asyn_info.eot[3] = txt_ptr[3]; - case '2': info_ptr->asyn_info.eot[2] = txt_ptr[2]; - case '1': info_ptr->asyn_info.eot[1] = txt_ptr[1]; - case '0': - info_ptr->asyn_info.eot[0] = txt_ptr[0]; - break; - default: - EL755_errcode = EL755__BAD_PAR; - return False; - } - /*------------------------------------*/ - }else if (strcmp (txt_ptr, "index") == 0) { - intval = va_arg (ap, int); - if ((intval < 1) || (intval > 8)) { - EL755_errcode = EL755__BAD_PAR; - return False; - } - info_ptr->index = intval; - /*------------------------------------*/ - }else { - EL755_errcode = EL755__BAD_PAR; - return False; + va_start(ap, handle); /* Set up var arg machinery */ + txt_ptr = va_arg(ap, char *); /* Get pntr to first parameter ident */ + while (txt_ptr != NULL) { + my_txt_l = sizeof(my_txt); + StrEdit(my_txt, txt_ptr, "lowercase", &my_txt_l); + /*------------------------------------*/ + if (strcmp(my_txt, "msectmo") == 0) { + intval = va_arg(ap, int); + if ((intval < 100) || (intval > 999999)) { + EL755_errcode = EL755__BAD_PAR; + return False; } - /*------------------------------------*/ - txt_ptr = va_arg (ap, char *); /* Get pntr to next parameter ident */ + sprintf(buff, "%04d", intval / 100); /* Convert to ASCII as .. + ** .. deci-secs */ + memcpy(info_ptr->asyn_info.tmo, buff, 4); + /*------------------------------------*/ + } else if (strcmp(my_txt, "eot") == 0) { + txt_ptr = va_arg(ap, char *); + if (txt_ptr == NULL) { + EL755_errcode = EL755__BAD_PAR; + return False; + } + memcpy(info_ptr->asyn_info.eot, "\0\0\0\0", 4); + switch (txt_ptr[0]) { + case '3': + info_ptr->asyn_info.eot[3] = txt_ptr[3]; + case '2': + info_ptr->asyn_info.eot[2] = txt_ptr[2]; + case '1': + info_ptr->asyn_info.eot[1] = txt_ptr[1]; + case '0': + info_ptr->asyn_info.eot[0] = txt_ptr[0]; + break; + default: + EL755_errcode = EL755__BAD_PAR; + return False; + } + /*------------------------------------*/ + } else if (strcmp(txt_ptr, "index") == 0) { + intval = va_arg(ap, int); + if ((intval < 1) || (intval > 8)) { + EL755_errcode = EL755__BAD_PAR; + return False; + } + info_ptr->index = intval; + /*------------------------------------*/ + } else { + EL755_errcode = EL755__BAD_PAR; + return False; } - - if (EL755_errcode == 0) EL755_call_depth--; - return True; + /*------------------------------------*/ + txt_ptr = va_arg(ap, char *); /* Get pntr to next parameter ident */ } + + if (EL755_errcode == 0) + EL755_call_depth--; + return True; +} + /* ** ------------------------------------------------------------------------- ** EL755_ErrInfo: Return detailed status from last operation. */ - void EL755_ErrInfo ( +void EL755_ErrInfo( /* ============= -*/ char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno) { +*/ char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno) +{ - int i, j, k; - char buff[80]; - int asyn_errcode, asyn_errno, asyn_vaxerrno; - char* asyn_errtxt; + int i, j, k; + char buff[80]; + int asyn_errcode, asyn_errno, asyn_vaxerrno; + char *asyn_errtxt; - if (EL755_call_depth <= 0) { - strcpy (EL755_routine[0], "EL755_no_error_detected"); - *errcode = 0; - *my_errno = 0; - *vaxc_errno = 0; - }else { - if (EL755_call_depth > 1) { /* Concatenate the names */ - for (i = 1; i < EL755_call_depth; i++) { - strcat (EL755_routine[0], "/"); - StrJoin (EL755_routine[0], sizeof (EL755_routine), - EL755_routine[0], EL755_routine[i]); - } + if (EL755_call_depth <= 0) { + strcpy(EL755_routine[0], "EL755_no_error_detected"); + *errcode = 0; + *my_errno = 0; + *vaxc_errno = 0; + } else { + if (EL755_call_depth > 1) { /* Concatenate the names */ + for (i = 1; i < EL755_call_depth; i++) { + strcat(EL755_routine[0], "/"); + StrJoin(EL755_routine[0], sizeof(EL755_routine), + EL755_routine[0], EL755_routine[i]); } - *errcode = EL755_errcode; - *my_errno = EL755_errno; - *vaxc_errno = EL755_vaxc_errno; - switch (EL755_errcode) { - case EL755__BAD_ASYNSRV: strcpy (buff, "/EL755__BAD_ASYNSRV"); break; - case EL755__BAD_CMD: - case EL755__BAD_DEV: strcpy (buff, "/EL755__BAD_DEV"); break; - case EL755__BAD_ILLG: strcpy (buff, "/EL755__BAD_ILLG"); break; - case EL755__BAD_MALLOC: strcpy (buff, "/EL755__BAD_MALLOC"); break; - case EL755__BAD_OFL: strcpy (buff, "/EL755__BAD_OFL"); break; - case EL755__BAD_PAR: strcpy (buff, "/EL755__BAD_PAR"); break; - case EL755__BAD_SOCKET: strcpy (buff, "/EL755__BAD_SOCKET"); break; - case EL755__BAD_TMO: strcpy (buff, "/EL755__BAD_TMO"); break; - case EL755__FORCED_CLOSED: strcpy (buff, "/EL755__FORCED_CLOSED"); break; - case EL755__NOT_OPEN: strcpy (buff, "/EL755__NOT_OPEN"); break; - case EL755__NO_SOCKET: strcpy (buff, "/EL755__NO_SOCKET"); break; - case EL755__OFFLINE: strcpy (buff, "/EL755__OFFLINE"); break; - case EL755__OUT_OF_RANGE: strcpy (buff, "/EL755__OUT_OF_RANGE"); break; - case EL755__OVFLOW: strcpy (buff, "/EL755__OVFLOW"); break; - case EL755__TOO_LARGE: strcpy (buff, "/EL755__TOO_LARGE"); break; - case EL755__TOO_MANY: strcpy (buff, "/EL755__TOO_MANY"); break; - case EL755__TURNED_OFF: strcpy (buff, "/EL755__TURNED_OFF"); break; - default: sprintf (buff, "/EL755__unknown_err_code: %d", EL755_errcode); - } - StrJoin (EL755_routine[0], sizeof(EL755_routine), EL755_routine[0], buff); } - AsynSrv_ErrInfo (&asyn_errtxt, &asyn_errcode, &asyn_errno, &asyn_vaxerrno); - if (asyn_errcode != 0) { - strcat (EL755_routine[0], "/"); - StrJoin (EL755_routine[0], sizeof(EL755_routine), - EL755_routine[0], asyn_errtxt); + *errcode = EL755_errcode; + *my_errno = EL755_errno; + *vaxc_errno = EL755_vaxc_errno; + switch (EL755_errcode) { + case EL755__BAD_ASYNSRV: + strcpy(buff, "/EL755__BAD_ASYNSRV"); + break; + case EL755__BAD_CMD: + case EL755__BAD_DEV: + strcpy(buff, "/EL755__BAD_DEV"); + break; + case EL755__BAD_ILLG: + strcpy(buff, "/EL755__BAD_ILLG"); + break; + case EL755__BAD_MALLOC: + strcpy(buff, "/EL755__BAD_MALLOC"); + break; + case EL755__BAD_OFL: + strcpy(buff, "/EL755__BAD_OFL"); + break; + case EL755__BAD_PAR: + strcpy(buff, "/EL755__BAD_PAR"); + break; + case EL755__BAD_SOCKET: + strcpy(buff, "/EL755__BAD_SOCKET"); + break; + case EL755__BAD_TMO: + strcpy(buff, "/EL755__BAD_TMO"); + break; + case EL755__FORCED_CLOSED: + strcpy(buff, "/EL755__FORCED_CLOSED"); + break; + case EL755__NOT_OPEN: + strcpy(buff, "/EL755__NOT_OPEN"); + break; + case EL755__NO_SOCKET: + strcpy(buff, "/EL755__NO_SOCKET"); + break; + case EL755__OFFLINE: + strcpy(buff, "/EL755__OFFLINE"); + break; + case EL755__OUT_OF_RANGE: + strcpy(buff, "/EL755__OUT_OF_RANGE"); + break; + case EL755__OVFLOW: + strcpy(buff, "/EL755__OVFLOW"); + break; + case EL755__TOO_LARGE: + strcpy(buff, "/EL755__TOO_LARGE"); + break; + case EL755__TOO_MANY: + strcpy(buff, "/EL755__TOO_MANY"); + break; + case EL755__TURNED_OFF: + strcpy(buff, "/EL755__TURNED_OFF"); + break; + default: + sprintf(buff, "/EL755__unknown_err_code: %d", EL755_errcode); } - *entry_txt = EL755_routine[0]; - EL755_call_depth = 0; - EL755_errcode = 0; + StrJoin(EL755_routine[0], sizeof(EL755_routine), EL755_routine[0], + buff); } + AsynSrv_ErrInfo(&asyn_errtxt, &asyn_errcode, &asyn_errno, + &asyn_vaxerrno); + if (asyn_errcode != 0) { + strcat(EL755_routine[0], "/"); + StrJoin(EL755_routine[0], sizeof(EL755_routine), + EL755_routine[0], asyn_errtxt); + } + *entry_txt = EL755_routine[0]; + EL755_call_depth = 0; + EL755_errcode = 0; +} + /* **--------------------------------------------------------------------------- ** EL755_GetCurrents: Get currents from EL755. */ - int EL755_GetCurrents ( +int EL755_GetCurrents( /* ================= -*/ void **handle, - float *soll, - float *ist) { +*/ void **handle, + float *soll, float *ist) +{ - int iret; - char cmnd[32]; - struct EL755info *info_ptr; - /*---------------------------------------------- + int iret; + char cmnd[32]; + struct EL755info *info_ptr; + /*---------------------------------------------- */ - *soll = *ist = 0.0; - info_ptr = (struct EL755info *) *handle; + *soll = *ist = 0.0; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_GetCurrents")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_GetCurrents")) + return False; + /*---------------------------------------------- ** Send I command to get EL755 currents. Repeat until ** first value is same 2 times consecutively. */ - sprintf (cmnd, "I %d\r", info_ptr->index); - iret = EL755_SendTillTwoVals (handle, cmnd, soll, ist); - if (!iret) return False; + sprintf(cmnd, "I %d\r", info_ptr->index); + iret = EL755_SendTillTwoVals(handle, cmnd, soll, ist); + if (!iret) + return False; + + if (EL755_errcode == 0) + EL755_call_depth--; + return True; +} - if (EL755_errcode == 0) EL755_call_depth--; - return True; - } /* **--------------------------------------------------------------------------- ** EL755_Open: Open a connection to an EL755 controller. */ - int EL755_Open ( +int EL755_Open( /* ========== -*/ void **handle, - char *host, - int port, - int chan, - int indx) { +*/ void **handle, + char *host, int port, int chan, int indx) +{ - int status, i; - char tmo_save[4]; - char *rply_ptr0, *rply_ptr1, *rply_ptr2; - struct EL755info *my_handle; + int status, i; + char tmo_save[4]; + char *rply_ptr0, *rply_ptr1, *rply_ptr2; + struct EL755info *my_handle; /*-------------------------------------------------------- ** Initialise the error info stack and pre-set the ** routine name (in case of error). */ - EL755_errcode = EL755_errno = EL755_vaxc_errno = 0; - strcpy (EL755_routine[0], "EL755_Open"); - EL755_call_depth = 1; + EL755_errcode = EL755_errno = EL755_vaxc_errno = 0; + strcpy(EL755_routine[0], "EL755_Open"); + EL755_call_depth = 1; /*-------------------------------------------------------- ** Assume trouble */ - *handle = NULL; + *handle = NULL; /*-------------------------------------------------------- ** Reserve space for the data we need to store. */ - my_handle = (struct EL755info *) calloc (1, sizeof (*my_handle)); - if (my_handle == NULL) { - EL755_errcode = EL755__BAD_MALLOC; /* calloc failed!! */ - return False; - } + my_handle = (struct EL755info *) calloc(1, sizeof(*my_handle)); + if (my_handle == NULL) { + EL755_errcode = EL755__BAD_MALLOC; /* calloc failed!! */ + return False; + } /*-------------------------------------------------------- ** Set up the connection */ - StrJoin (my_handle->asyn_info.host, sizeof (my_handle->asyn_info.host), - host, ""); - my_handle->asyn_info.port = port; - my_handle->asyn_info.chan = chan; - status = AsynSrv_Open (&my_handle->asyn_info); - if (!status) { - EL755_errcode = EL755__BAD_SOCKET; - GetErrno (&EL755_errno, &EL755_vaxc_errno); /* Save errno info */ - EL755_ErrorLog ("EL755_Open/AsynSrv_Open", "Failed to make connection."); - free (my_handle); - return False; - } - memcpy (tmo_save, my_handle->asyn_info.tmo, 4); - status = EL755_Config ((void *) &my_handle, - "msecTmo", 100, /* Set a short time-out initially since - ** there should be no reason for the RMT, - ** ECHO or ID commands to take very long. - */ - "eot", "1\r", - "index", indx, - NULL); - if (!status) { - /* Some error occurred in EL755_Config - should be impossible! - */ - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - } - /* - ** Now ensure that there's an EL755 connected to the line. The first - ** "RMT 1" command can fail due to pending characters in the EL755 - ** input buffer causing the command to be corrupted. The response is - ** ignored for this reason. - */ - status = AsynSrv_SendCmnds (&my_handle->asyn_info, - &my_handle->to_host, &my_handle->from_host, - "RMT 1\r", /* Try to put EL755 on-line */ - "RMT 1\r", /* Try again in case type-ahead chars corrupted .. - ** .. the first attempt. */ - "ECHO 0\r", /* And turn off echoing */ - NULL); - status = AsynSrv_SendCmnds (&my_handle->asyn_info, - &my_handle->to_host, &my_handle->from_host, - "RMT 1\r", - "ECHO 0\r", - "ID\r", - NULL); - if (!status) { - /* Some error occurred in AsynSrv_SendCmnds. - */ - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - EL755_errcode = EL755__BAD_ASYNSRV; - return False; - } - - rply_ptr0 = AsynSrv_GetReply (&my_handle->asyn_info, - &my_handle->from_host, NULL); - rply_ptr1 = AsynSrv_GetReply (&my_handle->asyn_info, - &my_handle->from_host, rply_ptr0); - rply_ptr2 = AsynSrv_GetReply (&my_handle->asyn_info, - &my_handle->from_host, rply_ptr1); - if ((rply_ptr0 == NULL) || (rply_ptr1 == NULL) || (rply_ptr2 == NULL)) { - /* Some error occurred in AsynSrv_GetReply. - */ - EL755_AddCallStack (my_handle, "NULL response"); - AsynSrv_Close (&my_handle->asyn_info, False); - EL755_errcode = EL755__BAD_DEV; - free (my_handle); - return False; - } - if (rply_ptr0[0] != '\0') { - EL755_AddCallStack (my_handle, rply_ptr0); - AsynSrv_Close (&my_handle->asyn_info, False); /* Bad response! */ - EL755_errcode = EL755__BAD_DEV; - free (my_handle); - return False; - } - if (rply_ptr1[0] != '\0') { - EL755_AddCallStack (my_handle, rply_ptr1); - AsynSrv_Close (&my_handle->asyn_info, False); /* Bad response! */ - EL755_errcode = EL755__BAD_DEV; - free (my_handle); - return False; - } - if (strncmp (rply_ptr2, "EL755 MAGST", 11) != 0) { - EL755_AddCallStack (my_handle, rply_ptr2); - AsynSrv_Close (&my_handle->asyn_info, False); /* Bad response! */ - EL755_errcode = EL755__BAD_DEV; - free (my_handle); - return False; - } - /* The device seems to be an EL755! */ - - memcpy (my_handle->asyn_info.tmo, tmo_save, 4); /* Restore time-out */ - /* - ** The connection is complete. Pass the data structure - ** back to the caller as a handle. - */ - *handle = my_handle; - if (EL755_errcode == 0) EL755_call_depth--; - return True; + StrJoin(my_handle->asyn_info.host, sizeof(my_handle->asyn_info.host), + host, ""); + my_handle->asyn_info.port = port; + my_handle->asyn_info.chan = chan; + status = AsynSrv_Open(&my_handle->asyn_info); + if (!status) { + EL755_errcode = EL755__BAD_SOCKET; + GetErrno(&EL755_errno, &EL755_vaxc_errno); /* Save errno info */ + EL755_ErrorLog("EL755_Open/AsynSrv_Open", + "Failed to make connection."); + free(my_handle); + return False; } + memcpy(tmo_save, my_handle->asyn_info.tmo, 4); + status = EL755_Config((void *) &my_handle, "msecTmo", 100, /* Set a short time-out initially since + ** there should be no reason for the RMT, + ** ECHO or ID commands to take very long. + */ + "eot", "1\r", "index", indx, NULL); + if (!status) { + /* Some error occurred in EL755_Config - should be impossible! + */ + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } + /* + ** Now ensure that there's an EL755 connected to the line. The first + ** "RMT 1" command can fail due to pending characters in the EL755 + ** input buffer causing the command to be corrupted. The response is + ** ignored for this reason. + */ + status = AsynSrv_SendCmnds(&my_handle->asyn_info, &my_handle->to_host, &my_handle->from_host, "RMT 1\r", /* Try to put EL755 on-line */ + "RMT 1\r", /* Try again in case type-ahead chars corrupted .. + ** .. the first attempt. */ + "ECHO 0\r", /* And turn off echoing */ + NULL); + status = AsynSrv_SendCmnds(&my_handle->asyn_info, + &my_handle->to_host, &my_handle->from_host, + "RMT 1\r", "ECHO 0\r", "ID\r", NULL); + if (!status) { + /* Some error occurred in AsynSrv_SendCmnds. + */ + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + EL755_errcode = EL755__BAD_ASYNSRV; + return False; + } + + rply_ptr0 = AsynSrv_GetReply(&my_handle->asyn_info, + &my_handle->from_host, NULL); + rply_ptr1 = AsynSrv_GetReply(&my_handle->asyn_info, + &my_handle->from_host, rply_ptr0); + rply_ptr2 = AsynSrv_GetReply(&my_handle->asyn_info, + &my_handle->from_host, rply_ptr1); + if ((rply_ptr0 == NULL) || (rply_ptr1 == NULL) || (rply_ptr2 == NULL)) { + /* Some error occurred in AsynSrv_GetReply. + */ + EL755_AddCallStack(my_handle, "NULL response"); + AsynSrv_Close(&my_handle->asyn_info, False); + EL755_errcode = EL755__BAD_DEV; + free(my_handle); + return False; + } + if (rply_ptr0[0] != '\0') { + EL755_AddCallStack(my_handle, rply_ptr0); + AsynSrv_Close(&my_handle->asyn_info, False); /* Bad response! */ + EL755_errcode = EL755__BAD_DEV; + free(my_handle); + return False; + } + if (rply_ptr1[0] != '\0') { + EL755_AddCallStack(my_handle, rply_ptr1); + AsynSrv_Close(&my_handle->asyn_info, False); /* Bad response! */ + EL755_errcode = EL755__BAD_DEV; + free(my_handle); + return False; + } + if (strncmp(rply_ptr2, "EL755 MAGST", 11) != 0) { + EL755_AddCallStack(my_handle, rply_ptr2); + AsynSrv_Close(&my_handle->asyn_info, False); /* Bad response! */ + EL755_errcode = EL755__BAD_DEV; + free(my_handle); + return False; + } + /* The device seems to be an EL755! */ + + memcpy(my_handle->asyn_info.tmo, tmo_save, 4); /* Restore time-out */ + /* + ** The connection is complete. Pass the data structure + ** back to the caller as a handle. + */ + *handle = my_handle; + if (EL755_errcode == 0) + EL755_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL755_PutOffline: put the EL755 off-line */ - int EL755_PutOffline ( +int EL755_PutOffline( /* ================ -*/ void **handle) { +*/ void **handle) +{ - int status; - struct EL755info *info_ptr; - char *rply_ptr0, *rply_ptr1, *rply_ptr2; - char buff[132]; - /*---------------------------------------------- + int status; + struct EL755info *info_ptr; + char *rply_ptr0, *rply_ptr1, *rply_ptr2; + char buff[132]; + /*---------------------------------------------- */ - info_ptr = (struct EL755info *) *handle; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_PutOffline")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_PutOffline")) + return False; + /*---------------------------------------------- ** The problem which this routine has is that the EL755 ** may already be off-line. The following is, therefore, ** rather pedantic for most cases which occur in practice. */ - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "RMT 1\r", - "RMT 1\r", - "ECHO 1\r", - "RMT 0\r", - NULL); - if (!status) { - EL755_errcode = EL755__BAD_ASYNSRV; - return False; - } - - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "RMT\r", "", NULL); - if (!status) { - EL755_errcode = EL755__BAD_ASYNSRV; - return False; - } - - rply_ptr0 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - rply_ptr1 = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, rply_ptr0); - if ((rply_ptr0 == NULL) || (rply_ptr1 == NULL)) { - EL755_AddCallStack (info_ptr, "NULL response"); - EL755_errcode = EL755__BAD_ILLG; return False;} - - if ((strcmp (rply_ptr0, "RMT") == 0) && - (strcmp (rply_ptr1, "\n0") == 0)) { - EL755_call_depth--; - return True; - } - - if (strcmp (rply_ptr0, "?OF") == 0) { - EL755_errcode = EL755__OFFLINE; - }else if (strcmp (rply_ptr0, "?OFL") == 0) { - EL755_errcode = EL755__BAD_OFL; - }else if (strcmp (rply_ptr0, "?syntax failure") == 0) { - EL755_errcode = EL755__BAD_CMD; - }else if (strncmp (rply_ptr0, "?TMO", 4) == 0) { - EL755_errcode = EL755__BAD_TMO; - }else { - sprintf (buff, "Cmnd=\"RMT.\" Rply0=\"%.10s\" Rply1=\"%.10s\"", - rply_ptr0, rply_ptr1); - MakePrintable (buff, sizeof(buff), buff); - EL755_AddCallStack (info_ptr, buff); - - sprintf (buff, "Unrecognised responses to RMT command: \"%s\" \"%s\"", - rply_ptr0, rply_ptr1); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - - EL755_errcode = EL755__BAD_ILLG; - } + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "RMT 1\r", + "RMT 1\r", "ECHO 1\r", "RMT 0\r", NULL); + if (!status) { + EL755_errcode = EL755__BAD_ASYNSRV; return False; } + + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "RMT\r", "", NULL); + if (!status) { + EL755_errcode = EL755__BAD_ASYNSRV; + return False; + } + + rply_ptr0 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + rply_ptr1 = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, + rply_ptr0); + if ((rply_ptr0 == NULL) || (rply_ptr1 == NULL)) { + EL755_AddCallStack(info_ptr, "NULL response"); + EL755_errcode = EL755__BAD_ILLG; + return False; + } + + if ((strcmp(rply_ptr0, "RMT") == 0) && (strcmp(rply_ptr1, "\n0") == 0)) { + EL755_call_depth--; + return True; + } + + if (strcmp(rply_ptr0, "?OF") == 0) { + EL755_errcode = EL755__OFFLINE; + } else if (strcmp(rply_ptr0, "?OFL") == 0) { + EL755_errcode = EL755__BAD_OFL; + } else if (strcmp(rply_ptr0, "?syntax failure") == 0) { + EL755_errcode = EL755__BAD_CMD; + } else if (strncmp(rply_ptr0, "?TMO", 4) == 0) { + EL755_errcode = EL755__BAD_TMO; + } else { + sprintf(buff, "Cmnd=\"RMT.\" Rply0=\"%.10s\" Rply1=\"%.10s\"", + rply_ptr0, rply_ptr1); + MakePrintable(buff, sizeof(buff), buff); + EL755_AddCallStack(info_ptr, buff); + + sprintf(buff, "Unrecognised responses to RMT command: \"%s\" \"%s\"", + rply_ptr0, rply_ptr1); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + + EL755_errcode = EL755__BAD_ILLG; + } + return False; +} + /* **--------------------------------------------------------------------------- ** EL755_PutOnline: put the EL755 on-line */ - int EL755_PutOnline ( +int EL755_PutOnline( /* =============== -*/ void **handle, - int echo) { +*/ void **handle, + int echo) +{ - int status, my_echo; - struct EL755info *info_ptr; - char cmnd0[10], buff[132]; - char *rply_ptr; - /*---------------------------------------------- + int status, my_echo; + struct EL755info *info_ptr; + char cmnd0[10], buff[132]; + char *rply_ptr; + /*---------------------------------------------- */ - info_ptr = (struct EL755info *) *handle; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_PutOnline")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_PutOnline")) + return False; + /*---------------------------------------------- */ - if ((echo != 0) && (echo != 1)) { - EL755_errcode = EL755__BAD_PAR; return False; - } - /*---------------------------------------------- + if ((echo != 0) && (echo != 1)) { + EL755_errcode = EL755__BAD_PAR; + return False; + } + /*---------------------------------------------- ** The problem which this routine has is that the state ** of the EL755 is not known. The following is, therefore, ** rather pedantic for most cases which occur in practice. */ - sprintf (cmnd0, "ECHO %d\r", echo); - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "RMT 1\r", - "RMT 1\r", - cmnd0, - NULL); - if (!status) { - EL755_errcode = EL755__BAD_ASYNSRV; - return False; - } - - status = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - "ECHO\r", NULL); - if (!status) { - EL755_errcode = EL755__BAD_ASYNSRV; - return False; - } - - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr == NULL) { - EL755_AddCallStack (info_ptr, "NULL response"); - EL755_errcode = EL755__BAD_ILLG; return False;} - - if ((echo == 1) && (strcmp (rply_ptr, "ECHO") == 0)) { - EL755_call_depth--; - return True; - }else if ((echo == 0) && - (sscanf (rply_ptr, "%d", &my_echo) == 1) && - (my_echo == echo)) { - EL755_call_depth--; - return True; - } - - if (strcmp (rply_ptr, "?OF") == 0) { - EL755_errcode = EL755__OFFLINE; - }else if (strcmp (rply_ptr, "?OFL") == 0) { - EL755_errcode = EL755__BAD_OFL; - }else if (strcmp (rply_ptr, "?syntax failure") == 0) { - EL755_errcode = EL755__BAD_CMD; - }else if (strncmp (rply_ptr, "?TMO", 4) == 0) { - EL755_errcode = EL755__BAD_TMO; - }else { - sprintf (buff, "Cmnd=\"ECHO.\" Rply=\"%.10s\"", rply_ptr); - MakePrintable (buff, sizeof(buff), buff); - EL755_AddCallStack (info_ptr, buff); - - sprintf (buff, "Unrecognised response to ECHO command: \"%s\"", - rply_ptr); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - - EL755_errcode = EL755__BAD_ILLG; - } + sprintf(cmnd0, "ECHO %d\r", echo); + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "RMT 1\r", "RMT 1\r", cmnd0, NULL); + if (!status) { + EL755_errcode = EL755__BAD_ASYNSRV; return False; } + + status = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + "ECHO\r", NULL); + if (!status) { + EL755_errcode = EL755__BAD_ASYNSRV; + return False; + } + + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr == NULL) { + EL755_AddCallStack(info_ptr, "NULL response"); + EL755_errcode = EL755__BAD_ILLG; + return False; + } + + if ((echo == 1) && (strcmp(rply_ptr, "ECHO") == 0)) { + EL755_call_depth--; + return True; + } else if ((echo == 0) && + (sscanf(rply_ptr, "%d", &my_echo) == 1) && + (my_echo == echo)) { + EL755_call_depth--; + return True; + } + + if (strcmp(rply_ptr, "?OF") == 0) { + EL755_errcode = EL755__OFFLINE; + } else if (strcmp(rply_ptr, "?OFL") == 0) { + EL755_errcode = EL755__BAD_OFL; + } else if (strcmp(rply_ptr, "?syntax failure") == 0) { + EL755_errcode = EL755__BAD_CMD; + } else if (strncmp(rply_ptr, "?TMO", 4) == 0) { + EL755_errcode = EL755__BAD_TMO; + } else { + sprintf(buff, "Cmnd=\"ECHO.\" Rply=\"%.10s\"", rply_ptr); + MakePrintable(buff, sizeof(buff), buff); + EL755_AddCallStack(info_ptr, buff); + + sprintf(buff, "Unrecognised response to ECHO command: \"%s\"", + rply_ptr); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + + EL755_errcode = EL755__BAD_ILLG; + } + return False; +} + /* **--------------------------------------------------------------------------- ** EL755_SendTillSameStr: Repeat a command until we get the same @@ -1018,85 +1074,104 @@ ** This routine is intended for internal use only! ** If too many retries, EL755_errcode is set to EL755__TOO_MANY. */ - int EL755_SendTillSameStr ( +int EL755_SendTillSameStr( /* ===================== -*/ void **handle, - char *cmnd, - char *rply, - int rply_len) { +*/ void **handle, + char *cmnd, char *rply, int rply_len) +{ - int iret, i, j, n_ovfl; - struct EL755info *info_ptr; - char *rply_ptr; - char buff[132]; - char replies[6][64]; - /*---------------------------------------------- + int iret, i, j, n_ovfl; + struct EL755info *info_ptr; + char *rply_ptr; + char buff[132]; + char replies[6][64]; + /*---------------------------------------------- */ - info_ptr = (struct EL755info *) *handle; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_SendTillSameStr")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_SendTillSameStr")) + return False; + /*---------------------------------------------- ** Send command. Do it in a ** loop until we get the same response twice to guard ** against RS-232-C problems with the EL755. */ - i = n_ovfl = 0; - StrJoin (rply, rply_len, "#", ""); + i = n_ovfl = 0; + StrJoin(rply, rply_len, "#", ""); - while (i < 6) { - iret = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!iret) { - EL755_errcode = EL755__BAD_ASYNSRV; return False;} - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr == NULL) { - EL755_AddCallStack (info_ptr, "NULL response"); - EL755_errcode = EL755__BAD_ILLG; return False;} - if (strncmp (rply_ptr, "?TMO", 4) == 0) { - EL755_errcode = EL755__BAD_TMO; return False;} - if (strcmp (rply_ptr, "?OF") == 0) { - EL755_errcode = EL755__OFFLINE; return False;} - if (strcmp (rply_ptr, "?OFL") == 0) { - EL755_errcode = EL755__BAD_OFL; return False;} - if (strcmp (rply_ptr, "?syntax failure") == 0) { - EL755_errcode = EL755__BAD_CMD; return False;} - if (strcmp (rply_ptr, "?OV") == 0) { /* Check for overflow. This seems - ** to be an EL755 problem which - ** needs fixing. In the meantime, - ** just force a repeat. - */ - sprintf (buff, "Warning -- \"?OV\" received in response to \"%s\".", - cmnd); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - n_ovfl++; - if (n_ovfl > 10) {EL755_errcode = EL755__TOO_MANY; return False;} - }else { - n_ovfl = 0; - if (strncmp (rply, rply_ptr, rply_len) == 0) break; - StrJoin (rply, rply_len, rply_ptr, ""); - MakePrintable (replies[i], sizeof (replies[0]), rply_ptr); - i++; - } - } - if (strncmp (rply, rply_ptr, rply_len) != 0) { - EL755_errcode = EL755__TOO_MANY; + while (i < 6) { + iret = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!iret) { + EL755_errcode = EL755__BAD_ASYNSRV; return False; } - if (i > 1) { - sprintf (buff, "Warning -- %d retries needed for Cmnd = \"%s\".", - (i - 1), cmnd); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - for (j = 0; j < i; j++) fprintf (stderr, " %d: \"%s\"\n", j, replies[j]); + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr == NULL) { + EL755_AddCallStack(info_ptr, "NULL response"); + EL755_errcode = EL755__BAD_ILLG; + return False; + } + if (strncmp(rply_ptr, "?TMO", 4) == 0) { + EL755_errcode = EL755__BAD_TMO; + return False; + } + if (strcmp(rply_ptr, "?OF") == 0) { + EL755_errcode = EL755__OFFLINE; + return False; + } + if (strcmp(rply_ptr, "?OFL") == 0) { + EL755_errcode = EL755__BAD_OFL; + return False; + } + if (strcmp(rply_ptr, "?syntax failure") == 0) { + EL755_errcode = EL755__BAD_CMD; + return False; + } + if (strcmp(rply_ptr, "?OV") == 0) { /* Check for overflow. This seems + ** to be an EL755 problem which + ** needs fixing. In the meantime, + ** just force a repeat. + */ + sprintf(buff, "Warning -- \"?OV\" received in response to \"%s\".", + cmnd); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + n_ovfl++; + if (n_ovfl > 10) { + EL755_errcode = EL755__TOO_MANY; + return False; + } + } else { + n_ovfl = 0; + if (strncmp(rply, rply_ptr, rply_len) == 0) + break; + StrJoin(rply, rply_len, rply_ptr, ""); + MakePrintable(replies[i], sizeof(replies[0]), rply_ptr); + i++; } - - if (EL755_errcode != 0) return False; - EL755_call_depth--; - return True; } + if (strncmp(rply, rply_ptr, rply_len) != 0) { + EL755_errcode = EL755__TOO_MANY; + return False; + } + if (i > 1) { + sprintf(buff, "Warning -- %d retries needed for Cmnd = \"%s\".", + (i - 1), cmnd); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + for (j = 0; j < i; j++) + fprintf(stderr, " %d: \"%s\"\n", j, replies[j]); + } + + if (EL755_errcode != 0) + return False; + EL755_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL755_SendTillSameVal: Repeat a command until we get the same @@ -1105,94 +1180,115 @@ ** This routine is intended for internal use only! ** If too many retries, EL755_errcode is set to EL755__TOO_MANY. */ - int EL755_SendTillSameVal ( +int EL755_SendTillSameVal( /* ===================== -*/ void **handle, - char *cmnd, - float *val) { +*/ void **handle, + char *cmnd, float *val) +{ - int iret, i, n_ovfl, cnt; - struct EL755info *info_ptr; - float last_val; - char *rply_ptr, *tok; - char buff[132]; - /*---------------------------------------------- + int iret, i, n_ovfl, cnt; + struct EL755info *info_ptr; + float last_val; + char *rply_ptr, *tok; + char buff[132]; + /*---------------------------------------------- */ - info_ptr = (struct EL755info *) *handle; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_SendTillSameVal")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_SendTillSameVal")) + return False; + /*---------------------------------------------- ** Send command. Do it in a ** loop until we get the same response twice to guard ** against RS-232-C problems with the EL755. */ - i = n_ovfl = 0; - *val = 9999.999; - last_val = *val - 1.0; + i = n_ovfl = 0; + *val = 9999.999; + last_val = *val - 1.0; - while (i < 6) { - iret = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!iret) { - EL755_errcode = EL755__BAD_ASYNSRV; return False;} + while (i < 6) { + iret = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!iret) { + EL755_errcode = EL755__BAD_ASYNSRV; + return False; + } - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr == NULL) { - EL755_AddCallStack (info_ptr, "NULL response"); - EL755_errcode = EL755__BAD_ILLG; return False;} + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr == NULL) { + EL755_AddCallStack(info_ptr, "NULL response"); + EL755_errcode = EL755__BAD_ILLG; + return False; + } - if (strcmp (rply_ptr, "?OV") == 0) { /* Check for overflow. This seems - ** to be an EL755 problem which - ** needs fixing. In the meantime, - ** just force a repeat. - */ - sprintf (buff, "Warning -- \"?OV\" received in response to \"%s\".", - cmnd); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - n_ovfl++; - if (n_ovfl > 10) {EL755_errcode = EL755__TOO_MANY; return False;} - }else { - n_ovfl = 0; - if (strcmp (rply_ptr, "?OF") == 0) { - EL755_errcode = EL755__OFFLINE; return False;} - if (strcmp (rply_ptr, "?OFL") == 0) { - EL755_errcode = EL755__BAD_OFL; return False;} - if (strcmp (rply_ptr, "?syntax failure") == 0) { - EL755_errcode = EL755__BAD_CMD; return False;} - if (strcmp (rply_ptr, "?power-supply OFF") == 0) { /* If off, return 0 */ - *val = 0.0; - EL755_call_depth--; return True; - }else { - tok = strtok (rply_ptr, " "); - if ((tok == NULL) || - (sscanf (tok, "%f%n", val, &cnt) != 1) || - (cnt != strlen (tok))) { - EL755_AddCallStack (info_ptr, rply_ptr); - EL755_errcode = EL755__BAD_ILLG; return False; - } - if (*val == last_val) break; - last_val = *val; - } - i++; + if (strcmp(rply_ptr, "?OV") == 0) { /* Check for overflow. This seems + ** to be an EL755 problem which + ** needs fixing. In the meantime, + ** just force a repeat. + */ + sprintf(buff, "Warning -- \"?OV\" received in response to \"%s\".", + cmnd); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + n_ovfl++; + if (n_ovfl > 10) { + EL755_errcode = EL755__TOO_MANY; + return False; } + } else { + n_ovfl = 0; + if (strcmp(rply_ptr, "?OF") == 0) { + EL755_errcode = EL755__OFFLINE; + return False; + } + if (strcmp(rply_ptr, "?OFL") == 0) { + EL755_errcode = EL755__BAD_OFL; + return False; + } + if (strcmp(rply_ptr, "?syntax failure") == 0) { + EL755_errcode = EL755__BAD_CMD; + return False; + } + if (strcmp(rply_ptr, "?power-supply OFF") == 0) { /* If off, return 0 */ + *val = 0.0; + EL755_call_depth--; + return True; + } else { + tok = strtok(rply_ptr, " "); + if ((tok == NULL) || + (sscanf(tok, "%f%n", val, &cnt) != 1) || + (cnt != strlen(tok))) { + EL755_AddCallStack(info_ptr, rply_ptr); + EL755_errcode = EL755__BAD_ILLG; + return False; + } + if (*val == last_val) + break; + last_val = *val; + } + i++; } - if (last_val != *val) { - EL755_errcode = EL755__TOO_MANY; return False;} - - if (i > 1) { - sprintf (buff, "Warning -- %d retries needed for Cmnd = \"%s\".", - (i - 1), cmnd); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - } - - if (EL755_errcode != 0) return False; - EL755_call_depth--; - return True; } + if (last_val != *val) { + EL755_errcode = EL755__TOO_MANY; + return False; + } + + if (i > 1) { + sprintf(buff, "Warning -- %d retries needed for Cmnd = \"%s\".", + (i - 1), cmnd); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + } + + if (EL755_errcode != 0) + return False; + EL755_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL755_SendTillTwoVals: Repeat a command until we get 2 fl.pt. @@ -1205,241 +1301,288 @@ ** changing as the power supply ramps to a new value. ** If too many retries, EL755_errcode is set to EL755__TOO_MANY. */ - int EL755_SendTillTwoVals ( +int EL755_SendTillTwoVals( /* ===================== -*/ void **handle, - char *cmnd, - float *val0, - float *val1) { +*/ void **handle, + char *cmnd, float *val0, float *val1) +{ - int iret, i, n_ovfl, cnt0, cnt1; - struct EL755info *info_ptr; - float last_val; - char *rply_ptr, *tok0, *tok1; - char buff[132]; - /*---------------------------------------------- + int iret, i, n_ovfl, cnt0, cnt1; + struct EL755info *info_ptr; + float last_val; + char *rply_ptr, *tok0, *tok1; + char buff[132]; + /*---------------------------------------------- */ - info_ptr = (struct EL755info *) *handle; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_SendTillTwoVals")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_SendTillTwoVals")) + return False; + /*---------------------------------------------- ** Send command. Do it in a ** loop until we get the same response twice to guard ** against RS-232-C problems with the EL755. */ - i = n_ovfl = 0; - *val0 = 9999.999; - last_val = *val0 - 1.0; + i = n_ovfl = 0; + *val0 = 9999.999; + last_val = *val0 - 1.0; - while (i < 6) { - iret = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!iret) { - EL755_errcode = EL755__BAD_ASYNSRV; return False;} + while (i < 6) { + iret = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!iret) { + EL755_errcode = EL755__BAD_ASYNSRV; + return False; + } - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr == NULL) { - EL755_AddCallStack (info_ptr, "NULL response"); - EL755_errcode = EL755__BAD_ILLG; return False;} + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr == NULL) { + EL755_AddCallStack(info_ptr, "NULL response"); + EL755_errcode = EL755__BAD_ILLG; + return False; + } - if (strcmp (rply_ptr, "?OV") == 0) { /* Check for overflow. This seems - ** to be an EL755 problem which - ** needs fixing. In the meantime, - ** just force a repeat. - */ - sprintf (buff, "Warning -- \"?OV\" received in response to \"%s\".", - cmnd); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - n_ovfl++; - if (n_ovfl > 10) {EL755_errcode = EL755__TOO_MANY; return False;} - }else { - n_ovfl = 0; - if (strcmp (rply_ptr, "?OF") == 0) { - EL755_errcode = EL755__OFFLINE; return False;} - if (strcmp (rply_ptr, "?OFL") == 0) { - EL755_errcode = EL755__BAD_OFL; return False;} - if (strcmp (rply_ptr, "?syntax failure") == 0) { - EL755_errcode = EL755__BAD_CMD; return False;} - if (strcmp (rply_ptr, "?power-supply OFF") == 0) { /* If off, return 0 */ - *val0 = 0.0; - *val1 = 0.0; - EL755_call_depth--; return True; - }else { - tok0 = strtok (rply_ptr, " "); - tok1 = strtok (NULL, " "); - if ((tok0 == NULL) || - (tok1 == NULL) || - (sscanf (tok0, "%f%n", val0, &cnt0) != 1) || - (sscanf (tok1, "%f%n", val1, &cnt1) != 1) || - (cnt0 != strlen (tok0)) || - (cnt1 != strlen (tok1))) { - EL755_AddCallStack (info_ptr, rply_ptr); - EL755_errcode = EL755__BAD_ILLG; return False; - } - if (*val0 == last_val) break; - last_val = *val0; - } - i++; + if (strcmp(rply_ptr, "?OV") == 0) { /* Check for overflow. This seems + ** to be an EL755 problem which + ** needs fixing. In the meantime, + ** just force a repeat. + */ + sprintf(buff, "Warning -- \"?OV\" received in response to \"%s\".", + cmnd); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + n_ovfl++; + if (n_ovfl > 10) { + EL755_errcode = EL755__TOO_MANY; + return False; } + } else { + n_ovfl = 0; + if (strcmp(rply_ptr, "?OF") == 0) { + EL755_errcode = EL755__OFFLINE; + return False; + } + if (strcmp(rply_ptr, "?OFL") == 0) { + EL755_errcode = EL755__BAD_OFL; + return False; + } + if (strcmp(rply_ptr, "?syntax failure") == 0) { + EL755_errcode = EL755__BAD_CMD; + return False; + } + if (strcmp(rply_ptr, "?power-supply OFF") == 0) { /* If off, return 0 */ + *val0 = 0.0; + *val1 = 0.0; + EL755_call_depth--; + return True; + } else { + tok0 = strtok(rply_ptr, " "); + tok1 = strtok(NULL, " "); + if ((tok0 == NULL) || + (tok1 == NULL) || + (sscanf(tok0, "%f%n", val0, &cnt0) != 1) || + (sscanf(tok1, "%f%n", val1, &cnt1) != 1) || + (cnt0 != strlen(tok0)) || (cnt1 != strlen(tok1))) { + EL755_AddCallStack(info_ptr, rply_ptr); + EL755_errcode = EL755__BAD_ILLG; + return False; + } + if (*val0 == last_val) + break; + last_val = *val0; + } + i++; } - if (last_val != *val0) { - EL755_errcode = EL755__TOO_MANY; return False;} - - if (i > 1) { - sprintf (buff, "Warning -- %d retries needed for Cmnd = \"%s\".", - (i - 1), cmnd); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - } - - if (EL755_errcode != 0) return False; - EL755_call_depth--; - return True; } + if (last_val != *val0) { + EL755_errcode = EL755__TOO_MANY; + return False; + } + + if (i > 1) { + sprintf(buff, "Warning -- %d retries needed for Cmnd = \"%s\".", + (i - 1), cmnd); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + } + + if (EL755_errcode != 0) + return False; + EL755_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL755_SetCurrent: Sets current via EL755. */ - int EL755_SetCurrent ( +int EL755_SetCurrent( /* ================ -*/ void **handle, - float soll) { +*/ void **handle, + float soll) +{ - int i, iret; - float my_soll, my_ist; - char cmnd[32], cmnd0[32], buff[132], buff1[132]; - struct EL755info *info_ptr; - /*---------------------------------------------- + int i, iret; + float my_soll, my_ist; + char cmnd[32], cmnd0[32], buff[132], buff1[132]; + struct EL755info *info_ptr; + /*---------------------------------------------- */ - info_ptr = (struct EL755info *) *handle; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_SetCurrent")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_SetCurrent")) + return False; + /*---------------------------------------------- ** Send I command to set EL755 current and I command ** to read back the set value. ** Repeat until set value is correct. */ - sprintf (cmnd, "I %d %.4f\r", info_ptr->index, soll); - sprintf (cmnd0, "I %d\r", info_ptr->index); - i = 0; - my_soll = soll + 1.0; + sprintf(cmnd, "I %d %.4f\r", info_ptr->index, soll); + sprintf(cmnd0, "I %d\r", info_ptr->index); + i = 0; + my_soll = soll + 1.0; - while ((i < 6) && (fabs (soll - my_soll) > 0.01)) { - iret = EL755_SendTillSameStr (handle, cmnd, buff, sizeof(buff)); - if (!iret) return False; - if (buff[0] == NIL) { /* We should get a null response */ - iret = EL755_SendTillSameVal (handle, cmnd0, &my_soll); - if (!iret) return False; - }else if (strcmp (buff, "?value out of range") == 0) { - EL755_errcode = EL755__OUT_OF_RANGE; return False; - }else if (strcmp (buff, "?current limitation") == 0) { - EL755_errcode = EL755__TOO_LARGE; return False; - }else if (strcmp (buff, "?power-supply OFF") == 0) { - if (soll == 0.0) { /* Suppress error if trying to set zero and - .. power supply is off! */ - if (EL755_errcode != 0) return False; - EL755_call_depth--; - return True; - }else { - EL755_errcode = EL755__TURNED_OFF; return False; - } - }else { - sprintf (buff1, "Cmnd=\"%s\" Rply=\"%.10s\"", cmnd, buff); - MakePrintable (buff1, sizeof(buff1), buff1); - EL755_AddCallStack (info_ptr, buff1); - EL755_errcode = EL755__BAD_ILLG; - return False; + while ((i < 6) && (fabs(soll - my_soll) > 0.01)) { + iret = EL755_SendTillSameStr(handle, cmnd, buff, sizeof(buff)); + if (!iret) + return False; + if (buff[0] == NIL) { /* We should get a null response */ + iret = EL755_SendTillSameVal(handle, cmnd0, &my_soll); + if (!iret) + return False; + } else if (strcmp(buff, "?value out of range") == 0) { + EL755_errcode = EL755__OUT_OF_RANGE; + return False; + } else if (strcmp(buff, "?current limitation") == 0) { + EL755_errcode = EL755__TOO_LARGE; + return False; + } else if (strcmp(buff, "?power-supply OFF") == 0) { + if (soll == 0.0) { /* Suppress error if trying to set zero and + .. power supply is off! */ + if (EL755_errcode != 0) + return False; + EL755_call_depth--; + return True; + } else { + EL755_errcode = EL755__TURNED_OFF; + return False; } - i++; + } else { + sprintf(buff1, "Cmnd=\"%s\" Rply=\"%.10s\"", cmnd, buff); + MakePrintable(buff1, sizeof(buff1), buff1); + EL755_AddCallStack(info_ptr, buff1); + EL755_errcode = EL755__BAD_ILLG; + return False; } - - if (fabs (soll - my_soll) > 0.01) { - EL755_errcode = EL755__TOO_MANY; return False;} - - if (i > 1) { - sprintf (buff, "Warning -- %d retries needed for Cmnd = \"%s\".", - (i - 1), cmnd); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - } - if (EL755_errcode != 0) return False; - EL755_call_depth--; - return True; + i++; } + + if (fabs(soll - my_soll) > 0.01) { + EL755_errcode = EL755__TOO_MANY; + return False; + } + + if (i > 1) { + sprintf(buff, "Warning -- %d retries needed for Cmnd = \"%s\".", + (i - 1), cmnd); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + } + if (EL755_errcode != 0) + return False; + EL755_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** EL755_Send sends a command to the EL755 and gets a reply ** ** This routine is intended for internal use only! */ - int EL755_Send ( +int EL755_Send( /* ===================== -*/ void **handle, - char *cmnd, - char *rply, - int rply_len) { +*/ void **handle, + char *cmnd, char *rply, int rply_len) +{ - int iret, i, j, n_ovfl; - struct EL755info *info_ptr; - char *rply_ptr; - char buff[132]; - char replies[6][64]; - /*---------------------------------------------- + int iret, i, j, n_ovfl; + struct EL755info *info_ptr; + char *rply_ptr; + char buff[132]; + char replies[6][64]; + /*---------------------------------------------- */ - info_ptr = (struct EL755info *) *handle; + info_ptr = (struct EL755info *) *handle; - if (!EL755_AddCallStack (info_ptr, "EL755_SendTillSameStr")) return False; - /*---------------------------------------------- + if (!EL755_AddCallStack(info_ptr, "EL755_SendTillSameStr")) + return False; + /*---------------------------------------------- ** Send command. */ - i = n_ovfl = 0; - StrJoin (rply, rply_len, "#", ""); + i = n_ovfl = 0; + StrJoin(rply, rply_len, "#", ""); - - iret = AsynSrv_SendCmnds (&info_ptr->asyn_info, - &info_ptr->to_host, &info_ptr->from_host, - cmnd, NULL); - if (!iret) { - EL755_errcode = EL755__BAD_ASYNSRV; return False;} - rply_ptr = AsynSrv_GetReply ( - &info_ptr->asyn_info, &info_ptr->from_host, NULL); - if (rply_ptr == NULL) { - EL755_AddCallStack (info_ptr, "NULL response"); - EL755_errcode = EL755__BAD_ILLG; return False;} - if (strncmp (rply_ptr, "?TMO", 4) == 0) { - EL755_errcode = EL755__BAD_TMO; return False;} - if (strcmp (rply_ptr, "?OF") == 0) { - EL755_errcode = EL755__OFFLINE; return False;} - if (strcmp (rply_ptr, "?OFL") == 0) { - EL755_errcode = EL755__BAD_OFL; return False;} - if (strcmp (rply_ptr, "?syntax failure") == 0) { - EL755_errcode = EL755__BAD_CMD; return False;} - if (strcmp (rply_ptr, "?OV") == 0) { /* Check for overflow. This seems - ** to be an EL755 problem which - ** needs fixing. In the meantime, - ** just force a repeat. - */ - sprintf (buff, "Warning -- \"?OV\" received in response to \"%s\".", - cmnd); - MakePrintable (buff, sizeof(buff), buff); - EL755_ErrorLog (EL755_routine[EL755_call_depth-1], buff); - n_ovfl++; - if (n_ovfl > 10) {EL755_errcode = EL755__TOO_MANY; return False;} - }else { - n_ovfl = 0; - if (strncmp (rply, rply_ptr, rply_len) == 0) - { - return False; - } - StrJoin (rply, rply_len, rply_ptr, ""); - MakePrintable (replies[i], sizeof (replies[0]), rply_ptr); - i++; - } - if (EL755_errcode != 0) return False; - EL755_call_depth--; - return True; + + iret = AsynSrv_SendCmnds(&info_ptr->asyn_info, + &info_ptr->to_host, &info_ptr->from_host, + cmnd, NULL); + if (!iret) { + EL755_errcode = EL755__BAD_ASYNSRV; + return False; } + rply_ptr = + AsynSrv_GetReply(&info_ptr->asyn_info, &info_ptr->from_host, NULL); + if (rply_ptr == NULL) { + EL755_AddCallStack(info_ptr, "NULL response"); + EL755_errcode = EL755__BAD_ILLG; + return False; + } + if (strncmp(rply_ptr, "?TMO", 4) == 0) { + EL755_errcode = EL755__BAD_TMO; + return False; + } + if (strcmp(rply_ptr, "?OF") == 0) { + EL755_errcode = EL755__OFFLINE; + return False; + } + if (strcmp(rply_ptr, "?OFL") == 0) { + EL755_errcode = EL755__BAD_OFL; + return False; + } + if (strcmp(rply_ptr, "?syntax failure") == 0) { + EL755_errcode = EL755__BAD_CMD; + return False; + } + if (strcmp(rply_ptr, "?OV") == 0) { /* Check for overflow. This seems + ** to be an EL755 problem which + ** needs fixing. In the meantime, + ** just force a repeat. + */ + sprintf(buff, "Warning -- \"?OV\" received in response to \"%s\".", + cmnd); + MakePrintable(buff, sizeof(buff), buff); + EL755_ErrorLog(EL755_routine[EL755_call_depth - 1], buff); + n_ovfl++; + if (n_ovfl > 10) { + EL755_errcode = EL755__TOO_MANY; + return False; + } + } else { + n_ovfl = 0; + if (strncmp(rply, rply_ptr, rply_len) == 0) { + return False; + } + StrJoin(rply, rply_len, rply_ptr, ""); + MakePrintable(replies[i], sizeof(replies[0]), rply_ptr); + i++; + } + if (EL755_errcode != 0) + return False; + EL755_call_depth--; + return True; +} + /*-------------------------------------------- End of EL755_Utility.C =======*/ diff --git a/hardsup/err.c b/hardsup/err.c index 9ef0fad..d409f06 100644 --- a/hardsup/err.c +++ b/hardsup/err.c @@ -3,103 +3,102 @@ EL734Error2Text converts between an EL734 error code to text -----------------------------------------------------------------------------*/ - void EL734Error2Text(char *pBuffer, int iErr) - { - switch(iErr) - { - case -28: - strcpy(pBuffer,"EL734__BAD_ADR"); - break; - case -8: - strcpy(pBuffer,"EL734__BAD_BIND"); - break; - case -30: - strcpy(pBuffer,"EL734__BAD_BSY"); - break; - case -3: - strcpy(pBuffer,"EL734__BAD_CMD"); - break; - case -9: - strcpy(pBuffer,"EL734__BAD_CONNECT"); - break; - case -23: - strcpy(pBuffer,"EL734__BAD_FLUSH"); - break; - case -6: - strcpy(pBuffer,"EL734__BAD_HOST"); - break; - case -10: - strcpy(pBuffer,"EL734__BAD_ID"); - break; - case -5: - strcpy(pBuffer,"EL734__BAD_ILLG"); - break; - case -2: - strcpy(pBuffer,"EL734__BAD_LOC"); - break; - case -11: - strcpy(pBuffer,"EL734__BAD_MALLOC"); - break; - case -21: - strcpy(pBuffer,"EL734__BAD_NOT_BCD"); - break; - case -4: - strcpy(pBuffer,"EL734__BAD_OFL"); - break; - case -29: - strcpy(pBuffer,"EL734__BAD_PAR"); - break; - - case -17: - strcpy(pBuffer,"EL734__BAD_RECV"); - break; - case -19: - strcpy(pBuffer,"EL734__BAD_RECV_NET"); - break; - case -18: - strcpy(pBuffer,"EL734__BAD_RECV_PIPE"); - break; - case -20: - strcpy(pBuffer,"EL734__BAD_RECV_UNKN"); - break; - case -22: - strcpy(pBuffer,"EL734__BAD_RECVLEN"); - break; - case -24: - strcpy(pBuffer,"EL734__BAD_RECV1"); - break; - case -26: - strcpy(pBuffer,"EL734__BAD_RECV1_NET"); - break; - case -25: - strcpy(pBuffer,"EL734__BAD_RECV1_PIPE"); - break; - case -27: - strcpy(pBuffer,"EL734__BAD_RNG"); - break; - case -13: - strcpy(pBuffer,"EL734__BAD_SEND"); - break; - case -14: - strcpy(pBuffer,"EL734__BAD_SEND_PIPE"); - break; - case -15: - strcpy(pBuffer,"EL734__BAD_SEND_NET"); - break; - case -16: - strcpy(pBuffer,"EL734__BAD_SEND_UNKN"); - break; - case -12: - strcpy(pBuffer,"EL734__BAD_SENDLEN"); - break; - case -7: - strcpy(pBuffer,"EL734__BAD_SOCKET"); - break; - case -1: - strcpy(pBuffer,"EL734__BAD_TMO"); - break; - default: - strcpy(pBuffer,"Unknown EL734 error"); - break; - } - } +void EL734Error2Text(char *pBuffer, int iErr) +{ + switch (iErr) { + case -28: + strcpy(pBuffer, "EL734__BAD_ADR"); + break; + case -8: + strcpy(pBuffer, "EL734__BAD_BIND"); + break; + case -30: + strcpy(pBuffer, "EL734__BAD_BSY"); + break; + case -3: + strcpy(pBuffer, "EL734__BAD_CMD"); + break; + case -9: + strcpy(pBuffer, "EL734__BAD_CONNECT"); + break; + case -23: + strcpy(pBuffer, "EL734__BAD_FLUSH"); + break; + case -6: + strcpy(pBuffer, "EL734__BAD_HOST"); + break; + case -10: + strcpy(pBuffer, "EL734__BAD_ID"); + break; + case -5: + strcpy(pBuffer, "EL734__BAD_ILLG"); + break; + case -2: + strcpy(pBuffer, "EL734__BAD_LOC"); + break; + case -11: + strcpy(pBuffer, "EL734__BAD_MALLOC"); + break; + case -21: + strcpy(pBuffer, "EL734__BAD_NOT_BCD"); + break; + case -4: + strcpy(pBuffer, "EL734__BAD_OFL"); + break; + case -29: + strcpy(pBuffer, "EL734__BAD_PAR"); + break; + + case -17: + strcpy(pBuffer, "EL734__BAD_RECV"); + break; + case -19: + strcpy(pBuffer, "EL734__BAD_RECV_NET"); + break; + case -18: + strcpy(pBuffer, "EL734__BAD_RECV_PIPE"); + break; + case -20: + strcpy(pBuffer, "EL734__BAD_RECV_UNKN"); + break; + case -22: + strcpy(pBuffer, "EL734__BAD_RECVLEN"); + break; + case -24: + strcpy(pBuffer, "EL734__BAD_RECV1"); + break; + case -26: + strcpy(pBuffer, "EL734__BAD_RECV1_NET"); + break; + case -25: + strcpy(pBuffer, "EL734__BAD_RECV1_PIPE"); + break; + case -27: + strcpy(pBuffer, "EL734__BAD_RNG"); + break; + case -13: + strcpy(pBuffer, "EL734__BAD_SEND"); + break; + case -14: + strcpy(pBuffer, "EL734__BAD_SEND_PIPE"); + break; + case -15: + strcpy(pBuffer, "EL734__BAD_SEND_NET"); + break; + case -16: + strcpy(pBuffer, "EL734__BAD_SEND_UNKN"); + break; + case -12: + strcpy(pBuffer, "EL734__BAD_SENDLEN"); + break; + case -7: + strcpy(pBuffer, "EL734__BAD_SOCKET"); + break; + case -1: + strcpy(pBuffer, "EL734__BAD_TMO"); + break; + default: + strcpy(pBuffer, "Unknown EL734 error"); + break; + } +} diff --git a/hardsup/failinet.c b/hardsup/failinet.c index 38c39e8..7dd4f8e 100644 --- a/hardsup/failinet.c +++ b/hardsup/failinet.c @@ -89,21 +89,23 @@ /* ** FailInet: Some network failure has occurred. */ - void FailInet (char *text) { +void FailInet(char *text) +{ /* ======== ** Output the given text and exit the process. */ - int my_errno, my_vaxc_errno; + int my_errno, my_vaxc_errno; - GetErrno (&my_errno, &my_vaxc_errno); - printf ("### Internet Error ###\n"); + GetErrno(&my_errno, &my_vaxc_errno); + printf("### Internet Error ###\n"); #ifdef __VMS - printf (" ### errno = %d.\n", my_errno); - printf (" ### vaxc$errno = %d.\n", my_vaxc_errno); + printf(" ### errno = %d.\n", my_errno); + printf(" ### vaxc$errno = %d.\n", my_vaxc_errno); #else - printf (" ### errno = %d.\n", my_errno); + printf(" ### errno = %d.\n", my_errno); #endif - perror (text); - exit (EXIT_FAILURE); - } + perror(text); + exit(EXIT_FAILURE); +} + /*------------------------------------------------- End of FAILINET.C =======*/ diff --git a/hardsup/geterrno.c b/hardsup/geterrno.c index c3edcf1..705a5b6 100644 --- a/hardsup/geterrno.c +++ b/hardsup/geterrno.c @@ -82,15 +82,17 @@ /*-------------------------------------------------------------------------- ** GetErrno: Make copies of errno and vaxc$errno for debug. */ - void GetErrno (int *his_errno, int *his_vaxc_errno) { +void GetErrno(int *his_errno, int *his_vaxc_errno) +{ /* ======== */ - *his_errno = errno; /* Make copy of errno */ + *his_errno = errno; /* Make copy of errno */ #ifdef __VMS - *his_vaxc_errno = vaxc$errno; /* Make copy of vaxc$errno */ + *his_vaxc_errno = vaxc$errno; /* Make copy of vaxc$errno */ #else - *his_vaxc_errno = 1; + *his_vaxc_errno = 1; #endif - return; - } + return; +} + /*------------------------------------------------- End of GETERRNO.C =======*/ diff --git a/hardsup/itc4util.c b/hardsup/itc4util.c index e34dc30..b909790 100644 --- a/hardsup/itc4util.c +++ b/hardsup/itc4util.c @@ -45,377 +45,340 @@ #include "itc4util.h" /* -------------------------------------------------------------------------*/ - int ITC4_Open(pITC4 *pData, char *pHost, int iPort, int iChannel, int iMode) - { - int iRet; - char pCommand[80]; - char pReply[132]; - pITC4 self = NULL; - - self = (pITC4)malloc(sizeof(ITC4)); - if(self == NULL) - { - return ITC4__BADMALLOC; - } - *pData = self; - self->iControl = 1; - self->iRead = 1; - self->iReadOnly = iMode; - self->fDiv = 10.; - self->fMult = 10.; - - iRet = SerialOpen(&self->pData, pHost, iPort, iChannel); - if(iRet != 1) - { - return iRet; - } +int ITC4_Open(pITC4 * pData, char *pHost, int iPort, int iChannel, + int iMode) +{ + int iRet; + char pCommand[80]; + char pReply[132]; + pITC4 self = NULL; - /* set an lengthy timeout for the configuration in order to - prevent problems. - */ - iRet = SerialConfig(&self->pData, 100); - if(iRet != 1) - { - return iRet; - } - - /* an identification test has been here, but I had to removed as not all - ITC4 controllers at SINQ answer the V command. Some versions of the - controller do not recognize it. Sighhhhhhh. I had to put it in again - in order to check for ITC-503, but I handle the thing by default as - an ITC4 if I do not get a proper response. - */ - self->i503 = 0; - iRet = SerialWriteRead(&self->pData,"V\r\n",pReply,131); - if(iRet != 1) - { - return iRet; - } - if(strstr(pReply,"ITC503") != NULL) - { - self->i503 = 1; - } - - if(!self->iReadOnly) - { - /* switch to remote and locked operation */ - iRet = SerialWriteRead(&self->pData,"C3\r\n",pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - - /* set the control sensor, for this we need to switch A0 first, - the do it and switch back - */ - iRet = SerialWriteRead(&self->pData,"A0\r\n",pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - - sprintf(pCommand,"H%1.1d\r\n",self->iControl); - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - - /* controls to automatic */ - iRet = SerialWriteRead(&self->pData,"A3\r\n",pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - /* reset timeout */ - iRet = SerialConfig(&self->pData, 10); - if(iRet != 1) - { - return iRet; - } - } - return 1; - } -/*--------------------------------------------------------------------------*/ - void ITC4_Close(pITC4 *pData) - { - char pReply[132]; - int iRet; - pITC4 self; - - self = *pData; - - /* switch to local operation */ - iRet = SerialWriteRead(&self->pData,"C0\r\n",pReply,131); - /* ignore errors on this one, the thing may be down */ - - /* close connection */ - SerialClose(&self->pData); - - /* free memory */ - free(self); - *pData = NULL; - } -/*--------------------------------------------------------------------------*/ - int ITC4_Config(pITC4 *pData, int iTmo, int iRead, int iControl, - float fDiv,float fMult) - { - int iRet; - char pReply[132]; - char pCommand[10]; - pITC4 self; - - self = *pData; - - /* first timeout */ - if(iTmo > 0) - { - iRet = SerialConfig(&self->pData, iTmo); - if(iRet != 1) - { - return iRet; - } - } - - /* Read Sensor */ - if( (iRead > 0) && (iRead < 5) && (self->iRead != iRead) ) - { - self->iRead = iRead; - } - - /* Control Sensor */ - if( (iControl > 0) && (iControl < 5) ) - { - /* set the control sensor, for this we need to switch A0 first, - the do it and switch back - */ - iRet = SerialWriteRead(&self->pData,"A0\r\n",pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - - /* set sensor */ - sprintf(pCommand,"H%1.1d\r\n",iControl); - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - - /* controls to automatic */ - iRet = SerialWriteRead(&self->pData,"A3\r\n",pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - } - self->fDiv = fDiv; - self->fMult = fMult; - - return 1; - } -/* --------------------------------------------------------------------------*/ - int ITC4_Send(pITC4 *pData, char *pCommand, char *pReply, int iLen) - { - pITC4 self; - - self = *pData; - - /* make sure, that there is a \r at the end of the command */ - if(strchr(pCommand,(int)'\r') == NULL) - { - strcat(pCommand,"\r"); - } - return SerialWriteRead(&self->pData,pCommand,pReply,iLen); - } -/*--------------------------------------------------------------------------*/ - int ITC4_Read(pITC4 *pData, float *fVal) - { - char pCommand[10], pReply[132]; - int iRet; - float fRead = -9999999.; - pITC4 self; - - self = *pData; - - - /* format and send R command */ - sprintf(pCommand,"R%1.1d\r\n",self->iRead); - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - - /* analyse reply */ - if(pReply[0] != 'R') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - - iRet = sscanf(&pReply[1],"%f",&fRead); - if(iRet != 1) - { - return ITC4__BADREAD; - } - if(self->i503) - { - *fVal = fRead; - } - else - { - *fVal = fRead/self->fDiv; - } - return 1; + self = (pITC4) malloc(sizeof(ITC4)); + if (self == NULL) { + return ITC4__BADMALLOC; } -/* -------------------------------------------------------------------------*/ - int ITC4_Set(pITC4 *pData, float fVal) - { - char pCommand[10], pReply[132]; - int iRet, i, iRead; - const float fPrecision = 0.0001; - float fSet, fDelta, fRead, fDum; - pITC4 self; - int iSet; + *pData = self; + self->iControl = 1; + self->iRead = 1; + self->iReadOnly = iMode; + self->fDiv = 10.; + self->fMult = 10.; - self = *pData; + iRet = SerialOpen(&self->pData, pHost, iPort, iChannel); + if (iRet != 1) { + return iRet; + } - if(self->iReadOnly) - { - return ITC4__READONLY; - } - - /* format command */ - if(self->i503) - { - sprintf(pCommand,"T%-7.3f\r\n",fVal); - } - else - { - fSet = fVal; - iSet = (int)(fSet*self->fMult); - sprintf(pCommand,"T%5.5d\r\n",iSet); - } - - /* try three times: send, read, test, if OK return, else - resend. This must be done because the ITC4 tends to loose - characters + /* set an lengthy timeout for the configuration in order to + prevent problems. + */ + iRet = SerialConfig(&self->pData, 100); + if (iRet != 1) { + return iRet; + } + + /* an identification test has been here, but I had to removed as not all + ITC4 controllers at SINQ answer the V command. Some versions of the + controller do not recognize it. Sighhhhhhh. I had to put it in again + in order to check for ITC-503, but I handle the thing by default as + an ITC4 if I do not get a proper response. + */ + self->i503 = 0; + iRet = SerialWriteRead(&self->pData, "V\r\n", pReply, 131); + if (iRet != 1) { + return iRet; + } + if (strstr(pReply, "ITC503") != NULL) { + self->i503 = 1; + } + + if (!self->iReadOnly) { + /* switch to remote and locked operation */ + iRet = SerialWriteRead(&self->pData, "C3\r\n", pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + + /* set the control sensor, for this we need to switch A0 first, + the do it and switch back */ - for(i = 0; i < 3; i++) - { - /* send command */ - iRet = SerialWriteRead(&self->pData,pCommand,pReply,131); - if(iRet != 1) - { - return iRet; - } - if(pReply[0] == '?') - { - strcpy(self->pAns,pReply); - return ITC4__BADCOM; - } - /* read the set value again */ - iRead = self->iRead; - self->iRead = 0; /* make a R0 */ - fDum = self->fDiv; - self->fDiv = self->fMult; - iRet = ITC4_Read(pData,&fRead); - self->iRead = iRead; - self->fDiv = fDum; - if(iRet != 1) - { - return iRet; - } - /* check the value read back */ - if(self->i503) - { - fDelta = fRead - fVal; - } - else - { - fDelta = fRead - fSet; - } - if(fDelta < 0) - fDelta = -fDelta; - if(fDelta < fPrecision) - { - /* Success, go home */ - return 1; - } - } - return ITC4__BADSET; - } -/* -------------------------------------------------------------------------*/ - void ITC4_ErrorTxt(pITC4 *pData,int iCode, char *pError, int iLen) - { - char pBueffel[512]; - pITC4 self; + iRet = SerialWriteRead(&self->pData, "A0\r\n", pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } - self = *pData; - - switch(iCode) - { - case ITC4__BADCOM: - sprintf(pBueffel,"ITC4: Invalid command or offline, got %s", - self->pAns); - strncpy(pError,pBueffel,iLen); - break; - case ITC4__BADPAR: - strncpy(pError,"ITC4: Invalid parameter specified",iLen); - break; - case ITC4__BADMALLOC: - strncpy(pError,"ITC4: Error allocating memory in ITC4",iLen); - break; - case ITC4__BADREAD: - strncpy(pError,"ITC4: Badly formatted answer",iLen); - break; - case ITC4__BADSET: - strncpy(pError,"ITC4: Failed three times to write new set value to ITC4",iLen); - break; - default: - SerialError(iCode, pError,iLen); - break; - } + sprintf(pCommand, "H%1.1d\r\n", self->iControl); + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + + /* controls to automatic */ + iRet = SerialWriteRead(&self->pData, "A3\r\n", pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + /* reset timeout */ + iRet = SerialConfig(&self->pData, 10); + if (iRet != 1) { + return iRet; + } } + return 1; +} + +/*--------------------------------------------------------------------------*/ +void ITC4_Close(pITC4 * pData) +{ + char pReply[132]; + int iRet; + pITC4 self; + + self = *pData; + + /* switch to local operation */ + iRet = SerialWriteRead(&self->pData, "C0\r\n", pReply, 131); + /* ignore errors on this one, the thing may be down */ + + /* close connection */ + SerialClose(&self->pData); + + /* free memory */ + free(self); + *pData = NULL; +} + +/*--------------------------------------------------------------------------*/ +int ITC4_Config(pITC4 * pData, int iTmo, int iRead, int iControl, + float fDiv, float fMult) +{ + int iRet; + char pReply[132]; + char pCommand[10]; + pITC4 self; + + self = *pData; + + /* first timeout */ + if (iTmo > 0) { + iRet = SerialConfig(&self->pData, iTmo); + if (iRet != 1) { + return iRet; + } + } + + /* Read Sensor */ + if ((iRead > 0) && (iRead < 5) && (self->iRead != iRead)) { + self->iRead = iRead; + } + + /* Control Sensor */ + if ((iControl > 0) && (iControl < 5)) { + /* set the control sensor, for this we need to switch A0 first, + the do it and switch back + */ + iRet = SerialWriteRead(&self->pData, "A0\r\n", pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + + /* set sensor */ + sprintf(pCommand, "H%1.1d\r\n", iControl); + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + + /* controls to automatic */ + iRet = SerialWriteRead(&self->pData, "A3\r\n", pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + } + self->fDiv = fDiv; + self->fMult = fMult; + + return 1; +} + +/* --------------------------------------------------------------------------*/ +int ITC4_Send(pITC4 * pData, char *pCommand, char *pReply, int iLen) +{ + pITC4 self; + + self = *pData; + + /* make sure, that there is a \r at the end of the command */ + if (strchr(pCommand, (int) '\r') == NULL) { + strcat(pCommand, "\r"); + } + return SerialWriteRead(&self->pData, pCommand, pReply, iLen); +} + +/*--------------------------------------------------------------------------*/ +int ITC4_Read(pITC4 * pData, float *fVal) +{ + char pCommand[10], pReply[132]; + int iRet; + float fRead = -9999999.; + pITC4 self; + + self = *pData; + + + /* format and send R command */ + sprintf(pCommand, "R%1.1d\r\n", self->iRead); + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + + /* analyse reply */ + if (pReply[0] != 'R') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + + iRet = sscanf(&pReply[1], "%f", &fRead); + if (iRet != 1) { + return ITC4__BADREAD; + } + if (self->i503) { + *fVal = fRead; + } else { + *fVal = fRead / self->fDiv; + } + return 1; +} + +/* -------------------------------------------------------------------------*/ +int ITC4_Set(pITC4 * pData, float fVal) +{ + char pCommand[10], pReply[132]; + int iRet, i, iRead; + const float fPrecision = 0.0001; + float fSet, fDelta, fRead, fDum; + pITC4 self; + int iSet; + + self = *pData; + + if (self->iReadOnly) { + return ITC4__READONLY; + } + + /* format command */ + if (self->i503) { + sprintf(pCommand, "T%-7.3f\r\n", fVal); + } else { + fSet = fVal; + iSet = (int) (fSet * self->fMult); + sprintf(pCommand, "T%5.5d\r\n", iSet); + } + + /* try three times: send, read, test, if OK return, else + resend. This must be done because the ITC4 tends to loose + characters + */ + for (i = 0; i < 3; i++) { + /* send command */ + iRet = SerialWriteRead(&self->pData, pCommand, pReply, 131); + if (iRet != 1) { + return iRet; + } + if (pReply[0] == '?') { + strcpy(self->pAns, pReply); + return ITC4__BADCOM; + } + /* read the set value again */ + iRead = self->iRead; + self->iRead = 0; /* make a R0 */ + fDum = self->fDiv; + self->fDiv = self->fMult; + iRet = ITC4_Read(pData, &fRead); + self->iRead = iRead; + self->fDiv = fDum; + if (iRet != 1) { + return iRet; + } + /* check the value read back */ + if (self->i503) { + fDelta = fRead - fVal; + } else { + fDelta = fRead - fSet; + } + if (fDelta < 0) + fDelta = -fDelta; + if (fDelta < fPrecision) { + /* Success, go home */ + return 1; + } + } + return ITC4__BADSET; +} + +/* -------------------------------------------------------------------------*/ +void ITC4_ErrorTxt(pITC4 * pData, int iCode, char *pError, int iLen) +{ + char pBueffel[512]; + pITC4 self; + + self = *pData; + + switch (iCode) { + case ITC4__BADCOM: + sprintf(pBueffel, "ITC4: Invalid command or offline, got %s", + self->pAns); + strncpy(pError, pBueffel, iLen); + break; + case ITC4__BADPAR: + strncpy(pError, "ITC4: Invalid parameter specified", iLen); + break; + case ITC4__BADMALLOC: + strncpy(pError, "ITC4: Error allocating memory in ITC4", iLen); + break; + case ITC4__BADREAD: + strncpy(pError, "ITC4: Badly formatted answer", iLen); + break; + case ITC4__BADSET: + strncpy(pError, + "ITC4: Failed three times to write new set value to ITC4", + iLen); + break; + default: + SerialError(iCode, pError, iLen); + break; + } +} diff --git a/hardsup/itc4util.h b/hardsup/itc4util.h index f700c8a..1f225b2 100644 --- a/hardsup/itc4util.h +++ b/hardsup/itc4util.h @@ -16,11 +16,11 @@ defined are those defined for serialsinq plus a few additional ones: */ -#define ITC4__BADCOM -501 +#define ITC4__BADCOM -501 /* command not recognized */ -#define ITC4__BADPAR -502 +#define ITC4__BADPAR -502 /* bad parameter to command */ -#define ITC4__BADMALLOC -503 +#define ITC4__BADMALLOC -503 /* error allocating memory */ #define ITC4__BADREAD -504 /* error analysing command string on Read */ @@ -30,21 +30,22 @@ /* failed three times to set temperature */ #define ITC4__READONLY -531 /*------------------------------------------------------------------------*/ - typedef struct __ITC4 { - int iRead; - int iControl; - void *pData; - char pAns[80]; - float fDiv; - float fMult; - int iReadOnly; - int i503; /* flag for model 503, understanding float*/ - } ITC4; +typedef struct __ITC4 { + int iRead; + int iControl; + void *pData; + char pAns[80]; + float fDiv; + float fMult; + int iReadOnly; + int i503; /* flag for model 503, understanding float */ +} ITC4; - typedef struct __ITC4 *pITC4; +typedef struct __ITC4 *pITC4; /*-----------------------------------------------------------------------*/ - int ITC4_Open(pITC4 *pData,char *pHost, int iPort, int iChannel, int iMode); +int ITC4_Open(pITC4 * pData, char *pHost, int iPort, int iChannel, + int iMode); /***** creates an ITC4 datastructure and opens a connection to the ITCL4 controller. Input Parameters are: the hostname @@ -55,17 +56,17 @@ Return values are 1 for success, a negative error code on failure. - */ - - void ITC4_Close(pITC4 *pData); + */ + +void ITC4_Close(pITC4 * pData); /****** close a connection to an ITC4controller and frees its data structure. The only parameter is a pointer to the data structure for this controller. This pointer will be invalid after this call. - */ + */ - int ITC4_Config(pITC4 *pData, int iTmo, int iRead, - int iControl, float fDiv, float fMult); +int ITC4_Config(pITC4 * pData, int iTmo, int iRead, + int iControl, float fDiv, float fMult); /***** configure some aspects of a ITC4temperature controller. The parameter are: - a pointer to the data structure for the controller as @@ -80,8 +81,8 @@ The function returns 1 on success, a negative error code on failure. */ - - int ITC4_Send(pITC4 *pData, char *pCommand, char *pReply, int iLen); + +int ITC4_Send(pITC4 * pData, char *pCommand, char *pReply, int iLen); /******* send a the command in pCommand to the ITC4controller. A possible reply is returned in the buffer pReply. Maximum iLen characters are copied to pReply. @@ -91,8 +92,8 @@ Return values are 1 for success, a negative error code on failure. */ - - int ITC4_Read(pITC4 *pData, float *fVal); + +int ITC4_Read(pITC4 * pData, float *fVal); /******* reads the current actual temperature of the sensor configured by ConfigITC4for reading. The value is returned in fVal. The first parameter is a pointer to a ITCL4 @@ -102,7 +103,7 @@ failure. */ - int ITC4_Set(pITC4 *pData, float fVal); +int ITC4_Set(pITC4 * pData, float fVal); /****** sets a new preset temperature in the ITC4temperature controller. Parameters are: - a pointer to a ITC4data structure as returned by OpenITCL4. @@ -111,14 +112,12 @@ Return values are 1 for success, a negative error code on failure. */ - - void ITC4_ErrorTxt(pITC4 *pData, int iCode, char *pError, int iLen); + +void ITC4_ErrorTxt(pITC4 * pData, int iCode, char *pError, int iLen); /******* translates one of the negative error ITC4error codes into text. Maximum iLen bytes will be copied to the buffer pError; - */ - - -#endif - - + */ + + +#endif diff --git a/hardsup/makeprint.c b/hardsup/makeprint.c index 63f2041..cf949bd 100644 --- a/hardsup/makeprint.c +++ b/hardsup/makeprint.c @@ -154,7 +154,7 @@ #include #include #ifdef FORTIFY - #include +#include #endif #include @@ -167,110 +167,157 @@ **-------------------------------------------------------------------------- ** MakeCharPrintable: makes a single character printable. */ - int MakeCharPrintable (char *out, int out_size, char in) { +int MakeCharPrintable(char *out, int out_size, char in) +{ /* ================= ** ** Return value is number of chars put into *out. */ - char buff[8], *pntr; + char buff[8], *pntr; - pntr = buff; + pntr = buff; - if ((in & 0x80) != 0) { /* Parity bit set? */ - *pntr++ = '^'; /* Yes. Put a '^' in the buffer .. */ - in = in & 0x7f; /* .. and remove the parity bit. */ - } - - switch (in) { - case '^': *pntr++ = '\\'; *pntr++ = '^'; break; - case '\\': *pntr++ = '\\'; *pntr++ = '\\'; break; - case '\000': *pntr++ = '\\'; *pntr++ = '0'; break; - case '\007': *pntr++ = '\\'; *pntr++ = 'a'; break; - case '\010': *pntr++ = '\\'; *pntr++ = 'b'; break; - case '\011': *pntr++ = '\\'; *pntr++ = 't'; break; - case '\012': *pntr++ = '\\'; *pntr++ = 'n'; break; - case '\013': *pntr++ = '\\'; *pntr++ = 'v'; break; - case '\014': *pntr++ = '\\'; *pntr++ = 'f'; break; - case '\015': *pntr++ = '\\'; *pntr++ = 'r'; break; - default: - if ((in < ' ') || (in > '~')) { - pntr += sprintf (pntr, "\\%3.3o", in); - }else { - *pntr++ = in; - } - } - out_size = (out_size > (pntr - buff)) ? (pntr - buff) : out_size; - memcpy (out, buff, out_size); - return out_size; + if ((in & 0x80) != 0) { /* Parity bit set? */ + *pntr++ = '^'; /* Yes. Put a '^' in the buffer .. */ + in = in & 0x7f; /* .. and remove the parity bit. */ } + + switch (in) { + case '^': + *pntr++ = '\\'; + *pntr++ = '^'; + break; + case '\\': + *pntr++ = '\\'; + *pntr++ = '\\'; + break; + case '\000': + *pntr++ = '\\'; + *pntr++ = '0'; + break; + case '\007': + *pntr++ = '\\'; + *pntr++ = 'a'; + break; + case '\010': + *pntr++ = '\\'; + *pntr++ = 'b'; + break; + case '\011': + *pntr++ = '\\'; + *pntr++ = 't'; + break; + case '\012': + *pntr++ = '\\'; + *pntr++ = 'n'; + break; + case '\013': + *pntr++ = '\\'; + *pntr++ = 'v'; + break; + case '\014': + *pntr++ = '\\'; + *pntr++ = 'f'; + break; + case '\015': + *pntr++ = '\\'; + *pntr++ = 'r'; + break; + default: + if ((in < ' ') || (in > '~')) { + pntr += sprintf(pntr, "\\%3.3o", in); + } else { + *pntr++ = in; + } + } + out_size = (out_size > (pntr - buff)) ? (pntr - buff) : out_size; + memcpy(out, buff, out_size); + return out_size; +} + /* **-------------------------------------------------------------------------- ** MakeMemPrintable: alternative version of MakePrintable. */ - char *MakeMemPrintable ( +char *MakeMemPrintable( /* ================ -*/ char *out, - int out_size, - char *in, - int in_len) { +*/ char *out, + int out_size, char *in, int in_len) +{ - int i; - char *pntr; + int i; + char *pntr; - if (out_size <= 0) return out; - - while ((out_size > 1) && (in_len > 0)) { - i = MakeCharPrintable (out, (out_size - 1), *in); - out += i; out_size -= i; - in++; in_len--; - } - *out = NIL; + if (out_size <= 0) return out; + + while ((out_size > 1) && (in_len > 0)) { + i = MakeCharPrintable(out, (out_size - 1), *in); + out += i; + out_size -= i; + in++; + in_len--; } + *out = NIL; + return out; +} + /* **-------------------------------------------------------------------------- ** MakePrint: Make all characters in a buffer printable. */ - char *MakePrint (char *chr) { +char *MakePrint(char *chr) +{ /* ========= */ - int len, i; - - for (i = 0; chr[i] != NIL; i++) chr[i] &= 0x7F; + int len, i; - len = strlen (chr); - if (len <= 0) return chr; - - if (chr[len-1] == '\r') chr[len-1] = NIL; - if (chr[len-1] == '\n') chr[len-1] = NIL; - - for (i = 0; chr[i] != NIL; i++) { - if (chr[i] < ' ') chr[i] = '.'; - if (chr[i] == 0x7F) chr[i] = '.'; - } + for (i = 0; chr[i] != NIL; i++) + chr[i] &= 0x7F; + len = strlen(chr); + if (len <= 0) return chr; + + if (chr[len - 1] == '\r') + chr[len - 1] = NIL; + if (chr[len - 1] == '\n') + chr[len - 1] = NIL; + + for (i = 0; chr[i] != NIL; i++) { + if (chr[i] < ' ') + chr[i] = '.'; + if (chr[i] == 0x7F) + chr[i] = '.'; } + + return chr; +} + /* **-------------------------------------------------------------------------- ** MakePrintable: improved version of MakePrint. */ - char *MakePrintable ( +char *MakePrintable( /* ============= -*/ char *out, - int out_size, - char *in) { +*/ char *out, + int out_size, char *in) +{ - int i; - char *pntr; + int i; + char *pntr; - if (out_size <= 0) return out; - - while ((out_size > 1) && (*in != NIL)) { - i = MakeCharPrintable (out, (out_size - 1), *in); - in++; out += i; out_size -= i; - } - *out = NIL; + if (out_size <= 0) return out; + + while ((out_size > 1) && (*in != NIL)) { + i = MakeCharPrintable(out, (out_size - 1), *in); + in++; + out += i; + out_size -= i; } + *out = NIL; + return out; +} + /*-------------------------------------------- End of MakePrint.C =======*/ diff --git a/hardsup/rs232c_def.h b/hardsup/rs232c_def.h index 2753bd1..de201e7 100644 --- a/hardsup/rs232c_def.h +++ b/hardsup/rs232c_def.h @@ -9,8 +9,8 @@ #define RS__PROTOCOL_ID "V01A" #define RS__PROTOCOL_ID_V01B "V01B" -#define RS__PROTOCOL_CODE 1 /* Code corresponding to RS__PROTOCOL_ID */ -#define RS__PROTOCOL_CODE_V01B 2 /* Code corresponding to RS__PROTOCOL_ID_0 */ +#define RS__PROTOCOL_CODE 1 /* Code corresponding to RS__PROTOCOL_ID */ +#define RS__PROTOCOL_CODE_V01B 2 /* Code corresponding to RS__PROTOCOL_ID_0 */ #ifndef OffsetOf #define OffsetOf(type, identifier) ((size_t)(&((type*) NULL)->identifier)) @@ -76,28 +76,28 @@ ** ** An example of a command item might be: "0006RMT 1\r" **--------------------------------------------------------------------------*/ - struct RS__MsgStruct { - char msg_size[4]; /* 4 ASCII decimal chars!! */ - char msg_id[4]; - char c_pcol_lvl[4]; /* Client protocol level */ - char serial_port[4]; - char tmo[4]; /* Units are 0.1 secs */ - char terms[4]; - char n_cmnds[4]; - char cmnds[356]; - }; - /* - ** The "cmnds" buffer in RS__MsgStruct is a concatenated - ** list of the following structures. - */ - struct RS__CmndStruct { - char cmnd_len[2]; - char cmnd[1]; - }; - struct RS__CmndStruct_V01B { - char cmnd_len[4]; - char cmnd[1]; - }; +struct RS__MsgStruct { + char msg_size[4]; /* 4 ASCII decimal chars!! */ + char msg_id[4]; + char c_pcol_lvl[4]; /* Client protocol level */ + char serial_port[4]; + char tmo[4]; /* Units are 0.1 secs */ + char terms[4]; + char n_cmnds[4]; + char cmnds[356]; +}; + /* + ** The "cmnds" buffer in RS__MsgStruct is a concatenated + ** list of the following structures. + */ +struct RS__CmndStruct { + char cmnd_len[2]; + char cmnd[1]; +}; +struct RS__CmndStruct_V01B { + char cmnd_len[4]; + char cmnd[1]; +}; /*---------------------------------------------------------------------------- ** Structure of Reply from Server to Client - everything is sent in ASCII ** for LabView's benefit. @@ -150,37 +150,37 @@ ** ** An example of a reply item might be: "0009\r12.3456\0" **--------------------------------------------------------------------------*/ - struct RS__RespStruct { - char msg_size[4]; - char msg_id[4]; - char s_pcol_lvl[4]; /* Server protocol level */ - char n_rply[4]; /* Error if < 0 */ - union { - char rplys[496]; - char sub_status[12]; - } u; - }; - /* - ** The "rplys" buffer in RS__RespStruct is a - ** concatenated list of the following structures. - */ - struct RS__RplyStruct { - char rply_len[2]; /* 2 ASCII decimal chars!! - ** The length includes the - ** terminator, term, and the - ** zero terminator of rply. - */ - char term; /* The terminating character */ - char rply[1]; /* Zero terminated string */ - }; - struct RS__RplyStruct_V01B { - char rply_len[4]; /* 4 ASCII decimal chars!! - ** The length includes the - ** terminator, term, and the - ** zero terminator of rply. - */ - char term; /* The terminating character */ - char rply[1]; /* Zero terminated string */ - }; +struct RS__RespStruct { + char msg_size[4]; + char msg_id[4]; + char s_pcol_lvl[4]; /* Server protocol level */ + char n_rply[4]; /* Error if < 0 */ + union { + char rplys[496]; + char sub_status[12]; + } u; +}; + /* + ** The "rplys" buffer in RS__RespStruct is a + ** concatenated list of the following structures. + */ +struct RS__RplyStruct { + char rply_len[2]; /* 2 ASCII decimal chars!! + ** The length includes the + ** terminator, term, and the + ** zero terminator of rply. + */ + char term; /* The terminating character */ + char rply[1]; /* Zero terminated string */ +}; +struct RS__RplyStruct_V01B { + char rply_len[4]; /* 4 ASCII decimal chars!! + ** The length includes the + ** terminator, term, and the + ** zero terminator of rply. + */ + char term; /* The terminating character */ + char rply[1]; /* Zero terminated string */ +}; /*------------------------------------------------ End of RS232C_DEF.H --*/ -#endif /* _rs232c_def_ */ +#endif /* _rs232c_def_ */ diff --git a/hardsup/serialsinq.c b/hardsup/serialsinq.c index 5c35846..e0e3787 100644 --- a/hardsup/serialsinq.c +++ b/hardsup/serialsinq.c @@ -76,399 +76,406 @@ #define True 1 #define False 0 - struct SerialInfo { - int skt; - int iForce; - int port; - int chan; - char host[20]; - int tmo; - int msg_id; - int n_replies, max_replies; - char pTerms[4]; - char pSendTerm[10]; - struct RS__MsgStruct to_host; - struct RS__RespStruct from_host; - SerialSleep pFunc; - void *pData; - struct AsynSrv__info sAsync; - }; +struct SerialInfo { + int skt; + int iForce; + int port; + int chan; + char host[20]; + int tmo; + int msg_id; + int n_replies, max_replies; + char pTerms[4]; + char pSendTerm[10]; + struct RS__MsgStruct to_host; + struct RS__RespStruct from_host; + SerialSleep pFunc; + void *pData; + struct AsynSrv__info sAsync; +}; /*------------------- The default sleep function -----------------------*/ - static int SerialNccrrrh(void *pData, int iTime) - { - usleep(50); - return 1; - } +static int SerialNccrrrh(void *pData, int iTime) +{ + usleep(50); + return 1; +} /*-----------------------------------------------------------------------*/ - int SerialOpen(void **pData, char *pHost, int iPort, int iChannel) - { - int status; - struct SerialInfo *my_info; - void *my_hndl; - struct hostent *rmt_hostent; - struct in_addr *rmt_inet_addr_pntr; - int rmt_sockname_len; - struct sockaddr_in lcl_sockname; - struct sockaddr_in rmt_sockname; - char msr_cmnd[20]; - struct RS__RplyStruct *rply_ptr; +int SerialOpen(void **pData, char *pHost, int iPort, int iChannel) +{ + int status; + struct SerialInfo *my_info; + void *my_hndl; + struct hostent *rmt_hostent; + struct in_addr *rmt_inet_addr_pntr; + int rmt_sockname_len; + struct sockaddr_in lcl_sockname; + struct sockaddr_in rmt_sockname; + char msr_cmnd[20]; + struct RS__RplyStruct *rply_ptr; + + *pData = NULL; - *pData = NULL; - /* ** allocate memory first */ - *pData = malloc (sizeof (struct SerialInfo)); - if (*pData == NULL) { - return EL734__BAD_MALLOC; /* malloc failed!! */ - } - my_info = *pData; - memset(my_info,0,sizeof(struct SerialInfo)); + *pData = malloc(sizeof(struct SerialInfo)); + if (*pData == NULL) { + return EL734__BAD_MALLOC; /* malloc failed!! */ + } + my_info = *pData; + memset(my_info, 0, sizeof(struct SerialInfo)); /* **-------------------------- Set up the connection */ - my_info->sAsync.port = iPort; - strcpy(my_info->sAsync.host,pHost); - my_info->sAsync.chan = iChannel; - status = AsynSrv_Open (&(my_info->sAsync)); - if (status != 1) { - return OPENFAILURE; - } - - /* intialize data structures */ - StrJoin (my_info->host, sizeof (my_info->host), pHost, ""); - my_info->skt = my_info->sAsync.skt; - my_info->port = iPort; - my_info->chan = iChannel; - my_info->tmo = 100; - my_info->msg_id = 0; - my_info->pFunc = SerialNccrrrh; - my_info->pData = NULL; - strcpy(my_info->pTerms,"1\r\n\0"); - my_info->iForce = 0; - memset(my_info->pSendTerm,0,9); - strcpy(my_info->pSendTerm,"\r\n"); - - return 1; + my_info->sAsync.port = iPort; + strcpy(my_info->sAsync.host, pHost); + my_info->sAsync.chan = iChannel; + status = AsynSrv_Open(&(my_info->sAsync)); + if (status != 1) { + return OPENFAILURE; } + + /* intialize data structures */ + StrJoin(my_info->host, sizeof(my_info->host), pHost, ""); + my_info->skt = my_info->sAsync.skt; + my_info->port = iPort; + my_info->chan = iChannel; + my_info->tmo = 100; + my_info->msg_id = 0; + my_info->pFunc = SerialNccrrrh; + my_info->pData = NULL; + strcpy(my_info->pTerms, "1\r\n\0"); + my_info->iForce = 0; + memset(my_info->pSendTerm, 0, 9); + strcpy(my_info->pSendTerm, "\r\n"); + + return 1; +} + /*-----------------------------------------------------------------------*/ - int SerialForceOpen(void **pData, char *pHost, int iPort, int iChannel) - { - int status; - struct SerialInfo *my_info; - void *my_hndl; - struct hostent *rmt_hostent; - struct in_addr *rmt_inet_addr_pntr; - int rmt_sockname_len; - struct sockaddr_in lcl_sockname; - struct sockaddr_in rmt_sockname; - char msr_cmnd[20]; - struct RS__RplyStruct *rply_ptr; +int SerialForceOpen(void **pData, char *pHost, int iPort, int iChannel) +{ + int status; + struct SerialInfo *my_info; + void *my_hndl; + struct hostent *rmt_hostent; + struct in_addr *rmt_inet_addr_pntr; + int rmt_sockname_len; + struct sockaddr_in lcl_sockname; + struct sockaddr_in rmt_sockname; + char msr_cmnd[20]; + struct RS__RplyStruct *rply_ptr; + + *pData = NULL; + + /* create pData */ + *pData = malloc(sizeof(struct SerialInfo)); + if (*pData == NULL) { + return EL734__BAD_MALLOC; /* malloc failed!! */ + } + my_info = *pData; + memset(my_info, 0, sizeof(struct SerialInfo)); - *pData = NULL; - /* create pData */ - *pData = malloc (sizeof (struct SerialInfo)); - if (*pData == NULL) { - return EL734__BAD_MALLOC; /* malloc failed!! */ - } - my_info = *pData; - memset(my_info,0,sizeof(struct SerialInfo)); - - /* **-------------------------- Set up the connection */ - my_info->sAsync.port = iPort; - strcpy(my_info->sAsync.host,pHost); - my_info->sAsync.chan = iChannel; - status = AsynSrv_OpenNew (&(my_info->sAsync)); - if (status != 1) { - return OPENFAILURE; - } - - /* intialize data structures */ - StrJoin (my_info->host, sizeof (my_info->host), pHost, ""); - my_info->skt = my_info->sAsync.skt; - my_info->port = iPort; - my_info->chan = iChannel; - my_info->tmo = 100; - my_info->msg_id = 0; - my_info->pFunc = SerialNccrrrh; - my_info->pData = NULL; - strcpy(my_info->pTerms,"1\r\n\0"); - my_info->iForce = 1; - memset(my_info->pSendTerm,0,9); - strcpy(my_info->pSendTerm,"\r\n"); - - return 1; + my_info->sAsync.port = iPort; + strcpy(my_info->sAsync.host, pHost); + my_info->sAsync.chan = iChannel; + status = AsynSrv_OpenNew(&(my_info->sAsync)); + if (status != 1) { + return OPENFAILURE; } -/*--------------------------------------------------------------------------*/ - int SerialConfig(void **pData, int iTmo) - { - struct SerialInfo *my_info = NULL; - - my_info = (struct SerialInfo *)*pData; - assert(my_info); - - if(iTmo < 100) - { - my_info->tmo = 1; - return 1; - } - else - { - my_info->tmo = iTmo/100; /* convert to deci seconds */ - if(my_info->tmo > 9999)my_info->tmo = 9999; - } - return 1; - } -/*--------------------------------------------------------------------------*/ - int GetSerialTmo(void **pData) - { - struct SerialInfo *my_info = NULL; - int iTmo; - - my_info = (struct SerialInfo *)*pData; - assert(my_info); - - iTmo = my_info->tmo*100-99; /* convert back to milli seconds */ - return iTmo; - } - int SerialGetTmo(void **pData) - { - return GetSerialTmo(pData); + /* intialize data structures */ + StrJoin(my_info->host, sizeof(my_info->host), pHost, ""); + my_info->skt = my_info->sAsync.skt; + my_info->port = iPort; + my_info->chan = iChannel; + my_info->tmo = 100; + my_info->msg_id = 0; + my_info->pFunc = SerialNccrrrh; + my_info->pData = NULL; + strcpy(my_info->pTerms, "1\r\n\0"); + my_info->iForce = 1; + memset(my_info->pSendTerm, 0, 9); + strcpy(my_info->pSendTerm, "\r\n"); + + return 1; +} + +/*--------------------------------------------------------------------------*/ +int SerialConfig(void **pData, int iTmo) +{ + struct SerialInfo *my_info = NULL; + + my_info = (struct SerialInfo *) *pData; + assert(my_info); + + if (iTmo < 100) { + my_info->tmo = 1; + return 1; + } else { + my_info->tmo = iTmo / 100; /* convert to deci seconds */ + if (my_info->tmo > 9999) + my_info->tmo = 9999; } + return 1; +} + /*--------------------------------------------------------------------------*/ - int SerialGetSocket(void **pData) - { - struct SerialInfo *my_info = NULL; - int iTmo; - - my_info = (struct SerialInfo *)*pData; - assert(my_info); - - return my_info->skt; +int GetSerialTmo(void **pData) +{ + struct SerialInfo *my_info = NULL; + int iTmo; + + my_info = (struct SerialInfo *) *pData; + assert(my_info); + + iTmo = my_info->tmo * 100 - 99; /* convert back to milli seconds */ + + return iTmo; +} +int SerialGetTmo(void **pData) +{ + return GetSerialTmo(pData); +} - return 1; - } /*--------------------------------------------------------------------------*/ - int SerialClose(void **pData) - { +int SerialGetSocket(void **pData) +{ + struct SerialInfo *my_info = NULL; + int iTmo; - struct SerialInfo *info_ptr; - char buff[4]; + my_info = (struct SerialInfo *) *pData; + assert(my_info); - info_ptr = (struct SerialInfo *) *pData; - if (info_ptr == NULL) return True; + return my_info->skt; - if (info_ptr->skt != 0) { - AsynSrv_Close (&(info_ptr->sAsync),0); - info_ptr->skt = 0; - } - free (*pData); - *pData = NULL; + return 1; +} + +/*--------------------------------------------------------------------------*/ +int SerialClose(void **pData) +{ + + struct SerialInfo *info_ptr; + char buff[4]; + + info_ptr = (struct SerialInfo *) *pData; + if (info_ptr == NULL) return True; + + if (info_ptr->skt != 0) { + AsynSrv_Close(&(info_ptr->sAsync), 0); + info_ptr->skt = 0; } + free(*pData); + *pData = NULL; + return True; +} + /*--------------------------------------------------------------------------*/ - int SerialForceClose(void **pData) - { +int SerialForceClose(void **pData) +{ - struct SerialInfo *info_ptr; - char buff[4]; + struct SerialInfo *info_ptr; + char buff[4]; - info_ptr = (struct SerialInfo *) *pData; - if (info_ptr == NULL) return True; - - if (info_ptr->skt != 0) { - AsynSrv_Close (&(info_ptr->sAsync),1); - info_ptr->skt = 0; - } - free (*pData); - *pData = NULL; + info_ptr = (struct SerialInfo *) *pData; + if (info_ptr == NULL) return True; + + if (info_ptr->skt != 0) { + AsynSrv_Close(&(info_ptr->sAsync), 1); + info_ptr->skt = 0; } + free(*pData); + *pData = NULL; + return True; +} + /*--------------------------------------------------------------------------*/ - int SerialATerm(void **pData, char *pTerm) - { - struct SerialInfo *my_info = NULL; - - my_info = (struct SerialInfo *)*pData; - if(my_info == NULL) - { - printf("Serious Programming problem: data = NULL\n"); - return 0; - } - - /* only three characters in this field */ - if(strlen(pTerm) > 4) - { - return 0; - } - memset(my_info->pTerms,0,4); - strcpy(my_info->pTerms,pTerm); - - return 1; - } +int SerialATerm(void **pData, char *pTerm) +{ + struct SerialInfo *my_info = NULL; + + my_info = (struct SerialInfo *) *pData; + if (my_info == NULL) { + printf("Serious Programming problem: data = NULL\n"); + return 0; + } + + /* only three characters in this field */ + if (strlen(pTerm) > 4) { + return 0; + } + memset(my_info->pTerms, 0, 4); + strcpy(my_info->pTerms, pTerm); + + return 1; +} + /*--------------------------------------------------------------------------*/ - int SerialAGetTerm(void **pData, char *pTerm, int iTermLen) - { - struct SerialInfo *my_info = NULL; - - my_info = (struct SerialInfo *)*pData; - assert(my_info); - - strncpy(pTerm,my_info->pTerms,iTermLen); - - return 1; - } +int SerialAGetTerm(void **pData, char *pTerm, int iTermLen) +{ + struct SerialInfo *my_info = NULL; + + my_info = (struct SerialInfo *) *pData; + assert(my_info); + + strncpy(pTerm, my_info->pTerms, iTermLen); + + return 1; +} + /*-------------------------------------------------------------------------*/ - int SerialSendTerm(void **pData, char *pTerm) - { - struct SerialInfo *my_info = NULL; - - my_info = (struct SerialInfo *)*pData; - assert(my_info); - - /* only 0 characters in this field */ - if(strlen(pTerm) > 9) - { - return 0; - } - strcpy(my_info->pSendTerm,pTerm); - - return 1; - } - +int SerialSendTerm(void **pData, char *pTerm) +{ + struct SerialInfo *my_info = NULL; + + my_info = (struct SerialInfo *) *pData; + assert(my_info); + + /* only 0 characters in this field */ + if (strlen(pTerm) > 9) { + return 0; + } + strcpy(my_info->pSendTerm, pTerm); + + return 1; +} + /*---------------------------------------------------------------------------*/ - int SerialSend(void **pData, char *pCommand) - { - struct SerialInfo *info_ptr; - int status, c_len, size, max_size, ncmnds; - int bytes_to_come, bytes_left; - int iResult; - char *nxt_byte_ptr; - char err_text[80]; - char text[20]; - char *txt_ptr; - char *cmnd_lst_ptr; - char *pComCom = NULL; +int SerialSend(void **pData, char *pCommand) +{ + struct SerialInfo *info_ptr; + int status, c_len, size, max_size, ncmnds; + int bytes_to_come, bytes_left; + int iResult; + char *nxt_byte_ptr; + char err_text[80]; + char text[20]; + char *txt_ptr; + char *cmnd_lst_ptr; + char *pComCom = NULL; - /* - ** Do nothing if no connection - the connection gets - ** closed if an error is detected. - */ - info_ptr = (struct SerialInfo *) *pData; - if (info_ptr == NULL) return NOCONNECTION; - if (info_ptr->skt == 0) { - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return NOCONNECTION; - } - - info_ptr->msg_id++; /* Set up an incrementing message id */ - if (info_ptr->msg_id > 9999) info_ptr->msg_id = 1; - sprintf (info_ptr->to_host.msg_id, "%4.4d", info_ptr->msg_id); - - memcpy (info_ptr->to_host.c_pcol_lvl, RS__PROTOCOL_ID_V01B, - sizeof (info_ptr->to_host.c_pcol_lvl)); - sprintf (info_ptr->to_host.serial_port, "%4.4d", info_ptr->chan); - sprintf (info_ptr->to_host.tmo, "%04d", info_ptr->tmo); - - strncpy(info_ptr->sAsync.eot,info_ptr->pTerms,4); - memcpy (info_ptr->to_host.terms, info_ptr->pTerms, - sizeof (info_ptr->to_host.terms)); - memcpy (info_ptr->to_host.n_cmnds, "0000", - sizeof (info_ptr->to_host.n_cmnds)); - - - txt_ptr = pCommand; /* Get pntr to cmnd string */ - ncmnds = 0; - cmnd_lst_ptr = &info_ptr->to_host.cmnds[0]; - bytes_left = sizeof (info_ptr->to_host) - - OffsetOf (struct RS__MsgStruct, cmnds[0]); - - size = strlen (txt_ptr) + strlen(info_ptr->pSendTerm); - if (size > bytes_left) { - return EL734__BAD_SENDLEN; /* Too much to send */ - }else { - strcpy (cmnd_lst_ptr+4, txt_ptr); - /* make sure that the string is properly terminated */ - if((strstr(txt_ptr,info_ptr->pSendTerm) == 0) && - (strlen(txt_ptr) > 0) ) - { - strcpy(cmnd_lst_ptr+4+strlen(txt_ptr),info_ptr->pSendTerm); - c_len = strlen(txt_ptr) + strlen(info_ptr->pSendTerm); - } - else - { - c_len = strlen (txt_ptr); - } - sprintf (text, "%4.4d", c_len); - memcpy (cmnd_lst_ptr, text, 4); - cmnd_lst_ptr = cmnd_lst_ptr + c_len + 4; - ncmnds++; - bytes_left = bytes_left - size; - } - - sprintf (text, "%4.4d", ncmnds); - memcpy (info_ptr->to_host.n_cmnds, - text, sizeof (info_ptr->to_host.n_cmnds)); - - size = cmnd_lst_ptr - info_ptr->to_host.msg_id; - size = (size + 3) & (~3); /* Round up to multiple of 4 */ - sprintf (text, "%4.4d", size); - memcpy (info_ptr->to_host.msg_size, text, 4); - - status = send (info_ptr->skt, (char *) &info_ptr->to_host, size+4, 0); - if (status != (size+4)) { - if (status == 0) { - iResult = EL734__BAD_SEND; /* Server exited (probably) */ - }else if (status == -1) { - iResult = EL734__BAD_SEND_PIPE; /* Server exited (probably) */ - } - AsynSrv_Close (&(info_ptr->sAsync),1); - info_ptr->skt = 0; - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return iResult; - } - return 1; + /* + ** Do nothing if no connection - the connection gets + ** closed if an error is detected. + */ + info_ptr = (struct SerialInfo *) *pData; + if (info_ptr == NULL) + return NOCONNECTION; + if (info_ptr->skt == 0) { + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return NOCONNECTION; } -/*-------------------------------------------------------------------------*/ - int SerialReceive(void **pData, char *pBuffer, int iBufLen) - { - struct SerialInfo *info_ptr; - int status, c_len, size, max_size, ncmnds; - int bytes_to_come, bytes_left; - int iResult; - char *nxt_byte_ptr; - char err_text[80]; - char text[20]; - char *txt_ptr; - char *cmnd_lst_ptr; - struct RS__RplyStruct_V01B *ptr = NULL; - long lMask = 0L; - struct timeval tmo = {0,1}; - - /* - ** Do nothing if no connection - the connection gets - ** closed if an error is detected. - */ - info_ptr = (struct SerialInfo *) *pData; - if (info_ptr == NULL) return NOCONNECTION; - if (info_ptr->skt == 0) { - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return NOCONNECTION; + info_ptr->msg_id++; /* Set up an incrementing message id */ + if (info_ptr->msg_id > 9999) + info_ptr->msg_id = 1; + sprintf(info_ptr->to_host.msg_id, "%4.4d", info_ptr->msg_id); + + memcpy(info_ptr->to_host.c_pcol_lvl, RS__PROTOCOL_ID_V01B, + sizeof(info_ptr->to_host.c_pcol_lvl)); + sprintf(info_ptr->to_host.serial_port, "%4.4d", info_ptr->chan); + sprintf(info_ptr->to_host.tmo, "%04d", info_ptr->tmo); + + strncpy(info_ptr->sAsync.eot, info_ptr->pTerms, 4); + memcpy(info_ptr->to_host.terms, info_ptr->pTerms, + sizeof(info_ptr->to_host.terms)); + memcpy(info_ptr->to_host.n_cmnds, "0000", + sizeof(info_ptr->to_host.n_cmnds)); + + + txt_ptr = pCommand; /* Get pntr to cmnd string */ + ncmnds = 0; + cmnd_lst_ptr = &info_ptr->to_host.cmnds[0]; + bytes_left = sizeof(info_ptr->to_host) - + OffsetOf(struct RS__MsgStruct, cmnds[0]); + + size = strlen(txt_ptr) + strlen(info_ptr->pSendTerm); + if (size > bytes_left) { + return EL734__BAD_SENDLEN; /* Too much to send */ + } else { + strcpy(cmnd_lst_ptr + 4, txt_ptr); + /* make sure that the string is properly terminated */ + if ((strstr(txt_ptr, info_ptr->pSendTerm) == 0) && + (strlen(txt_ptr) > 0)) { + strcpy(cmnd_lst_ptr + 4 + strlen(txt_ptr), info_ptr->pSendTerm); + c_len = strlen(txt_ptr) + strlen(info_ptr->pSendTerm); + } else { + c_len = strlen(txt_ptr); } + sprintf(text, "%4.4d", c_len); + memcpy(cmnd_lst_ptr, text, 4); + cmnd_lst_ptr = cmnd_lst_ptr + c_len + 4; + ncmnds++; + bytes_left = bytes_left - size; + } - /* try with select if there is data */ + sprintf(text, "%4.4d", ncmnds); + memcpy(info_ptr->to_host.n_cmnds, + text, sizeof(info_ptr->to_host.n_cmnds)); + + size = cmnd_lst_ptr - info_ptr->to_host.msg_id; + size = (size + 3) & (~3); /* Round up to multiple of 4 */ + sprintf(text, "%4.4d", size); + memcpy(info_ptr->to_host.msg_size, text, 4); + + status = send(info_ptr->skt, (char *) &info_ptr->to_host, size + 4, 0); + if (status != (size + 4)) { + if (status == 0) { + iResult = EL734__BAD_SEND; /* Server exited (probably) */ + } else if (status == -1) { + iResult = EL734__BAD_SEND_PIPE; /* Server exited (probably) */ + } + AsynSrv_Close(&(info_ptr->sAsync), 1); + info_ptr->skt = 0; + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return iResult; + } + return 1; +} + +/*-------------------------------------------------------------------------*/ +int SerialReceive(void **pData, char *pBuffer, int iBufLen) +{ + struct SerialInfo *info_ptr; + int status, c_len, size, max_size, ncmnds; + int bytes_to_come, bytes_left; + int iResult; + char *nxt_byte_ptr; + char err_text[80]; + char text[20]; + char *txt_ptr; + char *cmnd_lst_ptr; + struct RS__RplyStruct_V01B *ptr = NULL; + long lMask = 0L; + struct timeval tmo = { 0, 1 }; + + + /* + ** Do nothing if no connection - the connection gets + ** closed if an error is detected. + */ + info_ptr = (struct SerialInfo *) *pData; + if (info_ptr == NULL) + return NOCONNECTION; + if (info_ptr->skt == 0) { + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return NOCONNECTION; + } + + /* try with select if there is data */ /* lMask = (1 << info_ptr->skt); tmo.tv_usec = 10; status = select((info_ptr->skt +1), (fd_set *)&lMask, NULL,NULL,&tmo); @@ -478,119 +485,116 @@ } */ - /* try read the message length to come */ - size = sizeof (info_ptr->from_host.msg_size); - status = recv (info_ptr->skt, info_ptr->from_host.msg_size, size, 0); - if (status != size) { - if(status > 0) - { - iResult = EL734__BAD_RECV; /* Server exited (probably) */ - } - else if (status == -1) { - iResult = EL734__BAD_RECV_NET; /* It's some other net problem */ - } - else - { - iResult = EL734__BAD_RECV_NET; - } - AsynSrv_Close (&(info_ptr->sAsync),1); - info_ptr->skt = 0; - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return iResult; + /* try read the message length to come */ + size = sizeof(info_ptr->from_host.msg_size); + status = recv(info_ptr->skt, info_ptr->from_host.msg_size, size, 0); + if (status != size) { + if (status > 0) { + iResult = EL734__BAD_RECV; /* Server exited (probably) */ + } else if (status == -1) { + iResult = EL734__BAD_RECV_NET; /* It's some other net problem */ + } else { + iResult = EL734__BAD_RECV_NET; } - if (sscanf (info_ptr->from_host.msg_size, "%4d", &bytes_to_come) != 1) { - return EL734__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ - } - - max_size = sizeof (info_ptr->from_host) - - sizeof (info_ptr->from_host.msg_size); - if (bytes_to_come > max_size) { - iResult = EL734__BAD_RECVLEN; - nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; - while (bytes_to_come > 0) { /* Flush out the incoming message */ - bytes_left = bytes_to_come; - if (bytes_left > max_size) bytes_left = max_size; - status = recv (info_ptr->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - iResult = EL734__BAD_FLUSH; /* TCP/IP problem whilst flushing */ - AsynSrv_Close (&(info_ptr->sAsync),1); - info_ptr->skt = 0; - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return iResult; - } - bytes_to_come = bytes_to_come - status; - } - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return iResult; - }else { - nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; - bytes_left = bytes_to_come; - while (bytes_left > 0) { /* Read the rest of the response */ - status = recv (info_ptr->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - if (status == 0) { - iResult = EL734__BAD_RECV1; /* Server exited (probably) */ - }else { - iResult = EL734__BAD_RECV1_NET; /* It's some other net fault */ - } - AsynSrv_Close (&(info_ptr->sAsync),1); - info_ptr->skt = 0; - return iResult; - } - bytes_left = bytes_left - status; - nxt_byte_ptr = nxt_byte_ptr + status; - } - } - /* well, we got data, make it available */ - if (sscanf (info_ptr->from_host.n_rply, "%4d", - &info_ptr->max_replies) != 1)info_ptr->max_replies = 0; - if (info_ptr->max_replies > 0) - ptr = (struct RS__RplyStruct_V01B *) info_ptr->from_host.u.rplys; - info_ptr->n_replies = 1; - if(ptr) - { - strncpy(pBuffer, ptr->rply,iBufLen); - } - else - { - return NOREPLY; - } - return True; + AsynSrv_Close(&(info_ptr->sAsync), 1); + info_ptr->skt = 0; + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return iResult; + } + if (sscanf(info_ptr->from_host.msg_size, "%4d", &bytes_to_come) != 1) { + return EL734__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ } -/*-------------------------------------------------------------------------*/ - int SerialReceiveWithTerm(void **pData, char *pBuffer, - int iBufLen, char *cTerm ) - { - struct SerialInfo *info_ptr; - int status, c_len, size, max_size, ncmnds; - int bytes_to_come, bytes_left; - int iResult; - char *nxt_byte_ptr; - char err_text[80]; - char text[20]; - char *txt_ptr; - char *cmnd_lst_ptr; - struct RS__RplyStruct_V01B *ptr = NULL; - long lMask = 0L; - struct timeval tmo = {0,1}; - - /* - ** Do nothing if no connection - the connection gets - ** closed if an error is detected. - */ - info_ptr = (struct SerialInfo *) *pData; - if (info_ptr == NULL) return NOCONNECTION; - if (info_ptr->skt == 0) { - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return NOCONNECTION; + max_size = sizeof(info_ptr->from_host) - + sizeof(info_ptr->from_host.msg_size); + if (bytes_to_come > max_size) { + iResult = EL734__BAD_RECVLEN; + nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; + while (bytes_to_come > 0) { /* Flush out the incoming message */ + bytes_left = bytes_to_come; + if (bytes_left > max_size) + bytes_left = max_size; + status = recv(info_ptr->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + iResult = EL734__BAD_FLUSH; /* TCP/IP problem whilst flushing */ + AsynSrv_Close(&(info_ptr->sAsync), 1); + info_ptr->skt = 0; + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return iResult; + } + bytes_to_come = bytes_to_come - status; } + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return iResult; + } else { + nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; + bytes_left = bytes_to_come; + while (bytes_left > 0) { /* Read the rest of the response */ + status = recv(info_ptr->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + if (status == 0) { + iResult = EL734__BAD_RECV1; /* Server exited (probably) */ + } else { + iResult = EL734__BAD_RECV1_NET; /* It's some other net fault */ + } + AsynSrv_Close(&(info_ptr->sAsync), 1); + info_ptr->skt = 0; + return iResult; + } + bytes_left = bytes_left - status; + nxt_byte_ptr = nxt_byte_ptr + status; + } + } + /* well, we got data, make it available */ + if (sscanf(info_ptr->from_host.n_rply, "%4d", + &info_ptr->max_replies) != 1) + info_ptr->max_replies = 0; + if (info_ptr->max_replies > 0) + ptr = (struct RS__RplyStruct_V01B *) info_ptr->from_host.u.rplys; + info_ptr->n_replies = 1; + if (ptr) { + strncpy(pBuffer, ptr->rply, iBufLen); + } else { + return NOREPLY; + } + return True; +} - /* try with select if there is data */ +/*-------------------------------------------------------------------------*/ +int SerialReceiveWithTerm(void **pData, char *pBuffer, + int iBufLen, char *cTerm) +{ + struct SerialInfo *info_ptr; + int status, c_len, size, max_size, ncmnds; + int bytes_to_come, bytes_left; + int iResult; + char *nxt_byte_ptr; + char err_text[80]; + char text[20]; + char *txt_ptr; + char *cmnd_lst_ptr; + struct RS__RplyStruct_V01B *ptr = NULL; + long lMask = 0L; + struct timeval tmo = { 0, 1 }; + + + /* + ** Do nothing if no connection - the connection gets + ** closed if an error is detected. + */ + info_ptr = (struct SerialInfo *) *pData; + if (info_ptr == NULL) + return NOCONNECTION; + if (info_ptr->skt == 0) { + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return NOCONNECTION; + } + + /* try with select if there is data */ /* lMask = (1 << info_ptr->skt); tmo.tv_usec = 10; status = select((info_ptr->skt +1), (fd_set *)&lMask, NULL,NULL,&tmo); @@ -600,316 +604,286 @@ } */ - /* try read the message length to come */ - size = sizeof (info_ptr->from_host.msg_size); - status = recv (info_ptr->skt, info_ptr->from_host.msg_size, size, 0); - if (status != size) { - if(status > 0) - { - iResult = EL734__BAD_RECV; /* Server exited (probably) */ - } - else if (status == -1) { - iResult = EL734__BAD_RECV_NET; /* It's some other net problem */ - } - else - { - iResult = EL734__BAD_RECV_NET; - } - AsynSrv_Close (&(info_ptr->sAsync),1); - info_ptr->skt = 0; - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return iResult; + /* try read the message length to come */ + size = sizeof(info_ptr->from_host.msg_size); + status = recv(info_ptr->skt, info_ptr->from_host.msg_size, size, 0); + if (status != size) { + if (status > 0) { + iResult = EL734__BAD_RECV; /* Server exited (probably) */ + } else if (status == -1) { + iResult = EL734__BAD_RECV_NET; /* It's some other net problem */ + } else { + iResult = EL734__BAD_RECV_NET; } - if (sscanf (info_ptr->from_host.msg_size, "%4d", &bytes_to_come) != 1) { - return EL734__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ - } - - max_size = sizeof (info_ptr->from_host) - - sizeof (info_ptr->from_host.msg_size); - if (bytes_to_come > max_size) { - iResult = EL734__BAD_RECVLEN; - nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; - while (bytes_to_come > 0) { /* Flush out the incoming message */ - bytes_left = bytes_to_come; - if (bytes_left > max_size) bytes_left = max_size; - status = recv (info_ptr->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - iResult = EL734__BAD_FLUSH; /* TCP/IP problem whilst flushing */ - AsynSrv_Close (&(info_ptr->sAsync),1); - info_ptr->skt = 0; - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return iResult; - } - bytes_to_come = bytes_to_come - status; - } - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return iResult; - }else { - nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; - bytes_left = bytes_to_come; - while (bytes_left > 0) { /* Read the rest of the response */ - status = recv (info_ptr->skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - if (status == 0) { - iResult = EL734__BAD_RECV1; /* Server exited (probably) */ - }else { - iResult = EL734__BAD_RECV1_NET; /* It's some other net fault */ - } - AsynSrv_Close (&(info_ptr->sAsync),1); - info_ptr->skt = 0; - return iResult; - } - bytes_left = bytes_left - status; - nxt_byte_ptr = nxt_byte_ptr + status; - } - } - /* well, we got data, make it available */ - if (sscanf (info_ptr->from_host.n_rply, "%4d", - &info_ptr->max_replies) != 1)info_ptr->max_replies = 0; - if (info_ptr->max_replies > 0) - ptr = (struct RS__RplyStruct_V01B *) info_ptr->from_host.u.rplys; - info_ptr->n_replies = 1; - if(ptr) - { - strncpy(pBuffer, ptr->rply,iBufLen); - *cTerm = ptr->term; - } - else - { - return NOREPLY; - } - return True; + AsynSrv_Close(&(info_ptr->sAsync), 1); + info_ptr->skt = 0; + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return iResult; } - + if (sscanf(info_ptr->from_host.msg_size, "%4d", &bytes_to_come) != 1) { + return EL734__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ + } + + max_size = sizeof(info_ptr->from_host) - + sizeof(info_ptr->from_host.msg_size); + if (bytes_to_come > max_size) { + iResult = EL734__BAD_RECVLEN; + nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; + while (bytes_to_come > 0) { /* Flush out the incoming message */ + bytes_left = bytes_to_come; + if (bytes_left > max_size) + bytes_left = max_size; + status = recv(info_ptr->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + iResult = EL734__BAD_FLUSH; /* TCP/IP problem whilst flushing */ + AsynSrv_Close(&(info_ptr->sAsync), 1); + info_ptr->skt = 0; + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return iResult; + } + bytes_to_come = bytes_to_come - status; + } + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return iResult; + } else { + nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; + bytes_left = bytes_to_come; + while (bytes_left > 0) { /* Read the rest of the response */ + status = recv(info_ptr->skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + if (status == 0) { + iResult = EL734__BAD_RECV1; /* Server exited (probably) */ + } else { + iResult = EL734__BAD_RECV1_NET; /* It's some other net fault */ + } + AsynSrv_Close(&(info_ptr->sAsync), 1); + info_ptr->skt = 0; + return iResult; + } + bytes_left = bytes_left - status; + nxt_byte_ptr = nxt_byte_ptr + status; + } + } + /* well, we got data, make it available */ + if (sscanf(info_ptr->from_host.n_rply, "%4d", + &info_ptr->max_replies) != 1) + info_ptr->max_replies = 0; + if (info_ptr->max_replies > 0) + ptr = (struct RS__RplyStruct_V01B *) info_ptr->from_host.u.rplys; + info_ptr->n_replies = 1; + if (ptr) { + strncpy(pBuffer, ptr->rply, iBufLen); + *cTerm = ptr->term; + } else { + return NOREPLY; + } + return True; +} + /*---------------------------------------------------------------------------*/ - int SerialError(int iErr, char *pBuffer, int iBufLen) - { - switch(iErr) - { - case -320: - strncpy(pBuffer,"Select failed to find data",iBufLen); - break; - case -300: - case NOCONNECTION: - strncpy(pBuffer,"Not connected",iBufLen); - break; - case -301: - strncpy(pBuffer,"No reply found", iBufLen); - break; - case -100: - strncpy(pBuffer,"No reply found", iBufLen); - break; - case EL734__BAD_ADR: - strncpy(pBuffer,"SERIAL__BAD_ADR",iBufLen); - break; - case EL734__BAD_BIND: - strncpy(pBuffer,"SERIAL__BAD_BIND",iBufLen); - break; - case EL734__BAD_CMD: - strncpy(pBuffer,"SERIAL__BAD_CMD",iBufLen); - break; - case EL734__BAD_CONNECT: - strncpy(pBuffer,"SERIAL__BAD_CONNECT",iBufLen); - break; - case EL734__BAD_FLUSH: - strncpy(pBuffer,"SERIAL__BAD_FLUSH",iBufLen); - break; - case EL734__BAD_HOST: - strncpy(pBuffer,"SERIAL__BAD_HOST",iBufLen); - break; - case EL734__BAD_ID: - strncpy(pBuffer,"SERIAL__BAD_ID",iBufLen); - break; - case EL734__BAD_ILLG: - strncpy(pBuffer,"SERIAL__BAD_ILLG",iBufLen); - break; - case EL734__BAD_LOC: - strncpy(pBuffer,"SERIAL__BAD_LOC",iBufLen); - break; - case EL734__BAD_MALLOC: - strncpy(pBuffer,"SERIAL__BAD_MALLOC",iBufLen); - break; - case EL734__BAD_NOT_BCD: - strncpy(pBuffer,"SERIAL__BAD_NOT_BCD",iBufLen); - break; - case EL734__BAD_OFL: - strncpy(pBuffer,"SERIAL__BAD_OFL",iBufLen); - break; - case EL734__BAD_PAR: - strncpy(pBuffer,"SERIAL__BAD_PAR",iBufLen); - break; - - case EL734__BAD_RECV: - strncpy(pBuffer,"SERIAL__BAD_RECV",iBufLen); - break; - case EL734__BAD_RECV_NET: - strncpy(pBuffer,"SERIAL__BAD_RECV_NET",iBufLen); - break; - case EL734__BAD_RECV_PIPE: - strncpy(pBuffer,"SERIAL__BAD_RECV_PIPE",iBufLen); - break; - case EL734__BAD_RECV_UNKN: - strncpy(pBuffer,"SERIAL__BAD_RECV_UNKN",iBufLen); - break; - case EL734__BAD_RECVLEN: - strncpy(pBuffer,"SERIAL__BAD_RECVLEN",iBufLen); - break; - case EL734__BAD_RECV1: - strncpy(pBuffer,"SERIAL__BAD_RECV1",iBufLen); - break; - case EL734__BAD_RECV1_NET: - strncpy(pBuffer,"SERIAL__BAD_RECV1_NET",iBufLen); - break; - case EL734__BAD_RECV1_PIPE: - strncpy(pBuffer,"SERIAL__BAD_RECV1_PIPE",iBufLen); - break; - case EL734__BAD_RNG: - strncpy(pBuffer,"SERIAL__BAD_RNG",iBufLen); - break; - case EL734__BAD_SEND: - strncpy(pBuffer,"SERIAL__BAD_SEND",iBufLen); - break; - case EL734__BAD_SEND_PIPE: - strncpy(pBuffer,"SERIAL__BAD_SEND_PIPE",iBufLen); - break; - case EL734__BAD_SEND_NET: - strncpy(pBuffer,"SERIAL__BAD_SEND_NET",iBufLen); - break; - case EL734__BAD_SEND_UNKN: - strncpy(pBuffer,"SERIAL__BAD_SEND_UNKN",iBufLen); - break; - case EL734__BAD_SENDLEN: - strncpy(pBuffer,"SERIAL__BAD_SENDLEN",iBufLen); - break; - case EL734__BAD_SOCKET: - strncpy(pBuffer,"SERIAL__BAD_SOCKET",iBufLen); - break; - case EL734__BAD_TMO: - strncpy(pBuffer,"SERIAL__BAD_TMO",iBufLen); - break; - case EL734__FORCED_CLOSED: - strncpy(pBuffer,"SERIAL__FORCED_CLOSED",iBufLen); - break; - case OPENFAILURE: - strncpy(pBuffer, - "FAILED to open connection to serial port server", iBufLen); - break; - default: - strcpy(pBuffer,"Unknown SERIAL error"); - break; - } - return 1; - } +int SerialError(int iErr, char *pBuffer, int iBufLen) +{ + switch (iErr) { + case -320: + strncpy(pBuffer, "Select failed to find data", iBufLen); + break; + case -300: + case NOCONNECTION: + strncpy(pBuffer, "Not connected", iBufLen); + break; + case -301: + strncpy(pBuffer, "No reply found", iBufLen); + break; + case -100: + strncpy(pBuffer, "No reply found", iBufLen); + break; + case EL734__BAD_ADR: + strncpy(pBuffer, "SERIAL__BAD_ADR", iBufLen); + break; + case EL734__BAD_BIND: + strncpy(pBuffer, "SERIAL__BAD_BIND", iBufLen); + break; + case EL734__BAD_CMD: + strncpy(pBuffer, "SERIAL__BAD_CMD", iBufLen); + break; + case EL734__BAD_CONNECT: + strncpy(pBuffer, "SERIAL__BAD_CONNECT", iBufLen); + break; + case EL734__BAD_FLUSH: + strncpy(pBuffer, "SERIAL__BAD_FLUSH", iBufLen); + break; + case EL734__BAD_HOST: + strncpy(pBuffer, "SERIAL__BAD_HOST", iBufLen); + break; + case EL734__BAD_ID: + strncpy(pBuffer, "SERIAL__BAD_ID", iBufLen); + break; + case EL734__BAD_ILLG: + strncpy(pBuffer, "SERIAL__BAD_ILLG", iBufLen); + break; + case EL734__BAD_LOC: + strncpy(pBuffer, "SERIAL__BAD_LOC", iBufLen); + break; + case EL734__BAD_MALLOC: + strncpy(pBuffer, "SERIAL__BAD_MALLOC", iBufLen); + break; + case EL734__BAD_NOT_BCD: + strncpy(pBuffer, "SERIAL__BAD_NOT_BCD", iBufLen); + break; + case EL734__BAD_OFL: + strncpy(pBuffer, "SERIAL__BAD_OFL", iBufLen); + break; + case EL734__BAD_PAR: + strncpy(pBuffer, "SERIAL__BAD_PAR", iBufLen); + break; + + case EL734__BAD_RECV: + strncpy(pBuffer, "SERIAL__BAD_RECV", iBufLen); + break; + case EL734__BAD_RECV_NET: + strncpy(pBuffer, "SERIAL__BAD_RECV_NET", iBufLen); + break; + case EL734__BAD_RECV_PIPE: + strncpy(pBuffer, "SERIAL__BAD_RECV_PIPE", iBufLen); + break; + case EL734__BAD_RECV_UNKN: + strncpy(pBuffer, "SERIAL__BAD_RECV_UNKN", iBufLen); + break; + case EL734__BAD_RECVLEN: + strncpy(pBuffer, "SERIAL__BAD_RECVLEN", iBufLen); + break; + case EL734__BAD_RECV1: + strncpy(pBuffer, "SERIAL__BAD_RECV1", iBufLen); + break; + case EL734__BAD_RECV1_NET: + strncpy(pBuffer, "SERIAL__BAD_RECV1_NET", iBufLen); + break; + case EL734__BAD_RECV1_PIPE: + strncpy(pBuffer, "SERIAL__BAD_RECV1_PIPE", iBufLen); + break; + case EL734__BAD_RNG: + strncpy(pBuffer, "SERIAL__BAD_RNG", iBufLen); + break; + case EL734__BAD_SEND: + strncpy(pBuffer, "SERIAL__BAD_SEND", iBufLen); + break; + case EL734__BAD_SEND_PIPE: + strncpy(pBuffer, "SERIAL__BAD_SEND_PIPE", iBufLen); + break; + case EL734__BAD_SEND_NET: + strncpy(pBuffer, "SERIAL__BAD_SEND_NET", iBufLen); + break; + case EL734__BAD_SEND_UNKN: + strncpy(pBuffer, "SERIAL__BAD_SEND_UNKN", iBufLen); + break; + case EL734__BAD_SENDLEN: + strncpy(pBuffer, "SERIAL__BAD_SENDLEN", iBufLen); + break; + case EL734__BAD_SOCKET: + strncpy(pBuffer, "SERIAL__BAD_SOCKET", iBufLen); + break; + case EL734__BAD_TMO: + strncpy(pBuffer, "SERIAL__BAD_TMO", iBufLen); + break; + case EL734__FORCED_CLOSED: + strncpy(pBuffer, "SERIAL__FORCED_CLOSED", iBufLen); + break; + case OPENFAILURE: + strncpy(pBuffer, + "FAILED to open connection to serial port server", iBufLen); + break; + default: + strcpy(pBuffer, "Unknown SERIAL error"); + break; + } + return 1; +} + /*---------------------------------------------------------------------------*/ - int SerialWriteRead(void **pData, char *pCommand, - char *pBuffer, int iBufLen) - { - - struct SerialInfo *pInfo = NULL; - int iRet; - time_t tTarget, tCurrent; - - pInfo = (struct SerialInfo *)*pData; - - /* write */ - iRet = SerialSend(pData,pCommand); - if(iRet != 1) - { - SerialError(iRet, pBuffer,iBufLen); - return iRet; - } - - /* check for answers for maximum time out */ - tTarget = tCurrent = time(&tCurrent); - tTarget += pInfo->tmo*100 - 90; - - while(tCurrent < tTarget) - { - pInfo->pFunc(pInfo->pData, 100); - iRet = SerialReceive(pData, pBuffer,iBufLen); - if( iRet != 1) - { - if(iRet != SELECTFAIL) - { - /* error ! */ - SerialError(iRet, pBuffer,iBufLen); - return iRet; - } - } - else - { - return 1; /* there is data read, we are done */ - } - tCurrent = time(&tCurrent); - } - return TIMEOUT; - } +int SerialWriteRead(void **pData, char *pCommand, + char *pBuffer, int iBufLen) +{ + + struct SerialInfo *pInfo = NULL; + int iRet; + time_t tTarget, tCurrent; + + pInfo = (struct SerialInfo *) *pData; + + /* write */ + iRet = SerialSend(pData, pCommand); + if (iRet != 1) { + SerialError(iRet, pBuffer, iBufLen); + return iRet; + } + + /* check for answers for maximum time out */ + tTarget = tCurrent = time(&tCurrent); + tTarget += pInfo->tmo * 100 - 90; + + while (tCurrent < tTarget) { + pInfo->pFunc(pInfo->pData, 100); + iRet = SerialReceive(pData, pBuffer, iBufLen); + if (iRet != 1) { + if (iRet != SELECTFAIL) { + /* error ! */ + SerialError(iRet, pBuffer, iBufLen); + return iRet; + } + } else { + return 1; /* there is data read, we are done */ + } + tCurrent = time(&tCurrent); + } + return TIMEOUT; +} + /*---------------------------------------------------------------------------*/ - int SerialNoReply(void **pData, char *pCommand) - { - - struct SerialInfo *pInfo = NULL; - int iRet, iOld, i; - char pBuffer[30]; - - pInfo = (struct SerialInfo *)*pData; - - iOld = pInfo->tmo; - pInfo->tmo = 0; - - /* write */ - iRet = SerialSend(pData,pCommand); - if(iRet != 1) - { - pInfo->tmo = iOld; - return iRet; - } - - /* try some time to find a TMO */ - for(i = 0 ; i < 10; i++) - { - usleep(50); - SerialReceive(pData, pBuffer,29); - if(strcmp(pBuffer,"?TMO") == 0) - { - break; - } - } - if(i > 7) - { - printf("TMO received after %d cycles \n",i); - } - pInfo->tmo = iOld; - return 1; - } +int SerialNoReply(void **pData, char *pCommand) +{ + + struct SerialInfo *pInfo = NULL; + int iRet, iOld, i; + char pBuffer[30]; + + pInfo = (struct SerialInfo *) *pData; + + iOld = pInfo->tmo; + pInfo->tmo = 0; + + /* write */ + iRet = SerialSend(pData, pCommand); + if (iRet != 1) { + pInfo->tmo = iOld; + return iRet; + } + + /* try some time to find a TMO */ + for (i = 0; i < 10; i++) { + usleep(50); + SerialReceive(pData, pBuffer, 29); + if (strcmp(pBuffer, "?TMO") == 0) { + break; + } + } + if (i > 7) { + printf("TMO received after %d cycles \n", i); + } + pInfo->tmo = iOld; + return 1; +} + /*-------------------------------------------------------------------------*/ - void SetSerialSleep(void **pData, SerialSleep pFun, void *pUserData) - { - struct SerialInfo *pInfo = NULL; - int iRet; - - pInfo = (struct SerialInfo *)*pData; - pInfo->pFunc = pFun; - pInfo->pData = pUserData; - - } - - - - - - - - - - - - - - - +void SetSerialSleep(void **pData, SerialSleep pFun, void *pUserData) +{ + struct SerialInfo *pInfo = NULL; + int iRet; + pInfo = (struct SerialInfo *) *pData; + pInfo->pFunc = pFun; + pInfo->pData = pUserData; +} diff --git a/hardsup/serialsinq.h b/hardsup/serialsinq.h index 60e5dc8..400b0f0 100644 --- a/hardsup/serialsinq.h +++ b/hardsup/serialsinq.h @@ -22,25 +22,25 @@ #line 30 "velodorn.w" - int SerialOpen(void **pData, char *pHost, int iPort, int iChannel); - int SerialForceOpen(void **pData, char *pHost, int iPort, int iChannel); - int SerialConfig(void **pData, int iTmo); - int SerialGetTmo(void **pData); - int SerialATerm(void **pData, char *pTerm); - int SerialAGetTerm(void **pData, char *pTerm, int iTermLen); - int SerialSendTerm(void **pData, char *pTerm); - int SerialGetSocket(void **pData); - int SerialClose(void **pData); - int SerialForceClose(void **pData); +int SerialOpen(void **pData, char *pHost, int iPort, int iChannel); +int SerialForceOpen(void **pData, char *pHost, int iPort, int iChannel); +int SerialConfig(void **pData, int iTmo); +int SerialGetTmo(void **pData); +int SerialATerm(void **pData, char *pTerm); +int SerialAGetTerm(void **pData, char *pTerm, int iTermLen); +int SerialSendTerm(void **pData, char *pTerm); +int SerialGetSocket(void **pData); +int SerialClose(void **pData); +int SerialForceClose(void **pData); - int SerialSend(void **pData, char *pCommand); - int SerialReceive(void **pData, char *pBuffer, int iBufLen); - int SerialReceiveWithTerm(void **pData, char *pBuffer, - int iBufLen,char *cTerm); - int SerialError(int iError, char *pError, int iErrLen); - int SerialWriteRead(void **pData, char *pCommand, - char *pBuffer, int iBufLen); - int SerialNoReply(void **pData, char *pCommand); +int SerialSend(void **pData, char *pCommand); +int SerialReceive(void **pData, char *pBuffer, int iBufLen); +int SerialReceiveWithTerm(void **pData, char *pBuffer, + int iBufLen, char *cTerm); +int SerialError(int iError, char *pError, int iErrLen); +int SerialWriteRead(void **pData, char *pCommand, + char *pBuffer, int iBufLen); +int SerialNoReply(void **pData, char *pCommand); #line 175 "velodorn.w" @@ -48,8 +48,8 @@ #line 116 "velodorn.w" - typedef int (*SerialSleep)(void *pData, int iTime); - void SetSerialSleep(void **pData, SerialSleep pFunc, void *pUserData); +typedef int (*SerialSleep) (void *pData, int iTime); +void SetSerialSleep(void **pData, SerialSleep pFunc, void *pUserData); #line 177 "velodorn.w" diff --git a/hardsup/sinq_prototypes.h b/hardsup/sinq_prototypes.h index 2be3165..372e369 100644 --- a/hardsup/sinq_prototypes.h +++ b/hardsup/sinq_prototypes.h @@ -19,656 +19,242 @@ #include #endif /* ---------------------------------------------------------------------*/ - int AsynSrv_ChanClose ( - struct AsynSrv__info *asyn_info); - int AsynSrv_Close ( - struct AsynSrv__info *asyn_info, - int force_flag); - int AsynSrv_Config ( - struct AsynSrv__info *asyn_info, - ...); - int AsynSrv_ConfigDflt ( - char *par_id, - ...); - void AsynSrv_ErrInfo ( - char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno); - int AsynSrv_Flush ( - struct AsynSrv__info *asyn_info); - int AsynSrv_GetLenTerm ( - struct AsynSrv__info *asyn_info, - struct RS__RespStruct *rcve_buff, - char *rply, - int *len, - char *term); - char *AsynSrv_GetReply ( - struct AsynSrv__info *asyn_info, - struct RS__RespStruct *rcve_buff, - char *last_rply); - int AsynSrv_Open ( - struct AsynSrv__info *asyn_info); - int AsynSrv_OpenNew ( - struct AsynSrv__info *asyn_info); - int AsynSrv_SendCmnds ( - struct AsynSrv__info *asyn_info, - struct RS__MsgStruct *send_buff, - struct RS__RespStruct *rcve_buff, - ...); - int AsynSrv_SendCmndsBig ( - struct AsynSrv__info *asyn_info, - struct RS__MsgStruct *send_buff, - int send_buff_size, - struct RS__RespStruct *rcve_buff, - int rcve_buff_size, - ...); - int AsynSrv_Trace ( - struct AsynSrv__info *asyn_info, - int state); - int AsynSrv_Trace_Write ( - struct AsynSrv__info *asyn_info); +int AsynSrv_ChanClose(struct AsynSrv__info *asyn_info); +int AsynSrv_Close(struct AsynSrv__info *asyn_info, int force_flag); +int AsynSrv_Config(struct AsynSrv__info *asyn_info, ...); +int AsynSrv_ConfigDflt(char *par_id, ...); +void AsynSrv_ErrInfo(char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno); +int AsynSrv_Flush(struct AsynSrv__info *asyn_info); +int AsynSrv_GetLenTerm(struct AsynSrv__info *asyn_info, + struct RS__RespStruct *rcve_buff, + char *rply, int *len, char *term); +char *AsynSrv_GetReply(struct AsynSrv__info *asyn_info, + struct RS__RespStruct *rcve_buff, char *last_rply); +int AsynSrv_Open(struct AsynSrv__info *asyn_info); +int AsynSrv_OpenNew(struct AsynSrv__info *asyn_info); +int AsynSrv_SendCmnds(struct AsynSrv__info *asyn_info, + struct RS__MsgStruct *send_buff, + struct RS__RespStruct *rcve_buff, ...); +int AsynSrv_SendCmndsBig(struct AsynSrv__info *asyn_info, + struct RS__MsgStruct *send_buff, + int send_buff_size, + struct RS__RespStruct *rcve_buff, + int rcve_buff_size, ...); +int AsynSrv_Trace(struct AsynSrv__info *asyn_info, int state); +int AsynSrv_Trace_Write(struct AsynSrv__info *asyn_info); /* ---------------------------------------------------------------------*/ - int C_log_arr_get ( - char *name, - int arr_size, - int *value, - int indx); - int C_log_flt_get ( - char *name, - float *value, - int indx); - int C_log_int_get ( - char *name, - long int *value, - int indx); - int C_log_str_get ( - char *name, - char *value, - int val_size, - int indx); +int C_log_arr_get(char *name, int arr_size, int *value, int indx); +int C_log_flt_get(char *name, float *value, int indx); +int C_log_int_get(char *name, long int *value, int indx); +int C_log_str_get(char *name, char *value, int val_size, int indx); /* ---------------------------------------------------------------------*/ - int C_str_edit ( - char *out, - char *in, - char *ctrl, - int *length); +int C_str_edit(char *out, char *in, char *ctrl, int *length); /* ---------------------------------------------------------------------*/ - int C_tt_port_config ( - int *hndl, - int mask); - int C_tt_port_connect ( - int *hndl, - int *chan, - char *lognam, - char *pwd); - int C_tt_port_disconnect ( - int *hndl); - int C_tt_port_io ( - int *hndl, - char *rqst, - char *term, - char *answ, - int *answ_len, - int flush, - int tmo); +int C_tt_port_config(int *hndl, int mask); +int C_tt_port_connect(int *hndl, int *chan, char *lognam, char *pwd); +int C_tt_port_disconnect(int *hndl); +int C_tt_port_io(int *hndl, + char *rqst, + char *term, + char *answ, int *answ_len, int flush, int tmo); /* ---------------------------------------------------------------------*/ - int EL734_Close ( - void **handle, - int force_flag); - int EL734_Config ( - void **handle, - ...); - char *EL734_EncodeMSR ( - char *text, - int text_len, - int msr, - int ored_msr, - int fp_cntr, - int fr_cntr); - char *EL734_EncodeSS ( - char *text, - int text_len, - int ss); - void EL734_ErrInfo ( - char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno); - int EL734_GetAirCush ( - void **handle, - int *present, - int *state); - int EL734_GetEncGearing ( - void **handle, - int *nominator, - int *denominator); - int EL734_GetId ( - void **handle, - char *id_txt, - int id_len); - int EL734_GetLimits ( - void **handle, - float *lo, - float *hi); - int EL734_GetMotorGearing ( - void **handle, - int *nominator, - int *denominator); - int EL734_GetNullPoint ( - void **handle, - int *null_pt); - int EL734_GetPosition ( - void **handle, - float *ist_posit); - int EL734_GetPrecision ( - void **handle, - int *n_dec); - int EL734_GetRefMode ( - void **handle, - int *mode); - int EL734_GetRefParam ( - void **handle, - float *param); - int EL734_GetSpeeds ( - void **handle, - int *lo, - int *hi, - int *ramp); - int EL734_GetStatus ( - void **handle, - int *msr, - int *ored_msr, - int *fp_cntr, - int *fr_cntr, - int *ss, - float *ist_posit); - int EL734_GetZeroPoint ( - void **handle, - float *zero_pt); - int EL734_MoveNoWait ( - void **handle, - float soll_posit); - int EL734_MoveWait ( - void **handle, - float soll_posit, - int *ored_msr, - int *fp_cntr, - int *fr_cntr, - float *ist_posit); - int EL734_Open ( - void **handle, - char *host, - int port, - int chan, - int motor, - char *id); - int EL734_PutOffline ( - void **handle); - int EL734_PutOnline ( - void **handle, - int echo); - int EL734_SendCmnd ( - void **handle, - char *cmnd, - char *rply, - int rply_size); - int EL734_SetAirCush ( - void **handle, - int state); - int EL734_SetErrcode ( - struct EL734info *info_ptr, - char *response, - char *cmnd); - int EL734_SetHighSpeed ( - void **handle, - int hi); - int EL734_SetLowSpeed ( - void **handle, - int lo); - int EL734_SetRamp ( - void **handle, - int ramp); - int EL734_Stop ( - void **handle); - int EL734_WaitIdle ( - void **handle, - int *ored_msr, - int *fp_cntr, - int *fr_cntr, - float *ist_posit); - void EL734_ZeroStatus ( - void **handle); +int EL734_Close(void **handle, int force_flag); +int EL734_Config(void **handle, ...); +char *EL734_EncodeMSR(char *text, + int text_len, + int msr, int ored_msr, int fp_cntr, int fr_cntr); +char *EL734_EncodeSS(char *text, int text_len, int ss); +void EL734_ErrInfo(char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno); +int EL734_GetAirCush(void **handle, int *present, int *state); +int EL734_GetEncGearing(void **handle, int *nominator, int *denominator); +int EL734_GetId(void **handle, char *id_txt, int id_len); +int EL734_GetLimits(void **handle, float *lo, float *hi); +int EL734_GetMotorGearing(void **handle, int *nominator, int *denominator); +int EL734_GetNullPoint(void **handle, int *null_pt); +int EL734_GetPosition(void **handle, float *ist_posit); +int EL734_GetPrecision(void **handle, int *n_dec); +int EL734_GetRefMode(void **handle, int *mode); +int EL734_GetRefParam(void **handle, float *param); +int EL734_GetSpeeds(void **handle, int *lo, int *hi, int *ramp); +int EL734_GetStatus(void **handle, + int *msr, + int *ored_msr, + int *fp_cntr, int *fr_cntr, int *ss, float *ist_posit); +int EL734_GetZeroPoint(void **handle, float *zero_pt); +int EL734_MoveNoWait(void **handle, float soll_posit); +int EL734_MoveWait(void **handle, + float soll_posit, + int *ored_msr, + int *fp_cntr, int *fr_cntr, float *ist_posit); +int EL734_Open(void **handle, + char *host, int port, int chan, int motor, char *id); +int EL734_PutOffline(void **handle); +int EL734_PutOnline(void **handle, int echo); +int EL734_SendCmnd(void **handle, char *cmnd, char *rply, int rply_size); +int EL734_SetAirCush(void **handle, int state); +int EL734_SetErrcode(struct EL734info *info_ptr, + char *response, char *cmnd); +int EL734_SetHighSpeed(void **handle, int hi); +int EL734_SetLowSpeed(void **handle, int lo); +int EL734_SetRamp(void **handle, int ramp); +int EL734_Stop(void **handle); +int EL734_WaitIdle(void **handle, + int *ored_msr, + int *fp_cntr, int *fr_cntr, float *ist_posit); +void EL734_ZeroStatus(void **handle); /* ---------------------------------------------------------------------*/ - int EL737_Close ( - void **handle, - int force_flag); - int EL737_Config ( - void **handle, - ...); - int EL737_Continue ( - void **handle, - int *status); - int EL737_EnableThresh ( - void **handle, - int indx); - void EL737_ErrInfo ( - char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno); - int EL737_GetMonIntegTime ( - void **handle, - int indx, - float *mon_integ_time); - int EL737_GetRateIntegTime ( - void **handle, - float *rate_integ_time); - void *EL737_GetReply ( - void **handle, - void *last_rply); - int EL737_GetStatus ( - void **handle, - int *c1, - int *c2, - int *c3, - int *c4, - float *timer, - int *rs); - int EL737_GetStatusExtra ( - void **handle, - int *c5, - int *c6, - int *c7, - int *c8); - int EL737_GetThresh ( - void **handle, - int *indx, - float *val); - int EL737_Open ( - void **handle, - char *host, - int port, - int chan); - int EL737_Pause ( - void **handle, - int *status); - int EL737_SendCmnd ( - void **handle, - char *cmnd, - char *rply, - int rply_size); - int EL737_SetErrcode ( - struct EL737info *info_ptr, - char *response, - char *cmnd); - int EL737_SetThresh ( - void **handle, - int indx, - float val); - int EL737_StartCnt ( - void **handle, - int preset_count, - int *status); - int EL737_StartTime ( - void **handle, - float preset_time, - int *status); - int EL737_Stop ( - void **handle, - int *c1, - int *c2, - int *c3, - int *c4, - float *timer, - int *status); - int EL737_StopFast ( - void **handle); - int EL737_WaitIdle ( - void **handle, - int *c1, - int *c2, - int *c3, - int *c4, - float *timer); +int EL737_Close(void **handle, int force_flag); +int EL737_Config(void **handle, ...); +int EL737_Continue(void **handle, int *status); +int EL737_EnableThresh(void **handle, int indx); +void EL737_ErrInfo(char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno); +int EL737_GetMonIntegTime(void **handle, int indx, float *mon_integ_time); +int EL737_GetRateIntegTime(void **handle, float *rate_integ_time); +void *EL737_GetReply(void **handle, void *last_rply); +int EL737_GetStatus(void **handle, + int *c1, + int *c2, int *c3, int *c4, float *timer, int *rs); +int EL737_GetStatusExtra(void **handle, + int *c5, int *c6, int *c7, int *c8); +int EL737_GetThresh(void **handle, int *indx, float *val); +int EL737_Open(void **handle, char *host, int port, int chan); +int EL737_Pause(void **handle, int *status); +int EL737_SendCmnd(void **handle, char *cmnd, char *rply, int rply_size); +int EL737_SetErrcode(struct EL737info *info_ptr, + char *response, char *cmnd); +int EL737_SetThresh(void **handle, int indx, float val); +int EL737_StartCnt(void **handle, int preset_count, int *status); +int EL737_StartTime(void **handle, float preset_time, int *status); +int EL737_Stop(void **handle, + int *c1, + int *c2, int *c3, int *c4, float *timer, int *status); +int EL737_StopFast(void **handle); +int EL737_WaitIdle(void **handle, + int *c1, int *c2, int *c3, int *c4, float *timer); /* ---------------------------------------------------------------------*/ - int EL755_Close ( - void **handle, - int force_flag); - int EL755_Config ( - void **handle, - ...); - void EL755_ErrInfo ( - char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno); - int EL755_ErrorLog ( - char *routine_name, - char *text); - int EL755_GetConstant ( - void **handle, - float *value); - int EL755_GetCurrents ( - void **handle, - float *soll, - float *ist); - int EL755_GetId ( - void **handle, - char *id_txt, - int id_len); - int EL755_GetLimit ( - void **handle, - float *value); - int EL755_GetRamp ( - void **handle, - float *value); - int EL755_GetTimeConstant ( - void **handle, - float *value); - int EL755_GetVoltageRange ( - void **handle, - float *value); - int EL755_Open ( - void **handle, - char *host, - int port, - int chan, - int indx); - int EL755_PutOffline ( - void **handle); - int EL755_PutOnline ( - void **handle, - int echo); - int EL755_SendTillSameStr ( - void **handle, - char *cmnd, - char *rply, - int rply_len); - int EL755_SendTillSameVal ( - void **handle, - char *cmnd, - float *val); - int EL755_SendTillTwoVals ( - void **handle, - char *cmnd, - float *val0, - float *val1); - int EL755_SetConstant ( - void **handle, - float value); - int EL755_SetCurrent ( - void **handle, - float soll); - int EL755_SetLimit ( - void **handle, - float value); - int EL755_SetRamp ( - void **handle, - float value); - int EL755_SetTimeConstant ( - void **handle, - float value); - int EL755_SetVoltageRange ( - void **handle, - float value); +int EL755_Close(void **handle, int force_flag); +int EL755_Config(void **handle, ...); +void EL755_ErrInfo(char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno); +int EL755_ErrorLog(char *routine_name, char *text); +int EL755_GetConstant(void **handle, float *value); +int EL755_GetCurrents(void **handle, float *soll, float *ist); +int EL755_GetId(void **handle, char *id_txt, int id_len); +int EL755_GetLimit(void **handle, float *value); +int EL755_GetRamp(void **handle, float *value); +int EL755_GetTimeConstant(void **handle, float *value); +int EL755_GetVoltageRange(void **handle, float *value); +int EL755_Open(void **handle, char *host, int port, int chan, int indx); +int EL755_PutOffline(void **handle); +int EL755_PutOnline(void **handle, int echo); +int EL755_SendTillSameStr(void **handle, + char *cmnd, char *rply, int rply_len); +int EL755_SendTillSameVal(void **handle, char *cmnd, float *val); +int EL755_SendTillTwoVals(void **handle, + char *cmnd, float *val0, float *val1); +int EL755_SetConstant(void **handle, float value); +int EL755_SetCurrent(void **handle, float soll); +int EL755_SetLimit(void **handle, float value); +int EL755_SetRamp(void **handle, float value); +int EL755_SetTimeConstant(void **handle, float value); +int EL755_SetVoltageRange(void **handle, float value); /* ---------------------------------------------------------------------*/ - int Fluke_Close ( - void **handle, - int force_flag); - int Fluke_Config ( - void **handle, - ...); - void Fluke_ErrInfo ( - char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno); - int Fluke_ErrorLog ( - char *routine_name, - char *text); - int Fluke_Open ( - void **handle, - char *host, - int port, - int chan); - int Fluke_Read ( - void **handle, - float *ist); - int Fluke_SendTillSame ( - void **handle, - char *cmnd, - char *rply, - int rply_len); - int Fluke_SendTillSameVal ( - void **handle, - char *cmnd, - float *val); +int Fluke_Close(void **handle, int force_flag); +int Fluke_Config(void **handle, ...); +void Fluke_ErrInfo(char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno); +int Fluke_ErrorLog(char *routine_name, char *text); +int Fluke_Open(void **handle, char *host, int port, int chan); +int Fluke_Read(void **handle, float *ist); +int Fluke_SendTillSame(void **handle, + char *cmnd, char *rply, int rply_len); +int Fluke_SendTillSameVal(void **handle, char *cmnd, float *val); /* ---------------------------------------------------------------------*/ - int ITC_Close ( - void **handle, - int force_flag); - int ITC_Config ( - void **handle, - ...); - int ITC_Dump_RAM ( - void **handle, - int buff_size, - char *buff, - int *dump_len, - int *n_diffs); - void ITC_ErrInfo ( - char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno); - int ITC_GetConfig ( - void **handle, - ...); - int ITC_Load_RAM ( - void **handle, - int load_len, - char *buff); - int ITC_Load_Table ( - void **handle, - char *buff); - int ITC_Open ( - void **handle, - char *host, - int port, - int chan); - int ITC_Read_ITC_Sensor ( - void **handle, - int sensor, - float factor, - float *value); - int ITC_Read_LTC11_Sensor ( - void **handle, - int sensor, - float *value); - int ITC_Read_LTC11_SetPt ( - void **handle, - float *value); - int ITC_ReadAuxTemp ( - void **handle, - float *value); - int ITC_ReadControlTemp ( - void **handle, - float *value); - int ITC_ReadHeaterOp ( - void **handle, - float *op_level, - float *op_percent); - int ITC_ReadId ( - void **handle, - char *id_txt, - int id_txt_len, - int *id_len); - int ITC_ReadPID ( - void **handle, - float *p, - float *i, - float *d); - int ITC_ReadSampleTemp ( - void **handle, - float *s_temp); - int ITC_ReadSetPoint ( - void **handle, - float *sp_temp); - int ITC_ReadStatus ( - void **handle, - char *status_txt, - int status_txt_len, - int *status_len, - int *auto_state, - int *remote_state); - int ITC_SendTillAckOk ( - void **handle, - struct RS__MsgStruct *to_host, - struct RS__RespStruct *from_host, - char *cmnd); - int ITC_SendTillSame ( - void **handle, - struct RS__MsgStruct *to_host, - struct RS__RespStruct *from_host, - char *cmnd, - char *rply, - int rply_len); - int ITC_SendTillSameLen ( - void **handle, - struct RS__MsgStruct *to_host, - struct RS__RespStruct *from_host, - char *cmnd, - char *rply, - int rply_len); - int ITC_SendTillSameLenAckOK ( - void **handle, - struct RS__MsgStruct *to_host, - struct RS__RespStruct *from_host, - char *cmnd, - char *rply, - int rply_len); - int ITC_SetControlTemp ( - void **handle, - float s_temp); - int ITC_SetHeatLevel ( - void **handle, - float heat_percent); - int ITC_ErrorLog ( - char *routine_name, - char *text); +int ITC_Close(void **handle, int force_flag); +int ITC_Config(void **handle, ...); +int ITC_Dump_RAM(void **handle, + int buff_size, char *buff, int *dump_len, int *n_diffs); +void ITC_ErrInfo(char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno); +int ITC_GetConfig(void **handle, ...); +int ITC_Load_RAM(void **handle, int load_len, char *buff); +int ITC_Load_Table(void **handle, char *buff); +int ITC_Open(void **handle, char *host, int port, int chan); +int ITC_Read_ITC_Sensor(void **handle, + int sensor, float factor, float *value); +int ITC_Read_LTC11_Sensor(void **handle, int sensor, float *value); +int ITC_Read_LTC11_SetPt(void **handle, float *value); +int ITC_ReadAuxTemp(void **handle, float *value); +int ITC_ReadControlTemp(void **handle, float *value); +int ITC_ReadHeaterOp(void **handle, float *op_level, float *op_percent); +int ITC_ReadId(void **handle, char *id_txt, int id_txt_len, int *id_len); +int ITC_ReadPID(void **handle, float *p, float *i, float *d); +int ITC_ReadSampleTemp(void **handle, float *s_temp); +int ITC_ReadSetPoint(void **handle, float *sp_temp); +int ITC_ReadStatus(void **handle, + char *status_txt, + int status_txt_len, + int *status_len, int *auto_state, int *remote_state); +int ITC_SendTillAckOk(void **handle, + struct RS__MsgStruct *to_host, + struct RS__RespStruct *from_host, char *cmnd); +int ITC_SendTillSame(void **handle, + struct RS__MsgStruct *to_host, + struct RS__RespStruct *from_host, + char *cmnd, char *rply, int rply_len); +int ITC_SendTillSameLen(void **handle, + struct RS__MsgStruct *to_host, + struct RS__RespStruct *from_host, + char *cmnd, char *rply, int rply_len); +int ITC_SendTillSameLenAckOK(void **handle, + struct RS__MsgStruct *to_host, + struct RS__RespStruct *from_host, + char *cmnd, char *rply, int rply_len); +int ITC_SetControlTemp(void **handle, float s_temp); +int ITC_SetHeatLevel(void **handle, float heat_percent); +int ITC_ErrorLog(char *routine_name, char *text); /* ---------------------------------------------------------------------*/ - int SPS_Close ( - void **handle, - int force_flag); - int SPS_Config ( - void **handle, - ...); - void SPS_ErrInfo ( - char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno); - int SPS_ErrorLog ( - char *routine_name, - char *text); - int SPS_GetStatus ( - void **handle, - unsigned char *status_vals, - int n_status_vals, - int *adc_vals, - int n_adc_vals); - int SPS_Open ( - void **handle, - char *host, - int port, - int chan); - int SPS_SendTillSame ( - void **handle, - struct RS__MsgStruct *to_host, - struct RS__RespStruct *from_host, - char *cmnd, - char *rply, - int rply_len); - int SPS_SendTillSameLen ( - void **handle, - struct RS__MsgStruct *to_host, - struct RS__RespStruct *from_host, - char *cmnd, - char *rply, - int rply_len); +int SPS_Close(void **handle, int force_flag); +int SPS_Config(void **handle, ...); +void SPS_ErrInfo(char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno); +int SPS_ErrorLog(char *routine_name, char *text); +int SPS_GetStatus(void **handle, + unsigned char *status_vals, + int n_status_vals, int *adc_vals, int n_adc_vals); +int SPS_Open(void **handle, char *host, int port, int chan); +int SPS_SendTillSame(void **handle, + struct RS__MsgStruct *to_host, + struct RS__RespStruct *from_host, + char *cmnd, char *rply, int rply_len); +int SPS_SendTillSameLen(void **handle, + struct RS__MsgStruct *to_host, + struct RS__RespStruct *from_host, + char *cmnd, char *rply, int rply_len); /* ---------------------------------------------------------------------*/ - int VelSel_Close ( - void **handle, - int force_flag); - void VelSel_Config ( - void **handle, - int msec_tmo, - char *eot_str); - void VelSel_ErrInfo ( - char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno); - void *VelSel_GetReply ( - void **handle, - void *last_rply); - int VelSel_GetStatus ( - void **handle, - char *status_str, - int status_str_len); - int VelSel_Open ( - void **handle, - char *host, - int port, - int chan); - int VelSel_SendCmnd ( - void **handle, - char *cmnd, - char *rply, - int rply_size); +int VelSel_Close(void **handle, int force_flag); +void VelSel_Config(void **handle, int msec_tmo, char *eot_str); +void VelSel_ErrInfo(char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno); +void *VelSel_GetReply(void **handle, void *last_rply); +int VelSel_GetStatus(void **handle, char *status_str, int status_str_len); +int VelSel_Open(void **handle, char *host, int port, int chan); +int VelSel_SendCmnd(void **handle, char *cmnd, char *rply, int rply_size); /* ---------------------------------------------------------------------*/ - void FailInet ( - char *text); - void GetErrno ( - int *his_errno, - int *his_vaxc_errno); - int MakeCharPrintable ( - char *out, - int out_size, - char in); - char *MakePrint ( - char *text); - char *MakePrintable ( - char *out, - int out_size, - char *in); - void *Map_to_ACS (); - char *StrEdit ( - char *out, - char *in, - char *ctrl, - int *ln); - char *StrJoin ( - char *result, - int result_size, - char *str_a, - char *str_b); - int StrMatch ( - char *str_a, - char *str_b, - int min_len); - int Get_TASMAD_Info ( - char *file_name, - int *nItems, - ...); - int Get_TASMAD_Info_Filename ( - char *file_name, - char *buf, - int *bufSize); - int Update_TASMAD_Info ( - char *file_name, - int *nItems, - ...); +void FailInet(char *text); +void GetErrno(int *his_errno, int *his_vaxc_errno); +int MakeCharPrintable(char *out, int out_size, char in); +char *MakePrint(char *text); +char *MakePrintable(char *out, int out_size, char *in); +void *Map_to_ACS(); +char *StrEdit(char *out, char *in, char *ctrl, int *ln); +char *StrJoin(char *result, int result_size, char *str_a, char *str_b); +int StrMatch(char *str_a, char *str_b, int min_len); +int Get_TASMAD_Info(char *file_name, int *nItems, ...); +int Get_TASMAD_Info_Filename(char *file_name, char *buf, int *bufSize); +int Update_TASMAD_Info(char *file_name, int *nItems, ...); /*--------------------------------------------- End of SINQ_PROTOTYPES.H --*/ -#endif /* _sinq_prototypes_loaded_ */ +#endif /* _sinq_prototypes_loaded_ */ diff --git a/hardsup/sinqhm.c b/hardsup/sinqhm.c index 3cea5af..3a06b84 100644 --- a/hardsup/sinqhm.c +++ b/hardsup/sinqhm.c @@ -70,1714 +70,1537 @@ extern int close(int fp); /*-----------------------------------------------------------------------*/ - static int SendDAQCommand(pSINQHM self, int iCommand, int *iDaq) - { - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - int status, iRet; +static int SendDAQCommand(pSINQHM self, int iCommand, int *iDaq) +{ + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + int status, iRet; - assert(self); - assert(self->iClientSocket); + assert(self); + assert(self->iClientSocket); - memset(&Req_buff,0,sizeof(struct req_buff_struct)); - memset(&Rply_buff,0,sizeof(struct rply_buff_struct)); - - /* prepare a message */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_DAQ); - Req_buff.u.daq.sub_cmnd = htonl (iCommand); + memset(&Req_buff, 0, sizeof(struct req_buff_struct)); + memset(&Rply_buff, 0, sizeof(struct rply_buff_struct)); - /* send the message */ - status = send (self->iClientSocket, (char *) &Req_buff, sizeof (Req_buff), 0); - if (status == -1) - { - return SEND_ERROR; - } - if (status != sizeof (Req_buff)) - { - return SEND_ERROR; - } + /* prepare a message */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_DAQ); + Req_buff.u.daq.sub_cmnd = htonl(iCommand); - /* get a response */ - status = recv (self->iClientSocket, (char *) &Rply_buff, - sizeof (Rply_buff), MSG_WAITALL); - - /* check various error conditions */ - if (status == -1) - { - return RECEIVE_ERROR; - } - if (status != sizeof (Rply_buff)) - { - return INSUFFICIENT_DATA; - } - if(ntohl (Rply_buff.bigend) != 0x12345678) - { - return BYTE_ORDER_CHAOS; - } - if((iRet = ntohl (Rply_buff.status)) != KER__SUCCESS) - { - return SOFTWARE_ERROR; - } - - *iDaq = ntohs (Rply_buff.u.daq.daq_now); - - /* success */ - return 1; + /* send the message */ + status = + send(self->iClientSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; } + if (status != sizeof(Req_buff)) { + return SEND_ERROR; + } + + /* get a response */ + status = recv(self->iClientSocket, (char *) &Rply_buff, + sizeof(Rply_buff), MSG_WAITALL); + + /* check various error conditions */ + if (status == -1) { + return RECEIVE_ERROR; + } + if (status != sizeof(Rply_buff)) { + return INSUFFICIENT_DATA; + } + if (ntohl(Rply_buff.bigend) != 0x12345678) { + return BYTE_ORDER_CHAOS; + } + if ((iRet = ntohl(Rply_buff.status)) != KER__SUCCESS) { + return SOFTWARE_ERROR; + } + + *iDaq = ntohs(Rply_buff.u.daq.daq_now); + + /* success */ + return 1; +} + /*-----------------------------------------------------------------------*/ - static int SendDAQStatus(pSINQHM self, - struct rply_buff_struct *pReply) - { - struct req_buff_struct Req_buff; - int status, iRet; +static int SendDAQStatus(pSINQHM self, struct rply_buff_struct *pReply) +{ + struct req_buff_struct Req_buff; + int status, iRet; - assert(self); + assert(self); - if(!self->iClientSocket) - { - return 0; - } - - /* prepare a message */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_STATUS); - - /* send the message */ - status = send (self->iClientSocket, (char *) &Req_buff, sizeof (Req_buff), 0); - if (status == -1) - { - return SEND_ERROR; - } - if (status != sizeof (Req_buff)) - { - return SEND_ERROR; - } - - /* get a response */ - status = recv (self->iClientSocket, (char *) pReply, - sizeof (struct rply_buff_struct), MSG_WAITALL); - - /* check various error conditions */ - if (status == -1) - { - return RECEIVE_ERROR; - } - if (status != sizeof (struct rply_buff_struct)) - { - return INSUFFICIENT_DATA; - } - if(ntohl (pReply->bigend) != 0x12345678) - { - return BYTE_ORDER_CHAOS; - } - if((iRet = ntohl (pReply->status)) != KER__SUCCESS) - { - return SOFTWARE_ERROR; - } - - /* success */ - return 1; + if (!self->iClientSocket) { + return 0; } + /* prepare a message */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_STATUS); + + /* send the message */ + status = + send(self->iClientSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + if (status != sizeof(Req_buff)) { + return SEND_ERROR; + } + + /* get a response */ + status = recv(self->iClientSocket, (char *) pReply, + sizeof(struct rply_buff_struct), MSG_WAITALL); + + /* check various error conditions */ + if (status == -1) { + return RECEIVE_ERROR; + } + if (status != sizeof(struct rply_buff_struct)) { + return INSUFFICIENT_DATA; + } + if (ntohl(pReply->bigend) != 0x12345678) { + return BYTE_ORDER_CHAOS; + } + if ((iRet = ntohl(pReply->status)) != KER__SUCCESS) { + return SOFTWARE_ERROR; + } + + /* success */ + return 1; +} + /*-------------------------------------------------------------------------*/ - pSINQHM CreateSINQHM(char *pHMComputer, int iMasterPort) - { - pSINQHM pNew = NULL; - - /* new memory */ - pNew = (pSINQHM)malloc(sizeof(SINQHM)); - if(!pNew) - { - return NULL; - } - memset(pNew,0,sizeof(SINQHM)); - - pNew->pHMComputer = strdup(pHMComputer); - pNew->iMasterPort = iMasterPort; - - return pNew; - } +pSINQHM CreateSINQHM(char *pHMComputer, int iMasterPort) +{ + pSINQHM pNew = NULL; + + /* new memory */ + pNew = (pSINQHM) malloc(sizeof(SINQHM)); + if (!pNew) { + return NULL; + } + memset(pNew, 0, sizeof(SINQHM)); + + pNew->pHMComputer = strdup(pHMComputer); + pNew->iMasterPort = iMasterPort; + + return pNew; +} + /*-----------------------------------------------------------------------*/ - pSINQHM CopySINQHM(pSINQHM self) - { - pSINQHM pNew = NULL; +pSINQHM CopySINQHM(pSINQHM self) +{ + pSINQHM pNew = NULL; - assert(self); - - pNew = CreateSINQHM(self->pHMComputer,self->iMasterPort); - if(!pNew) - { - return NULL; - } - pNew->iBinWidth = self->iBinWidth; - pNew->iPacket = self->iPacket; - pNew->iRank = self->iRank; - pNew->iLength = self->iLength; - return pNew; - } -/*-------------------------------------------------------------------------*/ - void SINQHMSetPar(pSINQHM self, int iRank, int iLength, int iBin) - { - assert(self); - - self->iRank = iRank; - self->iLength = iLength; - self->iBinWidth = iBin; - } -/*-------------------------------------------------------------------------*/ - void DeleteSINQHM(pSINQHM self) - { - int i; + assert(self); - assert(self); - - if(self->pHMComputer) - { - free(self->pHMComputer); - } - - for(i = 0; i < self->iBanks; i++) - { - if(self->pBank[i].iEdges) - { - free(self->pBank[i].iEdges); - } - } - - /* make sure a possible clients connection gets murdered */ - if(self->iClientSocket) - { - SINQHMCloseDAQ(self); - } - free(self); + pNew = CreateSINQHM(self->pHMComputer, self->iMasterPort); + if (!pNew) { + return NULL; } -/*------------------------------------------------------------------------*/ - int SINQHMConfigure(pSINQHM self, int iMode, int iRank, int iLength, - int iBinWidth, int iLowBin, int iCompress) - { - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - - assert(self); - - /* branch specially for TOF flight mode */ - if( (iMode >= SQHM__TOF) && (iMode < SQHM__HM_PSD) ) - { - self->iBinWidth = iBinWidth; - return SINQHMTimeBin(self,iMode); - } - - /* fill in the request data structure */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_CONFIG); - Req_buff.u.cnfg.mode = htonl (iMode); - Req_buff.u.cnfg.u.hm_dig.n_hists = htonl (iRank); - printf("%d\n", ntohl(Req_buff.u.cnfg.u.hm_dig.n_hists)); - Req_buff.u.cnfg.u.hm_dig.lo_bin = htonl (iLowBin); - Req_buff.u.cnfg.u.hm_dig.num_bins = htonl (iLength); - Req_buff.u.cnfg.u.hm_dig.bytes_per_bin = htonl (iBinWidth); - Req_buff.u.cnfg.u.hm_dig.compress = htonl (iCompress); - - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - return status; - } - - /* send request */ - status = send(self->iMasterSocket,(char *)&Req_buff, sizeof(Req_buff),0); - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - if(iRet < 0) - { - /* try close the socket */ - close(self->iMasterSocket); - self->iMasterSocket = 0; - return iRet; - } - else - { - /* configure successful, keep the data */ - self->iBinWidth = iBinWidth; - self->iLength = iLength; - self->iRank = iRank; - - /* close the socket */ - status = close(self->iMasterSocket); - self->iMasterSocket = 0; - if((status != 0) && (errno != ECONNRESET)) - { - return CLOSE_ERROR; - } - } - return 1; /* success, finally */ - } -/*------------------------------------------------------------------------*/ - int SINQHMConfigurePSD(pSINQHM self, int iMode, - int xSize, int xOff, int xFac, - int ySize, int yOff, int yFac, - int iBinWidth, - float *iEdges, int iEdgeLength) - { - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - int iLength, i, iDelay; - unsigned int iExtra; - char *pBuffer = NULL, *pPtr; - struct tof_edge_arr tea; - int iTeaLength; - struct tof_bank toba; - - assert(self); + pNew->iBinWidth = self->iBinWidth; + pNew->iPacket = self->iPacket; + pNew->iRank = self->iRank; + pNew->iLength = self->iLength; + return pNew; +} - /* set up detector bank information. This code supports only - one detector bank as of now. Which is appropriate for the - detector at hand. - */ - self->iBinWidth = iBinWidth; - SINQHMDefineBank(self,0,0,xSize*ySize, - iEdges,iEdgeLength); - - /* figure out how long we are going to be*/ - iLength = 36 + self->iBanks*sizeof(struct tof_bank); - for(i = 0; i < self->iBanks; i++) - { - iLength += 8 + self->pBank[i].iEdgeLength*sizeof(SQint32); - } - if(iLength < 64) - iLength = 64; - /* allocate send buffer */ - pBuffer = (char *)malloc(iLength*sizeof(char)); - if(!pBuffer) - { - return HIST_BAD_ALLOC; - } - memset(pBuffer,0,iLength); - - /* do the message header */ - iExtra = iLength - sizeof(Req_buff); - if(iExtra < 0) - iExtra = 0; - iDelay = self->pBank[0].iEdges[0]; - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_CONFIG); - Req_buff.u.cnfg.mode = htonl (iMode); - Req_buff.u.cnfg.u.psd.n_extra_bytes = htonl (iExtra); - Req_buff.u.cnfg.u.psd.n_edges = htons (1); - Req_buff.u.cnfg.u.psd.n_banks = htons (1); - Req_buff.u.cnfg.u.psd.xOffset = htons (xOff); - Req_buff.u.cnfg.u.psd.yOffset = htons (yOff); - Req_buff.u.cnfg.u.psd.xFactor = htons (xFac); - Req_buff.u.cnfg.u.psd.yFactor = htons (yFac); - Req_buff.u.cnfg.u.psd.xSize = htons (xSize); - Req_buff.u.cnfg.u.psd.ySize = htons (ySize); - Req_buff.u.cnfg.u.psd.preset_delay = htonl((int)iEdges[0]); - memcpy(pBuffer,&Req_buff,36); - pPtr = pBuffer + 36; +/*-------------------------------------------------------------------------*/ +void SINQHMSetPar(pSINQHM self, int iRank, int iLength, int iBin) +{ + assert(self); + + self->iRank = iRank; + self->iLength = iLength; + self->iBinWidth = iBin; +} + +/*-------------------------------------------------------------------------*/ +void DeleteSINQHM(pSINQHM self) +{ + int i; + + assert(self); + + if (self->pHMComputer) { + free(self->pHMComputer); + } + + for (i = 0; i < self->iBanks; i++) { + if (self->pBank[i].iEdges) { + free(self->pBank[i].iEdges); + } + } + + /* make sure a possible clients connection gets murdered */ + if (self->iClientSocket) { + SINQHMCloseDAQ(self); + } + free(self); +} - /* do the edge thingies */ - for(i = 0; i < self->iBanks; i++) - { - tea.n_bins = htonl(self->pBank[i].iEdgeLength-1); - if(self->pBank[i].iEdgeLength == 2) - { - tea.flag = htonl(0); - } - else - { - tea.flag = htonl(1); - } - tea.edges = self->pBank[i].iEdges; - memcpy(pPtr,&tea,8); - pPtr += 8; - iTeaLength = self->pBank[i].iEdgeLength*4; - memcpy(pPtr,self->pBank[i].iEdges,iTeaLength); - pPtr += iTeaLength; - } - - /* do the swiss bank structures */ - for(i = 0; i < self->iBanks; i++) - { - toba.first = htons(self->pBank[i].iStart); - toba.n_cntrs = htons(self->pBank[i].iEnd); - toba.edge_indx = htons(i); - toba.bytes_per_bin = htons(self->iBinWidth); - memcpy(pPtr,&toba,sizeof(struct tof_bank)); - pPtr += sizeof(struct tof_bank); - } - - /* all packed up neat and nicely, send it */ - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - if(pBuffer) - free(pBuffer); - return status; - } - - /* send request */ - status = send(self->iMasterSocket,pBuffer,iLength ,0); - if(pBuffer) - { - free(pBuffer); - } - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - if(iRet < 0) - { - /* try close the socket */ - close(self->iMasterSocket); - self->iMasterSocket = 0; - return iRet; - } - else - { - /* close the socket */ - status = close(self->iMasterSocket); - self->iMasterSocket = 0; - if((status != 0) && (errno != ECONNRESET)) - { - return CLOSE_ERROR; - } - } - return 1; /* success, finally */ - } /*------------------------------------------------------------------------*/ - int SINQHMDeconfigure(pSINQHM self, int iHarsh) - { - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - - assert(self); - if( (iHarsh != 0) && (iHarsh != 1) ) - { - return INVALID_HARSH; - } - memset(&Req_buff,0,sizeof(struct req_buff_struct)); - memset(&Rply_buff,0,sizeof(struct rply_buff_struct)); - - /* fill in the request data structure */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_DECONFIG); - Req_buff.u.decnfg.sub_code = htonl(iHarsh); - - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - return status; - } - - /* send request */ - status = send(self->iMasterSocket,(char *)&Req_buff, sizeof(Req_buff),0); - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - if(iRet < 0) - { - close(self->iMasterSocket); - self->iMasterSocket = 0; - return iRet; - } - else - { - status = close(self->iMasterSocket); - self->iMasterSocket = 0; - if((status != 0) && (errno != ECONNRESET)) - { - return CLOSE_ERROR; - } - } - return 1; /* success, finally */ - } +int SINQHMConfigure(pSINQHM self, int iMode, int iRank, int iLength, + int iBinWidth, int iLowBin, int iCompress) +{ + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + + assert(self); + + /* branch specially for TOF flight mode */ + if ((iMode >= SQHM__TOF) && (iMode < SQHM__HM_PSD)) { + self->iBinWidth = iBinWidth; + return SINQHMTimeBin(self, iMode); + } + + /* fill in the request data structure */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_CONFIG); + Req_buff.u.cnfg.mode = htonl(iMode); + Req_buff.u.cnfg.u.hm_dig.n_hists = htonl(iRank); + printf("%d\n", ntohl(Req_buff.u.cnfg.u.hm_dig.n_hists)); + Req_buff.u.cnfg.u.hm_dig.lo_bin = htonl(iLowBin); + Req_buff.u.cnfg.u.hm_dig.num_bins = htonl(iLength); + Req_buff.u.cnfg.u.hm_dig.bytes_per_bin = htonl(iBinWidth); + Req_buff.u.cnfg.u.hm_dig.compress = htonl(iCompress); + + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + return status; + } + + /* send request */ + status = + send(self->iMasterSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + if (iRet < 0) { + /* try close the socket */ + close(self->iMasterSocket); + self->iMasterSocket = 0; + return iRet; + } else { + /* configure successful, keep the data */ + self->iBinWidth = iBinWidth; + self->iLength = iLength; + self->iRank = iRank; + + /* close the socket */ + status = close(self->iMasterSocket); + self->iMasterSocket = 0; + if ((status != 0) && (errno != ECONNRESET)) { + return CLOSE_ERROR; + } + } + return 1; /* success, finally */ +} + /*------------------------------------------------------------------------*/ - int SINQHMGetStatus(pSINQHM self, int *iMode,int *iDaq, - int *iRank, int *iBinWidth, - int *iLength, int *iClients) - { - int status, iRet; - short sDaq, sFill; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - - assert(self); - - - status = 0; - if(self->iClientSocket) - { - status = SendDAQStatus(self,&Rply_buff); - } - else - { - /* fill in the request data structure */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_STATUS); - - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - return status; - } - - /* send request */ - status = send(self->iMasterSocket,(char *)&Req_buff, sizeof(Req_buff),0); - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - if(iRet < 0) - { - close(self->iMasterSocket); - self->iMasterSocket = 0; - return iRet; - } - else - { - status = 1; - /* close the socket and go */ - iRet = close(self->iMasterSocket); - self->iMasterSocket = 0; - if((iRet != 0) && (errno != ECONNRESET)) - { - return CLOSE_ERROR; - } - } - } - - if(status) - { - /* transfer results */ - *iMode = ntohl(Rply_buff.u.status.cfg_state); - if((sDaq = ntohs(Rply_buff.u.status.daq_now)) == 0) /* DAQ active */ - { - *iDaq = 1; - } - else - { - sFill = ntohs(Rply_buff.u.status.filler_mask); - if(sFill & sDaq) - { - /* filler is not running */ - *iDaq = 0; - } - else - { - /* inhibited by some mean client */ - *iDaq = 2; - } - } - *iRank = ntohs(Rply_buff.u.status.n_hists); - *iLength = ntohl(Rply_buff.u.status.num_bins); - *iBinWidth = Rply_buff.u.status.bytes_per_bin; - *iClients = Rply_buff.u.status.act_srvrs; - } - return 1; /* success, finally */ - } +int SINQHMConfigurePSD(pSINQHM self, int iMode, + int xSize, int xOff, int xFac, + int ySize, int yOff, int yFac, + int iBinWidth, float *iEdges, int iEdgeLength) +{ + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + int iLength, i, iDelay; + unsigned int iExtra; + char *pBuffer = NULL, *pPtr; + struct tof_edge_arr tea; + int iTeaLength; + struct tof_bank toba; + + assert(self); + + /* set up detector bank information. This code supports only + one detector bank as of now. Which is appropriate for the + detector at hand. + */ + self->iBinWidth = iBinWidth; + SINQHMDefineBank(self, 0, 0, xSize * ySize, iEdges, iEdgeLength); + + /* figure out how long we are going to be */ + iLength = 36 + self->iBanks * sizeof(struct tof_bank); + for (i = 0; i < self->iBanks; i++) { + iLength += 8 + self->pBank[i].iEdgeLength * sizeof(SQint32); + } + if (iLength < 64) + iLength = 64; + /* allocate send buffer */ + pBuffer = (char *) malloc(iLength * sizeof(char)); + if (!pBuffer) { + return HIST_BAD_ALLOC; + } + memset(pBuffer, 0, iLength); + + /* do the message header */ + iExtra = iLength - sizeof(Req_buff); + if (iExtra < 0) + iExtra = 0; + iDelay = self->pBank[0].iEdges[0]; + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_CONFIG); + Req_buff.u.cnfg.mode = htonl(iMode); + Req_buff.u.cnfg.u.psd.n_extra_bytes = htonl(iExtra); + Req_buff.u.cnfg.u.psd.n_edges = htons(1); + Req_buff.u.cnfg.u.psd.n_banks = htons(1); + Req_buff.u.cnfg.u.psd.xOffset = htons(xOff); + Req_buff.u.cnfg.u.psd.yOffset = htons(yOff); + Req_buff.u.cnfg.u.psd.xFactor = htons(xFac); + Req_buff.u.cnfg.u.psd.yFactor = htons(yFac); + Req_buff.u.cnfg.u.psd.xSize = htons(xSize); + Req_buff.u.cnfg.u.psd.ySize = htons(ySize); + Req_buff.u.cnfg.u.psd.preset_delay = htonl((int) iEdges[0]); + memcpy(pBuffer, &Req_buff, 36); + pPtr = pBuffer + 36; + + /* do the edge thingies */ + for (i = 0; i < self->iBanks; i++) { + tea.n_bins = htonl(self->pBank[i].iEdgeLength - 1); + if (self->pBank[i].iEdgeLength == 2) { + tea.flag = htonl(0); + } else { + tea.flag = htonl(1); + } + tea.edges = self->pBank[i].iEdges; + memcpy(pPtr, &tea, 8); + pPtr += 8; + iTeaLength = self->pBank[i].iEdgeLength * 4; + memcpy(pPtr, self->pBank[i].iEdges, iTeaLength); + pPtr += iTeaLength; + } + + /* do the swiss bank structures */ + for (i = 0; i < self->iBanks; i++) { + toba.first = htons(self->pBank[i].iStart); + toba.n_cntrs = htons(self->pBank[i].iEnd); + toba.edge_indx = htons(i); + toba.bytes_per_bin = htons(self->iBinWidth); + memcpy(pPtr, &toba, sizeof(struct tof_bank)); + pPtr += sizeof(struct tof_bank); + } + + /* all packed up neat and nicely, send it */ + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + if (pBuffer) + free(pBuffer); + return status; + } + + /* send request */ + status = send(self->iMasterSocket, pBuffer, iLength, 0); + if (pBuffer) { + free(pBuffer); + } + if (status == -1) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + if (iRet < 0) { + /* try close the socket */ + close(self->iMasterSocket); + self->iMasterSocket = 0; + return iRet; + } else { + /* close the socket */ + status = close(self->iMasterSocket); + self->iMasterSocket = 0; + if ((status != 0) && (errno != ECONNRESET)) { + return CLOSE_ERROR; + } + } + return 1; /* success, finally */ +} + /*------------------------------------------------------------------------*/ - int SINQHMDebug(pSINQHM self, int iLevel) - { - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - - assert(self); - - /* fill in the request data structure */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_DBG); - Req_buff.u.dbg.mask = htonl(iLevel); - - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - return status; - } - - /* send request */ - status = send(self->iMasterSocket,(char *)&Req_buff, sizeof(Req_buff),0); - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - if(iRet < 0) - { - close(self->iMasterSocket); - self->iMasterSocket = 0; - return iRet; - } - else - { - status = close(self->iMasterSocket); - self->iMasterSocket = 0; - if((status != 0) && (errno != ECONNRESET)) - { - return CLOSE_ERROR; - } - } - return 1; /* success, finally */ - } +int SINQHMDeconfigure(pSINQHM self, int iHarsh) +{ + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + + assert(self); + if ((iHarsh != 0) && (iHarsh != 1)) { + return INVALID_HARSH; + } + memset(&Req_buff, 0, sizeof(struct req_buff_struct)); + memset(&Rply_buff, 0, sizeof(struct rply_buff_struct)); + + /* fill in the request data structure */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_DECONFIG); + Req_buff.u.decnfg.sub_code = htonl(iHarsh); + + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + return status; + } + + /* send request */ + status = + send(self->iMasterSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + if (iRet < 0) { + close(self->iMasterSocket); + self->iMasterSocket = 0; + return iRet; + } else { + status = close(self->iMasterSocket); + self->iMasterSocket = 0; + if ((status != 0) && (errno != ECONNRESET)) { + return CLOSE_ERROR; + } + } + return 1; /* success, finally */ +} + /*------------------------------------------------------------------------*/ - int SINQHMKill(pSINQHM self) - { - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - - assert(self); - - /* fill in the request data structure */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_EXIT); - - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - return status; - } - - /* send request */ - status = send(self->iMasterSocket,(char *)&Req_buff, sizeof(Req_buff),0); - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - if(iRet < 0) - { - close(self->iMasterSocket); - self->iMasterSocket = 0; - return iRet; - } - else - { - status = close(self->iMasterSocket); - self->iMasterSocket = 0; - if((status != 0) && (errno != ECONNRESET)) - { - return CLOSE_ERROR; - } - } - return 1; /* success, finally */ - } +int SINQHMGetStatus(pSINQHM self, int *iMode, int *iDaq, + int *iRank, int *iBinWidth, + int *iLength, int *iClients) +{ + int status, iRet; + short sDaq, sFill; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + + assert(self); + + + status = 0; + if (self->iClientSocket) { + status = SendDAQStatus(self, &Rply_buff); + } else { + /* fill in the request data structure */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_STATUS); + + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + return status; + } + + /* send request */ + status = + send(self->iMasterSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + if (iRet < 0) { + close(self->iMasterSocket); + self->iMasterSocket = 0; + return iRet; + } else { + status = 1; + /* close the socket and go */ + iRet = close(self->iMasterSocket); + self->iMasterSocket = 0; + if ((iRet != 0) && (errno != ECONNRESET)) { + return CLOSE_ERROR; + } + } + } + + if (status) { + /* transfer results */ + *iMode = ntohl(Rply_buff.u.status.cfg_state); + if ((sDaq = ntohs(Rply_buff.u.status.daq_now)) == 0) { /* DAQ active */ + *iDaq = 1; + } else { + sFill = ntohs(Rply_buff.u.status.filler_mask); + if (sFill & sDaq) { + /* filler is not running */ + *iDaq = 0; + } else { + /* inhibited by some mean client */ + *iDaq = 2; + } + } + *iRank = ntohs(Rply_buff.u.status.n_hists); + *iLength = ntohl(Rply_buff.u.status.num_bins); + *iBinWidth = Rply_buff.u.status.bytes_per_bin; + *iClients = Rply_buff.u.status.act_srvrs; + } + return 1; /* success, finally */ +} + +/*------------------------------------------------------------------------*/ +int SINQHMDebug(pSINQHM self, int iLevel) +{ + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + + assert(self); + + /* fill in the request data structure */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_DBG); + Req_buff.u.dbg.mask = htonl(iLevel); + + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + return status; + } + + /* send request */ + status = + send(self->iMasterSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + if (iRet < 0) { + close(self->iMasterSocket); + self->iMasterSocket = 0; + return iRet; + } else { + status = close(self->iMasterSocket); + self->iMasterSocket = 0; + if ((status != 0) && (errno != ECONNRESET)) { + return CLOSE_ERROR; + } + } + return 1; /* success, finally */ +} + +/*------------------------------------------------------------------------*/ +int SINQHMKill(pSINQHM self) +{ + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + + assert(self); + + /* fill in the request data structure */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_EXIT); + + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + return status; + } + + /* send request */ + status = + send(self->iMasterSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + if (iRet < 0) { + close(self->iMasterSocket); + self->iMasterSocket = 0; + return iRet; + } else { + status = close(self->iMasterSocket); + self->iMasterSocket = 0; + if ((status != 0) && (errno != ECONNRESET)) { + return CLOSE_ERROR; + } + } + return 1; /* success, finally */ +} /*====================== DAQ functions ==================================*/ - int SINQHMOpenDAQ(pSINQHM self) - { - int status, iRet, iPacket; - struct sockaddr_in lcl_sockname; - struct sockaddr_in rmt_sockname; - struct hostent *rmt_hostent; - struct in_addr *rmt_inet_addr_pntr; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - - assert(self); - - memset(&Req_buff,0,sizeof(struct req_buff_struct)); - memset(&Rply_buff,0,sizeof(struct rply_buff_struct)); - - /* fill in the request data structure */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_CNCT); - Req_buff.u.cnct.max_pkt = htonl (8192); - Req_buff.u.cnct.strt_mode = htonl (0); - - - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - return status; - } - - /* send request */ - status = send(self->iMasterSocket,(char *)&Req_buff, sizeof(Req_buff),0); - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - status = close(self->iMasterSocket); - self->iMasterSocket = 0; - if(iRet < 0) - { - return iRet; - } - if(status != 0) - { - return CLOSE_ERROR; - } - - /* read the port and packet size to use */ - self->iClientPort = ntohl (Rply_buff.u.cnct.port); - iPacket = ntohl (Rply_buff.u.cnct.pkt_size); - self->iPacket = iPacket; +int SINQHMOpenDAQ(pSINQHM self) +{ + int status, iRet, iPacket; + struct sockaddr_in lcl_sockname; + struct sockaddr_in rmt_sockname; + struct hostent *rmt_hostent; + struct in_addr *rmt_inet_addr_pntr; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; - /* now we are ready to open the connection to our very own histogram - memory slave server - */ - - /* first a socket */ - self->iClientSocket = socket (AF_INET, SOCK_STREAM, 0); - if(self->iClientSocket == -1) - { - return SOCKET_ERROR; - } + assert(self); - /* now try a bind */ - lcl_sockname.sin_family = AF_INET; - lcl_sockname.sin_port = htons (0); - lcl_sockname.sin_addr.s_addr = 0; - status = bind (self->iClientSocket, (struct sockaddr *) &lcl_sockname, - sizeof (lcl_sockname)); - if (status == -1) - { - self->iClientSocket = 0; - return BIND_ERROR; - } + memset(&Req_buff, 0, sizeof(struct req_buff_struct)); + memset(&Rply_buff, 0, sizeof(struct rply_buff_struct)); - /* get hostname (again). This is double work (has happened in + /* fill in the request data structure */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_CNCT); + Req_buff.u.cnct.max_pkt = htonl(8192); + Req_buff.u.cnct.strt_mode = htonl(0); + + + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + return status; + } + + /* send request */ + status = + send(self->iMasterSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + status = close(self->iMasterSocket); + self->iMasterSocket = 0; + if (iRet < 0) { + return iRet; + } + if (status != 0) { + return CLOSE_ERROR; + } + + /* read the port and packet size to use */ + self->iClientPort = ntohl(Rply_buff.u.cnct.port); + iPacket = ntohl(Rply_buff.u.cnct.pkt_size); + self->iPacket = iPacket; + + /* now we are ready to open the connection to our very own histogram + memory slave server + */ + + /* first a socket */ + self->iClientSocket = socket(AF_INET, SOCK_STREAM, 0); + if (self->iClientSocket == -1) { + return SOCKET_ERROR; + } + + /* now try a bind */ + lcl_sockname.sin_family = AF_INET; + lcl_sockname.sin_port = htons(0); + lcl_sockname.sin_addr.s_addr = 0; + status = bind(self->iClientSocket, (struct sockaddr *) &lcl_sockname, + sizeof(lcl_sockname)); + if (status == -1) { + self->iClientSocket = 0; + return BIND_ERROR; + } + + /* get hostname (again). This is double work (has happened in OpenMasterConnection before) but I decided for this in order to avoid carrying that extra adress pointer needed for connect around. + */ + rmt_hostent = gethostbyname(self->pHMComputer); + if (rmt_hostent == NULL) { + /* this should never happen, as we got it recently in + OpenMasterConnection */ - rmt_hostent = gethostbyname (self->pHMComputer); - if (rmt_hostent == NULL) { - /* this should never happen, as we got it recently in - OpenMasterConnection - */ - return HMCOMPUTER_NOT_FOUND; - } - rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; - - /* and connect */ - rmt_sockname.sin_family = AF_INET; - rmt_sockname.sin_port = htons (self->iClientPort); - rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; - status = connect (self->iClientSocket, (struct sockaddr *) &rmt_sockname, - sizeof (rmt_sockname)); - if (status == -1) - { - self->iClientSocket = 0; - return CONNECT_ERROR; - } - - /* done! Surprise! Everything worked! */ - return 1; + return HMCOMPUTER_NOT_FOUND; } + rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; + + /* and connect */ + rmt_sockname.sin_family = AF_INET; + rmt_sockname.sin_port = htons(self->iClientPort); + rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; + status = connect(self->iClientSocket, (struct sockaddr *) &rmt_sockname, + sizeof(rmt_sockname)); + if (status == -1) { + self->iClientSocket = 0; + return CONNECT_ERROR; + } + + /* done! Surprise! Everything worked! */ + return 1; +} + /*------------------------------------------------------------------------*/ - int SINQHMCloseDAQ(pSINQHM self) - { - struct req_buff_struct Req_buff; - int status, iRet; +int SINQHMCloseDAQ(pSINQHM self) +{ + struct req_buff_struct Req_buff; + int status, iRet; - assert(self); - if(self->iClientSocket <= 0) - { - /* already colsed */ - return 1; - } - - iRet = 1; - memset(&Req_buff,0,sizeof(struct req_buff_struct)); - - - /* send close message, this helps the master to clean up */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_CLOSE); - status = send (self->iClientSocket, (char *) &Req_buff, sizeof (Req_buff), 0); - if (status == -1) - { - iRet = SEND_ERROR; - } - if (status != sizeof (Req_buff)) - { - iRet = SEND_ERROR; - } - status = close (self->iClientSocket); - if (status != 0) - { - iRet = CLOSE_ERROR; - } - self->iClientSocket = 0; - self->iClientPort = 0; - return iRet; - } -/*-----------------------------------------------------------------------*/ - int SINQHMStartDAQ(pSINQHM self) - { - int status, iDaq; - - assert(self); - - status = SendDAQCommand(self,DAQ__GO,&iDaq); - if(status < 0) /* error */ - { - return status; - } - if(iDaq != 0) - { - return DAQ_INHIBIT; - } - return 1; - } -/*-----------------------------------------------------------------------*/ - int SINQHMStopDAQ(pSINQHM self) - { - int status, iDaq; - - assert(self); - - status = SendDAQCommand(self,DAQ__STOP,&iDaq); - if(status < 0) /* error */ - { - return status; - } - if(iDaq == 0) - { - return DAQ_NOTSTOPPED; - } - return 1; - } - -/*-----------------------------------------------------------------------*/ - int SINQHMContinueDAQ(pSINQHM self) - { - int status, iDaq; - - assert(self); - - status = SendDAQCommand(self,DAQ__CLR,&iDaq); - if(status < 0) /* error */ - { - return status; - } - if(iDaq != 0) - { - return DAQ_INHIBIT; - } - return 1; - } -/*-----------------------------------------------------------------------*/ - int SINQHMInhibitDAQ(pSINQHM self) - { - int status, iDaq; - - assert(self); - - status = SendDAQCommand(self,DAQ__INH,&iDaq); - if(status < 0) /* error */ - { - return status; - } - if(iDaq == 0) - { - return DAQ_NOTSTOPPED; - } - return 1; - - } -/*-----------------------------------------------------------------------*/ - int SINQHMWrite(pSINQHM self, int iNum, int iStart, int iEnd, void *pData) - { - long lBytes2Go,lBins, i; - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - SQint16 *p16; - SQint32 *p32; - char *pPtr; - - assert(self); - - /* calculate number of bins */ - lBins = iEnd; - - /* take care of byte order first */ - if (0x12345678 != ntohl (0x12345678)) - { - /* Swap bytes, if necessary */ - switch (self->iBinWidth) - { - case 1: - break; - case 2: - p16 = (SQint16 *) pData; - for (i=0; i < lBins; i++) - { - p16[i] = htons (p16[i]); - } - break; - case 4: - p32 = (SQint32 *) pData; - for (i=0; i < lBins; i++) - { - p32[i] = htonl (p32[i]); - } - break; - } - } - - /* initialize the Request data */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_WRITE); - Req_buff.u.write.n_bins = htonl (lBins); - Req_buff.u.write.first_bin = htonl (iStart); - Req_buff.u.write.bytes_per_bin = htonl (self->iBinWidth); - Req_buff.u.write.hist_no = htonl (iNum); - - /* send the message */ - status = send (self->iClientSocket, (char *) &Req_buff, sizeof (Req_buff), 0); - if (status == -1) - { - return SEND_ERROR; - } - if (status != sizeof (Req_buff)) - { - return SEND_ERROR; - } - - /* send data */ - lBytes2Go = lBins * self->iBinWidth; - pPtr = (char *)pData; - while (lBytes2Go > 0) - { - i = (lBytes2Go > self->iPacket) ? self->iPacket : lBytes2Go; - status = send (self->iClientSocket, (char *) pPtr, i, 0); - if (status <= 0) - { - return SEND_ERROR; - } - lBytes2Go -= status; - pPtr += status; - } - - /* get status */ - status = recv (self->iClientSocket, (char *) &Rply_buff, - sizeof (Rply_buff), MSG_WAITALL); - - /* check various error conditions */ - if (status == -1) - { - return RECEIVE_ERROR; - } - if (status != sizeof (Rply_buff)) - { - return INSUFFICIENT_DATA; - } - if(ntohl (Rply_buff.bigend) != 0x12345678) - { - return BYTE_ORDER_CHAOS; - } - if((iRet = ntohl (Rply_buff.status)) == KER__BAD_VALUE) - { - return HIST_BAD_VALUE; - } - if(iRet != KER__SUCCESS) - { - return HIST_BAD_CODE; - } - /* success */ - return 1; - } -/*-----------------------------------------------------------------------*/ - int SINQHMRead(pSINQHM self, int iNum, int iStart, int iEnd, - void *pData, int iDataLen) - { - long lBins2Get, lSpace,iNoBins, i; - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - SQint16 *p16; - SQint32 *p32; - char *pPtr; - char pBuffer[8192]; - - assert(self); - - /* initialize the Request data */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_READ); - Req_buff.u.read.n_bins = htonl (iEnd-iStart); - Req_buff.u.read.first_bin = htonl (iStart); - Req_buff.u.read.hist_no = htonl (iNum); - - /* send the message */ - status = send (self->iClientSocket, (char *) &Req_buff, - sizeof (Req_buff), 0); - if (status == -1) - { - return SEND_ERROR; - } - if (status != sizeof (Req_buff)) - { - return SEND_ERROR; - } - - /* wait for an answer */ - status = recv (self->iClientSocket, (char *) &Rply_buff, - sizeof (Rply_buff), MSG_WAITALL); - - /* check various error conditions */ - if (status == -1) - { - return RECEIVE_ERROR; - } - if (status != sizeof (Rply_buff)) - { - return INSUFFICIENT_DATA; - } - if(ntohl (Rply_buff.bigend) != 0x12345678) - { - return BYTE_ORDER_CHAOS; - } - iRet = ntohl(Rply_buff.status); - if(iRet != KER__SUCCESS) - { - return HIST_BAD_CODE; - } - - /* calculate the size of things to come */ - lBins2Get = ntohl(Rply_buff.u.read.n_bins) * ntohl(Rply_buff.u.read.bytes_per_bin); - - /* read data */ - pPtr = (char *)pData; - lSpace = iDataLen; - iNoBins = ntohl(Rply_buff.u.read.n_bins); - while (lBins2Get > 0) - { - if(lBins2Get > self->iPacket) - { - i = self->iPacket; - } - else - { - i = lBins2Get; - } - status = recv (self->iClientSocket, pBuffer, - i, 0); - if (status == -1) - { - return SEND_ERROR; - } - lBins2Get -= status; - if((lSpace - status) > 0) - { - memcpy(pPtr,pBuffer,status); - lSpace -= status; - pPtr += status; - } - else - { - if(lSpace > 0) - { - memcpy(pPtr,pBuffer,lSpace); - lSpace = 0; - } - } - } - - /* swap bytes if necessary */ - if ((self->iBinWidth > 0) && (Rply_buff.bigend != 0x12345678)) - { - switch (self->iBinWidth) - { /* Byte swapping is necessary */ - case 2: - /* Not sure how to do this - this might be wrong! */ - p16 = (SQint16 *) pData; - for (i = 0; i < iNoBins; i++) - { - p16[i] = ntohs (p16[i]); - } - break; - case 4: - p32 = (SQint32 *) pData; - for (i = 0; i < iNoBins; i++) - { - p32[i] = ntohl(p32[i]); - } - break; - } - } - /* done */ + assert(self); + if (self->iClientSocket <= 0) { + /* already colsed */ return 1; } + + iRet = 1; + memset(&Req_buff, 0, sizeof(struct req_buff_struct)); + + + /* send close message, this helps the master to clean up */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_CLOSE); + status = + send(self->iClientSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + iRet = SEND_ERROR; + } + if (status != sizeof(Req_buff)) { + iRet = SEND_ERROR; + } + status = close(self->iClientSocket); + if (status != 0) { + iRet = CLOSE_ERROR; + } + self->iClientSocket = 0; + self->iClientPort = 0; + return iRet; +} + /*-----------------------------------------------------------------------*/ - int SINQHMProject(pSINQHM self, int code, int xStart, int nx, - int yStart, int ny, - void *pData, int iDataLen) - { - long lBins2Get, lSpace,iNoBins, i; - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - SQint16 *p16; - SQint32 *p32; - char *pPtr; - char pBuffer[8192]; +int SINQHMStartDAQ(pSINQHM self) +{ + int status, iDaq; - assert(self); + assert(self); - /* initialize the Request data */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_PROJECT); - Req_buff.u.project.sub_code = htonl (code); + status = SendDAQCommand(self, DAQ__GO, &iDaq); + if (status < 0) { /* error */ + return status; + } + if (iDaq != 0) { + return DAQ_INHIBIT; + } + return 1; +} - Req_buff.u.project.x_lo = htonl (xStart); - Req_buff.u.project.nx = htonl (nx); - Req_buff.u.project.y_lo = htonl (yStart); - Req_buff.u.project.ny = htonl (ny); - Req_buff.u.project.nhist = htonl (1); +/*-----------------------------------------------------------------------*/ +int SINQHMStopDAQ(pSINQHM self) +{ + int status, iDaq; - /* send the message */ - status = send (self->iClientSocket, (char *) &Req_buff, - sizeof (Req_buff), 0); - if (status == -1) - { - return SEND_ERROR; - } - if (status != sizeof (Req_buff)) - { - return SEND_ERROR; - } + assert(self); - /* wait for an answer */ - status = recv (self->iClientSocket, (char *) &Rply_buff, - sizeof (Rply_buff), MSG_WAITALL); + status = SendDAQCommand(self, DAQ__STOP, &iDaq); + if (status < 0) { /* error */ + return status; + } + if (iDaq == 0) { + return DAQ_NOTSTOPPED; + } + return 1; +} - /* check various error conditions */ - if (status == -1) - { - return RECEIVE_ERROR; - } - if (status != sizeof (Rply_buff)) - { - return INSUFFICIENT_DATA; - } - if(ntohl (Rply_buff.bigend) != 0x12345678) - { - return BYTE_ORDER_CHAOS; - } - iRet = ntohl(Rply_buff.status); - if(iRet != KER__SUCCESS) - { - return HIST_BAD_CODE; - } - - /* calculate the size of things to come */ - lBins2Get = ntohl(Rply_buff.u.project.n_bins) * - ntohl(Rply_buff.u.project.bytes_per_bin); +/*-----------------------------------------------------------------------*/ +int SINQHMContinueDAQ(pSINQHM self) +{ + int status, iDaq; - /* read data */ - pPtr = (char *)pData; - lSpace = iDataLen; - iNoBins = ntohl(Rply_buff.u.project.n_bins); - while (lBins2Get > 0) - { - if(lBins2Get > self->iPacket) - { - i = self->iPacket; - } - else - { - i = lBins2Get; - } - status = recv (self->iClientSocket, pBuffer, - i, 0); - if (status == -1) - { - return SEND_ERROR; - } - lBins2Get -= status; - if((lSpace - status) > 0) - { - memcpy(pPtr,pBuffer,status); - lSpace -= status; - pPtr += status; - } - else - { - if(lSpace > 0) - { - memcpy(pPtr,pBuffer,lSpace); - lSpace = 0; - } - } + assert(self); + + status = SendDAQCommand(self, DAQ__CLR, &iDaq); + if (status < 0) { /* error */ + return status; + } + if (iDaq != 0) { + return DAQ_INHIBIT; + } + return 1; +} + +/*-----------------------------------------------------------------------*/ +int SINQHMInhibitDAQ(pSINQHM self) +{ + int status, iDaq; + + assert(self); + + status = SendDAQCommand(self, DAQ__INH, &iDaq); + if (status < 0) { /* error */ + return status; + } + if (iDaq == 0) { + return DAQ_NOTSTOPPED; + } + return 1; + +} + +/*-----------------------------------------------------------------------*/ +int SINQHMWrite(pSINQHM self, int iNum, int iStart, int iEnd, void *pData) +{ + long lBytes2Go, lBins, i; + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + SQint16 *p16; + SQint32 *p32; + char *pPtr; + + assert(self); + + /* calculate number of bins */ + lBins = iEnd; + + /* take care of byte order first */ + if (0x12345678 != ntohl(0x12345678)) { + /* Swap bytes, if necessary */ + switch (self->iBinWidth) { + case 1: + break; + case 2: + p16 = (SQint16 *) pData; + for (i = 0; i < lBins; i++) { + p16[i] = htons(p16[i]); + } + break; + case 4: + p32 = (SQint32 *) pData; + for (i = 0; i < lBins; i++) { + p32[i] = htonl(p32[i]); + } + break; } - - /* swap bytes if necessary */ - iNoBins = iDataLen/self->iBinWidth; - if ((self->iBinWidth > 0) && (Rply_buff.bigend != 0x12345678)) - { - switch (self->iBinWidth) - { /* Byte swapping is necessary */ - case 2: - /* Not sure how to do this - this might be wrong! */ - p16 = (SQint16 *) pData; - for (i = 0; i < iNoBins; i++) - { - p16[i] = ntohs (p16[i]); - } - break; - case 4: - p32 = (SQint32 *) pData; - for (i = 0; i < iNoBins; i++) - { - p32[i] = ntohl(p32[i]); - } - break; + } + + /* initialize the Request data */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_WRITE); + Req_buff.u.write.n_bins = htonl(lBins); + Req_buff.u.write.first_bin = htonl(iStart); + Req_buff.u.write.bytes_per_bin = htonl(self->iBinWidth); + Req_buff.u.write.hist_no = htonl(iNum); + + /* send the message */ + status = + send(self->iClientSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + if (status != sizeof(Req_buff)) { + return SEND_ERROR; + } + + /* send data */ + lBytes2Go = lBins * self->iBinWidth; + pPtr = (char *) pData; + while (lBytes2Go > 0) { + i = (lBytes2Go > self->iPacket) ? self->iPacket : lBytes2Go; + status = send(self->iClientSocket, (char *) pPtr, i, 0); + if (status <= 0) { + return SEND_ERROR; + } + lBytes2Go -= status; + pPtr += status; + } + + /* get status */ + status = recv(self->iClientSocket, (char *) &Rply_buff, + sizeof(Rply_buff), MSG_WAITALL); + + /* check various error conditions */ + if (status == -1) { + return RECEIVE_ERROR; + } + if (status != sizeof(Rply_buff)) { + return INSUFFICIENT_DATA; + } + if (ntohl(Rply_buff.bigend) != 0x12345678) { + return BYTE_ORDER_CHAOS; + } + if ((iRet = ntohl(Rply_buff.status)) == KER__BAD_VALUE) { + return HIST_BAD_VALUE; + } + if (iRet != KER__SUCCESS) { + return HIST_BAD_CODE; + } + /* success */ + return 1; +} + +/*-----------------------------------------------------------------------*/ +int SINQHMRead(pSINQHM self, int iNum, int iStart, int iEnd, + void *pData, int iDataLen) +{ + long lBins2Get, lSpace, iNoBins, i; + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + SQint16 *p16; + SQint32 *p32; + char *pPtr; + char pBuffer[8192]; + + assert(self); + + /* initialize the Request data */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_READ); + Req_buff.u.read.n_bins = htonl(iEnd - iStart); + Req_buff.u.read.first_bin = htonl(iStart); + Req_buff.u.read.hist_no = htonl(iNum); + + /* send the message */ + status = send(self->iClientSocket, (char *) &Req_buff, + sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + if (status != sizeof(Req_buff)) { + return SEND_ERROR; + } + + /* wait for an answer */ + status = recv(self->iClientSocket, (char *) &Rply_buff, + sizeof(Rply_buff), MSG_WAITALL); + + /* check various error conditions */ + if (status == -1) { + return RECEIVE_ERROR; + } + if (status != sizeof(Rply_buff)) { + return INSUFFICIENT_DATA; + } + if (ntohl(Rply_buff.bigend) != 0x12345678) { + return BYTE_ORDER_CHAOS; + } + iRet = ntohl(Rply_buff.status); + if (iRet != KER__SUCCESS) { + return HIST_BAD_CODE; + } + + /* calculate the size of things to come */ + lBins2Get = + ntohl(Rply_buff.u.read.n_bins) * + ntohl(Rply_buff.u.read.bytes_per_bin); + + /* read data */ + pPtr = (char *) pData; + lSpace = iDataLen; + iNoBins = ntohl(Rply_buff.u.read.n_bins); + while (lBins2Get > 0) { + if (lBins2Get > self->iPacket) { + i = self->iPacket; + } else { + i = lBins2Get; + } + status = recv(self->iClientSocket, pBuffer, i, 0); + if (status == -1) { + return SEND_ERROR; + } + lBins2Get -= status; + if ((lSpace - status) > 0) { + memcpy(pPtr, pBuffer, status); + lSpace -= status; + pPtr += status; + } else { + if (lSpace > 0) { + memcpy(pPtr, pBuffer, lSpace); + lSpace = 0; } } - /* done */ - return 1; } + + /* swap bytes if necessary */ + if ((self->iBinWidth > 0) && (Rply_buff.bigend != 0x12345678)) { + switch (self->iBinWidth) { /* Byte swapping is necessary */ + case 2: + /* Not sure how to do this - this might be wrong! */ + p16 = (SQint16 *) pData; + for (i = 0; i < iNoBins; i++) { + p16[i] = ntohs(p16[i]); + } + break; + case 4: + p32 = (SQint32 *) pData; + for (i = 0; i < iNoBins; i++) { + p32[i] = ntohl(p32[i]); + } + break; + } + } + /* done */ + return 1; +} + +/*-----------------------------------------------------------------------*/ +int SINQHMProject(pSINQHM self, int code, int xStart, int nx, + int yStart, int ny, void *pData, int iDataLen) +{ + long lBins2Get, lSpace, iNoBins, i; + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + SQint16 *p16; + SQint32 *p32; + char *pPtr; + char pBuffer[8192]; + + assert(self); + + /* initialize the Request data */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_PROJECT); + Req_buff.u.project.sub_code = htonl(code); + + Req_buff.u.project.x_lo = htonl(xStart); + Req_buff.u.project.nx = htonl(nx); + Req_buff.u.project.y_lo = htonl(yStart); + Req_buff.u.project.ny = htonl(ny); + Req_buff.u.project.nhist = htonl(1); + + /* send the message */ + status = send(self->iClientSocket, (char *) &Req_buff, + sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + if (status != sizeof(Req_buff)) { + return SEND_ERROR; + } + + /* wait for an answer */ + status = recv(self->iClientSocket, (char *) &Rply_buff, + sizeof(Rply_buff), MSG_WAITALL); + + /* check various error conditions */ + if (status == -1) { + return RECEIVE_ERROR; + } + if (status != sizeof(Rply_buff)) { + return INSUFFICIENT_DATA; + } + if (ntohl(Rply_buff.bigend) != 0x12345678) { + return BYTE_ORDER_CHAOS; + } + iRet = ntohl(Rply_buff.status); + if (iRet != KER__SUCCESS) { + return HIST_BAD_CODE; + } + + /* calculate the size of things to come */ + lBins2Get = ntohl(Rply_buff.u.project.n_bins) * + ntohl(Rply_buff.u.project.bytes_per_bin); + + /* read data */ + pPtr = (char *) pData; + lSpace = iDataLen; + iNoBins = ntohl(Rply_buff.u.project.n_bins); + while (lBins2Get > 0) { + if (lBins2Get > self->iPacket) { + i = self->iPacket; + } else { + i = lBins2Get; + } + status = recv(self->iClientSocket, pBuffer, i, 0); + if (status == -1) { + return SEND_ERROR; + } + lBins2Get -= status; + if ((lSpace - status) > 0) { + memcpy(pPtr, pBuffer, status); + lSpace -= status; + pPtr += status; + } else { + if (lSpace > 0) { + memcpy(pPtr, pBuffer, lSpace); + lSpace = 0; + } + } + } + + /* swap bytes if necessary */ + iNoBins = iDataLen / self->iBinWidth; + if ((self->iBinWidth > 0) && (Rply_buff.bigend != 0x12345678)) { + switch (self->iBinWidth) { /* Byte swapping is necessary */ + case 2: + /* Not sure how to do this - this might be wrong! */ + p16 = (SQint16 *) pData; + for (i = 0; i < iNoBins; i++) { + p16[i] = ntohs(p16[i]); + } + break; + case 4: + p32 = (SQint32 *) pData; + for (i = 0; i < iNoBins; i++) { + p32[i] = ntohl(p32[i]); + } + break; + } + } + /* done */ + return 1; +} + /*------------------------------------------------------------------------ This is the old version, using a master socjet, delete if the other version with client socket works alright. */ - int SINQHMZero2(pSINQHM self, int iNumber, int iStart, int iEnd) - { - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - - assert(self); - - /* fill in the request data structure */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_ZERO); - Req_buff.u.zero.hist_no = htonl (iNumber); - Req_buff.u.zero.first_bin = htonl (iStart); - Req_buff.u.zero.n_bins = htonl (iEnd); - - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - return status; - } - - /* send request */ - status = send(self->iMasterSocket,(char *)&Req_buff, sizeof(Req_buff),0); - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - if(iRet < 0) - { - close(self->iMasterSocket); - self->iMasterSocket = 0; - return iRet; - } - else - { - status = close(self->iMasterSocket); - self->iMasterSocket = 0; - if((status != 0) && (errno != ECONNRESET)) - { - return CLOSE_ERROR; - } - } - return 1; /* success, finally */ - } -/*-----------------------------------------------------------------------*/ - int SINQHMZero(pSINQHM self, int iNumber, int iStart, int iEnd) - { - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - - assert(self); - - /* fill in the request data structure */ - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_ZERO); - Req_buff.u.zero.hist_no = htonl (iNumber); - Req_buff.u.zero.first_bin = htonl (iStart); - Req_buff.u.zero.n_bins = htonl (iEnd); - - /* send request */ - status = send(self->iClientSocket,(char *)&Req_buff, sizeof(Req_buff),0); - if(status == -1) - { - return SEND_ERROR; - } - if(status != sizeof(Req_buff)) - { - return SEND_ERROR; - } +int SINQHMZero2(pSINQHM self, int iNumber, int iStart, int iEnd) +{ + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; - /* get a reply */ - iRet = recv(self->iClientSocket,(char *)&Rply_buff,sizeof(Rply_buff), - MSG_WAITALL); - if(iRet < 0) - { - return RECEIVE_ERROR; - } - if(iRet != sizeof(Rply_buff)) - { - return INSUFFICIENT_DATA; - } - if(ntohl (Rply_buff.bigend) != 0x12345678) - { - return BYTE_ORDER_CHAOS; - } - iRet = ntohl(Rply_buff.status); - if(iRet != KER__SUCCESS) - { - return HIST_BAD_CODE; - } - return 1; /* success, finally */ - } + assert(self); -/*------------------------------------------------------------------------*/ - static int OpenMasterConnection(pSINQHM self) - { - struct hostent *rmt_hostent; - struct sockaddr_in lcl_sockname; - int rmt_sockname_len; - struct sockaddr_in rmt_sockname; - struct in_addr *rmt_inet_addr_pntr; - int status; - - - /* get hostname */ - rmt_hostent = gethostbyname (self->pHMComputer); - if (rmt_hostent == NULL) { - return HMCOMPUTER_NOT_FOUND; - } - rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; - - - /* try, open socket */ - self->iMasterSocket = socket (AF_INET, SOCK_STREAM, 0); - if (self->iMasterSocket == -1) - { - return SOCKET_ERROR; - } + /* fill in the request data structure */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_ZERO); + Req_buff.u.zero.hist_no = htonl(iNumber); + Req_buff.u.zero.first_bin = htonl(iStart); + Req_buff.u.zero.n_bins = htonl(iEnd); - /* bind it */ - lcl_sockname.sin_family = AF_INET; - lcl_sockname.sin_port = htons(0); - lcl_sockname.sin_addr.s_addr = 0; - status = bind (self->iMasterSocket, (struct sockaddr *) &lcl_sockname, - sizeof (lcl_sockname)); - if (status == -1) - { - return BIND_ERROR; - } - - /* try to connect */ - rmt_sockname_len = sizeof (rmt_sockname); - rmt_sockname.sin_family = AF_INET; - rmt_sockname.sin_port = htons (self->iMasterPort); - rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; - status = connect (self->iMasterSocket, (struct sockaddr *) &rmt_sockname, - sizeof (rmt_sockname)); - if (status == -1) { - return CONNECT_ERROR; - } - - /* Success */ - return 1; + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + return status; } -/*------------------------------------------------------------------------*/ - static int GetMasterReply(pSINQHM self, struct rply_buff_struct *reply, - int iBufLen) - { - int status; - - assert(self->iMasterSocket); + /* send request */ + status = + send(self->iMasterSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } - /* get reply structure */ - status = recv (self->iMasterSocket, (char *) reply, - iBufLen, MSG_WAITALL); - if (status == -1) { - return RECEIVE_ERROR; - } else if (status != iBufLen) { - return INSUFFICIENT_DATA; + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + if (iRet < 0) { + close(self->iMasterSocket); + self->iMasterSocket = 0; + return iRet; + } else { + status = close(self->iMasterSocket); + self->iMasterSocket = 0; + if ((status != 0) && (errno != ECONNRESET)) { + return CLOSE_ERROR; } - - /* check endedness */ - if (ntohl (reply->bigend) != 0x12345678) { - return BYTE_ORDER_CHAOS; - } - - /* check histogram memory status codes */ - status = ntohl (reply->status); - if (status == KER__SUCCESS) { - return 1; - }else if (status == KER__BAD_CREATE) { - return HIST_BAD_CREATE; - }else if (status == KER__BAD_STATE) { - return HIST_BAD_STATE; - }else if (status == KER__BAD_VALUE) { - return HIST_BAD_VALUE; - }else if (status == KER__BAD_RECV) { - return HIST_BAD_RECV; - }else if (status == KER__BAD_ALLOC) { - return HIST_BAD_ALLOC; - }else { - return HIST_BAD_CODE; - } - /* not reached, usually */ + } + return 1; /* success, finally */ +} + +/*-----------------------------------------------------------------------*/ +int SINQHMZero(pSINQHM self, int iNumber, int iStart, int iEnd) +{ + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + + assert(self); + + /* fill in the request data structure */ + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_ZERO); + Req_buff.u.zero.hist_no = htonl(iNumber); + Req_buff.u.zero.first_bin = htonl(iStart); + Req_buff.u.zero.n_bins = htonl(iEnd); + + /* send request */ + status = + send(self->iClientSocket, (char *) &Req_buff, sizeof(Req_buff), 0); + if (status == -1) { + return SEND_ERROR; + } + if (status != sizeof(Req_buff)) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = recv(self->iClientSocket, (char *) &Rply_buff, sizeof(Rply_buff), + MSG_WAITALL); + if (iRet < 0) { + return RECEIVE_ERROR; + } + if (iRet != sizeof(Rply_buff)) { + return INSUFFICIENT_DATA; + } + if (ntohl(Rply_buff.bigend) != 0x12345678) { + return BYTE_ORDER_CHAOS; + } + iRet = ntohl(Rply_buff.status); + if (iRet != KER__SUCCESS) { return HIST_BAD_CODE; - } -/*-------------------------------------------------------------------------*/ - int SINQHMError2Text(int iErr, char *pBuffer, int iBufLen) - { - /* the trivial case */ - if(iErr > 0) - { - strncpy(pBuffer,"No error ocurred",iBufLen); - return 0; - } + } + return 1; /* success, finally */ +} + +/*------------------------------------------------------------------------*/ +static int OpenMasterConnection(pSINQHM self) +{ + struct hostent *rmt_hostent; + struct sockaddr_in lcl_sockname; + int rmt_sockname_len; + struct sockaddr_in rmt_sockname; + struct in_addr *rmt_inet_addr_pntr; + int status; + + + /* get hostname */ + rmt_hostent = gethostbyname(self->pHMComputer); + if (rmt_hostent == NULL) { + return HMCOMPUTER_NOT_FOUND; + } + rmt_inet_addr_pntr = (struct in_addr *) rmt_hostent->h_addr_list[0]; + + + /* try, open socket */ + self->iMasterSocket = socket(AF_INET, SOCK_STREAM, 0); + if (self->iMasterSocket == -1) { + return SOCKET_ERROR; + } + + /* bind it */ + lcl_sockname.sin_family = AF_INET; + lcl_sockname.sin_port = htons(0); + lcl_sockname.sin_addr.s_addr = 0; + status = bind(self->iMasterSocket, (struct sockaddr *) &lcl_sockname, + sizeof(lcl_sockname)); + if (status == -1) { + return BIND_ERROR; + } + + /* try to connect */ + rmt_sockname_len = sizeof(rmt_sockname); + rmt_sockname.sin_family = AF_INET; + rmt_sockname.sin_port = htons(self->iMasterPort); + rmt_sockname.sin_addr.s_addr = rmt_inet_addr_pntr->s_addr; + status = connect(self->iMasterSocket, (struct sockaddr *) &rmt_sockname, + sizeof(rmt_sockname)); + if (status == -1) { + return CONNECT_ERROR; + } + + /* Success */ + return 1; +} + +/*------------------------------------------------------------------------*/ +static int GetMasterReply(pSINQHM self, struct rply_buff_struct *reply, + int iBufLen) +{ + + int status; + + assert(self->iMasterSocket); + + /* get reply structure */ + status = recv(self->iMasterSocket, (char *) reply, iBufLen, MSG_WAITALL); + if (status == -1) { + return RECEIVE_ERROR; + } else if (status != iBufLen) { + return INSUFFICIENT_DATA; + } + + /* check endedness */ + if (ntohl(reply->bigend) != 0x12345678) { + return BYTE_ORDER_CHAOS; + } + + /* check histogram memory status codes */ + status = ntohl(reply->status); + if (status == KER__SUCCESS) { + return 1; + } else if (status == KER__BAD_CREATE) { + return HIST_BAD_CREATE; + } else if (status == KER__BAD_STATE) { + return HIST_BAD_STATE; + } else if (status == KER__BAD_VALUE) { + return HIST_BAD_VALUE; + } else if (status == KER__BAD_RECV) { + return HIST_BAD_RECV; + } else if (status == KER__BAD_ALLOC) { + return HIST_BAD_ALLOC; + } else { + return HIST_BAD_CODE; + } + /* not reached, usually */ + return HIST_BAD_CODE; +} + +/*-------------------------------------------------------------------------*/ +int SINQHMError2Text(int iErr, char *pBuffer, int iBufLen) +{ + /* the trivial case */ + if (iErr > 0) { + strncpy(pBuffer, "No error ocurred", iBufLen); + return 0; + } + + switch (iErr) { + case HMCOMPUTER_NOT_FOUND: + strncpy(pBuffer, + "No name server entry for histogram memory computer", iBufLen); + break; + case SOCKET_ERROR: + strncpy(pBuffer, + "Insufficient system resources for socket creation", iBufLen); + break; + case BIND_ERROR: + strncpy(pBuffer, "Cannot bind", iBufLen); + break; + case CONNECT_ERROR: + strncpy(pBuffer, + "Cannot connect, probably port number wrong", iBufLen); + break; + case RECEIVE_ERROR: + strncpy(pBuffer, "Error receiving data", iBufLen); + break; + case INSUFFICIENT_DATA: + strncpy(pBuffer, + "Not enough bytes received from host, network trouble", + iBufLen); + break; + case BYTE_ORDER_CHAOS: + strncpy(pBuffer, "Reply not in network byte order", iBufLen); + break; + case HIST_BAD_CREATE: + strncpy(pBuffer, + "Master histogram server failed to spawn child", iBufLen); + break; + case HIST_BAD_VALUE: + strncpy(pBuffer, "Invalid parameter detected", iBufLen); + break; + case HIST_BAD_STATE: + strncpy(pBuffer, "Histogram memory NOT configured", iBufLen); + break; + case HIST_BAD_RECV: + strncpy(pBuffer, "Histogram server failed to read command", iBufLen); + break; + case HIST_BAD_ALLOC: + strncpy(pBuffer, "Histogram memory out of memory!", iBufLen); + break; + case HIST_BAD_CODE: + strncpy(pBuffer, + "Unknown or corrupted status code sent from master server", + iBufLen); + break; + case SEND_ERROR: + strncpy(pBuffer, "Error sending data", iBufLen); + break; + case CLOSE_ERROR: + strncpy(pBuffer, "Error closing connection", iBufLen); + break; + case INVALID_HARSH: + strncpy(pBuffer, "Invalid parameter for harshness", iBufLen); + break; + case SOFTWARE_ERROR: + strncpy(pBuffer, + "Internal error or software error at histogram memory computer, consult a hacker", + iBufLen); + break; + case DAQ_INHIBIT: + strncpy(pBuffer, "Data aquisition inhibited by some client", iBufLen); + break; + case DAQ_NOTSTOPPED: + strncpy(pBuffer, + "Data aquisition not stopped, suggests SW or network problem", + iBufLen); + break; + + default: + strncpy(pBuffer, "Unknown error code", iBufLen); + } + return 1; +} - switch(iErr) - { - case HMCOMPUTER_NOT_FOUND: - strncpy(pBuffer, - "No name server entry for histogram memory computer", - iBufLen); - break; - case SOCKET_ERROR: - strncpy(pBuffer, - "Insufficient system resources for socket creation", - iBufLen); - break; - case BIND_ERROR: - strncpy(pBuffer, - "Cannot bind", - iBufLen); - break; - case CONNECT_ERROR: - strncpy(pBuffer, - "Cannot connect, probably port number wrong", - iBufLen); - break; - case RECEIVE_ERROR: - strncpy(pBuffer, - "Error receiving data", iBufLen); - break; - case INSUFFICIENT_DATA: - strncpy(pBuffer, - "Not enough bytes received from host, network trouble", - iBufLen); - break; - case BYTE_ORDER_CHAOS: - strncpy(pBuffer, - "Reply not in network byte order", - iBufLen); - break; - case HIST_BAD_CREATE: - strncpy(pBuffer, - "Master histogram server failed to spawn child", - iBufLen); - break; - case HIST_BAD_VALUE: - strncpy(pBuffer, - "Invalid parameter detected", - iBufLen); - break; - case HIST_BAD_STATE: - strncpy(pBuffer, - "Histogram memory NOT configured", - iBufLen); - break; - case HIST_BAD_RECV: - strncpy(pBuffer, - "Histogram server failed to read command", - iBufLen); - break; - case HIST_BAD_ALLOC: - strncpy(pBuffer, - "Histogram memory out of memory!", - iBufLen); - break; - case HIST_BAD_CODE: - strncpy(pBuffer, - "Unknown or corrupted status code sent from master server", - iBufLen); - break; - case SEND_ERROR: - strncpy(pBuffer, - "Error sending data", - iBufLen); - break; - case CLOSE_ERROR: - strncpy(pBuffer, - "Error closing connection", - iBufLen); - break; - case INVALID_HARSH: - strncpy(pBuffer, - "Invalid parameter for harshness", - iBufLen); - break; - case SOFTWARE_ERROR: - strncpy(pBuffer, - "Internal error or software error at histogram memory computer, consult a hacker", - iBufLen); - break; - case DAQ_INHIBIT: - strncpy(pBuffer, - "Data aquisition inhibited by some client", - iBufLen); - break; - case DAQ_NOTSTOPPED: - strncpy(pBuffer, - "Data aquisition not stopped, suggests SW or network problem", - iBufLen); - break; - - default: - strncpy(pBuffer, - "Unknown error code",iBufLen); - } - return 1; - } /*------------------------------------------------------------------------- SINQHM needs an additional top bin defining the upper edge of the histogram. So, for a 512 bin array, 513 bins are needed. The additional bin is created in the code below. This explains the strange arithmetic with EdgeLength and the code at the end of the for loop -*/ - int SINQHMDefineBank(pSINQHM self, int iBankNumber, int iStart, int iEnd, - float *iEdges, int iEdgeLength) - { - pSBank pWork = NULL; - int i, iDelay, iDiff; - - assert(self); - assert(iBankNumber >= 0); - assert(iBankNumber < MAXBANK); - assert(iEdgeLength >= 1); - assert(iStart >= 0); - assert(iEnd >= iStart); +*/ +int SINQHMDefineBank(pSINQHM self, int iBankNumber, int iStart, int iEnd, + float *iEdges, int iEdgeLength) +{ + pSBank pWork = NULL; + int i, iDelay, iDiff; - if(iBankNumber >= self->iBanks) - { - self->iBanks = iBankNumber +1; - } - pWork = &(self->pBank[iBankNumber]); - if(pWork->iEdges != NULL) - { - free(pWork->iEdges); - pWork->iEdges = NULL; - } - iDelay = (int)iEdges[0]; - pWork->iStart = iStart; - pWork->iEnd = iEnd; - pWork->iEdgeLength = iEdgeLength; - if(iEdgeLength == 2) - { /* - fixed binwidth: two values required: start stop in - edge[0], edge[1] - */ - pWork->iFlag = 0; - pWork->iDelay = iDelay; - pWork->iEdges = (unsigned int *)malloc(2*sizeof(unsigned int)); - if(!pWork->iEdges) - { - return HIST_BAD_ALLOC; - } - pWork->iEdges[0] = htonl((unsigned int)iEdges[0]); - pWork->iEdges[1] = htonl((unsigned int)(iEdges[1] - iDelay)); - return 1; - } + assert(self); + assert(iBankNumber >= 0); + assert(iBankNumber < MAXBANK); + assert(iEdgeLength >= 1); + assert(iStart >= 0); + assert(iEnd >= iStart); - /* - normal case: create the bin boundaries - */ - pWork->iFlag = 1; - pWork->iEdgeLength++; - iEdgeLength++; - pWork->iEdges = (unsigned int *)malloc(iEdgeLength * - sizeof(unsigned int)); - if(!pWork->iEdges) - { - return HIST_BAD_ALLOC; - } - pWork->iDelay = iDelay; - for(i = 0; i < iEdgeLength-1; i++) - { - pWork->iEdges[i] = htonl((unsigned int)(iEdges[i]-iDelay)); - } - iDiff = iEdges[1] - iEdges[0]; - pWork->iEdges[iEdgeLength-1] = htonl(iEdges[iEdgeLength-2] - + iDiff - iDelay); - return 1; - } -/*-----------------------------------------------------------------------*/ - struct tof { - uint n_extra_bytes; - usint n_edges; - usint n_banks; - uint preset_delay; - struct tof_edge_arr edge_0; - struct tof_bank bank_0; - }; - -/*------------------------------------------------------------------------*/ - int SINQHMTimeBin(pSINQHM self,int iMode) - { - int status, iRet; - struct req_buff_struct Req_buff; - struct rply_buff_struct Rply_buff; - int iLength, i, iDelay; - unsigned int iExtra; - char *pBuffer = NULL, *pPtr; - struct tof_edge_arr tea; - int iTeaLength; - struct tof_bank toba; - struct tof tofi; - - assert(self); - - memset(&Req_buff,0,sizeof(struct req_buff_struct)); - memset(&Rply_buff,0,sizeof(struct rply_buff_struct)); - - /* figure out how long we are going to be*/ - iLength = 24 + self->iBanks*sizeof(struct tof_bank); - for(i = 0; i < self->iBanks; i++) - { - iLength += 8 + self->pBank[i].iEdgeLength*sizeof(SQint32); - } - if(iLength < 64) - iLength = 64; - /* allocate send buffer */ - pBuffer = (char *)malloc(iLength*sizeof(char)); - if(!pBuffer) - { - return HIST_BAD_ALLOC; - } - memset(pBuffer,0,iLength); - - /* do the message header */ - iExtra = iLength - 64; - if(iExtra < 0) - iExtra = 0; - iDelay = self->pBank[0].iEdges[0]; - Req_buff.bigend = htonl (0x12345678); - Req_buff.cmnd = htonl (SQHM_CONFIG); - Req_buff.u.cnfg.mode = htonl (iMode); - memcpy(pBuffer,&Req_buff,12); - pPtr = pBuffer + 12; - - tofi.n_extra_bytes = htonl(iExtra); - tofi.n_edges = htons(self->iBanks); - tofi.n_banks = htons(self->iBanks); - tofi.preset_delay = htonl(self->pBank[0].iDelay); - memcpy(pPtr,&tofi,12); - pPtr += 12; - - /* do the edge thingies */ - for(i = 0; i < self->iBanks; i++) - { - tea.n_bins = htonl(self->pBank[i].iEdgeLength-1); - if(self->pBank[i].iEdgeLength == 2) - { - tea.flag = htonl(0); - } - else - { - tea.flag = htonl(1); - } - tea.edges = self->pBank[i].iEdges; - memcpy(pPtr,&tea,8); - pPtr += 8; - iTeaLength = self->pBank[i].iEdgeLength*4; - memcpy(pPtr,self->pBank[i].iEdges,iTeaLength); - pPtr += iTeaLength; - } - - /* do the swiss bank structures */ - for(i = 0; i < self->iBanks; i++) - { - toba.first = htons(self->pBank[i].iStart); - toba.n_cntrs = htons(self->pBank[i].iEnd); - toba.edge_indx = htons(i); - toba.bytes_per_bin = htons(self->iBinWidth); - memcpy(pPtr,&toba,sizeof(struct tof_bank)); - pPtr += sizeof(struct tof_bank); - } - - /* all packed up neat and nicely, send it */ - /* try, get a connection to master server */ - status = OpenMasterConnection(self); - if(status < 0) - { - if(pBuffer) - free(pBuffer); - return status; - } - - /* send request */ - status = send(self->iMasterSocket,pBuffer,iLength ,0); - if(pBuffer) - { - free(pBuffer); - } - if(status == -1) - { - return SEND_ERROR; - } - - /* get a reply */ - iRet = GetMasterReply(self,&Rply_buff,sizeof(Rply_buff)); - if(iRet < 0) - { - /* try close the socket */ - close(self->iMasterSocket); - self->iMasterSocket = 0; - return iRet; - } - else - { - /* close the socket */ - status = close(self->iMasterSocket); - self->iMasterSocket = 0; - if((status != 0) && (errno != ECONNRESET)) - { - return CLOSE_ERROR; - } - } - return 1; /* success, finally */ + if (iBankNumber >= self->iBanks) { + self->iBanks = iBankNumber + 1; } + pWork = &(self->pBank[iBankNumber]); + if (pWork->iEdges != NULL) { + free(pWork->iEdges); + pWork->iEdges = NULL; + } + iDelay = (int) iEdges[0]; + pWork->iStart = iStart; + pWork->iEnd = iEnd; + pWork->iEdgeLength = iEdgeLength; + if (iEdgeLength == 2) { /* + fixed binwidth: two values required: start stop in + edge[0], edge[1] + */ + pWork->iFlag = 0; + pWork->iDelay = iDelay; + pWork->iEdges = (unsigned int *) malloc(2 * sizeof(unsigned int)); + if (!pWork->iEdges) { + return HIST_BAD_ALLOC; + } + pWork->iEdges[0] = htonl((unsigned int) iEdges[0]); + pWork->iEdges[1] = htonl((unsigned int) (iEdges[1] - iDelay)); + return 1; + } + + /* + normal case: create the bin boundaries + */ + pWork->iFlag = 1; + pWork->iEdgeLength++; + iEdgeLength++; + pWork->iEdges = (unsigned int *) malloc(iEdgeLength * + sizeof(unsigned int)); + if (!pWork->iEdges) { + return HIST_BAD_ALLOC; + } + pWork->iDelay = iDelay; + for (i = 0; i < iEdgeLength - 1; i++) { + pWork->iEdges[i] = htonl((unsigned int) (iEdges[i] - iDelay)); + } + iDiff = iEdges[1] - iEdges[0]; + pWork->iEdges[iEdgeLength - 1] = htonl(iEdges[iEdgeLength - 2] + + iDiff - iDelay); + return 1; +} + +/*-----------------------------------------------------------------------*/ +struct tof { + uint n_extra_bytes; + usint n_edges; + usint n_banks; + uint preset_delay; + struct tof_edge_arr edge_0; + struct tof_bank bank_0; +}; + +/*------------------------------------------------------------------------*/ +int SINQHMTimeBin(pSINQHM self, int iMode) +{ + int status, iRet; + struct req_buff_struct Req_buff; + struct rply_buff_struct Rply_buff; + int iLength, i, iDelay; + unsigned int iExtra; + char *pBuffer = NULL, *pPtr; + struct tof_edge_arr tea; + int iTeaLength; + struct tof_bank toba; + struct tof tofi; + + assert(self); + + memset(&Req_buff, 0, sizeof(struct req_buff_struct)); + memset(&Rply_buff, 0, sizeof(struct rply_buff_struct)); + + /* figure out how long we are going to be */ + iLength = 24 + self->iBanks * sizeof(struct tof_bank); + for (i = 0; i < self->iBanks; i++) { + iLength += 8 + self->pBank[i].iEdgeLength * sizeof(SQint32); + } + if (iLength < 64) + iLength = 64; + /* allocate send buffer */ + pBuffer = (char *) malloc(iLength * sizeof(char)); + if (!pBuffer) { + return HIST_BAD_ALLOC; + } + memset(pBuffer, 0, iLength); + + /* do the message header */ + iExtra = iLength - 64; + if (iExtra < 0) + iExtra = 0; + iDelay = self->pBank[0].iEdges[0]; + Req_buff.bigend = htonl(0x12345678); + Req_buff.cmnd = htonl(SQHM_CONFIG); + Req_buff.u.cnfg.mode = htonl(iMode); + memcpy(pBuffer, &Req_buff, 12); + pPtr = pBuffer + 12; + + tofi.n_extra_bytes = htonl(iExtra); + tofi.n_edges = htons(self->iBanks); + tofi.n_banks = htons(self->iBanks); + tofi.preset_delay = htonl(self->pBank[0].iDelay); + memcpy(pPtr, &tofi, 12); + pPtr += 12; + + /* do the edge thingies */ + for (i = 0; i < self->iBanks; i++) { + tea.n_bins = htonl(self->pBank[i].iEdgeLength - 1); + if (self->pBank[i].iEdgeLength == 2) { + tea.flag = htonl(0); + } else { + tea.flag = htonl(1); + } + tea.edges = self->pBank[i].iEdges; + memcpy(pPtr, &tea, 8); + pPtr += 8; + iTeaLength = self->pBank[i].iEdgeLength * 4; + memcpy(pPtr, self->pBank[i].iEdges, iTeaLength); + pPtr += iTeaLength; + } + + /* do the swiss bank structures */ + for (i = 0; i < self->iBanks; i++) { + toba.first = htons(self->pBank[i].iStart); + toba.n_cntrs = htons(self->pBank[i].iEnd); + toba.edge_indx = htons(i); + toba.bytes_per_bin = htons(self->iBinWidth); + memcpy(pPtr, &toba, sizeof(struct tof_bank)); + pPtr += sizeof(struct tof_bank); + } + + /* all packed up neat and nicely, send it */ + /* try, get a connection to master server */ + status = OpenMasterConnection(self); + if (status < 0) { + if (pBuffer) + free(pBuffer); + return status; + } + + /* send request */ + status = send(self->iMasterSocket, pBuffer, iLength, 0); + if (pBuffer) { + free(pBuffer); + } + if (status == -1) { + return SEND_ERROR; + } + + /* get a reply */ + iRet = GetMasterReply(self, &Rply_buff, sizeof(Rply_buff)); + if (iRet < 0) { + /* try close the socket */ + close(self->iMasterSocket); + self->iMasterSocket = 0; + return iRet; + } else { + /* close the socket */ + status = close(self->iMasterSocket); + self->iMasterSocket = 0; + if ((status != 0) && (errno != ECONNRESET)) { + return CLOSE_ERROR; + } + } + return 1; /* success, finally */ +} diff --git a/hardsup/sinqhm.h b/hardsup/sinqhm.h index ad692a1..91c2296 100644 --- a/hardsup/sinqhm.h +++ b/hardsup/sinqhm.h @@ -14,7 +14,7 @@ #define SINQHMUTILITY #include "sinqhm_def.h" - typedef struct __SINQHM *pSINQHM; +typedef struct __SINQHM *pSINQHM; /*------------------------------ Error codes -----------------------------*/ #line 341 "sinqhm.w" @@ -46,61 +46,59 @@ #line 118 "sinqhm.w" - pSINQHM CreateSINQHM(char *pHMComputer, int iMasterPort); - pSINQHM CopySINQHM(pSINQHM self); - void DeleteSINQHM(pSINQHM self); - void SINQHMSetPar(pSINQHM self, int iRank, int iLength, int iBinWidth); - void SINQHMSetPSD(pSINQHM self, int xSize, int xOff, int xFac, - int ySize, int yOff, int yFac); +pSINQHM CreateSINQHM(char *pHMComputer, int iMasterPort); +pSINQHM CopySINQHM(pSINQHM self); +void DeleteSINQHM(pSINQHM self); +void SINQHMSetPar(pSINQHM self, int iRank, int iLength, int iBinWidth); +void SINQHMSetPSD(pSINQHM self, int xSize, int xOff, int xFac, + int ySize, int yOff, int yFac); #line 142 "sinqhm.w" - int SINQHMError2Text(int iErr, char *pBuffer, int iBufLen); +int SINQHMError2Text(int iErr, char *pBuffer, int iBufLen); #line 155 "sinqhm.w" - int SINQHMConfigure(pSINQHM self, int iMode, int iRank, int iLength, - int iBinWidth, int iLowBin, int iCompress); - int SINQHMConfigurePSD(pSINQHM self, int iMode, - int xSize, int xOff, int xFac, - int ySize, int yOff, int yFac, - int iBinWidth, - float *iEdges, int iEdgeLength); +int SINQHMConfigure(pSINQHM self, int iMode, int iRank, int iLength, + int iBinWidth, int iLowBin, int iCompress); +int SINQHMConfigurePSD(pSINQHM self, int iMode, + int xSize, int xOff, int xFac, + int ySize, int yOff, int yFac, + int iBinWidth, float *iEdges, int iEdgeLength); + +int SINQHMDeconfigure(pSINQHM self, int iHarsh); +int SINQHMGetStatus(pSINQHM self, int *iMode, int *iDaq, + int *iRank, int *iBinWidth, + int *iLength, int *iClients); +int SINQHMDebug(pSINQHM self, int iLevel); +int SINQHMKill(pSINQHM self); - int SINQHMDeconfigure(pSINQHM self, int iHarsh); - int SINQHMGetStatus(pSINQHM self,int *iMode, int *iDaq, - int *iRank, int *iBinWidth, - int *iLength, int *iClients); - int SINQHMDebug(pSINQHM self, int iLevel); - int SINQHMKill(pSINQHM self); - #line 261 "sinqhm.w" - int SINQHMOpenDAQ(pSINQHM self); - int SINQHMCloseDAQ(pSINQHM self); +int SINQHMOpenDAQ(pSINQHM self); +int SINQHMCloseDAQ(pSINQHM self); - int SINQHMStartDAQ(pSINQHM self); - int SINQHMStopDAQ(pSINQHM self); - int SINQHMInhibitDAQ(pSINQHM self); - int SINQHMContinueDAQ(pSINQHM self); - - int SINQHMWrite(pSINQHM self, int iNum, int iStart, int iEnd, - void *pData); - long SINQHMSize(pSINQHM self, int iNum, int iStart, int iEnd); - int SINQHMRead(pSINQHM self, int iNum, int iStart, int iEnd, - void *pData, int iDataLen); - int SINQHMProject(pSINQHM self, int code, int xStart, int nx, - int yStart, int ny, void *pData, int iDataLen); - int SINQHMZero(pSINQHM self, int iNum, int iStart, int iEnd); +int SINQHMStartDAQ(pSINQHM self); +int SINQHMStopDAQ(pSINQHM self); +int SINQHMInhibitDAQ(pSINQHM self); +int SINQHMContinueDAQ(pSINQHM self); + +int SINQHMWrite(pSINQHM self, int iNum, int iStart, int iEnd, void *pData); +long SINQHMSize(pSINQHM self, int iNum, int iStart, int iEnd); +int SINQHMRead(pSINQHM self, int iNum, int iStart, int iEnd, + void *pData, int iDataLen); +int SINQHMProject(pSINQHM self, int code, int xStart, int nx, + int yStart, int ny, void *pData, int iDataLen); +int SINQHMZero(pSINQHM self, int iNum, int iStart, int iEnd); #line 382 "sinqhm.w" #line 232 "sinqhm.w" - int SINQHMDefineBank(pSINQHM self, int iBankNumber, int iStart, int iEnd, - float *iEdges, int iEdgeLength); +int SINQHMDefineBank(pSINQHM self, int iBankNumber, int iStart, int iEnd, + float *iEdges, int iEdgeLength); #line 383 "sinqhm.w" diff --git a/hardsup/sinqhm_def.h b/hardsup/sinqhm_def.h index 6b17b2e..45e8bc4 100644 --- a/hardsup/sinqhm_def.h +++ b/hardsup/sinqhm_def.h @@ -19,15 +19,15 @@ /*------------------------------------------------------------------------------ ** Define some defaults. */ -#define PORT_BASE 2400 /* The Internet Port for Server Requests */ -#define MAX_CLIENTS 8 /* The maximum number of active clients */ -#define MAX_TOF_CNTR 1024 /* The maximum number of individual counters .. - ** which can be handled in TOF mode */ +#define PORT_BASE 2400 /* The Internet Port for Server Requests */ +#define MAX_CLIENTS 8 /* The maximum number of active clients */ +#define MAX_TOF_CNTR 1024 /* The maximum number of individual counters .. + ** which can be handled in TOF mode */ #define MAX_PSD_CNTR 1048576 /* maximum number of PSD elements */ -#define MAX_TOF_NBINS 32768 /* The maximum number of bins in a TOF histog */ -#define MAX_TOF_EDGE 16 /* The maximum number of TOF edge arrays */ -#define VMIO_BASE_ADDR 0x1900 /* VME address of a (possible) VMIO10 module */ -#define IDENT_MSGE_LEN 256 /* Length of Ident info for SQHM_IDENT */ +#define MAX_TOF_NBINS 32768 /* The maximum number of bins in a TOF histog */ +#define MAX_TOF_EDGE 16 /* The maximum number of TOF edge arrays */ +#define VMIO_BASE_ADDR 0x1900 /* VME address of a (possible) VMIO10 module */ +#define IDENT_MSGE_LEN 256 /* Length of Ident info for SQHM_IDENT */ #define uchar unsigned char #define usint unsigned short int @@ -71,76 +71,76 @@ #define SQHM_STATUS 0x0a #define SQHM_WRITE 0x0b #define SQHM_ZERO 0x0c - /* - ** Define the various operation modes - */ -#define SQHM__TRANS 0x1000 /* Transparent mode */ -#define SQHM__HM_DIG 0x2000 /* Hist mode (with digitised read-out) */ -#define SQHM__TOF 0x3000 /* Time-of-Flight mode */ -#define SQHM__HM_PSD 0x4000 /* Hist mode (with Pos-sens-detect read-out) */ -#define SQHM__HRPT 0x5000 /* Hist mode for HRPT */ - /* - ** Define the various sub-mode bits of the operation modes - */ -#define SQHM__SUB_MODE_MSK 0xff /* Mask for extracting "sub-mode" bits */ -#define SQHM__DEBUG 0x01 /* Debug flag - FILLER will suspend itself .. - ** .. after starting to allow debugging */ -#define SQHM__UD 0x02 /* Use Up/Down bit information */ + /* + ** Define the various operation modes + */ +#define SQHM__TRANS 0x1000 /* Transparent mode */ +#define SQHM__HM_DIG 0x2000 /* Hist mode (with digitised read-out) */ +#define SQHM__TOF 0x3000 /* Time-of-Flight mode */ +#define SQHM__HM_PSD 0x4000 /* Hist mode (with Pos-sens-detect read-out) */ +#define SQHM__HRPT 0x5000 /* Hist mode for HRPT */ + /* + ** Define the various sub-mode bits of the operation modes + */ +#define SQHM__SUB_MODE_MSK 0xff /* Mask for extracting "sub-mode" bits */ +#define SQHM__DEBUG 0x01 /* Debug flag - FILLER will suspend itself .. + ** .. after starting to allow debugging */ +#define SQHM__UD 0x02 /* Use Up/Down bit information */ -#define SQHM__BO_MSK 0x18 /* Mask for extracting "bin-overflow" bits */ -#define SQHM__BO_IGN 0x00 /* Ignore bin-overflows (bin-contents wrap) */ -#define SQHM__BO_SMAX 0x08 /* On bin-overflow, stop at maximum */ -#define SQHM__BO_CNT 0x10 /* Keep counts of overflow bins */ +#define SQHM__BO_MSK 0x18 /* Mask for extracting "bin-overflow" bits */ +#define SQHM__BO_IGN 0x00 /* Ignore bin-overflows (bin-contents wrap) */ +#define SQHM__BO_SMAX 0x08 /* On bin-overflow, stop at maximum */ +#define SQHM__BO_CNT 0x10 /* Keep counts of overflow bins */ -#define SQHM__STROBO 0x20 /* Use strobo-bit information */ -#define SQHM__REFLECT 0x40 /* Reflect histograms */ -#define SQHM__NO_STAT 0x80 /* Suppress status info from "Filler" */ - /* - ** ---------------------------------------------------------- - ** SQHM_DAQ sub-function codes - */ +#define SQHM__STROBO 0x20 /* Use strobo-bit information */ +#define SQHM__REFLECT 0x40 /* Reflect histograms */ +#define SQHM__NO_STAT 0x80 /* Suppress status info from "Filler" */ + /* + ** ---------------------------------------------------------- + ** SQHM_DAQ sub-function codes + */ #define DAQ__EXIT 0xffffffff #define DAQ__CLR 0x01 #define DAQ__GO 0x02 #define DAQ__INH 0x03 #define DAQ__STOP 0x04 #define DAQ__TST 0x05 - /* - ** ---------------------------------------------------------- - ** SQHM_PROJECT sub-codes - */ -#define PROJECT__ON_Y 0x0001 /* Project onto y-axis */ -#define PROJECT__1_DIM 0x0002 /* Make projection of a 1-dim histogram */ + /* + ** ---------------------------------------------------------- + ** SQHM_PROJECT sub-codes + */ +#define PROJECT__ON_Y 0x0001 /* Project onto y-axis */ +#define PROJECT__1_DIM 0x0002 /* Make projection of a 1-dim histogram */ #define PROJECT__COLL 0x0003 /* collapse PSD on one time channel */ #define PROJECT__SAMPLE 0x0004 /* sum a rectangular part of the PSD detector in time - */ - /* - ** ---------------------------------------------------------- - ** Definition of bits in of TOF edge-array - */ -#define FLAG__VAR_BIN 0x01 /* Bin span of histogram is variable */ - /* - ** ---------------------------------------------------------- - ** Definition of bits in of SQHM_STATUS response - */ -#define STATUS_FLAGS__PF 0x8000 /* PF - Power Fail */ -#define STATUS_FLAGS__SWC 0x4000 /* SWC - Status Word Changed */ -#define STATUS_FLAGS__NRL 0x2000 /* NRL - Neutron Rate Low */ -#define STATUS_FLAGS__DAQ 0x1000 /* DAQ on -- set if Hdr Mask Bits are - ** correct so that data acq is active */ -#define STATUS_FLAGS__SYNC3 0x0800 /* Ext Synch Bit #3 */ -#define STATUS_FLAGS__SYNC2 0x0400 /* Ext Synch Bit #2 */ -#define STATUS_FLAGS__SYNC1 0x0200 /* Ext Synch Bit #1 */ -#define STATUS_FLAGS__SYNC0 0x0100 /* Ext Synch Bit #0 */ -#define STATUS_FLAGS__UD 0x0080 /* UD - Up/Down */ -#define STATUS_FLAGS__GU 0x0040 /* GU - Gummi (i.e. Strobo) */ - /* - ** ---------------------------------------------------------- - */ -#define N_HISTS_MAX 64 /* Maximum number of histograms supported */ -#define N_BINS_MAX 0x00ffff /* Maximum histogram bin number permitted */ -#define N_TOTAL_BYTES 0x400000 /* Maximum total bytes of histogram */ + */ + /* + ** ---------------------------------------------------------- + ** Definition of bits in of TOF edge-array + */ +#define FLAG__VAR_BIN 0x01 /* Bin span of histogram is variable */ + /* + ** ---------------------------------------------------------- + ** Definition of bits in of SQHM_STATUS response + */ +#define STATUS_FLAGS__PF 0x8000 /* PF - Power Fail */ +#define STATUS_FLAGS__SWC 0x4000 /* SWC - Status Word Changed */ +#define STATUS_FLAGS__NRL 0x2000 /* NRL - Neutron Rate Low */ +#define STATUS_FLAGS__DAQ 0x1000 /* DAQ on -- set if Hdr Mask Bits are + ** correct so that data acq is active */ +#define STATUS_FLAGS__SYNC3 0x0800 /* Ext Synch Bit #3 */ +#define STATUS_FLAGS__SYNC2 0x0400 /* Ext Synch Bit #2 */ +#define STATUS_FLAGS__SYNC1 0x0200 /* Ext Synch Bit #1 */ +#define STATUS_FLAGS__SYNC0 0x0100 /* Ext Synch Bit #0 */ +#define STATUS_FLAGS__UD 0x0080 /* UD - Up/Down */ +#define STATUS_FLAGS__GU 0x0040 /* GU - Gummi (i.e. Strobo) */ + /* + ** ---------------------------------------------------------- + */ +#define N_HISTS_MAX 64 /* Maximum number of histograms supported */ +#define N_BINS_MAX 0x00ffff /* Maximum histogram bin number permitted */ +#define N_TOTAL_BYTES 0x400000 /* Maximum total bytes of histogram */ /* **------------------------------------------------------------------------------ ** Definitions of Filler states in HRPT mode @@ -151,74 +151,74 @@ **------------------------------------------------------------------------------ ** Definitions for the LWL Datagrams */ -#define LWL_HDR_TYPE_MASK (0x1f000000) /* Mask for extracting main dgrm .. - ** .. hdr command-type bits */ -#define LWL_HDR_PF_MASK (0x80000000) /* Mask for extr Power Fail bit */ -#define LWL_HDR_SWC_MASK (0x40000000) /* Mask for extr Status Word Chng bit */ -#define LWL_HDR_NRL_MASK (0x20000000) /* Mask for extr Neutron Rate Low bit */ -#define LWL_HDR_SYNC3_MASK (0x00800000) /* Mask for one of ext synch bits */ -#define LWL_HDR_SYNC2_MASK (0x00400000) /* Mask for one of ext synch bits */ -#define LWL_HDR_SYNC1_MASK (0x00200000) /* Mask for one of ext synch bits */ -#define LWL_HDR_SYNC0_MASK (0x00100000) /* Mask for one of ext synch bits */ -#define LWL_HDR_UD_MASK LWL_HDR_SYNC1_MASK /* Mask for Up/Down bit */ -#define LWL_HDR_GU_MASK LWL_HDR_SYNC0_MASK /* Mask for GU bit */ -#define LWL_HDR_BA_MASK (0x00f00000) /* Mask for TSI Binning Addr */ -#define LWL_HDR_TS_MASK (0x000fffff) /* Mask for TSI Time Stamp */ +#define LWL_HDR_TYPE_MASK (0x1f000000) /* Mask for extracting main dgrm .. + ** .. hdr command-type bits */ +#define LWL_HDR_PF_MASK (0x80000000) /* Mask for extr Power Fail bit */ +#define LWL_HDR_SWC_MASK (0x40000000) /* Mask for extr Status Word Chng bit */ +#define LWL_HDR_NRL_MASK (0x20000000) /* Mask for extr Neutron Rate Low bit */ +#define LWL_HDR_SYNC3_MASK (0x00800000) /* Mask for one of ext synch bits */ +#define LWL_HDR_SYNC2_MASK (0x00400000) /* Mask for one of ext synch bits */ +#define LWL_HDR_SYNC1_MASK (0x00200000) /* Mask for one of ext synch bits */ +#define LWL_HDR_SYNC0_MASK (0x00100000) /* Mask for one of ext synch bits */ +#define LWL_HDR_UD_MASK LWL_HDR_SYNC1_MASK /* Mask for Up/Down bit */ +#define LWL_HDR_GU_MASK LWL_HDR_SYNC0_MASK /* Mask for GU bit */ +#define LWL_HDR_BA_MASK (0x00f00000) /* Mask for TSI Binning Addr */ +#define LWL_HDR_TS_MASK (0x000fffff) /* Mask for TSI Time Stamp */ -#define LWL_FIFO_EMPTY (0x1e000000) /* FIFO Empty */ +#define LWL_FIFO_EMPTY (0x1e000000) /* FIFO Empty */ -#define LWL_TSI_TR (0x1f000000) /* Time-Status-Info Transp-Mode */ -#define LWL_TSI_HM_NC (0x1f000000) /* Time-Status-Info Hist-Mode+No-Coinc */ -#define LWL_TSI_HM_C (0x0e000000) /* Time-Status-Info Hist-Mode+Coinc */ -#define LWL_TSI_TOF (0x1f000000) /* Time-Status-Info TOF-Mode */ -#define LWL_TSI_SM_NC (0x1f000000) /* Time-Status-Info Strobo-Mode+No-Coin */ -#define LWL_TSI_SM_C (0x0e000000) /* Time-Status-Info Strobo-Mode+Coinc */ -#define LWL_TSI_DT_MSK (0x000fffff) /* Mask for Dead-Time in TSI */ -#define LWL_TSI_DTS_MSK (0x000fffff) /* Mask for Delay-Time-to-Start in TSI */ +#define LWL_TSI_TR (0x1f000000) /* Time-Status-Info Transp-Mode */ +#define LWL_TSI_HM_NC (0x1f000000) /* Time-Status-Info Hist-Mode+No-Coinc */ +#define LWL_TSI_HM_C (0x0e000000) /* Time-Status-Info Hist-Mode+Coinc */ +#define LWL_TSI_TOF (0x1f000000) /* Time-Status-Info TOF-Mode */ +#define LWL_TSI_SM_NC (0x1f000000) /* Time-Status-Info Strobo-Mode+No-Coin */ +#define LWL_TSI_SM_C (0x0e000000) /* Time-Status-Info Strobo-Mode+Coinc */ +#define LWL_TSI_DT_MSK (0x000fffff) /* Mask for Dead-Time in TSI */ +#define LWL_TSI_DTS_MSK (0x000fffff) /* Mask for Delay-Time-to-Start in TSI */ -#define LWL_TR_C1 (0x00000001) /* Transp. Mode Chan 1 */ -#define LWL_TR_C2 (0x00000002) /* Transp. Mode Chan 2 */ -#define LWL_TR_C3 (0x00000003) /* Transp. Mode Chan 3 */ -#define LWL_TR_C4 (0x00000004) /* Transp. Mode Chan 4 */ -#define LWL_TR_C5 (0x00000005) /* Transp. Mode Chan 5 */ -#define LWL_TR_C6 (0x00000006) /* Transp. Mode Chan 6 */ -#define LWL_TR_C7 (0x00000007) /* Transp. Mode Chan 7 */ -#define LWL_TR_C8 (0x00000008) /* Transp. Mode Chan 8 */ -#define LWL_TR_C9 (0x00000009) /* Transp. Mode Chan 9 */ +#define LWL_TR_C1 (0x00000001) /* Transp. Mode Chan 1 */ +#define LWL_TR_C2 (0x00000002) /* Transp. Mode Chan 2 */ +#define LWL_TR_C3 (0x00000003) /* Transp. Mode Chan 3 */ +#define LWL_TR_C4 (0x00000004) /* Transp. Mode Chan 4 */ +#define LWL_TR_C5 (0x00000005) /* Transp. Mode Chan 5 */ +#define LWL_TR_C6 (0x00000006) /* Transp. Mode Chan 6 */ +#define LWL_TR_C7 (0x00000007) /* Transp. Mode Chan 7 */ +#define LWL_TR_C8 (0x00000008) /* Transp. Mode Chan 8 */ +#define LWL_TR_C9 (0x00000009) /* Transp. Mode Chan 9 */ -#define LWL_HM_NC (0x10000000) /* Hist-Mode/No-Coinc 0 chan dgrm hdr */ -#define LWL_HM_NC_C1 (0x11000000) /* Hist-Mode/No-Coinc 1 chan dgrm hdr */ -#define LWL_HM_NC_C2 (0x12000000) /* Hist-Mode/No-Coinc 2 chan dgrm hdr */ -#define LWL_HM_NC_C3 (0x13000000) /* Hist-Mode/No-Coinc 3 chan dgrm hdr */ -#define LWL_HM_NC_C4 (0x14000000) /* Hist-Mode/No-Coinc 4 chan dgrm hdr */ -#define LWL_HM_NC_C5 (0x15000000) /* Hist-Mode/No-Coinc 5 chan dgrm hdr */ -#define LWL_HM_NC_C6 (0x16000000) /* Hist-Mode/No-Coinc 6 chan dgrm hdr */ -#define LWL_HM_NC_C7 (0x17000000) /* Hist-Mode/No-Coinc 7 chan dgrm hdr */ -#define LWL_HM_NC_C8 (0x18000000) /* Hist-Mode/No-Coinc 8 chan dgrm hdr */ -#define LWL_HM_NC_C9 (0x19000000) /* Hist-Mode/No-Coinc 9 chan dgrm hdr */ +#define LWL_HM_NC (0x10000000) /* Hist-Mode/No-Coinc 0 chan dgrm hdr */ +#define LWL_HM_NC_C1 (0x11000000) /* Hist-Mode/No-Coinc 1 chan dgrm hdr */ +#define LWL_HM_NC_C2 (0x12000000) /* Hist-Mode/No-Coinc 2 chan dgrm hdr */ +#define LWL_HM_NC_C3 (0x13000000) /* Hist-Mode/No-Coinc 3 chan dgrm hdr */ +#define LWL_HM_NC_C4 (0x14000000) /* Hist-Mode/No-Coinc 4 chan dgrm hdr */ +#define LWL_HM_NC_C5 (0x15000000) /* Hist-Mode/No-Coinc 5 chan dgrm hdr */ +#define LWL_HM_NC_C6 (0x16000000) /* Hist-Mode/No-Coinc 6 chan dgrm hdr */ +#define LWL_HM_NC_C7 (0x17000000) /* Hist-Mode/No-Coinc 7 chan dgrm hdr */ +#define LWL_HM_NC_C8 (0x18000000) /* Hist-Mode/No-Coinc 8 chan dgrm hdr */ +#define LWL_HM_NC_C9 (0x19000000) /* Hist-Mode/No-Coinc 9 chan dgrm hdr */ -#define LWL_HM_CO (0x10000000) /* Hist-Mode+Coinc 0 chan dgrm hdr */ -#define LWL_HM_CO_C2 (0x12000000) /* Hist-Mode+Coinc 2 chan dgrm hdr */ -#define LWL_HM_CO_C3 (0x13000000) /* Hist-Mode+Coinc 3 chan dgrm hdr */ -#define LWL_HM_CO_C4 (0x14000000) /* Hist-Mode+Coinc 4 chan dgrm hdr */ -#define LWL_HM_CO_C5 (0x15000000) /* Hist-Mode+Coinc 5 chan dgrm hdr */ -#define LWL_HM_CO_C6 (0x16000000) /* Hist-Mode+Coinc 6 chan dgrm hdr */ -#define LWL_HM_CO_C7 (0x17000000) /* Hist-Mode+Coinc 7 chan dgrm hdr */ -#define LWL_HM_CO_C8 (0x18000000) /* Hist-Mode+Coinc 8 chan dgrm hdr */ -#define LWL_HM_CO_C9 (0x19000000) /* Hist-Mode+Coinc 9 chan dgrm hdr */ +#define LWL_HM_CO (0x10000000) /* Hist-Mode+Coinc 0 chan dgrm hdr */ +#define LWL_HM_CO_C2 (0x12000000) /* Hist-Mode+Coinc 2 chan dgrm hdr */ +#define LWL_HM_CO_C3 (0x13000000) /* Hist-Mode+Coinc 3 chan dgrm hdr */ +#define LWL_HM_CO_C4 (0x14000000) /* Hist-Mode+Coinc 4 chan dgrm hdr */ +#define LWL_HM_CO_C5 (0x15000000) /* Hist-Mode+Coinc 5 chan dgrm hdr */ +#define LWL_HM_CO_C6 (0x16000000) /* Hist-Mode+Coinc 6 chan dgrm hdr */ +#define LWL_HM_CO_C7 (0x17000000) /* Hist-Mode+Coinc 7 chan dgrm hdr */ +#define LWL_HM_CO_C8 (0x18000000) /* Hist-Mode+Coinc 8 chan dgrm hdr */ +#define LWL_HM_CO_C9 (0x19000000) /* Hist-Mode+Coinc 9 chan dgrm hdr */ -#define LWL_TOF_C1 (0x01000000) /* TOF-Mode 1 chan dgrm hdr */ -#define LWL_TOF_C2 (0x02000000) /* TOF-Mode 2 chan dgrm hdr */ -#define LWL_TOF_C3 (0x03000000) /* TOF-Mode 3 chan dgrm hdr */ -#define LWL_TOF_C4 (0x04000000) /* TOF-Mode 4 chan dgrm hdr */ -#define LWL_TOF_C5 (0x05000000) /* TOF-Mode 5 chan dgrm hdr */ -#define LWL_TOF_C6 (0x06000000) /* TOF-Mode 6 chan dgrm hdr */ -#define LWL_TOF_C7 (0x07000000) /* TOF-Mode 7 chan dgrm hdr */ -#define LWL_TOF_C8 (0x08000000) /* TOF-Mode 8 chan dgrm hdr */ -#define LWL_TOF_C9 (0x09000000) /* TOF-Mode 9 chan dgrm hdr */ +#define LWL_TOF_C1 (0x01000000) /* TOF-Mode 1 chan dgrm hdr */ +#define LWL_TOF_C2 (0x02000000) /* TOF-Mode 2 chan dgrm hdr */ +#define LWL_TOF_C3 (0x03000000) /* TOF-Mode 3 chan dgrm hdr */ +#define LWL_TOF_C4 (0x04000000) /* TOF-Mode 4 chan dgrm hdr */ +#define LWL_TOF_C5 (0x05000000) /* TOF-Mode 5 chan dgrm hdr */ +#define LWL_TOF_C6 (0x06000000) /* TOF-Mode 6 chan dgrm hdr */ +#define LWL_TOF_C7 (0x07000000) /* TOF-Mode 7 chan dgrm hdr */ +#define LWL_TOF_C8 (0x08000000) /* TOF-Mode 8 chan dgrm hdr */ +#define LWL_TOF_C9 (0x09000000) /* TOF-Mode 9 chan dgrm hdr */ #define LWL_PSD_TSI 0x0E000000 /* PSD-Mode TSI datagram */ -#define LWL_PSD_DATA 0x12000000 /* PSD-mode data datagram */ +#define LWL_PSD_DATA 0x12000000 /* PSD-mode data datagram */ #define LWL_PSD_PWF 0x20000000 /* PSD-mode Power Fail bit */ #define LWL_PSD_TIME 0x000fffff /* PSD-mode time stamp extraction mask */ @@ -226,258 +226,276 @@ #define LWL_PSD_XORF 0x2000 /* mask for TDC-XORF bit */ #define LWL_PSD_CONF 0x0100 /* mask for TDC-CONF flag */ -#define LWL_SM_NC (0x10000000) /* Strobo-Mode/No-Coinc 0 chan dgrm hdr */ -#define LWL_SM_NC_C1 (0x11000000) /* Strobo-Mode/No-Coinc 1 chan dgrm hdr */ -#define LWL_SM_NC_C2 (0x12000000) /* Strobo-Mode/No-Coinc 2 chan dgrm hdr */ -#define LWL_SM_NC_C3 (0x13000000) /* Strobo-Mode/No-Coinc 3 chan dgrm hdr */ -#define LWL_SM_NC_C4 (0x14000000) /* Strobo-Mode/No-Coinc 4 chan dgrm hdr */ -#define LWL_SM_NC_C5 (0x15000000) /* Strobo-Mode/No-Coinc 5 chan dgrm hdr */ -#define LWL_SM_NC_C6 (0x16000000) /* Strobo-Mode/No-Coinc 6 chan dgrm hdr */ -#define LWL_SM_NC_C7 (0x17000000) /* Strobo-Mode/No-Coinc 7 chan dgrm hdr */ -#define LWL_SM_NC_C8 (0x18000000) /* Strobo-Mode/No-Coinc 8 chan dgrm hdr */ -#define LWL_SM_NC_C9 (0x19000000) /* Strobo-Mode/No-Coinc 9 chan dgrm hdr */ +#define LWL_SM_NC (0x10000000) /* Strobo-Mode/No-Coinc 0 chan dgrm hdr */ +#define LWL_SM_NC_C1 (0x11000000) /* Strobo-Mode/No-Coinc 1 chan dgrm hdr */ +#define LWL_SM_NC_C2 (0x12000000) /* Strobo-Mode/No-Coinc 2 chan dgrm hdr */ +#define LWL_SM_NC_C3 (0x13000000) /* Strobo-Mode/No-Coinc 3 chan dgrm hdr */ +#define LWL_SM_NC_C4 (0x14000000) /* Strobo-Mode/No-Coinc 4 chan dgrm hdr */ +#define LWL_SM_NC_C5 (0x15000000) /* Strobo-Mode/No-Coinc 5 chan dgrm hdr */ +#define LWL_SM_NC_C6 (0x16000000) /* Strobo-Mode/No-Coinc 6 chan dgrm hdr */ +#define LWL_SM_NC_C7 (0x17000000) /* Strobo-Mode/No-Coinc 7 chan dgrm hdr */ +#define LWL_SM_NC_C8 (0x18000000) /* Strobo-Mode/No-Coinc 8 chan dgrm hdr */ +#define LWL_SM_NC_C9 (0x19000000) /* Strobo-Mode/No-Coinc 9 chan dgrm hdr */ -#define LWL_SM_CO (0x10000000) /* Strobo-Mode + Coinc 0 chan dgrm hdr */ -#define LWL_SM_CO_C1 (0x11000000) /* Strobo-Mode + Coinc 1 chan dgrm hdr */ -#define LWL_SM_CO_C2 (0x12000000) /* Strobo-Mode + Coinc 2 chan dgrm hdr */ -#define LWL_SM_CO_C3 (0x13000000) /* Strobo-Mode + Coinc 3 chan dgrm hdr */ -#define LWL_SM_CO_C4 (0x14000000) /* Strobo-Mode + Coinc 4 chan dgrm hdr */ -#define LWL_SM_CO_C5 (0x15000000) /* Strobo-Mode + Coinc 5 chan dgrm hdr */ -#define LWL_SM_CO_C6 (0x16000000) /* Strobo-Mode + Coinc 6 chan dgrm hdr */ -#define LWL_SM_CO_C7 (0x17000000) /* Strobo-Mode + Coinc 7 chan dgrm hdr */ -#define LWL_SM_CO_C8 (0x18000000) /* Strobo-Mode + Coinc 8 chan dgrm hdr */ -#define LWL_SM_CO_C9 (0x19000000) /* Strobo-Mode + Coinc 9 chan dgrm hdr */ +#define LWL_SM_CO (0x10000000) /* Strobo-Mode + Coinc 0 chan dgrm hdr */ +#define LWL_SM_CO_C1 (0x11000000) /* Strobo-Mode + Coinc 1 chan dgrm hdr */ +#define LWL_SM_CO_C2 (0x12000000) /* Strobo-Mode + Coinc 2 chan dgrm hdr */ +#define LWL_SM_CO_C3 (0x13000000) /* Strobo-Mode + Coinc 3 chan dgrm hdr */ +#define LWL_SM_CO_C4 (0x14000000) /* Strobo-Mode + Coinc 4 chan dgrm hdr */ +#define LWL_SM_CO_C5 (0x15000000) /* Strobo-Mode + Coinc 5 chan dgrm hdr */ +#define LWL_SM_CO_C6 (0x16000000) /* Strobo-Mode + Coinc 6 chan dgrm hdr */ +#define LWL_SM_CO_C7 (0x17000000) /* Strobo-Mode + Coinc 7 chan dgrm hdr */ +#define LWL_SM_CO_C8 (0x18000000) /* Strobo-Mode + Coinc 8 chan dgrm hdr */ +#define LWL_SM_CO_C9 (0x19000000) /* Strobo-Mode + Coinc 9 chan dgrm hdr */ -#define LWL_TSI_MODE_MASK (0x000e) /* Mask for mode in Time Status Info */ -#define LWL_TSI_MODE_TR (0x0000) /* TSI Transparent-Mode */ -#define LWL_TSI_MODE_HM (0x0002) /* TSI Hist-Mode */ -#define LWL_TSI_MODE_TOF (0x0004) /* TSI TOF-Mode */ -#define LWL_TSI_MODE_SM1 (0x0006) /* TSI Strobo-Mode 1 - time-stamp coded */ -#define LWL_TSI_MODE_TR_UD (0x0008) /* TSI Transparent-Mode Up-Down */ -#define LWL_TSI_MODE_HM_UD (0x000a) /* TSI Hist-Mode Up-Down */ -#define LWL_TSI_MODE_TOF_UD (0x000c) /* TSI TOF-Mode Up-Down */ -#define LWL_TSI_MODE_SM2 (0x000e) /* TSI Strobo-Mode 2 - h/w coded */ +#define LWL_TSI_MODE_MASK (0x000e) /* Mask for mode in Time Status Info */ +#define LWL_TSI_MODE_TR (0x0000) /* TSI Transparent-Mode */ +#define LWL_TSI_MODE_HM (0x0002) /* TSI Hist-Mode */ +#define LWL_TSI_MODE_TOF (0x0004) /* TSI TOF-Mode */ +#define LWL_TSI_MODE_SM1 (0x0006) /* TSI Strobo-Mode 1 - time-stamp coded */ +#define LWL_TSI_MODE_TR_UD (0x0008) /* TSI Transparent-Mode Up-Down */ +#define LWL_TSI_MODE_HM_UD (0x000a) /* TSI Hist-Mode Up-Down */ +#define LWL_TSI_MODE_TOF_UD (0x000c) /* TSI TOF-Mode Up-Down */ +#define LWL_TSI_MODE_SM2 (0x000e) /* TSI Strobo-Mode 2 - h/w coded */ /* **------------------------------------------------------------------------------ ** Define structure of a TOF histogram data item. */ - struct tof_histog { - int cntr_nmbr; /* Counter number */ - uint lo_edge; /* Low edge of first bin (20-bit value) */ - uint hi_edge; /* Top edge of last bin (20-bit value) */ - usint flag; /* Bit mask giving info on histog -- may be - ** used to help optimise the code */ - usint bytes_per_bin; /* Number of bytes in each histogram bin */ - uint n_bins; /* Number of bins in histogram */ - uint cnt_early_up; /* Count of early events (pol'n up) */ - uint cnt_late_up; /* Count of late events (pol'n up) */ - uint cnt_early_down; /* Count of early events (pol'n down) */ - uint cnt_late_down; /* Count of late events (pol'n down) */ - uint *bin_edge; /* Pointer to array of bin edges */ - union { /* Pointer to histogram array */ - uchar *b_bin_data; /* .. pointer if it's 8-bit bins */ - usint *w_bin_data; /* .. pointer if it's 16-bit bins */ - uint *l_bin_data; /* .. pointer if it's 32-bit bins */ - } u; - }; +struct tof_histog { + int cntr_nmbr; /* Counter number */ + uint lo_edge; /* Low edge of first bin (20-bit value) */ + uint hi_edge; /* Top edge of last bin (20-bit value) */ + usint flag; /* Bit mask giving info on histog -- may be + ** used to help optimise the code */ + usint bytes_per_bin; /* Number of bytes in each histogram bin */ + uint n_bins; /* Number of bins in histogram */ + uint cnt_early_up; /* Count of early events (pol'n up) */ + uint cnt_late_up; /* Count of late events (pol'n up) */ + uint cnt_early_down; /* Count of early events (pol'n down) */ + uint cnt_late_down; /* Count of late events (pol'n down) */ + uint *bin_edge; /* Pointer to array of bin edges */ + union { /* Pointer to histogram array */ + uchar *b_bin_data; /* .. pointer if it's 8-bit bins */ + usint *w_bin_data; /* .. pointer if it's 16-bit bins */ + uint *l_bin_data; /* .. pointer if it's 32-bit bins */ + } u; +}; /* Define a TOF 'edge-info' structure. This structure is created ** as a result of a TOF 'edge-array' in a SQHM__TOF config cmnd. */ - struct tof_edge_info { - uint n_bins; /* Number of bins in histogram */ - uint flag; /* Flag bits defining type of histo */ - uint bin_span; /* Time spanned by a histogram bin (20-bit - ** value) if bin width is constant. Otherwise - ** it is zero. */ - uint hi_edge; /* Top edge of last bin (20-bit value) */ - uint edges[2]; /* Array of edge data (20-bit values). There - ** are actually (n_bins+1) items in the array - ** and give the bottom edges of the bin */ - }; +struct tof_edge_info { + uint n_bins; /* Number of bins in histogram */ + uint flag; /* Flag bits defining type of histo */ + uint bin_span; /* Time spanned by a histogram bin (20-bit + ** value) if bin width is constant. Otherwise + ** it is zero. */ + uint hi_edge; /* Top edge of last bin (20-bit value) */ + uint edges[2]; /* Array of edge data (20-bit values). There + ** are actually (n_bins+1) items in the array + ** and give the bottom edges of the bin */ +}; /* Define structure of a TOF 'edge-array' in SQHM__TOF config cmnd */ - struct tof_edge_arr { - uint n_bins; /* Number of bins in histogram */ - uint flag; /* Flag (0/1) for fixed/variable bin size */ - uint *edges; /* Array of bottom edges (20-bit values) */ - }; +struct tof_edge_arr { + uint n_bins; /* Number of bins in histogram */ + uint flag; /* Flag (0/1) for fixed/variable bin size */ + uint *edges; /* Array of bottom edges (20-bit values) */ +}; /* Define structure of a TOF 'bank' in SQHM__TOF config command */ - struct tof_bank { - usint first; /* Number of first counter in bank */ - usint n_cntrs; /* Number of counters in bank */ - usint edge_indx; /* Index of edge array */ - usint bytes_per_bin; /* Number of bytes per bin */ - }; +struct tof_bank { + usint first; /* Number of first counter in bank */ + usint n_cntrs; /* Number of counters in bank */ + usint edge_indx; /* Index of edge array */ + usint bytes_per_bin; /* Number of bytes per bin */ +}; /* **------------------------------------------------------------------------------ ** Define command structure. */ - struct req_buff_struct { /* For messages to SinqHM */ - uint bigend; - uint cmnd; +struct req_buff_struct { /* For messages to SinqHM */ + uint bigend; + uint cmnd; + union { + char filler[56]; + + struct { + uint max_pkt, strt_mode; + } cnct; + + struct { + uint mode; union { - char filler[56]; - - struct {uint max_pkt, - strt_mode;} cnct; - - struct {uint mode; - union { - struct { - uint n_buffs; - uint n_bytes; - } trans; - struct { - uint n_hists; - uint lo_bin; - uint num_bins; - uint bytes_per_bin; - uint compress; - } hm_dig; - struct { - uint n_extra_bytes; - usint n_edges; - usint n_banks; - uint preset_delay; - struct tof_edge_arr edge_0; - struct tof_bank bank_0; - } tof; - struct { - uint n_extra_bytes; - usint n_edges; - usint n_banks; - uint preset_delay; - usint xFactor; - usint yFactor; - usint xOffset; - usint yOffset; - usint xSize; - usint ySize; - struct tof_edge_arr edge_0; - struct tof_bank bank_0; - } psd; - } u; - } cnfg; - - struct {uint mask;} dbg; - - struct {uint sub_code;} decnfg; - - struct {uint sub_cmnd;} daq; - - struct {uint sub_code, - x_lo, - nx, - y_lo, - ny, - xdim, - nhist;} project; - - struct {uint hist_no, - first_bin, - n_bins;} read; - - struct {uint hist_no;} select; - - struct {uint hist_no, - first_bin, - n_bins, - bytes_per_bin;} write; - - struct {uint hist_no, - first_bin, - n_bins;} zero; + struct { + uint n_buffs; + uint n_bytes; + } trans; + struct { + uint n_hists; + uint lo_bin; + uint num_bins; + uint bytes_per_bin; + uint compress; + } hm_dig; + struct { + uint n_extra_bytes; + usint n_edges; + usint n_banks; + uint preset_delay; + struct tof_edge_arr edge_0; + struct tof_bank bank_0; + } tof; + struct { + uint n_extra_bytes; + usint n_edges; + usint n_banks; + uint preset_delay; + usint xFactor; + usint yFactor; + usint xOffset; + usint yOffset; + usint xSize; + usint ySize; + struct tof_edge_arr edge_0; + struct tof_bank bank_0; + } psd; } u; - }; + } cnfg; + + struct { + uint mask; + } dbg; + + struct { + uint sub_code; + } decnfg; + + struct { + uint sub_cmnd; + } daq; + + struct { + uint sub_code, x_lo, nx, y_lo, ny, xdim, nhist; + } project; + + struct { + uint hist_no, first_bin, n_bins; + } read; + + struct { + uint hist_no; + } select; + + struct { + uint hist_no, first_bin, n_bins, bytes_per_bin; + } write; + + struct { + uint hist_no, first_bin, n_bins; + } zero; + } u; +}; /* **------------------------------------------------------------------------------ ** Define status response structure. */ - struct rply_buff_struct { /* For messages from SinqHM */ - uint bigend; - uint status; - uint sub_status; +struct rply_buff_struct { /* For messages from SinqHM */ + uint bigend; + uint status; + uint sub_status; + union { + char message[52]; + + struct { + uint port; + uint pkt_size; + uint hm_mode; + uint n_hists; + uint num_bins; + uint bytes_per_bin; + uint curr_hist; + uint max_block; + uint total_bytes; + uint lo_cntr; + uint lo_bin; + uint compress; + uint up_time; + } cnct; + + struct { + usint daq_now; + usint daq_was; + usint filler_mask; + usint server_mask; + } daq; + + struct { + uint n_extra_bytes; + uint up_time; + usint offset_vxWorks_ident; + usint offset_vxWorks_date; + usint offset_instr; + usint offset_def_ident; + usint offset_sinqhm_main_ident; + usint offset_sinqhm_main_date; + usint offset_sinqhm_server_ident; + usint offset_sinqhm_server_date; + usint offset_sinqhm_filler_ident; + usint offset_sinqhm_filler_date; + usint offset_sinqhm_routines_ident; + usint offset_sinqhm_routines_date; + } ident; + + struct { + uint n_bins; + uint bytes_per_bin; + uint cnts_lo; + uint cnts_hi; + } project; + + struct { + uint first_bin; + uint n_bins; + uint bytes_per_bin; + uint cnts_lo; + uint cnts_hi; + } read; + + struct { + uint cfg_state; + usint n_hists, curr_hist; + uint num_bins; + uint max_n_hists; + uint max_num_bins; + uchar max_srvrs, act_srvrs, bytes_per_bin, compress; + usint daq_now, filler_mask; + uint max_block; + usint tsi_status, flags; union { - char message[52]; - - struct {uint port; - uint pkt_size; - uint hm_mode; - uint n_hists; - uint num_bins; - uint bytes_per_bin; - uint curr_hist; - uint max_block; - uint total_bytes; - uint lo_cntr; - uint lo_bin; - uint compress; - uint up_time;} cnct; - - struct {usint daq_now; - usint daq_was; - usint filler_mask; - usint server_mask;} daq; - - struct {uint n_extra_bytes; - uint up_time; - usint offset_vxWorks_ident; - usint offset_vxWorks_date; - usint offset_instr; - usint offset_def_ident; - usint offset_sinqhm_main_ident; - usint offset_sinqhm_main_date; - usint offset_sinqhm_server_ident; - usint offset_sinqhm_server_date; - usint offset_sinqhm_filler_ident; - usint offset_sinqhm_filler_date; - usint offset_sinqhm_routines_ident; - usint offset_sinqhm_routines_date;} ident; - - struct {uint n_bins; - uint bytes_per_bin; - uint cnts_lo; - uint cnts_hi;} project; - - struct {uint first_bin; - uint n_bins; - uint bytes_per_bin; - uint cnts_lo; - uint cnts_hi;} read; - - struct {uint cfg_state; - usint n_hists, curr_hist; - uint num_bins; - uint max_n_hists; - uint max_num_bins; - uchar max_srvrs, act_srvrs, bytes_per_bin, compress; - usint daq_now, filler_mask; - uint max_block; - usint tsi_status, flags; - union { - uint dead_time; - uint dts; - uint both; - } dt_or_dts; - uint num_bad_events; - uint up_time;} status; - } u; - }; + uint dead_time; + uint dts; + uint both; + } dt_or_dts; + uint num_bad_events; + uint up_time; + } status; + } u; +}; /* **------------------------------------------------------------------------------ ** Define structure of message to SinqHM-filler. */ - struct msg_to_filler_struct { /* For messages to SinqHM-filler */ - union { - char message[32]; /* Ensure buffer is 32 bytes total */ - struct { - uint cmnd; - uint index; - usint new_mask;} uu; - } u; - }; +struct msg_to_filler_struct { /* For messages to SinqHM-filler */ + union { + char message[32]; /* Ensure buffer is 32 bytes total */ + struct { + uint cmnd; + uint index; + usint new_mask; + } uu; + } u; +}; /*======================================================= End of SinqHM_def.h */ diff --git a/hardsup/stredit.c b/hardsup/stredit.c index 27b61c4..724bf22 100644 --- a/hardsup/stredit.c +++ b/hardsup/stredit.c @@ -163,253 +163,324 @@ ** strncpy to be sure result is always ** null terminated. */ - char *StrEdit ( +char *StrEdit( /* ======= -*/ char *out, - char *in, - char *ctrl, - int *ln) { +*/ char *out, + char *in, char *ctrl, int *ln) +{ - int i, j, k, l, m, len, inxt, out_size; - char my_ctrl[80]; - char *tok_nxt, *my_in, *my_out, *my_tmp, *nxt; - int do_collapse, do_compress, do_lowercase, do_trim; - int do_uncomment, do_upcase; + int i, j, k, l, m, len, inxt, out_size; + char my_ctrl[80]; + char *tok_nxt, *my_in, *my_out, *my_tmp, *nxt; + int do_collapse, do_compress, do_lowercase, do_trim; + int do_uncomment, do_upcase; - out_size = *ln; - if (out_size < 1) {*ln = 0; return NULL;} /* Can't do anything!! */ - - if (strlen (in) <= 0) { - *out = NIL; *ln = 0; return out; /* Nothing to do!! */ - } - /* - ** Scan ctrl looking to see what has to be done. Do this by first - ** taking a copy of it (in case it is declared "const" in the calling - ** routine, convert to lowercase and split into tokens at any space, - ** tab or comma. - */ - len = strlen (ctrl); - if (len >= sizeof (my_ctrl)) { - *out = NIL; *ln = 0; return NULL; - } - for (i = 0; i <= len; i++) my_ctrl[i] = tolower (ctrl[i]); - - do_collapse = do_compress = do_lowercase = do_trim = do_uncomment = - do_upcase = False; - tok_nxt = strtok (my_ctrl, ", \t\f\v\n"); - while (tok_nxt != NULL) { - if (strcmp (tok_nxt, "collapse") == 0) { - do_collapse = True; - }else if (strcmp (tok_nxt, "compress") == 0) { - do_compress = True; - }else if (strcmp (tok_nxt, "lowercase") == 0) { - do_lowercase = True; - }else if (strcmp (tok_nxt, "trim") == 0) { - do_trim = True; - }else if (strcmp (tok_nxt, "uncomment") == 0) { - do_uncomment = True; - }else if (strcmp (tok_nxt, "upcase") == 0) { - do_upcase = True; - }else { - *out = NIL; *ln = 0; return NULL; /* Illegal ctrl verb */ - } - tok_nxt = strtok (NULL, ", \t\f\v\n"); - } - - len = strlen (in) + 1; - my_in = malloc (len); /* Get some working space */ - if (my_in == NULL) { - *out = NIL; *ln = 0; return NULL; - } - /* - ** Copy "in" to the "my_in" working space, processing any '\' escape - ** sequences as we go. Note that, since "my_in" is big enough to hold - ** "in" and the escape sequence processing can only shorten the length - ** of "in", there's no need to check for an overflow of "my_in". Any - ** non-escaped double quotes are converted to something special so - ** that they can be recognised at the editing stage. - */ - nxt = my_in; - while (*in != '\0') { - if (*in == '\\') { /* Look for escape sequence */ - in++; - switch (*in) { - case 'a': case 'A': *nxt++ = '\007'; in++; break; - case 'b': case 'B': *nxt++ = '\010'; in++; break; - case 'f': case 'F': *nxt++ = '\014'; in++; break; - case 'n': case 'N': *nxt++ = '\012'; in++; break; - case 'r': case 'R': *nxt++ = '\015'; in++; break; - case 't': case 'T': *nxt++ = '\011'; in++; break; - case 'v': case 'V': *nxt++ = '\013'; in++; break; - case '\\': *nxt++ = '\\'; in++; break; - case '\'': *nxt++ = '\''; in++; break; - case '\"': *nxt++ = '\"'; in++; break; - case '\?': *nxt++ = '\?'; in++; break; - case 'x': case 'X': - in++; - i = strspn (in, "0123456789abcdefABCDEF"); - if (i > 0) { - *nxt++ = strtol (in, &in, 16); break; - }else { - *nxt++ = '\\'; break; - } - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - i = strspn (in, "01234567"); - if (i > 3) { - sscanf (in, "%3o", &j); - *nxt++ = j; - in += 3; - break; - }else if (i > 0) { - sscanf (in, "%o", &j); - *nxt++ = j; - in += i; - break; - }else { - *nxt++ = '\\'; - break; - } - default: - *nxt++ = '\\'; /* Invalid esc sequ - just copy it */ - } - }else if (*in == '\"') { /* Look for non-escaped double quotes */ - *nxt++ = QUOTE; *in++; /* Make it something unlikely */ - }else { - *nxt++ = *in++; - } - } - *nxt = '\0'; - - my_out = malloc (len); /* Get some working space */ - if (my_out == NULL) { - free (my_in); *out = NIL; *ln = 0; return NULL; - } - *my_out = NIL; - - my_tmp = malloc (len); /* Get some working space */ - if (my_tmp == NULL) { - free (my_out); free (my_in); - *out = NIL; *ln = 0; return NULL; - } - *my_tmp = NIL; - *out = NIL; - /* - ** Ensure "in" has an even number of non-escaped quotes. Return if not. - */ - i = 0; - for (j = 0; my_in[j] != NIL; j++) if (my_in[j] == QUOTE) i++; - if ((i & 1) == 1) { - free (my_tmp); - free (my_out); - free (my_in); - *ln = strlen (out); - return NULL; - } - /* - ** Scan through "in", substring by substring, to - ** handle quotation marks correctly. - */ - inxt = 0; - while (my_in[inxt] != NIL) { - if (my_in[inxt] == QUOTE) { /* Is there a quoted string next? */ - nxt = strchr (&my_in[inxt+1], QUOTE); /* Yes, find matching quote. */ - j = nxt - &my_in[inxt+1]; - memcpy (my_tmp, &my_in[inxt+1], j); /* Make copy of it */ - my_tmp[j] = NIL; - inxt = inxt + j + 2; - }else { - nxt = strchr (&my_in[inxt], QUOTE); /* Not a quoted string; .. - ** .. find next non-escaped .. - ** .. quote. - */ - if (nxt != NULL) { - j = nxt - my_in - inxt; - }else { - j = strlen (&my_in[inxt]); - } - memcpy (my_tmp, &my_in[inxt], j); /* Make copy for us to work on */ - my_tmp[j] = NIL; - inxt = inxt + j; - /* - ** For collapse and compress, start by turning all white space - ** chars to spaces. - */ - if (do_collapse || do_compress) { - for (k = 0; my_tmp[k] != NIL; k++) { - if (my_tmp[k] == '\t') my_tmp[k] = ' '; - if (my_tmp[k] == '\f') my_tmp[k] = ' '; - if (my_tmp[k] == '\v') my_tmp[k] = ' '; - if (my_tmp[k] == '\n') my_tmp[k] = ' '; - } - if (do_collapse) { - l = 0; - for (k = 0; my_tmp[k] != NIL; k++) { - if (my_tmp[k] != ' ') { - my_tmp[l] = my_tmp[k]; - l++; - } - } - my_tmp[l] = NIL; - }else if (do_compress) { - for (k = 0; my_tmp[k] != NIL; k++) { - if (my_tmp[k] == ' ') { - l = strspn (&my_tmp[k], " "); - if (l > 1) { - for (m = 0; my_tmp[k+l+m] != NIL; m++) { - my_tmp[k+m+1] = my_tmp[k+l+m]; - } - my_tmp[k+m+1] = NIL; - } - } - } - } - } - if (do_lowercase) { - for (k = 0; my_tmp[k] != NIL; k++) my_tmp[k] = _tolower (my_tmp[k]); - } - if (do_upcase) { - for (k = 0; my_tmp[k] != NIL; k++) my_tmp[k] = _toupper (my_tmp[k]); - } - if (do_uncomment) { - nxt = strchr (my_tmp, '!'); - if (nxt != NULL) { - *nxt = NIL; /* Truncate the string at the "!" */ - my_in[inxt] = NIL; /* Stop processing loop too */ - } - } - } - StrJoin (out, out_size, my_out, my_tmp); - strcpy (my_out, out); - } - - if (do_trim) { - i = strspn (my_out, " "); - if (i == strlen (my_out)) { /* If all spaces, result is a null string */ - *out = NIL; - }else { - for (j = strlen (my_out); my_out[j-1] == ' '; j--); - my_out[j] = NIL; - } - strcpy (out, &my_out[i]); - } - free (my_tmp); - free (my_out); - free (my_in); - *ln = strlen (out); - /* - ** Undo any encoded escape characters. - */ - for (i = 0; out[i] != NIL; i++) { - if (out[i] == ~'\"') out[i] = '\"'; - } - - return out; + out_size = *ln; + if (out_size < 1) { + *ln = 0; + return NULL; } + /* Can't do anything!! */ + if (strlen(in) <= 0) { + *out = NIL; + *ln = 0; + return out; /* Nothing to do!! */ + } + /* + ** Scan ctrl looking to see what has to be done. Do this by first + ** taking a copy of it (in case it is declared "const" in the calling + ** routine, convert to lowercase and split into tokens at any space, + ** tab or comma. + */ + len = strlen(ctrl); + if (len >= sizeof(my_ctrl)) { + *out = NIL; + *ln = 0; + return NULL; + } + for (i = 0; i <= len; i++) + my_ctrl[i] = tolower(ctrl[i]); + + do_collapse = do_compress = do_lowercase = do_trim = do_uncomment = + do_upcase = False; + tok_nxt = strtok(my_ctrl, ", \t\f\v\n"); + while (tok_nxt != NULL) { + if (strcmp(tok_nxt, "collapse") == 0) { + do_collapse = True; + } else if (strcmp(tok_nxt, "compress") == 0) { + do_compress = True; + } else if (strcmp(tok_nxt, "lowercase") == 0) { + do_lowercase = True; + } else if (strcmp(tok_nxt, "trim") == 0) { + do_trim = True; + } else if (strcmp(tok_nxt, "uncomment") == 0) { + do_uncomment = True; + } else if (strcmp(tok_nxt, "upcase") == 0) { + do_upcase = True; + } else { + *out = NIL; + *ln = 0; + return NULL; /* Illegal ctrl verb */ + } + tok_nxt = strtok(NULL, ", \t\f\v\n"); + } + + len = strlen(in) + 1; + my_in = malloc(len); /* Get some working space */ + if (my_in == NULL) { + *out = NIL; + *ln = 0; + return NULL; + } + /* + ** Copy "in" to the "my_in" working space, processing any '\' escape + ** sequences as we go. Note that, since "my_in" is big enough to hold + ** "in" and the escape sequence processing can only shorten the length + ** of "in", there's no need to check for an overflow of "my_in". Any + ** non-escaped double quotes are converted to something special so + ** that they can be recognised at the editing stage. + */ + nxt = my_in; + while (*in != '\0') { + if (*in == '\\') { /* Look for escape sequence */ + in++; + switch (*in) { + case 'a': + case 'A': + *nxt++ = '\007'; + in++; + break; + case 'b': + case 'B': + *nxt++ = '\010'; + in++; + break; + case 'f': + case 'F': + *nxt++ = '\014'; + in++; + break; + case 'n': + case 'N': + *nxt++ = '\012'; + in++; + break; + case 'r': + case 'R': + *nxt++ = '\015'; + in++; + break; + case 't': + case 'T': + *nxt++ = '\011'; + in++; + break; + case 'v': + case 'V': + *nxt++ = '\013'; + in++; + break; + case '\\': + *nxt++ = '\\'; + in++; + break; + case '\'': + *nxt++ = '\''; + in++; + break; + case '\"': + *nxt++ = '\"'; + in++; + break; + case '\?': + *nxt++ = '\?'; + in++; + break; + case 'x': + case 'X': + in++; + i = strspn(in, "0123456789abcdefABCDEF"); + if (i > 0) { + *nxt++ = strtol(in, &in, 16); + break; + } else { + *nxt++ = '\\'; + break; + } + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + i = strspn(in, "01234567"); + if (i > 3) { + sscanf(in, "%3o", &j); + *nxt++ = j; + in += 3; + break; + } else if (i > 0) { + sscanf(in, "%o", &j); + *nxt++ = j; + in += i; + break; + } else { + *nxt++ = '\\'; + break; + } + default: + *nxt++ = '\\'; /* Invalid esc sequ - just copy it */ + } + } else if (*in == '\"') { /* Look for non-escaped double quotes */ + *nxt++ = QUOTE; + *in++; /* Make it something unlikely */ + } else { + *nxt++ = *in++; + } + } + *nxt = '\0'; + + my_out = malloc(len); /* Get some working space */ + if (my_out == NULL) { + free(my_in); + *out = NIL; + *ln = 0; + return NULL; + } + *my_out = NIL; + + my_tmp = malloc(len); /* Get some working space */ + if (my_tmp == NULL) { + free(my_out); + free(my_in); + *out = NIL; + *ln = 0; + return NULL; + } + *my_tmp = NIL; + *out = NIL; + /* + ** Ensure "in" has an even number of non-escaped quotes. Return if not. + */ + i = 0; + for (j = 0; my_in[j] != NIL; j++) + if (my_in[j] == QUOTE) + i++; + if ((i & 1) == 1) { + free(my_tmp); + free(my_out); + free(my_in); + *ln = strlen(out); + return NULL; + } + /* + ** Scan through "in", substring by substring, to + ** handle quotation marks correctly. + */ + inxt = 0; + while (my_in[inxt] != NIL) { + if (my_in[inxt] == QUOTE) { /* Is there a quoted string next? */ + nxt = strchr(&my_in[inxt + 1], QUOTE); /* Yes, find matching quote. */ + j = nxt - &my_in[inxt + 1]; + memcpy(my_tmp, &my_in[inxt + 1], j); /* Make copy of it */ + my_tmp[j] = NIL; + inxt = inxt + j + 2; + } else { + nxt = strchr(&my_in[inxt], QUOTE); /* Not a quoted string; .. + ** .. find next non-escaped .. + ** .. quote. + */ + if (nxt != NULL) { + j = nxt - my_in - inxt; + } else { + j = strlen(&my_in[inxt]); + } + memcpy(my_tmp, &my_in[inxt], j); /* Make copy for us to work on */ + my_tmp[j] = NIL; + inxt = inxt + j; + /* + ** For collapse and compress, start by turning all white space + ** chars to spaces. + */ + if (do_collapse || do_compress) { + for (k = 0; my_tmp[k] != NIL; k++) { + if (my_tmp[k] == '\t') + my_tmp[k] = ' '; + if (my_tmp[k] == '\f') + my_tmp[k] = ' '; + if (my_tmp[k] == '\v') + my_tmp[k] = ' '; + if (my_tmp[k] == '\n') + my_tmp[k] = ' '; + } + if (do_collapse) { + l = 0; + for (k = 0; my_tmp[k] != NIL; k++) { + if (my_tmp[k] != ' ') { + my_tmp[l] = my_tmp[k]; + l++; + } + } + my_tmp[l] = NIL; + } else if (do_compress) { + for (k = 0; my_tmp[k] != NIL; k++) { + if (my_tmp[k] == ' ') { + l = strspn(&my_tmp[k], " "); + if (l > 1) { + for (m = 0; my_tmp[k + l + m] != NIL; m++) { + my_tmp[k + m + 1] = my_tmp[k + l + m]; + } + my_tmp[k + m + 1] = NIL; + } + } + } + } + } + if (do_lowercase) { + for (k = 0; my_tmp[k] != NIL; k++) + my_tmp[k] = _tolower(my_tmp[k]); + } + if (do_upcase) { + for (k = 0; my_tmp[k] != NIL; k++) + my_tmp[k] = _toupper(my_tmp[k]); + } + if (do_uncomment) { + nxt = strchr(my_tmp, '!'); + if (nxt != NULL) { + *nxt = NIL; /* Truncate the string at the "!" */ + my_in[inxt] = NIL; /* Stop processing loop too */ + } + } + } + StrJoin(out, out_size, my_out, my_tmp); + strcpy(my_out, out); + } + + if (do_trim) { + i = strspn(my_out, " "); + if (i == strlen(my_out)) { /* If all spaces, result is a null string */ + *out = NIL; + } else { + for (j = strlen(my_out); my_out[j - 1] == ' '; j--); + my_out[j] = NIL; + } + strcpy(out, &my_out[i]); + } + free(my_tmp); + free(my_out); + free(my_in); + *ln = strlen(out); + /* + ** Undo any encoded escape characters. + */ + for (i = 0; out[i] != NIL; i++) { + if (out[i] == ~'\"') + out[i] = '\"'; + } + + return out; +} + /*-------------------------------------------------- End of StrEdit.C -------*/ diff --git a/hardsup/strjoin.c b/hardsup/strjoin.c index 88a8b72..c22a691 100644 --- a/hardsup/strjoin.c +++ b/hardsup/strjoin.c @@ -89,54 +89,57 @@ ** strncpy to be sure result is always ** null terminated. */ - char *StrJoin ( +char *StrJoin( /* ======= -*/ char *result, - int result_size, - char *str_a, - char *str_b) { +*/ char *result, + int result_size, char *str_a, char *str_b) +{ - int i, size, size_a, size_b; + int i, size, size_a, size_b; - size = result_size - 1; + size = result_size - 1; - if (size < 0) return result; - - if (result == str_a) { /* Are the result and str_a the same? */ - size_a = strlen (str_a); /* Yes */ - if (size_a > size) { /* Check sizes anyway. */ - result[size] = NIL; /* Truncate str_a. No room for str_b! */ - }else { - size = size - strlen (result); /* And append str_b */ - if (size > 0) { - strncat (result, str_b, size); - } - } - }else if (result == str_b) { /* Are the result and str_b the same? */ - size_a = strlen (str_a); /* Yes, this is a bit complicated! */ - size_b = strlen (str_b); - if (size_a >= size) { /* If str_a completely fills result, .. */ - result[0] = NIL; /* .. then just copy in str_a */ - strncat (result, str_a, size); - }else { - /* - ** Otherwise, str_b must first be moved to - ** make room for str_a and then str_a must - ** be put at the front of the result. - */ - if ((size_a + size_b) > size) size_b = size - size_a; - result[size_a+size_b] = NIL; - for (i = (size_b-1); i >= 0; i--) { - result[size_a+i] = str_b[i]; - } - memcpy (result, str_a, size_a); - } - }else { /* Result is neither str_a nor str_b so .. */ - result[0] = NIL; /* .. str_a needs to be copied */ - strncat (result, str_a, size); - size = size - strlen (result); /* And str_a appended */ - if (size > 0) strncat (result, str_b, size); - } + if (size < 0) return result; + + if (result == str_a) { /* Are the result and str_a the same? */ + size_a = strlen(str_a); /* Yes */ + if (size_a > size) { /* Check sizes anyway. */ + result[size] = NIL; /* Truncate str_a. No room for str_b! */ + } else { + size = size - strlen(result); /* And append str_b */ + if (size > 0) { + strncat(result, str_b, size); + } + } + } else if (result == str_b) { /* Are the result and str_b the same? */ + size_a = strlen(str_a); /* Yes, this is a bit complicated! */ + size_b = strlen(str_b); + if (size_a >= size) { /* If str_a completely fills result, .. */ + result[0] = NIL; /* .. then just copy in str_a */ + strncat(result, str_a, size); + } else { + /* + ** Otherwise, str_b must first be moved to + ** make room for str_a and then str_a must + ** be put at the front of the result. + */ + if ((size_a + size_b) > size) + size_b = size - size_a; + result[size_a + size_b] = NIL; + for (i = (size_b - 1); i >= 0; i--) { + result[size_a + i] = str_b[i]; + } + memcpy(result, str_a, size_a); + } + } else { /* Result is neither str_a nor str_b so .. */ + result[0] = NIL; /* .. str_a needs to be copied */ + strncat(result, str_a, size); + size = size - strlen(result); /* And str_a appended */ + if (size > 0) + strncat(result, str_b, size); } + return result; +} + /*-------------------------------------------------- End of STRJOIN.C =======*/ diff --git a/hardsup/table.c b/hardsup/table.c index 41e1b9c..949a457 100644 --- a/hardsup/table.c +++ b/hardsup/table.c @@ -12,165 +12,149 @@ #include "table.h" /*-------------------------------------------------------------------------*/ - typedef struct __SicsTable { - float *fVal1; - float *fVal2; - int iLength; - } STable; -/*-------------------------------------------------------------------------*/ - pSTable CreateTable(FILE *fd) - { - pSTable pNew = NULL; - long lStart, lEnd, lData, i; - char *pBuffer = NULL, *pEnd = NULL, *pEndLine, *pPtr; - int iLength, iRet; - float fVal1, fVal2; - - assert(fd); - - /* find length of file, create a buffer and read it in */ - lStart = ftell(fd); - fseek(fd,0L,SEEK_END); - lEnd = ftell(fd); - lData = lEnd - lStart; - pBuffer = (char *)malloc(lData*sizeof(char)); - if(!pBuffer) - { - return NULL; - } - fseek(fd,lStart,SEEK_SET); - fread(pBuffer,sizeof(char),lData,fd); - - /* find number of lines */ - for(i = 0, iLength = 0; i < lData; i++) - { - if(pBuffer[i] == '\n') - { - iLength++; - } - } - - /* allocate the table structure */ - pNew = (pSTable)malloc(sizeof(STable)); - if(!pNew) - { - free(pBuffer); - return NULL; - } - pNew->iLength = iLength; - pNew->fVal1 = (float *)malloc(sizeof(float)*iLength); - pNew->fVal2 = (float *)malloc(sizeof(float)*iLength); - if( (!pNew->fVal1) || (!pNew->fVal2)) - { - free(pBuffer); - free(pNew); - return NULL; - } - memset(pNew->fVal1,0,iLength*sizeof(float)); - memset(pNew->fVal2,0,iLength*sizeof(float)); - - /* dodge through the file reading pairs until end */ - pPtr = pBuffer; - pEnd = pBuffer + lData; - pEndLine = pBuffer; - i = 0; - while(pEndLine < pEnd) - { - if(*pEndLine == '\n') - { - *pEndLine = '\0'; - iRet = sscanf(pPtr,"%f %f",&fVal1, &fVal2); - if(iRet == 2) - { - pNew->fVal1[i] = fVal1; - pNew->fVal2[i] = fVal2; - i++; - } - pEndLine++; - pPtr = pEndLine; - } - else - { - pEndLine++; - } - } - - free(pBuffer); - return pNew; +typedef struct __SicsTable { + float *fVal1; + float *fVal2; + int iLength; +} STable; +/*-------------------------------------------------------------------------*/ +pSTable CreateTable(FILE * fd) +{ + pSTable pNew = NULL; + long lStart, lEnd, lData, i; + char *pBuffer = NULL, *pEnd = NULL, *pEndLine, *pPtr; + int iLength, iRet; + float fVal1, fVal2; + + assert(fd); + + /* find length of file, create a buffer and read it in */ + lStart = ftell(fd); + fseek(fd, 0L, SEEK_END); + lEnd = ftell(fd); + lData = lEnd - lStart; + pBuffer = (char *) malloc(lData * sizeof(char)); + if (!pBuffer) { + return NULL; } -/*--------------------------------------------------------------------------*/ - void DeleteTable(pSTable self) - { - if(self->fVal1) - { - free(self->fVal1); - } - if(self->fVal2) - { - free(self->fVal2); - } - free(self); - } -/*--------------------------------------------------------------------------*/ - int InterpolateVal1(pSTable self, float fKey, float *fResult) - { - float fFrac; - int i1,i; - - assert(self); - assert(self->fVal1); - assert(self->fVal2); - - /* search the entry point */ - for(i = 0; i < self->iLength; i++) - { - if(self->fVal1[i] >= fKey) - { - i1 = i; - break; - } - } - if(i1 >= self->iLength) - { - return 0; - } + fseek(fd, lStart, SEEK_SET); + fread(pBuffer, sizeof(char), lData, fd); + + /* find number of lines */ + for (i = 0, iLength = 0; i < lData; i++) { + if (pBuffer[i] == '\n') { + iLength++; + } + } + + /* allocate the table structure */ + pNew = (pSTable) malloc(sizeof(STable)); + if (!pNew) { + free(pBuffer); + return NULL; + } + pNew->iLength = iLength; + pNew->fVal1 = (float *) malloc(sizeof(float) * iLength); + pNew->fVal2 = (float *) malloc(sizeof(float) * iLength); + if ((!pNew->fVal1) || (!pNew->fVal2)) { + free(pBuffer); + free(pNew); + return NULL; + } + memset(pNew->fVal1, 0, iLength * sizeof(float)); + memset(pNew->fVal2, 0, iLength * sizeof(float)); + + /* dodge through the file reading pairs until end */ + pPtr = pBuffer; + pEnd = pBuffer + lData; + pEndLine = pBuffer; + i = 0; + while (pEndLine < pEnd) { + if (*pEndLine == '\n') { + *pEndLine = '\0'; + iRet = sscanf(pPtr, "%f %f", &fVal1, &fVal2); + if (iRet == 2) { + pNew->fVal1[i] = fVal1; + pNew->fVal2[i] = fVal2; + i++; + } + pEndLine++; + pPtr = pEndLine; + } else { + pEndLine++; + } + } + + free(pBuffer); + return pNew; +} + +/*--------------------------------------------------------------------------*/ +void DeleteTable(pSTable self) +{ + if (self->fVal1) { + free(self->fVal1); + } + if (self->fVal2) { + free(self->fVal2); + } + free(self); +} + +/*--------------------------------------------------------------------------*/ +int InterpolateVal1(pSTable self, float fKey, float *fResult) +{ + float fFrac; + int i1, i; + + assert(self); + assert(self->fVal1); + assert(self->fVal2); + + /* search the entry point */ + for (i = 0; i < self->iLength; i++) { + if (self->fVal1[i] >= fKey) { + i1 = i; + break; + } + } + if (i1 >= self->iLength) { + return 0; + } + + /* interpolate */ + fFrac = (fKey - self->fVal1[i1 - 1]) + / (self->fVal1[i1] - self->fVal1[i1 - 1]); + *fResult = self->fVal2[i1 - 1] + + fFrac * (self->fVal2[i1] - self->fVal2[i1 - 1]); + return 1; +} - /* interpolate */ - fFrac = (fKey - self->fVal1[i1 -1]) - / (self->fVal1[i1] - self->fVal1[i1 - 1]); - *fResult = self->fVal2[i1-1] - + fFrac*(self->fVal2[i1] - self->fVal2[i1 -1]); - return 1; - } /*---------------------------------------------------------------------------*/ - int InterpolateVal2(pSTable self, float fKey, float *fResult) - { - float fFrac; - int i1,i; - - assert(self); - assert(self->fVal1); - assert(self->fVal2); - - /* search the entry point */ - for(i = 0; i < self->iLength; i++) - { - if(self->fVal2[i] <= fKey) - { - i1 = i; - break; - } - } - if(i1 >= self->iLength) - { - return 0; - } +int InterpolateVal2(pSTable self, float fKey, float *fResult) +{ + float fFrac; + int i1, i; - /* interpolate */ - fFrac = (fKey - self->fVal2[i1 -1]) - / (self->fVal2[i1] - self->fVal2[i1 - 1]); - *fResult = self->fVal1[i1-1] - + fFrac*(self->fVal1[i1] - self->fVal1[i1 -1]); - return 1; - } - + assert(self); + assert(self->fVal1); + assert(self->fVal2); + + /* search the entry point */ + for (i = 0; i < self->iLength; i++) { + if (self->fVal2[i] <= fKey) { + i1 = i; + break; + } + } + if (i1 >= self->iLength) { + return 0; + } + + /* interpolate */ + fFrac = (fKey - self->fVal2[i1 - 1]) + / (self->fVal2[i1] - self->fVal2[i1 - 1]); + *fResult = self->fVal1[i1 - 1] + + fFrac * (self->fVal1[i1] - self->fVal1[i1 - 1]); + return 1; +} diff --git a/hardsup/table.h b/hardsup/table.h index 7d98b29..9a2271a 100644 --- a/hardsup/table.h +++ b/hardsup/table.h @@ -8,28 +8,28 @@ copyright: see copyright.h ------------------------------------------------------------------------------*/ +-----------------------------------------------------------------------------*/ #ifndef SICSTABLE #define SICSTABLE - typedef struct __SicsTable *pSTable; - +typedef struct __SicsTable *pSTable; + /*------------------------- live & death ----------------------------------*/ - pSTable CreateTable(FILE *fd); +pSTable CreateTable(FILE * fd); /* creates a new table from a given file. The file is meant to have been positioned to the first entry for the table in the file. This leaves the caller free to examine a header, if any. - */ - void DeleteTable(pSTable self); + */ +void DeleteTable(pSTable self); /*------------------------- Interpolation --------------------------------*/ - int InterpolateVal1(pSTable pTable, float fKey, float *fResult); +int InterpolateVal1(pSTable pTable, float fKey, float *fResult); /* Returns a result from the second column for a key from the first column. - */ - int InterpolateVal2(pSTable pTable, float fKey, float *fResult); + */ +int InterpolateVal2(pSTable pTable, float fKey, float *fResult); /* Returns a result from the first column for a key from the second column. - */ -#endif + */ +#endif diff --git a/hardsup/velsel_def.h b/hardsup/velsel_def.h index 276eb65..970a131 100644 --- a/hardsup/velsel_def.h +++ b/hardsup/velsel_def.h @@ -9,50 +9,51 @@ #define OffsetOf(type, identifier) ((size_t)(&((type*) NULL)->identifier)) #endif -enum VelSel_Errors {VELSEL__BAD_TMO = -1, - VELSEL__BAD_CMD = -3, - VELSEL__BAD_OFL = -4, - VELSEL__BAD_ILLG = -5, - VELSEL__BAD_HOST = -6, - VELSEL__BAD_SOCKET = -7, - VELSEL__BAD_BIND = -8, - VELSEL__BAD_CONNECT = -9, - VELSEL__BAD_DEV = -10, - VELSEL__BAD_MALLOC = -11, - VELSEL__BAD_SENDLEN = -12, - VELSEL__BAD_SEND = -13, - VELSEL__BAD_SEND_PIPE = -14, - VELSEL__BAD_SEND_NET = -15, - VELSEL__BAD_SEND_UNKN = -16, - VELSEL__BAD_RECV = -17, - VELSEL__BAD_RECV_PIPE = -18, - VELSEL__BAD_RECV_NET = -19, - VELSEL__BAD_RECV_UNKN = -20, - VELSEL__BAD_NOT_BCD = -21, - VELSEL__BAD_RECVLEN = -22, - VELSEL__BAD_FLUSH = -23, - VELSEL__BAD_RECV1 = -24, - VELSEL__BAD_RECV1_PIPE = -25, - VELSEL__BAD_RECV1_NET = -26, - VELSEL__BAD_PAR = -29, - VELSEL__BAD_BSY = -30, - VELSEL__BAD_OPEN = -31, - VELSEL__FORCED_CLOSED = -32, - VELSEL__BAD_STP = -33, - VELSEL__NOT_OPEN = -35, - VELSEL__BAD_ASYNSRV = -36, - VELSEL__BAD_REPLY = -34}; +enum VelSel_Errors { VELSEL__BAD_TMO = -1, + VELSEL__BAD_CMD = -3, + VELSEL__BAD_OFL = -4, + VELSEL__BAD_ILLG = -5, + VELSEL__BAD_HOST = -6, + VELSEL__BAD_SOCKET = -7, + VELSEL__BAD_BIND = -8, + VELSEL__BAD_CONNECT = -9, + VELSEL__BAD_DEV = -10, + VELSEL__BAD_MALLOC = -11, + VELSEL__BAD_SENDLEN = -12, + VELSEL__BAD_SEND = -13, + VELSEL__BAD_SEND_PIPE = -14, + VELSEL__BAD_SEND_NET = -15, + VELSEL__BAD_SEND_UNKN = -16, + VELSEL__BAD_RECV = -17, + VELSEL__BAD_RECV_PIPE = -18, + VELSEL__BAD_RECV_NET = -19, + VELSEL__BAD_RECV_UNKN = -20, + VELSEL__BAD_NOT_BCD = -21, + VELSEL__BAD_RECVLEN = -22, + VELSEL__BAD_FLUSH = -23, + VELSEL__BAD_RECV1 = -24, + VELSEL__BAD_RECV1_PIPE = -25, + VELSEL__BAD_RECV1_NET = -26, + VELSEL__BAD_PAR = -29, + VELSEL__BAD_BSY = -30, + VELSEL__BAD_OPEN = -31, + VELSEL__FORCED_CLOSED = -32, + VELSEL__BAD_STP = -33, + VELSEL__NOT_OPEN = -35, + VELSEL__BAD_ASYNSRV = -36, + VELSEL__BAD_REPLY = -34 +}; /* ** Structure to which the VelSel_Open handle points. */ - struct VelSel_info { - struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */ - int tmo; - char eot[4]; - int msg_id; - int n_replies, max_replies; - struct RS__MsgStruct to_host; - struct RS__RespStruct from_host; - }; +struct VelSel_info { + struct AsynSrv__info asyn_info; /* Contains skt, host, port & chan */ + int tmo; + char eot[4]; + int msg_id; + int n_replies, max_replies; + struct RS__MsgStruct to_host; + struct RS__RespStruct from_host; +}; /*------------------------------------------------ End of VelSel_DEF.H --*/ -#endif /* _velsel_def_ */ +#endif /* _velsel_def_ */ diff --git a/hardsup/velsel_utility.c b/hardsup/velsel_utility.c index 3b277b0..121fd65 100644 --- a/hardsup/velsel_utility.c +++ b/hardsup/velsel_utility.c @@ -358,571 +358,658 @@ /*-------------------------------------------------------------------------- ** Global Variables */ - static int VelSel_call_depth = 0; - static char VelSel_routine[5][64]; - static int VelSel_errcode = 0; - static int VelSel_errno, VelSel_vaxc_errno; +static int VelSel_call_depth = 0; +static char VelSel_routine[5][64]; +static int VelSel_errcode = 0; +static int VelSel_errno, VelSel_vaxc_errno; /* **--------------------------------------------------------------------------- ** VelSel_Close: Close a connection to a Velocity Selector. */ - int VelSel_Close ( +int VelSel_Close( /* =========== -*/ void **handle, - int force_flag) { +*/ void **handle, + int force_flag) +{ - struct VelSel_info *info_ptr; - char buff[4]; - - info_ptr = (struct VelSel_info *) *handle; - if (info_ptr == NULL) return True; - - if (info_ptr->asyn_info.skt != 0) { - if (info_ptr->asyn_info.skt > 0) { - AsynSrv_Close (*handle, force_flag); - } - } - free (*handle); - *handle = NULL; + struct VelSel_info *info_ptr; + char buff[4]; + info_ptr = (struct VelSel_info *) *handle; + if (info_ptr == NULL) return True; + + if (info_ptr->asyn_info.skt != 0) { + if (info_ptr->asyn_info.skt > 0) { + AsynSrv_Close(*handle, force_flag); + } } + free(*handle); + *handle = NULL; + + return True; +} + /* **--------------------------------------------------------------------------- ** VelSel_Config: Configure a connection to a Velocity Selector. */ - void VelSel_Config ( +void VelSel_Config( /* ============ -*/ void **handle, - int msec_tmo, - char *eot_str) { +*/ void **handle, + int msec_tmo, char *eot_str) +{ - int i; - struct VelSel_info *info_ptr; + int i; + struct VelSel_info *info_ptr; - info_ptr = (struct VelSel_info *) *handle; - if (info_ptr == NULL) return; - /*------------------------- + info_ptr = (struct VelSel_info *) *handle; + if (info_ptr == NULL) + return; + /*------------------------- ** Set up the time-out */ - if (msec_tmo < 0) { - info_ptr->tmo = -1; - }else { - info_ptr->tmo = (msec_tmo + 99)/100; /* Convert to deci-secs */ - if (info_ptr->tmo > 9999) info_ptr->tmo = 9999; - } - /*--------------------------------- + if (msec_tmo < 0) { + info_ptr->tmo = -1; + } else { + info_ptr->tmo = (msec_tmo + 99) / 100; /* Convert to deci-secs */ + if (info_ptr->tmo > 9999) + info_ptr->tmo = 9999; + } + /*--------------------------------- ** Set up the end-of-text string */ - if (eot_str != NULL) { - for (i = 0; i < sizeof (info_ptr->eot); i++) info_ptr->eot[i] = '\0'; + if (eot_str != NULL) { + for (i = 0; i < sizeof(info_ptr->eot); i++) + info_ptr->eot[i] = '\0'; - for (i = 0; i < sizeof (info_ptr->eot); i++) { - if (eot_str[i] == '\0') break; - info_ptr->eot[i+1] = eot_str[i]; - } - info_ptr->eot[0] = '0' + i; + for (i = 0; i < sizeof(info_ptr->eot); i++) { + if (eot_str[i] == '\0') + break; + info_ptr->eot[i + 1] = eot_str[i]; } - return; + info_ptr->eot[0] = '0' + i; } + return; +} + /* ** ------------------------------------------------------------------------- ** VelSel_ErrInfo: Return detailed status from last operation. */ - void VelSel_ErrInfo ( +void VelSel_ErrInfo( /* ============= -*/ char **entry_txt, - int *errcode, - int *my_errno, - int *vaxc_errno) { +*/ char **entry_txt, + int *errcode, int *my_errno, int *vaxc_errno) +{ - int i; - char buff[80]; - int asyn_errcode, asyn_errno, asyn_vaxerrno; - char* asyn_errtxt; + int i; + char buff[80]; + int asyn_errcode, asyn_errno, asyn_vaxerrno; + char *asyn_errtxt; - if (VelSel_call_depth <= 0) { - strcpy (VelSel_routine[0], "VelSel_no_error_detected"); - *errcode = 0; - *my_errno = 0; - *vaxc_errno = 0; - }else { - if (VelSel_call_depth > 1) { /* Concatenate the names */ - for (i = 1; i < VelSel_call_depth; i++) { - strcat (VelSel_routine[0], "/"); - StrJoin (VelSel_routine[0], sizeof (VelSel_routine), - VelSel_routine[0], VelSel_routine[i]); - } + if (VelSel_call_depth <= 0) { + strcpy(VelSel_routine[0], "VelSel_no_error_detected"); + *errcode = 0; + *my_errno = 0; + *vaxc_errno = 0; + } else { + if (VelSel_call_depth > 1) { /* Concatenate the names */ + for (i = 1; i < VelSel_call_depth; i++) { + strcat(VelSel_routine[0], "/"); + StrJoin(VelSel_routine[0], sizeof(VelSel_routine), + VelSel_routine[0], VelSel_routine[i]); } - *errcode = VelSel_errcode; - *my_errno = VelSel_errno; - *vaxc_errno = VelSel_vaxc_errno; - switch (VelSel_errcode) { - case VELSEL__BAD_TMO: strcpy (buff, "/VELSEL__BAD_TMO"); break; - case VELSEL__BAD_CMD: strcpy (buff, "/VELSEL__BAD_CMD"); break; - case VELSEL__BAD_OFL: strcpy (buff, "/VELSEL__BAD_OFL"); break; - case VELSEL__BAD_ILLG: strcpy (buff, "/VELSEL__BAD_ILLG"); break; - case VELSEL__BAD_HOST: strcpy (buff, "/VELSEL__BAD_HOST"); break; - case VELSEL__BAD_SOCKET: strcpy (buff, "/VELSEL__BAD_SOCKET"); break; - case VELSEL__BAD_BIND: strcpy (buff, "/VELSEL__BAD_BIND"); break; - case VELSEL__BAD_CONNECT: strcpy (buff, "/VELSEL__BAD_CONNECT"); break; - case VELSEL__BAD_DEV: strcpy (buff, "/VELSEL__BAD_DEV"); break; - case VELSEL__BAD_MALLOC: strcpy (buff, "/VELSEL__BAD_MALLOC"); break; - case VELSEL__BAD_SENDLEN: strcpy (buff, "/VELSEL__BAD_SENDLEN"); break; - case VELSEL__BAD_SEND: strcpy (buff, "/VELSEL__BAD_SEND"); break; - case VELSEL__BAD_SEND_PIPE: strcpy (buff, "/VELSEL__BAD_SEND_PIPE"); break; - case VELSEL__BAD_SEND_NET: strcpy (buff, "/VELSEL__BAD_SEND_NET"); break; - case VELSEL__BAD_SEND_UNKN: strcpy (buff, "/VELSEL__BAD_SEND_UNKN"); break; - case VELSEL__BAD_RECV: strcpy (buff, "/VELSEL__BAD_RECV"); break; - case VELSEL__BAD_RECV_PIPE: strcpy (buff, "/VELSEL__BAD_RECV_PIPE"); break; - case VELSEL__BAD_RECV_NET: strcpy (buff, "/VELSEL__BAD_RECV_NET"); break; - case VELSEL__BAD_RECV_UNKN: strcpy (buff, "/VELSEL__BAD_RECV_UNKN"); break; - case VELSEL__BAD_NOT_BCD: strcpy (buff, "/VELSEL__BAD_NOT_BCD"); break; - case VELSEL__BAD_RECVLEN: strcpy (buff, "/VELSEL__BAD_RECVLEN"); break; - case VELSEL__BAD_FLUSH: strcpy (buff, "/VELSEL__BAD_FLUSH"); break; - case VELSEL__BAD_RECV1: strcpy (buff, "/VELSEL__BAD_RECV1"); break; - case VELSEL__BAD_RECV1_PIPE: strcpy (buff, "/VELSEL__BAD_RECV1_PIPE"); break; - case VELSEL__BAD_RECV1_NET: strcpy (buff, "/VELSEL__BAD_RECV1_NET"); break; - case VELSEL__BAD_PAR: strcpy (buff, "/VELSEL__BAD_PAR"); break; - case VELSEL__BAD_BSY: strcpy (buff, "/VELSEL__BAD_BSY"); break; - case VELSEL__BAD_OPEN: strcpy (buff, "/VELSEL__BAD_OPEN"); break; - case VELSEL__FORCED_CLOSED: strcpy (buff, "/VELSEL__FORCED_CLOSED"); break; - case VELSEL__BAD_STP: strcpy (buff, "/VELSEL__BAD_STP"); break; - case VELSEL__BAD_REPLY: strcpy (buff, "/VELSEL__BAD_REPLY"); break; - default: sprintf (buff, "/VELSEL__unknown_err_code: %d", VelSel_errcode); - } - StrJoin (VelSel_routine[0], sizeof(VelSel_routine), VelSel_routine[0], buff); } - AsynSrv_ErrInfo (&asyn_errtxt, &asyn_errcode, &asyn_errno, &asyn_vaxerrno); - if (asyn_errcode != 0) { - strcat (VelSel_routine[0], "/"); - StrJoin (VelSel_routine[0], sizeof(VelSel_routine), - VelSel_routine[0], asyn_errtxt); + *errcode = VelSel_errcode; + *my_errno = VelSel_errno; + *vaxc_errno = VelSel_vaxc_errno; + switch (VelSel_errcode) { + case VELSEL__BAD_TMO: + strcpy(buff, "/VELSEL__BAD_TMO"); + break; + case VELSEL__BAD_CMD: + strcpy(buff, "/VELSEL__BAD_CMD"); + break; + case VELSEL__BAD_OFL: + strcpy(buff, "/VELSEL__BAD_OFL"); + break; + case VELSEL__BAD_ILLG: + strcpy(buff, "/VELSEL__BAD_ILLG"); + break; + case VELSEL__BAD_HOST: + strcpy(buff, "/VELSEL__BAD_HOST"); + break; + case VELSEL__BAD_SOCKET: + strcpy(buff, "/VELSEL__BAD_SOCKET"); + break; + case VELSEL__BAD_BIND: + strcpy(buff, "/VELSEL__BAD_BIND"); + break; + case VELSEL__BAD_CONNECT: + strcpy(buff, "/VELSEL__BAD_CONNECT"); + break; + case VELSEL__BAD_DEV: + strcpy(buff, "/VELSEL__BAD_DEV"); + break; + case VELSEL__BAD_MALLOC: + strcpy(buff, "/VELSEL__BAD_MALLOC"); + break; + case VELSEL__BAD_SENDLEN: + strcpy(buff, "/VELSEL__BAD_SENDLEN"); + break; + case VELSEL__BAD_SEND: + strcpy(buff, "/VELSEL__BAD_SEND"); + break; + case VELSEL__BAD_SEND_PIPE: + strcpy(buff, "/VELSEL__BAD_SEND_PIPE"); + break; + case VELSEL__BAD_SEND_NET: + strcpy(buff, "/VELSEL__BAD_SEND_NET"); + break; + case VELSEL__BAD_SEND_UNKN: + strcpy(buff, "/VELSEL__BAD_SEND_UNKN"); + break; + case VELSEL__BAD_RECV: + strcpy(buff, "/VELSEL__BAD_RECV"); + break; + case VELSEL__BAD_RECV_PIPE: + strcpy(buff, "/VELSEL__BAD_RECV_PIPE"); + break; + case VELSEL__BAD_RECV_NET: + strcpy(buff, "/VELSEL__BAD_RECV_NET"); + break; + case VELSEL__BAD_RECV_UNKN: + strcpy(buff, "/VELSEL__BAD_RECV_UNKN"); + break; + case VELSEL__BAD_NOT_BCD: + strcpy(buff, "/VELSEL__BAD_NOT_BCD"); + break; + case VELSEL__BAD_RECVLEN: + strcpy(buff, "/VELSEL__BAD_RECVLEN"); + break; + case VELSEL__BAD_FLUSH: + strcpy(buff, "/VELSEL__BAD_FLUSH"); + break; + case VELSEL__BAD_RECV1: + strcpy(buff, "/VELSEL__BAD_RECV1"); + break; + case VELSEL__BAD_RECV1_PIPE: + strcpy(buff, "/VELSEL__BAD_RECV1_PIPE"); + break; + case VELSEL__BAD_RECV1_NET: + strcpy(buff, "/VELSEL__BAD_RECV1_NET"); + break; + case VELSEL__BAD_PAR: + strcpy(buff, "/VELSEL__BAD_PAR"); + break; + case VELSEL__BAD_BSY: + strcpy(buff, "/VELSEL__BAD_BSY"); + break; + case VELSEL__BAD_OPEN: + strcpy(buff, "/VELSEL__BAD_OPEN"); + break; + case VELSEL__FORCED_CLOSED: + strcpy(buff, "/VELSEL__FORCED_CLOSED"); + break; + case VELSEL__BAD_STP: + strcpy(buff, "/VELSEL__BAD_STP"); + break; + case VELSEL__BAD_REPLY: + strcpy(buff, "/VELSEL__BAD_REPLY"); + break; + default: + sprintf(buff, "/VELSEL__unknown_err_code: %d", VelSel_errcode); } - *entry_txt = VelSel_routine[0]; - VelSel_call_depth = 0; - VelSel_errcode = 0; + StrJoin(VelSel_routine[0], sizeof(VelSel_routine), VelSel_routine[0], + buff); } + AsynSrv_ErrInfo(&asyn_errtxt, &asyn_errcode, &asyn_errno, + &asyn_vaxerrno); + if (asyn_errcode != 0) { + strcat(VelSel_routine[0], "/"); + StrJoin(VelSel_routine[0], sizeof(VelSel_routine), + VelSel_routine[0], asyn_errtxt); + } + *entry_txt = VelSel_routine[0]; + VelSel_call_depth = 0; + VelSel_errcode = 0; +} + /* **--------------------------------------------------------------------------- ** VelSel_GetReply - Get next reply from a reply buffer. */ - void *VelSel_GetReply ( -/* ============== -*/ void **handle, /* Pointer to structure containing - ** message to pull apart */ - void *last_rply) { /* Starting point */ +void *VelSel_GetReply( + /* ============== +*/ void **handle, /* Pointer to structure containing + ** message to pull apart */ + void *last_rply) +{ /* Starting point */ - int rply_len; - struct RS__RplyStruct *ptr; - struct VelSel_info *my_info_ptr; - struct RS__RplyStruct *my_last_rply; + int rply_len; + struct RS__RplyStruct *ptr; + struct VelSel_info *my_info_ptr; + struct RS__RplyStruct *my_last_rply; - ptr = NULL; - my_info_ptr = (struct VelSel_info *) *handle; - my_last_rply = (struct RS__RplyStruct *) last_rply; + ptr = NULL; + my_info_ptr = (struct VelSel_info *) *handle; + my_last_rply = (struct RS__RplyStruct *) last_rply; - if (my_last_rply == NULL) { /* Start with first reply? */ - /* Yes */ - if (sscanf (my_info_ptr->from_host.n_rply, "%4d", - &my_info_ptr->max_replies) != 1) my_info_ptr->max_replies = 0; - if (my_info_ptr->max_replies > 0) - ptr = (struct RS__RplyStruct *) my_info_ptr->from_host.u.rplys; - my_info_ptr->n_replies = 1; - }else { - my_info_ptr->n_replies++; - if (my_info_ptr->n_replies <= my_info_ptr->max_replies) { - if (sscanf (my_last_rply->rply_len, "%2d", &rply_len) == 1) { - ptr = - (struct RS__RplyStruct *) ((char *) my_last_rply + rply_len + 2); - } + if (my_last_rply == NULL) { /* Start with first reply? */ + /* Yes */ + if (sscanf(my_info_ptr->from_host.n_rply, "%4d", + &my_info_ptr->max_replies) != 1) + my_info_ptr->max_replies = 0; + if (my_info_ptr->max_replies > 0) + ptr = (struct RS__RplyStruct *) my_info_ptr->from_host.u.rplys; + my_info_ptr->n_replies = 1; + } else { + my_info_ptr->n_replies++; + if (my_info_ptr->n_replies <= my_info_ptr->max_replies) { + if (sscanf(my_last_rply->rply_len, "%2d", &rply_len) == 1) { + ptr = + (struct RS__RplyStruct *) ((char *) my_last_rply + rply_len + + 2); } } - return (void *) ptr; } + return (void *) ptr; +} + /* **--------------------------------------------------------------------------- ** VelSel_GetStatus: Get "???" response from Vel Selector */ - int VelSel_GetStatus ( +int VelSel_GetStatus( /* =============== -*/ void **handle, - char *status_str, - int status_str_len) { +*/ void **handle, + char *status_str, int status_str_len) +{ - int status; - struct VelSel_info *info_ptr; - struct RS__RplyStruct *rply_ptr; - struct RS__RplyStruct *rply_ptr0; - /*---------------------------------------------- + int status; + struct VelSel_info *info_ptr; + struct RS__RplyStruct *rply_ptr; + struct RS__RplyStruct *rply_ptr0; + /*---------------------------------------------- */ - status_str[0] = '\0'; - /*---------------------------------------------- + status_str[0] = '\0'; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (VelSel_errcode == 0 && VelSel_call_depth < 5) { - strcpy (VelSel_routine[VelSel_call_depth], "VelSel_GetStatus"); - VelSel_call_depth++; - } - /*---------------------------------------------- + if (VelSel_errcode == 0 && VelSel_call_depth < 5) { + strcpy(VelSel_routine[VelSel_call_depth], "VelSel_GetStatus"); + VelSel_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. */ - info_ptr = (struct VelSel_info *) *handle; - if (info_ptr == NULL) { - return False; + info_ptr = (struct VelSel_info *) *handle; + if (info_ptr == NULL) { + return False; + } + if (info_ptr->asyn_info.skt <= 0) { + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + if ((VelSel_errcode == 0) && (info_ptr->asyn_info.skt < 0)) { + VelSel_errcode = VELSEL__FORCED_CLOSED; } - if (info_ptr->asyn_info.skt <= 0) { - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - if ((VelSel_errcode == 0) && (info_ptr->asyn_info.skt < 0)) { - VelSel_errcode = VELSEL__FORCED_CLOSED; - } - return False; - } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Send "???" command to Velocity Selector */ - status = VelSel_SendCmnds (handle, "???", NULL); - if (!status) { - /* Error in VelSel_SendCmnds */ - return False; - }else { - rply_ptr0 = VelSel_GetReply (handle, NULL); - if (rply_ptr0 == NULL) rply_ptr0 = (struct RS__RplyStruct *) "06\rNULL"; - StrJoin (status_str, status_str_len, rply_ptr0->rply, ""); - } - VelSel_call_depth--; - return True; + status = VelSel_SendCmnds(handle, "???", NULL); + if (!status) { + /* Error in VelSel_SendCmnds */ + return False; + } else { + rply_ptr0 = VelSel_GetReply(handle, NULL); + if (rply_ptr0 == NULL) + rply_ptr0 = (struct RS__RplyStruct *) "06\rNULL"; + StrJoin(status_str, status_str_len, rply_ptr0->rply, ""); } + VelSel_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** VelSel_Open: Open a connection to a Velocity Selector. */ - int VelSel_Open ( +int VelSel_Open( /* ========== -*/ void **handle, - char *host, - int port, - int chan) { +*/ void **handle, + char *host, int port, int chan) +{ - int status; - struct VelSel_info *my_handle; - struct RS__RplyStruct *rply_ptr; - struct RS__RplyStruct *rply_ptr0; - struct RS__RplyStruct *rply_ptr1; - struct RS__RplyStruct *rply_ptr2; - struct RS__RplyStruct *rply_ptr3; + int status; + struct VelSel_info *my_handle; + struct RS__RplyStruct *rply_ptr; + struct RS__RplyStruct *rply_ptr0; + struct RS__RplyStruct *rply_ptr1; + struct RS__RplyStruct *rply_ptr2; + struct RS__RplyStruct *rply_ptr3; /*-------------------------------------------------------- ** Initialise the error info stack and pre-set the ** routine name (in case of error). */ - VelSel_errcode = VelSel_errno = VelSel_vaxc_errno = 0; - strcpy (VelSel_routine[0], "VelSel_Open"); - VelSel_call_depth = 1; + VelSel_errcode = VelSel_errno = VelSel_vaxc_errno = 0; + strcpy(VelSel_routine[0], "VelSel_Open"); + VelSel_call_depth = 1; /*-------------------------------------------------------- ** Assume trouble */ - *handle = NULL; + *handle = NULL; /*-------------------------------------------------------- ** Reserve space for the data we need to store. */ - my_handle = (struct VelSel_info *) malloc (sizeof (*my_handle)); - if (my_handle == NULL) { - VelSel_errcode = VELSEL__BAD_MALLOC; /* malloc failed!! */ - return False; - } + my_handle = (struct VelSel_info *) malloc(sizeof(*my_handle)); + if (my_handle == NULL) { + VelSel_errcode = VELSEL__BAD_MALLOC; /* malloc failed!! */ + return False; + } /*-------------------------------------------------------- ** Set up the connection */ - StrJoin (my_handle->asyn_info.host, sizeof (my_handle->asyn_info.host), - host, ""); - my_handle->asyn_info.port = port; - my_handle->asyn_info.chan = chan; - status = AsynSrv_Open (&my_handle->asyn_info); - if (!status) { - VelSel_errcode = VELSEL__BAD_SOCKET; - GetErrno (&VelSel_errno, &VelSel_vaxc_errno); /* Save errno info */ - fprintf (stderr, "\nVelSel_Open/AsynSrv_Open: " - "Failed to make connection.\n"); - free (my_handle); - return False; - } - - my_handle->tmo = 25; /* Set a short time-out initially since - ** there should be no reason for the REM - ** command to take very long - */ - strcpy (my_handle->eot, "1\n\0\0"); - my_handle->msg_id = 0; - /* - ** Now ensure the VelSel is on-line. The first "REM" command can - ** fail due to pending characters in the VelSel input buffer causing - ** the "REM" to be corrupted. The response of the VelSel to this - ** command is ignored for this reason (but the VelSel_SendCmnds - ** status must be OK otherwise it indicates a network problem). - */ - status = VelSel_SendCmnds ((void *) &my_handle, "REM", NULL); - if (status) { - status = VelSel_SendCmnds ((void *) &my_handle, "REM", NULL); - } - if (!status) { - /* Some error occurred in VelSel_SendCmnds - Errcode will - ** have been set up there. - */ - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - } - /* - ** Check the responses carefully. - */ - rply_ptr0 = VelSel_GetReply ((void *) &my_handle, NULL); - - if (rply_ptr0 == NULL) rply_ptr0 = (struct RS__RplyStruct *) "06\rNULL"; - if (rply_ptr0->rply[0] == '?') { - VelSel_errcode = VELSEL__BAD_DEV; /* Error response - not a VelSel? */ - AsynSrv_Close (&my_handle->asyn_info, False); - free (my_handle); - return False; - } - /* - ** The connection is complete. Pass the data structure - ** back to the caller as a handle. - */ - my_handle->tmo = 100; /* Default time-out is 10 secs */ - *handle = my_handle; - VelSel_call_depth--; - return True; + StrJoin(my_handle->asyn_info.host, sizeof(my_handle->asyn_info.host), + host, ""); + my_handle->asyn_info.port = port; + my_handle->asyn_info.chan = chan; + status = AsynSrv_Open(&my_handle->asyn_info); + if (!status) { + VelSel_errcode = VELSEL__BAD_SOCKET; + GetErrno(&VelSel_errno, &VelSel_vaxc_errno); /* Save errno info */ + fprintf(stderr, "\nVelSel_Open/AsynSrv_Open: " + "Failed to make connection.\n"); + free(my_handle); + return False; } + + my_handle->tmo = 25; /* Set a short time-out initially since + ** there should be no reason for the REM + ** command to take very long + */ + strcpy(my_handle->eot, "1\n\0\0"); + my_handle->msg_id = 0; + /* + ** Now ensure the VelSel is on-line. The first "REM" command can + ** fail due to pending characters in the VelSel input buffer causing + ** the "REM" to be corrupted. The response of the VelSel to this + ** command is ignored for this reason (but the VelSel_SendCmnds + ** status must be OK otherwise it indicates a network problem). + */ + status = VelSel_SendCmnds((void *) &my_handle, "REM", NULL); + if (status) { + status = VelSel_SendCmnds((void *) &my_handle, "REM", NULL); + } + if (!status) { + /* Some error occurred in VelSel_SendCmnds - Errcode will + ** have been set up there. + */ + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } + /* + ** Check the responses carefully. + */ + rply_ptr0 = VelSel_GetReply((void *) &my_handle, NULL); + + if (rply_ptr0 == NULL) + rply_ptr0 = (struct RS__RplyStruct *) "06\rNULL"; + if (rply_ptr0->rply[0] == '?') { + VelSel_errcode = VELSEL__BAD_DEV; /* Error response - not a VelSel? */ + AsynSrv_Close(&my_handle->asyn_info, False); + free(my_handle); + return False; + } + /* + ** The connection is complete. Pass the data structure + ** back to the caller as a handle. + */ + my_handle->tmo = 100; /* Default time-out is 10 secs */ + *handle = my_handle; + VelSel_call_depth--; + return True; +} + /* **--------------------------------------------------------------------------- ** VelSel_SendCmnds - Send commands to RS232C server. */ - int VelSel_SendCmnds ( +int VelSel_SendCmnds( /* ================ -*/ void **handle, - ...) { /* Now we have list of commands - - ** char *txt = pntr to cmnd strng - ** Terminate list with *txt = NULL. - */ - struct VelSel_info *info_ptr; - int i, status, c_len, size, max_size, ncmnds; - int bytes_to_come, bytes_left; - char *nxt_byte_ptr; - char err_text[80]; - char text[20]; - va_list ap; /* Pointer to variable args */ - char *txt_ptr; - char *cmnd_lst_ptr; - /*---------------------------------------------- +*/ void **handle, + ...) +{ /* Now we have list of commands - + ** char *txt = pntr to cmnd strng + ** Terminate list with *txt = NULL. + */ + struct VelSel_info *info_ptr; + int i, status, c_len, size, max_size, ncmnds; + int bytes_to_come, bytes_left; + char *nxt_byte_ptr; + char err_text[80]; + char text[20]; + va_list ap; /* Pointer to variable args */ + char *txt_ptr; + char *cmnd_lst_ptr; + /*---------------------------------------------- ** Pre-set the routine name (in case of error) */ - if (VelSel_errcode == 0 && VelSel_call_depth < 5) { - strcpy (VelSel_routine[VelSel_call_depth], "VelSel_SendCmnds"); - VelSel_call_depth++; - } - /*---------------------------------------------- + if (VelSel_errcode == 0 && VelSel_call_depth < 5) { + strcpy(VelSel_routine[VelSel_call_depth], "VelSel_SendCmnds"); + VelSel_call_depth++; + } + /*---------------------------------------------- ** Do nothing if no connection - the connection gets ** closed if an error is detected. The connection may ** also be marked to have been forcefully closed. */ - info_ptr = (struct VelSel_info *) *handle; - if (info_ptr == NULL) { - return False; + info_ptr = (struct VelSel_info *) *handle; + if (info_ptr == NULL) { + return False; + } + if (info_ptr->asyn_info.skt <= 0) { + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + if ((VelSel_errcode == 0) && (info_ptr->asyn_info.skt < 0)) { + VelSel_errcode = VELSEL__FORCED_CLOSED; } - if (info_ptr->asyn_info.skt <= 0) { - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - if ((VelSel_errcode == 0) && (info_ptr->asyn_info.skt < 0)) { - VelSel_errcode = VELSEL__FORCED_CLOSED; - } - return False; - } - /*---------------------------------------------- + return False; + } + /*---------------------------------------------- ** Build message for Vel Selector from the list of commands. */ - info_ptr->n_replies = info_ptr->max_replies = 0; + info_ptr->n_replies = info_ptr->max_replies = 0; - info_ptr->msg_id++; /* Set up an incrementing message id */ - if (info_ptr->msg_id > 9999) info_ptr->msg_id = 1; - sprintf (info_ptr->to_host.msg_id, "%04.4d", info_ptr->msg_id); + info_ptr->msg_id++; /* Set up an incrementing message id */ + if (info_ptr->msg_id > 9999) + info_ptr->msg_id = 1; + sprintf(info_ptr->to_host.msg_id, "%04.4d", info_ptr->msg_id); - memcpy (info_ptr->to_host.c_pcol_lvl, RS__PROTOCOL_ID, - sizeof (info_ptr->to_host.c_pcol_lvl)); - sprintf (info_ptr->to_host.serial_port, "%04.4d", info_ptr->asyn_info.chan); - sprintf (info_ptr->to_host.tmo, "%04.4d", info_ptr->tmo); + memcpy(info_ptr->to_host.c_pcol_lvl, RS__PROTOCOL_ID, + sizeof(info_ptr->to_host.c_pcol_lvl)); + sprintf(info_ptr->to_host.serial_port, "%04.4d", + info_ptr->asyn_info.chan); + sprintf(info_ptr->to_host.tmo, "%04.4d", info_ptr->tmo); - memcpy (info_ptr->to_host.terms, info_ptr->eot, - sizeof (info_ptr->to_host.terms)); - memcpy (info_ptr->to_host.n_cmnds, "0000", - sizeof (info_ptr->to_host.n_cmnds)); + memcpy(info_ptr->to_host.terms, info_ptr->eot, + sizeof(info_ptr->to_host.terms)); + memcpy(info_ptr->to_host.n_cmnds, "0000", + sizeof(info_ptr->to_host.n_cmnds)); - va_start (ap, handle); /* Set up var arg machinery */ + va_start(ap, handle); /* Set up var arg machinery */ - txt_ptr = va_arg (ap, char *); /* Get pntr to next cmnd string */ - ncmnds = 0; - cmnd_lst_ptr = &info_ptr->to_host.cmnds[0]; - bytes_left = sizeof (info_ptr->to_host) - - OffsetOf (struct RS__MsgStruct, cmnds[0]); + txt_ptr = va_arg(ap, char *); /* Get pntr to next cmnd string */ + ncmnds = 0; + cmnd_lst_ptr = &info_ptr->to_host.cmnds[0]; + bytes_left = sizeof(info_ptr->to_host) - + OffsetOf(struct RS__MsgStruct, cmnds[0]); - while (txt_ptr != NULL) { - size = 2 + strlen (txt_ptr); - if (size > bytes_left) { - VelSel_errcode = VELSEL__BAD_SENDLEN; /* Too much to send */ - fprintf (stderr, "\nVelSel_SendCmnds/send: too much to send" - " - request ignored.\n"); - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return False; - }else { - strcpy (cmnd_lst_ptr+2, txt_ptr); - c_len = strlen (txt_ptr); - sprintf (text, "%02.2d", c_len); - memcpy (cmnd_lst_ptr, text, 2); - cmnd_lst_ptr = cmnd_lst_ptr + c_len + 2; - ncmnds++; - bytes_left = bytes_left - size; - txt_ptr = va_arg (ap, char *); - } - } - sprintf (text, "%04.4d", ncmnds); - memcpy (info_ptr->to_host.n_cmnds, - text, sizeof (info_ptr->to_host.n_cmnds)); - - size = cmnd_lst_ptr - info_ptr->to_host.msg_id; - size = (size + 3) & (~3); /* Round up to multiple of 4 */ - sprintf (text, "%04.4d", size); - memcpy (info_ptr->to_host.msg_size, text, 4); - - status = send (info_ptr->asyn_info.skt, - (char *) &info_ptr->to_host, size+4, 0); - if (status != (size+4)) { - GetErrno (&VelSel_errno, &VelSel_vaxc_errno); - if (status == 0) { - VelSel_errcode = VELSEL__BAD_SEND; /* Server exited (probably) */ - fprintf (stderr, "\nVelSel_SendCmnds/send: probable network problem"); - }else if (status == -1) { - if (VelSel_errno == EPIPE) { - VelSel_errcode = VELSEL__BAD_SEND_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nVelSel_SendCmnds/send: broken network pipe"); - }else { - VelSel_errcode = VELSEL__BAD_SEND_NET; /* It's some other net problem */ - perror ("VelSel_SendCmnds/send"); - } - }else { - VelSel_errcode = VELSEL__BAD_SEND_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nVelSel_SendCmnds/send: probable TCP/IP problem"); - } - VelSel_Close (handle, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); + while (txt_ptr != NULL) { + size = 2 + strlen(txt_ptr); + if (size > bytes_left) { + VelSel_errcode = VELSEL__BAD_SENDLEN; /* Too much to send */ + fprintf(stderr, "\nVelSel_SendCmnds/send: too much to send" + " - request ignored.\n"); + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); return False; + } else { + strcpy(cmnd_lst_ptr + 2, txt_ptr); + c_len = strlen(txt_ptr); + sprintf(text, "%02.2d", c_len); + memcpy(cmnd_lst_ptr, text, 2); + cmnd_lst_ptr = cmnd_lst_ptr + c_len + 2; + ncmnds++; + bytes_left = bytes_left - size; + txt_ptr = va_arg(ap, char *); } - - size = sizeof (info_ptr->from_host.msg_size); - status = recv (info_ptr->asyn_info.skt, - info_ptr->from_host.msg_size, size, 0); - if (status != size) { - GetErrno (&VelSel_errno, &VelSel_vaxc_errno); - if (status == 0) { - VelSel_errcode = VELSEL__BAD_RECV; /* Server exited (probably) */ - fprintf (stderr, "\nVelSel_SendCmnds/recv: probable network problem"); - }else if (status == -1) { - if (VelSel_errno == EPIPE) { - VelSel_errcode = VELSEL__BAD_RECV_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nVelSel_SendCmnds/recv: broken network pipe"); - }else { - VelSel_errcode = VELSEL__BAD_RECV_NET; /* It's some other net problem */ - perror ("VelSel_SendCmnds/recv"); - } - }else { - VelSel_errcode = VELSEL__BAD_RECV_UNKN; /* TCP/IP problems */ - fprintf (stderr, "\nVelSel_SendCmnds/recv: probable TCP/IP problem"); - } - VelSel_Close (handle, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - if (sscanf (info_ptr->from_host.msg_size, "%4d", &bytes_to_come) != 1) { - VelSel_errcode = VELSEL__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ - VelSel_Close (handle, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nVelSel_SendCmnds/recv: non-BCD byte count" - " - link to server force-closed.\n"); - return False; - } - max_size = sizeof (info_ptr->from_host) - - sizeof (info_ptr->from_host.msg_size); - if (bytes_to_come > max_size) { - VelSel_errcode = VELSEL__BAD_RECVLEN; - fprintf (stderr, "\nVelSel_SendCmnds/recv: pending message length too big" - " - flushing ...\n"); - nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; - while (bytes_to_come > 0) { /* Flush out the incoming message */ - bytes_left = bytes_to_come; - if (bytes_left > max_size) bytes_left = max_size; - status = recv (info_ptr->asyn_info.skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - VelSel_errcode = VELSEL__BAD_FLUSH; /* TCP/IP problem whilst flushing */ - GetErrno (&VelSel_errno, &VelSel_vaxc_errno); - VelSel_Close (handle, True); /* Force close TCP/IP connection */ - fprintf (stderr, "\nVelSel_SendCmnds/recv: network problem during" - " flush.\nLink to server force-closed.\n"); - return False; - } - bytes_to_come = bytes_to_come - status; - } - fprintf (stderr, "\n flushed OK.\n"); - memset (info_ptr->from_host.msg_size, - '0', sizeof (info_ptr->from_host.msg_size)); - return False; - }else { - nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; - bytes_left = bytes_to_come; - while (bytes_left > 0) { /* Read the rest of the response */ - status = recv (info_ptr->asyn_info.skt, nxt_byte_ptr, bytes_left, 0); - if (status <= 0) { - GetErrno (&VelSel_errno, &VelSel_vaxc_errno); - if (status == 0) { - VelSel_errcode = VELSEL__BAD_RECV1; /* Server exited (probably) */ - fprintf (stderr, "\nVelSel_SendCmnds/recv/1: probable network " - "problem"); - }else { - if (VelSel_errno == EPIPE) { - VelSel_errcode = VELSEL__BAD_RECV1_PIPE; /* Server exited (probably) */ - fprintf (stderr, "\nVelSel_SendCmnds/recv/1: broken network pipe"); - }else { - VelSel_errcode = VELSEL__BAD_RECV1_NET; /* It's some other net fault */ - perror ("VelSel_SendCmnds/recv/1"); - } - } - VelSel_Close (handle, True); /* Force close TCP/IP connection */ - fprintf (stderr, " - link to server force-closed.\n"); - return False; - } - bytes_left = bytes_left - status; - nxt_byte_ptr = nxt_byte_ptr + status; - } - if ((sscanf (info_ptr->from_host.n_rply, "%4d", - &info_ptr->max_replies) != 1) || - (info_ptr->max_replies < 0)) { - VelSel_errcode = VELSEL__BAD_REPLY; /* Reply is bad */ - if (VelSel_call_depth < 5) { /* Add reply to routine stack */ - bytes_to_come = bytes_to_come + 4; - if (bytes_to_come >= sizeof (VelSel_routine[0])) - bytes_to_come = sizeof (VelSel_routine[0]) - 1; - for (i=0; ifrom_host.msg_size[i] == '\0') - info_ptr->from_host.msg_size[i] = '.'; - } - info_ptr->from_host.msg_size[bytes_to_come] = '\0'; - strcpy (VelSel_routine[VelSel_call_depth], - info_ptr->from_host.msg_size); - VelSel_call_depth++; - } - return False; - } - } - VelSel_call_depth--; - return True; } + sprintf(text, "%04.4d", ncmnds); + memcpy(info_ptr->to_host.n_cmnds, + text, sizeof(info_ptr->to_host.n_cmnds)); + + size = cmnd_lst_ptr - info_ptr->to_host.msg_id; + size = (size + 3) & (~3); /* Round up to multiple of 4 */ + sprintf(text, "%04.4d", size); + memcpy(info_ptr->to_host.msg_size, text, 4); + + status = send(info_ptr->asyn_info.skt, + (char *) &info_ptr->to_host, size + 4, 0); + if (status != (size + 4)) { + GetErrno(&VelSel_errno, &VelSel_vaxc_errno); + if (status == 0) { + VelSel_errcode = VELSEL__BAD_SEND; /* Server exited (probably) */ + fprintf(stderr, "\nVelSel_SendCmnds/send: probable network problem"); + } else if (status == -1) { + if (VelSel_errno == EPIPE) { + VelSel_errcode = VELSEL__BAD_SEND_PIPE; /* Server exited (probably) */ + fprintf(stderr, "\nVelSel_SendCmnds/send: broken network pipe"); + } else { + VelSel_errcode = VELSEL__BAD_SEND_NET; /* It's some other net problem */ + perror("VelSel_SendCmnds/send"); + } + } else { + VelSel_errcode = VELSEL__BAD_SEND_UNKN; /* TCP/IP problems */ + fprintf(stderr, "\nVelSel_SendCmnds/send: probable TCP/IP problem"); + } + VelSel_Close(handle, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + + size = sizeof(info_ptr->from_host.msg_size); + status = recv(info_ptr->asyn_info.skt, + info_ptr->from_host.msg_size, size, 0); + if (status != size) { + GetErrno(&VelSel_errno, &VelSel_vaxc_errno); + if (status == 0) { + VelSel_errcode = VELSEL__BAD_RECV; /* Server exited (probably) */ + fprintf(stderr, "\nVelSel_SendCmnds/recv: probable network problem"); + } else if (status == -1) { + if (VelSel_errno == EPIPE) { + VelSel_errcode = VELSEL__BAD_RECV_PIPE; /* Server exited (probably) */ + fprintf(stderr, "\nVelSel_SendCmnds/recv: broken network pipe"); + } else { + VelSel_errcode = VELSEL__BAD_RECV_NET; /* It's some other net problem */ + perror("VelSel_SendCmnds/recv"); + } + } else { + VelSel_errcode = VELSEL__BAD_RECV_UNKN; /* TCP/IP problems */ + fprintf(stderr, "\nVelSel_SendCmnds/recv: probable TCP/IP problem"); + } + VelSel_Close(handle, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + if (sscanf(info_ptr->from_host.msg_size, "%4d", &bytes_to_come) != 1) { + VelSel_errcode = VELSEL__BAD_NOT_BCD; /* Header not an ASCII BCD integer */ + VelSel_Close(handle, True); /* Force close TCP/IP connection */ + fprintf(stderr, "\nVelSel_SendCmnds/recv: non-BCD byte count" + " - link to server force-closed.\n"); + return False; + } + max_size = sizeof(info_ptr->from_host) - + sizeof(info_ptr->from_host.msg_size); + if (bytes_to_come > max_size) { + VelSel_errcode = VELSEL__BAD_RECVLEN; + fprintf(stderr, + "\nVelSel_SendCmnds/recv: pending message length too big" + " - flushing ...\n"); + nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; + while (bytes_to_come > 0) { /* Flush out the incoming message */ + bytes_left = bytes_to_come; + if (bytes_left > max_size) + bytes_left = max_size; + status = recv(info_ptr->asyn_info.skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + VelSel_errcode = VELSEL__BAD_FLUSH; /* TCP/IP problem whilst flushing */ + GetErrno(&VelSel_errno, &VelSel_vaxc_errno); + VelSel_Close(handle, True); /* Force close TCP/IP connection */ + fprintf(stderr, "\nVelSel_SendCmnds/recv: network problem during" + " flush.\nLink to server force-closed.\n"); + return False; + } + bytes_to_come = bytes_to_come - status; + } + fprintf(stderr, "\n flushed OK.\n"); + memset(info_ptr->from_host.msg_size, + '0', sizeof(info_ptr->from_host.msg_size)); + return False; + } else { + nxt_byte_ptr = &info_ptr->from_host.msg_size[size]; + bytes_left = bytes_to_come; + while (bytes_left > 0) { /* Read the rest of the response */ + status = recv(info_ptr->asyn_info.skt, nxt_byte_ptr, bytes_left, 0); + if (status <= 0) { + GetErrno(&VelSel_errno, &VelSel_vaxc_errno); + if (status == 0) { + VelSel_errcode = VELSEL__BAD_RECV1; /* Server exited (probably) */ + fprintf(stderr, "\nVelSel_SendCmnds/recv/1: probable network " + "problem"); + } else { + if (VelSel_errno == EPIPE) { + VelSel_errcode = VELSEL__BAD_RECV1_PIPE; /* Server exited (probably) */ + fprintf(stderr, + "\nVelSel_SendCmnds/recv/1: broken network pipe"); + } else { + VelSel_errcode = VELSEL__BAD_RECV1_NET; /* It's some other net fault */ + perror("VelSel_SendCmnds/recv/1"); + } + } + VelSel_Close(handle, True); /* Force close TCP/IP connection */ + fprintf(stderr, " - link to server force-closed.\n"); + return False; + } + bytes_left = bytes_left - status; + nxt_byte_ptr = nxt_byte_ptr + status; + } + if ((sscanf(info_ptr->from_host.n_rply, "%4d", + &info_ptr->max_replies) != 1) || + (info_ptr->max_replies < 0)) { + VelSel_errcode = VELSEL__BAD_REPLY; /* Reply is bad */ + if (VelSel_call_depth < 5) { /* Add reply to routine stack */ + bytes_to_come = bytes_to_come + 4; + if (bytes_to_come >= sizeof(VelSel_routine[0])) + bytes_to_come = sizeof(VelSel_routine[0]) - 1; + for (i = 0; i < bytes_to_come; i++) { + if (info_ptr->from_host.msg_size[i] == '\0') + info_ptr->from_host.msg_size[i] = '.'; + } + info_ptr->from_host.msg_size[bytes_to_come] = '\0'; + strcpy(VelSel_routine[VelSel_call_depth], + info_ptr->from_host.msg_size); + VelSel_call_depth++; + } + return False; + } + } + VelSel_call_depth--; + return True; +} + /*-------------------------------------------- End of VelSel_Utility.C =======*/