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 "Config.hpp"
33 #include "ConfigFiles.hpp"
34 #include "FailureWrapper.hpp"
35 
36 #include <ParameterMgrFullConnector.h>
37 
38 namespace parameterFramework
39 {
40 
41 /** This forward declaration is an implementation detail, client should expect its presence.
42  * @note This forward definition should not be needed as the `friend class ElementHandle`
43  *       declaration in ParameterFramework is itself a forward declaration.
44  *       Unfortunately there seem to be a bug in visual studio 2015, it is required.
45  */
46 class ElementHandle;
47 
48 /** Wrapper around the Parameter Framework to throw exceptions on errors and
49  *  have more user friendly methods.
50  *  @see parameterFramework::ElementHandle to access elements of the parameter tree.
51  */
52 class ParameterFramework : private parameterFramework::ConfigFiles,
53                            private FailureWrapper<CParameterMgrFullConnector>
54 {
55 protected:
56     /** Alias to the Platform Connector PF.
57      * It should not be usefull as PF is a super set but is useful
58      * to disambiguate overloaded method for MS visual compiler.
59      */
60     using PPF = CParameterMgrPlatformConnector;
61     using PF = CParameterMgrFullConnector;
62     using EH = ::ElementHandle;
63 
64 public:
ParameterFramework(const Config & config=Config ())65     ParameterFramework(const Config &config = Config())
66         : ConfigFiles(config), FailureWrapper(getPath())
67     {
68         setForceNoRemoteInterface(true);
69     }
70 
start()71     void start() { mayFailCall(&PF::start); }
72 
73     /** @name Forwarded methods
74      * Forward those methods without modification as there are ergonomic and
75      * can not fail (no failure to throw).
76      * @{ */
77     using PF::applyConfigurations;
78     using PF::getFailureOnMissingSubsystem;
79     using PF::getFailureOnFailedSettingsLoad;
80     using PF::getForceNoRemoteInterface;
81     using PF::setForceNoRemoteInterface;
82     using PF::getSchemaUri;
83     using PF::setSchemaUri;
84     using PF::getValidateSchemasOnStart;
85     using PF::isValueSpaceRaw;
86     using PF::isOutputRawFormatHex;
87     using PF::isTuningModeOn;
88     using PF::isAutoSyncOn;
89     using PF::setLogger;
90     using PF::createCommandHandler;
91     /** @} */
92 
93     /** Wrap PF::setValidateSchemasOnStart to throw an exception on failure. */
setValidateSchemasOnStart(bool validate)94     void setValidateSchemasOnStart(bool validate)
95     {
96         mayFailCall(&PPF::setValidateSchemasOnStart, validate);
97     }
98 
99     /** Wrap PF::setFailureOnFailedSettingsLoad to throw an exception on failure. */
setFailureOnFailedSettingsLoad(bool fail)100     void setFailureOnFailedSettingsLoad(bool fail)
101     {
102         mayFailCall(&PPF::setFailureOnFailedSettingsLoad, fail);
103     }
104 
105     /** Wrap PF::setFailureOnMissingSubsystem to throw an exception on failure. */
setFailureOnMissingSubsystem(bool fail)106     void setFailureOnMissingSubsystem(bool fail)
107     {
108         mayFailCall(&PPF::setFailureOnMissingSubsystem, fail);
109     }
110 
111     /** Renaming for better readability (and coherency with PF::isValueSpaceRaw)
112      *  of PF::setValueSpace. */
setRawValueSpace(bool enable)113     void setRawValueSpace(bool enable) { setValueSpace(enable); }
114 
115     /** Renaming for better readability (and coherency with PF::isValueSpaceRaw)
116      *  of PF::setValueSpace. */
setHexOutputFormat(bool enable)117     void setHexOutputFormat(bool enable) { setOutputRawFormat(enable); }
118 
119     /** Wrap PF::setTuningMode to throw an exception on failure. */
setTuningMode(bool enable)120     void setTuningMode(bool enable) { mayFailCall(&PF::setTuningMode, enable); }
121 
122     /** Wrap PF::setAutoSync to throw an exception on failure. */
setAutoSync(bool enable)123     void setAutoSync(bool enable) { mayFailCall(&PF::setAutoSync, enable); }
124 
125     /** Wrap PF::accessParameterValue in "set" mode (and rename it) to throw an
126      * exception on failure
127      */
setParameter(const std::string & path,std::string & value)128     void setParameter(const std::string &path, std::string &value)
129     {
130         mayFailCall(&PF::accessParameterValue, path, value, true);
131     }
132     /** Wrap PF::accessParameterValue in "get" mode (and rename it) to throw an
133      * exception on failure
134      */
getParameter(const std::string & path,std::string & value)135     void getParameter(const std::string &path, std::string &value)
136     {
137         mayFailCall(&PF::accessParameterValue, path, value, false);
138     }
139 
140     /** Wrap PF::accessConfigurationValue in "set" mode (and rename it) to throw an
141      * exception on failure
142      */
setConfigurationParameter(const std::string domain,const std::string & configuration,const std::string & path,std::string & value)143     void setConfigurationParameter(const std::string domain, const std::string &configuration,
144                                    const std::string &path, std::string &value)
145     {
146         mayFailCall(&PF::accessConfigurationValue, domain, configuration, path, value, true);
147     }
148 
149     /** Wrap PF::accessConfigurationValue in "get" mode (and rename it) to throw an
150      * exception on failure
151      */
getConfigurationParameter(const std::string & domain,const std::string & configuration,const std::string & path,std::string & value)152     void getConfigurationParameter(const std::string &domain, const std::string &configuration,
153                                    const std::string &path, std::string &value)
154     {
155         mayFailCall(&PF::accessConfigurationValue, domain, configuration, path, value, false);
156     }
157 
158 private:
159     /** Create an unwrapped element handle.
160      *
161      * Is not public as this method is intended to be used by ElementHandle facade.
162      */
createElementHandle(const std::string & path)163     EH createElementHandle(const std::string &path)
164     {
165         // PF::createElementHandle takes it's handler in the free store
166         std::unique_ptr<EH> newedHandle{mayFailCall(&PF::createElementHandle, path)};
167         EH handle{*newedHandle};
168         return handle;
169     }
170     friend class ElementHandle;
171 };
172 
173 } // namespace parameterFramework
174