2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef _WRTPLUGINS_COMMONS_SRC_COMMONS_STATICDECLARATIOS_H_
18 #define _WRTPLUGINS_COMMONS_SRC_COMMONS_STATICDECLARATIOS_H_
24 #include <dpl/noncopyable.h>
25 #include <dpl/assert.h>
26 #include <dpl/foreach.h>
27 #include <Commons/TypesDeclaration.h>
28 #include <wrt-commons/wrt_plugin_export.h>
30 namespace WrtDeviceApis {
31 namespace CommonsJavaScript {
33 template<typename ParamType,
34 typename DeviceCapType,
35 typename FunctionType>
36 class StaticDeclarations : public DPL::Noncopyable
38 struct FunctionTriplet {
40 std::vector<DeviceCapType> devCaps;
41 std::vector<const char*> features;
46 typedef typename std::map<ParamType, const char*> ParamsMap;
48 typedef std::map<DeviceCapType,
49 std::pair<const char*,
50 std::vector<ParamType> > > DeviceCapsMaps;
52 typedef std::map<FunctionType, FunctionTriplet > FunctionsMap;
54 typedef std::map<FunctionType, WrtDeviceApis::Commons::AceFunction>
57 typedef std::map<std::string, std::vector<DeviceCapType> > FeaturesMap;
59 static const std::string getParamName(const ParamType& paramId)
61 auto it = m_params.find(paramId);
63 Assert(it != m_params.end() && "No such paramId");
69 * Returns set of device capabilities WITHOUT params
70 * for given device capability id
72 static WrtDeviceApis::Commons::AceDeviceCapability
73 getDeviceCapabilityWithoutParams(const DeviceCapType& devCapsId)
75 WrtDeviceApis::Commons::AceDeviceCapability deviceCap;
76 auto it = m_deviceCaps.find(devCapsId);
78 Assert(it != m_deviceCaps.end() && "No such device cap");
80 deviceCap.devCapName = it->second.first;
86 * Returns set of device capabilities with set params
87 * for given device capability id
89 static WrtDeviceApis::Commons::AceDeviceCapability
90 getDeviceCapability(const DeviceCapType& devCapsId)
92 auto it = m_deviceCaps.find(devCapsId);
94 Assert(it != m_deviceCaps.end() && "No such dev-cap found");
96 WrtDeviceApis::Commons::AceDeviceCapability deviceCap;
97 deviceCap.devCapName = it->second.first;
99 FOREACH(paramIt, it->second.second)
101 WrtDeviceApis::Commons::AceDeviceCapParam param(
102 getParamName(*paramIt),
105 deviceCap.devCapParams.push_back(param);
111 static void addDeviceCapabilty(
112 const DeviceCapType& devCapsId,
113 WrtDeviceApis::Commons::AceFunction& aceFunction)
115 aceFunction.deviceCapabilities.push_back(
116 getDeviceCapability(devCapsId));
120 * Returns names of device-capabilities base on capability id
122 static std::string getDevCapNameById(DeviceCapType devCapId)
124 auto it = m_deviceCaps.find(devCapId);
125 Assert(it != m_deviceCaps.end() && "No such devcapid found!");
126 return it->second.first;
130 * Sets parameter value for given paramId
132 static bool setParamValue(WrtDeviceApis::Commons::AceFunction& function,
134 DeviceCapType devCapId,
135 const std::string& value)
137 //get name of the deviceCaps
138 std::string devCapName = getDevCapNameById(devCapId) ;
139 std::string paramName = getParamName(paramId);
141 //search throw all the device capabilities
142 FOREACH(devCapIt, function.deviceCapabilities) {
143 if(devCapIt->devCapName == devCapName) {
144 //device capability has been found
146 FOREACH(devParamIt, devCapIt->devCapParams) {
147 if(devParamIt->name == paramName) {
148 devParamIt->value = value;
157 * Return struct Commons::AceFunction with set function name
159 * To set device capabilities you may use setDeviceCap function
160 * To set param value function you may use setParamValue Function
162 static WrtDeviceApis::Commons::AceFunction getEmptyFunction(
163 const FunctionType& functionId)
165 WrtDeviceApis::Commons::AceFunction function;
166 auto it = m_functions.find(functionId);
167 Assert(it != m_functions.end() && "No such a function");
168 function.name = it->second.first;
174 * The most useful Function
175 * Return Commons::AceFunction with filled all required fields:
176 * name, device caps and proper param namespace
178 * To set param value function you may use setParamValue function
180 static WrtDeviceApis::Commons::AceFunction getSecurityFunction(
181 const FunctionType& functionId)
183 WrtDeviceApis::Commons::AceFunction function;
184 auto it = m_functions.find(functionId);
185 Assert(it != m_functions.end() && "No such function found!");
187 function.name = it->second.name;
189 FOREACH (featIt, it->second.features)
190 function.features.push_back(std::string(*featIt));
192 FOREACH(devCapIt, it->second.devCaps) {
193 function.deviceCapabilities.push_back(
194 getDeviceCapability(*devCapIt));
200 * To create static map
202 static void createStaticAceFunctions()
204 FOREACH(functionIt, m_functions)
206 m_aceFunctions[functionIt->first] =
207 getSecurityFunction(functionIt->first);
211 static feature_mapping_t* getFeaturesToDevCapMapping()
213 feature_mapping_t* mapping = new feature_mapping_t;
215 mapping->featuresCount = m_features.size();
216 mapping->features = new feature_devcaps_t[mapping->featuresCount];
220 FOREACH(featureIt, m_features)
222 mapping->features[i].feature_name =
223 strndup(featureIt->first.c_str(), featureIt->first.size() + 1);
225 mapping->features[i].devCaps.devCapsCount =
226 featureIt->second.size();
228 mapping->features[i].devCaps.deviceCaps =
229 new char*[mapping->features[i].devCaps.devCapsCount];
232 j < mapping->features[i].devCaps.devCapsCount;
235 std::string dc = getDevCapNameById(featureIt->second[j]);
237 mapping->features[i].devCaps.deviceCaps[j] =
238 strndup(dc.c_str(), dc.size() + 1);
247 static const devcaps_t* devcapsGetter(pfeature_mapping_t feats,
248 const char* featureName)
250 Assert(featureName && "Trying to extract info about NULL api feature");
252 std::string feature(featureName);
254 devcaps_t* ret = NULL;
256 for (size_t i = 0; i < feats->featuresCount ; ++i)
258 Assert(feats->features &&
259 feats->features[i].feature_name &&
260 "NULL api feature in feature mapping");
262 std::string feat(feats->features[i].feature_name);
266 ret = &(feats->features[i].devCaps);
274 static void featuresDeinitializer(feature_mapping_t* mapping)
278 if (mapping->features)
280 for (size_t i = 0; i < mapping->featuresCount; ++i)
282 free(mapping->features[i].feature_name);
284 devcaps_t& dc = mapping->features[i].devCaps;
288 for (size_t j = 0; j < dc.devCapsCount; ++j)
290 free(dc.deviceCaps[j]);
293 delete []dc.deviceCaps;
296 delete []mapping->features;
302 static void getMappingInterface(feature_mapping_interface_t *mapping)
306 mapping->featGetter =
307 StaticDeclarations::getFeaturesToDevCapMapping;
308 mapping->dcGetter = StaticDeclarations::devcapsGetter;
309 mapping->release = StaticDeclarations::featuresDeinitializer;
314 static ParamsMap m_params;
315 static DeviceCapsMaps m_deviceCaps;
316 static FunctionsMap m_functions;
317 static AceFunctionsMap m_aceFunctions;
319 static FeaturesMap m_features;