📄 associationexamplem.nc
字号:
/*
*
* @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
* @author Andre Cunha
*
*/
module AssociationExampleM
{
provides {
interface StdControl;
}
uses {
interface Timer;
interface Leds;
interface MLME_START;
interface MLME_ASSOCIATE;
interface MLME_DISASSOCIATE;
interface MLME_SCAN;
interface MLME_RESET;
interface MLME_SYNC;
interface MLME_SYNC_LOSS;
interface MLME_BEACON_NOTIFY;
interface MLME_COMM_STATUS;
interface MLME_SET;
interface MLME_GET;
interface MLME_GTS;
//MCPS
interface MCPS_DATA;
interface MCPS_PURGE;
interface StdControl as Mac_control;
//periodic data send timer
interface Timer as Send_Data_Timer;
interface Time;
}
}
implementation
{
//number of data frames sent after association and before dissassociation
uint16_t frame_counter=0;
//associated devices
uint16_t address_poll = 0x0003;
uint8_t number_associations =0;
PANDescriptor pan_des;
void try_disassociation();
uint16_t my_short_address= 0xffff;
command result_t StdControl.init() {
return SUCCESS;
}
command result_t StdControl.start() {
printfUART("i_am_pan: %i\n", TYPE_DEVICE);
if (TYPE_DEVICE == COORDINATOR)
{
//assign the short address of the device
my_short_address = 0x0000;
call Timer.start(TIMER_ONE_SHOT,3000);
}
else
{
call Timer.start(TIMER_REPEAT,8000);
}
return SUCCESS;
}
command result_t StdControl.stop() {
call Mac_control.stop();
return SUCCESS;
}
//fired when the mote starts
event result_t Timer.fired()
{
uint8_t v_temp[2];
uint32_t c_addr[2];
if (TYPE_DEVICE == COORDINATOR)
{
//set the MAC short address variable
v_temp[0] = (uint8_t)(my_short_address >> 8);
v_temp[1] = (uint8_t)(my_short_address );
call MLME_SET.request(MACSHORTADDRESS,v_temp);
//set the MAC PANID variable
v_temp[0] = (uint8_t)(PANID >> 8);
v_temp[1] = (uint8_t)(PANID );
call MLME_SET.request(MACPANID,v_temp);
//start sending beacons
call MLME_START.request(PANID, LOGICAL_CHANNEL, BEACON_ORDER, SUPERFRAME_ORDER,1,0,0,0,0);
}
else
{
//the device will try to scan all the channels looking for a suitable PAN coordinator
//only the ACTIVE SCAN/ED SCAN and a full channel scan is implemented
//call MLME_SCAN.request(PASSIVE_SCAN,0xFFFFFFFF,7);
//call MLME_SCAN.request(ED_SCAN,0xFFFFFFFF,0x10);
c_addr[0] = 0x00000000;
c_addr[1] = 0x00000000;
call MLME_ASSOCIATE.request(0x15,SHORT_ADDRESS,0x1234,c_addr,0x00,0x00);
//call Leds.redOn();
call Timer.stop();
}
return SUCCESS;
}
/*****************************************************************************************************/
/*****************************************************************************************************/
/**************************************MLME EVENTS****************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/**************************************MLME-GTS*******************************************************/
/*****************************************************************************************************/
event result_t MLME_GTS.confirm(uint8_t GTSCharacteristics, uint8_t status)
{
return SUCCESS;
}
event result_t MLME_GTS.indication(uint16_t DevAddress, uint8_t GTSCharacteristics, bool SecurityUse, uint8_t ACLEntry)
{
return SUCCESS;
}
/*****************************************************************************************************/
/**************************************MLME-START*****************************************************/
/*****************************************************************************************************/
event result_t MLME_START.confirm(uint8_t status)
{
return SUCCESS;
}
/*****************************************************************************************************/
/**************************************MLME-ASSOCIATE*************************************************/
/*****************************************************************************************************/
event result_t MLME_ASSOCIATE.indication(uint32_t DeviceAddress[], uint8_t CapabilityInformation, bool SecurityUse, uint8_t ACLEntry)
{
//the coordinator device receives the association request and assigns the device with a short address
address_poll ++;
number_associations++;
printfUART("address pool: %i %i\n", address_poll,number_associations);
call MLME_ASSOCIATE.response(DeviceAddress,address_poll, CMD_RESP_ASSOCIATION_SUCCESSFUL, 0);
return SUCCESS;
}
event result_t MLME_ASSOCIATE.confirm(uint16_t AssocShortAddress, uint8_t status)
{
//the end device receives the association confirm and activates the data frame send timer
uint8_t v_temp[2];
printfUART("MLME_ASSOCIATE.confirm\n", "");
printfUART("Short: %x\n", AssocShortAddress);
printfUART("Status: %i\n", status);
if (AssocShortAddress == 0x0000)
{
call Timer.start(TIMER_ONE_SHOT,8000);
}
else
{
my_short_address = AssocShortAddress;
v_temp[0] = (my_short_address >> 8);
v_temp[1] = my_short_address;
//call Leds.redOn();
call MLME_SET.request(MACSHORTADDRESS,v_temp);
call Send_Data_Timer.start(TIMER_REPEAT,3000);
call Timer.stop();
}
return SUCCESS;
}
/*****************************************************************************************************/
/**************************************MLME-DISASSOCIATE**********************************************/
/*****************************************************************************************************/
event result_t MLME_DISASSOCIATE.indication(uint32_t DeviceAddress[], uint8_t DisassociateReason, bool SecurityUse, uint8_t ACLEntry)
{
return SUCCESS;
}
event result_t MLME_DISASSOCIATE.confirm(uint8_t status)
{
return SUCCESS;
}
/*****************************************************************************************************/
/**************************************MLME-BEACON NOTIFY*********************************************/
/*****************************************************************************************************/
event result_t MLME_BEACON_NOTIFY.indication(uint8_t BSN,PANDescriptor pan_descriptor, uint8_t PenAddrSpec, uint8_t AddrList, uint8_t sduLength, uint8_t sdu[])
{
return SUCCESS;
}
/*****************************************************************************************************/
/*********************** MLME-SYNC_LOSS **************************************/
/*****************************************************************************************************/
event result_t MLME_SYNC_LOSS.indication(uint8_t LossReason)
{
return SUCCESS;
}
/*****************************************************************************************************/
/************************* MLME-RESET ********************************************/
/*****************************************************************************************************/
event result_t MLME_RESET.confirm(uint8_t status)
{
return SUCCESS;
}
/*****************************************************************************************************/
/************************* MLME-SCAN *******************************************/
/*****************************************************************************************************/
event result_t MLME_SCAN.confirm(uint8_t status,uint8_t ScanType, uint32_t UnscannedChannels, uint8_t ResultListSize, uint8_t EnergyDetectList[], SCAN_PANDescriptor PANDescriptorList[])
{
//the device channel scan ends with a scan confirm containing a list of the PANs (beacons received during the scan)
int i;
uint8_t max_lqi=0;
uint8_t best_pan_index=0;
uint32_t coordinator_addr[2];
call Leds.redOff();
printfUART("MLME_SCAN.confirm\n", "");
if(ScanType == ED_SCAN)
{
for(i=0;i<ResultListSize;i++)
{
printfUART("ED SCAN %i %i\n", (0x0A + i),EnergyDetectList[i]);
}
return SUCCESS;
}
for (i=0; i<ResultListSize;i++)
{ /*
printfUART("cord id %i", PANDescriptorList[i].CoordPANId);
printfUART("CoordAddress %i", PANDescriptorList[i].CoordAddress);
printfUART("LogicalChannel %i", PANDescriptorList[i].LogicalChannel);
printfUART("SuperframeSpec %i", PANDescriptorList[i].SuperframeSpec);
printfUART("lqi %i\n", PANDescriptorList[i].lqi);
*/
if(max_lqi < PANDescriptorList[i].lqi)
{
max_lqi =PANDescriptorList[i].lqi;
best_pan_index = i;
}
}
printfUART("SELECTED cord id %i", PANDescriptorList[best_pan_index].CoordPANId);
printfUART("CoordAddress %i", PANDescriptorList[best_pan_index].CoordAddress);
printfUART("LogicalChannel %i", PANDescriptorList[best_pan_index].LogicalChannel);
printfUART("SuperframeSpec %i", PANDescriptorList[best_pan_index].SuperframeSpec);
printfUART("lqi %i\n", PANDescriptorList[best_pan_index].lqi);
coordinator_addr[0] = 0x00000001;
coordinator_addr[1] = (uint32_t)PANDescriptorList[best_pan_index].CoordAddress;
//enables the TimerAsync events, in order to enable the synchronization with the PAN coordinator
call MLME_SYNC.request(PANDescriptorList[best_pan_index].LogicalChannel,0);
call MLME_ASSOCIATE.request(PANDescriptorList[best_pan_index].LogicalChannel,SHORT_ADDRESS,PANDescriptorList[best_pan_index].CoordPANId,coordinator_addr,0x00,0x00);
return SUCCESS;
}
/*****************************************************************************************************/
/************************** MLME-COMM_STATUS **************************************/
/*****************************************************************************************************/
event result_t MLME_COMM_STATUS.indication(uint16_t PANId,uint8_t SrcAddrMode, uint32_t SrcAddr[], uint8_t DstAddrMode, uint32_t DstAddr[], uint8_t status)
{
return SUCCESS;
}
/*****************************************************************************************************/
/************************* MLME-GET ******************************************/
/*****************************************************************************************************/
event result_t MLME_GET.confirm(uint8_t status,uint8_t PIBAttribute, uint8_t PIBAttributeValue[])
{
return SUCCESS;
}
/*****************************************************************************************************/
/********************** MLME-SET ******************************************/
/*****************************************************************************************************/
event result_t MLME_SET.confirm(uint8_t status,uint8_t PIBAttribute)
{
return SUCCESS;
}
/*****************************************************************************************************/
/*****************************************************************************************************/
/**************** MCPS EVENTS *************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/********************* MCPS-DATA ***************************************/
/*****************************************************************************************************/
event result_t MCPS_DATA.confirm(uint8_t msduHandle, uint8_t status)
{
return SUCCESS;
}
event result_t MCPS_DATA.indication(uint16_t SrcAddrMode, uint16_t SrcPANId, uint32_t SrcAddr[2], uint16_t DstAddrMode, uint16_t DestPANId, uint32_t DstAddr[2], uint16_t msduLength,uint8_t msdu[100],uint16_t mpduLinkQuality, uint16_t SecurityUse, uint16_t ACLEntry)
{
return SUCCESS;
}
/*****************************************************************************************************/
/************************* MCPS-PURGE *************************************/
/*****************************************************************************************************/
event result_t MCPS_PURGE.confirm(uint8_t msduHandle, uint8_t status)
{
return SUCCESS;
}
/*****************************************************************************************************/
/*********************** OTHER FUNCTIONS **************************************/
/*****************************************************************************************************/
event result_t Send_Data_Timer.fired()
{
uint32_t SrcAddr[2];
uint32_t DstAddr[2];
uint8_t msdu_payload[4];
frame_counter++;
if (frame_counter == 5)
{
//after sending 5 data frames the device tries to dissassociate from the PAN
call Send_Data_Timer.stop();
try_disassociation();
}
else
{
call Leds.greenToggle();
if (my_short_address == 0x0000ffff)
return SUCCESS;
SrcAddr[0]=0x00000000;
SrcAddr[1]=my_short_address;
DstAddr[0]=0x00000000;
DstAddr[1]=0x00000000;
call MCPS_DATA.request(SHORT_ADDRESS, PANID, SrcAddr, SHORT_ADDRESS, PANID, DstAddr, 4, msdu_payload,1,set_txoptions(1,0,0,0));
}
return SUCCESS;
}
void try_disassociation()
{
uint32_t coordinator_addr[2];
coordinator_addr[0] = 0x00000001;
coordinator_addr[1] = 0x00000001;
call MLME_DISASSOCIATE.request(coordinator_addr,MAC_PAN_DEVICE_LEAVE,0x00);
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -