1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-03-02 19:29:15 +01:00

More work on the java UAVObject implementation

This commit is contained in:
James Cotton 2011-03-06 00:03:20 -06:00
parent 1c2eb1bf22
commit a2e93f1971
5 changed files with 497 additions and 382 deletions

View File

@ -9,39 +9,78 @@ public abstract class UAVDataObject extends UAVObject {
* @param isSet * @param isSet
* @param name * @param name
*/ */
public UAVDataObject(int objID, Boolean isSingleInst, Boolean isSet, String name) { public UAVDataObject(int objID, Boolean isSingleInst, Boolean isSet, String name) throws Exception {
super(objID, isSingleInst, name); super(objID, isSingleInst, name);
mobj = null;
this.isSet = isSet;
} }
/**
public abstract void initialize(int instID, UAVMetaObject mobj); * Initialize instance ID and assign a metaobject
*/
public abstract void initialize(UAVMetaObject mobj); public void initialize(int instID, UAVMetaObject mobj)
{
Boolean isSettings() { //QMutexLocker locker(mutex);
return null; this.mobj = mobj;
super.initialize(instID);
}
/**
* Assign a metaobject
*/
public void initialize(UAVMetaObject mobj)
{
//QMutexLocker locker(mutex);
this.mobj = mobj;
}
/**
* Returns true if this is a data object holding module settings
*/
public boolean isSettings()
{
return isSet;
}
/**
* Set the object's metadata
*/
public void setMetadata(Metadata mdata)
{
if ( mobj != null )
{
mobj.setData(mdata);
}
}
/**
* Get the object's metadata
*/
public Metadata getMetadata()
{
if ( mobj != null)
{
return mobj.getData();
}
else
{
return getDefaultMetadata();
}
}
/**
* Get the metaobject
*/
public UAVMetaObject getMetaObject()
{
return mobj;
} }
UAVMetaObject getMetaObject() {
return null;
}
// TODO: Make abstract
public UAVDataObject clone(int instID) { public UAVDataObject clone(int instID) {
try { return (UAVDataObject) super.clone();
return (UAVDataObject) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} }
public: private UAVMetaObject mobj;
static int OBJID = $(OBJIDHEX); private boolean isSet;
static String NAME;
static String DESCRIPTION;
static boolean ISSINGLEINST = $(ISSINGLEINST);
static boolean ISSETTINGS = $(ISSETTINGS);
static int NUMBYTES = sizeof(DataFields);
} }

View File

@ -1,76 +1,116 @@
package org.openpilot.uavtalk; package org.openpilot.uavtalk;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
public class UAVMetaObject extends UAVObject { public class UAVMetaObject extends UAVObject {
public UAVMetaObject(int objID, String name, UAVDataObject parent) { public UAVMetaObject(int objID, String name, UAVDataObject parent) throws Exception {
super(objID, true, name); super(objID, true, name);
this.parent = parent; this.parent = parent;
ownMetadata.flightAccess = UAVObject.AccessMode.ACCESS_READWRITE; ownMetadata.flightAccess = UAVObject.AccessMode.ACCESS_READWRITE;
ownMetadata.gcsAccess = UAVObject.AccessMode.ACCESS_READWRITE; ownMetadata.gcsAccess = UAVObject.AccessMode.ACCESS_READWRITE;
ownMetadata.flightTelemetryAcked = UAVObject.Acked.TRUE; ownMetadata.flightTelemetryAcked = UAVObject.Acked.TRUE;
ownMetadata.flightTelemetryUpdateMode = UAVObject.UpdateMode.UPDATEMODE_ONCHANGE; ownMetadata.flightTelemetryUpdateMode = UAVObject.UpdateMode.UPDATEMODE_ONCHANGE;
ownMetadata.flightTelemetryUpdatePeriod = 0; ownMetadata.flightTelemetryUpdatePeriod = 0;
ownMetadata.gcsTelemetryAcked = UAVObject.Acked.TRUE; ownMetadata.gcsTelemetryAcked = UAVObject.Acked.TRUE;
ownMetadata.gcsTelemetryUpdateMode = UAVObject.UpdateMode.UPDATEMODE_ONCHANGE; ownMetadata.gcsTelemetryUpdateMode = UAVObject.UpdateMode.UPDATEMODE_ONCHANGE;
ownMetadata.gcsTelemetryUpdatePeriod = 0; ownMetadata.gcsTelemetryUpdatePeriod = 0;
ownMetadata.loggingUpdateMode = UAVObject.UpdateMode.UPDATEMODE_ONCHANGE; ownMetadata.loggingUpdateMode = UAVObject.UpdateMode.UPDATEMODE_ONCHANGE;
ownMetadata.loggingUpdatePeriod = 0; ownMetadata.loggingUpdatePeriod = 0;
// Setup fields
List<String> boolEnum = new ArrayList<String>();
boolEnum.add("False");
boolEnum.add("True");
List<String> updateModeEnum = new ArrayList<String>();
updateModeEnum.add("Periodic");
updateModeEnum.add("On Change");
updateModeEnum.add("Manual");
updateModeEnum.add("Never");
List<String> accessModeEnum = new ArrayList<String>();
accessModeEnum.add("Read/Write");
accessModeEnum.add("Read Only");
List<UAVObjectField> fields = new ArrayList<UAVObjectField>();
fields.add( new UAVObjectField("Flight Access Mode", "", UAVObjectField.FieldType.ENUM, 1, accessModeEnum) );
fields.add( new UAVObjectField("GCS Access Mode", "", UAVObjectField.FieldType.ENUM, 1, accessModeEnum) );
fields.add( new UAVObjectField("Flight Telemetry Acked", "", UAVObjectField.FieldType.ENUM, 1, boolEnum) );
fields.add( new UAVObjectField("Flight Telemetry Update Mode", "", UAVObjectField.FieldType.ENUM, 1, updateModeEnum) );
fields.add( new UAVObjectField("Flight Telemetry Update Period", "", UAVObjectField.FieldType.UINT32, 1, null) );
fields.add( new UAVObjectField("GCS Telemetry Acked", "", UAVObjectField.FieldType.ENUM, 1, boolEnum) );
fields.add( new UAVObjectField("GCS Telemetry Update Mode", "", UAVObjectField.FieldType.ENUM, 1, updateModeEnum) );
fields.add( new UAVObjectField("GCS Telemetry Update Period", "", UAVObjectField.FieldType.UINT32, 1, null ) );
fields.add( new UAVObjectField("Logging Update Mode", "", UAVObjectField.FieldType.ENUM, 1, updateModeEnum) );
fields.add( new UAVObjectField("Logging Update Period", "", UAVObjectField.FieldType.UINT32, 1, null ) );
// Initialize parent
super.initialize(0);
super.initializeFields(fields, ByteBuffer.allocate(10), 10);
// Setup metadata of parent
parentMetadata = parent.getDefaultMetadata();
} }
UAVObject getParentObject() { /**
* Get the parent object
*/
public UAVObject getParentObject()
{
return parent; return parent;
} }
@Override /**
public void deserialize(byte[] arr, int offset) { * Set the metadata of the metaobject, this function will
// TODO Auto-generated method stub * do nothing since metaobjects have read-only metadata.
*/
public void setMetadata(Metadata mdata)
{
return; // can not update metaobject's metadata
} }
@Override /**
public Metadata getMetadata() { * Get the metadata of the metaobject
*/
public Metadata getMetadata()
{
return ownMetadata; return ownMetadata;
} }
@Override /**
public Metadata getDefaultMetadata() { * Get the default metadata
return ownMetadata; */
public Metadata getDefaultMetadata()
{
return ownMetadata;
} }
@Override /**
public String getDescription() { * Set the metadata held by the metaobject
// TODO Auto-generated method stub */
return null; public void setData(Metadata mdata)
{
//QMutexLocker locker(mutex);
parentMetadata = mdata;
// TODO: Callbacks
// emit objectUpdatedAuto(this); // trigger object updated event
// emit objectUpdated(this);
} }
@Override /**
public int getObjID() { * Get the metadata held by the metaobject
// TODO Auto-generated method stub */
return 0; public Metadata getData()
{
// QMutexLocker locker(mutex);
return parentMetadata;
} }
@Override
public String getObjName() {
// TODO Auto-generated method stub
return null;
}
@Override
public byte[] serialize() {
// TODO Auto-generated method stub
return null;
}
public UAVMetaObject(quint32 objID, const QString& name, UAVObject* parent);
UAVObject* getParentObject();
void setMetadata(const Metadata& mdata);
void setData(const Metadata& mdata);
Metadata getData();
private UAVObject parent; private UAVObject parent;
private Metadata ownMetadata; private Metadata ownMetadata;
private Metadata parentMetadata; private Metadata parentMetadata;

View File

@ -6,241 +6,274 @@ import java.util.ListIterator;
public abstract class UAVObject { public abstract class UAVObject {
/** /**
* Object update mode * Object update mode
*/ */
public enum UpdateMode { public enum UpdateMode {
UPDATEMODE_PERIODIC, /** Automatically update object at periodic intervals */ UPDATEMODE_PERIODIC, /**
* Automatically update object at periodic
* intervals
*/
UPDATEMODE_ONCHANGE, /** Only update object when its data changes */ UPDATEMODE_ONCHANGE, /** Only update object when its data changes */
UPDATEMODE_MANUAL, /** Manually update object, by calling the updated() function */ UPDATEMODE_MANUAL, /**
UPDATEMODE_NEVER /** Object is never updated */ * Manually update object, by calling the updated()
} ; * function
*/
UPDATEMODE_NEVER
/** Object is never updated */
};
/** /**
* Access mode * Access mode
*/ */
public enum AccessMode{ public enum AccessMode {
ACCESS_READWRITE, ACCESS_READWRITE, ACCESS_READONLY
ACCESS_READONLY };
} ;
/** /**
* Access mode * Access mode
*/ */
public enum Acked{ public enum Acked {
FALSE, FALSE, TRUE
TRUE };
} ;
protected final class Metadata { public final class Metadata {
public AccessMode flightAccess; /** Defines the access level for the local flight transactions (readonly and readwrite) */ public AccessMode flightAccess;
public AccessMode gcsAccess; /** Defines the access level for the local GCS transactions (readonly and readwrite) */ /**
public Acked flightTelemetryAcked; /** Defines if an ack is required for the transactions of this object (1:acked, 0:not acked) */ * Defines the access level for the local flight transactions (readonly
public UpdateMode flightTelemetryUpdateMode; /** Update mode used by the autopilot (UpdateMode) */ * and readwrite)
public int flightTelemetryUpdatePeriod; /** Update period used by the autopilot (only if telemetry mode is PERIODIC) */ */
public Acked gcsTelemetryAcked; /** Defines if an ack is required for the transactions of this object (1:acked, 0:not acked) */ public AccessMode gcsAccess;
public UpdateMode gcsTelemetryUpdateMode; /** Update mode used by the GCS (UpdateMode) */ /**
public int gcsTelemetryUpdatePeriod; /** Update period used by the GCS (only if telemetry mode is PERIODIC) */ * Defines the access level for the local GCS transactions (readonly and
public UpdateMode loggingUpdateMode; /** Update mode used by the logging module (UpdateMode) */ * readwrite)
public int loggingUpdatePeriod; /** Update period used by the logging module (only if logging mode is PERIODIC) */ */
} ; public Acked flightTelemetryAcked;
/**
* Defines if an ack is required for the transactions of this object
* (1:acked, 0:not acked)
*/
public UpdateMode flightTelemetryUpdateMode;
/** Update mode used by the autopilot (UpdateMode) */
public int flightTelemetryUpdatePeriod;
/**
* Update period used by the autopilot (only if telemetry mode is
* PERIODIC)
*/
public Acked gcsTelemetryAcked;
/**
* Defines if an ack is required for the transactions of this object
* (1:acked, 0:not acked)
*/
public UpdateMode gcsTelemetryUpdateMode;
/** Update mode used by the GCS (UpdateMode) */
public int gcsTelemetryUpdatePeriod;
/** Update period used by the GCS (only if telemetry mode is PERIODIC) */
public UpdateMode loggingUpdateMode;
/** Update mode used by the logging module (UpdateMode) */
public int loggingUpdatePeriod;
/**
* Update period used by the logging module (only if logging mode is
* PERIODIC)
*/
};
public UAVObject(int objID, Boolean isSingleInst, String name) { public UAVObject(int objID, Boolean isSingleInst, String name) {
this.objID = objID; this.objID = objID;
this.instID = 0; this.instID = 0;
this.isSingleInst = isSingleInst; this.isSingleInst = isSingleInst;
this.name = name; this.name = name;
// this.mutex = new QMutex(QMutex::Recursive); // this.mutex = new QMutex(QMutex::Recursive);
}; };
public void initialize(int instID) { public void initialize(int instID) {
//QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
this.instID = instID; this.instID = instID;
} }
/** /**
* Initialize objects' data fields * Initialize objects' data fields
* @param fields List of fields held by the object *
* @param data Pointer to that actual object data, this is needed by the fields to access the data * @param fields
* @param numBytes Number of bytes in the object (total, including all fields) * List of fields held by the object
* @throws Exception When unable to unpack a field * @param data
* Pointer to that actual object data, this is needed by the
* fields to access the data
* @param numBytes
* Number of bytes in the object (total, including all fields)
* @throws Exception
* When unable to unpack a field
*/ */
public void initializeFields(List<UAVObjectField> fields, ByteBuffer data, int numBytes) throws Exception public void initializeFields(List<UAVObjectField> fields, ByteBuffer data,
{ int numBytes) throws Exception {
// TODO: QMutexLocker locker(mutex); // TODO: QMutexLocker locker(mutex);
this.numBytes = numBytes; this.numBytes = numBytes;
this.data = data; // this.data = data;
this.fields = fields; this.fields = fields;
// Initialize fields // Initialize fields
for (int n = 0; n < fields.size(); ++n) for (int n = 0; n < fields.size(); ++n) {
{
fields.get(n).initialize(this); fields.get(n).initialize(this);
} }
unpack(data); // unpack(data);
} }
/** /**
* Get the object ID * Get the object ID
*/ */
public int getObjID() public int getObjID() {
{
return objID; return objID;
} }
/** /**
* Get the instance ID * Get the instance ID
*/ */
public int getInstID() public int getInstID() {
{
return instID; return instID;
} }
/** /**
* Returns true if this is a single instance object * Returns true if this is a single instance object
*/ */
public boolean isSingleInstance() public boolean isSingleInstance() {
{
return isSingleInst; return isSingleInst;
} }
/** /**
* Get the name of the object * Get the name of the object
*/ */
public String getName() public String getName() {
{
return name; return name;
} }
/** /**
* Get the description of the object * Get the description of the object
*
* @return The description of the object * @return The description of the object
*/ */
public String getDescription() public String getDescription() {
{
return description; return description;
} }
/** /**
* Set the description of the object * Set the description of the object
* @param The description of the object *
* @return * @param The
* description of the object
* @return
*/ */
public void setDescription(String description) public void setDescription(String description) {
{
this.description = description; this.description = description;
} }
/** /**
* Get the total number of bytes of the object's data * Get the total number of bytes of the object's data
*/ */
public int getNumBytes() public int getNumBytes() {
{
return numBytes; return numBytes;
} }
// /** // /**
// * Request that this object is updated with the latest values from the autopilot // * Request that this object is updated with the latest values from the
// */ // autopilot
// /* void UAVObject::requestUpdate() // */
// { // /* void UAVObject::requestUpdate()
// emit updateRequested(this); // {
// } */ // emit updateRequested(this);
// } */
// //
// /** // /**
// * Signal that the object has been updated // * Signal that the object has been updated
// */ // */
// /* void UAVObject::updated() // /* void UAVObject::updated()
// { // {
// emit objectUpdatedManual(this); // emit objectUpdatedManual(this);
// emit objectUpdated(this); // emit objectUpdated(this);
// } */ // } */
// //
// /** // /**
// * Lock mutex of this object // * Lock mutex of this object
// */ // */
// /* void UAVObject::lock() // /* void UAVObject::lock()
// { // {
// mutex->lock(); // mutex->lock();
// } */ // } */
// //
// /** // /**
// * Lock mutex of this object // * Lock mutex of this object
// */ // */
// /* void UAVObject::lock(int timeoutMs) // /* void UAVObject::lock(int timeoutMs)
// { // {
// mutex->tryLock(timeoutMs); // mutex->tryLock(timeoutMs);
// } */ // } */
// //
// /** // /**
// * Unlock mutex of this object // * Unlock mutex of this object
// */ // */
// /* void UAVObject::unlock() // /* void UAVObject::unlock()
// { // {
// mutex->unlock(); // mutex->unlock();
// } */ // } */
// //
// /** // /**
// * Get object's mutex // * Get object's mutex
// */ // */
// QMutex* UAVObject::getMutex() // QMutex* UAVObject::getMutex()
// { // {
// return mutex; // return mutex;
// } // }
/** /**
* Get the number of fields held by this object * Get the number of fields held by this object
*/ */
public int getNumFields() public int getNumFields() {
{ // QMutexLocker locker(mutex);
//QMutexLocker locker(mutex);
return fields.size(); return fields.size();
} }
/** /**
* Get the object's fields * Get the object's fields
*/ */
public List<UAVObjectField> getFields() public List<UAVObjectField> getFields() {
{ // QMutexLocker locker(mutex);
//QMutexLocker locker(mutex);
return fields; return fields;
} }
/** /**
* Get a specific field * Get a specific field
* @throws Exception *
* @throws Exception
* @returns The field or NULL if not found * @returns The field or NULL if not found
*/ */
public UAVObjectField getField(String name) throws Exception public UAVObjectField getField(String name) {
{ // QMutexLocker locker(mutex);
//QMutexLocker locker(mutex);
// Look for field // Look for field
ListIterator<UAVObjectField> li = fields.listIterator(); ListIterator<UAVObjectField> li = fields.listIterator();
while(li.hasNext()) { while (li.hasNext()) {
UAVObjectField field = li.next(); UAVObjectField field = li.next();
if(field.getName().equals(name)) if (field.getName().equals(name))
return field; return field;
} }
throw new Exception("Field not found"); //throw new Exception("Field not found");
return null;
} }
/** /**
* Pack the object data into a byte array * Pack the object data into a byte array
* @param dataOut ByteBuffer to receive the data. *
* @throws Exception * @param dataOut
* ByteBuffer to receive the data.
* @throws Exception
* @returns The number of bytes copied * @returns The number of bytes copied
* @note The array must already have enough space allocated for the object * @note The array must already have enough space allocated for the object
*/ */
public int pack(ByteBuffer dataOut) throws Exception public int pack(ByteBuffer dataOut) throws Exception {
{
// QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
if(dataOut.remaining() < getNumBytes()) if (dataOut.remaining() < getNumBytes())
throw new Exception("Not enough bytes in ByteBuffer to pack object"); throw new Exception("Not enough bytes in ByteBuffer to pack object");
int numBytes = 0; int numBytes = 0;
ListIterator<UAVObjectField> li = fields.listIterator(); ListIterator<UAVObjectField> li = fields.listIterator();
while(li.hasNext()) { while (li.hasNext()) {
UAVObjectField field = li.next(); UAVObjectField field = li.next();
numBytes += field.pack(dataOut); numBytes += field.pack(dataOut);
} }
@ -249,232 +282,232 @@ public abstract class UAVObject {
/** /**
* Unpack the object data from a byte array * Unpack the object data from a byte array
* @param dataIn The ByteBuffer to pull data from *
* @throws Exception * @param dataIn
* The ByteBuffer to pull data from
* @throws Exception
* @returns The number of bytes copied * @returns The number of bytes copied
*/ */
public int unpack(ByteBuffer dataIn) throws Exception public int unpack(ByteBuffer dataIn) throws Exception {
{ if( dataIn == null )
return 0;
System.out.println( dataIn.toString() );
// QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
int numBytes = 0; int numBytes = 0;
ListIterator<UAVObjectField> li = fields.listIterator(); ListIterator<UAVObjectField> li = fields.listIterator();
while(li.hasNext()) { while (li.hasNext()) {
UAVObjectField field = li.next(); UAVObjectField field = li.next();
System.out.println(field.toString());
numBytes += field.unpack(dataIn); numBytes += field.unpack(dataIn);
} }
return numBytes; return numBytes;
// TODO: Callbacks // TODO: Callbacks
//emit objectUnpacked(this); // trigger object updated event // emit objectUnpacked(this); // trigger object updated event
//emit objectUpdated(this); // emit objectUpdated(this);
} }
// /** // /**
// * Save the object data to the file. // * Save the object data to the file.
// * The file will be created in the current directory // * The file will be created in the current directory
// * and its name will be the same as the object with // * and its name will be the same as the object with
// * the .uavobj extension. // * the .uavobj extension.
// * @returns True on success, false on failure // * @returns True on success, false on failure
// */ // */
// bool UAVObject::save() // bool UAVObject::save()
// { // {
// QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
// //
// // Open file // // Open file
// QFile file(name + ".uavobj"); // QFile file(name + ".uavobj");
// if (!file.open(QFile::WriteOnly)) // if (!file.open(QFile::WriteOnly))
// { // {
// return false; // return false;
// } // }
// //
// // Write object // // Write object
// if ( !save(file) ) // if ( !save(file) )
// { // {
// return false; // return false;
// } // }
// //
// // Close file // // Close file
// file.close(); // file.close();
// return true; // return true;
// } // }
// //
// /** // /**
// * Save the object data to the file. // * Save the object data to the file.
// * The file is expected to be already open for writting. // * The file is expected to be already open for writting.
// * The data will be appended and the file will not be closed. // * The data will be appended and the file will not be closed.
// * @returns True on success, false on failure // * @returns True on success, false on failure
// */ // */
// bool UAVObject::save(QFile& file) // bool UAVObject::save(QFile& file)
// { // {
// QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
// quint8 buffer[numBytes]; // quint8 buffer[numBytes];
// quint8 tmpId[4]; // quint8 tmpId[4];
// //
// // Write the object ID // // Write the object ID
// qToLittleEndian<quint32>(objID, tmpId); // qToLittleEndian<quint32>(objID, tmpId);
// if ( file.write((const char*)tmpId, 4) == -1 ) // if ( file.write((const char*)tmpId, 4) == -1 )
// { // {
// return false; // return false;
// } // }
// //
// // Write the instance ID // // Write the instance ID
// if (!isSingleInst) // if (!isSingleInst)
// { // {
// qToLittleEndian<quint16>(instID, tmpId); // qToLittleEndian<quint16>(instID, tmpId);
// if ( file.write((const char*)tmpId, 2) == -1 ) // if ( file.write((const char*)tmpId, 2) == -1 )
// { // {
// return false; // return false;
// } // }
// } // }
// //
// // Write the data // // Write the data
// pack(buffer); // pack(buffer);
// if ( file.write((const char*)buffer, numBytes) == -1 ) // if ( file.write((const char*)buffer, numBytes) == -1 )
// { // {
// return false; // return false;
// } // }
// //
// // Done // // Done
// return true; // return true;
// } // }
// //
// /** // /**
// * Load the object data from a file. // * Load the object data from a file.
// * The file will be openned in the current directory // * The file will be openned in the current directory
// * and its name will be the same as the object with // * and its name will be the same as the object with
// * the .uavobj extension. // * the .uavobj extension.
// * @returns True on success, false on failure // * @returns True on success, false on failure
// */ // */
// bool UAVObject::load() // bool UAVObject::load()
// { // {
// QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
// //
// // Open file // // Open file
// QFile file(name + ".uavobj"); // QFile file(name + ".uavobj");
// if (!file.open(QFile::ReadOnly)) // if (!file.open(QFile::ReadOnly))
// { // {
// return false; // return false;
// } // }
// //
// // Load object // // Load object
// if ( !load(file) ) // if ( !load(file) )
// { // {
// return false; // return false;
// } // }
// //
// // Close file // // Close file
// file.close(); // file.close();
// return true; // return true;
// } // }
// //
// /** // /**
// * Load the object data from file. // * Load the object data from file.
// * The file is expected to be already open for reading. // * The file is expected to be already open for reading.
// * The data will be read and the file will not be closed. // * The data will be read and the file will not be closed.
// * @returns True on success, false on failure // * @returns True on success, false on failure
// */ // */
// bool UAVObject::load(QFile& file) // bool UAVObject::load(QFile& file)
// { // {
// QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
// quint8 buffer[numBytes]; // quint8 buffer[numBytes];
// quint8 tmpId[4]; // quint8 tmpId[4];
// //
// // Read the object ID // // Read the object ID
// if ( file.read((char*)tmpId, 4) != 4 ) // if ( file.read((char*)tmpId, 4) != 4 )
// { // {
// return false; // return false;
// } // }
// //
// // Check that the IDs match // // Check that the IDs match
// if (qFromLittleEndian<quint32>(tmpId) != objID) // if (qFromLittleEndian<quint32>(tmpId) != objID)
// { // {
// return false; // return false;
// } // }
// //
// // Read the instance ID // // Read the instance ID
// if ( file.read((char*)tmpId, 2) != 2 ) // if ( file.read((char*)tmpId, 2) != 2 )
// { // {
// return false; // return false;
// } // }
// //
// // Check that the IDs match // // Check that the IDs match
// if (qFromLittleEndian<quint16>(tmpId) != instID) // if (qFromLittleEndian<quint16>(tmpId) != instID)
// { // {
// return false; // return false;
// } // }
// //
// // Read and unpack the data // // Read and unpack the data
// if ( file.read((char*)buffer, numBytes) != numBytes ) // if ( file.read((char*)buffer, numBytes) != numBytes )
// { // {
// return false; // return false;
// } // }
// unpack(buffer); // unpack(buffer);
// //
// // Done // // Done
// return true; // return true;
// } // }
/** /**
* Return a string with the object information * Return a string with the object information
*/ */
@Override @Override
public String toString() public String toString() {
{
return toStringBrief() + toStringData(); return toStringBrief() + toStringData();
} }
/** /**
* Return a string with the object information (only the header) * Return a string with the object information (only the header)
*/ */
public String toStringBrief() public String toStringBrief() {
{ return getName() + " ( " + getObjID() + " " + getInstID() + " " + getNumBytes() + ")\n";
return String.format("%1 (ID: %2, InstID: %3, NumBytes: %4, SInst: %5)\n", // getName(), getObjID(), getInstID(), getNumBytes(),
getName(), // isSingleInstance());
getObjID(),
getInstID(),
getNumBytes(),
isSingleInstance());
} }
/** /**
* Return a string with the object information (only the data) * Return a string with the object information (only the data)
*/ */
public String toStringData() public String toStringData() {
{
String s = new String(); String s = new String();
ListIterator<UAVObjectField> li = fields.listIterator(); ListIterator<UAVObjectField> li = fields.listIterator();
while(li.hasNext()) { while (li.hasNext()) {
UAVObjectField field = li.next(); UAVObjectField field = li.next();
s += field.toString(); s += field.toString();
} }
return s; return s;
} }
// /** // /**
// * Emit the transactionCompleted event (used by the UAVTalk plugin) // * Emit the transactionCompleted event (used by the UAVTalk plugin)
// */ // */
// void UAVObject::emitTransactionCompleted(bool success) // void UAVObject::emitTransactionCompleted(bool success)
// { // {
// emit transactionCompleted(this, success); // emit transactionCompleted(this, success);
// } // }
/** /**
* Java specific functions * Java specific functions
*/ */
public UAVObject clone() { public UAVObject clone() {
return (UAVObject) clone(); return (UAVObject) clone();
} }
/** /**
* Abstract functions * Abstract functions
*/ */
public abstract void setMetadata(Metadata mdata); public abstract void setMetadata(Metadata mdata);
public abstract Metadata getMetadata(); public abstract Metadata getMetadata();
public abstract Metadata getDefaultMetadata(); public abstract Metadata getDefaultMetadata();
public abstract UAVDataObject clone(int instID);
protected abstract void setDefaultFieldValues();
/** /**
* Private data for the object, common to all * Private data for the object, common to all
*/ */
protected int objID; protected int objID;
protected int instID; protected int instID;
protected boolean isSingleInst; protected boolean isSingleInst;
@ -482,6 +515,6 @@ public abstract class UAVObject {
protected String description; protected String description;
protected int numBytes; protected int numBytes;
// TODO: QMutex* mutex; // TODO: QMutex* mutex;
protected ByteBuffer data; // protected ByteBuffer data;
protected List<UAVObjectField> fields; protected List<UAVObjectField> fields;
} }

View File

@ -248,34 +248,37 @@ public class UAVObjectField {
return null; return null;
} }
public void setValue(Object data) throws Exception { setValue(data,0); } public void setValue(Object data) { setValue(data,0); }
public void setValue(Object data, int index) throws Exception { public void setValue(Object data, int index) {
// QMutexLocker locker(obj->getMutex()); // QMutexLocker locker(obj->getMutex());
// Check that index is not out of bounds // Check that index is not out of bounds
if ( index >= numElements ) if ( index >= numElements )
throw new Exception("Index out of bounds"); //throw new Exception("Index out of bounds");
System.out.println(data.toString()); System.out.println(data.toString());
System.out.println(this.data.toString()); System.out.println(this.data.toString());
// Get metadata // Get metadata
//UAVObject.Metadata mdata = obj.getMetadata(); UAVObject.Metadata mdata = obj.getMetadata();
// Update value if the access mode permits // Update value if the access mode permits
if ( true ) //mdata.gcsAccess == UAVObject.AccessMode.ACCESS_READWRITE ) if ( mdata.gcsAccess == UAVObject.AccessMode.ACCESS_READWRITE )
{ {
ByteBuffer bbuf = ByteBuffer.allocate(numBytesPerElement);
switch (type) switch (type)
{ {
case INT8: case INT8:
data = new Integer((Byte) data);
case INT16: case INT16:
data = new Integer((Short) data);
case INT32: case INT32:
{ {
List<Integer> l = (List<Integer>) this.data; List<Integer> l = (List<Integer>) this.data;
l.set(index, (Integer) data); l.set(index,(Integer) data);
break; break;
} }
case UINT8: case UINT8:
data = new Integer((Byte) data);
case UINT16: case UINT16:
data = new Integer((Short) data);
case UINT32: case UINT32:
{ {
List<Integer> l = (List<Integer>) this.data; List<Integer> l = (List<Integer>) this.data;
@ -291,14 +294,14 @@ public class UAVObjectField {
case ENUM: case ENUM:
{ {
byte val = (byte) options.indexOf((String) data); byte val = (byte) options.indexOf((String) data);
if(val < 0) throw new Exception("Enumerated value not found"); //if(val < 0) throw new Exception("Enumerated value not found");
List<Byte> l = (List<Byte>) this.data; List<Byte> l = (List<Byte>) this.data;
l.set(index, val); l.set(index, val);
break; break;
} }
case STRING: case STRING:
{ {
throw new Exception("Sorry I haven't implemented strings yet"); //throw new Exception("Sorry I haven't implemented strings yet");
} }
} }
} }
@ -309,24 +312,24 @@ public class UAVObjectField {
return Double.valueOf((Double) getValue(index)); return Double.valueOf((Double) getValue(index));
} }
void setDouble(double value) throws Exception { setDouble(value, 0); }; public void setDouble(double value) throws Exception { setDouble(value, 0); };
void setDouble(double value, int index) throws Exception { public void setDouble(double value, int index) throws Exception {
setValue(value, index); setValue(value, index);
} }
int getDataOffset() { public int getDataOffset() {
return offset; return offset;
} }
int getNumBytes() { public int getNumBytes() {
return numBytesPerElement * numElements; return numBytesPerElement * numElements;
} }
int getNumBytesElement() { public int getNumBytesElement() {
return numBytesPerElement; return numBytesPerElement;
} }
boolean isNumeric() { public boolean isNumeric() {
switch (type) switch (type)
{ {
case INT8: case INT8:
@ -352,7 +355,7 @@ public class UAVObjectField {
} }
} }
boolean isText() { public boolean isText() {
switch (type) switch (type)
{ {
case INT8: case INT8:

View File

@ -21,8 +21,9 @@ public class UAVObjectManager {
* A new instance can be created directly by instantiating a new object or by calling clone() of * A new instance can be created directly by instantiating a new object or by calling clone() of
* an existing object. The object will be registered and will be properly initialized so that it can accept * an existing object. The object will be registered and will be properly initialized so that it can accept
* updates. * updates.
* @throws Exception
*/ */
Boolean registerObject(UAVDataObject obj) public boolean registerObject(UAVDataObject obj) throws Exception
{ {
// QMutexLocker locker(mutex); // QMutexLocker locker(mutex);
@ -126,7 +127,7 @@ public class UAVObjectManager {
return true; return true;
} }
void addObject(UAVObject obj) public void addObject(UAVObject obj)
{ {
// Add to list // Add to list
List<UAVObject> ls = new ArrayList<UAVObject>(); List<UAVObject> ls = new ArrayList<UAVObject>();
@ -139,7 +140,7 @@ public class UAVObjectManager {
* Get all objects. A two dimentional QList is returned. Objects are grouped by * Get all objects. A two dimentional QList is returned. Objects are grouped by
* instances of the same object type. * instances of the same object type.
*/ */
List<List<UAVObject>> getObjects() public List<List<UAVObject>> getObjects()
{ {
//QMutexLocker locker(mutex); //QMutexLocker locker(mutex);
return objects; return objects;
@ -148,7 +149,7 @@ public class UAVObjectManager {
/** /**
* Same as getObjects() but will only return DataObjects. * Same as getObjects() but will only return DataObjects.
*/ */
List< List<UAVDataObject> > getDataObjects() public List< List<UAVDataObject> > getDataObjects()
{ {
return new ArrayList<List<UAVDataObject>>(); return new ArrayList<List<UAVDataObject>>();
@ -186,7 +187,7 @@ public class UAVObjectManager {
/** /**
* Same as getObjects() but will only return MetaObjects. * Same as getObjects() but will only return MetaObjects.
*/ */
List <List<UAVMetaObject> > getMetaObjects() public List <List<UAVMetaObject> > getMetaObjects()
{ {
return new ArrayList< List<UAVMetaObject> >(); return new ArrayList< List<UAVMetaObject> >();
/* /*
@ -227,7 +228,7 @@ public class UAVObjectManager {
* Get a specific object given its name and instance ID * Get a specific object given its name and instance ID
* @returns The object is found or NULL if not * @returns The object is found or NULL if not
*/ */
UAVObject getObject(String name, int instId) public UAVObject getObject(String name, int instId)
{ {
return getObject(name, 0, instId); return getObject(name, 0, instId);
} }
@ -236,7 +237,7 @@ public class UAVObjectManager {
* Get a specific object given its object and instance ID * Get a specific object given its object and instance ID
* @returns The object is found or NULL if not * @returns The object is found or NULL if not
*/ */
UAVObject getObject(int objId, int instId) public UAVObject getObject(int objId, int instId)
{ {
return getObject(null, objId, instId); return getObject(null, objId, instId);
} }
@ -244,7 +245,7 @@ public class UAVObjectManager {
/** /**
* Helper function for the public getObject() functions. * Helper function for the public getObject() functions.
*/ */
UAVObject getObject(String name, int objId, int instId) public UAVObject getObject(String name, int objId, int instId)
{ {
//QMutexLocker locker(mutex); //QMutexLocker locker(mutex);
// Check if this object type is already in the list // Check if this object type is already in the list
@ -271,7 +272,7 @@ public class UAVObjectManager {
/** /**
* Get all the instances of the object specified by name * Get all the instances of the object specified by name
*/ */
List<UAVObject> getObjectInstances(String name) public List<UAVObject> getObjectInstances(String name)
{ {
return getObjectInstances(name, 0); return getObjectInstances(name, 0);
} }
@ -279,7 +280,7 @@ public class UAVObjectManager {
/** /**
* Get all the instances of the object specified by its ID * Get all the instances of the object specified by its ID
*/ */
List<UAVObject> getObjectInstances(int objId) public List<UAVObject> getObjectInstances(int objId)
{ {
return getObjectInstances(null, objId); return getObjectInstances(null, objId);
} }
@ -287,7 +288,7 @@ public class UAVObjectManager {
/** /**
* Helper function for the public getObjectInstances() * Helper function for the public getObjectInstances()
*/ */
List<UAVObject> getObjectInstances(String name, int objId) public List<UAVObject> getObjectInstances(String name, int objId)
{ {
//QMutexLocker locker(mutex); //QMutexLocker locker(mutex);
// Check if this object type is already in the list // Check if this object type is already in the list
@ -307,7 +308,7 @@ public class UAVObjectManager {
/** /**
* Get the number of instances for an object given its name * Get the number of instances for an object given its name
*/ */
int getNumInstances(String name) public int getNumInstances(String name)
{ {
return getNumInstances(name, 0); return getNumInstances(name, 0);
} }
@ -315,7 +316,7 @@ public class UAVObjectManager {
/** /**
* Get the number of instances for an object given its ID * Get the number of instances for an object given its ID
*/ */
int getNumInstances(int objId) public int getNumInstances(int objId)
{ {
return getNumInstances(null, objId); return getNumInstances(null, objId);
} }
@ -323,10 +324,9 @@ public class UAVObjectManager {
/** /**
* Helper function for public getNumInstances * Helper function for public getNumInstances
*/ */
int getNumInstances(String name, int objId) public int getNumInstances(String name, int objId)
{ {
return getObjectInstances(name,objId).size(); return getObjectInstances(name,objId).size();
} }
} }