00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package sensix;
00036
00037 import java.net.*;
00038 import java.io.*;
00039 import java.util.*;
00040 import sensix.discovery.*;
00041
00042
00043 public class TinyOSClient
00044 implements Runnable, LevelOneClient, net.tinyos.message.MessageListener
00045 {
00046 protected Logger logger;
00047 protected SensixNetworking snet;
00048 protected SensixMarshalling marshal;
00049 protected ThreadGroup my_grp;
00050 protected SenseServer parent;
00051 protected String serial_port;
00052 protected Thread t;
00053 protected TaskTracking track;
00054 protected String simulator;
00055 protected net.tinyos.message.MoteIF mote;
00056
00057
00058 class TosMessenger implements net.tinyos.util.Messenger {
00059 public void message(String message) {
00060 log(Logger.LOG.DEBUG, message);
00061 }
00062 }
00063
00064
00065 public TinyOSClient(SenseServer serv, String app, byte debug, String sim) {
00066 this(serv, "/dev/ttyS0", app, debug, sim);
00067 }
00068
00069 public TinyOSClient(SenseServer serv, String serial, String app,
00070 byte debug, String sim) {
00071 t = new Thread(new ThreadGroup("tinyos"), this, "tinyos_proc");
00072 parent = serv;
00073 marshal = serv.marshal;
00074 snet = serv.net;
00075 serial_port = serial;
00076 initLogger(app, debug);
00077 track = new TaskTracking();
00078 simulator = sim;
00079
00080 t.start();
00081 }
00082
00083
00084 public void run() {
00085 my_grp = t.getThreadGroup();
00086 startLevelOneInterface();
00087 }
00088
00089
00090 public void startLevelOneInterface() {
00091 String default_baud = ":38400";
00092 String portname = "serial@" + serial_port;
00093
00094 if (portname.indexOf(':') == -1)
00095 portname += default_baud;
00096 if (simulator != "")
00097 portname = "tossim-radio@" + simulator;
00098
00099 int group_id = 125;
00100 TosMessenger msgr = new TosMessenger();
00101 net.tinyos.packet.PhoenixSource source =
00102 net.tinyos.packet.BuildSource.makePhoenix(portname, msgr);
00103 net.tinyos.message.MoteIF mote =
00104 new net.tinyos.message.MoteIF(source, group_id);
00105 mote.registerListener(new SensixMsg(), this);
00106 mote.start();
00107
00108 log(Logger.LOG.DEBUG, "Spawning TinyOS interface at " + portname);
00109 }
00110
00111 public void messageReceived(int dest, net.tinyos.message.Message m) {
00112 if (!(m instanceof SensixMsg))
00113 return;
00114
00115 SensixMsg msg = (SensixMsg)m;
00116 byte[] data = new byte[SensixMsg.DEFAULT_MESSAGE_SIZE];
00117 for (int i = 0; i < data.length; i++)
00118 data[i] = (byte)msg.getElement_bytes(i);
00119 marshal.parse_packet(data, data.length);
00120 }
00121
00122
00123 public void initLogger(String app, byte debug_level) {
00124 logger = new Logger(app, debug_level);
00125 }
00126
00127 public void log(Logger.LOG type, String msg) {
00128 logger.log(type, msg);
00129 }
00130
00131 public void shutdown() {
00132 log(Logger.LOG.DEBUG, "TinyOS shutdown");
00133 my_grp.interrupt();
00134 }
00135
00136
00137
00138 public int identity() {
00139 return snet.identifier();
00140 }
00141
00142 public double check_energy() {
00143 return 1.0;
00144 }
00145
00146 public TaskTracking tracker() {
00147 return track;
00148 }
00149
00150 public SortedMap<Byte,Capability> capabilities() {
00151 return parent.local_capabilities;
00152 }
00153
00154
00155 public void send_up(int what, int size, byte msg[]) {
00156 parent.send_up(what, size, msg);
00157 }
00158
00159 public void bcast(int what, int size, byte msg[]) {
00160 parent.bcast(what, size, msg);
00161 }
00162
00163 public void send_dn(int what, int size, byte msg[]) {
00164 byte[] hdr = marshal.load_header((byte)(what & 0xff),
00165 (byte)(size & 0xff));
00166 short[] total = new short[size + SensixMarshalling.HEADER_SIZE];
00167 SensixMarshalling.byte2short_arraycopy(hdr, 0, total, 0,
00168 SensixMarshalling.HEADER_SIZE);
00169 SensixMarshalling.byte2short_arraycopy(msg, 0, total,
00170 SensixMarshalling.HEADER_SIZE,
00171 size);
00172 if (mote != null) {
00173 try {
00174 SensixMsg sm = new SensixMsg();
00175 for (int i = 0; i < total.length; i++)
00176 sm.setElement_bytes(i, total[i]);
00177 mote.send(net.tinyos.message.MoteIF.TOS_BCAST_ADDR,
00178 (net.tinyos.message.Message)sm);
00179 } catch (java.io.IOException e) {
00180 log(Logger.LOG.ERROR, "Error sending to TinyOS nodes: " + e);
00181 }
00182 }
00183 }
00184
00185 public void apply(Functor f) {
00186 parent.apply(f);
00187 }
00188
00189 public void cancel(Functor f) {
00190 parent.cancel(f);
00191 }
00192 }