Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifndef _COMMANDSHANDLER_H_ |
| 31 | #define _COMMANDSHANDLER_H_ |
| 32 | |
| 33 | #include <iostream> |
| 34 | #include <memory> |
| 35 | #include <map> |
| 36 | #include "MessageParser.h" |
| 37 | #include "HostDefinitions.h" |
| 38 | #include <sstream> |
| 39 | |
| 40 | class Host; |
| 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | |
| 45 | // ************************************************************************************************* |
| 46 | |
| 47 | class CommandsHandler |
| 48 | { |
| 49 | public: |
| 50 | /* |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 51 | * pCommandFunction is used for the functions map - each function gets two arguments: |
| 52 | * vector of strings which holds the arguments and the number of arguments in that vector |
| 53 | */ |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 54 | typedef ResponseMessage(CommandsHandler::*pCommandFunction)(vector<string>, unsigned int); |
| 55 | |
| 56 | /* |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 57 | * The constructor inserts each one of the available functions into the map - m_functionHandler - according to server type (TCP/UDP) |
| 58 | */ |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 59 | CommandsHandler(ServerType type, Host& host); |
| 60 | |
| 61 | ConnectionStatus ExecuteCommand(string message, ResponseMessage &referencedResponse); |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 62 | ConnectionStatus ExecuteBinaryCommand(uint8_t* binaryInput, ResponseMessage &referencedResponse); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 63 | |
| 64 | private: |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 65 | //m_functionHandler is a map that maps a string = command name, to a function |
| 66 | map<string, pCommandFunction> m_functionHandler; |
| 67 | Host& m_host; // a refernce to the host (enables access to deviceManager and hostInfo) |
| 68 | |
| 69 | enum CommandsHandlerResponseStatus |
| 70 | { |
| 71 | chrsInvalidNumberOfArguments, |
| 72 | chrsInvalidArgument, |
| 73 | chrsOperationFailure, |
| 74 | chrsLinuxSupportOnly, |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 75 | chrsDeviceIsSilent, |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 76 | chrsSuccess |
| 77 | }; |
| 78 | |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 79 | string GetCommandsHandlerResponseStatusString(CommandsHandlerResponseStatus status); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 80 | |
| 81 | /* |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 82 | FormatResponseMessage |
| 83 | Decorate the response message with time stamp and a success status |
| 84 | @param: successStatus - true for a successful operation, false otherwise |
| 85 | @param: message - the content of the response |
| 86 | @return: the decorated response |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 87 | */ |
| 88 | string DecorateResponseMessage(bool successStatus, string message = ""); |
| 89 | |
| 90 | // **********************************Commands Functions:**************************************** |
| 91 | ResponseMessage GetInterfaces(vector<string> arguments, unsigned int numberOfArguments); |
| 92 | |
| 93 | ResponseMessage OpenInterface(vector<string> arguments, unsigned int numberOfArguments); |
| 94 | |
| 95 | ResponseMessage CloseInterface(vector<string> arguments, unsigned int numberOfArguments); |
| 96 | |
| 97 | ResponseMessage Read(vector<string> arguments, unsigned int numberOfArguments); |
| 98 | |
| 99 | ResponseMessage Write(vector<string> arguments, unsigned int numberOfArguments); |
| 100 | |
| 101 | ResponseMessage ReadBlock(vector<string> arguments, unsigned int numberOfArguments); |
| 102 | |
| 103 | ResponseMessage WriteBlock(vector<string> arguments, unsigned int numberOfArguments); |
| 104 | |
| 105 | ResponseMessage InterfaceReset(vector<string> arguments, unsigned int numberOfArguments); |
| 106 | |
| 107 | ResponseMessage SwReset(vector<string> arguments, unsigned int numberOfArguments); |
| 108 | |
| 109 | ResponseMessage AllocPmc(vector<string> arguments, unsigned int numberOfArguments); |
| 110 | |
| 111 | ResponseMessage DeallocPmc(vector<string> arguments, unsigned int numberOfArguments); |
| 112 | |
| 113 | ResponseMessage CreatePmcFile(vector<string> arguments, unsigned int numberOfArguments); |
| 114 | |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 115 | ResponseMessage FindPmcFile(vector<string> arguments, unsigned int numberOfArguments); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 116 | |
| 117 | ResponseMessage SendWmi(vector<string> arguments, unsigned int numberOfArguments); |
| 118 | |
| 119 | ResponseMessage GetTime(vector<string> arguments, unsigned int numberOfArguments); |
| 120 | |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 121 | ResponseMessage SetDriverMode(vector<string> arguments, unsigned int numberOfArguments); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 122 | |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 123 | ResponseMessage GetHostManagerVersion(vector<string> arguments, unsigned int numberOfArguments); |
| 124 | |
| 125 | ResponseMessage DriverControl(vector<string> arguments, unsigned int numberOfArguments); |
| 126 | |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 127 | ResponseMessage DriverCommand(vector<string> arguments, unsigned int numberOfArguments); |
| 128 | |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 129 | ResponseMessage GenericDriverIO(vector<string> arguments, void* inputBuf, unsigned int inputBufSize); |
| 130 | |
| 131 | ResponseMessage GetDeviceSilenceMode(vector<string> arguments, unsigned int numberOfArguments); |
| 132 | |
| 133 | ResponseMessage SetDeviceSilenceMode(vector<string> arguments, unsigned int numberOfArguments); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 134 | |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 135 | ResponseMessage GetConnectedUsers(vector<string> arguments, unsigned int numberOfArguments); |
| 136 | |
| 137 | ResponseMessage GetDeviceCapabilities(vector<string> arguments, unsigned int numberOfArguments); |
| 138 | |
| 139 | bool ValidArgumentsNumber(string functionName, size_t numberOfArguments, size_t expectedNumOfArguments, string& responseMessage); |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 140 | |
| 141 | /* |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 142 | GetHostNetworkInfo |
| 143 | Return host's IP and host's alias as the Response |
| 144 | @param: an empty vector |
| 145 | @return: a response with a string that contains both host's IP and host's alias |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 146 | */ |
| 147 | ResponseMessage GetHostNetworkInfo(vector<string> arguments, unsigned int numberOfArguments); |
| 148 | |
| 149 | /* |
Vadim Iosevich | 40679a1 | 2017-06-06 14:24:07 +0300 | [diff] [blame] | 150 | SetHostAlias |
| 151 | Get a new alias and define it as the new host's alias |
| 152 | @param: a vector with one string representing the new alias |
| 153 | @return: a response with feedback about the operation status (success/failure) |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 154 | */ |
| 155 | ResponseMessage SetHostAlias(vector<string> arguments, unsigned int numberOfArguments); |
| 156 | |
Vadim Iosevich | 20d50d8 | 2017-09-19 11:38:15 +0300 | [diff] [blame] | 157 | ResponseMessage GetHostAlias(vector<string> arguments, unsigned int numberOfArguments); |
| 158 | |
Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 159 | ResponseMessage GetHostCapabilities(vector<string> arguments, unsigned int numberOfArguments); |
| 160 | |
| 161 | ResponseMessage OnTargetLogRecording(vector<string> arguments, unsigned int numberOfArguments); |
| 162 | |
Vadim Iosevich | d50ea46 | 2017-03-30 16:19:08 +0300 | [diff] [blame] | 163 | const char m_device_delimiter = ' '; |
| 164 | |
| 165 | const char m_array_delimiter = ' '; |
| 166 | |
| 167 | const char m_reply_feilds_delimiter = '|'; |
| 168 | }; |
| 169 | |
| 170 | |
| 171 | #endif // !_COMMANDSHANDLER_H_ |