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