2011-01-13 03:26:00 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file uavobjectgeneratorjava.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief produce java 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
|
|
|
|
*/
|
|
|
|
|
2011-03-06 07:03:46 +01:00
|
|
|
#include <QDebug>
|
2011-01-13 03:26:00 +01:00
|
|
|
#include "uavobjectgeneratorjava.h"
|
|
|
|
using namespace std;
|
|
|
|
|
2011-01-22 18:38:48 +01:00
|
|
|
bool UAVObjectGeneratorJava::generate(UAVObjectParser* parser,QString templatepath,QString outputpath) {
|
2011-03-06 09:26:09 +01:00
|
|
|
fieldTypeStrCPP << "Byte" << "Short" << "Int" <<
|
|
|
|
"Short" << "Int" << "Long" << "Float" << "Byte";
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2011-03-06 07:03:46 +01:00
|
|
|
fieldTypeStrCPPClass << "INT8" << "INT16" << "INT32"
|
|
|
|
<< "UINT8" << "UINT16" << "UINT32" << "FLOAT32" << "ENUM";
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2011-03-06 07:03:46 +01:00
|
|
|
javaCodePath = QDir( templatepath + QString(JAVA_TEMPLATE_DIR));
|
|
|
|
javaOutputPath = QDir( outputpath + QString("java") );
|
|
|
|
javaOutputPath.mkpath(javaOutputPath.absolutePath());
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2013-04-30 02:02:36 +02:00
|
|
|
javaCodeTemplate = readFile( javaCodePath.absoluteFilePath("uavobject.java.template") );
|
|
|
|
QString javaInitTemplate = readFile( javaCodePath.absoluteFilePath("uavobjectsinit.java.template") );
|
2011-03-06 07:03:46 +01:00
|
|
|
|
|
|
|
if (javaCodeTemplate.isEmpty() || javaInitTemplate.isEmpty()) {
|
|
|
|
std::cerr << "Problem reading java code templates" << endl;
|
2011-01-13 03:26:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-06 07:03:46 +01:00
|
|
|
QString objInc;
|
|
|
|
QString javaObjInit;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
for (int objidx = 0; objidx < parser->getNumObjects(); ++objidx) {
|
|
|
|
ObjectInfo* info=parser->getObjectByIndex(objidx);
|
|
|
|
process_object(info);
|
|
|
|
|
2011-03-06 20:12:32 +01:00
|
|
|
javaObjInit.append("\t\t\tobjMngr.registerObject( new " + info->name + "() );\n");
|
2011-03-06 07:03:46 +01:00
|
|
|
objInc.append("#include \"" + info->namelc + ".h\"\n");
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
|
|
|
|
2011-03-06 07:03:46 +01:00
|
|
|
// Write the gcs object inialization files
|
|
|
|
javaInitTemplate.replace( QString("$(OBJINC)"), objInc);
|
|
|
|
javaInitTemplate.replace( QString("$(OBJINIT)"), javaObjInit);
|
2011-03-06 20:12:32 +01:00
|
|
|
bool res = writeFileIfDiffrent( javaOutputPath.absolutePath() + "/UAVObjectsInitialize.java", javaInitTemplate );
|
2011-01-13 03:26:00 +01:00
|
|
|
if (!res) {
|
2011-03-06 07:03:46 +01:00
|
|
|
cout << "Error: Could not write output files" << endl;
|
2011-01-13 03:26:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true; // if we come here everything should be fine
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the java object files
|
|
|
|
*/
|
|
|
|
bool UAVObjectGeneratorJava::process_object(ObjectInfo* info)
|
|
|
|
{
|
2011-03-06 07:03:46 +01:00
|
|
|
if (info == NULL)
|
|
|
|
return false;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
// Prepare output strings
|
2011-03-06 07:03:46 +01:00
|
|
|
QString outInclude = javaIncludeTemplate;
|
2011-01-13 03:26:00 +01:00
|
|
|
QString outCode = javaCodeTemplate;
|
|
|
|
|
|
|
|
// Replace common tags
|
2011-03-06 07:03:46 +01:00
|
|
|
replaceCommonTags(outInclude, info);
|
2011-01-13 03:26:00 +01:00
|
|
|
replaceCommonTags(outCode, info);
|
|
|
|
|
2011-03-06 07:03:46 +01:00
|
|
|
// Replace the $(DATAFIELDS) tag
|
|
|
|
QString type;
|
|
|
|
QString fields;
|
|
|
|
for (int n = 0; n < info->fields.length(); ++n)
|
|
|
|
{
|
|
|
|
// Determine type
|
|
|
|
type = fieldTypeStrCPP[info->fields[n]->type];
|
|
|
|
// Append field
|
|
|
|
if ( info->fields[n]->numElements > 1 )
|
|
|
|
{
|
|
|
|
fields.append( QString(" %1 %2[%3];\n").arg(type).arg(info->fields[n]->name)
|
|
|
|
.arg(info->fields[n]->numElements) );
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
2011-03-06 07:03:46 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fields.append( QString(" %1 %2;\n").arg(type).arg(info->fields[n]->name) );
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
2011-03-06 07:03:46 +01:00
|
|
|
}
|
|
|
|
outInclude.replace(QString("$(DATAFIELDS)"), fields);
|
|
|
|
|
|
|
|
// Replace the $(FIELDSINIT) tag
|
|
|
|
QString finit;
|
|
|
|
for (int n = 0; n < info->fields.length(); ++n)
|
|
|
|
{
|
|
|
|
finit.append("\n");
|
|
|
|
|
|
|
|
// Setup element names
|
|
|
|
QString varElemName = info->fields[n]->name + "ElemNames";
|
|
|
|
finit.append( QString("\t\tList<String> %1 = new ArrayList<String>();\n").arg(varElemName) );
|
|
|
|
QStringList elemNames = info->fields[n]->elementNames;
|
|
|
|
for (int m = 0; m < elemNames.length(); ++m)
|
|
|
|
finit.append( QString("\t\t%1.add(\"%2\");\n")
|
|
|
|
.arg(varElemName)
|
|
|
|
.arg(elemNames[m]) );
|
|
|
|
|
|
|
|
// Only for enum types
|
|
|
|
if (info->fields[n]->type == FIELDTYPE_ENUM) {
|
|
|
|
QString varOptionName = info->fields[n]->name + "EnumOptions";
|
|
|
|
finit.append( QString("\t\tList<String> %1 = new ArrayList<String>();\n").arg(varOptionName) );
|
|
|
|
QStringList options = info->fields[n]->options;
|
|
|
|
for (int m = 0; m < options.length(); ++m)
|
|
|
|
{
|
|
|
|
finit.append( QString("\t\t%1.add(\"%2\");\n")
|
|
|
|
.arg(varOptionName)
|
|
|
|
.arg(options[m]) );
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
2011-03-06 07:03:46 +01:00
|
|
|
finit.append( QString("\t\tfields.add( new UAVObjectField(\"%1\", \"%2\", UAVObjectField.FieldType.ENUM, %3, %4) );\n")
|
|
|
|
.arg(info->fields[n]->name)
|
|
|
|
.arg(info->fields[n]->units)
|
|
|
|
.arg(varElemName)
|
|
|
|
.arg(varOptionName) );
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
2011-03-06 07:03:46 +01:00
|
|
|
// For all other types
|
|
|
|
else {
|
|
|
|
finit.append( QString("\t\tfields.add( new UAVObjectField(\"%1\", \"%2\", UAVObjectField.FieldType.%3, %4, null) );\n")
|
|
|
|
.arg(info->fields[n]->name)
|
|
|
|
.arg(info->fields[n]->units)
|
|
|
|
.arg(fieldTypeStrCPPClass[info->fields[n]->type])
|
|
|
|
.arg(varElemName) );
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
2011-03-06 07:03:46 +01:00
|
|
|
}
|
|
|
|
outCode.replace(QString("$(FIELDSINIT)"), finit);
|
|
|
|
|
|
|
|
// Replace the $(DATAFIELDINFO) tag
|
|
|
|
QString name;
|
|
|
|
QString enums;
|
|
|
|
for (int n = 0; n < info->fields.length(); ++n)
|
|
|
|
{
|
|
|
|
enums.append(QString(" // Field %1 information\n").arg(info->fields[n]->name));
|
|
|
|
// Only for enum types
|
|
|
|
if (info->fields[n]->type == FIELDTYPE_ENUM)
|
|
|
|
{
|
|
|
|
enums.append(QString(" /* Enumeration options for field %1 */\n").arg(info->fields[n]->name));
|
|
|
|
enums.append(" typedef enum { ");
|
|
|
|
// Go through each option
|
|
|
|
QStringList options = info->fields[n]->options;
|
|
|
|
for (int m = 0; m < options.length(); ++m) {
|
|
|
|
QString s = (m != (options.length()-1)) ? "%1_%2=%3, " : "%1_%2=%3";
|
|
|
|
enums.append( s.arg( info->fields[n]->name.toUpper() )
|
|
|
|
.arg( options[m].toUpper().replace(QRegExp(ENUM_SPECIAL_CHARS), "") )
|
|
|
|
.arg(m) );
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
}
|
2011-03-06 07:03:46 +01:00
|
|
|
enums.append( QString(" } %1Options;\n")
|
|
|
|
.arg( info->fields[n]->name ) );
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
2011-03-06 07:03:46 +01:00
|
|
|
// Generate element names (only if field has more than one element)
|
|
|
|
if (info->fields[n]->numElements > 1 && !info->fields[n]->defaultElementNames) {
|
|
|
|
enums.append(QString(" /* Array element names for field %1 */\n").arg(info->fields[n]->name));
|
|
|
|
enums.append(" typedef enum { ");
|
|
|
|
// Go through the element names
|
2011-01-13 03:26:00 +01:00
|
|
|
QStringList elemNames = info->fields[n]->elementNames;
|
|
|
|
for (int m = 0; m < elemNames.length(); ++m) {
|
2011-03-06 07:03:46 +01:00
|
|
|
QString s = (m != (elemNames.length()-1)) ? "%1_%2=%3, " : "%1_%2=%3";
|
|
|
|
enums.append( s.arg( info->fields[n]->name.toUpper() )
|
|
|
|
.arg( elemNames[m].toUpper() )
|
|
|
|
.arg(m) );
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
}
|
2011-03-06 07:03:46 +01:00
|
|
|
enums.append( QString(" } %1Elem;\n")
|
|
|
|
.arg( info->fields[n]->name ) );
|
|
|
|
}
|
|
|
|
// Generate array information
|
|
|
|
if (info->fields[n]->numElements > 1) {
|
|
|
|
enums.append(QString(" /* Number of elements for field %1 */\n").arg(info->fields[n]->name));
|
|
|
|
enums.append( QString(" static const quint32 %1_NUMELEM = %2;\n")
|
|
|
|
.arg( info->fields[n]->name.toUpper() )
|
|
|
|
.arg( info->fields[n]->numElements ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outInclude.replace(QString("$(DATAFIELDINFO)"), enums);
|
|
|
|
|
|
|
|
// Replace the $(INITFIELDS) tag
|
|
|
|
QString initfields;
|
|
|
|
for (int n = 0; n < info->fields.length(); ++n)
|
|
|
|
{
|
|
|
|
if (!info->fields[n]->defaultValues.isEmpty() )
|
|
|
|
{
|
|
|
|
// For non-array fields
|
|
|
|
if ( info->fields[n]->numElements == 1)
|
|
|
|
{
|
|
|
|
if ( info->fields[n]->type == FIELDTYPE_ENUM )
|
|
|
|
{
|
2011-03-06 20:33:56 +01:00
|
|
|
initfields.append( QString("\t\tgetField(\"%1\").setValue(\"%2\");\n")
|
2011-03-06 07:03:46 +01:00
|
|
|
.arg( info->fields[n]->name )
|
2011-03-06 20:33:56 +01:00
|
|
|
.arg( info->fields[n]->defaultValues[0] ) );
|
2011-03-06 07:03:46 +01:00
|
|
|
}
|
|
|
|
else if ( info->fields[n]->type == FIELDTYPE_FLOAT32 )
|
|
|
|
{
|
|
|
|
initfields.append( QString("\t\tgetField(\"%1\").setValue(%2);\n")
|
|
|
|
.arg( info->fields[n]->name )
|
|
|
|
.arg( info->fields[n]->defaultValues[0].toFloat() ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initfields.append( QString("\t\tgetField(\"%1\").setValue(%2);\n")
|
|
|
|
.arg( info->fields[n]->name )
|
|
|
|
.arg( info->fields[n]->defaultValues[0].toInt() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Initialize all fields in the array
|
|
|
|
for (int idx = 0; idx < info->fields[n]->numElements; ++idx)
|
|
|
|
{
|
|
|
|
if ( info->fields[n]->type == FIELDTYPE_ENUM ) {
|
2011-03-06 20:33:56 +01:00
|
|
|
initfields.append( QString("\t\tgetField(\"%1\").setValue(\"%3\",%2);\n")
|
2011-03-06 07:03:46 +01:00
|
|
|
.arg( info->fields[n]->name )
|
|
|
|
.arg( idx )
|
2011-03-06 20:33:56 +01:00
|
|
|
.arg( info->fields[n]->defaultValues[idx] ) );
|
2011-03-06 07:03:46 +01:00
|
|
|
}
|
|
|
|
else if ( info->fields[n]->type == FIELDTYPE_FLOAT32 ) {
|
|
|
|
initfields.append( QString("\t\tgetField(\"%1\").setValue(%3,%2);\n")
|
|
|
|
.arg( info->fields[n]->name )
|
|
|
|
.arg( idx )
|
|
|
|
.arg( info->fields[n]->defaultValues[idx].toFloat() ) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
initfields.append( QString("\t\tgetField(\"%1\").setValue(%3,%2);\n")
|
|
|
|
.arg( info->fields[n]->name )
|
|
|
|
.arg( idx )
|
|
|
|
.arg( info->fields[n]->defaultValues[idx].toInt() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-06 07:03:46 +01:00
|
|
|
outCode.replace(QString("$(INITFIELDS)"), initfields);
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2011-03-06 07:03:46 +01:00
|
|
|
// Write the java code
|
2012-02-05 01:30:04 +01:00
|
|
|
bool res = writeFileIfDiffrent( javaOutputPath.absolutePath() + "/" + info->name + ".java", outCode );
|
2011-01-13 03:26:00 +01:00
|
|
|
if (!res) {
|
2011-03-06 07:03:46 +01:00
|
|
|
cout << "Error: Could not write gcs output files" << endl;
|
2011-01-13 03:26:00 +01:00
|
|
|
return false;
|
2011-03-06 07:03:46 +01:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|