Adding new commands to slush

8 May

Adding new commands to slush
But it doesn’t end there. We can just as easily create our own commands and add them to slush. For this, we are putting back in the commands we deleted or commented out in the previous steps and removing the optional commands that we added, as they are going to take memory space. We will be then be adding a new command, owr, (for 1-Wire report), which will list all 1-Wire devices by type and serial number on all 1-Wire busses. This is the source for OwrCommand.java that implements the owr command. Copy the file to the slush\command directory.

Listing 7-2: OwrCommand.java

package  com.dalsemi.slush.command;
import  com.dalsemi.shell.server.*;
import java.io.*;
import java.util.*;
import com.dalsemi.system.*;
import com.dalsemi.slush.*;
import com.dalsemi.onewire.*;
import  com.dalsemi.onewire.adapter.*;
import  com.dalsemi.onewire.container.*;
public class OwrCommand implements SlushCommand
{
public String getUsageString()
{
return “owr\r\n\r\nDisplay current 1-wire devlice list.”;
}
public void execute(SystemInputStream in, SystemPrintStream out,
SystemPrintStream err, String[] args,

Hashtable env) throws Exception
{
boolean verbose = false;
for(int i = 0; i < args.length; i++) {
if(args[i].equals(“-v”)) {
verbose = true;
}
else {
out.println(getUsageString());
return;
}
}
Slush slush = (Slush)TINIOS.getShell();
try
{
Enumeration adapter_enum =
OneWireAccessProvider.enumerateAllAdapters();
while( adapter_enum.hasMoreElements() ) {
DSPortAdapter  adapter=(DSPortAdapter)
out.println( “Adapter: “ + adapter.getAdapterName() );
adapter.setSearchAllDevices();
adapter.targetAllFamilies();
Enumeration ibutton_enum =
adapter.getAllDeviceContainers();
while( ibutton_enum.hasMoreElements() ) {
OneWireContainer node =
(OneWireContainer) ibutton_enum.nextElement();
out.println(node.getName() + “ – “ +
node.getAddressAsString());
}
}
}
catch (Exception e)
{
out.println(“Error displaying stats”);
out.println(e);
}
}
}

You may notice that this is essentially an extension of the FindDevices.java program we developed in Chapter 5 to verify we installed the 1-Wire libraries properly. If you are curious how this works, you can compare this program to any of the other slush commands. We have only implemented two methods:

getUsageString() and execute(). The getUsageString() method is called from slush whenever the user asks for help on this command (help owr). The execute() method is called from slush whenever the user types the owr command by the slush command interpreter. This command then asks the TINI firmware for a list of all 1- Wire adapters:

Enumeration adapter_enum =
OneWireAccessProvider.enumerateAllAdapters();
while( adapter_enum.hasMoreElements() ) {
DSPortAdapter  adapter=(DSPortAdapter)
adapter_enum.nextElement();
out.println( “Adapter: “ + adapter.getAdapterName() );
We then ask each adapter it finds for a list of all 1-Wire devices connected to that
adapter:
adapter.setSearchAllDevices();
adapter.targetAllFamilies();
Enumeration ibutton_enum =
adapter.getAllDeviceContainers();
while( ibutton_enum.hasMoreElements() ) {
We then print out a little information on each 1-Wire device.
OneWireContainer node =
(OneWireContainer) ibutton_enum.nextElement();
out.println(node.getName() + “ – “ +
node.getAddressAsString());

We will be getting deeper into the 1-Wire API and the details of 1-Wire in Chapter 10.

To add this new command to slush, edit Slush.java and in the
initializeShellCommands() method, add a line like the following to add the owr
command to the command interpreter:

CommandInterpreter.addCommand(“owr”, new OwrCommand());
Save slush.java, recompile slush and run TINIConvertor exactly as we did in the
previous examples.

C:\>set SMUSH=\projects\modslush
C:\>javac    -classpath         %TINI_HOME%\bin\tiniclasses.jar;%SMUSH%
Slush.java
C:\>java   -cp %TINI_HOME%\bin\tini.jar TINIConvertor
-o Slush.tbin -l
-d %TINI_HOME%\bin\tini.db
-f .

Converting to flash format. (57585 bytes)
Segment start address: 70100, length: 57585, CRC: 78da
Flash file size: 57592
Finished with build.
Use JavaKit to send the slush.tbin file to TINI and then test it.

TINI /> owr
Adapter: TINIExternalAdapter
Device type: 10 – 700000004B8F1010
Device type: 18 – 1C00000002795818
Device type: 12 – CB00000017006112
Device type: 01 – 21000007997D9F01
Adapter: TINIInternalAdapter
Device type: 89 – 9F5E70005C8D1089

Wow, it works! Notice the “Device type:” for the container name. That’s because we didn’t use BuildDependency to build in the container classes that contain the actual names of the 1-Wire devices. Now this is where BuildDependency gets tricky (or just plain odd). We would think this command would work (due to DOS command line limits you may need to put the following into a batch file and execute that):

C:\> java  -cp %TINI_HOME%\bin\tini.jar BuildDependency
-f .
-o slush.tbin
-l
-d %TINI_HOME%\bin\tini.db
-x %TINI_HOME%\bin\owapi_dep.txt
-p %TINI_HOME%\bin\owapi_dependencies_TINI.jar
-add  OneWireContainer01;OneWireContainer10
-debug

Notice that we included the -add OneWireContainer01;OneWireContainer10 which tells BuildDependency to include the class files for these classes by looking them up in the owapi_dep.txt file, which tells BuildDependency which .class files to include from owapi_dependencies_TINI.jar.

But it doesn’t work.

Could not create the application: Could not Add class com/dalsemi/ onewire/container/OneWireContainer01 JiBDB Error: Attempted addition of duplicate class “com/dalsemi/onewire/container/OneWireContainer01” BuildDependency clean-up complete. The program did not successfully terminate. Try running with the ‘-debug’ option to determine the cause.

This is really odd. Why does it think we are adding a duplicate class of OneWireContainer01? It has to do with the way BuildDependency looks for included files. It’s possible to get BuildDependency to work with something like the following:

C:\>java -cp %TINI_HOME%\bin\tini.jar BuildDependency
-f slush.class
-f commandinterpreter.class
-f command\
-f Slush$SlushDHCPListener.class
-o slush.tbin
-l
-d %TINI_HOME%\bin\tini.db
-x%TINI_HOME%\bin\owapi_dep.txt
-p%TINI_HOME%\bin\owapi_dependencies_TINI.jar
-add  OneWireContainer01;OneWireContainer10
-debug

However, this gets rather unmanageable. For one thing, javac will split up large class files into several smaller pieces. (Look at the directory in which we are compiling Slush right now. You will see Slush$1.class as well as Slush.class.) We will need to include all of these intermediate class files using the -f directive to BuildDependency. A simpler way to deal with this is to compile Slush from one level higher in the directory hierarchy and let the -f directive include the whole subdirectory we are using.

C:\> cd ..
C:\> notepad build.bat

Enter the following into Notepad (or your favorite text editor) but as a single line of
text:

java -cp %TINI_HOME%\bin\tini.jar BuildDependency
-f slush
-o slush.tbin
-l
-d %TINI_HOME%\bin\tini.db
-x%TINI_HOME%\bin\owapi_dep.txt
-p%TINI_HOME%\bin\owapi_dependencies_TINI.jar
-add OneWireContainer01;OneWireContainer10 -debug

What we are doing is including the whole slush subdirectory by using the -f slush command line switch. This is much easier than worrying about what intermediate files the Java compiler creates. We need to execute this batch file on the directory that contains the slush directory (not in the slush directory) since we are specifying the whole slush directory with the -f slush commandline directive.

Compile Slush:
C:\> build
Converting to flash format. (59119 bytes)
Segment start address: 70100, length: 59119, CRC: 70bc
Flash file size: 59126
Finished with build.
We can then load this version of slush onto TINI and test it out.
TINI /> owr
Adapter: TINIExternalAdapter
DS1920 – 700000004B8F1010
Device type: 18 – 1C00000002795818
Device type: 12 – CB00000017006112
DS1990A – 21000007997D9F01
Adapter: TINIInternalAdapter
Device type: 89 – 9F5E70005C8D1089

You may be wondering why we only included OneWireContainer01 and OneWireContainer10 when there are more devices connected to TINI. The primary reason is that if we add the other containers then we will seriously exceed the 64kbyte limit. Why’s that? Take a peek in the file \opt\tini\bin\owapi_dep.txt and see why. This file tells BuildDependency where to find all of the classes that are needed by each container type. Look at line 20 where OneWireContainer18 is defined (CONT_ROOT is defined as “com.dalsemi.onewire.container”) so this line means that OneWireConatiner18 includes the following classes:

com.dalsemi.onewire.container.OneWireContainer18
com.dalsemi.onewire.container.MemoryBank
com.dalsemi.onewire.container.MemoryBankNV
com.dalsemi.onewire.container.MemoryBankNVCRC
com.dalsemi.onewire.container.MemoryBankScratch
com.dalsemi.onewire.container.MemoryBankScratchCRC
com.dalsemi.onewire.container.MemoryBankScratchEx
com.dalsemi.onewire.container.MemoryBankScratchSHA
com.dalsemi.onewire.container.PagedMemoryBank
com.dalsemi.onewire.container.ScratchPad
com.dalsemi.onewire.container.SHAiButton

The other containers are similarly defined. If we include several of these into our version of slush (using the -add option), then suddenly we are adding a lot of classes.

Random Posts

Comments are closed.