mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-18 03:52:11 +01:00
Java autogenerated code to be more compatible with gcs code
This commit is contained in:
parent
a2e93f1971
commit
a9066494c7
@ -28,61 +28,110 @@
|
|||||||
|
|
||||||
package org.openpilot.uavtalk.uavobjects;
|
package org.openpilot.uavtalk.uavobjects;
|
||||||
|
|
||||||
import org.openpilot.uavtalk.UAVObject;
|
import java.nio.ByteBuffer;
|
||||||
import org.openpilot.uavtalk.UAVDataObject;
|
import java.util.ArrayList;
|
||||||
import org.openpilot.uavtalk.UAVMetaObject;
|
import java.util.List;
|
||||||
import org.openpilot.uavtalk.UAVObjectField;
|
import java.util.ListIterator;
|
||||||
|
|
||||||
|
import org.openpilot.uavtalk.UAVObjectManager;
|
||||||
|
import org.openpilot.uavtalk.UAVObject;
|
||||||
|
import org.openpilot.uavtalk.UAVDataObject;
|
||||||
|
import org.openpilot.uavtalk.UAVMetaObject;
|
||||||
|
import org.openpilot.uavtalk.UAVObjectField;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
$(DESCRIPTION)
|
$(DESCRIPTION)
|
||||||
|
|
||||||
generated from $(XMLFILE)
|
generated from $(XMLFILE)
|
||||||
**/
|
**/
|
||||||
public class $(NAME) extends UAVDataObject {
|
public class $(NAME) extends UAVDataObject {
|
||||||
|
|
||||||
|
public $(NAME)() throws Exception {
|
||||||
|
super(OBJID, ISSINGLEINST, ISSETTINGS, NAME);
|
||||||
|
|
||||||
|
List<UAVObjectField> fields = new ArrayList<UAVObjectField>();
|
||||||
|
|
||||||
$(FIELDSINIT)
|
$(FIELDSINIT)
|
||||||
|
|
||||||
public $(NAME) (int objID, Boolean isSingleInst, Boolean isSet, String name) {
|
// Compute the number of bytes for this object
|
||||||
super(OBJID, ISSINGLEINST, ISSETTINGS, NAME);
|
int numBytes = 0;
|
||||||
}
|
ListIterator<UAVObjectField> li = fields.listIterator();
|
||||||
|
while(li.hasNext()) {
|
||||||
|
numBytes += li.next().getNumBytesElement();
|
||||||
|
}
|
||||||
|
NUMBYTES = numBytes;
|
||||||
|
|
||||||
|
|
||||||
public void setGeneratedMetaData() {
|
// Initialize object
|
||||||
|
initializeFields(fields, ByteBuffer.allocate(NUMBYTES), NUMBYTES);
|
||||||
|
// Set the default field values
|
||||||
|
setDefaultFieldValues();
|
||||||
|
// Set the object description
|
||||||
|
setDescription(DESCRIPTION);
|
||||||
|
}
|
||||||
|
|
||||||
getMetaData().gcsAccess = UAVObject.AccessMode.$(GCSACCESS);
|
/**
|
||||||
getMetaData().gcsTelemetryAcked = UAVObject.Acked.$(GCSTELEM_ACKEDTF);
|
* Create a Metadata object filled with default values for this object
|
||||||
getMetaData().gcsTelemetryUpdateMode = UAVObject.UpdateMode.$(GCSTELEM_UPDATEMODE);
|
* @return Metadata object with default values
|
||||||
getMetaData().gcsTelemetryUpdatePeriod = $(GCSTELEM_UPDATEPERIOD);
|
*/
|
||||||
|
public Metadata getDefaultMetadata() {
|
||||||
|
UAVObject.Metadata metadata = new UAVObject.Metadata();
|
||||||
|
metadata.gcsAccess = UAVObject.AccessMode.$(GCSACCESS);
|
||||||
|
metadata.gcsTelemetryAcked = UAVObject.Acked.$(GCSTELEM_ACKEDTF);
|
||||||
|
metadata.gcsTelemetryUpdateMode = UAVObject.UpdateMode.$(GCSTELEM_UPDATEMODE);
|
||||||
|
metadata.gcsTelemetryUpdatePeriod = $(GCSTELEM_UPDATEPERIOD);
|
||||||
|
|
||||||
getMetaData().flightAccess = UAVObject.AccessMode.$(FLIGHTACCESS);
|
metadata.flightAccess = UAVObject.AccessMode.$(FLIGHTACCESS);
|
||||||
getMetaData().flightTelemetryAcked = UAVObject.Acked.$(FLIGHTTELEM_ACKEDTF);
|
metadata.flightTelemetryAcked = UAVObject.Acked.$(FLIGHTTELEM_ACKEDTF);
|
||||||
getMetaData().flightTelemetryUpdateMode = UAVObject.UpdateMode.$(FLIGHTTELEM_UPDATEMODE);
|
metadata.flightTelemetryUpdateMode = UAVObject.UpdateMode.$(FLIGHTTELEM_UPDATEMODE);
|
||||||
getMetaData().flightTelemetryUpdatePeriod = $(FLIGHTTELEM_UPDATEPERIOD);
|
metadata.flightTelemetryUpdatePeriod = $(FLIGHTTELEM_UPDATEPERIOD);
|
||||||
|
|
||||||
getMetaData().loggingUpdateMode = UAVObject.UpdateMode.$(LOGGING_UPDATEMODE);
|
metadata.loggingUpdateMode = UAVObject.UpdateMode.$(LOGGING_UPDATEMODE);
|
||||||
getMetaData().loggingUpdatePeriod = $(LOGGING_UPDATEPERIOD);
|
metadata.loggingUpdatePeriod = $(LOGGING_UPDATEPERIOD);
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* Initialize object fields with the default values.
|
||||||
public int getObjID() {
|
* If a default value is not specified the object fields
|
||||||
return $(OBJIDHEX);
|
* will be initialized to zero.
|
||||||
}
|
*/
|
||||||
|
public void setDefaultFieldValues()
|
||||||
public String getObjName() {
|
{
|
||||||
return "$(NAME)";
|
$(INITFIELDS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a clone of this object, a new instance ID must be specified.
|
||||||
|
* Do not use this function directly to create new instances, the
|
||||||
|
* UAVObjectManager should be used instead.
|
||||||
|
*/
|
||||||
|
public UAVDataObject clone(int instID) {
|
||||||
|
// TODO: Need to get specific instance to clone
|
||||||
|
try {
|
||||||
|
$(NAME) obj = new $(NAME)();
|
||||||
|
obj.initialize(instID, this.getMetaObject());
|
||||||
|
return obj;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static function to retrieve an instance of the object.
|
||||||
|
*/
|
||||||
|
public $(NAME) GetInstance(UAVObjectManager objMngr, int instID)
|
||||||
|
{
|
||||||
|
return ($(NAME))(objMngr.getObject($(NAME).OBJID, instID));
|
||||||
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return "$(DESCRIPTION)";
|
|
||||||
}
|
|
||||||
protected:
|
|
||||||
// Constants
|
// Constants
|
||||||
static final int OBJID = $(OBJIDHEX);
|
protected static final int OBJID = $(OBJIDHEX);
|
||||||
static final String NAME;
|
protected static final String NAME = "$(NAME)";
|
||||||
static final String DESCRIPTION;
|
protected static String DESCRIPTION = "$(DESCRIPTION)";
|
||||||
static final boolean ISSINGLEINST = $(ISSINGLEINST);
|
protected static final boolean ISSINGLEINST = $(ISSINGLEINST) == 1;
|
||||||
static final boolean ISSETTINGS = $(ISSETTINGS);
|
protected static final boolean ISSETTINGS = $(ISSETTINGS) == 1;
|
||||||
static final int NUMBYTES = sizeof(DataFields);
|
protected static int NUMBYTES = 0;
|
||||||
|
|
||||||
|
|
||||||
$(GETTERSETTER)
|
|
||||||
}
|
}
|
@ -24,313 +24,245 @@
|
|||||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
#include "uavobjectgeneratorjava.h"
|
#include "uavobjectgeneratorjava.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
bool UAVObjectGeneratorJava::generate(UAVObjectParser* parser,QString templatepath,QString outputpath) {
|
bool UAVObjectGeneratorJava::generate(UAVObjectParser* parser,QString templatepath,QString outputpath) {
|
||||||
QDir javaTemplatePath = QDir( templatepath + QString(JAVA_TEMPLATE_DIR));
|
fieldTypeStrCPP << "qint8" << "qint16" << "qint32" <<
|
||||||
|
"quint8" << "quint16" << "quint32" << "float" << "quint8";
|
||||||
|
|
||||||
javaCodePath = QDir ( outputpath + QString(JAVA_GENERATED_DIR));
|
fieldTypeStrCPPClass << "INT8" << "INT16" << "INT32"
|
||||||
|
<< "UINT8" << "UINT16" << "UINT32" << "FLOAT32" << "ENUM";
|
||||||
|
|
||||||
javaCodeTemplate = readFile( javaTemplatePath.absoluteFilePath("uavobjecttemplate.java") );
|
javaCodePath = QDir( templatepath + QString(JAVA_TEMPLATE_DIR));
|
||||||
QString javaInitCodeTemplate = readFile( javaTemplatePath.absoluteFilePath("uavobjectsinittemplate.java") );
|
javaOutputPath = QDir( outputpath + QString("java") );
|
||||||
|
javaOutputPath.mkpath(javaOutputPath.absolutePath());
|
||||||
|
|
||||||
if (javaCodeTemplate.isEmpty() || javaInitCodeTemplate.isEmpty()) {
|
javaCodeTemplate = readFile( javaCodePath.absoluteFilePath("uavobjecttemplate.java") );
|
||||||
std::cerr << "Problem reading java templates" << endl;
|
QString javaInitTemplate = readFile( javaCodePath.absoluteFilePath("uavobjectsinittemplate.java") );
|
||||||
|
|
||||||
|
if (javaCodeTemplate.isEmpty() || javaInitTemplate.isEmpty()) {
|
||||||
|
std::cerr << "Problem reading java code templates" << endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString javaObjInit,javaObjGetter;
|
QString objInc;
|
||||||
javaCodePath.mkpath(javaCodePath.absolutePath() + "/uavobjects");
|
QString javaObjInit;
|
||||||
|
|
||||||
for (int objidx = 0; objidx < parser->getNumObjects(); ++objidx) {
|
for (int objidx = 0; objidx < parser->getNumObjects(); ++objidx) {
|
||||||
ObjectInfo* info=parser->getObjectByIndex(objidx);
|
ObjectInfo* info=parser->getObjectByIndex(objidx);
|
||||||
process_object(info);
|
process_object(info);
|
||||||
|
|
||||||
if (!javaObjInit.isNull())
|
javaObjInit.append(" objMngr->registerObject( new " + info->name + "() );\n");
|
||||||
javaObjInit.append(",\n");
|
objInc.append("#include \"" + info->namelc + ".h\"\n");
|
||||||
|
|
||||||
javaObjInit.append(" new " + info->name + "()");
|
|
||||||
javaObjGetter.append(QString(" public static %1 get%1() { return (%1)uavobjects[%2];}\n").arg( info->name).arg(objidx));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
javaInitCodeTemplate.replace("$(OBJINIT)",javaObjInit);
|
// Write the gcs object inialization files
|
||||||
javaInitCodeTemplate.replace("$(OBJGETTER)",javaObjGetter);
|
javaInitTemplate.replace( QString("$(OBJINC)"), objInc);
|
||||||
|
javaInitTemplate.replace( QString("$(OBJINIT)"), javaObjInit);
|
||||||
replaceCommonTags(javaInitCodeTemplate);
|
bool res = writeFileIfDiffrent( javaOutputPath.absolutePath() + "/uavobjectsinit.java", javaInitTemplate );
|
||||||
|
|
||||||
// Write the java object inialization files
|
|
||||||
bool res = writeFileIfDiffrent(javaCodePath.absolutePath() + "/UAVObjects.java", javaInitCodeTemplate );
|
|
||||||
if (!res) {
|
if (!res) {
|
||||||
cerr << "Error: Could not write java init output files" << endl;
|
cout << "Error: Could not write output files" << endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true; // if we come here everything should be fine
|
return true; // if we come here everything should be fine
|
||||||
}
|
}
|
||||||
|
|
||||||
QString UAVObjectGeneratorJava::getFieldTypeStr(int type,bool as_obj) {
|
|
||||||
|
|
||||||
switch(type) {
|
|
||||||
case FIELDTYPE_INT8:
|
|
||||||
return (QString(as_obj?"Byte":"byte"));
|
|
||||||
case FIELDTYPE_INT16:
|
|
||||||
case FIELDTYPE_INT32:
|
|
||||||
case FIELDTYPE_UINT8:
|
|
||||||
case FIELDTYPE_UINT16:
|
|
||||||
return (QString(as_obj?"Integer":"int"));
|
|
||||||
case FIELDTYPE_UINT32:
|
|
||||||
return (QString(as_obj?"Long":"long"));
|
|
||||||
case FIELDTYPE_FLOAT32:
|
|
||||||
return (QString(as_obj?"Float":"float"));
|
|
||||||
case FIELDTYPE_ENUM:
|
|
||||||
return (QString(as_obj?"Byte":"byte"));
|
|
||||||
default:
|
|
||||||
return QString("error: unknown type");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* format a value from FieldInfo with a given index (idx) to a java code snippet
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
QString UAVObjectGeneratorJava::formatJavaValue(FieldInfo* info,int idx) {
|
|
||||||
switch(info->type) {
|
|
||||||
case FIELDTYPE_ENUM:
|
|
||||||
return (info->name.toUpper() + QString("_") + info->defaultValues[idx].toUpper().replace(QRegExp(ENUM_SPECIAL_CHARS), ""));
|
|
||||||
case FIELDTYPE_FLOAT32:
|
|
||||||
return QString("%1f").arg( info->defaultValues[idx].toFloat() );
|
|
||||||
default:
|
|
||||||
return QString("%1").arg( info->defaultValues[idx].toInt() );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QString UAVObjectGeneratorJava::QStringList2JavaArray(QStringList strl) {
|
|
||||||
QString csv=QString();
|
|
||||||
for (int i = 0; i < strl.length(); i++){
|
|
||||||
if (i!=0)
|
|
||||||
csv.append(QString(","));
|
|
||||||
csv.append(QString("\"%1\"").arg(strl[i]));
|
|
||||||
}
|
|
||||||
return QString("new String[] { %1 }").arg(csv);
|
|
||||||
}
|
|
||||||
|
|
||||||
QString UAVObjectGeneratorJava::serializeJavaValue(int type,QString name)
|
|
||||||
{
|
|
||||||
QString res;
|
|
||||||
switch(type) {
|
|
||||||
case FIELDTYPE_ENUM: // OK
|
|
||||||
|
|
||||||
case FIELDTYPE_INT8:
|
|
||||||
res=name;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FIELDTYPE_UINT8:
|
|
||||||
res=QString("(byte)(%1&0xFF)").arg(name);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FIELDTYPE_INT16:
|
|
||||||
case FIELDTYPE_UINT16:
|
|
||||||
res=QString("(byte)(("+name+">>0)&0xFF) , (byte)(("+name+">>8)&0xFF)");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FIELDTYPE_UINT32:
|
|
||||||
case FIELDTYPE_INT32:
|
|
||||||
res=QString("(byte)(("+name+">>0)&0xFF) , (byte)(("+name+">>8)&0xFF) , (byte)(("+name+">>16)&0xFF) , (byte)(("+name+">>24)&0xFF)");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FIELDTYPE_FLOAT32:
|
|
||||||
res=QString("(byte)((Float.floatToIntBits("+name+")>>0)&0xFF) , (byte)((Float.floatToIntBits("+name+")>>8)&0xFF) , (byte)((Float.floatToIntBits("+name+")>>16)&0xFF) , (byte)((Float.floatToIntBits("+name+")>>24)&0xFF) "); // OK
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
res=QString("unresolved type");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* this function is used to build the deserializing code
|
|
||||||
*/
|
|
||||||
QString UAVObjectGeneratorJava::deSerializeJavaValue(int type,QString name)
|
|
||||||
{
|
|
||||||
switch(type) {
|
|
||||||
case FIELDTYPE_ENUM: // OK
|
|
||||||
case FIELDTYPE_INT8:
|
|
||||||
return (name.append("=arr[pos++];\n"));
|
|
||||||
|
|
||||||
case FIELDTYPE_UINT8:
|
|
||||||
return (name.append("=arr[pos++]&0xFF;\n")); /*test */
|
|
||||||
|
|
||||||
case FIELDTYPE_INT16:
|
|
||||||
case FIELDTYPE_UINT16:
|
|
||||||
return (name.append("=((arr[pos++]&0xff)<<0) | ((arr[pos++]&0xff)<<8) ;\n")); /* test */
|
|
||||||
|
|
||||||
case FIELDTYPE_UINT32:
|
|
||||||
case FIELDTYPE_INT32:
|
|
||||||
return (name.append("=((arr[pos++]&0xff)<<0) | ((arr[pos++]&0xff)<<8) | ((arr[pos++]&0xff)<<16) | ((arr[pos++]&0xff)<<24) ;\n")); /*test */
|
|
||||||
|
|
||||||
case FIELDTYPE_FLOAT32:
|
|
||||||
return (name.append("=Float.intBitsToFloat(((arr[pos++]&0xff)<<0) | ((arr[pos++]&0xff)<<8) | ((arr[pos++]&0xff)<<16) | ((arr[pos++]&0xff)<<24) ); ")); // OK
|
|
||||||
|
|
||||||
default:
|
|
||||||
cout << "Warning: unresolved type " << type << " for " << name.toStdString() << endl;
|
|
||||||
return(QString("unresolved type")); // will throw an arr when compiling then
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate the java object files
|
* Generate the java object files
|
||||||
*/
|
*/
|
||||||
bool UAVObjectGeneratorJava::process_object(ObjectInfo* info)
|
bool UAVObjectGeneratorJava::process_object(ObjectInfo* info)
|
||||||
{
|
{
|
||||||
if (info == NULL) return false;
|
if (info == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
// Prepare output strings
|
// Prepare output strings
|
||||||
|
QString outInclude = javaIncludeTemplate;
|
||||||
QString outCode = javaCodeTemplate;
|
QString outCode = javaCodeTemplate;
|
||||||
|
|
||||||
// Replace common tags
|
// Replace common tags
|
||||||
|
replaceCommonTags(outInclude, info);
|
||||||
replaceCommonTags(outCode, info);
|
replaceCommonTags(outCode, info);
|
||||||
|
|
||||||
QString type,fieldsinit,serialize_code,serialize_code_inner,deserialize_code,gettersetter;
|
// Replace the $(DATAFIELDS) tag
|
||||||
|
QString type;
|
||||||
QString fielddesc=QString(" public final UAVObjectFieldDescription[] getFieldDescriptions() { return new UAVObjectFieldDescription[] {");
|
QString fields;
|
||||||
QString fieldgetter=QString(" public Object getField(int fieldid,int arr_pos) { switch(fieldid) {\n");
|
for (int n = 0; n < info->fields.length(); ++n)
|
||||||
QString fieldsetter=QString(" public void setField(int fieldid,int arr_pos,Object obj) { switch(fieldid) {\n");
|
{
|
||||||
|
|
||||||
for (int n = 0; n < info->fields.length(); ++n) {
|
|
||||||
FieldInfo* act_field_info=info->fields[n];
|
|
||||||
|
|
||||||
bool is_array = info->fields[n]->numElements > 1;
|
|
||||||
type = getFieldTypeStr(act_field_info->type,false); // Determine type
|
|
||||||
|
|
||||||
for (int enum_n = 0; enum_n < act_field_info->options.length(); ++enum_n)
|
|
||||||
fieldsinit.append(QString(" public final static byte %1 = %2;\n").arg(act_field_info->name.toUpper() + QString("_") + act_field_info->options[enum_n].toUpper().replace(QRegExp(ENUM_SPECIAL_CHARS), "")).arg(enum_n) );
|
|
||||||
|
|
||||||
fieldsinit.append(QString(" private ") + type);
|
|
||||||
|
|
||||||
if ( is_array )
|
|
||||||
fieldsinit.append(QString("[]"));
|
|
||||||
|
|
||||||
fieldsinit.append(QString(" %1").arg(act_field_info->name));
|
|
||||||
|
|
||||||
if (!info->fields[n]->defaultValues.isEmpty()) { // if we have default vals
|
|
||||||
fieldsinit.append(QString("="));
|
|
||||||
if ( is_array ) {
|
|
||||||
fieldsinit.append(QString("{"));
|
|
||||||
for (int val_n = 0; val_n < act_field_info->defaultValues.length(); ++val_n)
|
|
||||||
fieldsinit.append( ((val_n>0)?QString(","):QString() ) + formatJavaValue(act_field_info,val_n) );
|
|
||||||
fieldsinit.append(QString("}"));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
fieldsinit.append(formatJavaValue(act_field_info,0));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if ( is_array ) // when it is an array
|
|
||||||
fieldsinit.append(QString("= new ") + type + QString("[%1]").arg(info->fields[n]->numElements));
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldsinit.append(QString(";\n"));
|
|
||||||
|
|
||||||
if (n!=0)
|
|
||||||
serialize_code_inner.append(QString(","));
|
|
||||||
|
|
||||||
if ( is_array ) {
|
|
||||||
for (int el=0;el<info->fields[n]->numElements;el++) {
|
|
||||||
if (el!=0)
|
|
||||||
serialize_code_inner.append(QString(","));
|
|
||||||
|
|
||||||
serialize_code_inner.append(serializeJavaValue(info->fields[n]->type,info->fields[n]->name+ QString("[%1]").arg(el)));
|
|
||||||
deserialize_code.append(deSerializeJavaValue(info->fields[n]->type,info->fields[n]->name+ QString("[%1]").arg(el)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else { // no array
|
|
||||||
serialize_code_inner.append(serializeJavaValue(info->fields[n]->type,info->fields[n]->name));
|
|
||||||
deserialize_code.append(deSerializeJavaValue(info->fields[n]->type,info->fields[n]->name));
|
|
||||||
}
|
|
||||||
|
|
||||||
serialize_code_inner.append(QString("//%1\n" ).arg(info->fields[n]->name));
|
|
||||||
|
|
||||||
// Determine type
|
// Determine type
|
||||||
type = getFieldTypeStr(act_field_info->type,false);
|
type = fieldTypeStrCPP[info->fields[n]->type];
|
||||||
|
// Append field
|
||||||
QString typespec=type;
|
if ( info->fields[n]->numElements > 1 )
|
||||||
|
{
|
||||||
if ( is_array ) {
|
fields.append( QString(" %1 %2[%3];\n").arg(type).arg(info->fields[n]->name)
|
||||||
typespec.append(QString("[]"));
|
.arg(info->fields[n]->numElements) );
|
||||||
fieldgetter.append(QString("case %1: return (Object)%2[arr_pos];\n").arg(n).arg(act_field_info->name));
|
|
||||||
fieldsetter.append(QString("case %1: %2[arr_pos]=(%3)obj;break;\n").arg(n).arg(act_field_info->name).arg( getFieldTypeStr(act_field_info->type,true)));
|
|
||||||
}
|
}
|
||||||
else { // no array
|
else
|
||||||
fieldgetter.append(QString("case %1: return (Object)%2;\n").arg(n).arg(act_field_info->name));
|
{
|
||||||
fieldsetter.append(QString("case %1: %2=(%3)obj;break;\n").arg(n).arg(act_field_info->name).arg(getFieldTypeStr(act_field_info->type,true)));
|
fields.append( QString(" %1 %2;\n").arg(type).arg(info->fields[n]->name) );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
outInclude.replace(QString("$(DATAFIELDS)"), fields);
|
||||||
|
|
||||||
|
// Replace the $(FIELDSINIT) tag
|
||||||
|
QString finit;
|
||||||
|
for (int n = 0; n < info->fields.length(); ++n)
|
||||||
|
{
|
||||||
|
finit.append("\n");
|
||||||
|
|
||||||
if (!act_field_info->units.isEmpty()) // when we have unit info
|
// Setup element names
|
||||||
gettersetter.append(QString("\n /**\n * unit: %1\n */\n").arg(act_field_info->units));
|
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]) );
|
||||||
|
|
||||||
gettersetter.append(QString(" public void set%1( %2 _%1 ) { %1=_%1; }\n").arg(act_field_info->name).arg(typespec));
|
// Only for enum types
|
||||||
|
if (info->fields[n]->type == FIELDTYPE_ENUM) {
|
||||||
if (n!=0)
|
QString varOptionName = info->fields[n]->name + "EnumOptions";
|
||||||
fielddesc.append(QString("\t,"));
|
finit.append( QString("\t\tList<String> %1 = new ArrayList<String>();\n").arg(varOptionName) );
|
||||||
|
QStringList options = info->fields[n]->options;
|
||||||
fielddesc.append(QString("new UAVObjectFieldDescription(\"%1\",this.getObjID(),(byte)%2,(byte)%3,\"%4\",%5,%6)\n").arg(act_field_info->name).arg(n).arg(act_field_info->type).arg(act_field_info->units).arg(QStringList2JavaArray( act_field_info->options)).arg(QStringList2JavaArray( act_field_info->elementNames)) );
|
for (int m = 0; m < options.length(); ++m)
|
||||||
|
{
|
||||||
if (!act_field_info->units.isEmpty())
|
finit.append( QString("\t\t%1.add(\"%2\");\n")
|
||||||
gettersetter.append(QString("\n /**\n * unit: %1\n */\n").arg(act_field_info->units));
|
.arg(varOptionName)
|
||||||
|
.arg(options[m]) );
|
||||||
gettersetter.append(QString(" public ") + typespec);
|
|
||||||
|
|
||||||
gettersetter.append(QString(" get%1() { return %1; }\n").arg(act_field_info->name));
|
|
||||||
|
|
||||||
if ( act_field_info->options.length()!=0) {
|
|
||||||
QString enumOptionsGetter=QString(" public final static String[] get%1EnumOptions() { return new String[] {").arg(act_field_info->name);
|
|
||||||
|
|
||||||
// gettersetter.append(QString(" get%1s() { return %1; }\n").arg(act_field_info->name));
|
|
||||||
for (int enum_n = 0; enum_n < act_field_info->options.length(); ++enum_n) {
|
|
||||||
if (enum_n!=0)
|
|
||||||
enumOptionsGetter.append(QString(","));
|
|
||||||
enumOptionsGetter.append(QString("\"")+act_field_info->options[enum_n] +QString("\""));
|
|
||||||
}
|
}
|
||||||
|
finit.append( QString("\t\tfields.add( new UAVObjectField(\"%1\", \"%2\", UAVObjectField.FieldType.ENUM, %3, %4) );\n")
|
||||||
gettersetter.append(enumOptionsGetter+QString("};}\n"));
|
.arg(info->fields[n]->name)
|
||||||
|
.arg(info->fields[n]->units)
|
||||||
|
.arg(varElemName)
|
||||||
|
.arg(varOptionName) );
|
||||||
}
|
}
|
||||||
|
// 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) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outCode.replace(QString("$(FIELDSINIT)"), finit);
|
||||||
|
|
||||||
if ( is_array ) { // when it is an array create getter for the element names
|
// Replace the $(DATAFIELDINFO) tag
|
||||||
QString elementListGetter=QString(" public final static String[] get%1ElementNames() { return new String[] {").arg(act_field_info->name);
|
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) );
|
||||||
|
|
||||||
|
}
|
||||||
|
enums.append( QString(" } %1Options;\n")
|
||||||
|
.arg( info->fields[n]->name ) );
|
||||||
|
}
|
||||||
|
// 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
|
||||||
QStringList elemNames = info->fields[n]->elementNames;
|
QStringList elemNames = info->fields[n]->elementNames;
|
||||||
for (int m = 0; m < elemNames.length(); ++m) {
|
for (int m = 0; m < elemNames.length(); ++m) {
|
||||||
gettersetter.append(QString(" public ") + type);
|
QString s = (m != (elemNames.length()-1)) ? "%1_%2=%3, " : "%1_%2=%3";
|
||||||
gettersetter.append(QString(" get%1%2() { return %1[%3]; }\n").arg(act_field_info->name).arg(elemNames[m]).arg(m));
|
enums.append( s.arg( info->fields[n]->name.toUpper() )
|
||||||
if (m!=0)
|
.arg( elemNames[m].toUpper() )
|
||||||
elementListGetter.append(QString(","));
|
.arg(m) );
|
||||||
|
|
||||||
elementListGetter.append(QString("\"") + elemNames[m]+QString("\""));
|
|
||||||
}
|
}
|
||||||
gettersetter.append(elementListGetter+QString("};}\n"));
|
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 )
|
||||||
|
{
|
||||||
|
initfields.append( QString("\t\tgetField(\"%1\").setValue(%2);\n")
|
||||||
|
.arg( info->fields[n]->name )
|
||||||
|
.arg( info->fields[n]->options.indexOf( info->fields[n]->defaultValues[0] ) ) );
|
||||||
|
}
|
||||||
|
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 ) {
|
||||||
|
initfields.append( QString("\t\tgetField(\"%1\").setValue(%3,%2);\n")
|
||||||
|
.arg( info->fields[n]->name )
|
||||||
|
.arg( idx )
|
||||||
|
.arg( info->fields[n]->options.indexOf( info->fields[n]->defaultValues[idx] ) ) );
|
||||||
|
}
|
||||||
|
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() ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize_code.append(QString(" public byte[] serialize() { return new byte[]{%1 } ;}; \n ").arg(serialize_code_inner));
|
outCode.replace(QString("$(INITFIELDS)"), initfields);
|
||||||
|
|
||||||
serialize_code.append(QString(" public void deserialize(byte[] arr,int offset) {\nsuper.deserialize(arr,offset);\n int pos=offset;\n%1 };\n").arg(deserialize_code));
|
// Write the java code
|
||||||
|
bool res = writeFileIfDiffrent( javaOutputPath.absolutePath() + "/" + info->namelc + ".java", outCode );
|
||||||
outCode.replace(QString("$(FIELDSINIT)"), fieldsinit);
|
|
||||||
|
|
||||||
fielddesc.append(QString("};}"));
|
|
||||||
fieldgetter.append(QString("};\n return null;\n}\n"));
|
|
||||||
fieldsetter.append(QString("};\n}\n"));
|
|
||||||
outCode.replace(QString("$(GETTERSETTER)"), gettersetter + serialize_code+ fielddesc + fieldgetter + fieldsetter);
|
|
||||||
|
|
||||||
bool res = writeFileIfDiffrent(javaCodePath.absolutePath() + "/uavobjects/" + info->name + ".java", outCode );
|
|
||||||
if (!res) {
|
if (!res) {
|
||||||
cerr << "Error: Could not write java init output file " << info->name.toStdString()<< endl;
|
cout << "Error: Could not write gcs output files" << endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
#define UAVOBJECTGENERATORJAVA_H
|
#define UAVOBJECTGENERATORJAVA_H
|
||||||
|
|
||||||
#define JAVA_TEMPLATE_DIR "ground/openpilotgcs/src/libs/juavobjects/templates/"
|
#define JAVA_TEMPLATE_DIR "ground/openpilotgcs/src/libs/juavobjects/templates/"
|
||||||
#define JAVA_GENERATED_DIR "java/src/org/openpilot/uavtalk"
|
#define JAVA_CODE_DIR "java/src/org/openpilot/uavtalk"
|
||||||
|
|
||||||
#include "../generator_common.h"
|
#include "../generator_common.h"
|
||||||
|
|
||||||
@ -38,14 +38,12 @@ public:
|
|||||||
bool generate(UAVObjectParser* gen,QString templatepath,QString outputpath);
|
bool generate(UAVObjectParser* gen,QString templatepath,QString outputpath);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString javaCodeTemplate;
|
|
||||||
QDir javaCodePath;
|
|
||||||
bool process_object(ObjectInfo* info);
|
bool process_object(ObjectInfo* info);
|
||||||
QString formatJavaValue(FieldInfo* info,int idx);
|
|
||||||
QString QStringList2JavaArray(QStringList strl);
|
QString javaCodeTemplate, javaIncludeTemplate;
|
||||||
QString serializeJavaValue(int type,QString name);
|
QStringList fieldTypeStrCPP,fieldTypeStrCPPClass;
|
||||||
QString deSerializeJavaValue(int type,QString name);
|
QDir javaCodePath;
|
||||||
QString getFieldTypeStr(int type,bool as_obj);
|
QDir javaOutputPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user