Skip to content

Java

IoTize Studio includes a reduced version of Java Machine which is optimized to allow you to:

  • analyze the data and make decisions to create alarms and perform data logging
  • access the I/O signals
  • format MQTT messages
  • use the TapNLink as a standalone device (without target)

If you really need to use a method that is not currently supported, please contact IoTize. We may find a workaround, or we may add a method, but our goal is to keep a very light process so that the IOT_JVM can provide high performances:

  1. Very limited RAM usage (a few hundredth of bytes).
  2. Very limited FLASH footprint (around 50KB).
  3. Very efficient execution (10µs per instruction).

General principles of the IOT_JVM

IOT_JVM supports only a single class with a strict definition. Refer to the examples in IoTize\IoTize Studio\Examples\JVM to get an idea of what you can do.

One class: Only one class is supported. Its name must match the java file name, and have the following mandatory methods (the constructor is optional):

  • void check (int id) : this function is called periodically (depending of the periods declared for the TapNLink variables registration).

  • void onException (final in errorcode, final int jpc, final int jsp).

Native subclasses: Some ‘predefined subclasses’ are provided to link this main class to the lwM2M machine and are executed as native code. Their declarations are in IoTize Studio/bin/com/iotize/JVM directory.
JAVA running exceptions: JAVA exceptions are NOT managed. Only the running exceptions (divide by zero, stack overflow/underflow,…) redirect to the onException method of the main class.
Static JVM: The IOT_JVM is quite static. Constructors can only be called during initialization, only the handlers of the created object can be called without any memory management. The only exception is the String management that provides some limited flexibility.
Restricted variable types: The only variable types allowed are: final types (INT8, UINT8, INT16, UINT16, INT32, UINT32, FLOAT32, ARRAY), TapNLinkVar derivative types (TapNLinkFloat, ….), or arrays of final types (arrays of bytes, floats,…). Native subclasses also offer some String facility and access to low level peripherals.
.bcb file: The .bcb file is equivalent to the .class file generated by the JAVAC compiler, but it has been reformatted to make reading and execution easier in our simplified JVM. Mostly, it contains static tables that the JVM handles very efficiency. Most of the ASCII names of the declared objects are also removed in order to shrink the initial .class file.

How to proceed?

Prerequirements

If you don't have Oracle javac compiler, install Oracle OpenJDK suite to obtain javac.exe.

1. Create your .java file

We recommend that you take a copy of a java file from the examples and rename it to the name of your class. Copy and paste it into Studio's Java editor, or use your preferred editor. image

Keep these import commands unchanged:

  • import com.iotize.jvm.*; // System classes (Variables)
  • import com.iotize.jvm.hal.*; // HAL drivers (UART)

Create a public class (with the same name as the file) and:

  • mandatory onException and check methods
  • optionally a constructor
  • and whatever other features you desire that are supported by IOT_JVM.

2. Compile and assemble your Class, and add to IoTize Studio configuration

Compile your Java class with javac.exe (image icon in Studio's Java Editor).

If you get an error message saying javac is not installed:
  1. Open the Start Windows.
  2. Search for environment, then select Edit environment variables for your account.
  3. Click on the Path variable and Edit.
  4. At the end of the variable value, type ; then paste the pathname of the directory that contains the javac.exe. (e.g. ;C:\Program Files\Java\jdk-14.0.2\bin)

  • Any compiler errors before the 2 ============= separator lines are java call errors that must be resolved first.
  • Errors after these lines are due to the intentionally reduced JVM version and you should try to find a workaround. Contact IoTize if you have problems.

3. Assemble your Class

Assemble the resulting TapNLink_User.java file using IOT_JVM.exe (in ‘IoTize Studio/bin’ folder) to check compliance with the IOT_JVM environment and produce a .bcb file.

Check the assemblers messages, and verify that your Tap's firmware is the same version as the most recent method (currently version 1.95). If it isn't, please update your firmware to obtain the most recent methods and reassemble.

4. Add to IoTize Studio configuration

Add the .bcb (or .java) file to IoTize Studio in Tap / JVM Custom Code File. image

Native subclasses

IoTize specific

The IOT_JVM environment has several subclasses, detailed here, that give access to the TapNLink environment. They are implemented in the TapNLink firmware as native code, but they are not fully compliant with their official JAVA implementation (only a few methods are available).

  • The TapNLinkVar object provides a simple synchronization between the variables of the IOTIZE STUDIO project and the IOT_JVM. Depending on the variable type (float, byte, int, array,…), various predefined subclasses exist (see \IoTize\IoTize Studio\bin\com\iotize\jvm).

  • The Hal/Pin subclass provides a generic access to the GPIOs of the extension port. The pins addressability depends on the module. The end pins are always power pins, some pins are reserved, and some have different uses depending on the Tap. The “pin_id’ is an unsigned byte that specifies the reference of the pin on the extension ports:

imageimage

“java/lang” class

Class java/lang/Integer

public Integer   valueOf(int i)
Returns an Integer instance representing the specified int value.

public Integer   valueOf(String s)
Returns an Integer object holding the value of the specified String.

public int       intValue()
Returns the value of this Integer as an int.

Class java/lang/Short

public Short     valueOf(short s)
Returns a Short instance representing the specified short value.

public Short     valueOf(String s)
Returns a Short object holding the value of the specified String.

public short     shortValue()
Returns the value of this Short as a short.

Class java/lang/Boolean

public Boolean   valueOf(String s)
Returns a Boolean object holding the value of the specified String.

Class java/lang/Byte

public Byte      valueOf(byte s)
Returns a Byte instance representing the specified byte value.

public Byte      valueOf(String s)
Returns a Byte object holding the value of the specified String.

public byte      byteValue()
Returns the value of this Byte as a byte.

Class java/lang/Character

public Character valueOf(char c)
Returns a Character instance representing the specified char value.

Class java/lang/Long

public Long      valueOf(long s)
Returns a Long instance representing the specified long value.

public Long      valueOf(String s)
Returns a Long object holding the value of the specified String.

public long      longValue()
Returns the value of this Long as a long.

Class java/lang/Float

public Float     valueOf(float s)
Returns a Float instance representing the specified float value.

public Float     valueOf(String s)
Returns a Float object holding the value of the specified String.

public float     floatValue()
Returns the value of this Float as a float.

Class java/lang/Double

public Double    valueOf(double s)
Returns a Double instance representing the specified double value.

public Double    valueOf(String s)
Returns a Double object holding the value of the specified String.

public double    doubleValue()
Returns the value of this Double as a double.

Class java/lang/String

public String()
Initializes a newly created String object so that it represents an empty character sequence.

public String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform's default charset.

public String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

public String   format(String format, Object... args)
Returns a formatted string using the specified format string and arguments.

public int        length()
Returns the length of this string.

public char       charAt(int index)
Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

public byte[]     getBytes()
Encodes this String into a sequence of bytes using the utf8 charset, storing the result into a new byte array.
Returns the resultant byte array

Class java/lang/StringBuilder

public StringBuilder()
Constructs a string builder with no characters in it and an initial capacity of 16 characters.

public StringBuilder append:(String str)
Appends the specified string to this character sequence.
Returns a reference to the resulting object

public StringBuilder append(int i)
Appends the string representation of the int argument to this sequence. The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(int), and the characters of that string were then appended to this character sequence.

public StringBuilder append(short i)
Appends the string representation of the short argument to this sequence. The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(int), and the characters of that string were then appended to this character sequence.

public StringBuilder append(double i)
Appends the string representation of the double argument to this sequence. The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(int), and the characters of that string were then appended to this character sequence.

public StringBuilder append(float i)
Appends the string representation of the float argument to this sequence. The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(int), and the characters of that string were then appended to this character sequence.

public StringBuilder append(long i)
Appends the string representation of the long argument to this sequence. The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(int), and the characters of that string were then appended to this character sequence.

public StringBuilder append(byte i)
Appends the string representation of the byte argument to this sequence. The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(int), and the characters of that string were then appended to this character sequence.

public String     toString()
Returns a string representing the data in this sequence.

public int        length()
Returns the length (character count).

public char       charAt (int pos)
Returns the char value in this sequence at the specified index. The first char value is at index 0, the next at index 1, and so on, as in array indexing.
The index argument must be greater than or equal to 0, and less than the length of this sequence.

Class java/lang/Math

public double sin(double val)
Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.

public double cos(double val)
Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.

public double acos(double val)
Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.

public double asin(double val)
Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.

public double tan(double val)
Returns the trigonometric tangent of an angle.

public double atan(double val)
Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.

public double exp(double val)
Returns Euler's number e raised to the power of a double value.

public double log(double val)
Returns the natural logarithm (base e) of a double value.

public double log10(double val)
Returns the base 10 logarithm of a double value.

public double abs(double val)
Returns the absolute value of a double value.

public double pow(double a, double b)
Returns the value of the first argument raised to the power of the second argument.

public double sqrt(double val)
Returns the correctly rounded positive square root of a double value.

public double floor(double val)
Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.