[Tinyos-2-commits] CVS: tinyos-2.x/tos/lib/net/zigbee/ieee802154/includes PrintfUART.h, NONE, 1.1 frame_format.h, NONE, 1.1 mac_func.h, NONE, 1.1 nwk_func.h, NONE, 1.1

André Cunha a_cunha at users.sourceforge.net
Mon Feb 11 09:50:34 PST 2008


Update of /cvsroot/tinyos/tinyos-2.x/tos/lib/net/zigbee/ieee802154/includes
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv29875/includes

Added Files:
	PrintfUART.h frame_format.h mac_func.h nwk_func.h 
Log Message:


--- NEW FILE: PrintfUART.h ---
/*
 * Copyright (c) 2005
 *	The President and Fellows of Harvard College.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* 
 * Writes printf like output to the UART.  
 * This works only on the AVR and MSP430 Microcontrollers!
 * <p>
 * Note: For AVR we explicitly place the print statements in ROM; for
 * MSP430 this is done by default!  For AVR, if we don't place it
 * explicitely in ROM, the statements will go in RAM, which will
 * quickly cause a descent size program to run out of RAM.  By default
 * it doesn't disable the interupts; disabling the interupts when
 * writing to the UART, slows down/makes the mote quite unresponsive,
 * and can lead to problems!  If you wish to disable all printfs to
 * the UART, then comment the flag: <code>PRINTFUART_ENABLED</code>.

 * <p> <pre>
 * How to use:
 *   // (1) Call printfUART_init() from your initialization function 
 *   //     to initialize the UART
 *   printfUART_init();
 *   // (2) Set your UART client to the correct baud rate.  Look at 
 *   //     the comments in printfUART_init(), to figure out what 
 *   //     baud to use for your particular mote
 *
 *   // (3) Send printf statements like this:
 *   printfUART("Hello World, we are in year= %i\n", 2004);
 *
 * Examples and caveats:
 *   // (1) - If no parameters are passed, then the second quotes 
 *            are required because of how the macro is defined
 *   printfUART("Timer fired\n", ""); 
 *   // (2) - Must use curly braces in single section statements
 *   if (x < 3)
 *       {printfUART("The value of x is %i\n", x);}
 *   // (3) - Otherwise it more or less works like regular printf
 *   printfUART("\nThe value of x=%i, and y=%i\n", x, y); 
 * </pre>
 * <pre>URL: http://www.eecs.harvard.edu/~konrad/projects/motetrack</pre>
 * @author Konrad Lorincz
 * @version 2.0, January 5, 2005
 */
#ifndef PRINTFUART_H
#define PRINTFUART_H
#include <stdarg.h>
//#include <stdio.h>

// Comment out the line below to DISABLE printf statements.
//#define PRINTFUART_ENABLED


// -------------------------------------------------------------------
#ifdef PRINTFUART_ENABLED
    #define DEBUGBUF_SIZE 256
    char debugbuf[DEBUGBUF_SIZE];
    char debugbufROMtoRAM[DEBUGBUF_SIZE];

    #if defined(PLATFORM_MICAZ) || defined(PLATFORM_MICA2) || defined(PLATFORM_MICA2DOT)
        #define printfUART(__format, __args...) {                  \
            static const char strROM[] PROGMEM = __format;         \
            strcpy_P((char*) &debugbufROMtoRAM, (PGM_P) &strROM);  \
            sprintf(debugbuf, debugbufROMtoRAM, __args);           \
            writedebug();                                          \
        }   
    #else  // assume MSP430 architecture (e.g. TelosA, TelosB, etc.)
        #define printfUART(__format, __args...) {      \
            sprintf(debugbuf, __format, __args);       \
            writedebug();                              \
        }   
    #endif
#else
    #define printfUART(__format, __args...)
    void printfUART_init() {}
#endif

#define NOprintfUART(__format, __args...)


/** Used for terminating the program from a non-nesc file. Calling <code>exit(1)</code> 
 *  from a non-nesc file doesn't terminate the program immeditely
 */
static int EXIT_PROGRAM = 0;  



// -------------------------------------------------------------------
#ifdef PRINTFUART_ENABLED

/**
 * Initialize the UART port.  Call this from your startup routine.
 */
#define printfUART_init() {atomic printfUART_init_private();}
void printfUART_init_private()
{
    #if defined(PLATFORM_MICAZ) || defined(PLATFORM_MICA2)
        // 56K baud
        outp(0,UBRR0H);
        outp(15, UBRR0L);                              //set baud rate
        outp((1<<U2X),UCSR0A);                         // Set UART double speed
        outp(((1 << UCSZ1) | (1 << UCSZ0)) , UCSR0C);  // Set frame format: 8 data-bits, 1 stop-bit
        inp(UDR0);
        outp((1 << TXEN) ,UCSR0B);   // Enable uart reciever and transmitter

    #else
      #if defined(PLATFORM_MICA2DOT)  
        // 19.2K baud
        outp(0,UBRR0H);            // Set baudrate to 19.2 KBps
        outp(12, UBRR0L);
        outp(0,UCSR0A);            // Disable U2X and MPCM
        outp(((1 << UCSZ1) | (1 << UCSZ0)) , UCSR0C);
        inp(UDR0);
        outp((1 << TXEN) ,UCSR0B);
  
      #else  // assume TelosA, TelosB, etc.
        //9.6K baud
        uint16_t l_br = 0;
        uint8_t l_mctl = 0;
        uint8_t l_ssel = 0;


        TOSH_SEL_UTXD1_MODFUNC();
        TOSH_SEL_URXD1_MODFUNC();


        UCTL1 = SWRST;  
        UCTL1 |= CHAR;  // 8-bit char, UART-mode
    
        U1RCTL &= ~URXEIE;  // even erroneous characters trigger interrupts

        UCTL1 = SWRST;
        UCTL1 |= CHAR;  // 8-bit char, UART-mode

        if (l_ssel & 0x80) {
            U1TCTL &= ~(SSEL_0 | SSEL_1 | SSEL_2 | SSEL_3);
            U1TCTL |= (l_ssel & 0x7F); 
        }
        else {
            U1TCTL &= ~(SSEL_0 | SSEL_1 | SSEL_2 | SSEL_3);
            U1TCTL |= SSEL_ACLK; // use ACLK, assuming 32khz
        }

        if ((l_mctl != 0) || (l_br != 0)) {
            U1BR0 = l_br & 0x0FF;
            U1BR1 = (l_br >> 8) & 0x0FF;
            U1MCTL = l_mctl;
        }
        else {
            U1BR0 = 0x03;   // 9600 baud
            U1BR1 = 0x00;
            U1MCTL = 0x4A;
        }
      
        ME2 &= ~USPIE1;   // USART1 SPI module disable
        ME2 |= (UTXE1 | URXE1);   // USART1 UART module enable
      
        U1CTL &= ~SWRST;
    
        IFG2 &= ~(UTXIFG1 | URXIFG1);
        IE2 &= ~(UTXIE1 | URXIE1);  // interrupt disabled

      #endif
    #endif
}

#if defined(PLATFORM_MICAZ) || defined(PLATFORM_MICA2) || defined(PLATFORM_MICA2DOT)
#else // assume AVR architecture (e.g. TelosA, TelosB)
    bool isTxIntrPending()
    {
        if (U1TCTL & TXEPT) {
            return TRUE;
        }
        return FALSE;
    }
#endif

/**
 * Outputs a char to the UART.
 */
void UARTPutChar(char c)
{
    if (c == '\n')
        UARTPutChar('\r');


    #if defined(PLATFORM_MICAZ) || defined(PLATFORM_MICA2) || defined(PLATFORM_MICA2DOT)
        loop_until_bit_is_set(UCSR0A, UDRE);
        outb(UDR0,c);
    #else // assume AVR architecture (e.g. TelosA, TelosB)
        U1TXBUF = c;  
        while( !isTxIntrPending() )  
            continue;
    #endif
}

/**
 * Outputs the entire debugbuf to the UART, or until it encounters '\0'.
 */
void writedebug()
{
    uint16_t i = 0;
    
    while (debugbuf[i] != '\0' && i < DEBUGBUF_SIZE)
        UARTPutChar(debugbuf[i++]);
}



/**
 * Simplified sprintf
 */
#define SCRATCH 16
int sprintf(uint8_t *buf, const uint8_t *format, ...)
{
    uint8_t scratch[SCRATCH];
    uint8_t format_flag;
    uint32_t u_val=0, base;//modified by Andre Cunha
    uint8_t *ptr;
    va_list ap;

   //memset(scratch, 0, SCRATCH);
	
    buf[0] = '\0';  // KLDEBUG - fixes subtle bug ...
    va_start (ap, format);
    for (;;){
        while ((format_flag = *format++) != '%'){      // Until '%' or '\0'
            if (!format_flag) {va_end (ap); return (0);}
            *buf = format_flag; buf++; *buf=0;
        }

        switch (format_flag = *format++){

        case 'c':
            format_flag = va_arg(ap,int);
        default:
            *buf = format_flag; buf++; *buf=0;
            continue;
        case 'S':
        case 's':
            ptr = va_arg(ap,char *);
            strcat(buf, ptr);
            continue;
        case 'o':
            base = 8;
            *buf = '0'; buf++; *buf=0;
            goto CONVERSION_LOOP;
        case 'i':
            if (((int)u_val) < 0){
                u_val = - u_val;
                *buf = '-'; buf++; *buf=0;
            }
			base = 10;
            goto CONVERSION_LOOP;
            // no break -> run into next case
		 case 'd'://added by Andre Cunha
            if (((int32_t)u_val) < 0){
                u_val = - u_val;
                *buf = '-'; buf++; *buf=0;
            }
            
			base = 10;
            goto CONVERSION_LOOP32;
        case 'u':
            base = 10;
            goto CONVERSION_LOOP;
        case 'x':
            base = 16;
			goto CONVERSION_LOOP;
			
		case 'y'://unsigned int 32 bits hexadecimal//added by Andre Cunha
            base = 16;
			goto CONVERSION_LOOP32;
			
        CONVERSION_LOOP:
            u_val = va_arg(ap,int);
            ptr = scratch + SCRATCH;
            *--ptr = 0;
            do {
                char ch = u_val % base + '0';
                if (ch > '9')
                    ch += 'a' - '9' - 1;
                *--ptr = ch;
                u_val /= base;
            } while (u_val);
            strcat(buf, ptr);
            buf += strlen(ptr);
			break;
			
		CONVERSION_LOOP32:
            u_val = va_arg(ap,int32_t);
            ptr = scratch + SCRATCH;
            *--ptr = 0;
            do {
                char ch = u_val % base + '0';
                if (ch > '9')
                    ch += 'a' - '9' - 1;
                *--ptr = ch;
                u_val /= base;
            } while (u_val);
            strcat(buf, ptr);
            buf += strlen(ptr);
        }
    }
}


#endif  // PRINTFUART_ENABLED
// -------------------------------------------------------------------

#endif  // PRINTFUART_H


--- NEW FILE: frame_format.h ---
/*
 * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
 * @author open-zb http://www.open-zb.net
 * @author Andre Cunha
 */

//MAC frame Superstructure

#ifndef __FRAME_FORMAT__
#define __FRAME_FORMAT__


#define MPDU_HEADER_LEN 5

typedef struct MPDU
{
	uint8_t length;
	//info on frame type/ack/etc
	uint8_t frame_control1;
	//info on addressing fields
	uint8_t frame_control2;
	//uint16_t frame_control;
	uint8_t seq_num;
	uint8_t data[120];
}MPDU;

typedef struct MPDUBuffer
{
	uint8_t length;
	//uint16_t frame_control;
	uint8_t frame_control1;
	uint8_t frame_control2;
	uint8_t seq_num;
	uint8_t data[120];
	uint8_t retransmission;
	uint8_t indirect;
}MPDUBuffer;

//PD_DATA validation structures



/*****************************************************/
/*				BEACON FRAME SCTRUCTURES			 */
/*****************************************************/

//#define beacon_addr_short_length 7
//#define beacon_addr_long_length 12


typedef struct beacon_addr_short
{
	uint16_t destination_PAN_identifier;
	uint16_t destination_address;
	uint16_t source_address;
	uint16_t superframe_specification;
}beacon_addr_short;
/*
typedef struct beacon_struct
{
	uint8_t length;
	uint16_t frame_control;
	uint8_t seq_num;
	uint16_t source_PAN_identifier;
	uint16_t destination_address;
	uint16_t source_address;
	uint16_t superframe_specification;
}beacon_struct;
*/
/*
typedef struct beacon_addr_long
{
	uint16_t source_PAN_identifier;
	uint32_t source_address0;
	uint32_t source_address1;
	uint16_t superframe_specification;
}beacon_addr_long;
*/
/*****************************************************/
/*				ACK FRAME Structures 				 */
/*****************************************************/

typedef struct ACK
{
	uint8_t length;
	uint8_t frame_control1;
	uint8_t frame_control2;
	//uint16_t frame_control;
	uint8_t seq_num;
}ACK;

/*****************************************************/
/*				COMMAND FRAME Structures 			 */
/*****************************************************/

typedef struct cmd_association_request
{
	uint8_t command_frame_identifier;
	uint8_t capability_information;
}cmd_association_request;

typedef struct cmd_association_response
{
	uint8_t command_frame_identifier;
	uint8_t short_address1;
	uint8_t short_address2;
	//uint16_t short_address;
	uint8_t association_status;
}cmd_association_response;

//disassociacion notification command structure pag. 126
typedef struct cmd_disassociation_notification
{
	uint16_t destination_PAN_identifier;
	uint32_t destination_address0;
	uint32_t destination_address1;
	uint16_t source_PAN_identifier;
	uint32_t source_address0;
	uint32_t source_address1;
	uint8_t command_frame_identifier;
	uint8_t disassociation_reason;
}cmd_disassociation_notification;

//pag 130
typedef struct cmd_beacon_request
{
	uint16_t destination_PAN_identifier;
	uint16_t destination_address;
	uint8_t command_frame_identifier;
}cmd_beacon_request;


//pag 132
typedef struct cmd_gts_request
{
	uint16_t source_PAN_identifier;
	uint16_t source_address;
	uint8_t command_frame_identifier;
	uint8_t gts_characteristics;
}cmd_gts_request;

typedef struct cmd_default
{
	uint8_t command_frame_identifier;
}cmd_default;


//131
typedef struct cmd_coord_realignment
{
	uint8_t command_frame_identifier;
	uint8_t PAN_identifier0;
	uint8_t PAN_identifier1;
	uint8_t coordinator_short_address0;
	uint8_t coordinator_short_address1;
	
	/*
	uint16_t PAN_identifier;
	uint16_t coordinator_short_address;
	*/
	uint8_t logical_channel;
	uint16_t short_address;
}cmd_coord_realignment;



/*******************************************************/
/*     			ADDRESSING FIELDS ONLY				   */
/*******************************************************/
#define DEST_SHORT_LEN 4
#define DEST_LONG_LEN 10
#define INTRA_PAN_SOURCE_SHORT_LEN 2
#define INTRA_PAN_SOURCE_LONG_LEN 8
#define SOURCE_SHORT_LEN 4
#define SOURCE_LONG_LEN 10


//DESTINATION
typedef struct dest_short
{
	uint16_t destination_PAN_identifier;
	uint16_t destination_address;
}dest_short;

typedef struct dest_long
{
	uint16_t destination_PAN_identifier;
	uint32_t destination_address0;
	uint32_t destination_address1;
}dest_long;

//SOURCE
typedef struct intra_pan_source_short
{
	uint16_t source_address;
}intra_pan_source_short;

typedef struct intra_pan_source_long
{
	uint32_t source_address0;
	uint32_t source_address1;
}intra_pan_source_long;


typedef struct source_short
{
	uint16_t source_PAN_identifier;
	uint16_t source_address;
}source_short;


typedef struct source_long
{
	uint16_t source_PAN_identifier;
	uint32_t source_address0;
	uint32_t source_address1;
}source_long;

#endif


--- NEW FILE: mac_func.h ---
/*
 * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
 * @author open-zb http://www.open-zb.net
 * @author Andre Cunha
 */


#ifndef __MAC_FUNC__
#define __MAC_FUNC__

/*******************************************************************************************************************/ 

uint8_t set_capability_information(uint8_t alternate_PAN_coordinator, uint8_t device_type, uint8_t power_source, uint8_t receiver_on_when_idle, uint8_t security, uint8_t allocate_address)
{
	
	return ((allocate_address << 7 ) | (security << 6 ) | (receiver_on_when_idle << 3 ) | (power_source << 2 ) | ( device_type << 1 ) | (alternate_PAN_coordinator << 0) );
}

uint8_t get_alternate_PAN_coordinator(uint8_t capability_information)
{

if ( (capability_information & 0x01) == 0x01)
	return 1;
else
	return 0;

}

  
/*******************************************************************************************************************/  
/********************************FRAME CONTROL FUNCTIONS************************************************************/
/*******************************************************************************************************************/
  
 //build MPDU frame control field
uint16_t set_frame_control(uint8_t frame_type,uint8_t security,uint8_t frame_pending,uint8_t ack_request,uint8_t intra_pan,uint8_t dest_addr_mode,uint8_t source_addr_mode) 
{
	  uint8_t fc_b1=0;
	  uint8_t fc_b2=0;
  	  fc_b1 = ( (intra_pan << 6) | (ack_request << 5) | (frame_pending << 4) |
 	   		  (security << 3) | (frame_type << 0) );				  
	  fc_b2 = ( (source_addr_mode << 6) | (dest_addr_mode << 2));
	  return ( (fc_b2 << 8 ) | (fc_b1 << 0) );

} 


//return the type of destination address specified in the frame control 

uint8_t get_fc2_dest_addr(uint8_t frame_control)
{
	switch( frame_control & 0xC )
	{
	case 0x4:	return RESERVED_ADDRESS; 
							break;
	case 0x8: return SHORT_ADDRESS;
						 break;
	case 0xC: return LONG_ADDRESS;
						break;
	default:
			return 0; 
			break;
	}
}


//return the type of source address specified in the frame control 

uint8_t get_fc2_source_addr(uint8_t frame_control)
{
	switch(frame_control & 0xC0 )
	{
	case 0x40:	return RESERVED_ADDRESS; 
							break;
	case 0x80: return SHORT_ADDRESS;
						 break;
	case 0xC0: return LONG_ADDRESS;
						break;
	default:
			return 0; 
			break;
	}
}



bool get_fc1_security(uint8_t frame_control)
{

if ( (frame_control & 0x8) == 0x8)
	return 1;
else
	return 0;

}

bool get_fc1_frame_pending(uint8_t frame_control)
{

if ( (frame_control & 0x10) == 0x10)
	return 1;
else
	return 0;
	
}

bool get_fc1_ack_request(uint8_t frame_control)
{

if ( (frame_control & 0x20) == 0x20)
	return 1;
else
	return 0;
	
}

bool get_fc1_intra_pan(uint8_t frame_control)
{

if ( (frame_control & 0x40) == 0x40)
	return 1;
else
	return 0;
	
} 
 
  
/*******************************************************************************************************************/  
/********************************SUPERFRAME SPECIFICATION FUNCTIONS*************************************************/
/*******************************************************************************************************************/

//build beacon superframe specification
uint16_t set_superframe_specification(uint8_t beacon_order,uint8_t superframe_order,uint8_t final_cap_slot,uint8_t battery_life_extension,uint8_t pan_coordinator,uint8_t association_permit)
{
	  uint8_t sf_b1=0;
	  uint8_t sf_b2=0;
	  sf_b1 = ( (superframe_order << 4) | (beacon_order <<0));
	  sf_b2 = ( (association_permit << 7) | (pan_coordinator << 6) |
	  		    (battery_life_extension << 4) | (final_cap_slot << 0) );
	   return  ( (sf_b2 <<8 ) | (sf_b1 << 0) );  
   
}

uint8_t get_beacon_order(uint16_t superframe)
{
	return ((uint8_t)superframe &  0xF);
}

uint8_t get_superframe_order(uint16_t superframe)
{
	return (((uint8_t)superframe >> 4) &  0xF);
}



bool get_pan_coordinator(uint16_t superframe)
{
if ( ((uint8_t)superframe & 0x40) == 0x40)
	return 1;
else
	return 0;
	
}

bool get_association_permit(uint16_t superframe)
{
if ( ((uint8_t)superframe & 0x80) == 0x80)
	return 1;
else
	return 0;	
}

bool get_battery_life_extention(uint16_t superframe)
{
if ( ((uint8_t)superframe & 0x10) == 0x10)
	return 1;
else
	return 0;	
}

uint8_t get_final_cap_slot(uint16_t superframe)
{
return (((uint8_t)superframe >> 4) &  0xF);
}


/*******************************************************************************************************************/  
/********************************      DATA TX OPTIONS   ************************************************************/
/*******************************************************************************************************************/
  
  
uint8_t set_txoptions(uint8_t ack, uint8_t gts, uint8_t indirect_transmission,uint8_t security)
{
return ( (ack << 0) | (gts << 1) | (indirect_transmission << 2) | (security << 3 ) );
}  
  
bool get_txoptions_ack(uint8_t txoptions)
{

if ( (txoptions & 0x1) == 0x1)
	return 1;
else
	return 0;

}
  
bool get_txoptions_gts(uint8_t txoptions)
{

if ( (txoptions & 0x2) == 0x2)
	return 1;
else
	return 0;

}  

bool get_txoptions_indirect_transmission(uint8_t txoptions)
{

if ( (txoptions & 0x4) == 0x4)
	return 1;
else
	return 0;

}  

bool get_txoptions_security(uint8_t txoptions)
{

if ( (txoptions & 0x8) == 0x8)
	return 1;
else
	return 0;
}


//BEACON SCHEDULING IMPLEMENTATION
bool get_txoptions_upstream_buffer(uint8_t txoptions) 
{

if ( (txoptions & 0x10) == 0x10)
	return 1;
else
	return 0;
}

uint8_t set_txoptions_upstream(uint8_t ack, uint8_t gts, uint8_t indirect_transmission,uint8_t security,uint8_t upstream)
{
return ( (ack << 0) | (gts << 1) | (indirect_transmission << 2) | (security << 3 ) | (upstream << 4) );
} 


/*******************************************************************************************************************/  
/********************************PENDING ADDRESSES FUNCTIONS********************************************************/
/*******************************************************************************************************************/
uint8_t set_pending_address_specification(uint8_t number_short, uint8_t number_extended)
{
	return ( (number_extended << 4) | (number_short << 0) );
}

uint8_t get_number_short(uint8_t pending_specification)
{
	return (pending_specification & 0x07);
}

uint8_t get_number_extended(uint8_t pending_specification)
{
	return ( (pending_specification >> 4) & 0x07);
}


/*******************************************************************************************************************/  
/********************************GTS FIELDS FUNCTIONS***************************************************************/
/*******************************************************************************************************************/
uint8_t set_gts_specification(uint8_t gts_descriptor_count, uint8_t gts_permit)
{
	return ( ( gts_descriptor_count << 0) | (gts_permit << 7) );  
}

uint8_t get_gts_permit(uint8_t gts_specification)
{
return ( (gts_specification >> 7) & 0x01);
}


///UNUSED
uint8_t set_gts_directions(uint8_t gts1,uint8_t gts2,uint8_t gts3,uint8_t gts4,uint8_t gts5,uint8_t gts6,uint8_t gts7)
{
	return ((gts1 << 0) | (0 << 7));
}


uint8_t set_gts_descriptor(uint8_t GTS_starting_slot, uint8_t GTS_length)
{
//part of the descriptor list
	return ( (GTS_starting_slot << 0) | (GTS_length << 4) );
}

uint8_t get_gts_descriptor_len(uint8_t gts_des_part)
{
	return ( (gts_des_part & 0xf0) >> 4);
}

uint8_t get_gts_descriptor_ss(uint8_t gts_des_part)
{
	return (gts_des_part & 0x0f);
}



/************************************************************************************************/  
/********************************GTS CHARACTERISTICS*************************************************/
/************************************************************************************************/
uint8_t set_gts_characteristics(uint8_t gts_length, uint8_t gts_direction, uint8_t characteristic_type)
{
	return ( (gts_length << 0) | (gts_direction << 4) | (characteristic_type << 5));
}  
 
 
uint8_t get_gts_length(uint8_t gts_characteristics)
{
	return (gts_characteristics &  0xF);
}

bool get_gts_direction(uint8_t gts_characteristics)
{
	if ( (gts_characteristics & 0x10) == 0x10)
		return 1;
	else
		return 0;
}  

uint8_t get_characteristic_type(uint8_t gts_characteristics)
{
	if ( (gts_characteristics & 0x20) == 0x20)
		return 1;
	else
		return 0;
} 


/************************************************************************************************/  
/********************************OTHER FUNCTIONS*************************************************/
/************************************************************************************************/
  /* A Task to calculate CRC for message transmission */
  /*
  task void CRCCalc() {
    uint16_t length = txLength;
    uint16_t crc = calcrc(sendPtr, length - 2);
    
    sendPtr[length - 2] = crc & 0xff;
    sendPtr[length - 1] = (crc >> 8) & 0xff;
    
  }
  */
  
uint16_t get_crcByte(uint16_t crc, uint8_t b)
{
  uint8_t i;
  
  crc = crc ^ b << 8;
  i = 8;
  do
    if (crc & 0x8000)
      crc = crc << 1 ^ 0x1021;
    else
      crc = crc << 1;
  while (--i);

  return crc;
}
  /* Internal function to calculate 16 bit CRC */
  uint16_t calcrc(uint8_t *ptr, uint8_t count) {
    uint16_t crc;
    //uint8_t i;
  
    crc = 0;
    while (count-- > 0)
      crc = get_crcByte(crc, *ptr++);

    return crc;
  }

#endif


--- NEW FILE: nwk_func.h ---
/*
 * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
 * @author IPP HURRAY http://www.open-zb.net
 * @author Andre Cunha
 */

#ifndef __NWK_FUNC__
#define __NWK_FUNC__

//TEST
typedef struct associated_device
{

uint32_t address0;
uint32_t address1;

uint16_t pan_address;

}associated_device;
//END TEST

typedef struct routing_fields
{

//uint8_t frame_control1;
//uint8_t frame_control2;
uint16_t frame_control;
uint16_t destination_address;
uint16_t source_address;
uint8_t radius;
uint8_t sequence_number;

}routing_fields;

/*******************************************************************************************************************/  
/********************************NETWORK LAYER FRAME CONTROL FUNCTIONS************************************************************/
/*******************************************************************************************************************/
  
 //build NPDU frame control field
uint16_t set_route_frame_control(uint8_t Frame_type,uint8_t Protocol_version,uint8_t Discover_route,uint8_t Security) 
{
	  uint8_t fc_byte1=0;
	  uint8_t fc_byte2=0;
  	  fc_byte1 = ( (Discover_route << 6) | (Protocol_version << 2) | (Frame_type << 0) );				  
	  fc_byte2 = ((Security<< 2));
	  return ( (fc_byte2 <<8 ) | (fc_byte1 << 0) );

}


uint8_t route_fc1(uint8_t Security) 
{
	uint8_t fc;
	fc = ((Security << 2));
	return fc;
}

uint8_t route_fc2(uint8_t Frame_type,uint8_t Protocol_version,uint8_t Discover_route) 
{
	uint8_t fc;
	fc = ( (Discover_route << 6) | (Protocol_version << 2) | (Frame_type << 0) );	
	return fc;
}

uint8_t get_route_frame_type(uint16_t frame_control)
{
	return (frame_control & 0x3);
}

uint8_t get_route_protocol_version(uint16_t frame_control)
{
	return ( (frame_control >> 2) & 0xf);
}

uint8_t get_route_discover_route(uint16_t frame_control)
{
	return ( (frame_control >> 6) & 0x3);
}

uint8_t get_route_security(uint16_t frame_control)
{
	if( ((frame_control >> 8) & 0x2) == 0x2)
		return 1;
	else
		return 0;
}

/*******************************************************************************************************************/  
/********************************NETWORK LAYER BEACON PAYLOAD INFORMATION FUNCTIONS*********************************/
/*******************************************************************************************************************/

uint8_t nwk_payload_profile_protocolversion(uint8_t stackprofile,uint8_t nwkcprotocolversion)
{

return ((stackprofile << 0) | ( nwkcprotocolversion << 4));
}

uint8_t nwk_payload_capacity(uint8_t routercapacity,uint8_t devicedepth,uint8_t enddevicecapacity)
{

return ((enddevicecapacity << 7) | ( devicedepth << 3 ) | (routercapacity << 2 ) );
}


uint8_t get_protocolid(uint32_t nwk_information)
{
	return (uint8_t)((nwk_information & 0xFF000000) >> 24);
}

uint8_t get_stackprofile(uint32_t nwk_information)
{
	return (uint8_t)((nwk_information & 0x00F00000)>>20);
}

uint8_t get_nwkcprotocolversion(uint32_t nwk_information)
{
	return (uint8_t)((nwk_information & 0x000F0000)>>16);
}

uint8_t get_routercapacity(uint32_t nwk_information)
{
	if ( ( nwk_information & 0x00002000) == 0x00002000)
		return 1;
	else
		return 0;
}

uint8_t get_devicedepth(uint32_t nwk_information)
{
	return (uint8_t)((nwk_information & 0x00001F00) >> 8);
}

uint8_t get_enddevicecapacity(uint32_t nwk_information)
{
	if ( ( nwk_information & 0x00000001) == 0x00000001)
		return 1;
	else
		return 0;
}

#endif



More information about the Tinyos-2-commits mailing list