m_desc.add_options()
("help,h", "Display this help message")
("uuid,u", boost::program_options::value<std::string>(),
- "TA UUID to set / clear port for")
+ "TA UUID to set / clear / query port for")
("port,p", boost::program_options::value<int32_t>(),
"Set debug port number")
- ("clear,c", "Clear debug port number");
+ ("clear,c", "Clear debug port number")
+ ("query,q", "Query debug port number");
}
void DebugPortHandler::readOptions(int argc, char *argv[]) {
void DebugPortHandler::checkExactlyOneMainFlag()
{
- std::vector<std::string> mainOptions = { "port", "clear" };
+ std::vector<std::string> mainOptions = { "port", "clear", "query" };
auto optionCount = [this](std::string &s) { return m_opts.count(s) > 0; };
bool mainOptionCount = std::count_if(mainOptions.begin(),
mainOptions.end(),
readOptionsPermissively(argc, argv);
if (m_opts.count("help"))
return;
+
+ readOptions(argc, argv);
checkExactlyOneMainFlag();
+}
+
+TEEC_UUID DebugPortHandler::getUuidArgument()
+{
+ int ret;
+ TEEC_UUID uuid;
if (!m_opts.count("uuid"))
throw boost::program_options::required_option("uuid");
+ ret = uuidStringToUuid(m_opts["uuid"].as<std::string>(), uuid);
+ if (ret)
+ throw boost::program_options::error(
+ "Invalid UUID format, expected x{8}-x{4}-x{4}-x{4}-x{12}");
+ return uuid;
}
-bool DebugPortHandler::handleCommandlineArgs(
- int argc, char *argv[], SetPortControlCommand &cmd)
+int32_t DebugPortHandler::getPortArgument()
{
- int ret;
+ if (!m_opts.count("port"))
+ throw boost::program_options::required_option("port");
+ int32_t port = m_opts["port"].as<int32_t>();
+ if (port < 0)
+ throw boost::program_options::error(
+ "Port number cannot be negative!");
+ return port;
+}
+int DebugPortHandler::doHandle(int argc, char *argv[])
+{
parseCommandlineArgs(argc, argv);
if (m_opts.count("help")) {
std::cout << m_desc;
- return false;
+ return 0;
}
+ if (m_opts.count("query"))
+ return handleQueryPortCommand();
+ else
+ return handleSetPortCommand();
+}
+
+int DebugPortHandler::handleSetPortCommand()
+{
+ SetPortControlCommand cmd;
+ SetPortControlCommandReply reply;
+ int ret;
+
if (m_opts.count("clear")) {
cmd.clear = true;
cmd.port = 0;
} else if (m_opts.count("port")) {
cmd.clear = false;
- cmd.port = m_opts["port"].as<int32_t>();
- if (cmd.port < 0)
- throw boost::program_options::error(
- "Port number cannot be negative!");
+ cmd.port = getPortArgument();
}
-
- TEEC_UUID taUuid;
- ret = uuidStringToUuid(m_opts["uuid"].as<std::string>(), taUuid);
+ cmd.uuid = getUuidArgument();
+ ret = communicateWithDaemon(CTL_SET_PORT, &cmd, sizeof(cmd),
+ CTL_SET_PORT_REPLY, &reply, sizeof(reply));
if (ret)
- throw boost::program_options::error(
- "Invalid UUID format, expected x{8}-x{4}-x{4}-x{4}-x{12}");
- cmd.uuid = taUuid;
- return true;
+ return ret;
+ if (reply.status != CTL_REPLY_SUCCESS) {
+ std::cerr << "Command failed: " << controlErrors[reply.status] << std::endl;
+ return 1;
+ }
+ return 0;
}
-int DebugPortHandler::handle(int argc, char *argv[])
+int DebugPortHandler::handleQueryPortCommand()
{
+ QueryPortControlCommand cmd;
+ QueryPortControlCommandReply reply;
int ret;
- bool shouldCommunicate;
- SetPortControlCommand cmd;
- SetPortControlCommandReply reply;
-
- try {
- shouldCommunicate = handleCommandlineArgs(argc, argv, cmd);
- } catch (boost::program_options::error &e) {
- std::cerr << "Commandline parsing error: " << e.what() << std::endl;
- std::cerr << m_desc;
- return 1;
- }
- if (!shouldCommunicate)
- return 0;
- ret = communicateWithDaemon(CTL_SET_PORT, &cmd, sizeof(cmd),
- CTL_SET_PORT_REPLY, &reply, sizeof(reply));
+ cmd.uuid = getUuidArgument();
+ ret = communicateWithDaemon(CTL_QUERY_PORT, &cmd, sizeof(cmd),
+ CTL_QUERY_PORT_REPLY, &reply, sizeof(reply));
if (ret)
return ret;
-
if (reply.status != CTL_REPLY_SUCCESS) {
std::cerr << "Command failed: " << controlErrors[reply.status] << std::endl;
return 1;
+ } else {
+ if (reply.clear)
+ std::cout << std::endl;
+ else
+ std::cout << reply.port << std::endl;
}
-
return 0;
}
+
+int DebugPortHandler::handle(int argc, char *argv[])
+{
+ try {
+ return doHandle(argc, argv);
+ } catch (boost::program_options::error &e) {
+ std::cerr << "Commandline parsing error: " << e.what() << std::endl;
+ std::cerr << m_desc;
+ return 1;
+ }
+}
#include <cstring>
#include <memory>
#include <boost/asio.hpp>
+#include <boost/optional.hpp>
+#include <boost/none.hpp>
#include "log.h"
#include "ControlConnectionHandler.h"
#include "IConnectionWriter.h"
switch(cmd) {
case CTL_SET_PORT:
return sizeof(SetPortControlCommand);
+ case CTL_QUERY_PORT:
+ return sizeof(QueryPortControlCommand);
default:
LOGE(SIM_DAEMON, "Invalid command received!");
return -1;
case CTL_SET_PORT:
handleSetPortCommand(data);
break;
+ case CTL_QUERY_PORT:
+ handleQueryPortCommand(data);
default:
handleInvalidCommand();
break;
m_writer->write(CTL_SET_PORT_REPLY, (char *) &reply, sizeof(reply));
}
+void ControlConnectionHandler::handleQueryPortCommand(std::vector<char> &data)
+{
+ struct QueryPortControlCommand cmd;
+ struct QueryPortControlCommandReply reply;
+
+ auto UUIDConfig = getUUIDConfig();
+ if (!UUIDConfig) {
+ reply.status = CTL_REPLY_INTERNAL_ERROR;
+ LOGE(SIM_DAEMON, "Setting UUID debug port failed - config manager not found");
+ m_writer->write(CTL_QUERY_PORT_REPLY, (char *) &reply, sizeof(reply));
+ return;
+ }
+
+ std::memcpy(&cmd, data.data(), sizeof(cmd));
+ boost::optional<uint32_t> port;
+ try {
+ port = UUIDConfig->at(cmd.uuid)->getDebugPort();
+ } catch (std::out_of_range) {
+ port = boost::none;
+ }
+
+ if (port == boost::none) {
+ reply.clear = true;
+ reply.port = 0;
+ } else {
+ reply.clear = false;
+ reply.port = *port;
+ }
+ reply.status = CTL_REPLY_SUCCESS;
+ m_writer->write(CTL_QUERY_PORT_REPLY, (char *) &reply, sizeof(reply));
+}
+
void ControlConnectionHandler::handleInvalidCommand()
{
m_writer->write(CTL_INVALID_CMD_REPLY, nullptr, 0);