blob: 5d92127c74b82943a40229a92a0d64d41e947d3b [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31#include "SubsystemObject.h"
32#include "InstanceConfigurableElement.h"
33#include "ParameterBlackboard.h"
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +010034#include "MappingContext.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020035#include <assert.h>
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020036#include <stdlib.h>
37#include <string.h>
38#include <sstream>
Patrick Benavoli63499d42011-10-24 18:50:03 +020039#include <stdarg.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020040
41CSubsystemObject::CSubsystemObject(CInstanceConfigurableElement* pInstanceConfigurableElement)
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020042 : _pInstanceConfigurableElement(pInstanceConfigurableElement),
43 _uiDataSize(pInstanceConfigurableElement->getFootPrint()),
44 _pucBlackboardLocation(NULL),
45 _uiAccessedIndex(0)
Patrick Benavoli68a91282011-08-31 11:23:23 +020046{
47 // Syncer
48 _pInstanceConfigurableElement->setSyncer(this);
49}
50
51CSubsystemObject::~CSubsystemObject()
52{
53 _pInstanceConfigurableElement->unsetSyncer();
54}
55
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020056// Blackboard data location
57uint8_t* CSubsystemObject::getBlackboardLocation() const
Patrick Benavoli68a91282011-08-31 11:23:23 +020058{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020059 return _pucBlackboardLocation;
Patrick Benavoli68a91282011-08-31 11:23:23 +020060}
61
62// Size
63uint32_t CSubsystemObject::getSize() const
64{
65 return _uiDataSize;
66}
67
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020068// Conversion utility
69uint32_t CSubsystemObject::asInteger(const string& strValue)
70{
71 return strtoul(strValue.c_str(), NULL, 0);
72}
73
74string CSubsystemObject::asString(uint32_t uiValue)
75{
76 ostringstream ostr;
77
78 ostr << uiValue;
79
80 return ostr.str();
81}
82
Patrick Benavoli68a91282011-08-31 11:23:23 +020083// Synchronization
84bool CSubsystemObject::sync(CParameterBlackboard& parameterBlackboard, bool bBack, string& strError)
85{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020086 // Get blackboard location
87 _pucBlackboardLocation = parameterBlackboard.getLocation(_pInstanceConfigurableElement->getOffset());
88 // Access index init
89 _uiAccessedIndex = 0;
Patrick Benavoli68a91282011-08-31 11:23:23 +020090
91#ifdef SIMULATION
92 return true;
93#endif
94
95 // Synchronize to/from HW
96 if (bBack) {
97
98 // Read from HW
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020099 if (!accessHW(true, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200100
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200101 strError = "Unable to back synchronize configurable element " + _pInstanceConfigurableElement->getPath() + ": " + strError;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200102
103 return false;
104 }
105
Patrick Benavoli68a91282011-08-31 11:23:23 +0200106 } else {
107
Patrick Benavoli68a91282011-08-31 11:23:23 +0200108 // Send to HW
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200109 if (!accessHW(false, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200110
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200111 strError = "Unable to synchronize configurable element " + _pInstanceConfigurableElement->getPath() + ": " + strError;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200112
113 return false;
114 }
115 }
116 return true;
117}
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200118
119// Sync to/from HW
120bool CSubsystemObject::sendToHW(string& strError)
121{
122 strError = "Send to HW interface not implemented at subsystsem level!";
123
124 return false;
125}
126
127bool CSubsystemObject::receiveFromHW(string& strError)
128{
129 strError = "Receive from HW interface not implemented at subsystsem level!";
130
131 return false;
132}
133
134// Fall back HW access
135bool CSubsystemObject::accessHW(bool bReceive, string& strError)
136{
Patrick Benavoli63499d42011-10-24 18:50:03 +0200137 // Default access fall back
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200138 if (bReceive) {
139
140 return receiveFromHW(strError);
141 } else {
142
143 return sendToHW(strError);
144 }
145}
146
147// Blackboard access from subsystems
148void CSubsystemObject::blackboardRead(void* pvData, uint32_t uiSize)
149{
150 assert(_uiAccessedIndex + uiSize <= _uiDataSize);
151
152 memcpy(pvData, _pucBlackboardLocation + _uiAccessedIndex, uiSize);
153
154 _uiAccessedIndex += uiSize;
155}
156
157void CSubsystemObject::blackboardWrite(const void* pvData, uint32_t uiSize)
158{
159 assert(_uiAccessedIndex + uiSize <= _uiDataSize);
160
161 memcpy(_pucBlackboardLocation + _uiAccessedIndex, pvData, uiSize);
162
163 _uiAccessedIndex += uiSize;
164}
Patrick Benavoli63499d42011-10-24 18:50:03 +0200165
166// Logging
167void CSubsystemObject::log(const string& strMessage, ...) const
168{
169 char acBuffer[512];
170 va_list listPointer;
171
172 va_start(listPointer, strMessage);
173
174 vsnprintf(acBuffer, sizeof(acBuffer), strMessage.c_str(), listPointer);
175
176 va_end(listPointer);
177
178 _pInstanceConfigurableElement->log(acBuffer);
179}
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100180
181// Amendment
182string CSubsystemObject::formatMappingValue(const string& strMappingValue, uint32_t uiFirstAmendKey, uint32_t uiNbAmendKeys, const CMappingContext& context)
183{
184 string strFormattedValue = strMappingValue;
185 // Search for amendment (only one supported for now)
186 size_t uiPercentPos = strFormattedValue.find('%', 0);
187
188 // Amendment limited to one digit (values from 1 to 9)
189 assert((uiNbAmendKeys > 0) && (uiNbAmendKeys <= 9));
190
191 // Check we found one and that there's room for value
192 if (uiPercentPos != string::npos && uiPercentPos < strFormattedValue.size() - 1) {
193
194 // Get Amend number
195 uint32_t uiAmendNumber = strFormattedValue[uiPercentPos + 1] - '0';
196
197 // Valid?
198 if (uiAmendNumber && uiAmendNumber <= uiNbAmendKeys) {
199
200 uint32_t uiAmendType = uiFirstAmendKey + uiAmendNumber - 1;
201
202 // Set?
203 if (context.iSet(uiAmendType)) {
204
205 // Get Amend value
206 string strAmendValue = context.getItem(uiAmendType);
207
208 // Make the amendment
209 strFormattedValue = strFormattedValue.substr(0, uiPercentPos) + strAmendValue + strFormattedValue.substr(uiPercentPos + 2, strFormattedValue.size() - uiPercentPos - 2);
210 }
211 }
212 }
213 return strFormattedValue;
214}