1 /*
2  * Copyright (c) 2011-2014, 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 "ParameterMgrPlatformConnector.h"
33 #include "RemoteCommandHandlerTemplate.h"
34 #include <string>
35 #include <list>
36 #include <semaphore.h>
37 
38 class CParameterMgrPlatformConnectorLogger;
39 class CRemoteProcessorServer;
40 class ISelectionCriterionInterface;
41 
42 class CTestPlatform
43 {
44     typedef TRemoteCommandHandlerTemplate<CTestPlatform> CCommandHandler;
45     typedef CCommandHandler::CommandStatus CommandReturn;
46 public:
47     CTestPlatform(const std::string &strclass, int iPortNumber, sem_t& exitSemaphore);
48     virtual ~CTestPlatform();
49 
50     // Init
51     bool load(std::string& strError);
52 
53 private:
54     //////////////// Remote command parsers
55     /// Selection Criterion
56     CommandReturn createExclusiveSelectionCriterionFromStateList(
57             const IRemoteCommand& remoteCommand, std::string& strResult);
58     CommandReturn createInclusiveSelectionCriterionFromStateList(
59             const IRemoteCommand& remoteCommand, std::string& strResult);
60 
61     CommandReturn createExclusiveSelectionCriterion(
62             const IRemoteCommand& remoteCommand, std::string& strResult);
63     CommandReturn createInclusiveSelectionCriterion(
64             const IRemoteCommand& remoteCommand, std::string& strResult);
65 
66     /** Callback to set a criterion's value, see ISelectionCriterionInterface::setCriterionState.
67      * @see CCommandHandler::RemoteCommandParser for detail on each arguments and return
68      *
69      * @param[in] remoteCommand the first argument should be the name of the criterion to set.
70      *                          if the criterion is provided in lexical space,
71      *                              the following arguments should be criterion new values
72      *                          if the criterion is provided in numerical space,
73      *                              the second argument should be the criterion new value
74      */
75     CommandReturn setCriterionState(
76             const IRemoteCommand& remoteCommand, std::string& strResult);
77 
78     /** Callback to start the PFW, see CParameterMgrPlatformConnector::start.
79      * @see CCommandHandler::RemoteCommandParser for detail on each arguments and return
80      *
81      * @param[in] remoteCommand is ignored
82      */
83     CommandReturn startParameterMgr(
84             const IRemoteCommand& remoteCommand, std::string& strResult);
85 
86     /** Callback to apply PFW configuration, see CParameterMgrPlatformConnector::applyConfiguration.
87      * @see CCommandHandler::RemoteCommandParser for detail on each arguments and return
88      *
89      * @param[in] remoteCommand is ignored
90      *
91      * @return EDone (never fails)
92      */
93     CommandReturn applyConfigurations(
94             const IRemoteCommand& remoteCommand, std::string& strResult);
95 
96     /** Callback to exit the test-platform.
97      *
98      * @param[in] remoteCommand is ignored
99      *
100      * @return EDone (never fails)
101      */
102     CommandReturn exit(const IRemoteCommand& remoteCommand, std::string& strResult);
103 
104     /** The type of a CParameterMgrPlatformConnector boolean setter. */
105     typedef bool (CParameterMgrPlatformConnector::*setter_t)(bool, std::string&);
106     /** Template callback to create a _pParameterMgrPlatformConnector boolean setter callback.
107      * @see CCommandHandler::RemoteCommandParser for detail on each arguments and return
108      *
109      * Convert the remoteCommand first argument to a boolean and call the
110      * template parameter function with this value.
111      *
112      * @tparam the boolean setter method.
113      * @param[in] remoteCommand the first argument should be ether "on" or "off".
114      */
115     template<setter_t setFunction>
116     CommandReturn setter(
117             const IRemoteCommand& remoteCommand, std::string& strResult);
118 
119     /** The type of a CParameterMgrPlatformConnector boolean getter. */
120     typedef bool (CParameterMgrPlatformConnector::*getter_t)();
121     /** Template callback to create a ParameterMgrPlatformConnector boolean getter callback.
122      * @see CCommandHandler::RemoteCommandParser for detail on each arguments and return
123      *
124      * Convert to boolean returned by the template parameter function converted to a
125      * std::string ("True", "False") and return it.
126      *
127      * @param the boolean getter method.
128      * @param[in] remoteCommand is ignored
129      *
130      * @return EDone (never fails)
131      */
132     template<getter_t getFunction>
133     CommandReturn getter(const IRemoteCommand& remoteCommand, std::string& strResult);
134 
135     // Commands
136     bool createExclusiveSelectionCriterionFromStateList(const std::string& strName, const IRemoteCommand& remoteCommand, std::string& strResult);
137     bool createInclusiveSelectionCriterionFromStateList(const std::string& strName, const IRemoteCommand& remoteCommand, std::string& strResult);
138 
139     bool createExclusiveSelectionCriterion(const std::string& strName, uint32_t uiNbValues, std::string& strResult);
140     bool createInclusiveSelectionCriterion(const std::string& strName, uint32_t uiNbValues, std::string& strResult);
141     bool setCriterionState(const std::string& strName, uint32_t uiState, std::string& strResult);
142     bool setCriterionStateByLexicalSpace(const IRemoteCommand& remoteCommand, std::string& strResult);
143 
144     // Connector
145     CParameterMgrPlatformConnector* _pParameterMgrPlatformConnector;
146 
147     // Logger
148     CParameterMgrPlatformConnectorLogger* _pParameterMgrPlatformConnectorLogger;
149 
150     // Command Handler
151     CCommandHandler* _pCommandHandler;
152 
153     // Remote Processor Server
154     CRemoteProcessorServer* _pRemoteProcessorServer;
155 
156     // Semaphore used by calling thread to avoid exiting
157     sem_t& _exitSemaphore;
158 };
159 
160