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 "ParameterType.h"
32 #include "MappingContext.h"
33 #include "TESTMappingKeys.h"
34 #include "InstanceConfigurableElement.h"
35 #include "TESTSubsystemObject.h"
36 #include <log/Context.h>
37 #include <sstream>
38 #include <vector>
39 
40 #define base CSubsystemObject
41 
CTESTSubsystemObject(const std::string &,CInstanceConfigurableElement * pInstanceConfigurableElement,const CMappingContext & context,core::log::Logger & logger)42 CTESTSubsystemObject::CTESTSubsystemObject(
43     const std::string & /*strMappingValue*/,
44     CInstanceConfigurableElement *pInstanceConfigurableElement, const CMappingContext &context,
45     core::log::Logger &logger)
46     : base(pInstanceConfigurableElement, logger)
47 {
48     // Get actual element type
49     const CParameterType *pParameterType =
50         static_cast<const CParameterType *>(pInstanceConfigurableElement->getTypeElement());
51 
52     _scalarSize = pParameterType->getSize();
53     _arraySize = pInstanceConfigurableElement->getFootPrint() / _scalarSize;
54     _bIsScalar = pParameterType->isScalar();
55 
56     _strFilePath = context.getItem(ETESTDirectory) + "/" + pInstanceConfigurableElement->getName();
57     _bLog = context.iSet(ETESTLog) && (context.getItem(ETESTLog) == "yes");
58 }
59 
sendToHW(std::string & strError)60 bool CTESTSubsystemObject::sendToHW(std::string &strError)
61 {
62     std::ofstream outputFile;
63 
64     outputFile.open(_strFilePath.c_str());
65 
66     if (!outputFile.is_open()) {
67 
68         strError = "Unable to open file: " + _strFilePath;
69 
70         return false;
71     }
72 
73     sendToFile(outputFile);
74 
75     outputFile.close();
76 
77     return true;
78 }
79 
receiveFromHW(std::string &)80 bool CTESTSubsystemObject::receiveFromHW(std::string & /*strError*/)
81 {
82     std::ifstream inputFile;
83 
84     inputFile.open(_strFilePath.c_str());
85 
86     if (!inputFile.is_open()) {
87 
88         return true;
89     }
90 
91     receiveFromFile(inputFile);
92 
93     inputFile.close();
94     return true;
95 }
96 
sendToFile(std::ofstream & outputFile)97 void CTESTSubsystemObject::sendToFile(std::ofstream &outputFile)
98 {
99     for (size_t index = 0; index < _arraySize; index++) {
100 
101         std::vector<uint8_t> aucValue(_scalarSize);
102 
103         void *pvValue = aucValue.data();
104 
105         // Read Value in BlackBoard
106         blackboardRead(pvValue, _scalarSize);
107 
108         std::string strValue = toString(pvValue, _scalarSize);
109 
110         outputFile << strValue << std::endl;
111 
112         if (_bLog) {
113 
114             if (_bIsScalar) {
115 
116                 info() << "TESTSUBSYSTEM: Writing '" << strValue << "' to file " << _strFilePath;
117             } else {
118 
119                 info() << "TESTSUBSYSTEM: Writing '" << strValue << "' to file " << _strFilePath
120                        << "[" << index << "]";
121             }
122         }
123     }
124 }
125 
receiveFromFile(std::ifstream & inputFile)126 void CTESTSubsystemObject::receiveFromFile(std::ifstream &inputFile)
127 {
128     for (size_t index = 0; index < _arraySize; index++) {
129 
130         std::vector<uint8_t> aucValue(_scalarSize);
131 
132         void *pvValue = aucValue.data();
133 
134         std::string strValue;
135 
136         inputFile >> strValue;
137 
138         if (_bLog) {
139 
140             if (_bIsScalar) {
141 
142                 info() << "TESTSUBSYSTEM: Reading '" << strValue << "' to file " << _strFilePath;
143             } else {
144 
145                 info() << "TESTSUBSYSTEM: Reading '" << strValue << "' to file " << _strFilePath
146                        << "[" << index << "]";
147             }
148         }
149 
150         fromString(strValue, pvValue, _scalarSize);
151 
152         // Write Value in Blackboard
153         blackboardWrite(pvValue, _scalarSize);
154     }
155 }
156