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) {
209 AbstractInterface* inter;
211 if ((elt.isNull()) || (elt.tagName() != "interfaces")) throw (Exception(BLOCKFILE_CORRUPTED));
213 QDomElement eltInputs = elt.firstChildElement("inputs");
214 QDomNodeList listNodeInputs = eltInputs.elementsByTagName("input");
215 for(int i=0;i<listNodeInputs.size();i++) {
216 QDomNode node = listNodeInputs.at(i);
217 QDomElement eltInput = node.toElement();
218 nameStr = eltInput.attribute("name","none");
219 typeStr = eltInput.attribute("type","none");
220 widthStr = eltInput.attribute("width","none");
221 purposeStr = eltInput.attribute("purpose","none");
222 cout << "block : " << this->getName().toStdString() << endl;
223 cout << "purpose for " << nameStr.toStdString() << " : " << purposeStr.toStdString() << endl;
224 purpose = ReferenceInterface::translatePurpose(purposeStr);
225 cout << "translated purpose : " << purpose << endl;
226 multStr = eltInput.attribute("multiplicity","none");
227 mult = ReferenceInterface::translateMultiplicity(multStr);
229 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Input, purpose, mult);
230 inputs.append(inter);
233 QDomElement eltOutputs = eltInputs.nextSiblingElement("outputs");
234 QDomNodeList listNodeOutputs = eltOutputs.elementsByTagName("output");
235 for(int i=0;i<listNodeOutputs.size();i++) {
236 QDomNode node = listNodeOutputs.at(i);
237 QDomElement eltOutput = node.toElement();
238 nameStr = eltOutput.attribute("name","none");
239 typeStr = eltOutput.attribute("type","none");
240 widthStr = eltOutput.attribute("width","none");
241 purposeStr = eltOutput.attribute("type","none");
242 purpose = ReferenceInterface::translatePurpose(purposeStr);
243 multStr = eltOutput.attribute("multiplicity","none");
244 mult = ReferenceInterface::translateMultiplicity(multStr);
246 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Output, purpose, mult);
247 outputs.append(inter);
250 QDomElement eltBidirs = eltInputs.nextSiblingElement("bidirs");
251 QDomNodeList listNodeBidirs = eltBidirs.elementsByTagName("bidir");
252 for(int i=0;i<listNodeBidirs.size();i++) {
253 QDomNode node = listNodeBidirs.at(i);
254 QDomElement eltBidir = node.toElement();
255 nameStr = eltBidir.attribute("name","none");
256 typeStr = eltBidir.attribute("type","none");
257 widthStr = eltBidir.attribute("width","none");
258 purposeStr = eltBidir.attribute("type","none");
259 purpose = ReferenceInterface::translatePurpose(purposeStr);
260 multStr = eltBidir.attribute("multiplicity","none");
261 mult = ReferenceInterface::translateMultiplicity(multStr);
263 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::InOut, purpose, mult);
264 bidirs.append(inter);
268 void ReferenceBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure)
274 only used to save all ReferenceBlock in a library in binary format, so that reference blocks
275 are read very fast at application startup.
278 QDataStream& operator<<(QDataStream &out, const ReferenceBlock &b) {
280 out.setVersion(QDataStream::Qt_5_0);
282 QByteArray blockData;
283 QDataStream toWrite(&blockData, QIODevice::WriteOnly);
286 toWrite << b.xmlFile;
287 toWrite << b.descriptionBrief;
288 toWrite << b.descriptionDetail;
289 toWrite << b.categories;
290 toWrite << b.hashMd5;
291 toWrite << b.params.size();
293 for(int i=0;i<b.params.size();i++) {
295 toWrite << p->getContext();
296 toWrite << p->getName();
297 toWrite << p->getTypeString();
298 toWrite << p->getValue().toString();
299 if (p->isPortParameter()) {
300 toWrite << ((BlockParameterPort*)p)->getIfaceName();
302 else if (p->isWishboneParameter()) {
303 BlockParameterWishbone* pwb = (BlockParameterWishbone*)p;
304 toWrite << pwb->getWidth();
305 toWrite << pwb->getWBAccess();
306 toWrite << pwb->getWBValue();
307 toWrite << pwb->getWBDuration();
312 toWrite << b.inputs.size();
313 for(int i=0; i<b.inputs.size(); i++){
314 ReferenceInterface *iface = (ReferenceInterface *)(b.inputs.at(i));
315 toWrite << iface->getName();
316 toWrite << iface->getWidth();
317 toWrite << iface->getPurpose();
318 toWrite << iface->getDirection();
319 toWrite << iface->getMultiplicity();
321 toWrite << b.outputs.size();
322 for(int i=0; i<b.outputs.size(); i++){
323 ReferenceInterface *iface = (ReferenceInterface *)(b.outputs.at(i));
324 toWrite << iface->getName();
325 toWrite << iface->getWidth();
326 toWrite << iface->getPurpose();
327 toWrite << iface->getDirection();
328 toWrite << iface->getMultiplicity();
330 toWrite << b.bidirs.size();
331 for(int i=0; i<b.bidirs.size(); i++){
332 ReferenceInterface *iface = (ReferenceInterface *)(b.bidirs.at(i));
333 toWrite << iface->getName();
334 toWrite << iface->getWidth();
335 toWrite << iface->getPurpose();
336 toWrite << iface->getDirection();
337 toWrite << iface->getMultiplicity();
345 QDataStream& operator>>(QDataStream &in, ReferenceBlock &b) {
348 ReferenceInterface* iface;
353 in.setVersion(QDataStream::Qt_5_0);
359 in >> b.descriptionBrief;
360 in >> b.descriptionDetail;
366 cout << qPrintable(b.name) << " has " << nb << " parameters" << endl;
367 for(int i=0;i<nb;i++) {
368 QString contextStr = "";
370 QString typeStr = "";
371 QString valueStr = "";
377 if (contextStr == "user") {
378 p = new BlockParameterUser(&b,nameStr,valueStr);
380 else if (contextStr == "generic") {
381 p = new BlockParameterGeneric(&b,nameStr,typeStr,valueStr);
383 else if (contextStr == "port") {
384 QString ifaceStr = "";
386 p = new BlockParameterPort(&b,nameStr,valueStr,ifaceStr);
388 else if (contextStr == "wb") {
389 QString widthStr = "";
397 p = new BlockParameterWishbone(&b,nameStr,typeStr,widthStr,valueStr,wbAccess,wbValue,wbDuration);
404 for(int i=0;i<nb;i++) {
405 iface = new ReferenceInterface(&b);
409 iface->setWidth(txt);
411 iface->setPurpose(val);
413 iface->setDirection(val);
415 iface->setMultiplicity(val);
416 b.inputs.append(iface);
421 for(int i=0;i<nb;i++) {
422 iface = new ReferenceInterface(&b);
426 iface->setWidth(txt);
428 iface->setPurpose(val);
430 iface->setDirection(val);
432 iface->setMultiplicity(val);
433 b.outputs.append(iface);
438 for(int i=0;i<nb;i++) {
439 iface = new ReferenceInterface(&b);
443 iface->setWidth(txt);
445 iface->setPurpose(val);
447 iface->setDirection(val);
449 iface->setMultiplicity(val);
450 b.bidirs.append(iface);