From 695e2eefac04486856369e04112aefbdc8c539c6 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Mon, 28 Nov 2016 14:50:32 -0800 Subject: [PATCH] Fixing bugs on char translation; testing for arrays --- config/iotpolicy/testclasspolicy.pol | 16 ++ iotjava/iotpolicy/IoTCompiler.java | 113 ++++++++--- iotjava/iotrmi/C++/IoTRMIUtil.hpp | 186 ++++++++++++++---- iotjava/iotrmi/C++/basics/TestClass.hpp | 53 +++++ iotjava/iotrmi/C++/basics/TestClass_Stub.cpp | 34 +++- iotjava/iotrmi/Java/basics/TestClass.java | 45 +++++ .../iotrmi/Java/basics/TestClass_Stub.java | 18 ++ 7 files changed, 395 insertions(+), 70 deletions(-) diff --git a/config/iotpolicy/testclasspolicy.pol b/config/iotpolicy/testclasspolicy.pol index 24be14c..e2b70cc 100644 --- a/config/iotpolicy/testclasspolicy.pol +++ b/config/iotpolicy/testclasspolicy.pol @@ -7,6 +7,15 @@ public interface TestClassInterface { public double getDouble(double in); public boolean getBoolean(boolean in); public char getChar(char in); + + public byte[] getByteArray(byte in[]); + public short[] getShortArray(short in[]); + public long[] getLongArray(long in[]); + public float[] getFloatArray(float in[]); + public double[] getDoubleArray(double in[]); + public boolean[] getBooleanArray(boolean in[]); + public char[] getCharArray(char in[]); + public int getA(); public void setA(int _int); public void setB(float _float); @@ -24,6 +33,13 @@ public interface TestClassInterface { method = "getDouble(double in)"; method = "getBoolean(boolean in)"; method = "getChar(char in)"; + method = "getByteArray(byte in[])"; + method = "getShortArray(short in[])"; + method = "getLongArray(long in[])"; + method = "getFloatArray(float in[])"; + method = "getDoubleArray(double in[])"; + method = "getBooleanArray(boolean in[])"; + method = "getCharArray(char in[])"; method = "getA()"; method = "setA(int _int)"; method = "setB(float _float)"; diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index ee3d287..651a4b8 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -327,7 +327,9 @@ public class IoTCompiler { InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); List methods = intDecl.getMethods(); Set importClasses = getImportClasses(methods, intDecl); - printImportStatements(importClasses); + List stdImportClasses = getStandardJavaIntfaceImportClasses(); + List allImportClasses = getAllLibClasses(stdImportClasses, importClasses); + printImportStatements(allImportClasses); // Write interface header println(""); println("public interface " + intface + " {"); @@ -359,13 +361,16 @@ public class IoTCompiler { DeclarationHandler decHandler = mapIntDeclHand.get(intface); InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface); // Pass in set of methods and get import classes - Set importClasses = getImportClasses(intMeth.getValue(), intDecl); - printImportStatements(importClasses); + List methods = intDecl.getMethods(); + Set importClasses = getImportClasses(methods, intDecl); + List stdImportClasses = getStandardJavaIntfaceImportClasses(); + List allImportClasses = getAllLibClasses(stdImportClasses, importClasses); + printImportStatements(allImportClasses); // Write interface header println(""); println("public interface " + newIntface + " {\n"); // Write methods - writeMethodJavaInterface(intMeth.getValue(), intDecl); + writeMethodJavaInterface(methods, intDecl); println("}"); pw.close(); System.out.println("IoTCompiler: Generated interface " + newIntface + ".java..."); @@ -2494,6 +2499,9 @@ public class IoTCompiler { List methParams = intDecl.getMethodParams(method); List methPrmTypes = intDecl.getMethodParamTypes(method); + + //System.out.println("\n\nMethod return type: " + checkAndGetCplusType(intDecl.getMethodType(method)) + "\n\n"); + print(checkAndGetCplusType(intDecl.getMethodType(method)) + " " + intDecl.getMethodId(method) + "("); boolean isCallbackMethod = false; @@ -2561,8 +2569,8 @@ public class IoTCompiler { println("int methodId = " + intDecl.getMethodNumId(method) + ";"); String retType = intDecl.getMethodType(method); //String retTypeC = checkAndGetCplusType(retType); - //println("string retType = \"" + checkAndGetCplusArrayType(retTypeC) + "\";"); - println("string retType = \"" + retType + "\";"); + //println("string retType = \"" + checkAndGetCplusArrayType(getStructType(getEnumType(retTypeC))) + "\";"); + println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";"); // Generate array of parameter types print("string paramCls[] = { "); for (int i = 0; i < methParams.size(); i++) { @@ -2570,10 +2578,10 @@ public class IoTCompiler { if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object print("\"int\""); } else { // Generate normal classes if it's not a callback object - //String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i)); - //String prmType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i)); - String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); - String prmType = getSimpleType(getEnumType(paramTypeC)); + //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); + //String prmType = getSimpleType(getEnumType(paramTypeC)); + String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); + String prmType = getEnumType(paramTypeC); print("\"" + prmType + "\""); } if (i != methParams.size() - 1) // Check if this is the last element @@ -2896,7 +2904,7 @@ public class IoTCompiler { String retType = intDecl.getMethodType(method); //String retTypeC = checkAndGetCplusType(retType); //println("string retType = \"" + checkAndGetCplusArrayType(getStructType(getEnumType(retTypeC))) + "\";"); - println("string retType = \"" + retType + "\";"); + println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";"); // Generate array of parameter types if (isStructPresent(methParams, methPrmTypes)) { writeStructParamClassCplusStub(methParams, methPrmTypes); @@ -2904,11 +2912,10 @@ public class IoTCompiler { println("int numParam = " + methParams.size() + ";"); print("string paramCls[] = { "); for (int i = 0; i < methParams.size(); i++) { - //String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i)); - //String paramType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i)); - //print("\"" + getEnumType(paramType) + "\""); - String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); - String prmType = getSimpleType(getEnumType(paramTypeC)); + //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); + //String prmType = getSimpleType(getEnumType(paramTypeC)); + String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); + String prmType = getEnumType(paramTypeC); print("\"" + prmType + "\""); // Check if this is the last element (don't print a comma) if (i != methParams.size() - 1) { @@ -2941,10 +2948,12 @@ public class IoTCompiler { if (getParamCategory(retType) == ParamCategory.ENUM) { checkAndWriteEnumRetTypeCplusStub(retType); } else { - if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) + //if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) + if (isArrayOrList(retType,retType)) println(checkAndGetCplusType(retType) + " retVal;"); - else + else { println(checkAndGetCplusType(retType) + " retVal = " + generateCplusInitializer(retType) + ";"); + } println("void* retObj = &retVal;"); println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);"); println("return retVal;"); @@ -3603,8 +3612,7 @@ public class IoTCompiler { if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type println("rmiObj->sendReturnObj(retObj, retCls, numRetObj);"); else - //println("rmiObj->sendReturnObj(retObj, \"" + getEnumType(checkAndGetCplusArrayType(retTypeC)) + "\");"); - println("rmiObj->sendReturnObj(retObj, \"" + getEnumType(retType) + "\");"); + println("rmiObj->sendReturnObj(retObj, \"" + checkAndGetCplusRetClsType(getEnumType(retType)) + "\");"); } } @@ -3626,11 +3634,10 @@ public class IoTCompiler { callbackType = paramType; print("\"int\""); } else { // Generate normal classes if it's not a callback object - //String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i)); - //String prmType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i)); - //print("\"" + getEnumType(prmType) + "\""); - String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); - String prmType = getSimpleType(getEnumType(paramTypeC)); + //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i)); + //String prmType = getSimpleType(getEnumType(paramTypeC)); + String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i)); + String prmType = getEnumType(paramTypeC); print("\"" + prmType + "\""); } if (i != methParams.size() - 1) { @@ -3650,7 +3657,6 @@ public class IoTCompiler { println("vector paramEnumInt" + i + ";"); } else { String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i)); - //println(methParamComplete + " = " + generateCplusInitializer(methPrmType) + ";"); println(methParamComplete + ";"); } } @@ -4570,6 +4576,18 @@ public class IoTCompiler { } + // Generate a set of standard classes for import statements + private List getStandardJavaIntfaceImportClasses() { + + List importClasses = new ArrayList(); + // Add the standard list first + importClasses.add("java.util.List"); + importClasses.add("java.util.ArrayList"); + + return importClasses; + } + + // Generate a set of standard classes for import statements private List getStandardJavaImportClasses() { @@ -4909,7 +4927,7 @@ public class IoTCompiler { } else return getNonPrimitiveCplusClass(paramType); } else if(paramType.contains("[]")) { // Array type (used for return type only) - String cArray = "vector<" + getSimpleArrayType(paramType) + ">"; + String cArray = "vector<" + convertType(getSimpleArrayType(paramType)) + ">"; return cArray; } else if(getParamCategory(paramType) == ParamCategory.USERDEFINED) { return paramType + "*"; @@ -4977,6 +4995,47 @@ public class IoTCompiler { } + // Return the class type for class resolution (for return value) + // - Check and return C++ array class, e.g. int A[] into int* + // - Check and return C++ vector class, e.g. List A into vector + private String checkAndGetCplusRetClsType(String paramType) { + + String paramTypeRet = null; + // Check for array declaration + if (paramType.contains("[]")) { + String type = paramType.split("\\[\\]")[0]; + paramTypeRet = getSimpleArrayType(type) + "*"; + } else if (paramType.contains("<") && paramType.contains(">")) { + // Just return it as is if it's not an array + String type = paramType.split("<")[1].split(">")[0]; + paramTypeRet = "vector<" + getGenericType(type) + ">"; + } else + paramTypeRet = paramType; + + return paramTypeRet; + } + + + // Return the class type for class resolution (for method arguments) + // - Check and return C++ array class, e.g. int A[] into int* + // - Check and return C++ vector class, e.g. List A into vector + private String checkAndGetCplusArgClsType(String paramType, String param) { + + String paramTypeRet = null; + // Check for array declaration + if (param.contains("[]")) { + paramTypeRet = getSimpleArrayType(paramType) + "*"; + } else if (paramType.contains("<") && paramType.contains(">")) { + // Just return it as is if it's not an array + String type = paramType.split("<")[1].split(">")[0]; + paramTypeRet = "vector<" + getGenericType(type) + ">"; + } else + paramTypeRet = paramType; + + return paramTypeRet; + } + + // Detect array declaration, e.g. int A[], // then generate type "int[]" private String checkAndGetArray(String paramType, String param) { diff --git a/iotjava/iotrmi/C++/IoTRMIUtil.hpp b/iotjava/iotrmi/C++/IoTRMIUtil.hpp index 6b33b61..5da411c 100644 --- a/iotjava/iotrmi/C++/IoTRMIUtil.hpp +++ b/iotjava/iotrmi/C++/IoTRMIUtil.hpp @@ -42,6 +42,7 @@ class IoTRMIUtil { static int getByteStringLength(vector arrString); // Primitives to byte array + static char* byteToByteArray(char c, char* bytes); static char* shortToByteArray(short i, char* bytes); static char* intToByteArray(int i, char* bytes); static char* longToByteArray(int64_t i, char* bytes); @@ -52,6 +53,7 @@ class IoTRMIUtil { static char* stringToByteArray(string c, char* bytes); // Byte array to primitives + static char* byteArrayToByte(char* result, char* bytes); static short* byteArrayToShort(short* result, char* bytes); static int* byteArrayToInt(int* result, char* bytes); static int64_t* byteArrayToLong(int64_t* result, char* bytes); @@ -67,6 +69,7 @@ class IoTRMIUtil { static char* getObjectBytes(char* retObjBytes, void* obj, const char* type); // Arrays to bytes + static char* arrByteToByteArray(vector arrByte, char* bytes); static char* arrShortToByteArray(vector arrShort, char* bytes); static char* arrIntToByteArray(vector arrInt, char* bytes); static char* arrLongToByteArray(vector arrInt, char* bytes); @@ -77,6 +80,7 @@ class IoTRMIUtil { static char* arrStringToByteArray(vector arrString, char* bytes); // Bytes to array + static vector* byteArrayToByteArray(vector* result, char* bytes, int len); static vector* byteArrayToShortArray(vector* result, char* bytes, int len); static vector* byteArrayToIntArray(vector* result, char* bytes, int len); static vector* byteArrayToLongArray(vector* result, char* bytes, int len); @@ -96,6 +100,7 @@ class IoTRMIUtil { const static int PARAM_LEN = 4; // 4 bytes = 32 bits (4-byte field that stores the length of the param) const static int RETURN_LEN = 4; // 4 bytes = 32 bits (4-byte field that stores the length of the return object) const static int CHAR_LEN = 2; // 2 bytes (we follow Java convention) + const static int BYTE_LEN = 1; // 1 byte const static int BOOL_LEN = 1; // 1 byte private: @@ -188,34 +193,52 @@ int IoTRMIUtil::getVarTypeSize(string type, void* paramObj) { if (type.compare("String") == 0) { // Get the length of the string through void* casting to string* paramLen = (*(string*)paramObj).length(); - } else if (type.compare("String[]") == 0) { + } else if ( (type.compare("String*") == 0) || + (type.compare("string*") == 0) || + (type.compare("vector") == 0)) { paramLen = IoTRMIUtil::getByteStringLength(*(vector*) paramObj); - } else if (type.compare("byte[]") == 0) { + } else if ( (type.compare("byte*") == 0) || + (type.compare("Byte*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("byte"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("short[]") == 0) { + } else if ( (type.compare("short*") == 0) || + (type.compare("Short*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("short"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("int[]") == 0) { + } else if ( (type.compare("int*") == 0) || + (type.compare("Integer*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("int"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("long[]") == 0) { + } else if ( (type.compare("long*") == 0) || + (type.compare("Long*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("long"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("float[]") == 0) { + } else if ( (type.compare("float*") == 0) || + (type.compare("Float*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("float"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("double[]") == 0) { + } else if ( (type.compare("double*") == 0) || + (type.compare("Double*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("double"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("bool[]") == 0) { + } else if ( (type.compare("boolean*") == 0) || + (type.compare("Boolean*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("boolean"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("char[]") == 0) { + } else if ( (type.compare("char*") == 0) || + (type.compare("Character*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("char"); paramLen = (*(vector*) paramObj).size() * dataSize; } else { - cerr << "IoTRMIUtil: Unrecognizable type: " << type; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; exit(-1); } @@ -249,7 +272,7 @@ void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramByte if (strcmp(type, "b") == 0 || strcmp(type, "byte") == 0) { - retObj = (void*) ¶mBytes[0]; + retObj = (void*) byteArrayToByte((char*) retObj, paramBytes); } else if ( strcmp(type, "s") == 0 || strcmp(type, "short") == 0) { retObj = (void*) byteArrayToShort((short*) retObj, paramBytes); @@ -274,11 +297,11 @@ void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramByte } else if ( strcmp(type, "Ss") == 0 || strcmp(type, "String") == 0) { retObj = (void*) byteArrayToString((string*) retObj, paramBytes, len); - } else if ( string(type).find("[]") != string::npos) { + } else if ( string(type).find("*") != string::npos) { // This is an array type, i.e. vector retObj = getArrayParamObject(retObj, type, paramBytes, len); } else { - cerr << "IoTRMIUtil: Unrecognizable type: " << type; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; exit(-1); } @@ -286,29 +309,46 @@ void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramByte } -// Get array of objects from byte array - overload getParamObject function +// Get array of objects from byte array void* IoTRMIUtil::getArrayParamObject(void* retObj, const char* type, char* paramBytes, int len) { - if (strcmp(type, "byte[]") == 0) { - retObj = (vector*) paramBytes; - } else if (strcmp(type, "short[]") == 0) { + if ((strcmp(type, "byte*") == 0) || + (strcmp(type, "Byte*") == 0) || + (strcmp(type, "vector") == 0)) { + retObj = byteArrayToByteArray((vector*) retObj, paramBytes, len); + } else if ( (strcmp(type, "short*") == 0) || + (strcmp(type, "Short*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToShortArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "int[]") == 0) { + } else if ( (strcmp(type, "int*") == 0) || + (strcmp(type, "Integer*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToIntArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "long[]") == 0) { + } else if ( (strcmp(type, "long*") == 0) || + (strcmp(type, "Long*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToLongArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "float[]") == 0) { + } else if ( (strcmp(type, "float*") == 0) || + (strcmp(type, "Float*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToFloatArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "double[]") == 0) { + } else if ( (strcmp(type, "double*") == 0) || + (strcmp(type, "Double*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToDoubleArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "bool[]") == 0) { + } else if ( (strcmp(type, "boolean*") == 0) || + (strcmp(type, "Boolean*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToBooleanArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "char[]") == 0) { + } else if ( (strcmp(type, "char*") == 0) || + (strcmp(type, "Character*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToCharArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "String[]") == 0) { + } else if ( (strcmp(type, "String*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToStringArray((vector*) retObj, paramBytes, len); } else { - cerr << "IoTRMIUtil: Unrecognizable type: " << type; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; exit(-1); } @@ -321,7 +361,7 @@ char* IoTRMIUtil::getObjectBytes(char* retObjBytes, void* obj, const char* type) if (strcmp(type, "b") == 0 || strcmp(type, "byte") == 0) { - retObjBytes = (char*) obj; + retObjBytes = byteToByteArray(*((char*) obj), retObjBytes); } else if ( strcmp(type, "s") == 0 || strcmp(type, "short") == 0) { retObjBytes = shortToByteArray(*((short*) obj), retObjBytes); @@ -344,14 +384,13 @@ char* IoTRMIUtil::getObjectBytes(char* retObjBytes, void* obj, const char* type) strcmp(type, "char") == 0) { retObjBytes = charToByteArray(*((char*) obj), retObjBytes); } else if ( strcmp(type, "Ss") == 0 || - strcmp(type, "String") == 0 || - strcmp(type, "string") == 0) { + strcmp(type, "String") == 0) { retObjBytes = stringToByteArray(*((string*) obj), retObjBytes); - } else if ( string(type).find("[]") != string::npos) { + } else if ( string(type).find("*") != string::npos) { // This is an array type, i.e. vector retObjBytes = getArrayObjectBytes(retObjBytes, obj, type); } else { - cerr << "IoTRMIUtil: Unrecognizable type: " << type; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; exit(-1); } @@ -362,26 +401,43 @@ char* IoTRMIUtil::getObjectBytes(char* retObjBytes, void* obj, const char* type) // Getting byte array for arrays of primitives char* IoTRMIUtil::getArrayObjectBytes(char* retObjBytes, void* obj, const char* type) { - if (strcmp(type, "byte[]") == 0) { - retObjBytes = (char*) obj; - } else if (strcmp(type, "short[]") == 0) { + if ((strcmp(type, "byte*") == 0) || + (strcmp(type, "Byte*") == 0) || + (strcmp(type, "vector") == 0)) { + retObjBytes = arrByteToByteArray(*((vector*) obj), retObjBytes); + } else if ( (strcmp(type, "short*") == 0) || + (strcmp(type, "Short*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrShortToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "int[]") == 0) { + } else if ( (strcmp(type, "int*") == 0) || + (strcmp(type, "Integer*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrIntToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "long[]") == 0) { + } else if ( (strcmp(type, "long*") == 0) || + (strcmp(type, "Long*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrLongToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "float[]") == 0) { + } else if ( (strcmp(type, "float*") == 0) || + (strcmp(type, "Float*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrFloatToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "double[]") == 0) { + } else if ( (strcmp(type, "double*") == 0) || + (strcmp(type, "Double*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrDoubleToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "bool[]") == 0) { + } else if ( (strcmp(type, "boolean*") == 0) || + (strcmp(type, "Boolean*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrBooleanToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "char[]") == 0) { + } else if ( (strcmp(type, "char*") == 0) || + (strcmp(type, "Character*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrCharToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "String[]") == 0) { + } else if ( (strcmp(type, "String*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrStringToByteArray(*((vector*) obj), retObjBytes); } else { - cerr << "IoTRMIUtil: Unrecognizable type: " << type; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; exit(-1); } @@ -392,6 +448,20 @@ char* IoTRMIUtil::getArrayObjectBytes(char* retObjBytes, void* obj, const char* // Conversions // Array handlers - we use vector data type and not traditional arrays // Array to bytes +char* IoTRMIUtil::arrByteToByteArray(vector arrByte, char* bytes) { + + int pos = 0; + for (char chr : arrByte) { + char tmpBytes[BYTE_LEN]; + byteToByteArray(chr, tmpBytes); + memcpy(bytes + pos, tmpBytes, BYTE_LEN); + pos = pos + BYTE_LEN; + } + + return bytes; +} + + char* IoTRMIUtil::arrShortToByteArray(vector arrShort, char* bytes) { int pos = 0; @@ -517,6 +587,24 @@ char* IoTRMIUtil::arrStringToByteArray(vector arrString, char* bytes) { // Bytes to array +vector* IoTRMIUtil::byteArrayToByteArray(vector* result, char* bytes, int len) { + + // Single element bytes + char elmt[BYTE_LEN]; + // Prepare vector + int arrLen = len/BYTE_LEN; + for(int i = 0; i < arrLen; i++) { + int offset = i * BYTE_LEN; + memcpy(elmt, bytes + offset, BYTE_LEN); + char res; + byteArrayToByte(&res, elmt); + result->push_back(res); + } + + return result; +} + + vector* IoTRMIUtil::byteArrayToShortArray(vector* result, char* bytes, int len) { // Single element bytes @@ -680,6 +768,15 @@ vector* IoTRMIUtil::byteArrayToStringArray(vector* result, char* // Conversions // Primitives to byte array +char* IoTRMIUtil::byteToByteArray(char c, char* bytes) { + + // Just copy the char into char* + bytes[0] = c; + + return bytes; +} + + char* IoTRMIUtil::shortToByteArray(short s, char* bytes) { short sInvert = htobe16(s); @@ -814,6 +911,13 @@ double* IoTRMIUtil::byteArrayToDouble(double* result, char* bytes) { } +char* IoTRMIUtil::byteArrayToByte(char* result, char* bytes) { + + *result = bytes[0]; + return result; +} + + char* IoTRMIUtil::byteArrayToChar(char* result, char* bytes) { *result = bytes[1]; diff --git a/iotjava/iotrmi/C++/basics/TestClass.hpp b/iotjava/iotrmi/C++/basics/TestClass.hpp index d39960b..24fe1ac 100644 --- a/iotjava/iotrmi/C++/basics/TestClass.hpp +++ b/iotjava/iotrmi/C++/basics/TestClass.hpp @@ -20,6 +20,15 @@ class TestClass : public TestClassInterface { double getDouble(double in); bool getBoolean(bool in); char getChar(char in); + + vector getByteArray(vector in); + vector getShortArray(vector in); + vector getLongArray(vector in); + vector getFloatArray(vector in); + vector getDoubleArray(vector in); + vector getBooleanArray(vector in); + vector getCharArray(vector in); + int getA(); void setA(int _int); void setB(float _float); @@ -53,6 +62,7 @@ TestClass::TestClass(int _int, float _float, string _string) { } +// Single variables char TestClass::getByte(char in) { return in; @@ -95,6 +105,49 @@ char TestClass::getChar(char in) { } +// Arrays +vector TestClass::getByteArray(vector in) { + + return in; +} + + +vector TestClass::getShortArray(vector in) { + + return in; +} + + +vector TestClass::getLongArray(vector in) { + + return in; +} + + +vector TestClass::getFloatArray(vector in) { + + return in; +} + + +vector TestClass::getDoubleArray(vector in) { + + return in; +} + + +vector TestClass::getBooleanArray(vector in) { + + return in; +} + + +vector TestClass::getCharArray(vector in) { + + return in; +} + + int TestClass::getA() { return intA; diff --git a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp index c4a692d..1d8ad91 100644 --- a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp @@ -16,14 +16,44 @@ int main(int argc, char *argv[]) ports.push_back(12345); TestClassComplete *tcStub = new TestClassComplete_Stub(port, address, rev, &bResult, ports); + cout << "==== SINGLE ====" << endl; cout << "Return value: " << tcStub->getByte(68) << endl; cout << "Return value: " << tcStub->getShort(1234) << endl; - //cout << "Return value: " << tcStub->getLong(12345678) << endl; + cout << "Return value: " << tcStub->getLong(12345678) << endl; cout << "Return value: " << tcStub->getFloat(12.345) << endl; cout << "Return value: " << tcStub->getDouble(12345.678) << endl; cout << "Return value: " << tcStub->getBoolean(true) << endl; cout << "Return value: " << tcStub->getChar('c') << endl; - + cout << "==== ARRAY ====" << endl; + vector in1; + in1.push_back(68); + in1.push_back(69); + cout << "Return value: " << tcStub->getByteArray(in1)[0] << ", " << tcStub->getByteArray(in1)[1] << endl; + vector in2; + in2.push_back(1234); + in2.push_back(1235); + cout << "Return value: " << tcStub->getShortArray(in2)[0] << ", " << tcStub->getShortArray(in2)[1] << endl; + vector in3; + in3.push_back(12345678); + in3.push_back(12356782); + cout << "Return value: " << tcStub->getLongArray(in3)[0] << ", " << tcStub->getLongArray(in3)[1] << endl; + vector in4; + in4.push_back(12.345); + in4.push_back(12.346); + cout << "Return value: " << tcStub->getFloatArray(in4)[0] << ", " << tcStub->getFloatArray(in4)[1] << endl; + vector in5; + in5.push_back(12345.678); + in5.push_back(12345.543); + cout << "Return value: " << tcStub->getDoubleArray(in5)[0] << ", " << tcStub->getDoubleArray(in5)[1] << endl; + vector in6; + in6.push_back(true); + in6.push_back(false); + cout << "Return value: " << tcStub->getBooleanArray(in6)[0] << ", " << tcStub->getBooleanArray(in6)[1] << endl; + vector in7; + in7.push_back('c'); + in7.push_back('e'); + cout << "Return value: " << tcStub->getCharArray(in7)[0] << ", " << tcStub->getCharArray(in7)[1] << endl; + cout << "==== OTHERS ====" << endl; cout << "Return value: " << tcStub->getA() << endl; cout << "Return value: " << tcStub->setAndGetA(123) << endl; cout << "Return value: " << tcStub->setACAndGetA("string", 123) << endl; diff --git a/iotjava/iotrmi/Java/basics/TestClass.java b/iotjava/iotrmi/Java/basics/TestClass.java index 19b99de..4f55242 100644 --- a/iotjava/iotrmi/Java/basics/TestClass.java +++ b/iotjava/iotrmi/Java/basics/TestClass.java @@ -30,6 +30,7 @@ public class TestClass implements TestClassInterface { } + // Single variables public byte getByte(byte in) { return in; @@ -72,6 +73,50 @@ public class TestClass implements TestClassInterface { } + // Arrays + public byte[] getByteArray(byte[] in) { + + return in; + } + + + public short[] getShortArray(short[] in) { + + return in; + } + + + public long[] getLongArray(long[] in) { + + return in; + } + + + public float[] getFloatArray(float[] in) { + + return in; + } + + + public double[] getDoubleArray(double[] in) { + + return in; + } + + + public boolean[] getBooleanArray(boolean[] in) { + + return in; + } + + + public char[] getCharArray(char[] in) { + + return in; + } + + + // Other functions public int getA() { return intA; diff --git a/iotjava/iotrmi/Java/basics/TestClass_Stub.java b/iotjava/iotrmi/Java/basics/TestClass_Stub.java index d94a09d..01b1bdd 100644 --- a/iotjava/iotrmi/Java/basics/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/basics/TestClass_Stub.java @@ -17,6 +17,7 @@ public class TestClass_Stub { System.out.println("Allocated ports: " + Arrays.toString(ports)); TestClassComplete_Stub tcstub = new TestClassComplete_Stub(port, address, rev, ports); + System.out.println("==== SINGLE ===="); System.out.println("Return value: " + tcstub.getByte((byte)68)); System.out.println("Return value: " + tcstub.getShort((short)1234)); System.out.println("Return value: " + tcstub.getLong(12345678l)); @@ -25,6 +26,23 @@ public class TestClass_Stub { System.out.println("Return value: " + tcstub.getBoolean(true)); System.out.println("Return value: " + tcstub.getChar('c')); + System.out.println("==== ARRAY ===="); + byte[] in1 = { 68, 69 }; + System.out.println("Return value: " + Arrays.toString(tcstub.getByteArray(in1))); + short[] in2 = { (short)1234, (short)1235 }; + System.out.println("Return value: " + Arrays.toString(tcstub.getShortArray(in2))); + long[] in3 = { 12345678l, 12356782l }; + System.out.println("Return value: " + Arrays.toString(tcstub.getLongArray(in3))); + float[] in4 = { 12.345f, 12.346f }; + System.out.println("Return value: " + Arrays.toString(tcstub.getFloatArray(in4))); + double[] in5 = { 12345.678, 12345.543 }; + System.out.println("Return value: " + Arrays.toString(tcstub.getDoubleArray(in5))); + boolean[] in6 = { true, false }; + System.out.println("Return value: " + Arrays.toString(tcstub.getBooleanArray(in6))); + char[] in7 = { 'c', 'e' }; + System.out.println("Return value: " + Arrays.toString(tcstub.getCharArray(in7))); + + System.out.println("==== OTHERS ===="); System.out.println("Return value: " + tcstub.getA()); System.out.println("Return value: " + tcstub.setAndGetA(123)); System.out.println("Return value: " + tcstub.setACAndGetA("string", 123)); -- 2.34.1