• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ParameterHandle.h"
31 #include "ParameterAccessContext.h"
32 #include "BaseParameter.h"
33 #include "Subsystem.h"
34 #include <assert.h>
35 #include "ParameterMgr.h"
36 #include "AutoLock.h"
37 
38 using std::string;
39 
CParameterHandle(const CBaseParameter * pParameter,CParameterMgr * pParameterMgr)40 CParameterHandle::CParameterHandle(const CBaseParameter* pParameter, CParameterMgr* pParameterMgr)
41     : _pBaseParameter(pParameter), _pParameterMgr(pParameterMgr), _bBigEndianSubsystem(pParameter->getBelongingSubsystem()->isBigEndian())
42 {
43 }
44 
45 // Parameter features
isRogue() const46 bool CParameterHandle::isRogue() const
47 {
48     return _pBaseParameter->isRogue();
49 }
50 
isArray() const51 bool CParameterHandle::isArray() const
52 {
53     return !_pBaseParameter->isScalar();
54 }
55 
56 // Array Length
getArrayLength() const57 uint32_t CParameterHandle::getArrayLength() const
58 {
59     return _pBaseParameter->getArrayLength();
60 }
61 
62 // Parameter path
getPath() const63 string CParameterHandle::getPath() const
64 {
65     return _pBaseParameter->getPath();
66 }
67 
68 // Parameter kind
getKind() const69 string CParameterHandle::getKind() const
70 {
71     return _pBaseParameter->getKind();
72 }
73 
74 // Boolean access
setAsBoolean(bool bValue,string & strError)75 bool CParameterHandle::setAsBoolean(bool bValue, string& strError)
76 {
77     // Check operation validity
78     if (!checkAccessValidity(true, 0, strError)) {
79 
80         return false;
81     }
82     // Ensure we're safe against blackboard foreign access
83     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
84 
85     // When in tuning mode, silently skip the request
86     if (_pParameterMgr->tuningModeOn()) {
87 
88         return true;
89     }
90 
91     // Define access context
92     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
93 
94     return _pBaseParameter->accessAsBoolean(bValue, true, parameterAccessContext);
95 }
96 
getAsBoolean(bool & bValue,string & strError) const97 bool CParameterHandle::getAsBoolean(bool& bValue, string& strError) const
98 {
99     // Check operation validity
100     if (!checkAccessValidity(false, 0, strError)) {
101 
102         return false;
103     }
104     // Ensure we're safe against blackboard foreign access
105     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
106 
107     // Define access context
108     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
109 
110     return _pBaseParameter->accessAsBoolean(bValue, false, parameterAccessContext);
111 }
112 
setAsBooleanArray(const std::vector<bool> & abValues,string & strError)113 bool CParameterHandle::setAsBooleanArray(const std::vector<bool>& abValues, string& strError)
114 {
115     // Check operation validity
116     if (!checkAccessValidity(true, abValues.size(), strError)) {
117 
118         return false;
119     }
120     // Ensure we're safe against blackboard foreign access
121     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
122 
123     // When in tuning mode, silently skip the request
124     if (_pParameterMgr->tuningModeOn()) {
125 
126         return true;
127     }
128 
129     // Define access context
130     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
131 
132     // Copy values for type adaptation
133     std::vector<bool> abUserValues = abValues;
134 
135     return _pBaseParameter->accessAsBooleanArray(abUserValues, true, parameterAccessContext);
136 }
137 
getAsBooleanArray(std::vector<bool> & abValues,string & strError) const138 bool CParameterHandle::getAsBooleanArray(std::vector<bool>& abValues, string& strError) const
139 {
140     // Check operation validity
141     if (!checkAccessValidity(false, -1, strError)) {
142 
143         return false;
144     }
145     // Ensure we're safe against blackboard foreign access
146     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
147 
148     // Define access context
149     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
150 
151     return _pBaseParameter->accessAsBooleanArray(abValues, false, parameterAccessContext);
152 }
153 
154 // Integer Access
setAsInteger(uint32_t uiValue,string & strError)155 bool CParameterHandle::setAsInteger(uint32_t uiValue, string& strError)
156 {
157     // Check operation validity
158     if (!checkAccessValidity(true, 0, strError)) {
159 
160         return false;
161     }
162     // Ensure we're safe against blackboard foreign access
163     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
164 
165     // When in tuning mode, silently skip the request
166     if (_pParameterMgr->tuningModeOn()) {
167 
168         return true;
169     }
170 
171     // Define access context
172     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
173 
174     return _pBaseParameter->accessAsInteger(uiValue, true, parameterAccessContext);
175 }
176 
getAsInteger(uint32_t & uiValue,string & strError) const177 bool CParameterHandle::getAsInteger(uint32_t& uiValue, string& strError) const
178 {
179     // Check operation validity
180     if (!checkAccessValidity(false, 0, strError)) {
181 
182         return false;
183     }
184     // Ensure we're safe against blackboard foreign access
185     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
186 
187     // Define access context
188     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
189 
190     return _pBaseParameter->accessAsInteger(uiValue, false, parameterAccessContext);
191 }
192 
setAsIntegerArray(const std::vector<uint32_t> & auiValues,string & strError)193 bool CParameterHandle::setAsIntegerArray(const std::vector<uint32_t>& auiValues, string& strError)
194 {
195     // Check operation validity
196     if (!checkAccessValidity(true, auiValues.size(), strError)) {
197 
198         return false;
199     }
200     // Ensure we're safe against blackboard foreign access
201     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
202 
203     // When in tuning mode, silently skip the request
204     if (_pParameterMgr->tuningModeOn()) {
205 
206         return true;
207     }
208 
209     // Define access context
210     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
211 
212     // Copy values for type adaptation
213     std::vector<uint32_t> auiUserValues = auiValues;
214 
215     return _pBaseParameter->accessAsIntegerArray(auiUserValues, true, parameterAccessContext);
216 }
217 
getAsIntegerArray(std::vector<uint32_t> & auiValues,string & strError) const218 bool CParameterHandle::getAsIntegerArray(std::vector<uint32_t>& auiValues, string& strError) const
219 {
220     // Check operation validity
221     if (!checkAccessValidity(false, -1, strError)) {
222 
223         return false;
224     }
225     // Ensure we're safe against blackboard foreign access
226     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
227 
228     // Define access context
229     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
230 
231     return _pBaseParameter->accessAsIntegerArray(auiValues, false, parameterAccessContext);
232 }
233 
234 // Signed Integer Access
setAsSignedInteger(int32_t iValue,string & strError)235 bool CParameterHandle::setAsSignedInteger(int32_t iValue, string& strError)
236 {
237     // Check operation validity
238     if (!checkAccessValidity(true, 0, strError)) {
239 
240         return false;
241     }
242     // Ensure we're safe against blackboard foreign access
243     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
244 
245     // When in tuning mode, silently skip the request
246     if (_pParameterMgr->tuningModeOn()) {
247 
248         return true;
249     }
250 
251     // Define access context
252     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
253 
254     return _pBaseParameter->accessAsSignedInteger(iValue, true, parameterAccessContext);
255 }
256 
getAsSignedInteger(int32_t & iValue,string & strError) const257 bool CParameterHandle::getAsSignedInteger(int32_t& iValue, string& strError) const
258 {
259     // Check operation validity
260     if (!checkAccessValidity(false, 0, strError)) {
261 
262         return false;
263     }
264     // Ensure we're safe against blackboard foreign access
265     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
266 
267     // Define access context
268     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
269 
270     return _pBaseParameter->accessAsSignedInteger(iValue, false, parameterAccessContext);
271 }
272 
setAsSignedIntegerArray(const std::vector<int32_t> & aiValues,string & strError)273 bool CParameterHandle::setAsSignedIntegerArray(const std::vector<int32_t>& aiValues, string& strError)
274 {
275     // Check operation validity
276     if (!checkAccessValidity(true, aiValues.size(), strError)) {
277 
278         return false;
279     }
280     // Ensure we're safe against blackboard foreign access
281     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
282 
283     // When in tuning mode, silently skip the request
284     if (_pParameterMgr->tuningModeOn()) {
285 
286         return true;
287     }
288 
289     // Define access context
290     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
291 
292     // Copy values for type adaptation
293     std::vector<int32_t> aiUserValues = aiValues;
294 
295     return _pBaseParameter->accessAsSignedIntegerArray(aiUserValues, true, parameterAccessContext);
296 }
297 
getAsSignedIntegerArray(std::vector<int32_t> & aiValues,string & strError) const298 bool CParameterHandle::getAsSignedIntegerArray(std::vector<int32_t>& aiValues, string& strError) const
299 {
300     // Check operation validity
301     if (!checkAccessValidity(false, -1, strError)) {
302 
303         return false;
304     }
305     // Ensure we're safe against blackboard foreign access
306     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
307 
308     // Define access context
309     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
310 
311     return _pBaseParameter->accessAsSignedIntegerArray(aiValues, false, parameterAccessContext);
312 }
313 
314 // Double Access
setAsDouble(double dValue,string & strError)315 bool CParameterHandle::setAsDouble(double dValue, string& strError)
316 {
317     // Check operation validity
318     if (!checkAccessValidity(true, 0, strError)) {
319 
320         return false;
321     }
322     // Ensure we're safe against blackboard foreign access
323     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
324 
325     // When in tuning mode, silently skip the request
326     if (_pParameterMgr->tuningModeOn()) {
327 
328         return true;
329     }
330 
331     // Define access context
332     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
333 
334     return _pBaseParameter->accessAsDouble(dValue, true, parameterAccessContext);
335 }
336 
getAsDouble(double & dValue,string & strError) const337 bool CParameterHandle::getAsDouble(double& dValue, string& strError) const
338 {
339     // Check operation validity
340     if (!checkAccessValidity(false, 0, strError)) {
341 
342         return false;
343     }
344     // Ensure we're safe against blackboard foreign access
345     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
346 
347     // Define access context
348     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
349 
350     return _pBaseParameter->accessAsDouble(dValue, false, parameterAccessContext);
351 }
352 
setAsDoubleArray(const std::vector<double> & adValues,string & strError)353 bool CParameterHandle::setAsDoubleArray(const std::vector<double>& adValues, string& strError)
354 {
355     // Check operation validity
356     if (!checkAccessValidity(true, adValues.size(), strError)) {
357 
358         return false;
359     }
360     // Ensure we're safe against blackboard foreign access
361     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
362 
363     // When in tuning mode, silently skip the request
364     if (_pParameterMgr->tuningModeOn()) {
365 
366         return true;
367     }
368 
369     // Define access context
370     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
371 
372     // Copy values for type adaptation
373     std::vector<double> adUserValues = adValues;
374 
375     return _pBaseParameter->accessAsDoubleArray(adUserValues, true, parameterAccessContext);
376 }
377 
getAsDoubleArray(std::vector<double> & adValues,string & strError) const378 bool CParameterHandle::getAsDoubleArray(std::vector<double>& adValues, string& strError) const
379 {
380     // Check operation validity
381     if (!checkAccessValidity(false, -1, strError)) {
382 
383         return false;
384     }
385     // Ensure we're safe against blackboard foreign access
386     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
387 
388     // Define access context
389     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
390 
391     return _pBaseParameter->accessAsDoubleArray(adValues, false, parameterAccessContext);
392 }
393 
394 // String Access
setAsString(const string & strValue,string & strError)395 bool CParameterHandle::setAsString(const string& strValue, string& strError)
396 {
397     // Check operation validity
398     if (!checkAccessValidity(true, 0, strError)) {
399 
400         return false;
401     }
402     // Ensure we're safe against blackboard foreign access
403     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
404 
405     // When in tuning mode, silently skip the request
406     if (_pParameterMgr->tuningModeOn()) {
407 
408         return true;
409     }
410 
411     // Define access context
412     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
413 
414     // Copy value for type adaptation
415     string strUserValue = strValue;
416 
417     return _pBaseParameter->accessAsString(strUserValue, true, parameterAccessContext);
418 }
419 
getAsString(string & strValue,string & strError) const420 bool CParameterHandle::getAsString(string& strValue, string& strError) const
421 {
422     // Check operation validity
423     if (!checkAccessValidity(false, 0, strError)) {
424 
425         return false;
426     }
427     // Ensure we're safe against blackboard foreign access
428     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
429 
430     // Define access context
431     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
432 
433     return _pBaseParameter->accessAsString(strValue, false, parameterAccessContext);
434 }
435 
setAsStringArray(const std::vector<string> & astrValues,string & strError)436 bool CParameterHandle::setAsStringArray(const std::vector<string>& astrValues, string& strError)
437 {
438     // Check operation validity
439     if (!checkAccessValidity(true, (uint32_t)astrValues.size(), strError)) {
440 
441         return false;
442     }
443     // Ensure we're safe against blackboard foreign access
444     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
445 
446     // When in tuning mode, silently skip the request
447     if (_pParameterMgr->tuningModeOn()) {
448 
449         return true;
450     }
451 
452     // Define access context
453     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
454 
455     // Copy values for type adaptation
456     std::vector<string> astrUserValues = astrValues;
457 
458     return _pBaseParameter->accessAsStringArray(astrUserValues, true, parameterAccessContext);
459 }
460 
getAsStringArray(std::vector<string> & astrValues,string & strError) const461 bool CParameterHandle::getAsStringArray(std::vector<string>& astrValues, string& strError) const
462 {
463     // Check operation validity
464     if (!checkAccessValidity(false, -1, strError)) {
465 
466         return false;
467     }
468     // Ensure we're safe against blackboard foreign access
469     CAutoLock autoLock(_pParameterMgr->getBlackboardMutex());
470 
471     // Define access context
472     CParameterAccessContext parameterAccessContext(strError, _bBigEndianSubsystem, _pParameterMgr->getParameterBlackboard());
473 
474     return _pBaseParameter->accessAsStringArray(astrValues, false, parameterAccessContext);
475 }
476 
477 // Access validity
checkAccessValidity(bool bSet,size_t uiArrayLength,string & strError) const478 bool CParameterHandle::checkAccessValidity(bool bSet, size_t uiArrayLength, string& strError) const
479 {
480     if (bSet && !isRogue()) {
481 
482         strError = "Parameter is not rogue: ";
483 
484         strError += getPath();
485 
486         return false;
487     }
488 
489     if (uiArrayLength && !isArray()) {
490 
491         strError = "Parameter is scalar: ";
492 
493         strError += getPath();
494 
495         return false;
496     }
497 
498     if (!uiArrayLength && isArray()) {
499 
500         strError = "Parameter is an array: ";
501 
502         strError += getPath();
503 
504         return false;
505     }
506 
507     if (bSet && uiArrayLength && (uiArrayLength != getArrayLength())) {
508 
509         strError = "Array length mismatch: ";
510 
511         strError += getPath();
512 
513         return false;
514     }
515 
516     return true;
517 }
518