diff --git a/flight/modules/FlightPlan/lib/uavobject.json.template b/flight/modules/FlightPlan/lib/uavobject.json.template new file mode 100644 index 00000000000..15ffd2bea32 --- /dev/null +++ b/flight/modules/FlightPlan/lib/uavobject.json.template @@ -0,0 +1,9 @@ +{ + "name": "$(NAME)", + "object_id": $(UOBJID), + "size": $(SIZE), + "unpackstr": "$(UNPACKSTR)", + "fields": [ +$(DATAFIELDS) + ] +} \ No newline at end of file diff --git a/ground/uavobjgenerator/generators/json/uavobjectgeneratorjson.cpp b/ground/uavobjgenerator/generators/json/uavobjectgeneratorjson.cpp new file mode 100644 index 00000000000..8b78aa04671 --- /dev/null +++ b/ground/uavobjgenerator/generators/json/uavobjectgeneratorjson.cpp @@ -0,0 +1,143 @@ +/** + ****************************************************************************** + * + * @file uavobjectgeneratorjson.cpp + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief produce json code for uavobjects + * + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "uavobjectgeneratorjson.h" +using namespace std; + +bool UAVObjectGeneratorJson::generate(UAVObjectParser *parser, QString templatepath, QString outputpath) +{ + // Load template and setup output directory + jsonCodePath = QDir(templatepath + QString("flight/modules/FlightPlan/lib")); + jsonOutputPath = QDir(outputpath + QString("json")); + jsonOutputPath.mkpath(jsonOutputPath.absolutePath()); + jsonCodeTemplate = readFile(jsonCodePath.absoluteFilePath("uavobject.json.template")); + if (jsonCodeTemplate.isEmpty()) { + std::cerr << "Problem reading json templates" << endl; + return false; + } + + // Process each object + for (int objidx = 0; objidx < parser->getNumObjects(); ++objidx) { + ObjectInfo *info = parser->getObjectByIndex(objidx); + process_object(info); + } + + return true; // if we come here everything should be fine +} + +/** + * Generate the json object files + */ +bool UAVObjectGeneratorJson::process_object(ObjectInfo *info) +{ + if (info == NULL) { + return false; + } + + // Prepare output strings + QString outCode = jsonCodeTemplate; + + // Replace common tags + replaceCommonTags(outCode, info); + + // Replace the ($DATAFIELDS) tag + QStringList datafields; + QString unpackstr = "<"; + unsigned int bytecount = 0; + Q_FOREACH(const FieldInfo *field, info->fields) { + bytecount += field->numBytes; + QString f = " {\n"; + // Class header + f.append(QString(" \"name\": \"%1\",\n").arg(field->name)); + f.append(QString(" \"type\": %1,\n").arg(field->type)); + f.append(QString(" \"numElements\": %1\n").arg(field->numElements)); + if(field->type == FIELDTYPE_INT8) { + unpackstr.append("b"); + } else if(field->type == FIELDTYPE_INT16) { + unpackstr.append("h"); + } else if(field->type == FIELDTYPE_INT32) { + unpackstr.append("i"); + } else if(field->type == FIELDTYPE_UINT8) { + unpackstr.append("B"); + } else if(field->type == FIELDTYPE_UINT16) { + unpackstr.append("H"); + } else if(field->type == FIELDTYPE_UINT32) { + unpackstr.append("I"); + } else if(field->type == FIELDTYPE_FLOAT32) { + unpackstr.append("f"); + } else if(field->type == FIELDTYPE_ENUM) { + unpackstr.append("B"); + } else { + std::cerr << "json object generator: Unknown field type " << field->type << std::endl; + } + unpackstr.append("(").append(field->name).append(")"); + // Only for enum types + if (field->type == FIELDTYPE_ENUM) { + /* + datafields.append(QString(" # Enumeration options\n")); + // Go through each option + QStringList options = info->fields[n]->options; + for (int m = 0; m < options.length(); ++m) { + QString name = options[m].toUpper().replace(QRegExp(ENUM_SPECIAL_CHARS), ""); + if (name[0].isDigit()) { + name = QString("N%1").arg(name); + } + datafields.append(QString(" %1 = %2\n").arg(name).arg(m)); + } + */ + } + // Generate element names (only if field has more than one element) + if (field->numElements > 1 && !field->defaultElementNames) { + /* + datafields.append(QString(" # Array element names\n")); + // Go through the element names + QStringList elemNames = info->fields[n]->elementNames; + for (int m = 0; m < elemNames.length(); ++m) { + QString name = elemNames[m].toUpper().replace(QRegExp(ENUM_SPECIAL_CHARS), ""); + if (name[0].isDigit()) { + name = QString("N%1").arg(name); + } + datafields.append(QString(" %1 = %2\n").arg(name).arg(m)); + } + */ + } + f.append(" }"); + datafields.append(f); + } + outCode.replace(QString("$(DATAFIELDS)"), datafields.join(",\n")); + outCode.replace(QString("$(UNPACKSTR)"), unpackstr); + outCode.replace(QString("$(SIZE)"), QString("%1").arg(bytecount)); + + + // Write the Json code + bool res = writeFileIfDiffrent(jsonOutputPath.absolutePath() + "/" + info->namelc + ".json", outCode); + if (!res) { + cout << "Error: Could not write Json output files" << endl; + return false; + } + + return true; +} diff --git a/ground/uavobjgenerator/generators/json/uavobjectgeneratorjson.h b/ground/uavobjgenerator/generators/json/uavobjectgeneratorjson.h new file mode 100644 index 00000000000..221b96bfe9f --- /dev/null +++ b/ground/uavobjgenerator/generators/json/uavobjectgeneratorjson.h @@ -0,0 +1,44 @@ +/** + ****************************************************************************** + * + * @file uavobjectgeneratorjson.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief produce json code for uavobjects + * + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef UAVOBJECTGENERATORJSON_H +#define UAVOBJECTGENERATORJSON_H + +#include "../generator_common.h" + +class UAVObjectGeneratorJson { +public: + bool generate(UAVObjectParser *gen, QString templatepath, QString outputpath); + +private: + bool process_object(ObjectInfo *info); + + QString jsonCodeTemplate; + QDir jsonCodePath; + QDir jsonOutputPath; +}; + +#endif diff --git a/ground/uavobjgenerator/main.cpp b/ground/uavobjgenerator/main.cpp index 55de04f3dac..d143ff6c887 100644 --- a/ground/uavobjgenerator/main.cpp +++ b/ground/uavobjgenerator/main.cpp @@ -34,6 +34,7 @@ #include "generators/gcs/uavobjectgeneratorgcs.h" #include "generators/matlab/uavobjectgeneratormatlab.h" #include "generators/python/uavobjectgeneratorpython.h" +#include "generators/json/uavobjectgeneratorjson.h" #include "generators/wireshark/uavobjectgeneratorwireshark.h" #define RETURN_ERR_USAGE 1 @@ -53,6 +54,7 @@ void usage() cout << "\t-flight build flight code" << endl; cout << "\t-java build java code" << endl; cout << "\t-python build python code" << endl; + cout << "\t-json build json code" << endl; cout << "\t-matlab build matlab code" << endl; cout << "\t-wireshark build wireshark plugin" << endl; cout << "\tIf no language is specified ( and not -none ) -> all are built." << endl; @@ -107,11 +109,12 @@ int main(int argc, char *argv[]) bool do_flight = (arguments_stringlist.removeAll("-flight") > 0); bool do_java = (arguments_stringlist.removeAll("-java") > 0); bool do_python = (arguments_stringlist.removeAll("-python") > 0); + bool do_json = (arguments_stringlist.removeAll("-json") > 0); bool do_matlab = (arguments_stringlist.removeAll("-matlab") > 0); bool do_wireshark = (arguments_stringlist.removeAll("-wireshark") > 0); bool do_none = (arguments_stringlist.removeAll("-none") > 0); // - bool do_all = ((do_gcs || do_flight || do_java || do_python || do_matlab) == false); + bool do_all = ((do_gcs || do_flight || do_java || do_python || do_json || do_matlab) == false); bool do_allObjects = true; if (arguments_stringlist.length() >= 2) { @@ -237,6 +240,13 @@ int main(int argc, char *argv[]) pygen.generate(parser, templatepath, outputpath); } + // generate json code if wanted + if (do_json | do_all) { + cout << "generating json code" << endl; + UAVObjectGeneratorJson pygen; + pygen.generate(parser, templatepath, outputpath); + } + // generate matlab code if wanted if (do_matlab | do_all) { cout << "generating matlab code" << endl; diff --git a/ground/uavobjgenerator/uavobjgenerator.pro b/ground/uavobjgenerator/uavobjgenerator.pro index d3a06d92a41..b0001c87c01 100644 --- a/ground/uavobjgenerator/uavobjgenerator.pro +++ b/ground/uavobjgenerator/uavobjgenerator.pro @@ -20,6 +20,7 @@ SOURCES += main.cpp \ generators/gcs/uavobjectgeneratorgcs.cpp \ generators/matlab/uavobjectgeneratormatlab.cpp \ generators/python/uavobjectgeneratorpython.cpp \ + generators/json/uavobjectgeneratorjson.cpp \ generators/wireshark/uavobjectgeneratorwireshark.cpp \ generators/generator_common.cpp HEADERS += uavobjectparser.h \ @@ -28,5 +29,6 @@ HEADERS += uavobjectparser.h \ generators/gcs/uavobjectgeneratorgcs.h \ generators/matlab/uavobjectgeneratormatlab.h \ generators/python/uavobjectgeneratorpython.h \ + generators/json/uavobjectgeneratorjson.h \ generators/wireshark/uavobjectgeneratorwireshark.h \ generators/generator_common.h