Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_KeyboardControl.cpp

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #include "C_KeyboardControl.hpp"
00021 
00022 #include "Utils.hpp"
00023 #include "GeneratorTrace.hpp"
00024 
00025 #include "C_Generator.hpp"
00026 
00027 #include "integer_t.hpp" // for strtoul_f
00028 
00029 #include <pthread.h> // for sched_yield()
00030 #include <cstring>
00031 #include <cctype>
00032 #include <sys/time.h>
00033 
00034 C_KeyboardControl::C_KeyboardControl():C_TaskControl(){
00035   m_gen = NULL ;
00036 }
00037 
00038 C_KeyboardControl::~C_KeyboardControl(){
00039   m_gen = NULL ;
00040 }
00041 
00042 void C_KeyboardControl::init_controller_configuration () {
00043 
00044   if (tcgetattr(0, &m_saved_conf) < 0) {
00045     GEN_FATAL(E_GEN_NO_ERROR, "Keyboard saved configuration failure");
00046   }
00047 
00048 
00049   m_controller_conf = m_saved_conf ;
00050 
00051   m_controller_conf.c_lflag &= ~(ICANON|ECHO);
00052   m_controller_conf.c_cc[VMIN]  = 0;
00053   m_controller_conf.c_cc[VTIME] = 0;
00054 
00055   if (tcsetattr (0, TCSANOW, &m_controller_conf) < 0) {
00056     GEN_FATAL(E_GEN_NO_ERROR, "Keyboard initial configuration failure");
00057   }
00058 }
00059 
00060 void C_KeyboardControl::controller_configuration () {
00061   if (tcsetattr (0, TCSANOW, &m_controller_conf) < 0) {
00062     GEN_FATAL(E_GEN_NO_ERROR, "Keyboard setting configuration fatal error");
00063   }
00064 }
00065 
00066 void C_KeyboardControl::system_configuration () {
00067   if (tcsetattr (0, TCSANOW, &m_saved_conf) < 0) {
00068     GEN_FATAL(E_GEN_NO_ERROR, "Keyboard restore configuration fatal error");
00069   }
00070 }
00071 
00072 void C_KeyboardControl::init(C_Generator *P_gen) {
00073   init_controller_configuration();
00074   m_gen = P_gen ;
00075   C_TaskControl::init() ;
00076 }
00077 
00078 T_GeneratorError C_KeyboardControl::InitProcedure() {
00079   GEN_DEBUG(0, "C_KeyboardControl::doInit() start");
00080   GEN_DEBUG(0, "C_KeyboardControl::doInit() end");
00081   return (E_GEN_NO_ERROR);
00082 }
00083 
00084 T_GeneratorError C_KeyboardControl::TaskProcedure() {
00085 
00086   fd_set              L_ReadMask ;
00087   int                 L_n = 0    ;
00088   int                 L_rc       ;
00089   struct timeval      L_TimeOut  ;
00090   char                L_char     ;
00091 
00092   GEN_DEBUG(0, "C_KeyboardControl::doTask() start");
00093 
00094   L_TimeOut.tv_sec        = 1 ;
00095   L_TimeOut.tv_usec       = 0 ;
00096 
00097   FD_ZERO(&L_ReadMask);
00098   FD_SET(0, &L_ReadMask); // O for stdin
00099 
00100   L_n = select(1,&L_ReadMask,NULL,NULL,&L_TimeOut);
00101   // no check on select error on purpose
00102   // select popped with event
00103   if (L_n > 0) {
00104 
00105     if (FD_ISSET(0, &L_ReadMask)) {
00106       L_rc = read(0, &L_char, 1) ;
00107       if (L_rc == 1) { execute_cmd(L_char); }
00108     }
00109     
00110   } // if L_n > 0
00111 
00112   GEN_DEBUG(0, "C_KeyboardControl::doTask() end");
00113   return (E_GEN_NO_ERROR);
00114 }
00115 
00116 T_GeneratorError C_KeyboardControl::EndProcedure() {
00117   system_configuration();
00118   return (E_GEN_NO_ERROR);
00119 }
00120 
00121 T_GeneratorError C_KeyboardControl::ForcedStoppingProcedure() {
00122   return (E_GEN_NO_ERROR);
00123 }
00124 
00125 T_GeneratorError C_KeyboardControl::StoppingProcedure() {
00126   M_state = C_TaskControl::E_STATE_STOPPED ;
00127   return (E_GEN_NO_ERROR);
00128 }
00129 
00130 void C_KeyboardControl::force_end_procedure () {
00131   (void) EndProcedure() ;
00132 }
00133 
00134 void C_KeyboardControl::execute_cmd(char P_char) {
00135 
00136   switch (P_char) {
00137   case 'a':
00138     m_gen->activate_percent_traffic();
00139     break ;
00140 
00141   case 'd':
00142     m_gen->reset_cumul_counters();
00143     break ;
00144   case 'q':
00145     m_gen->stop();
00146     break ;
00147   case 'p':
00148     m_gen->pause_traffic();
00149     break ;
00150   case 'r':
00151     m_gen->restart_traffic();
00152     break ;
00153     
00154   case 'f':
00155     m_gen->force_init();
00156     break ;
00157     
00158   case 'c': {
00159     char L_command[100] ;
00160     m_gen->pause_display() ;
00161     system_configuration() ;
00162     fprintf(stderr, "Command: ");
00163     memset(L_command, '\0', 100);
00164     read(0, L_command, 100);
00165     controller_configuration();
00166     m_gen->pause_display() ;
00167     analyze_cmd(L_command) ;
00168   }
00169   break ;
00170   
00171   case '+':
00172     m_gen->change_call_rate(E_GEN_OP_INCREASE, 0L);
00173     break ;
00174 
00175   case '-':
00176     m_gen->change_call_rate(E_GEN_OP_DECREASE, 0L);
00177     break ;
00178 
00179   default:
00180     m_gen->set_screen(P_char) ;
00181     break ;
00182 
00183   }
00184 }
00185 
00186 void C_KeyboardControl::analyze_cmd(char *P_cmd) {
00187 
00188   size_t  L_cmd_len, L_pos ;
00189   char   *L_ptr ;
00190 
00191   bool    L_cmd_found = false ;
00192   bool    L_cmd_error = false ;
00193   char   *L_arg1 = NULL;
00194   char   *L_arg2 = NULL;
00195   unsigned long L_value ;
00196   char *L_end_str = NULL ;
00197 
00198   L_cmd_len = strlen(P_cmd);
00199   L_pos = 0 ;
00200   while (isspace(P_cmd[L_pos]) && (L_pos < L_cmd_len)) {
00201     L_pos++ ;
00202   }
00203   if (L_pos == L_cmd_len) L_cmd_error = true ;
00204 
00205   if (L_cmd_error == false) {
00206     L_ptr = strstr(&P_cmd[L_pos], "set");
00207     if (L_ptr == &P_cmd[L_pos]) {
00208       L_pos += 3 ;
00209       if (L_pos >= L_cmd_len) L_cmd_error = true ;
00210     } else {
00211       L_cmd_error = true ;
00212     }
00213   }
00214 
00215   // skip space
00216   if (L_cmd_error == false) {
00217     while (isspace(P_cmd[L_pos]) && (L_pos < L_cmd_len)) {
00218       L_pos++ ;
00219     }
00220     if (L_pos == L_cmd_len) L_cmd_error = true ;
00221   }
00222 
00223   if (L_cmd_error == false) {
00224     L_arg1 = &P_cmd[L_pos];
00225     while ((!isspace(P_cmd[L_pos])) && (L_pos < L_cmd_len)) {
00226       L_pos++ ;
00227     }
00228     if (L_pos == L_cmd_len) { 
00229       L_cmd_error = true ; 
00230     } else {
00231       P_cmd[L_pos] = '\0' ;
00232       L_pos++ ;
00233       if (L_pos == L_cmd_len) L_cmd_error = true ;
00234     }
00235   }
00236    
00237   if (L_cmd_error == false) {
00238     while (isspace(P_cmd[L_pos]) && (L_pos < L_cmd_len)) {
00239     }
00240     if (L_pos == L_cmd_len) L_cmd_error = true ;
00241   }  
00242     
00243   if (L_cmd_error == false) {
00244     L_arg2 = &P_cmd[L_pos];
00245     while ((!isspace(P_cmd[L_pos])) && (L_pos < L_cmd_len)) {
00246       L_pos++ ;
00247     }
00248     if (L_pos == L_cmd_len) { 
00249       L_cmd_error = true ; 
00250     } else {
00251       P_cmd[L_pos] = '\0' ;
00252       L_cmd_found = true ;
00253     }
00254   }
00255     
00256   if (L_cmd_found == true) {
00257     
00258     if (strcmp(L_arg1, "call-rate") == 0) {
00259       L_value = strtoul_f(L_arg2, &L_end_str,10) ;
00260       if (L_end_str[0] != '\0') { // not a number
00261         L_cmd_error = true ;
00262       } else { // execute command
00263         m_gen->change_call_rate(E_GEN_OP_SET_VALUE, L_value);
00264       }
00265     } else if (strcmp(L_arg1, "burst-limit") == 0) {
00266       L_value = strtoul_f(L_arg2, &L_end_str,10) ;
00267       if (L_end_str[0] != '\0') { // not a number
00268         L_cmd_error = true ;
00269       } else { // execute command
00270         m_gen->change_burst(L_value);
00271       }
00272     } else if (strcmp(L_arg1, "call-rate-scale") == 0) {
00273       L_value = strtoul_f(L_arg2, &L_end_str,10) ;
00274       if (L_end_str[0] != '\0') { // not a number
00275         L_cmd_error = true ;
00276       } else { // execute command
00277         m_gen->change_rate_scale(L_value);
00278       }
00279     } else if (strcmp(L_arg1, "display-period") == 0) {
00280       L_value = strtoul_f(L_arg2, &L_end_str,10) ;
00281       if (L_end_str[0] != '\0') { // not a number
00282         L_cmd_error = true ;
00283       } else { // execute command
00284         m_gen->change_display_period(L_value);
00285       }
00286     } else {
00287       L_cmd_found = false ;
00288       L_cmd_error = true ;
00289     }
00290   }
00291     
00292   if (L_cmd_error == true) {
00293 
00294     GEN_ERROR(E_GEN_FATAL_ERROR, "Unknown command ignored");
00295   }
00296 
00297 }
00298 
00299 // end of file

Generated on Wed Mar 7 14:44:50 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002