Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
9.4 KiB

  1. // SacRunner.cpp : Defines the entry point for the console application.
  2. //
  3. #include "inclfile.h"
  4. #include "..\SacCommunicator\SacCommunicator.h"
  5. #include "ntlog.hxx"
  6. #define BUFFER_SIZE 1024
  7. LPCTSTR g_vConnectionParams[]= {"COM1", "9600", "8", "0", "1"};
  8. BOOL g_bVerbose = FALSE;
  9. LPCTSTR g_szInputFileName= NULL;
  10. LPCTSTR g_szRespDir = NULL;
  11. LPCTSTR g_szLogFileName = NULL;
  12. LPCTSTR g_szNtLogFile = NULL;
  13. LPCTSTR g_szBvtLogFile = NULL;
  14. CNtLog g_ntlogLogger;
  15. UINT g_nPassTotal= 0, g_nFailTotal= 0;
  16. BOOL WriteBvtLog(LPCTSTR szBvtLogFile, double ratioPassLimit, UINT nPassTotal, UINT nFailTotal, time_t tmStart, time_t tmFinish)
  17. {
  18. fstream f(szBvtLogFile, std::ios::out);
  19. if (!f.is_open())
  20. return FALSE;
  21. LPCTSTR vTestResults[]= {_T("NOCONFIG"), _T("PASS"), _T("FAIL")};
  22. int nTestResult= !(nPassTotal||nFailTotal) ? 0 : (nPassTotal/(nPassTotal+nFailTotal)<ratioPassLimit)+1;
  23. f<<"[TESTRESULT]\n";
  24. f<<"\tTEST: Basic Sac Stress\n";
  25. f<<"\tRESULT: "<<vTestResults[nTestResult]<<'\n';
  26. f<<"\tCONTACT: ahmedt\n";
  27. f<<"\tPM CONTACT: pasquale\n";
  28. f<<"\tDEV PRIME: adamba\n";
  29. f<<"\tDEV ALT: adamba\n";
  30. f<<"\tTEST PRIME: ahmedt\n";
  31. f<<"\tTEST ALT: rgeorge\n";
  32. struct tm *ptminfoStart, *ptminfoFinish, tminfoStart, tminfoFinish;
  33. ptminfoStart= localtime(&tmStart);
  34. tminfoStart= *ptminfoStart;
  35. ptminfoFinish= localtime(&tmFinish);
  36. tminfoFinish= *ptminfoFinish;
  37. f<<"\tSTART TIME:\t\t"<<tminfoStart.tm_mon<<'/'<<tminfoStart.tm_mday<<'/'<<tminfoStart.tm_year<<' '<<tminfoStart.tm_hour<<':'<<tminfoStart.tm_min<<':'<<tminfoStart.tm_sec<<'\n';
  38. f<<"\tEND TIME:\t\t"<<tminfoFinish.tm_mon<<'/'<<tminfoFinish.tm_mday<<'/'<<tminfoFinish.tm_year<<' '<<tminfoFinish.tm_hour<<':'<<tminfoFinish.tm_min<<':'<<tminfoFinish.tm_sec<<'\n';
  39. f<<"\n\t[Detailed Results]\n";
  40. f<<"\t\tPass Count:"<< g_nPassTotal<<'\n';
  41. f<<"\t\tFail Count:"<< g_nFailTotal<<'\n';
  42. f<<"\t\t\tFor more info check the command log-file: "<< g_szLogFileName<<'\n';
  43. f<<"\t[/Detailed Results]\n\n";
  44. f<<"[/TESTRESULT]\n";
  45. f.close();
  46. return TRUE;
  47. }
  48. BOOL RunSac(LPCTSTR szFileName, int nCommPortId, DCB dcb, BOOL b2Screen, LPCTSTR szLogfileName= NULL, LPCSTR szResponseDir= NULL, LPCTSTR szNtLogFile= NULL)
  49. {
  50. fstream f(szFileName, std::ios::in); // opening file
  51. if (!f.is_open()) // checking if open
  52. return FALSE; // cannot open input file then we have nothing to do
  53. fstream log_f; // log file
  54. BOOL bLog; // whether to log or not
  55. if (szLogfileName&&*szLogfileName) // if name supplied
  56. log_f.open(szLogfileName, std::ios::out); // open file
  57. bLog= log_f.is_open(); // only can log if log file is open
  58. CSacCommunicator SacCl(nCommPortId, dcb); // creating a sac client
  59. if (!SacCl.Connect()) // connecting to sac
  60. {
  61. log_f.close();
  62. f.close();
  63. return FALSE; // cannot init connection
  64. }
  65. // file is open if we r here and connected to port
  66. BOOL bNtLog; // whether or not having an nt log
  67. if (bNtLog= szNtLogFile!=NULL) // in order for that a file name has to be supplied
  68. {
  69. bNtLog= g_ntlogLogger.Init(szNtLogFile); // and intialization has to succeed so as to be able to log
  70. if (bNtLog)
  71. g_ntlogLogger.AttachThread();
  72. }
  73. // poke sac
  74. if (!SacCl.SacCommand( SAC_STR("\r") ))
  75. return FALSE;
  76. int nLineNo= 0;
  77. while (!f.eof())
  78. {
  79. ++nLineNo; // another line
  80. if (b2Screen)
  81. std::cout<<"Line: "<<nLineNo<<'\n'; // echo line #
  82. TCHAR szStaticBuffer[BUFFER_SIZE]; // reading buffer
  83. LPTSTR szBuffer;
  84. szBuffer= szStaticBuffer;
  85. f.getline(szBuffer, BUFFER_SIZE-1); // reading line taking into consideration the \r appended later
  86. while (*szBuffer==' '||*szBuffer=='\t')
  87. szBuffer++; // eat white
  88. if (!*szBuffer||*szBuffer=='#'||*szBuffer=='\r'||*szBuffer=='\n') // skip empty lines
  89. {
  90. if (b2Screen)
  91. std::cout<<"\tline skipped!!"<<std::endl;
  92. continue; // a comment or an empty line
  93. }
  94. int i= 0;
  95. while (szBuffer[i]!='\0'&&szBuffer[i]!=':')
  96. {
  97. if (!_tcsncmp(szBuffer+i, _T("\\\\"), 2))
  98. break; // rest of line comment
  99. i++;
  100. }
  101. int nCount= 1; // default count is 1
  102. if (szBuffer[i]==':') // if count present
  103. {
  104. int j= 1;
  105. while (szBuffer[i+j]!='\0')
  106. {
  107. if (!_tcsncmp(szBuffer+i+j, _T("//"), 2))
  108. break;
  109. j++;
  110. }
  111. szBuffer[i+j]= '\0';
  112. nCount= _ttoi(szBuffer+i+1); // get count
  113. szBuffer[i]='\0'; // remove it from string
  114. }
  115. for (i= _tcslen(szBuffer); i>0&&(szBuffer[i-1]==' '||szBuffer[i-1]=='\t'); i--)
  116. ;
  117. szBuffer[i]='\0';
  118. _tcscat(szBuffer, _T("\r")); // append CR
  119. for (i= 0; i<nCount; i++)
  120. {
  121. BOOL bSuccess;
  122. SacString strResponse;
  123. if (!_tcscmp(szBuffer, "Paging Off\r")) // look for special command
  124. bSuccess= SacCl.PagingOff(strResponse);
  125. else
  126. bSuccess= SacCl.SacCommand(szBuffer, strResponse, FALSE, 5000);
  127. g_nPassTotal+= bSuccess;
  128. g_nFailTotal+= !bSuccess;
  129. if (szResponseDir) // if output dir specified
  130. {
  131. TCHAR szResponseFileBuf[BUFFER_SIZE];
  132. _tcscpy(szResponseFileBuf, szResponseDir);
  133. sprintf(szResponseFileBuf+_tcslen(szResponseFileBuf), "\\L%d_C%d.out", nLineNo, i);
  134. fstream out_f(szResponseFileBuf, std::ios::out);
  135. if (out_f.is_open())
  136. out_f<<strResponse.data(); // log out
  137. out_f.close(); // close out file
  138. }
  139. LPCTSTR vstrStatus[]= { _T("FAILURE"), _T("SUCCESS") }; // status string vector
  140. if (b2Screen)
  141. std::cout<<"\tCount: "<<i+1<< " -> "<< vstrStatus[bSuccess]<< std::endl; // report 2 screen
  142. if (bLog)
  143. {
  144. log_f<< "Line["<< nLineNo<< "]\\ Count[" <<i+1<<"]-> Command: ";
  145. log_f<< szBuffer<< "\tStatus: "<< vstrStatus[bSuccess]<< std::endl; // report to log file
  146. }
  147. if (bNtLog)
  148. if (bSuccess)
  149. if (strResponse.length())
  150. g_ntlogLogger.Pass("Sac Responded -> L[%d], C[%d]: %s", nLineNo, i, szBuffer);
  151. else
  152. g_ntlogLogger.Warn("Sac NIL Response -> L[%d], C[%d]: %s", nLineNo, i, szBuffer);
  153. else
  154. g_ntlogLogger.Fail("Sac Communication Failed -> L[%d], C[%d]: %s", nLineNo, i, szBuffer);
  155. }
  156. }
  157. // finally
  158. goto Terminate;
  159. Terminate:
  160. SacCl.Disconnect(); // close connection
  161. log_f.close(); // close general log
  162. f.close(); // close input file
  163. if (bNtLog)
  164. {
  165. g_ntlogLogger.DetachThread();
  166. g_ntlogLogger.Close();
  167. }
  168. return TRUE; // that's it
  169. }
  170. BOOL GetArgs(int argc, LPTSTR argv[], LPCTSTR szErrBuffer= NULL)
  171. {
  172. if (*argv[1]!='-'&&*argv[1]!='/')
  173. g_szInputFileName= argv[1];
  174. for (int i= 1; i<argc; i++)
  175. {
  176. if (*argv[i]=='-'||*argv[i]=='/')
  177. switch (*(argv[i]+1))
  178. {
  179. case 'I':
  180. if (*(argv[i]+2))
  181. g_szInputFileName= argv[i]+2;
  182. else
  183. g_szInputFileName= argv[++i];
  184. break;
  185. case 'V':
  186. if (!*(argv[i]+2))
  187. g_bVerbose= TRUE;
  188. else
  189. return FALSE;
  190. break;
  191. case 'L':
  192. if (*(argv[i]+2))
  193. g_szLogFileName= argv[i]+2;
  194. else
  195. g_szLogFileName= argv[++i];
  196. break;
  197. case 'N':
  198. if (!_tcsncmp(argv[i]+2, _T("TL"), 2))
  199. if (*(argv[i]+4))
  200. g_szNtLogFile= argv[i]+4;
  201. else
  202. g_szNtLogFile= argv[++i];
  203. break;
  204. case 'B':
  205. if (!_tcsncmp(argv[i]+2, _T("VTL"), 2))
  206. if (*(argv[i]+5))
  207. g_szBvtLogFile= argv[i]+4;
  208. else
  209. g_szBvtLogFile= argv[++i];
  210. break;
  211. case 'D':
  212. if (*(argv[i]+2))
  213. g_szRespDir= argv[i]+2;
  214. else
  215. g_szRespDir= argv[++i];
  216. break;
  217. case 'C': // -C 9600, 8,N, 1
  218. // TCHAR* pCurr= *(argv[i]+2)? (argv[i]+2) : argv[++i];
  219. TCHAR* pCurr= argv[i]+2;
  220. for (int j= 0; j<4; j++)
  221. {
  222. int k;
  223. if (!*pCurr)
  224. pCurr= argv[++i];
  225. for (k= 0; pCurr[k]&&pCurr[k]!=','; )
  226. k++;
  227. pCurr[k]= '\0';
  228. g_vConnectionParams[j]= pCurr;
  229. pCurr+= k+1;
  230. }
  231. break;
  232. }
  233. }
  234. return TRUE;
  235. }
  236. int __cdecl main(int argc, char* argv[])
  237. {
  238. DCB dcb;
  239. GetArgs(argc, argv);
  240. if (!g_szInputFileName)
  241. {
  242. std::cout<< "Erorr: no input file name supplied"<< std::endl;
  243. return !0;
  244. }
  245. std::cout<< "attempting to run w/ following options:"<< std::endl;
  246. if (g_szLogFileName)
  247. std::cout<< "logging to: "<< g_szLogFileName<< '\n';
  248. else
  249. std::cout<< "no log filename supplied, logging truned off\n";
  250. if (g_szNtLogFile)
  251. std::cout<< "nt log will be written to: "<< g_szLogFileName<< ".*\n";
  252. else
  253. std::cout<< "no nt-log filename supplied, nt-logging truned off\n";
  254. if (g_szRespDir)
  255. std::cout<< "sac output files will be located in: "<< g_szRespDir<< "\\\n";
  256. else
  257. std::cout<< "no directory specified for sac output files, using current\n";
  258. if (g_bVerbose)
  259. std::cout<< "running verbose...\n";
  260. else
  261. std::cout<< "running non-verbose...\n";
  262. std::cout<<std::endl;
  263. for (int i= 0; CSacCommunicator::s_vctrCommPorts[i]; i++)
  264. if (!_tcscmp(CSacCommunicator::s_vctrCommPorts[i], g_vConnectionParams[0]))
  265. break;
  266. int nCommPort= i;
  267. BuildCommDCB( CSacCommunicator::s_vctrCommPorts[nCommPort], &dcb);
  268. dcb.BaudRate = _ttoi(g_vConnectionParams[1]); // set the baud rate
  269. dcb.ByteSize = (BYTE) _ttoi(g_vConnectionParams[2]); // data size, xmit, and rcv
  270. dcb.Parity = (BYTE) _ttoi(g_vConnectionParams[3]); // parity bit
  271. dcb.StopBits = (BYTE) _ttoi(g_vConnectionParams[4]); // one stop bit
  272. time_t tmStart, tmFinish;
  273. BOOL bTestResult;
  274. time(&tmStart);
  275. bTestResult= RunSac(g_szInputFileName, nCommPort, dcb, g_bVerbose, g_szLogFileName, g_szRespDir, g_szNtLogFile);
  276. time(&tmFinish);
  277. if (bTestResult)
  278. {
  279. WriteBvtLog(g_szBvtLogFile, 0.5, g_nPassTotal, g_nFailTotal, tmStart, tmFinish);
  280. std::cout<< "\nTEST RESULT: SUCCEEDED!!!\n";
  281. }
  282. else
  283. {
  284. WriteBvtLog(g_szBvtLogFile, 0.5, 0, 0, tmStart, tmFinish);
  285. std::cout<< "\nTEST RESULT: NOCONFIG | FAILED!!!\n";
  286. }
  287. return 0;
  288. }