blob: f98509ebf121c79315421320b4857d25192900c6 [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;
Patrick Benavoli68a91282011-08-31 11:23:23 +020096
97 return false;
98 }
99
Patrick Benavoli68a91282011-08-31 11:23:23 +0200100 } else {
101
Patrick Benavoli68a91282011-08-31 11:23:23 +0200102 // Send to HW
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200103 if (!accessHW(false, strError)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200104
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200105 strError = "Unable to synchronize configurable element " + _pInstanceConfigurableElement->getPath() + ": " + strError;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200106
107 return false;
108 }
109 }
110 return true;
111}
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200112
113// Sync to/from HW
114bool CSubsystemObject::sendToHW(string& strError)
115{
116 strError = "Send to HW interface not implemented at subsystsem level!";
117
118 return false;
119}
120
121bool CSubsystemObject::receiveFromHW(string& strError)
122{
123 strError = "Receive from HW interface not implemented at subsystsem level!";
124
125 return false;
126}
127
128// Fall back HW access
129bool CSubsystemObject::accessHW(bool bReceive, string& strError)
130{
Patrick Benavoli63499d42011-10-24 18:50:03 +0200131 // Default access fall back
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200132 if (bReceive) {
133
134 return receiveFromHW(strError);
135 } else {
136
137 return sendToHW(strError);
138 }
139}
140
141// Blackboard access from subsystems
142void CSubsystemObject::blackboardRead(void* pvData, uint32_t uiSize)
143{
144 assert(_uiAccessedIndex + uiSize <= _uiDataSize);
145
146 memcpy(pvData, _pucBlackboardLocation + _uiAccessedIndex, uiSize);
147
148 _uiAccessedIndex += uiSize;
149}
150
151void CSubsystemObject::blackboardWrite(const void* pvData, uint32_t uiSize)
152{
153 assert(_uiAccessedIndex + uiSize <= _uiDataSize);
154
155 memcpy(_pucBlackboardLocation + _uiAccessedIndex, pvData, uiSize);
156
157 _uiAccessedIndex += uiSize;
158}
Patrick Benavoli63499d42011-10-24 18:50:03 +0200159
160// Logging
161void CSubsystemObject::log(const string& strMessage, ...) const
162{
163 char acBuffer[512];
164 va_list listPointer;
165
166 va_start(listPointer, strMessage);
167
168 vsnprintf(acBuffer, sizeof(acBuffer), strMessage.c_str(), listPointer);
169
170 va_end(listPointer);
171
172 _pInstanceConfigurableElement->log(acBuffer);
173}
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100174
175// Amendment
176string CSubsystemObject::formatMappingValue(const string& strMappingValue, uint32_t uiFirstAmendKey, uint32_t uiNbAmendKeys, const CMappingContext& context)
177{
178 string strFormattedValue = strMappingValue;
Patrick Benavolic3fe14f2012-03-06 14:54:07 +0100179
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100180 // Search for amendment (only one supported for now)
181 size_t uiPercentPos = strFormattedValue.find('%', 0);
182
183 // Amendment limited to one digit (values from 1 to 9)
184 assert((uiNbAmendKeys > 0) && (uiNbAmendKeys <= 9));
185
186 // Check we found one and that there's room for value
187 if (uiPercentPos != string::npos && uiPercentPos < strFormattedValue.size() - 1) {
188
189 // Get Amend number
190 uint32_t uiAmendNumber = strFormattedValue[uiPercentPos + 1] - '0';
191
192 // Valid?
193 if (uiAmendNumber && uiAmendNumber <= uiNbAmendKeys) {
194
195 uint32_t uiAmendType = uiFirstAmendKey + uiAmendNumber - 1;
196
197 // Set?
198 if (context.iSet(uiAmendType)) {
199
Patrick Benavolic3fe14f2012-03-06 14:54:07 +0100200 // Make the amendment on the part of the string after the current Amend
201 string strEndOfLine = strFormattedValue.substr(uiPercentPos + 2, strFormattedValue.size() - uiPercentPos - 2);
202 string strEndOfLineAmended = formatMappingValue(strEndOfLine, uiFirstAmendKey, uiNbAmendKeys, context);
203
204 // Get current Amend value
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100205 string strAmendValue = context.getItem(uiAmendType);
206
207 // Make the amendment
Patrick Benavolic3fe14f2012-03-06 14:54:07 +0100208 strFormattedValue = strFormattedValue.substr(0, uiPercentPos) + strAmendValue + strEndOfLineAmended;
209
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100210 }
211 }
212 }
213 return strFormattedValue;
214}
Frederic Boisnard74edbc82012-02-28 15:59:22 +0100215
216// Configurable element retrieval
217const CInstanceConfigurableElement* CSubsystemObject::getConfigurableElement() const
218{
219 return _pInstanceConfigurableElement;
220}