--- /dev/null
+/*
+ * @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
+
--- /dev/null
+/*
+ * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
+ * @author open-zb http://www.open-zb.net
+ * @author Andre Cunha
+ */
+
+// The MAC constants are defined in here.
+// Notice that these makes use of the PHY constants.
+//pag 134
+
+#ifndef __MAC_CONST__
+#define __MAC_CONST__
+
+
+#define aBaseSlotDuration 60
+#define aBaseSuperframeDuration 960 //aBaseSlotDuration*aNumSuperframeSlots
+
+//#define aExtendedAddress // This should be defined by the device!
+
+#define aMaxBE 5 //CSMA-CA
+
+#define aMaxBeaconOverhead 75
+#define aMaxBeaconPayloadLength aMaxPHYPacketSize-aMaxBeaconOverhead
+#define aGTSDescPersistenceTime 4
+#define aMaxFrameOverhead 25
+#define aMaxFrameResponseTime 1220
+#define aMaxFrameRetries 1
+
+//(SYNC)number of beacons lost before sending a Beacon-Lost indication
+#define aMaxLostBeacons 4
+#define aMaxMACFrameSize aMaxPHYPacketSize-aMaxFrameOverhead
+#define aMaxSIFSFrameSize 18
+#define aMinCAPLength 440
+#define aMinLIFSPeriod 40
+#define aMinSIFSPeriod 12
+#define aNumSuperframeSlots 16
+#define aResponseWaitTime 32*aBaseSuperframeDuration
+#define aUnitBackoffPeriod 20
+
+
+#define TYPE_BEACON 0
+#define TYPE_DATA 1
+#define TYPE_ACK 2
+#define TYPE_CMD 3
+
+#define SHORT_ADDRESS 2
+#define LONG_ADDRESS 3
+#define RESERVED_ADDRESS 1
+
+#define NUMBER_TIME_SLOTS 16
+
+#define ACK_LENGTH 5
+
+//buffer sizes
+#define MAX_GTS_BUFFER 7
+
+//#define MAX_GTS_PEND 2
+//#define MAX_GTS_IN_SLOT 1
+
+#define INDIRECT_BUFFER_SIZE 2
+#define RECEIVE_BUFFER_SIZE 4
+#define SEND_BUFFER_SIZE 3
+
+#define UPSTREAM_BUFFER_SIZE 3
+
+#define GTS_SEND_BUFFER_SIZE 3
+
+#define BACKOFF_PERIOD_MS 0.34724
+#define BACKOFF_PERIOD_US 347.24
+
+//value of each symbol in us
+#define EFFECTIVE_SYMBOL_VALUE 17.362
+
+// MAC PIB attribute
+typedef struct
+{
+ //pag 135
+ uint8_t macAckWaitDuration;
+ bool macAssociationPermit;//FDD
+ bool macAutoRequest;
+ bool macBattLifeExt;
+ uint8_t macBattLifeExtPeriods;
+
+ uint8_t macBeaconPayload[aMaxBeaconPayloadLength];//FDD
+
+ uint8_t macBeaconPayloadLenght;//FDD
+ uint8_t macBeaconOrder;//FDD
+
+ uint32_t macBeaconTxTime;//FDD
+ uint8_t macBSN;//FDD
+ uint32_t macCoordExtendedAddress0;
+ uint32_t macCoordExtendedAddress1;
+ uint16_t macCoordShortAddress;
+ uint8_t macDSN;
+ bool macGTSPermit;//FDD
+ uint8_t macMaxCSMABackoffs;
+ uint8_t macMinBE;
+ uint16_t macPANId;
+ bool macPromiscuousMode;//FDD
+ bool macRxOnWhenIdle;
+ uint32_t macShortAddress;
+ uint8_t macSuperframeOrder;//FDD
+ uint32_t macTransactionPersistenceTime;//FDD
+
+} macPIB;
+
+// MAC PIB security ACL entry descriptor
+typedef struct
+{
+ uint32_t ACLExtendedAddress[2];
+ uint16_t ACLShortAddress;
+ uint16_t ACLPANId;
+ uint8_t ACLSecurityMaterialLength;
+ //variable string
+ uint8_t ACLSecurityMaterial;
+ uint8_t ACLSecuritySuite;
+
+}ACLDescriptor;
+
+// MAC PIB security attribute
+typedef struct
+{
+ //pag 138
+ ACLDescriptor macACLEntryDescriptorSet;
+ uint8_t macACLEntryDescriptorSetSize;
+ bool macDefaultSecurity;
+ uint8_t macDefaultSecurityMaterialLength;
+ //variable string
+ uint8_t macDefaultSecurityMaterial;
+ uint8_t macDefaultSecuritySuite;
+ uint8_t macSecurityMode;
+
+}macPIBsec;
+
+//MAC PANDescriptor
+typedef struct
+{
+ //pag76
+ uint8_t CoordAddrMode;
+ uint16_t CoordPANId;
+ uint32_t CoordAddress0;
+ uint32_t CoordAddress1;
+ uint8_t LogicalChannel;
+ //superframe specification field
+ uint16_t SuperframeSpec;
+ bool GTSPermit;
+ uint8_t LinkQuality;
+ uint32_t TimeStamp;
+ bool SecurityUse;
+ uint8_t ACLEntry;
+ bool SecurityFailure;
+
+}PANDescriptor;
+
+//GTS entry (used in the PAN coordinator)
+typedef struct
+{
+ uint8_t gts_id;
+ uint8_t starting_slot;
+ uint8_t length;
+ uint8_t direction;
+ uint16_t DevAddressType;
+ uint8_t expiration;
+
+}GTSinfoEntryType;
+
+//GTS entry (used in the PAN coordinator)
+typedef struct
+{
+ uint8_t gts_id;
+ uint8_t starting_slot;
+ uint8_t length;
+ uint16_t DevAddressType;
+ uint8_t persistencetime;
+
+}GTSinfoEntryType_null;
+
+typedef struct
+{
+ uint8_t handler;
+ uint16_t transaction_persistent_time;
+
+ //MPDU frame;
+ uint8_t frame[127];
+
+}indirect_transmission_element;
+
+typedef struct gts_slot_element
+{
+ uint8_t element_count;
+ uint8_t element_in;
+ uint8_t element_out;
+ uint8_t gts_send_frame_index[GTS_SEND_BUFFER_SIZE];
+
+}gts_slot_element;
+
+
+typedef struct time_stamp32
+{
+
+uint32_t time_stamp;
+
+}time_stamp32;
+
+typedef struct time_stamp16
+{
+
+uint16_t time_stamp;
+
+}time_stamp16;
+
+//MAC ACTIVE CHANNEL SCAN REDUCED PAN DESCRIPTOR (SHOR ADDRESS ONLY)
+typedef struct SCAN_PANDescriptor
+{
+ //pag76
+ uint16_t CoordPANId;
+ uint16_t CoordAddress;
+ uint8_t LogicalChannel;
+ //superframe specification field
+ uint16_t SuperframeSpec;
+ uint8_t lqi;
+}SCAN_PANDescriptor;
+
+
+#endif
--- /dev/null
+/*
+ * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
+ * @author open-zb http://www.open-zb.net
+ * @author Andre Cunha
+ */
+
+#ifndef __MAC_ENUMERATIONS__
+#define __MAC_ENUMERATIONS__
+
+//Mac enumerations standard pag 110
+
+enum {
+ MAC_SUCCESS = 0x00,
+ MAC_BEACON_LOSS = 0xE0,
+ MAC_CHANNEL_ACCESS_FAILURE = 0xE1,
+ MAC_DENIED = 0xE2,
+ //MLME-RESET
+ MAC_DISABLE_TRX_FAILURE = 0xE3,
+ MAC_FAILED_SECURITY_CHECK = 0xE4,
+ MAC_FRAME_TOO_LONG = 0xE5,
+ MAC_INVALID_GTS = 0xE6,
+ MAC_INVALID_HANDLE = 0xE7,
+ MAC_INVALID_PARAMETER = 0xE8,
+ MAC_NO_ACK = 0xE9,
+ MAC_NO_BEACON = 0xEA,
+ MAC_NO_DATA = 0xEB,
+ MAC_NO_SHORT_ADDRESS = 0xEC,
+ MAC_OUT_OF_CAP = 0xED,
+ MAC_PAN_ID_CONFLICT = 0xEE,
+ MAC_REALIGNMENT = 0xEF,
+ MAC_TRANSACTION_EXPIRED = 0xF0,
+ MAC_TRANSACTION_OVERFLOW = 0xF1,
+ MAC_TX_ACTIVE = 0xF2,
+ MAC_UNAVAILABLE_KEY = 0xF3,
+ MAC_UNSUPPORTED_ATTRIBUTE = 0xF4
+ };
+
+
+
+//mac dissassociation enums
+enum{
+ MAC_PAN_COORD_LEAVE = 0x01,
+ MAC_PAN_DEVICE_LEAVE = 0x02,
+
+};
+
+
+
+//mac commands enums
+enum {
+
+ CMD_ASSOCIATION_REQUEST = 0x01,
+ CMD_ASSOCIATION_RESPONSE = 0x02,
+ CMD_DISASSOCIATION_NOTIFICATION = 0x03,
+ CMD_DATA_REQUEST = 0x04,
+ CMD_PANID_CONFLICT = 0x05,
+ CMD_ORPHAN_NOTIFICATION = 0x06,
+ CMD_BEACON_REQUEST = 0x07,
+ CMD_COORDINATOR_REALIGNMENT = 0x08,
+ CMD_GTS_REQUEST = 0x09
+};
+
+
+//mac association responses
+enum {
+
+ CMD_RESP_ASSOCIATION_SUCCESSFUL = 0x00,
+ CMD_RESP_PAN_CAPACITY =0x01,
+ CMD_RESP_ACCESS_DENIED =0x02
+
+};
+
+//MAC PIB Enumeration
+enum {
+
+ MACACKWAITDURATION = 0x40,
+ MACASSOCIATIONPERMIT=0x41,
+ MACAUTOREQUEST = 0x42,
+ MACBATTLIFEEXT=0x43,
+ MACBATTLIFEEXTPERIODS=0x44,
+ MACBEACONPAYLOAD=0x45,
+ MACMAXBEACONPAYLOADLENGTH=0x46,
+ MACBEACONORDER=0x47,
+ MACBEACONTXTIME=0x48,
+ MACBSN=0x49,
+ MACCOORDEXTENDEDADDRESS=0x4a,
+ MACCOORDSHORTADDRESS=0x4b,
+ MACDSN=0x4c,
+ MACGTSPERMIT=0x4d,
+ MACMAXCSMABACKOFFS=0x4e,
+ MACMINBE=0x4f,
+ MACPANID=0x50,
+ MACPROMISCUOUSMODE=0x51,
+ MACRXONWHENIDLE=0x52,
+ MACSHORTADDRESS=0x53,
+ MACSUPERFRAMEORDER=0x54,
+ MACTRANSACTIONPERSISTENCETIME=0x55
+
+};
+
+//gts enumerations
+enum{
+ GTS_TX_ONLY = 0x00,
+ GTS_RX_ONLY = 0x01
+};
+
+//channel scan enumerations
+
+enum{
+ ED_SCAN = 0x00,
+ ACTIVE_SCAN = 0x01,
+ PASSIVE_SCAN = 0x02,
+ ORPHAN_SCAN = 0x03
+};
+
+
+
+#endif
+//
+#endif
+
--- /dev/null
+/*
+ * @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
+
--- /dev/null
+/*
+ * @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
--- /dev/null
+/*
+ * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
+ * @author Andre Cunha
+ */
+
+
+#ifndef __PHY_CONST__
+#define __PHY_CONST__
+
+// The PHY constants are defined here.
+#define aMaxPHYPacketSize 127
+#define aTurnaroundTime 12
+
+#define INIT_CURRENTCHANNEL 0x15
+#define INIT_CHANNELSSUPPORTED 0x0
+#define INIT_TRANSMITPOWER 15
+#define INIT_CCA_MODE 0
+
+#define CCA_IDLE 0
+#define CCA_BUSY 1
+
+// PHY PIB attribute and psdu
+typedef struct
+{
+ uint8_t phyCurrentChannel;
+ uint8_t phyChannelsSupported;
+ uint8_t phyTransmitPower;
+ uint8_t phyCcaMode;
+} phyPIB;
+
+#endif
+
--- /dev/null
+/*
+ * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
+ * @author Andre Cunha
+ */
+
+#ifndef __PHY_ENUMERATIONS__
+#define __PHY_ENUMERATIONS__
+
+
+//phy status enumerations
+enum{
+ PHY_BUSY = 0x00,
+ PHY_BUSY_RX = 0x01,
+ PHY_BUSY_TX = 0x02,
+ PHY_FORCE_TRX_OFF = 0x03,
+ PHY_IDLE = 0x04,
+ PHY_INVALID_PARAMETER = 0x05,
+ PHY_RX_ON = 0x06,
+ PHY_SUCCESS = 0x07,
+ PHY_TRX_OFF = 0x08,
+ PHY_TX_ON = 0x09,
+ PHY_UNSUPPORTED_ATTRIBUTE = 0x0a
+};
+
+//phy PIB attributes enumerations
+enum{
+ PHYCURRENTCHANNEL = 0x00,
+ PHYCHANNELSSUPPORTED = 0X01,
+ PHYTRANSMITPOWER = 0X02,
+ PHYCCAMODE=0X03
+};
+
+#endif
--- /dev/null
+/*
+ * 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
+