1 #include "ReferenceBlock.h"
3 #include "ReferenceInterface.h"
4 #include "BlockParameter.h"
5 #include "BlockParameterUser.h"
6 #include "BlockParameterGeneric.h"
7 #include "BlockParameterPort.h"
8 #include "BlockParameterWishbone.h"
10 ReferenceBlock::ReferenceBlock(const QString _xmlFile) : AbstractBlock() {
14 void ReferenceBlock::addCategory(int id) {
15 categories.append(id);
18 void ReferenceBlock::setBriefDescription(const QString& str) {
20 descriptionBrief = str;
23 void ReferenceBlock::setDetailedDescription(const QString& str) {
25 descriptionDetail = str;
28 void ReferenceBlock::addImplementation(BlockImplementation *impl) {
29 implementations.append(impl);
32 void ReferenceBlock::setHashMd5() {
34 if (file.open(QIODevice::ReadOnly)) {
35 QByteArray fileData = file.readAll();
36 QByteArray hashData = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
37 hashMd5 = QString(hashData.toHex());
38 cout << qPrintable(xmlFile) << " has md5 hash : " << qPrintable(hashMd5) << endl;
45 void ReferenceBlock::load(QDomElement &elt) throw(Exception) {
48 cout << "Block : get informations" << endl;
49 QDomElement eltInfo = elt.firstChildElement("informations");
51 loadInformations(eltInfo);
57 cout << "Block : get params" << endl;
58 QDomElement eltParams = eltInfo.nextSiblingElement("parameters");
60 loadParameters(eltParams);
66 cout << "Block : get interfaces" << endl;
67 QDomElement eltInter = eltParams.nextSiblingElement("interfaces");
69 loadInterfaces(eltInter);
76 void ReferenceBlock::loadInformations(QDomElement &elt) throw(Exception) {
79 if ((elt.isNull()) || (elt.tagName() != "informations")) throw (Exception(BLOCKFILE_CORRUPTED));
81 cout << "Block info : get name" << endl;
82 QDomNode nodeName = elt.firstChild();
83 QDomNode nodeNameTxt = nodeName.firstChild();
84 if (nodeNameTxt.isNull()) {
88 QDomText txtName = nodeNameTxt.toText();
89 name = txtName.data().trimmed();
90 cout<< "block name : " << qPrintable(name) << endl;
94 cout << "Block info : get categories" << endl;
95 QDomElement eltCat = nodeName.nextSiblingElement("category");
97 QString idsStr = eltCat.attribute("ids","none");
98 if (idsStr == "none") throw (Exception(BLOCKFILE_CORRUPTED));
99 QStringList listCat = idsStr.split(",");
100 foreach(QString str, listCat)
102 int idCat = str.toInt(&ok);
103 categories.append(idCat);
106 // getting description
107 cout << "Block info : get description" << endl;
108 QDomElement eltDesc = eltCat.nextSiblingElement("description");
110 QDomElement eltBrief = eltDesc.firstChildElement("brief");
111 QDomNode nodeBriefTxt = eltBrief.firstChild();
112 if (nodeBriefTxt.isNull()) {
113 descriptionBrief = "no brief description";
116 QDomText txtBrief = nodeBriefTxt.toText();
117 descriptionBrief = txtBrief.data().trimmed();
118 cout << "block brief desc : " << qPrintable(descriptionBrief) << endl;
121 QDomElement eltDetail = eltBrief.nextSiblingElement("detailed");
122 QDomNode nodeDetailTxt = eltDetail.firstChild();
123 if (nodeDetailTxt.isNull()) {
124 descriptionDetail = "no detailed description";
127 QDomText txtDetail = nodeDetailTxt.toText();
128 descriptionDetail = txtDetail.data().trimmed();
129 cout << "block detail desc : " << qPrintable(descriptionDetail) << endl;
133 void ReferenceBlock::loadParameters(QDomElement &elt) throw(Exception) {
135 if ((elt.isNull()) || (elt.tagName() != "parameters")) throw (Exception(BLOCKFILE_CORRUPTED));
137 QDomNodeList listNodeParam = elt.elementsByTagName("parameter");
138 for(int i=0; i<listNodeParam.size(); i++) {
139 QDomNode node = listNodeParam.at(i);
140 QDomElement elt = node.toElement();
141 QString nameStr = elt.attribute("name","none");
142 QString contextStr = elt.attribute("context","none");
143 QString typeStr = elt.attribute("type","none");
144 QString valueStr = elt.attribute("value","none");
145 BlockParameter *param = NULL;
147 if(valueStr == "none"){
148 if (contextStr == "generic") throw (Exception(BLOCKFILE_CORRUPTED)); // set is required for generics
151 if (contextStr == "user") {
152 param = new BlockParameterUser(this,nameStr,valueStr);
154 else if (contextStr == "generic") {
155 param = new BlockParameterGeneric(this,nameStr,typeStr,valueStr);
157 else if (contextStr == "wb") {
158 QString widthStr = elt.attribute("width","none");
159 QString wbStr = elt.attribute("wishbone","none");
162 QString wbValue = "";
163 QStringList listWb = wbStr.split(",");
165 if (listWb.at(0) == "r") {
166 access = BlockParameter::Read;
168 else if (wbStr == "w") {
169 access = BlockParameter::Write;
171 wbValue = listWb.at(1).toInt(&ok);
173 if(listWb.at(1) == "true" || listWb.at(1) == "false"){
174 wbValue = listWb.at(1);
179 if(listWb.at(2) == "trigger") {
180 duration = BlockParameter::Trigger;
182 else if(listWb.at(2) == "perm") {
183 duration = BlockParameter::Permanent;
186 param = new BlockParameterWishbone(this,nameStr,typeStr,widthStr,valueStr,access,wbValue,duration);
188 else if (contextStr == "port") {
189 QString ifaceStr = elt.attribute("iface","none");
190 param = new BlockParameterPort(this,nameStr,valueStr,ifaceStr);
194 throw (Exception(BLOCKFILE_CORRUPTED));
196 params.append(param);
200 void ReferenceBlock::loadInterfaces(QDomElement &elt) throw(Exception) {
211 AbstractInterface* inter;
213 if ((elt.isNull()) || (elt.tagName() != "interfaces")) throw (Exception(BLOCKFILE_CORRUPTED));
215 QDomElement eltInputs = elt.firstChildElement("inputs");
216 QDomNodeList listNodeInputs = eltInputs.elementsByTagName("input");
217 for(int i=0;i<listNodeInputs.size();i++) {
218 QDomNode node = listNodeInputs.at(i);
219 QDomElement eltInput = node.toElement();
220 nameStr = eltInput.attribute("name","none");
221 typeStr = eltInput.attribute("type","none");
222 widthStr = eltInput.attribute("width","none");
223 purposeStr = eltInput.attribute("purpose","none");
224 cout << "block : " << this->getName().toStdString() << endl;
225 cout << "purpose for " << nameStr.toStdString() << " : " << purposeStr.toStdString() << endl;
226 purpose = ReferenceInterface::translatePurpose(purposeStr);
227 cout << "translated purpose : " << purpose << endl;
228 levelStr = eltInput.attribute("level","none");
229 level = ReferenceInterface::translateLevel(levelStr);
230 multStr = eltInput.attribute("multiplicity","none");
231 mult = ReferenceInterface::translateMultiplicity(multStr);
233 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Input, purpose, level, mult);
234 inputs.append(inter);
237 QDomElement eltOutputs = eltInputs.nextSiblingElement("outputs");
238 QDomNodeList listNodeOutputs = eltOutputs.elementsByTagName("output");
239 for(int i=0;i<listNodeOutputs.size();i++) {
240 QDomNode node = listNodeOutputs.at(i);
241 QDomElement eltOutput = node.toElement();
242 nameStr = eltOutput.attribute("name","none");
243 typeStr = eltOutput.attribute("type","none");
244 widthStr = eltOutput.attribute("width","none");
245 purposeStr = eltOutput.attribute("type","none");
246 purpose = ReferenceInterface::translatePurpose(purposeStr);
247 levelStr = eltOutput.attribute("level","none");
248 level = ReferenceInterface::translateLevel(levelStr);
249 multStr = eltOutput.attribute("multiplicity","none");
250 mult = ReferenceInterface::translateMultiplicity(multStr);
252 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Output, purpose, level, mult);
253 outputs.append(inter);
256 QDomElement eltBidirs = eltInputs.nextSiblingElement("bidirs");
257 QDomNodeList listNodeBidirs = eltBidirs.elementsByTagName("bidir");
258 for(int i=0;i<listNodeBidirs.size();i++) {
259 QDomNode node = listNodeBidirs.at(i);
260 QDomElement eltBidir = node.toElement();
261 nameStr = eltBidir.attribute("name","none");
262 typeStr = eltBidir.attribute("type","none");
263 widthStr = eltBidir.attribute("width","none");
264 purposeStr = eltBidir.attribute("type","none");
265 purpose = ReferenceInterface::translatePurpose(purposeStr);
266 levelStr = eltBidir.attribute("level","none");
267 level = ReferenceInterface::translateLevel(levelStr);
268 multStr = eltBidir.attribute("multiplicity","none");
269 mult = ReferenceInterface::translateMultiplicity(multStr);
271 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::InOut, purpose, level, mult);
272 bidirs.append(inter);
276 void ReferenceBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure)
282 only used to save all ReferenceBlock in a library in binary format, so that reference blocks
283 are read very fast at application startup.
286 QDataStream& operator<<(QDataStream &out, const ReferenceBlock &b) {
288 out.setVersion(QDataStream::Qt_5_0);
290 QByteArray blockData;
291 QDataStream toWrite(&blockData, QIODevice::WriteOnly);
294 toWrite << b.xmlFile;
295 toWrite << b.descriptionBrief;
296 toWrite << b.descriptionDetail;
297 toWrite << b.categories;
298 toWrite << b.hashMd5;
299 toWrite << b.params.size();
301 for(int i=0;i<b.params.size();i++) {
303 toWrite << p->getContext();
304 toWrite << p->getName();
305 toWrite << p->getTypeString();
306 toWrite << p->getValue().toString();
307 if (p->isPortParameter()) {
308 toWrite << ((BlockParameterPort*)p)->getIfaceName();
310 else if (p->isWishboneParameter()) {
311 BlockParameterWishbone* pwb = (BlockParameterWishbone*)p;
312 toWrite << pwb->getWidth();
313 toWrite << pwb->getWBAccess();
314 toWrite << pwb->getWBValue();
315 toWrite << pwb->getWBDuration();
320 toWrite << b.inputs.size();
321 for(int i=0; i<b.inputs.size(); i++){
322 ReferenceInterface *iface = (ReferenceInterface *)(b.inputs.at(i));
323 toWrite << iface->getName();
324 toWrite << iface->getWidth();
325 toWrite << iface->getPurpose();
326 toWrite << iface->getDirection();
327 toWrite << iface->getLevel();
328 toWrite << iface->getMultiplicity();
330 toWrite << b.outputs.size();
331 for(int i=0; i<b.outputs.size(); i++){
332 ReferenceInterface *iface = (ReferenceInterface *)(b.outputs.at(i));
333 toWrite << iface->getName();
334 toWrite << iface->getWidth();
335 toWrite << iface->getPurpose();
336 toWrite << iface->getDirection();
337 toWrite << iface->getLevel();
338 toWrite << iface->getMultiplicity();
340 toWrite << b.bidirs.size();
341 for(int i=0; i<b.bidirs.size(); i++){
342 ReferenceInterface *iface = (ReferenceInterface *)(b.bidirs.at(i));
343 toWrite << iface->getName();
344 toWrite << iface->getWidth();
345 toWrite << iface->getPurpose();
346 toWrite << iface->getDirection();
347 toWrite << iface->getLevel();
348 toWrite << iface->getMultiplicity();
356 QDataStream& operator>>(QDataStream &in, ReferenceBlock &b) {
359 ReferenceInterface* iface;
364 in.setVersion(QDataStream::Qt_5_0);
370 in >> b.descriptionBrief;
371 in >> b.descriptionDetail;
377 cout << qPrintable(b.name) << " has " << nb << " parameters" << endl;
378 for(int i=0;i<nb;i++) {
379 QString contextStr = "";
381 QString typeStr = "";
382 QString valueStr = "";
388 if (contextStr == "user") {
389 p = new BlockParameterUser(&b,nameStr,valueStr);
391 else if (contextStr == "generic") {
392 p = new BlockParameterGeneric(&b,nameStr,typeStr,valueStr);
394 else if (contextStr == "port") {
395 QString ifaceStr = "";
397 p = new BlockParameterPort(&b,nameStr,valueStr,ifaceStr);
399 else if (contextStr == "wb") {
400 QString widthStr = "";
408 p = new BlockParameterWishbone(&b,nameStr,typeStr,widthStr,valueStr,wbAccess,wbValue,wbDuration);
415 for(int i=0;i<nb;i++) {
416 iface = new ReferenceInterface(&b);
420 iface->setWidth(txt);
422 iface->setPurpose(val);
424 iface->setDirection(val);
426 iface->setLevel(val);
428 iface->setMultiplicity(val);
429 b.inputs.append(iface);
434 for(int i=0;i<nb;i++) {
435 iface = new ReferenceInterface(&b);
439 iface->setWidth(txt);
441 iface->setPurpose(val);
443 iface->setDirection(val);
445 iface->setLevel(val);
447 iface->setMultiplicity(val);
448 b.outputs.append(iface);
453 for(int i=0;i<nb;i++) {
454 iface = new ReferenceInterface(&b);
458 iface->setWidth(txt);
460 iface->setPurpose(val);
462 iface->setDirection(val);
464 iface->setLevel(val);
466 iface->setMultiplicity(val);
467 b.bidirs.append(iface);