RDK Documentation (Open Sourced RDK Components)
AampcliCommandHandler.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2022 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 /**
21  * @file AampcliCommandHandler.cpp
22  * @brief Aampcli Command register and handler
23  */
24 
25 #include "AampcliCommandHandler.h"
26 #include "AampcliGet.h"
27 #include "AampcliSet.h"
28 #include "AampcliPlaybackCommand.h"
29 #include "AampcliSmokeTest.h"
30 
31 std::map<std::string, Command*> CommandHandler::mCommandMap = std::map<std::string, Command*>();
32 
33 void CommandHandler::registerAampcliCommands()
34 {
35  registerAllCommands();
36  registerCommandObjects();
37 }
38 
39 void CommandHandler::registerCommandObjects()
40 {
41  registerCommand( "set", new Set);
42  registerCommand( "get", new Get);
43  registerCommand( "harvest", new Harvestor);
44  registerCommand( "smoketest", new SmokeTest);
45  registerCommand( "default", new PlaybackCommand);
46 }
47 
48 void CommandHandler::registerCommand(const std::string& commandName, Command* command)
49 {
50  std::map<std::string, Command*>::iterator cmdPair = mCommandMap.find(commandName);
51  if (cmdPair != mCommandMap.end())
52  {
53  printf("%s:%d: Command already registered\n", __FUNCTION__, __LINE__);
54  }
55  else
56  {
57  mCommandMap[commandName] = command;
58  }
59 }
60 
61 bool CommandHandler::dispatchAampcliCommands(char *cmd, PlayerInstanceAAMP *playerInstanceAamp)
62 {
63  bool l_status = true;
64  char l_cmd[4096] = {'\0'};
65  strcpy(l_cmd,cmd);
66  char *token = strtok(l_cmd," ");
67 
68  if (token != NULL)
69  {
70  std::map<std::string, Command*>::iterator cmdPair;
71  cmdPair = mCommandMap.find(token);
72 
73  if(cmdPair == mCommandMap.end())
74  {
75  cmdPair = mCommandMap.find("default");
76  }
77 
78  Command* l_Command = cmdPair->second;
79  l_status = l_Command->execute(cmd,playerInstanceAamp);
80 
81  }
82 
83  return l_status;
84 }
85 
86 void CommandHandler::registerAllCommands()
87 {
88  PlaybackCommand lPlaybackCommand;
89  Set lSet;
90  Get lGet;
91 
92  lPlaybackCommand.registerPlaybackCommands();
93  lGet.registerGetCommands();
94  lSet.registerSetCommands();
95 
96 }
97 
98 char ** CommandHandler::commandCompletion(const char *text, int start, int end)
99 {
100  PlaybackCommand lPlaybackCommand;
101  Set lSet;
102  Get lGet;
103 
104  char *buffer = rl_line_buffer;
105 
106  rl_attempted_completion_over = 1;
107 
108  if(strncmp(buffer, "get", 3) == 0)
109  {
110  return rl_completion_matches(text, lGet.getCommandRecommender);
111  }
112  else if (strncmp(buffer, "set", 3) == 0)
113  {
114  return rl_completion_matches(text, lSet.setCommandRecommender);
115  }
116  else
117  {
118  return rl_completion_matches(text, lPlaybackCommand.commandRecommender);
119  }
120 
121 }
122 
Get::registerGetCommands
void registerGetCommands()
Show help menu with aamp command line interface.
Definition: AampcliGet.cpp:251
Set::registerSetCommands
void registerSetCommands()
Show help menu with aamp command line interface.
Definition: AampcliSet.cpp:1222
AampcliGet.h
AampcliGet header file.
AampcliSet.h
AampcliSet header file.
AampcliPlaybackCommand.h
AAMPcliPlaybackCommand header file.
PlayerInstanceAAMP
Player interface class for the JS pluggin.
Definition: main_aamp.h:692
SmokeTest
Definition: AampcliSmokeTest.h:31
AampcliCommandHandler.h
AampcliCommandHandler header file.
Get
Definition: AampcliGet.h:38
Harvestor
Definition: AampcliHarvestor.h:56
Command
Definition: AampcliCommandHandler.h:35
Set
Definition: AampcliSet.h:42
PlaybackCommand
Definition: AampcliPlaybackCommand.h:32
AampcliSmokeTest.h
AampcliSmokeTest header file.