blob: 8c5c5e453388da02e747f5134dbfe506cc3d01fd [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * INTEL CONFIDENTIAL
3 * Copyright © 2011 Intel
4 * Corporation All Rights Reserved.
5 *
6 * The source code contained or described herein and all documents related to
7 * the source code ("Material") are owned by Intel Corporation or its suppliers
8 * or licensors. Title to the Material remains with Intel Corporation or its
9 * suppliers and licensors. The Material contains trade secrets and proprietary
10 * and confidential information of Intel or its suppliers and licensors. The
11 * Material is protected by worldwide copyright and trade secret laws and
12 * treaty provisions. No part of the Material may be used, copied, reproduced,
13 * modified, published, uploaded, posted, transmitted, distributed, or
14 * disclosed in any way without Intel’s prior express written permission.
15 *
16 * No license under any patent, copyright, trade secret or other intellectual
17 * property right is granted to or conferred upon you by disclosure or delivery
18 * of the Materials, either expressly, by implication, inducement, estoppel or
19 * otherwise. Any license under such intellectual property rights must be
20 * express and approved by Intel in writing.
21 *
Patrick Benavoli68a91282011-08-31 11:23:23 +020022 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
Patrick Benavoli68a91282011-08-31 11:23:23 +020024 */
25#include "SubsystemObject.h"
26#include "InstanceConfigurableElement.h"
27#include "ParameterBlackboard.h"
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +010028#include "MappingContext.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020029#include <assert.h>
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020030#include <stdlib.h>
31#include <string.h>
32#include <sstream>
Patrick Benavoli63499d42011-10-24 18:50:03 +020033#include <stdarg.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020034
35CSubsystemObject::CSubsystemObject(CInstanceConfigurableElement* pInstanceConfigurableElement)
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020036 : _pInstanceConfigurableElement(pInstanceConfigurableElement),
37 _uiDataSize(pInstanceConfigurableElement->getFootPrint()),
38 _pucBlackboardLocation(NULL),
39 _uiAccessedIndex(0)
Patrick Benavoli68a91282011-08-31 11:23:23 +020040{
41 // Syncer
42 _pInstanceConfigurableElement->setSyncer(this);
43}
44
45CSubsystemObject::~CSubsystemObject()
46{
47 _pInstanceConfigurableElement->unsetSyncer();
48}
49
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020050// Blackboard data location
51uint8_t* CSubsystemObject::getBlackboardLocation() const
Patrick Benavoli68a91282011-08-31 11:23:23 +020052{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020053 return _pucBlackboardLocation;
Patrick Benavoli68a91282011-08-31 11:23:23 +020054}
55
56// Size
57uint32_t CSubsystemObject::getSize() const
58{
59 return _uiDataSize;
60}
61
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020062// Conversion utility
63uint32_t CSubsystemObject::asInteger(const string& strValue)
64{
65 return strtoul(strValue.c_str(), NULL, 0);
66}
67
68string CSubsystemObject::asString(uint32_t uiValue)
69{
70 ostringstream ostr;
71
72 ostr << uiValue;
73
74 return ostr.str();
75}
76
Patrick Benavoli68a91282011-08-31 11:23:23 +020077// Synchronization
78bool CSubsystemObject::sync(CParameterBlackboard& parameterBlackboard, bool bBack, string& strError)
79{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020080 // Get blackboard location
81 _pucBlackboardLocation = parameterBlackboard.getLocation(_pInstanceConfigurableElement->getOffset());
82 // Access index init
83 _uiAccessedIndex = 0;
Patrick Benavoli68a91282011-08-31 11:23:23 +020084
85#ifdef SIMULATION
86 return true;
87#endif
88
89 // Synchronize to/from HW
90 if (bBack) {
91
92 // Read from HW
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020093 if (!accessHW(true, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +020094
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020095 strError = "Unable to back synchronize configurable element " + _pInstanceConfigurableElement->getPath() + ": " + strError;
Kevin Rocardace81f82012-12-11 16:19:17 +010096 log_warning(strError);
Patrick Benavoli68a91282011-08-31 11:23:23 +020097
98 return false;
99 }
100
Patrick Benavoli68a91282011-08-31 11:23:23 +0200101 } else {
102
Patrick Benavoli68a91282011-08-31 11:23:23 +0200103 // Send to HW
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200104 if (!accessHW(false, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200105
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200106 strError = "Unable to synchronize configurable element " + _pInstanceConfigurableElement->getPath() + ": " + strError;
Kevin Rocardace81f82012-12-11 16:19:17 +0100107 log_warning(strError);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200108
109 return false;
110 }
111 }
112 return true;
113}
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200114
115// Sync to/from HW
116bool CSubsystemObject::sendToHW(string& strError)
117{
118 strError = "Send to HW interface not implemented at subsystsem level!";
119
120 return false;
121}
122
123bool CSubsystemObject::receiveFromHW(string& strError)
124{
125 strError = "Receive from HW interface not implemented at subsystsem level!";
126
127 return false;
128}
129
130// Fall back HW access
131bool CSubsystemObject::accessHW(bool bReceive, string& strError)
132{
Patrick Benavoli63499d42011-10-24 18:50:03 +0200133 // Default access fall back
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200134 if (bReceive) {
135
136 return receiveFromHW(strError);
137 } else {
138
139 return sendToHW(strError);
140 }
141}
142
143// Blackboard access from subsystems
144void CSubsystemObject::blackboardRead(void* pvData, uint32_t uiSize)
145{
146 assert(_uiAccessedIndex + uiSize <= _uiDataSize);
147
148 memcpy(pvData, _pucBlackboardLocation + _uiAccessedIndex, uiSize);
149
150 _uiAccessedIndex += uiSize;
151}
152
153void CSubsystemObject::blackboardWrite(const void* pvData, uint32_t uiSize)
154{
155 assert(_uiAccessedIndex + uiSize <= _uiDataSize);
156
157 memcpy(_pucBlackboardLocation + _uiAccessedIndex, pvData, uiSize);
158
159 _uiAccessedIndex += uiSize;
160}
Patrick Benavoli63499d42011-10-24 18:50:03 +0200161
162// Logging
Kevin Rocardace81f82012-12-11 16:19:17 +0100163void CSubsystemObject::log_info(const string& strMessage, ...) const
Patrick Benavoli63499d42011-10-24 18:50:03 +0200164{
165 char acBuffer[512];
166 va_list listPointer;
167
168 va_start(listPointer, strMessage);
169
170 vsnprintf(acBuffer, sizeof(acBuffer), strMessage.c_str(), listPointer);
171
172 va_end(listPointer);
173
Kevin Rocardace81f82012-12-11 16:19:17 +0100174 _pInstanceConfigurableElement->log_info(acBuffer);
175}
176
177void CSubsystemObject::log_warning(const string& strMessage, ...) const
178{
179 char acBuffer[512];
180 va_list listPointer;
181
182 va_start(listPointer, strMessage);
183
184 vsnprintf(acBuffer, sizeof(acBuffer), strMessage.c_str(), listPointer);
185
186 va_end(listPointer);
187
188 _pInstanceConfigurableElement->log_warning(acBuffer);
Patrick Benavoli63499d42011-10-24 18:50:03 +0200189}
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100190
191// Amendment
192string CSubsystemObject::formatMappingValue(const string& strMappingValue, uint32_t uiFirstAmendKey, uint32_t uiNbAmendKeys, const CMappingContext& context)
193{
194 string strFormattedValue = strMappingValue;
Patrick Benavolic3fe14f2012-03-06 14:54:07 +0100195
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100196 // Search for amendment (only one supported for now)
197 size_t uiPercentPos = strFormattedValue.find('%', 0);
198
199 // Amendment limited to one digit (values from 1 to 9)
200 assert((uiNbAmendKeys > 0) && (uiNbAmendKeys <= 9));
201
202 // Check we found one and that there's room for value
203 if (uiPercentPos != string::npos && uiPercentPos < strFormattedValue.size() - 1) {
204
205 // Get Amend number
206 uint32_t uiAmendNumber = strFormattedValue[uiPercentPos + 1] - '0';
207
208 // Valid?
209 if (uiAmendNumber && uiAmendNumber <= uiNbAmendKeys) {
210
211 uint32_t uiAmendType = uiFirstAmendKey + uiAmendNumber - 1;
212
213 // Set?
214 if (context.iSet(uiAmendType)) {
215
Patrick Benavolic3fe14f2012-03-06 14:54:07 +0100216 // Make the amendment on the part of the string after the current Amend
217 string strEndOfLine = strFormattedValue.substr(uiPercentPos + 2, strFormattedValue.size() - uiPercentPos - 2);
218 string strEndOfLineAmended = formatMappingValue(strEndOfLine, uiFirstAmendKey, uiNbAmendKeys, context);
219
220 // Get current Amend value
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100221 string strAmendValue = context.getItem(uiAmendType);
222
223 // Make the amendment
Patrick Benavolic3fe14f2012-03-06 14:54:07 +0100224 strFormattedValue = strFormattedValue.substr(0, uiPercentPos) + strAmendValue + strEndOfLineAmended;
225
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100226 }
227 }
228 }
229 return strFormattedValue;
230}
Frederic Boisnard74edbc82012-02-28 15:59:22 +0100231
232// Configurable element retrieval
233const CInstanceConfigurableElement* CSubsystemObject::getConfigurableElement() const
234{
235 return _pInstanceConfigurableElement;
236}