1/*
2 * Copyright (c) 2011-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#include <fstream>
31#include <string>
32#include <sstream>
33#include <cstdlib>
34#include <cassert>
35#include <iostream>
36#include "ParameterType.h"
37#include "MappingContext.h"
38#include "SkeletonMappingKeys.h"
39#include "InstanceConfigurableElement.h"
40#include "SkeletonSubsystemObject.h"
41
42#define STR_FORMAT_LENGTH 32
43
44#define base CFormattedSubsystemObject
45
46using std::string;
47
48CSkeletonSubsystemObject::CSkeletonSubsystemObject(
49    const string &strMappingValue, CInstanceConfigurableElement *pInstanceConfigurableElement,
50    const CMappingContext &context, core::log::Logger &logger)
51    : base(pInstanceConfigurableElement, logger, strMappingValue, EAmend1, EAmendEnd - EAmend1 + 1,
52           context),
53      _bWrongElementTypeError(false)
54{
55    // Get actual element type
56    const CParameterType *pParameterType =
57        static_cast<const CParameterType *>(pInstanceConfigurableElement->getTypeElement());
58
59    // Retrieve sizes
60    _scalarSize = pParameterType->getSize();
61    _arraySize = pInstanceConfigurableElement->getFootPrint() / _scalarSize;
62
63    // Construct message
64    _strMessage = context.getItem(ESkeletonOwner) + ":" + strMappingValue;
65
66    // Handle types
67    // Check we are able to handle elements (no exception support, defer the error)
68    switch (pInstanceConfigurableElement->getType()) {
69
70    case CInstanceConfigurableElement::EParameter:
71        break;
72    default:
73        _bWrongElementTypeError = true;
74        break;
75    }
76}
77// Sync to/from HW
78bool CSkeletonSubsystemObject::accessHW(bool bReceive, string &strError)
79{
80    // Check parameter type is ok (deferred error, no exceptions available :-()
81    if (_bWrongElementTypeError) {
82
83        strError = "Unsupported parameter type";
84
85        return false;
86    }
87
88    return base::accessHW(bReceive, strError);
89}
90
91bool CSkeletonSubsystemObject::sendToHW(string & /*strError*/)
92{
93    void *pvValue = alloca(_scalarSize);
94
95    for (size_t index = 0; index < _arraySize; index++) {
96
97        // Read Value in BlackBoard
98        blackboardRead(pvValue, _scalarSize);
99
100        // Send here the value
101        std::cout << "Sending to HW: " << _strMessage << std::endl;
102    }
103
104    return true;
105}
106
107bool CSkeletonSubsystemObject::receiveFromHW(string & /*strError*/)
108{
109    void *pvValue = alloca(_scalarSize);
110
111    for (size_t index = 0; index < _arraySize; index++) {
112
113        // Retreive here the value
114        std::cout << "Retreive from HW: " << _strMessage << std::endl;
115
116        // Write Value in Blackboard
117        blackboardWrite(pvValue, _scalarSize);
118    }
119
120    return true;
121}
122