Tools:
The java directory contains a control GUI for the antitheft demo app.
+To run it, change to the java subdirectory and type:
+ make # Unecessary if antitheft.jar exists
+ java net.tinyos.sf.SerialForwarder -comm serial@<serial port>:<mote>
+ # e.g., java net.tinyps.sf.SerialForwarder -comm serial@/dev/ttyUSB0:mica2
+ # or java net.tinyps.sf.SerialForwarder -comm serial@COM2:telosb
+ ./run
+
+The buttons and text field on the right allow you to change the theft detection
+and reporting settings. The interval text box changes the interval at which
+motes check for theft (default is every second). Changes are only sent to the
+mote network when you press the Update button. Finally, if you've selected
+the Server theft report option, the message area will report received theft
+messages.
Known bugs/limitations:
+- A newly turned on mote may not send theft reports (when the "Server"
+ theft report option is chosen), as:
+ o It takes a little while after motes turn on for them to join the multihop
+ collection network.
+ o It can take a little while for motes to receive the current settings.
+
None.
import net.tinyos.util.*;
public class AntiTheftGui implements MessageListener, Messenger {
- MoteIF mote;
- JFrame frame;
- JTextArea mssgArea;
- JTextField fieldInterval;
+ MoteIF mote; // For talking to the antitheft root node
+
+ /* Various swing components we need to use after initialisation */
+ JFrame frame; // The whole frame
+ JTextArea mssgArea; // The message area
+ JTextField fieldInterval; // The requested check interval
+
+ /* The checkboxes for the requested settings */
JCheckBox detDarkCb, detAccelCb, repLedCb, repSirenCb, repServerCb,
repNeighboursCb;
public AntiTheftGui() {
try {
guiInit();
+ /* Setup communication with the mote and request a messageReceived
+ callback when an AlertMsg is received */
mote = new MoteIF(this);
mote.registerListener(new AlertMsg(), this);
}
}
}
+ /* Build up the GUI using Swing magic. Nothing very exciting here - the
+ BagPanel class makes the code a bit cleaner/easier to read. */
private void guiInit() throws Exception {
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setMinimumSize(new Dimension(500, 250));
mainPanel.setPreferredSize(new Dimension(500, 300));
+ /* The message area */
JScrollPane mssgPanel = new JScrollPane();
mssgPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
mssgPanel.setAutoscrolls(true);
mainPanel.add(mssgPanel, BorderLayout.CENTER);
mssgPanel.getViewport().add(mssgArea, null);
+ /* The button area */
BagPanel buttonPanel = new BagPanel();
GridBagConstraints c = buttonPanel.c;
fieldInterval = buttonPanel.makeTextField(10, null);
fieldInterval.setText(Integer.toString(Constants.DEFAULT_CHECK_INTERVAL));
- // Send settings button
ActionListener settingsAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateSettings();
mainPanel.add(buttonPanel, BorderLayout.EAST);
- // The frame part
+ /* The frame part */
frame = new JFrame("AntiTheft");
frame.setSize(mainPanel.getPreferredSize());
frame.getContentPane().add(mainPanel);
});
}
+ /* Add a message to the message area, auto-scroll to end */
+ public synchronized void message(String s) {
+ mssgArea.append(s + "\n");
+ mssgArea.setCaretPosition(mssgArea.getDocument().getLength());
+ }
+
+ /* Popup an error message */
void error(String msg) {
JOptionPane.showMessageDialog(frame, msg, "Error",
JOptionPane.ERROR_MESSAGE);
}
+ /* User pressed the "Update" button. Read the GUI fields and
+ send a SettingsMsg with the requested values. When the
+ requested settings are bad, we silently update them to sane
+ values. */
public void updateSettings() {
SettingsMsg smsg = new SettingsMsg();
short alert = 0;
short detect = 0;
int checkInterval = Constants.DEFAULT_CHECK_INTERVAL;
- /* Extract current interval value, ignoring bad values */
+ /* Extract current interval value, fixing bad values */
String intervalS = fieldInterval.getText().trim();
try {
int newInterval = Integer.parseInt(intervalS);
fieldInterval.setText("" + checkInterval);
}
+ /* Extract alert settings */
if (repLedCb.isSelected())
alert |= Constants.ALERT_LEDS;
if (repSirenCb.isSelected())
repLedCb.setSelected(true);
}
+ /* Extract detection settings */
if (detDarkCb.isSelected())
detect |= Constants.DETECT_DARK;
if (detAccelCb.isSelected())
detDarkCb.setSelected(true);
}
+ /* Build and send settings message */
smsg.set_alert(alert);
smsg.set_detect(detect);
smsg.set_checkInterval(checkInterval);
-
try {
mote.send(MoteIF.TOS_BCAST_ADDR, smsg);
}
}
}
- public static void main(String[] args) {
- AntiTheftGui me = new AntiTheftGui();
- }
-
- synchronized public void messageReceived(int dest_addr, Message msg) {
+ /* Message received from mote network. Update message area if it's
+ a theft message. */
+ public void messageReceived(int dest_addr, Message msg) {
if (msg instanceof AlertMsg) {
AlertMsg alertMsg = (AlertMsg)msg;
-
- String warning = "Theft of " + alertMsg.get_stolenId() + "\n";
- mssgArea.append(warning);
- mssgArea.setCaretPosition(mssgArea.getDocument().getLength());
+ message("Theft of " + alertMsg.get_stolenId());
}
}
- public void message(String s) {
- error(s);
+ /* Just start the app... */
+ public static void main(String[] args) {
+ AntiTheftGui me = new AntiTheftGui();
}
}