scan345.cpp

Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * scan345: scan345.c 
00004  *
00005  * Copyright by Dr. Claudio Klein, X-Ray Research GmbH.
00006  *
00007  * Version:     3.0
00008  * Date:        31/10/2000
00009  *
00010  **********************************************************************/
00011 
00012 #include <stdio.h>
00013 #include <signal.h>
00014 #include <stdlib.h>
00015 #include <sys/wait.h>
00016 #include <string.h>
00017 #include <unistd.h>
00018 
00019 #ifdef __osf__
00020 #include <sys/types.h>
00021 #include <stdlib.h>
00022 #endif
00023 
00024 #ifdef VMS 
00025 #include <stdlib.h>
00026 #include <unixlib.h>
00027 #include <processes.h>
00028 #endif
00029 
00030 #include "scan345.h"
00031 
00032 #define MAR_GLOBAL 
00033 #include "marcmd.h"
00034 #include "marglobals.h"
00035 #include "mararrays.h"
00036 #include "esd.h"
00037 
00038 #include "marhw.h"
00039 
00040 /*
00041  * Global variables 
00042  */
00043 
00044 int             timeout                 = 0;
00045 int             input_offset            = -999999;
00046 float           input_scale             = -1.0;
00047 int             input_priority          = -1;
00048 int             status_interval         = 0;
00049 char            do_xform                = 0;
00050 
00051 char            input_keep_spiral       = 0;
00052 char            input_keep_image        = 1;    
00053 char            input_skip_op           = 0;    
00054 char            skip_selftest           = 1;    
00055 char            no_xform                = 0;    
00056 
00057 /*
00058  * Local variables 
00059  */
00060 
00061 static char     mar_continue=0;
00062 
00063 Scan345::Scan345(QObject *parent)
00064   : inherited(parent)
00065 {
00066 }
00067 
00068 /*
00069 *************************************************************************
00070 *
00071 * Main Program.
00072 *
00073 *************************************************************************
00074 */
00075 int 
00076 Scan345::scan345_main(int argc, char **argv)
00077 {
00078   int           scanner_status=0;
00079   int           i;
00080 
00081   extern char     keep_image;
00082 
00083   /*
00084    * External functions
00085    */
00086 
00087   strcpy( mar_host, "" );
00088   mar_port = 0;
00089   debug    = 0;
00090 
00091   setlinebuf(stdout);
00092 
00093   /*
00094    * Parse command line
00095    */
00096   for (argv++; argc-- > 1; argv++ ) {
00097 
00098     /* Keep spiral file on output */
00099     if (!strncmp(*argv,"-keep", 5 ) ) {
00100       input_keep_spiral=1;      
00101       keep_spiral=1;    
00102     }
00103 
00104     /* Skip selftest */
00105     else if (!strncmp(*argv,"-nose", 5 ) ) {
00106       skip_selftest=1;  
00107     }
00108 
00109     /* Skip transformation */
00110     else if (!strncmp(*argv,"-noxf", 5) ) {
00111       input_keep_image=0;       
00112       keep_image = 0;
00113       no_xform = 1;
00114     }
00115 
00116     /* Skip image output */
00117     else if (!strncmp(*argv,"-noop", 5) ) {
00118       input_skip_op=1;  
00119     }
00120 
00121     /* Debug */
00122     else if (!strncmp(*argv,"-debug", 6 ) ) {
00123       argc--;
00124       if ( argc <= 0 ) {
00125         usage();
00126       }
00127       else {
00128         argv++;
00129         debug = atoi( *argv );
00130       }
00131     }
00132 
00133     /* Priority */
00134     else if (!strncmp(*argv,"-prio", 5 ) ) {
00135       argc--;
00136       if ( argc <= 0 ) {
00137         usage();
00138       }
00139       else {
00140         argv++;
00141         input_priority = atoi( *argv );
00142       }
00143     }
00144 
00145     /* Net port */
00146     else if (!strncmp(*argv,"-port", 5 ) ) {
00147       argc--;
00148       if ( argc <= 0 ) {
00149         usage();
00150       }
00151       else {
00152         argv++;
00153         mar_port=atoi( *argv );
00154       }
00155     }
00156 
00157     /* Net host */
00158     else if (!strncmp(*argv,"-host", 5 ) ) {
00159       argc--;
00160       if ( argc <= 0 ) {
00161         usage();
00162       }
00163       else {
00164         argv++;
00165         strcpy( mar_host, *argv );
00166       }
00167     }
00168 
00169     /* Help */
00170     else if (!strcmp(*argv,"-h" ) ) {
00171       usage();
00172     }
00173 
00174     /* Apply scale factor to data */
00175     else if (!strncmp(*argv,"-sc", 3 ) ) {
00176       argc--;
00177       if ( argc <= 0 ) {
00178         usage();
00179       }
00180       else {
00181         argv++;
00182         input_scale = atof( *argv );
00183       }
00184     }
00185 
00186     /* Apply offset to data */
00187     else if (!strncmp(*argv,"-off", 4 ) ) {
00188       argc--;
00189       if ( argc <= 0 ) {
00190         usage();
00191       }
00192       else {
00193         argv++;
00194         input_offset = atoi( *argv );
00195       }
00196     }
00197 
00198     /* Print all scanner messages on stdout */
00199     else if (!strncmp(*argv,"-more", 5 ) ) {
00200       argc--;
00201       if ( argc <= 0 ) {
00202         usage();
00203       }
00204       else {
00205         argv++;
00206         verbose=atoi( *argv );
00207         if ( verbose < 0 || verbose > 5 ) {
00208           verbose = 0;
00209           usage();
00210         }
00211       }
00212     }
00213     else {
00214       usage();
00215       exit( 0 );
00216     }
00217 
00218   }
00219 
00220   if ( verbose > 2 ) {
00221     if ( !input_keep_image ) fprintf( stdout, "scan345: Images will not be transformed...\n");
00222     if ( input_keep_spiral       ) fprintf( stdout, "scan345: Spiral files will be produced ...\n");
00223     if ( skip_selftest           ) fprintf( stdout, "scan345: Selftest will be skipped ...\n");
00224   }
00225 
00226   /************************************************
00227    * Figure out if we are in control of scanner 
00228    ************************************************/
00229 
00230   scanner_status = marStartup( );
00231 
00232   if ( !netcontrol ) {
00233     raise(SIGSTOP);
00234     fprintf( stdout, "scan345: continue WITHOUT network control !!!\n");
00235   }
00236 
00237   /* Read scanner status to get current scanner info */
00238   i = get_status();
00239   sleep( 1 );
00240   i = get_status();
00241     
00242   /* Register signal handlers */
00243   if ( netcontrol == 1 ) {
00244     signal( SIGINT,  mar_kill );
00245     signal( SIGKILL, mar_kill );
00246     signal( SIGTERM, mar_kill );
00247     /* Abort signal */
00248     signal( SIGQUIT, mar_abort);
00249     signal( SIGPIPE, mar_kill );
00250   }
00251   else {
00252     signal( SIGCONT, mar_sig );
00253     signal( SIGUSR1, mar_sig );
00254   }
00255 
00256   /* Startup procedure */
00257   i=marTask( MDC_COM_STARTUP, 1.0 );
00258 }
00259 
00260 void Scan345::mar_loop()
00261 {
00262   int i;
00263 
00264   /* Go into command loop */
00265   while( 1 ) {
00266     i=get_status();
00267   }
00268   
00269   mar_quit( (int)0 );
00270 
00271   exit(0);
00272 }
00273 
00274 /******************************************************************
00275  * Function: usage
00276  ******************************************************************/
00277 void Scan345::usage()
00278 {
00279   printf( "scan345: wrong arguments on command line\n");
00280   printf( "Usage is:   scan345 [options]\n");
00281   printf( "       [-more 0...3]         verbose level\n");
00282 #ifdef DEBUG
00283   printf( "       [-debug 0...3]        debugging level\n");
00284 #endif
00285   printf( "       [-noself]             skip selftest\n");
00286   printf( "       [-noxf]               do not transform spirals\n");
00287   printf( "       [-noop]               do not write output file\n");
00288   printf( "       [-host host]          host name of scanner\n");
00289   printf( "       [-port port]          port number of scanner\n");
00290   printf( "       [-setd]               allow to set distance\n");
00291   printf( "       [-off  offset]        apply offset to spiral data\n");
00292   printf( "       [-sc   scale ]        apply scale  to spiral data\n");
00293   printf( "       [-h]                  help\n");
00294   exit( 0 );
00295 }
00296 
00297 /******************************************************************
00298  * Function: mar_sig
00299  ******************************************************************/
00300 void Scan345::mar_sig( int signo )
00301 {
00302   /*
00303    * The signal SIGUSR1 and SIGCONT come from marstart
00304    * SIGUSR1: continue
00305    * SIGUSR2: exit
00306    */
00307 
00308   if ( signo == SIGCONT ) {
00309     printf( "scan345: SIGCONT caught\n");
00310     mar_continue = 1;
00311   }
00312   else if ( signo == SIGUSR1 ) {
00313     printf( "scan345: SIGUSR1 caught\n");
00314     ::exit( 0 );
00315   }
00316 }
00317 
00318 /******************************************************************
00319  * Function: mar_kill
00320  ******************************************************************/
00321 void Scan345::mar_kill( int signo )
00322 {
00323   printf("scan345: Killed by signal %d\n",signo);
00324   mar_hw()->mar_quit( (int)0 );
00325 }