EchoTest.cxx

00001 #define USELIBRARY
00002 
00003 #include <iostream>
00004 
00005 #include "../TApi.h"
00006 
00007 using namespace std;
00008 
00009 struct { 
00010   int repetitions;
00011   unsigned int length;
00012   unsigned long *echoData;
00013   int multiRod;
00014 } globalConf;
00015 
00016 void usage();
00017 void parseArgs(int argc, char **argv);
00018 void buildData();
00019 bool checkResponse(bool verbose, unsigned long *body, unsigned long length);
00020 
00021 int main (int argc, char **argv) {
00022   TApi tapi;
00023 
00024   bool cont = true;
00025 
00026   int count = 0;
00027 
00028   globalConf.repetitions = 1;
00029   globalConf.length = 2;
00030   globalConf.multiRod = 0;
00031 
00032   parseArgs(argc, argv);
00033 
00034   buildData();
00035 
00036   tapi.initialiseAll(0);
00037 
00038   std::list<SctApi::RodLabel> rodList;
00039   if(globalConf.multiRod) {
00040     rodList = tapi.listRods();
00041     cout << "Send echo to All RODs\n";
00042   }
00043 
00044   try {
00045     for(int rep=0; rep<globalConf.repetitions && cont == true; rep++) {
00046       try {
00047         if(cont) {
00048           if(globalConf.multiRod) {
00049             tapi.echoAll(globalConf.length, globalConf.echoData);
00050           } else {
00051             tapi.echo(0, globalConf.length, globalConf.echoData);
00052           }
00053         }
00054       } catch(...) {
00055         cout << "Exception sending echo primitive\n";
00056         cont = false;
00057       }
00058 
00059       try {
00060         if(cont) { 
00061           if(globalConf.multiRod) {
00062 //             cerr << "Starting await response chain\n";
00063             for(list<SctApi::RodLabel>::const_iterator ri = rodList.begin(); ri != rodList.end(); ri++) {
00064 //               cerr << "Going to await response on " 
00065 //                    << ri->partition << " " << ri->crate << " " << ri->rod << "\n";
00066               tapi.awaitResponse(ri->rod);
00067 //               cerr << "Done await response on " 
00068 //                    << ri->partition << " " << ri->crate << " " << ri->rod << "\n";
00069             }
00070 //             cerr << "Done await response\n";
00071           } else {
00072             //         cerr << "Going to await response\n";
00073             tapi.awaitResponse(0);
00074             //         cerr << "Done await response\n";
00075           }
00076         }
00077       } catch(...) {
00078         cout << "Exception awaiting echo response\n";
00079         cont = false;
00080       }
00081 
00082       if(globalConf.multiRod) {
00083 //         cerr << "Starting check response chain\n";
00084         for(list<SctApi::RodLabel>::const_iterator ri = rodList.begin(); ri != rodList.end(); ri++) {
00085 //           cerr << "Going to check response on " 
00086 //                << ri->partition << " " << ri->crate << " " << ri->rod << "\n";
00087           unsigned long length;
00088           unsigned long *body = tapi.getResponse(ri->rod, &length);
00089 
00090           if(body) {
00091             if(!checkResponse(rep == 0, body, length)) {
00092               cout << "Check failed repetition " << rep << "\n";
00093             } else {
00094               count ++;
00095             }
00096           } else {
00097             cout << "Response was null\n";
00098             cont = false;
00099           }
00100 
00101 //           cerr << "Done check response on " 
00102 //                << ri->partition << " " << ri->crate << " " << ri->rod << "\n";
00103         }
00104 //         cerr << "Done check response chain\n";
00105       } else {
00106         unsigned long length;
00107         unsigned long *body = tapi.getResponse(0, &length);
00108 
00109         if(body) {
00110           if(!checkResponse(rep == 0, body, length)) 
00111             cout << "Check failed repetition " << rep << "\n";
00112         } else {
00113           cout << "Response was null\n";
00114           cont = false;
00115         }
00116       }
00117     }
00118     cout << "Complete\n";
00119   } catch(...) { // SctPixelRod::BaseException &b) {
00120     cout << "Got exception!" << endl;
00121   }
00122 
00123   if(globalConf.repetitions > 1) {
00124     cout << "Sent " << count << " successful primitives\n";
00125   }
00126 
00127   delete [] globalConf.echoData;
00128 
00129   tapi.shutdownAll();
00130   //  delete tapi;
00131 }
00132 
00133 bool checkResponse(bool verbose, unsigned long *outBody, unsigned long outLength) {
00134 //   unsigned long outLength;
00135 
00136 //   unsigned long *outBody = tapi.getResponse(0, 0, 0, &outLength);
00137 //   if(outBody) {
00138     if(verbose) {
00139       // Print results (User processing of outList)
00140       unsigned long outIndex = (unsigned long)(outBody[1]);
00141       unsigned long outNumPrims = outBody[2];
00142       unsigned long outPrimVersion = outBody[3];
00143       cerr << "outLength = " << outLength << ", outIndex = " << outIndex
00144            << ", outNumPrims = " << outNumPrims << ", outPrimVersion = "
00145            << outPrimVersion << '\n';
00146       int outPtr = 4;
00147       for (unsigned int j=0; j<outNumPrims; j++) {
00148         unsigned long primLength = outBody[outPtr++];
00149         unsigned long primIndex = outBody[outPtr++];
00150         unsigned long primId = outBody[outPtr++];
00151         unsigned long primVersion = outBody[outPtr++];
00152         cout << "primLength = " << primLength << ", primIndex = " << primIndex 
00153              << ", primId = " << primId << ", primVersion = " << primVersion << '\n';
00154         cout << "ECHO data: \n";
00155         hex(cout);
00156         for (unsigned int i=0; i<primLength-4; i++) {
00157           cout.width(8);
00158           cout << outBody[i+outPtr] <<" ";
00159           if (0 == (i+1)%8) cout << endl; 
00160         }
00161         if (0 != (primLength-4)%8) cout << endl;
00162         dec(cout);
00163       }
00164     }
00165 
00166     bool correct = true;
00167     for(unsigned int i=0; i<globalConf.length; i++) {
00168       if(outBody[i+8] != globalConf.echoData[i]) {
00169         correct = false;
00170         cout << "Check failed at word " << i << "\n";
00171         break;
00172       }
00173     }
00174 
00175     if(correct) cerr << ".";
00176     //         cout << "EchoTest completed successfully?\n";
00177 
00178     delete [] outBody;
00179 
00180     return correct;
00181 }
00182 
00183 void buildData() {
00184   // Create and Send a simple ECHO primitive
00185   globalConf.echoData = new unsigned long[globalConf.length];
00186 
00187   if(globalConf.length < 3) {
00188     if(globalConf.length > 0) globalConf.echoData[0] = 0xDEADF00D;
00189     if(globalConf.length > 1) globalConf.echoData[1] = 0x0;
00190   } else {
00191     for (unsigned int i=0; i<globalConf.length; i++) {
00192       globalConf.echoData[i] = 0xBEAD0000 + i;
00193     }
00194   }
00195 }
00196 
00197 void parseArgs(int argc, char **argv) {
00198   if(argc > 1) {
00199     for(int i=1; i<argc; i++) {
00200       if(argv[i][0] == '-') {
00201         switch(argv[i][1]) {
00202         case 'r':
00203           globalConf.repetitions = atoi(&argv[i][2]);
00204           break;
00205         case 'l':
00206           globalConf.length = atoi(&argv[i][2]);
00207           break;
00208         case 'm':
00209           globalConf.multiRod = 1;
00210           break;
00211         case 'h':
00212           usage();
00213           exit(1);
00214           break;
00215         }
00216       }
00217     }
00218   }
00219 }
00220 
00221 void usage() {
00222   cout << "EchoTest [opts]\n";
00223   cout << "\t-r# Repeat # times\n";
00224   cout << "\t-l# Send echo with length #\n";
00225   cout << "\t-m  Use all RODs\n";
00226   cout << "\t-h  Show this help\n";
00227 }
00228 

Generated on Mon Feb 6 14:01:19 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6