[Tinyos-2-commits] CVS: tinyos-2.x/apps/BaseStation15.4 BaseStationC.nc, NONE, 1.1 BaseStationP.nc, NONE, 1.1 Makefile, NONE, 1.1 listen14_5.c, NONE, 1.1

Phil Levis scipio at users.sourceforge.net
Mon Jun 9 16:52:27 PDT 2008


Update of /cvsroot/tinyos/tinyos-2.x/apps/BaseStation15.4
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv26704/BaseStation15.4

Added Files:
	BaseStationC.nc BaseStationP.nc Makefile listen14_5.c 
Log Message:
Base station for 15.4 code. Receive only.


--- NEW FILE: BaseStationC.nc ---
// $Id: BaseStationC.nc,v 1.1 2008/06/09 23:52:25 scipio Exp $

/*									tab:4
 * "Copyright (c) 2000-2003 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */

/**
 * The TinyOS 2.x base station that forwards packets between the UART
 * and radio.It replaces the GenericBase of TinyOS 1.0 and the
 * TOSBase of TinyOS 1.1.
 *
 * <p>On the serial link, BaseStation sends and receives simple active
 * messages (not particular radio packets): on the radio link, it
 * sends radio active messages, whose format depends on the network
 * stack being used. BaseStation will copy its compiled-in group ID to
 * messages moving from the serial link to the radio, and will filter
 * out incoming radio messages that do not contain that group ID.</p>
 *
 * <p>BaseStation includes queues in both directions, with a guarantee
 * that once a message enters a queue, it will eventually leave on the
 * other interface. The queues allow the BaseStation to handle load
 * spikes.</p>
 *
 * <p>BaseStation acknowledges a message arriving over the serial link
 * only if that message was successfully enqueued for delivery to the
 * radio link.</p>
 *
 * <p>The LEDS are programmed to toggle as follows:</p>
 * <ul>
 * <li><b>RED Toggle:</b>: Message bridged from serial to radio</li>
 * <li><b>GREEN Toggle:</b> Message bridged from radio to serial</li>
 * <li><b>YELLOW/BLUE Toggle:</b> Dropped message due to queue overflow in either direction</li>
 * </ul>
 *
 * @author Phil Buonadonna
 * @author Gilman Tolle
 * @author David Gay
 * @author Philip Levis
 * @date August 10 2005
 */

configuration BaseStationC {
}
implementation {
  components MainC, BaseStationP, LedsC;
  components ActiveMessageC as Radio;
  components Serial802_15_4C as Serial;
  
  MainC.Boot <- BaseStationP;

  BaseStationP.RadioControl -> Radio;
  BaseStationP.SerialControl -> Serial;
  
  BaseStationP.UartSend -> Serial;
  
  BaseStationP.RadioReceive -> Radio.Receive;
  BaseStationP.RadioSnoop -> Radio.Snoop;
  BaseStationP.RadioPacket -> Radio;
  BaseStationP.RadioAMPacket -> Radio;
  
  BaseStationP.Leds -> LedsC;
}

--- NEW FILE: BaseStationP.nc ---
// $Id: BaseStationP.nc,v 1.1 2008/06/09 23:52:25 scipio Exp $

/*									tab:4
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2005 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */

/*
 * @author Phil Buonadonna
 * @author Gilman Tolle
 * @author David Gay
 * Revision:	$Id: BaseStationP.nc,v 1.1 2008/06/09 23:52:25 scipio Exp $
 */
  
/* 
 * BaseStationP bridges packets between a serial channel and the radio.
 * Messages moving from serial to radio will be tagged with the group
 * ID compiled into the TOSBase, and messages moving from radio to
 * serial will be filtered by that same group id.
 */

#include "AM.h"
#include "Serial.h"

module BaseStationP {
  uses {
    interface Boot;
    interface SplitControl as SerialControl;
    interface SplitControl as RadioControl;

    interface Send as UartSend;
    
    interface Receive as RadioReceive[am_id_t id];
    interface Receive as RadioSnoop[am_id_t id];
    interface Packet as RadioPacket;
    interface AMPacket as RadioAMPacket;

    interface Leds;
  }
}

implementation
{
  enum {
    UART_QUEUE_LEN = 12,
    RADIO_QUEUE_LEN = 12,
  };

  message_t  uartQueueBufs[UART_QUEUE_LEN];
  message_t  * ONE_NOK uartQueue[UART_QUEUE_LEN];
  uint8_t    uartIn, uartOut;
  bool       uartBusy, uartFull;

  task void uartSendTask();

  void dropBlink() {
    call Leds.led2Toggle();
  }

  void failBlink() {
    call Leds.led2Toggle();
  }

  event void Boot.booted() {
    uint8_t i;

    for (i = 0; i < UART_QUEUE_LEN; i++)
      uartQueue[i] = &uartQueueBufs[i];
    uartIn = uartOut = 0;
    uartBusy = FALSE;
    uartFull = TRUE;

    call RadioControl.start();
    call SerialControl.start();
  }

  event void RadioControl.startDone(error_t error) {
  }

  event void SerialControl.startDone(error_t error) {
    if (error == SUCCESS) {
      uartFull = FALSE;
    }
  }

  event void SerialControl.stopDone(error_t error) {}
  event void RadioControl.stopDone(error_t error) {}

  uint8_t count = 0;

  message_t* ONE receive(message_t* ONE msg, void* payload, uint8_t len);
  
  event message_t *RadioSnoop.receive[am_id_t id](message_t *msg,
						    void *payload,
						    uint8_t len) {
    return receive(msg, payload, len);
  }
  
  event message_t *RadioReceive.receive[am_id_t id](message_t *msg,
						    void *payload,
						    uint8_t len) {
    return receive(msg, payload, len);
  }

  message_t* receive(message_t *msg, void *payload, uint8_t len) {
    message_t *ret = msg;

    atomic {
      if (!uartFull)
	{
	  ret = uartQueue[uartIn];
	  uartQueue[uartIn] = msg;

	  uartIn = (uartIn + 1) % UART_QUEUE_LEN;
	
	  if (uartIn == uartOut)
	    uartFull = TRUE;

	  if (!uartBusy)
	    {
	      post uartSendTask();
	      uartBusy = TRUE;
	    }
	}
      else
	dropBlink();
    }
    
    return ret;
  }

  uint8_t tmpLen;
  
  task void uartSendTask() {
    uint8_t len;
    message_t* msg;

    atomic {
      if (uartIn == uartOut && !uartFull) {
	uartBusy = FALSE;
	return;
      }
    }

    msg = uartQueue[uartOut];
    tmpLen = len = call RadioPacket.payloadLength(msg);

    if (call UartSend.send(uartQueue[uartOut], len) == SUCCESS) {
      call Leds.led1Toggle();
    }
    else  {
      failBlink();
      post uartSendTask();
    }
  }

  event void UartSend.sendDone(message_t* msg, error_t error) {
    if (error != SUCCESS)
      failBlink();
    else
      atomic
	if (msg == uartQueue[uartOut])
	  {
	    if (++uartOut >= UART_QUEUE_LEN)
	      uartOut = 0;
	    if (uartFull)
	      uartFull = FALSE;
	  }
    post uartSendTask();
  }

}  

--- NEW FILE: Makefile ---
COMPONENT=BaseStationC
CFLAGS += -DCC2420_NO_ACKNOWLEDGEMENTS
CFLAGS += -DCC2420_NO_ADDRESS_RECOGNITION

include $(MAKERULES)


--- NEW FILE: listen14_5.c ---
#include <stdio.h>
#include <stdlib.h>

#include "serialsource.h"

static char *msgs[] = {
  "unknown_packet_type",
  "ack_timeout"	,
  "sync"	,
  "too_long"	,
  "too_short"	,
  "bad_sync"	,
  "bad_crc"	,
  "closed"	,
  "no_memory"	,
  "unix_error"
};

void stderr_msg(serial_source_msg problem)
{
  fprintf(stderr, "Note: %s\n", msgs[problem]);
}

enum {
  TOS_SERIAL_802_15_4_ID = 2,
};

int main(int argc, char **argv)
{
  serial_source src;

  if (argc != 3)
    {
      fprintf(stderr, "Usage: %s <device> <rate> - dump packets from a serial port\n", argv[0]);
      exit(2);
    }
  src = open_serial_source(argv[1], platform_baud_rate(argv[2]), 0, stderr_msg);
  if (!src)
    {
      fprintf(stderr, "Couldn't open serial port at %s:%s\n",
	      argv[1], argv[2]);
      exit(1);
    }
  for (;;)
    {
      int len, i, plen;
      short fcf;
      const unsigned char *packet = read_serial_packet(src, &len);
      int intraPan = 0;
      
      if (!packet)
	exit(0);
      else if (packet[0] != TOS_SERIAL_802_15_4_ID) {
	printf("bad packet (serial type is %02x, not %02x)\n", packet[0], TOS_SERIAL_802_15_4_ID);
      }

      plen = packet[1];
      printf("Received packet of length %i: \n", plen);
      if (plen != len) {
	printf("Packet format error: read packet length (%hhx) is different than expected from frame (%hhx).\n", plen, len);
      }
      
      i = 2;
      // Read in FCF and i+=2
      fcf = packet[i+1] << 8 | packet[i];
      i += 2;
      

      {
	if ((fcf & 0x7) == 0x01) {
	  printf("  Frame type: data\n");
	}
	else if ((fcf & 0x7) == 0x02) {
	  printf("  Frame type: acknowledgement\n");
	}
	else {
	  printf("  Frame type: other\n");
	}

	printf("  Security: %s\n", (fcf & (1 << 3)) ? "enabled":"disabled");
	printf("  Frame pending: %s\n", (fcf & (1 << 4)) ? "yes":"no");
	printf("  Ack request: %s\n", (fcf & (1 << 5)) ? "yes":"no");
	printf("  Intra-PAN: %s\n", (fcf & (1 << 6)) ? "yes":"no");
	intraPan = (fcf & (1 << 6));
      }


      {
	char seqno = packet[i++];
	printf("  Sequence number: %hhx\n", seqno);
      }
      
      {
	char addrLen = (fcf >> 10) & 0x3;
	short saddr = 0;
	long long laddr = 0;

	// 16- and 64-bit destinations have a PAN ID
	if (addrLen == 2 || addrLen == 3) { 
	  short destPan = packet[i++] << 8 | packet[i++];
	  printf("  Destination PAN: %hx\n", destPan);
	}
	
	switch (addrLen) {
	case 0:
	  printf("  Destination address: none\n");
	  break;
	case 1:
	  printf("  Destination address: invalid? (0x01)\n");
	  break;
	case 2:
	  saddr =  (packet[i++] << 8 | packet[i++]);
	  printf("  Destination address: %hx\n", saddr);
	  break;
	case 3: {
	  int j;
	  for (j = 0; j < 8; j++) {
	    laddr = laddr << 8;
	    laddr |= packet[i++];
	  }
	  printf("  Destination address: %llx\n", laddr);
	  break;
	}
	default:
	  printf("  Destination address: parse serror\n");
	}
      }

      
      {
	char addrLen = (fcf >> 14) & 0x3;
	short saddr = 0;
	long long laddr = 0;

	if (!intraPan) { // Intra-PAN packet
	  short srcPan = packet[i++] << 8 | packet[i++];
	  printf("  Source PAN: %hx\n", srcPan);
	}
	
	switch (addrLen) {
	case 0:
	  printf("  Source address: none\n");
	  break;
	case 1:
	  printf("  Source address: invalid? (0x01)\n");
	  break;
	case 2:
	  saddr =  (packet[i++] << 8 | packet[i++]);
	  printf("  Source address: %hx\n", saddr);
	  break;
	case 3: {
	  int j;
	  for (j = 0; j < 8; j++) {
	    laddr = laddr << 8;
	    laddr |= packet[i++];
	  }
	  printf("  Source address: %llx\n", laddr);
	  break;
	}
	default:
	  printf("  Source address: parse serror\n");
	}
      }
      
      printf("  AM type: %hhx\n", packet[i++]);

      if (i >= plen) {
	printf("Packet format error: read packet is shorter than expected.\n");
      }
      else {
	printf("  Payload: ");
	for (; i < plen; i++) {
	  printf("%hhx ", packet[i]);
	}
	printf("\n\n");
	putchar('\n');
      }
      free((void *)packet);
    }
}



More information about the Tinyos-2-commits mailing list