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 "SelectionCriterionTypeInterface.h"
33 #include "SelectionCriterionInterface.h"
34 #include "ParameterHandle.h"
35 #include "ParameterMgrLoggerForward.h"
36 
37 #include <string>
38 #include <list>
39 #include <memory>
40 #include <stdint.h>
41 
42 class CParameterMgr;
43 
44 class CParameterMgrFullConnector
45 {
46     friend class CParameterMgrLogger<CParameterMgrFullConnector>;
47 
48 public:
49     CParameterMgrFullConnector(const std::string& strConfigurationFilePath);
50     ~CParameterMgrFullConnector();
51 
52     class ILogger
53     {
54     public:
55         virtual void log(bool bIsWarning, const std::string& strLog) = 0;
56     protected:
~ILogger()57         virtual ~ILogger() {}
58     };
59     // Logging
60     /** Should be called before start */
61     void setLogger(ILogger* pLogger);
62 
63 
64     bool start(std::string& strError);
65 
66     // Dynamic parameter handling
67     CParameterHandle* createParameterHandle(const std::string& strPath, std::string& strError);
68 
69     ISelectionCriterionTypeInterface* createSelectionCriterionType(bool bIsInclusive);
70     ISelectionCriterionInterface* createSelectionCriterion(const std::string& strName,
71             const ISelectionCriterionTypeInterface* pSelectionCriterionType);
72     ISelectionCriterionInterface* getSelectionCriterion(const std::string& strName);
73 
74     /** Is the remote interface forcefully disabled ?
75      */
76     bool getForceNoRemoteInterface() const;
77 
78     /**
79      * Forcefully disable the remote interface or cancel this policy.
80      *
81      * Has no effect if called after calling start().
82      *
83      * @param[in] bForceNoRemoteInterface disable the remote interface if true.
84      */
85     void setForceNoRemoteInterface(bool bForceNoRemoteInterface);
86 
87     void applyConfigurations();
88 
89     /** Should start fail in case of missing subsystems.
90       *
91       * @param[in] bFail: If set to true,  parameterMgr start will fail on missing subsystems.
92       *                   If set to false, missing subsystems will fallback on virtual subsystem.
93       */
94     void setFailureOnMissingSubsystem(bool bFail);
95 
96     /** Would start fail in case of missing subsystems.
97       *
98       * @return true if the subsystem will fail on missing subsystem, false otherwise.
99       */
100     bool getFailureOnMissingSubsystem() const;
101 
102     /** Should start fail in failed settings load.
103       *
104       * @param[in] bFail: If set to true, parameterMgr start will fail on failed settings load.
105       *                   If set to false, failed settings load will be ignored.
106       */
107     void setFailureOnFailedSettingsLoad(bool bFail);
108     /** Would start fail in case of failed settings load.
109       *
110       * @return failure on failed settings load policy state.
111       */
112     bool getFailureOnFailedSettingsLoad();
113 
114     /** Get the path to the directory containing the XML Schemas
115      *
116      * @returns the directory containing the XML Schemas
117      */
118     const std::string& getSchemaFolderLocation() const;
119 
120     /** Override the directory containing the XML Schemas
121      *
122      * @param[in] strSchemaFolderLocation directory containing the XML Schemas
123      */
124     void setSchemaFolderLocation(const std::string& strSchemaFolderLocation);
125 
126     /** Should .xml files be validated on start ?
127      *
128      * @param[in] bValidate:
129      *     If set to true, parameterMgr will report an error
130      *         when being unable to validate .xml files
131      *     If set to false, no .xml/xsd validation will happen
132      *     (default behaviour)
133      *
134      * @return false if unable to set, true otherwise.
135      */
136     void setValidateSchemasOnStart(bool bValidate);
137 
138     /** Would .xml files be validated on start?
139      *
140      * @return areSchemasValidated
141      */
142     bool getValidateSchemasOnStart() const;
143     //////////// Tuning /////////////
144     // Tuning mode
145     bool setTuningMode(bool bOn, std::string& strError);
146     bool isTuningModeOn() const;
147 
148     // Current value space for user set/get value interpretation
149     void setValueSpace(bool bIsRaw);
150     bool isValueSpaceRaw() const;
151 
152     // Current Output Raw Format for user get value interpretation
153     void setOutputRawFormat(bool bIsHex);
154     bool isOutputRawFormatHex() const;
155     // Automatic hardware synchronization control (during tuning session)
156     bool setAutoSync(bool bAutoSyncOn, std::string& strError);
157     bool isAutoSyncOn() const;
158     bool sync(std::string& strError);
159 
160     // User set/get parameters
161     bool accessParameterValue(const std::string& strPath, std::string& strValue, bool bSet, std::string& strError);
162     bool accessConfigurationValue(const std::string &strDomain, const std::string &strConfiguration, const std::string& strPath, std::string& strValue, bool bSet, std::string& strError);
163 
164     /**
165      * Returns the element mapping corresponding to the path given in parameter.
166      *
167      * @param[in] strPath Path of an element
168      * @param[out] strValue A sting containing the mapping
169      *
170      * @return true if a mapping was found for this element
171      */
172     bool getParameterMapping(const std::string& strPath, std::string& strValue) const;
173     ////////// Configuration/Domains handling //////////////
174     // Creation/Deletion
175     bool createDomain(const std::string& strName, std::string& strError);
176     bool deleteDomain(const std::string& strName, std::string& strError);
177     bool renameDomain(const std::string& strName, const std::string& strNewName, std::string& strError);
178     bool deleteAllDomains(std::string& strError);
179     bool setSequenceAwareness(const std::string& strName, bool bSequenceAware, std::string& strResult);
180     bool getSequenceAwareness(const std::string& strName, bool& bSequenceAware, std::string& strResult);
181     bool createConfiguration(const std::string& strDomain, const std::string& strConfiguration, std::string& strError);
182     bool deleteConfiguration(const std::string& strDomain, const std::string& strConfiguration, std::string& strError);
183     bool renameConfiguration(const std::string& strDomain, const std::string& strConfiguration, const std::string& strNewConfiguration, std::string& strError);
184 
185     // Save/Restore
186     bool restoreConfiguration(const std::string& strDomain, const std::string& strConfiguration, std::list<std::string>& strError);
187     bool saveConfiguration(const std::string& strDomain, const std::string& strConfiguration, std::string& strError);
188 
189     // Configurable element - domain association
190     bool addConfigurableElementToDomain(const std::string& strDomain, const std::string& strConfigurableElementPath, std::string& strError);
191     bool removeConfigurableElementFromDomain(const std::string& strDomain, const std::string& strConfigurableElementPath, std::string& strError);
192     bool split(const std::string& strDomain, const std::string& strConfigurableElementPath, std::string& strError);
193     bool setElementSequence(const std::string& strDomain, const std::string& strConfiguration, const std::vector<std::string>& astrNewElementSequence, std::string& strError);
194 
195     bool setApplicationRule(const std::string& strDomain, const std::string& strConfiguration,
196                             const std::string& strApplicationRule, std::string& strError);
197     bool getApplicationRule(const std::string& strDomain, const std::string& strConfiguration,
198                             std::string& strResult);
199     bool clearApplicationRule(const std::string& strDomain, const std::string& strConfiguration, std::string& strError);
200 
201     /**
202       * Method that imports Configurable Domains from an Xml source.
203       *
204       * @param[in] strXmlSource a string containing an xml description or a path to an xml file
205       * @param[in] bWithSettings a boolean that determines if the settings should be used in the
206       * xml description
207       * @param[in] bFromFile a boolean that determines if the source is an xml description in
208       * strXmlSource or contained in a file. In that case strXmlSource is just the file path.
209       * @param[out] strError is used as the error output
210       *
211       * @return false if any error occures
212       */
213     bool importDomainsXml(const std::string& strXmlSource, bool bWithSettings, bool bFromFile,
214                           std::string& strError);
215     /**
216       * Method that imports a single Configurable Domain from an Xml source.
217       *
218       * @param[in] xmlSource a string containing an xml description or a path to an xml file
219       * @param[in] overwrite when importing an existing domain, allow overwriting or return an
220       * error
221       * @param[in] withSettings a boolean that determines if the settings should be used in the
222       * xml description
223       * @param[in] fromFile a boolean that determines if the source is an xml description in
224       * strXmlSource or contained in a file. In that case strXmlSource is just the file path.
225       * @param[out] errorMsg is used as the error output
226       *
227       * @return false if any error occurs
228       */
229     bool importSingleDomainXml(const std::string& xmlSource, bool overwrite, bool withSettings,
230                                bool toFile, std::string& errorMsg);
231     /**
232       * Method that imports a single Configurable Domain from an string
233       * describing an Xml source.
234       *
235       * @deprecated use the other versions of importSingleDomainXml instead
236       *
237       * @param[in] strXmlSource a string containing an xml description
238       * @param[in] bOverwrite when importing an existing domain, allow overwriting or return an
239       * error
240       * @param[out] strError is used as the error output
241       *
242       * @return false if any error occurs
243       */
244     bool importSingleDomainXml(const std::string& strXmlSource, bool bOverwrite,
245                                std::string& strError);
246 
247 
248     /**
249       * Method that exports Configurable Domains to an Xml destination.
250       *
251       * @param[in,out] strXmlDest a string containing an xml description or a path to an xml file
252       * @param[in] bWithSettings a boolean that determines if the settings should be used in the
253       * xml description
254       * @param[in] bToFile a boolean that determines if the destination is an xml description in
255       * strXmlDest or contained in a file. In that case strXmlDest is just the file path.
256       * @param[out] strError is used as the error output
257       *
258       * @return false if any error occures, true otherwise.
259       */
260     bool exportDomainsXml(std::string& strXmlDest, bool bWithSettings, bool bToFile,
261                           std::string& strError) const;
262 
263     /**
264       * Method that exports a given Configurable Domain to an Xml destination.
265       *
266       * @param[in,out] strXmlDest a string containing an xml description or a path to an xml file
267       * @param[in] strDomainName the name of the domain to be exported
268       * @param[in] bWithSettings a boolean that determines if the settings should be used in the
269       * xml description
270       * @param[in] bToFile a boolean that determines if the destination is an xml description in
271       * strXmlDest or contained in a file. In that case strXmlDest is just the file path.
272       * @param[out] strError is used as the error output
273       *
274       * @return false if any error occurs, true otherwise.
275       */
276     bool exportSingleDomainXml(std::string& strXmlDest, const std::string& strDomainName, bool bWithSettings,
277                                bool bToFile, std::string& strError) const;
278 
279 private:
280     // disallow copying because this class manages raw pointers' lifecycle
281     CParameterMgrFullConnector(const CParameterMgrFullConnector&);
282     CParameterMgrFullConnector& operator=(const CParameterMgrFullConnector&);
283 
284     void doLog(bool bIsWarning, const std::string& strLog);
285 
286     CParameterMgr* _pParameterMgr;
287 
288     ILogger* _pLogger;
289     // Log wrapper
290     CParameterMgrLogger<CParameterMgrFullConnector>* _pParameterMgrLogger;
291 };
292