A TINI example

8 May

A TINI example
Now that we have discussed the tools needed to convert Java class files to a format that TINI recognizes, we will demonstrate the use of these programs on a Java program of our own. We will be examining a threaded web server (based on the threaded web server developed in Chapter 3) that prints out some TINI statuses like current time, uptime, API version, amount of free memory, all connected 1-Wire devices, etc. The point of this is to show you a complex TINI program example and to show more about TINIConvertor or BuildDependency tools. Then we will add our threaded web server to the .startup so it starts up automatically on boot and then we’ll install it on TINI in place of Slush. The program listing is shown below, TiniWebStat.java:

Listing 7-1: TiniWebStat.java
import java.util.*;
import java.io.*;
import java.net.*;
import com.dalsemi.system.*;
import com.dalsemi.tininet.*;
import com.dalsemi.onewire.*;
import  com.dalsemi.onewire.adapter.*;
import  com.dalsemi.onewire.container.*;
import  com.dalsemi.onewire.container.OneWireContainer.*;
/*
This program implements an HTTP server on TINI that
prints out the last time TINI rebooted and a whole bunch
of stats on your TINI.
*/
public class TiniWebStat implements Runnable {
protected Socket mySocket;
String who;
public TiniWebStat(Socket mySocket) {
this.mySocket = mySocket;
}
public static void main(String[] args) throws IOException {
int port = 80;
ServerSocket srv = new ServerSocket(port);
// listen forever and start new threads as requests come in
while (true) {
Socket mySocket = srv.accept();
TiniWebStat f2 = new TiniWebStat(mySocket);
Thread myThread = new Thread(f2);
myThread.start();
}
}
// This is the content of each thread
public void run() {
float upT;
try {
OutputStreamWriter osr =
new OutputStreamWriter(mySocket.getOutputStream());
BufferedWriter bwtr = new BufferedWriter(osr);
InputStreamReader isr =
new InputStreamReader(mySocket.getInputStream());
BufferedReader rd = new BufferedReader(isr);
// write out the one and only page and then clean up
writePage(bwtr);
rd.close();
mySocket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
//
public static void writePage(BufferedWriter wr) {
Date now                                                = new Date(System.currentTimeMillis());
// import form com.dalsemi.system
long uptm = TINIOS.uptimeMillis()/1000; // convert ms to secs
int ram                                                 = TINIOS.getFreeRAM();
int tsk                              = TINIOS.getTaskID();
String hw = TINIOS.getTINIHWVersion();
String sr = TINIOS.getTINISerialNumber();
String fr = TINIOS.getTINIOSFirmwareVersion();
String sh = TINIOS.getShellName();
// import from com.dalsemi.tininet
String dn = TININet.getDomainname();
String en = TININet.getEthernetAddress();
String hn = TININet.getHostname();
String ip = TININet.getIPAddress();
// figure out uptime in days, hours, minutes, seconds
int h = (int)(uptm/3600);
int m = (int)((uptm % 3600)/60);
int s = (int)(uptm % 60);
int d = h/24;
h = h % 24;
try {
wr.write(“HTTP/1.0 200 OK\r\n”);
wr.write(“Content-type:  text/html\r\n”);
wr.write(“<HTML><HEAD>\r\n”);
wr.write(“<TITLE>TINI  Stats</TITLE>\r\n”);
wr.write(“</HEAD>\r\n”);
wr.write(“<H1>TINI  Stats</H1>\r\n”);
wr.write(“<BODY><BLOCKQUOTE><PRE>\r\n”);
wr.write(“The current time:                          “ + now + “<br>” );
wr.write(“TINI has been up for:                      “ + d + “ days, “ );
wr.write(h + “ hours, “ + m + “ minutes, “ + s + “
seconds.<br>”);
wr.write(“Hardware version:                          “ + hw                                               + “<br>” );
wr.write(“Firmware version:                          “ + fr                                               + “<br>” );
wr.write(“Serial Number:                             “ + sr                                               + “<br>” );
wr.write(“Free RAM:                                  “ + ram + “<br>” );
wr.write(“Current Shell:                             “ + sh                                               + “<br>” );
wr.write(“Task ID:                                   “ + tsk + “<br>” );
wr.write(“Hostname:                                  “ + hn                                               + “<br>” );
wr.write(“Domainname:                                “ + dn                                               + “<br>” );
wr.write(“Ethernet(MAC) address: “ + en                                                                   + “<br>” );
wr.write(“IP adddress:                               “ + ip                                               + “<br>” );
wr.write(“<br>”);
wr.write(“One wire devcies connected:<br>”);
Enumeration adapter_enum =
OneWireAccessProvider.enumerateAllAdapters();
while( adapter_enum.hasMoreElements() ) {
DSPortAdapter adapter=(DSPortAdapter)
adapter_enum.nextElement();
wr.write( “Adapter: “ + adapter.getAdapterName() +
“<br>” );
adapter.setSearchAllDevices();
adapter.targetAllFamilies();
Enumeration ibutton_enum =
adapter.getAllDeviceContainers();
while( ibutton_enum.hasMoreElements() ) {
OneWireContainer node =
(OneWireContainer) ibutton_enum.nextElement();
wr.write(“ – “ + node.getName() + “ – “ +
node.getAddressAsString() + “<br>”);
}
}
wr.write(“</PRE></BLOCKQUOTE></BODY></HTML>\r\n”);
wr.flush();
}
catch (IOException e) {
}
catch (Exception e) {
}
}
}
As you examine the preceding Java program, you will notice the use of a number of the packages we discussed in the TINI API section (above). For instance, we import several of the com.dalsemi.* packages:
import com.dalsemi.system.*;
import com.dalsemi.tininet.*;
import com.dalsemi.onewire.*;

From the com.dalsemi.system.TINIOS package, we use a number of methods to get the status of TINI (uptime, amount of free RAM, and Task ID), and some of the TINI hardware and software versions. From the com.dalsemi.tininet.TININet package, we get a number of pieces of information on the network settings, and from the com.dalsemi.onewire.* package, we get the 1-Wire adapters and a list of all the 1- Wire devices attached to each adapter.

Compile this program with the following command line

C:\>javac -classpath %TINI_HOME%\bin\tiniclasses.jar;. TiniWebStat.java

Once this is compiled, we need to convert the .class file to a file that is compatible with the TINI firmware. Run TINIConvertor:

C:\>java -cp %TINI_HOME%\bin\tini.jar TINIConvertor
-d %TINI_HOME%\bin\tini.db
-f TiniWebStat.class
-o TiniWebStat.tini

We’ve generated a .tini file, so we will be running it under slush. With no errors, you can copy the file to your TINI with FTP and use Telnet to login and run the program. If you found any errors, check that you have typed all of the compile and TINIConvertor commands properly and that the program has been typed in accurately.

C:\>ftp 192.168.1.85
Connected to 192.168.1.85.
220 Welcome to slush.    (Version 1.02)   Ready for user login.
User (192.168.1.85:(none)): root
331 root login allowed. Password required.
Password: tini
230 User root logged in.
ftp> send TiniWebStat.tini
200 PORT Command successful.

150 ASCII connection open, putting TiniWebStat.tini
226 Closing data connection.
ftp: 2135 bytes sent in 0.00Seconds 2135000.00Kbytes/sec.
ftp> quit
221 Goodbye.
C:\> telnet 192.168.1.85
Trying 192.168.1.85…
Connected to TINI.
Escape character is ‘^]’.
Welcome to slush.    (Version 1.02)
TINI login: root
TINI password: tini
TINI /> java TiniWebStat.tini &
TINI />

Note that the “&” at the end of java  TiniWebStat.tini  &  runs the TiniWebStat
server as a background thread. Now start up a web browser on your desktop
computer and browse to your TINI. We wrote the server to listen to port 80. You need
to enter http:// followed by the IP address of your TINI followed by a :80. In this
case, my TINI board’s IP address is 192.168.1.85. You need to replace that with your
TINI board’s IP address.
Notice that 1-Wire devices are listed as “Device  Type:                                01” rather than the specific
Dallas Semiconductor device name.

One wire devcies connected:
Adapter: TINIExternalAdapter
- Device type: 10 – 700000004B8F1010
- Device type: 18 – 1C00000002795818
- Device type: 12 – CB00000017006112
- Device type: 01 – 21000007997D9F01
Adapter: TINIInternalAdapter
- Device type: 89 – 9F5E70005C8D1089

This is because the device name returned by OneWireContainer.getName() is using the default name as supplied by com.dalsemi.onewire.container and not by the specific container type (there is a container type for each type of device). To get the

Figure 7-17: Internet Explorer showing the TiniWebStat output

device name from these containers it is necessary to combine the dependencies into the .tini class file using BuildDependency. The following will do this:

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

This command may exceed the DOS command line size so you may have to create a batch file and put this command in the file and then execute the file, which is a good thing because you don’t want to type a command this long more than once anyway. So run BuildDependency on TiniWebStat.class and then FTP the TiniWebStat.tini file to your TINI. Before you run this new version, make sure you kill the old one—it’s still running (unless you have rebooted the TINI).

TINI /> ps
3 processes
1: Java GC (Owner root)
2: init (Owner root)
3: TiniWebStat.tini (Owner root)
TINI /> kill 3
TINI /> java TiniWebStat.tini &

Now browse the TiniWebStat server again and examine the output. Notice that the
“Device Type” has now changed to the actual Dallas Semiconductor device name (in
the cases where there is a named container—not all containers have named
containers, as you can see in the following output).
One wire devices connected:
Adapter: TINIExternalAdapter
- DS1920 – 700000004B8F1010
- DS1963S – 1C00000002795818
- DS2406 – CB00000017006112
- DS1990A – 21000007997D9F01
Adapter: TINIInternalAdapter
- Device type: 89 – 9F5E70005C8D1089

One drawback to this is that building in the device names also builds in a whole pile of other methods. Examine the last few lines of TINIConvertor and the last few lines of BuildDependency to look at the final file byte size. With TINIConvertor the Total Application Length is 2084 bytes; with BuildDependency the Total Application Length is 28575 bytes. Quite a big difference. This is because some of the containers we have included here have long dependency lists (lots of methods). Now that we have our server running, how can we make this server start automatically whenever the TINI is rebooted? There are two ways: 1. Add the java TiniWebStat.tini command to the .startup file; or 2. Replace slush with TiniWebStat.tbin (which requires running BuildDependency with the -l flag). First, let’s try adding this to the .startup file. Using FTP, get the /etc/.startup file.

C:\>ftp 192.168.1.85
Connected to 192.168.1.85.
220 Welcome to slush.
User (192.168.1.85:(none)): root
331 root login allowed. Password required.
Password:
230 User root logged in.
ftp> cd etc
250 CWD command successful.
ftp> get .startup
200 PORT Command successful.
150 ASCII connection open, getting .startup
226 Closing data connection.
ftp: 225 bytes received in 0.16Seconds 1.41Kbytes/sec.

ftp> quit
221 Goodbye.
C:\>
Now edit the .startup file using your favorite text editor (such as Notepad). Change
the file, adding a single line java  TiniWebStat.tini to the end of the file. Save the
file. Your .startup will now look like this:
#Autogen’d slush startup file
setenv FTPServer enable
setenv TelnetServer enable
setenv SerialServer enable
##
#Add user calls to setenv here:
##
initializeNetwork
#Add other user additions here:
java /TiniWebStat.tini
Now FTP the file back to your TINI:
C:\>ftp 192.168.1.85
Connected to 192.168.1.85.
220 Welcome to slush.                                                                        (Version 1.02)   Ready for user login.
User (192.168.1.85:(none)): root
331 root login allowed. Password required.
Password:
230 User root logged in.
ftp> cd etc
250 CWD command successful.
ftp> send .startup
200 PORT Command successful.
150 ASCII connection open, putting .startup
226 Closing data connection.
ftp: 247 bytes sent in 0.00Seconds 247000.00Kbytes/sec.
ftp> quit
221 Goodbye.
Through a Telnet session you can verify the file is there (use cat). Now reboot your TINI.
TINI /> reboot

Give your TINI a few seconds to reboot and then, using your web browser, check to see that the server is running again. You can also login to TINI and use the ps command to check the running processes:

TINI /> ps
3 processes
1: Java GC (Owner root)
2: init (Owner root)
3: /TiniWebStat.tini (Owner root)

That was fairly easy, no? Now let’s try running the server in place of Slush. Recompile the server into the .tbin format with BuildDependency, like this:

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

Using JavaKit, load the TiniWebStat.tbin file. After loading the file, don’t forget to press “E” at the loader prompt to exit the loader and start executing the TiniWebStat.tbin program. You will notice in the JavaKit window that the last line you see is “Application load complete.” At that point you can browse your web server. Remember, at this point we have replaced Slush with TiniWebStat so you cannot FTP or Telnet into your TINI. To login to your TINI you will need to use JavaKit to re-load Slush.tbin. Play with your new web server for a little while and than re-load Slush, as we are going to continue on with more new things.

Random Posts

Comments are closed.