18b52a31eSHanoh Haim/*
28b52a31eSHanoh Haim Hanoh Haim
38b52a31eSHanoh Haim Cisco Systems, Inc.
48b52a31eSHanoh Haim*/
58b52a31eSHanoh Haim
68b52a31eSHanoh Haim/*
78b52a31eSHanoh HaimCopyright (c) 2015-2015 Cisco Systems, Inc.
88b52a31eSHanoh Haim
98b52a31eSHanoh HaimLicensed under the Apache License, Version 2.0 (the "License");
108b52a31eSHanoh Haimyou may not use this file except in compliance with the License.
118b52a31eSHanoh HaimYou may obtain a copy of the License at
128b52a31eSHanoh Haim
138b52a31eSHanoh Haim    http://www.apache.org/licenses/LICENSE-2.0
148b52a31eSHanoh Haim
158b52a31eSHanoh HaimUnless required by applicable law or agreed to in writing, software
168b52a31eSHanoh Haimdistributed under the License is distributed on an "AS IS" BASIS,
178b52a31eSHanoh HaimWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
188b52a31eSHanoh HaimSee the License for the specific language governing permissions and
198b52a31eSHanoh Haimlimitations under the License.
208b52a31eSHanoh Haim*/
218b52a31eSHanoh Haim#include "global_io_mode.h"
228b52a31eSHanoh Haim#include "utl_term_io.h"
238b52a31eSHanoh Haim#include <stdlib.h>
248b52a31eSHanoh Haim
258b52a31eSHanoh Haim
268b52a31eSHanoh Haimvoid CTrexGlobalIoMode::set_mode(CliDumpMode  mode){
278b52a31eSHanoh Haim    switch (mode) {
288b52a31eSHanoh Haim    case  cdDISABLE:
298b52a31eSHanoh Haim        m_g_mode=gDISABLE;
308b52a31eSHanoh Haim        m_g_disable_first=false;
318b52a31eSHanoh Haim        break;
328b52a31eSHanoh Haim    case  cdNORMAL:
338b52a31eSHanoh Haim        Reset();
348b52a31eSHanoh Haim        break;
358b52a31eSHanoh Haim    case cdSHORT:
368b52a31eSHanoh Haim        m_g_mode=gNORMAL;
378b52a31eSHanoh Haim        m_pp_mode=ppDISABLE;
388b52a31eSHanoh Haim        m_ap_mode=apENABLE;
398b52a31eSHanoh Haim        m_l_mode=lDISABLE;
408b52a31eSHanoh Haim        m_rc_mode=rcDISABLE;
418b52a31eSHanoh Haim        break;
428b52a31eSHanoh Haim    }
438b52a31eSHanoh Haim}
448b52a31eSHanoh Haim
458b52a31eSHanoh Haim
468b52a31eSHanoh Haimbool CTrexGlobalIoMode::handle_io_modes(void){
478b52a31eSHanoh Haim    int c=utl_termio_try_getch();
488b52a31eSHanoh Haim    if (c) {
498b52a31eSHanoh Haim        if (c==3) {
508b52a31eSHanoh Haim            return true;
518b52a31eSHanoh Haim        }
528b52a31eSHanoh Haim        switch (c)  {
538b52a31eSHanoh Haim        case ccHELP:
548b52a31eSHanoh Haim            if (m_g_mode==gHELP) {
558b52a31eSHanoh Haim                m_g_mode=gNORMAL;
568b52a31eSHanoh Haim            }else{
578b52a31eSHanoh Haim                m_g_mode=gHELP;
588b52a31eSHanoh Haim            }
598b52a31eSHanoh Haim            break;
608b52a31eSHanoh Haim        case ccGDISABLE:
618b52a31eSHanoh Haim            if (m_g_mode==gDISABLE) {
628b52a31eSHanoh Haim                m_g_mode=gNORMAL;
638b52a31eSHanoh Haim            }else{
648b52a31eSHanoh Haim                m_g_mode=gDISABLE;
658b52a31eSHanoh Haim                m_g_disable_first=true;
668b52a31eSHanoh Haim            }
678b52a31eSHanoh Haim            break;
688b52a31eSHanoh Haim        case ccGNORAML:
698b52a31eSHanoh Haim            Reset();
708b52a31eSHanoh Haim            break;
718b52a31eSHanoh Haim        case ccGPP:
728b52a31eSHanoh Haim            m_g_mode=gNORMAL;
738b52a31eSHanoh Haim            m_pp_mode++;
748b52a31eSHanoh Haim            if (m_pp_mode==ppLAST) {
758b52a31eSHanoh Haim                m_pp_mode = ppDISABLE;
768b52a31eSHanoh Haim            }
778b52a31eSHanoh Haim            break;
788b52a31eSHanoh Haim        case ccGAP:
798b52a31eSHanoh Haim            m_g_mode=gNORMAL;
808b52a31eSHanoh Haim            m_ap_mode++;
818b52a31eSHanoh Haim            if (m_ap_mode == apLAST) {
828b52a31eSHanoh Haim                m_ap_mode = apDISABLE;
838b52a31eSHanoh Haim            }
848b52a31eSHanoh Haim            break;
858b52a31eSHanoh Haim        case ccGL:
868b52a31eSHanoh Haim            m_g_mode=gNORMAL;
878b52a31eSHanoh Haim            m_l_mode++;
888b52a31eSHanoh Haim            if (m_l_mode == lLAST) {
898b52a31eSHanoh Haim                m_l_mode = lDISABLE;
908b52a31eSHanoh Haim            }
918b52a31eSHanoh Haim            break;
928b52a31eSHanoh Haim        case ccGRC:
938b52a31eSHanoh Haim            m_g_mode=gNORMAL;
948b52a31eSHanoh Haim            m_rc_mode++;
958b52a31eSHanoh Haim            if (m_rc_mode == rcLAST) {
968b52a31eSHanoh Haim                m_rc_mode = rcDISABLE;
978b52a31eSHanoh Haim            }
988b52a31eSHanoh Haim            break;
99392f47fbSHanoh Haim        case ccMem:
100392f47fbSHanoh Haim            if ( m_g_mode==gNORMAL ){
101392f47fbSHanoh Haim                m_g_mode=gMem;
102392f47fbSHanoh Haim            }else{
103392f47fbSHanoh Haim                m_g_mode=gNORMAL;
104392f47fbSHanoh Haim            }
105392f47fbSHanoh Haim            break;
1067b9d1088SIdo Barnea        case ccNat:
1077b9d1088SIdo Barnea            m_g_mode=gNAT;
1087b9d1088SIdo Barnea            m_nat_mode++;
1097b9d1088SIdo Barnea            if (m_nat_mode==natLAST) {
1107b9d1088SIdo Barnea                m_nat_mode = natDISABLE;
1117b9d1088SIdo Barnea                m_g_mode = gNORMAL;
1127b9d1088SIdo Barnea            }
1137b9d1088SIdo Barnea            break;
1148b52a31eSHanoh Haim        }
115392f47fbSHanoh Haim
116392f47fbSHanoh Haim
1178b52a31eSHanoh Haim    }
1188b52a31eSHanoh Haim    return false;
1198b52a31eSHanoh Haim}
1208b52a31eSHanoh Haim
1218b52a31eSHanoh Haimvoid CTrexGlobalIoMode::Dump(FILE *fd){
1228b52a31eSHanoh Haim    fprintf(fd,"\033[2J");
1238b52a31eSHanoh Haim    fprintf(fd,"\033[2H");
1248b52a31eSHanoh Haim    fprintf(fd," global: %d \n",(int)m_g_mode);
1258b52a31eSHanoh Haim    fprintf(fd," pp    : %d \n",(int)m_pp_mode);
1268b52a31eSHanoh Haim    fprintf(fd," ap    : %d \n",(int)m_ap_mode);
1278b52a31eSHanoh Haim    fprintf(fd," l     : %d \n",(int)m_l_mode);
1288b52a31eSHanoh Haim    fprintf(fd," rc    : %d \n",(int)m_rc_mode);
1298b52a31eSHanoh Haim}
1308b52a31eSHanoh Haim
1318b52a31eSHanoh Haimvoid CTrexGlobalIoMode::DumpHelp(FILE *fd){
1327b9d1088SIdo Barnea        fprintf(fd, "Help for Interactive Commands\n" );
1337b9d1088SIdo Barnea        fprintf(fd, "  %c  : Toggle, Disable all/Default \n", ccGDISABLE);
1347b9d1088SIdo Barnea        fprintf(fd, "  %c  : Go back to default mode \n", ccGNORAML);
1357b9d1088SIdo Barnea        fprintf(fd, "  %c  : Toggle, Help/Default  \n", ccHELP);
1367b9d1088SIdo Barnea        fprintf(fd, "\n");
1377b9d1088SIdo Barnea        fprintf(fd, "  %c  : Per ports    toggle disable -> table -> normal \n", ccGPP);
1387b9d1088SIdo Barnea        fprintf(fd, "  %c  : Global ports toggle disable/enable \n", ccGAP);
1397b9d1088SIdo Barnea        fprintf(fd, "  %c  : Latency      toggle disable -> enable -> enhanced  \n", ccGL);
1407b9d1088SIdo Barnea        fprintf(fd, "  %c  : Rx check  toggle disable -> enable -> enhanced  \n", ccGRC);
1417b9d1088SIdo Barnea        fprintf(fd, "  %c  : Memory stats toggle disable/enable   \n", ccMem);
1427b9d1088SIdo Barnea        fprintf(fd, "  %c  : NAT pending flows toggle disable/enable   \n", ccNat);
1437b9d1088SIdo Barnea        fprintf(fd, "  Press %c or %c to go back to Normal mode \n", ccHELP, ccGNORAML);
1448b52a31eSHanoh Haim}
1458b52a31eSHanoh Haim
1468b52a31eSHanoh Haim
1478b52a31eSHanoh Haim
148