Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 1427fc1

Browse files
committed
Merge pull request #324 from krocard/make-command-interface-public
Allow a client to send "remote" commands programmatically Users can already send commands to a Parameter Framework instance through a socket (provided that they asked for this interface to be started). Some features are only available through this interface. In upcoming patches, we want to use those features and send such commands but we don't want to use a remote interface, which is sometimes painful. This patch makes the "remote" commands available through a programmatic API. This API expects a command name and a string-vector containing the arguments; which makes it fairly easy to use and to implement a user interface providing the same commands as they are used to - but without the need of a socket. The object that provides this API is allocated by the Parameter Framework but is owned by the client - we want to be C++03-retrocompatible, which forbids us from returning a unique_ptr, this is why we return a raw pointer.
2 parents 82a11ba + 231d6d1 commit 1427fc1

29 files changed

+668
-308
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,5 @@ PenaltyReturnTypeOnItsOwnLine: 10000
9090
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH, GIVEN, WHEN, AND_WHEN, THEN, AND_THEN,
9191
SECTION ]
9292

93+
SortIncludes: 'false'
9394
...

parameter/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ add_library(parameter SHARED
3636
BitParameterType.cpp
3737
BitwiseAreaConfiguration.cpp
3838
BooleanParameterType.cpp
39+
CommandHandlerWrapper.cpp
3940
ComponentInstance.cpp
4041
ComponentLibrary.cpp
4142
ComponentType.cpp
@@ -107,7 +108,7 @@ generate_export_header(parameter)
107108
target_link_libraries(parameter
108109
# Unfortunatly xmlSink and xmlSource need to be exposed to the plugins
109110
PUBLIC xmlserializer
110-
PRIVATE remote-processor pfw_utility
111+
PRIVATE pfw_utility remote-processor
111112
PRIVATE ${CMAKE_DL_LIBS})
112113

113114
target_include_directories(parameter
@@ -121,6 +122,7 @@ install(TARGETS parameter LIBRARY DESTINATION lib RUNTIME DESTINATION bin ARCHIV
121122
# Client headers
122123
install(FILES
123124
"${CMAKE_CURRENT_BINARY_DIR}/parameter_export.h"
125+
include/CommandHandlerInterface.h
124126
include/ElementHandle.h
125127
include/ParameterHandle.h
126128
include/ParameterMgrLoggerForward.h
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2015, Intel Corporation
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include "CommandHandlerWrapper.h"
32+
#include <RequestMessage.h>
33+
34+
CommandHandlerWrapper::CommandHandlerWrapper(std::unique_ptr<IRemoteCommandHandler> &&wrapped)
35+
: mWrapped(std::move(wrapped))
36+
{
37+
}
38+
39+
bool CommandHandlerWrapper::process(const std::string &command,
40+
const std::vector<std::string> &arguments, std::string &output)
41+
{
42+
CRequestMessage request(command);
43+
44+
for (auto &arg : arguments) {
45+
request.addArgument(arg);
46+
}
47+
48+
return mWrapped->remoteCommandProcess(request, output);
49+
}

parameter/CommandHandlerWrapper.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2015, Intel Corporation
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
#pragma once
31+
32+
#include "CommandHandlerInterface.h"
33+
#include <RemoteCommandHandler.h>
34+
#include <memory>
35+
36+
class CommandHandlerWrapper : public CommandHandlerInterface
37+
{
38+
public:
39+
CommandHandlerWrapper(std::unique_ptr<IRemoteCommandHandler> &&wrapped);
40+
41+
bool process(const std::string &command, const std::vector<std::string> &arguments,
42+
std::string &output) override;
43+
44+
private:
45+
std::unique_ptr<IRemoteCommandHandler> mWrapped;
46+
};

parameter/ParameterMgr.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,8 +2643,8 @@ bool CParameterMgr::serializeElement(std::ostream &output,
26432643

26442644
// Use a doc source by loading data from instantiated Configurable Domains
26452645
CXmlMemoryDocSource memorySource(&element, _bValidateSchemasOnStart,
2646-
element.getXmlElementName(),
2647-
"parameter-framework", getVersion());
2646+
element.getXmlElementName(), "parameter-framework",
2647+
getVersion());
26482648

26492649
// Use a doc sink to write the doc data in a stream
26502650
CXmlStreamDocSink sink(output);
@@ -2813,8 +2813,8 @@ void CParameterMgr::feedElementLibraries()
28132813
pParameterCreationLibrary->addElementBuilder(
28142814
"FloatingPointParameter", new TNamedElementBuilderTemplate<CFloatingPointParameterType>);
28152815
pParameterCreationLibrary->addElementBuilder(
2816-
"SubsystemInclude", new CFileIncluderElementBuilder(_bValidateSchemasOnStart,
2817-
getSchemaUri()));
2816+
"SubsystemInclude",
2817+
new CFileIncluderElementBuilder(_bValidateSchemasOnStart, getSchemaUri()));
28182818

28192819
_pElementLibrarySet->addElementLibrary(pParameterCreationLibrary);
28202820

@@ -2843,22 +2843,9 @@ void CParameterMgr::setForceNoRemoteInterface(bool bForceNoRemoteInterface)
28432843
_bForceNoRemoteInterface = bForceNoRemoteInterface;
28442844
}
28452845

2846-
// Remote Processor Server connection handling
2847-
bool CParameterMgr::handleRemoteProcessingInterface(string &strError)
2846+
CParameterMgr::CommandHandler CParameterMgr::createCommandHandler()
28482847
{
2849-
LOG_CONTEXT("Handling remote processing interface");
2850-
2851-
if (_bForceNoRemoteInterface) {
2852-
// The user requested not to start the remote interface
2853-
return true;
2854-
}
2855-
2856-
// Start server only if tuning allowed
2857-
if (!getConstFrameworkConfiguration()->isTuningAllowed()) {
2858-
return true;
2859-
}
2860-
2861-
CCommandHandler *commandHandler = new CCommandHandler(this);
2848+
auto commandHandler = utility::make_unique<CCommandHandler>(this);
28622849

28632850
// Add command parsers
28642851
for (const auto &remoteCommandParserItem : gastRemoteCommandParserItems) {
@@ -2867,12 +2854,30 @@ bool CParameterMgr::handleRemoteProcessingInterface(string &strError)
28672854
remoteCommandParserItem._minArgumentCount, remoteCommandParserItem._pcHelp,
28682855
remoteCommandParserItem._pcDescription);
28692856
}
2870-
std::unique_ptr<IRemoteCommandHandler> remoteCommandHandler(commandHandler);
2857+
2858+
return commandHandler;
2859+
}
2860+
2861+
bool CParameterMgr::isRemoteInterfaceRequired()
2862+
{
2863+
// The remote interface should only be started if the client didn't
2864+
// explicitly forbid it and if the configuration file allows it.
2865+
return (not _bForceNoRemoteInterface) and getConstFrameworkConfiguration()->isTuningAllowed();
2866+
}
2867+
2868+
// Remote Processor Server connection handling
2869+
bool CParameterMgr::handleRemoteProcessingInterface(string &strError)
2870+
{
2871+
LOG_CONTEXT("Handling remote processing interface");
2872+
2873+
if (not isRemoteInterfaceRequired()) {
2874+
return true;
2875+
}
28712876

28722877
auto port = getConstFrameworkConfiguration()->getServerPort();
28732878

28742879
// The ownership of remoteComandHandler is given to Bg remote processor server.
2875-
_pRemoteProcessorServer = new BackgroundRemoteProcessorServer(port, remoteCommandHandler);
2880+
_pRemoteProcessorServer = new BackgroundRemoteProcessorServer(port, createCommandHandler());
28762881

28772882
if (_pRemoteProcessorServer == NULL) {
28782883
strError = "ParameterMgr: Unable to create Remote Processor Server";

parameter/ParameterMgr.h

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,6 @@ class CParameterMgr : private CElement
7979
EParameterConfigurationLibrary
8080
};
8181

82-
// Remote command parsers
83-
typedef TRemoteCommandHandlerTemplate<CParameterMgr> CCommandHandler;
84-
85-
typedef CCommandHandler::CommandStatus (CParameterMgr::*RemoteCommandParser)(
86-
const IRemoteCommand &remoteCommand, std::string &strResult);
87-
88-
// Parser descriptions
89-
struct SRemoteCommandParserItem
90-
{
91-
const char *_pcCommandName;
92-
CParameterMgr::RemoteCommandParser _pfnParser;
93-
size_t _minArgumentCount;
94-
const char *_pcHelp;
95-
const char *_pcDescription;
96-
};
9782
// Version
9883
static const uint32_t guiEditionMajor = 3;
9984
static const uint32_t guiEditionMinor = 1;
@@ -116,6 +101,15 @@ class CParameterMgr : private CElement
116101
*/
117102
bool load(std::string &strError);
118103

104+
// Remote command parsers
105+
using CommandHandler = std::unique_ptr<TRemoteCommandHandlerTemplate<CParameterMgr>>;
106+
107+
/** Create and return a command handler for this ParameterMgr instance
108+
*
109+
* @returns a Command Handler
110+
*/
111+
CommandHandler createCommandHandler();
112+
119113
// Selection Criteria
120114
CSelectionCriterionType *createSelectionCriterionType(bool bIsInclusive);
121115
CSelectionCriterion *createSelectionCriterion(
@@ -411,6 +405,24 @@ class CParameterMgr : private CElement
411405
// Version
412406
std::string getVersion() const;
413407

408+
// This using is here for internal reasons: CommandHandler is public and is
409+
// a unique_ptr but we want the type that's inside. And for legacy reason
410+
// because that's the original name before a rework; this directive avoids
411+
// renaming a lot of stuff.
412+
using CCommandHandler = CommandHandler::element_type;
413+
using RemoteCommandParser = CCommandHandler::CommandStatus (CParameterMgr::*)(
414+
const IRemoteCommand &remoteCommand, std::string &strResult);
415+
416+
// Parser descriptions
417+
struct SRemoteCommandParserItem
418+
{
419+
const char *_pcCommandName;
420+
CParameterMgr::RemoteCommandParser _pfnParser;
421+
size_t _minArgumentCount;
422+
const char *_pcHelp;
423+
const char *_pcDescription;
424+
};
425+
414426
////////////////:: Remote command parsers
415427
/// Version
416428
CCommandHandler::CommandStatus versionCommandProcess(const IRemoteCommand &remoteCommand,
@@ -819,6 +831,7 @@ class CParameterMgr : private CElement
819831
void feedElementLibraries();
820832

821833
// Remote Processor Server connection handling
834+
bool isRemoteInterfaceRequired();
822835
bool handleRemoteProcessingInterface(std::string &strError);
823836

824837
/** Log the result of a function

parameter/ParameterMgrFullConnector.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "ParameterMgr.h"
3232
#include "ParameterMgrLogger.h"
3333

34+
#include "CommandHandlerWrapper.h"
35+
3436
#include <list>
3537

3638
using std::string;
@@ -40,6 +42,11 @@ CParameterMgrFullConnector::CParameterMgrFullConnector(const string &strConfigur
4042
{
4143
}
4244

45+
CommandHandlerInterface *CParameterMgrFullConnector::createCommandHandler()
46+
{
47+
return new CommandHandlerWrapper(_pParameterMgr->createCommandHandler());
48+
}
49+
4350
void CParameterMgrFullConnector::setFailureOnMissingSubsystem(bool bFail)
4451
{
4552
std::string error;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2015, Intel Corporation
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
#pragma once
31+
32+
#include <string>
33+
#include <vector>
34+
35+
/** Class used to send commands to a parameter framework.
36+
* @see the help command for more information on which command can be sent.
37+
* @see ParameterMgrFullConnector::createCommandHandler to create an instance.
38+
*
39+
* This interface is primary designed to send commands without using a
40+
* tcp socket for test purposes.
41+
*
42+
* Note: the fact that this class must be deleted by the client is because the
43+
* PF interface is not c++11.
44+
* TODO: When the interface will transition to C++11, return directly the
45+
* CommandHandlerWrapper as this base class only use is to hide the
46+
* move semantic that is not supported in C++03.
47+
*/
48+
class CommandHandlerInterface
49+
{
50+
public:
51+
/** Send a command synchronously and receive it's result.
52+
*
53+
* @see CParameterMgr::gastRemoteCommandParserItems for the list of possible
54+
* command and their description.
55+
*
56+
* @param[in] command the command to execute.
57+
* @param[in] arguments the command arguments.
58+
* @param[out] the result of the command.
59+
*
60+
* return true in the command executed succesfuly,
61+
* false otherwise.
62+
*/
63+
virtual bool process(const std::string &command, const std::vector<std::string> &arguments,
64+
std::string &output) = 0;
65+
66+
virtual ~CommandHandlerInterface(){};
67+
};

0 commit comments

Comments
 (0)