ACQ2XX_API
acqcmd.cpp
Go to the documentation of this file.
00001 /* ------------------------------------------------------------------------- */
00002 /* file acqcmd.cpp                                                           */
00003 /* ------------------------------------------------------------------------- */
00004 /*   Copyright (C) 2008 Peter Milne, D-TACQ Solutions Ltd
00005  *                      <Peter dot Milne at D hyphen TACQ dot com>
00006 
00007  This program is free software; you can redistribute it and/or modify
00008  it under the terms of Version 2 of the GNU General Public License
00009  as published by the Free Software Foundation;
00010 
00011  This program is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with this program; if not, write to the Free Software
00018  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
00019 /* ------------------------------------------------------------------------- */
00020 
00021 /** @file acqcmd.cpp implementation of acqcmd using API.
00022  * acqcmd is the interface to the low level command interpreter (see ICD).
00023  */
00024 
00025 #include <popt.h>
00026 
00027 #include "local.h"
00028 #include "acq2xx_api.h"
00029 #include "acq_transport.h"
00030 
00031 int acq200_debug;
00032 int timeout;
00033 char* device;
00034 int do_shell;
00035 
00036 #define MAXBUF 1024
00037 char command[MAXBUF] = {};
00038 char response[MAXBUF] = {};
00039 char *poll_while;
00040 char *poll_until;
00041 
00042 int main(int argc, const char** argv)
00043 {
00044         struct poptOption opt_table[] = {
00045                 { "device",     'f', POPT_ARG_STRING, &device,     'f' },
00046                 { "board",      'b', POPT_ARG_STRING, &device,      'b' },
00047                 { "shell",      's', POPT_ARG_STRING, &device,      's' },
00048                 { "verbose",    'v', POPT_ARG_INT,    &acq200_debug, 0   },
00049                 { "version",    0,   POPT_ARG_NONE,   0,           'V' },
00050                 { "while",      'w', POPT_ARG_STRING, &poll_while, 'p' },
00051                 { "until",      'u', POPT_ARG_STRING, &poll_until, 'p' },
00052                 { "timeout",    't', POPT_ARG_INT,    &timeout,    't',
00053                   "timeout in seconds"},
00054                 POPT_AUTOHELP
00055                 POPT_TABLEEND           
00056         };
00057         int rc;
00058         poptContext opt_context = 
00059                 poptGetContext(argv[0], argc, argv, opt_table, 0);
00060 
00061         while ( (rc = poptGetNextOpt(opt_context)) > 0 ){
00062                 switch(rc){
00063                 case 's':
00064                         do_shell = 1;
00065                         break;  
00066                 }
00067         }       
00068 
00069         if (device == 0){
00070                 err("usage acqcmd -f DEVICE command ...");
00071                 return -1;
00072         }
00073         Transport *t = Transport::getTransport(device); 
00074 
00075         if (poll_while){
00076                 rc = t->acqcmd("getState", response, MAXBUF);
00077                 while (strstr(response, poll_while)){
00078                         rc = t->waitStateChange(timeout, response, MAXBUF);
00079                         if (rc < 0){
00080                                 exit(rc);
00081                         }
00082                 }       
00083         }else if (poll_until){
00084                 rc = t->acqcmd("getState", response, MAXBUF);
00085                 while (strstr(response, poll_until) == 0){
00086                         rc = t->waitStateChange(timeout, response, MAXBUF);
00087                         if (rc < 0){
00088                                 exit(rc);
00089                         }
00090                 }
00091         }else{
00092 
00093                 if (poptPeekArg(opt_context)){  
00094                         for (int first=1; poptPeekArg(opt_context); first=0){
00095                                 if (!first){
00096                                         strcat(command, " ");
00097                                 }
00098                                 strcat(command, poptGetArg(opt_context));
00099                         }
00100 
00101                         if (do_shell){
00102                                 rc = t->acq2sh(command, response, MAXBUF);
00103                         }else{
00104                                 rc = t->acqcmd(command, response, MAXBUF);
00105                         }
00106 
00107                         puts(response);
00108 
00109                         if (STATUS_IS_OK(rc)){
00110                                 return 0;
00111                         }else{
00112                                 exit(rc);
00113                         }
00114                 }else{
00115                         // no batch processing at this time.
00116                         exit(0);
00117                 }
00118         }
00119 }