Hello Everyone:
   After I learned TinyOS tutorial Lesson 8, I decide to modify it so to
practice the nesC programming skill. I added Temp module to it so that the
SenseLightToLog application can sense temperature now. Then I modified the
BcastInject.java and changed the commands.
The problem is: When I run the application. The 'led_on' and
'led_off'command performs well, but the
'start_transfer' command did not work. It seems that the TOSBase received
messages, but the messageReceived() method has not been invoked. At first I
think the message may be wrong type or other reasons, but after I examine
the SimpleCmdM.nc, the LoggerRead.readDone() returned success and it seems
no problem there. I'm confused now. Can anybody help me?

Regards

Chao Sun


Here is the code, some implementation is omitted. All of the codes are in
the attachment.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


public class CommandCenter extends Thread{ // I modified the name to
CommandCenter, from original BcastInject

 static Properties p = new Properties();
 public static final byte LED_ON = 1;
 public static final byte LED_OFF = 2;
 public static final byte RADIO_LOUDER = 3;
 public static final byte RADIO_QUIETER = 4;
 public static final byte START_SENSING = 5;
 public static final byte STOP_SENSING = 6;
 public static final byte START_TRANSFER = 7;
 public static final byte STOP_TRANSFER = 8;

 public static final short TOS_BCAST_ADDR = (short) 0xffff;
 public static byte sequenceNo;
 public MoteIF mote = new MoteIF(PrintStreamMessenger.err);
 public SensingDaemon daemon;

 public static void usage() {
 }

 public static void startSensingUsage() {
 }

 public static void stopSensingUsage() {
 }

 public static void startTransferUsage() {
 }

 public static void stopTransferUsage() {
 }

 public static byte restoreSequenceNo() {
 }

 public static void saveSequenceNo(int i) {
 }

 public void run() {
   while(true) {
     String cmd;
     SensingCmdMsg packet = new SensingCmdMsg();
     boolean error = false; // tag if there are errors in the command
     boolean start_sensing = false;
     boolean stop_sensing = false;
     boolean start_transfer = false;
     boolean stop_transfer = false;

     // Set packet information
     sequenceNo = restoreSequenceNo();
     packet.set_seqno(sequenceNo);
     packet.set_hop_count((short)0);
     packet.set_source(0);

     // Print out the usage
     usage();

     System.err.println("Please input the command: ");
     InputStreamReader in = new InputStreamReader( System.in<http://system.in/>
);
     BufferedReader r = new BufferedReader(in);
     try {
   StringTokenizer input = new StringTokenizer(r.readLine ());
   cmd = input.nextToken();

   if(cmd.equals("led_on")) packet.set_action(LED_ON);
   else if(cmd.equals("led_off")) packet.set_action(LED_OFF);
   else if(cmd.equals("radio_louder")) packet.set_action(RADIO_LOUDER);
   else if(cmd.equals("radio_quieter")) packet.set_action(RADIO_QUIETER);
   else if(cmd.equals("start_sensing")) {
     if(input.countTokens() != 2) {
       startSensingUsage();
       error = true;
       break;
     }
     start_sensing = true;
     packet.set_action(START_SENSING);
     packet.set_args_ss_args_destaddr((int) Integer.parseInt(
input.nextToken()));
     packet.set_args_ss_args_sensing_interval((long)Integer.parseInt(
input.nextToken()));

   } else if(cmd.equals("stop_sensing")) {
     if(input.countTokens() != 1) {
       stopSensingUsage();
       error = true;
       break;
     }
     stop_sensing = true;
     packet.set_action(STOP_SENSING);
     packet.set_args_ss_args_destaddr ((int) Integer.parseInt(
input.nextToken()));
   } else if(cmd.equals("start_transfer")) {
     if(input.countTokens() != 2) {
       startTransferUsage();
       error = true;
       break;
     }
     start_transfer = true;
     packet.set_action(START_TRANSFER);
     packet.set_args_st_args_destaddr((int) Integer.parseInt(
input.nextToken()));
     packet.set_args_st_args_radio_interval((long)Integer.parseInt(
input.nextToken()));
     System.err.println("Get Here!");
   } else if(cmd.equals("stop_transfer")) {
     if(input.countTokens() != 1) {
       stopTransferUsage();
       error = true;
       break;
     }
     stop_transfer = true;
     packet.set_action(STOP_TRANSFER);
     packet.set_args_st_args_destaddr ((int) Integer.parseInt(
input.nextToken()));
   } else {
     usage();
     error = true;
   }

   if(error == false) {


     // TODO: Change to  uni-cast here.
     mote.send(TOS_BCAST_ADDR, packet);
     System.err.println("packet sended");

     if(start_transfer) {
       daemon = new SensingDaemon();
       mote.registerListener(new LogMsg(), daemon);
       System.err.println("Listener registered");
       daemon.run();
     }

     saveSequenceNo(sequenceNo+1);
   } // end of if(error == false)
     } catch(Exception e) {
   e.printStackTrace();
     }
   }
 }

 public static void main(String args[]) {
   CommandCenter cc = new CommandCenter();
   cc.run();
 }

 private class SensingDaemon extends Thread implements MessageListener{
   public boolean read_log_done = false;

   public SensingDaemon() {
     this.setDaemon(true);
   }

   public void run() {
     while(true) {
   try {
     synchronized(this) {
       if(read_log_done == false) {
         System.err.println("Waiting for response to read log...");
         this.wait(10000);
       }
       if(read_log_done == false) {
         System.err.println("Warning: Time out while waiting for sensing
data");
       }
     }
     saveSequenceNo(sequenceNo+1);
   } catch(InterruptedException ie) {
     System.err.println("Error: Something unexpected interrupted!");
     ie.printStackTrace();
   }
     }
   }

   /* (non-Javadoc)
    * @see net.tinyos.message.MessageListener#messageReceived(int,
net.tinyos.message.Message)
    */
   public void messageReceived(int to, Message m) {
     System.err.println("Message received");
     LogMsg lm = (LogMsg) m;
     System.err.println("Received log message: "+lm);

     System.err.print("Log values: ");
     for (int i = 0; i < lm.numElements_log(); i++) {
   short val = lm.getElement_log(i);
   System.err.print(Integer.toHexString((int)val)+" ");
     }
     System.err.println("");

     synchronized (this) {
   read_log_done = true;
   this.notifyAll();
     }
   }
 }
}

Attachment: SensingDemo.tar.gz
Description: GNU Zip compressed data

_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to