📄 nwkp.nc
字号:
if (received_beacon_count==5)
{
printfUART("sa \n", "");
go_associate=0;
//call MLME_ASSOCIATE.request(pan_des.LogicalChannel,SHORT_ADDRESS,pan_des.CoordPANId,coordinator_addr,0x00,0x00);
destinaddress[0]=0x00000000;
destinaddress[1]=0x00000000;
call MLME_ASSOCIATE.request(LOGICAL_CHANNEL,SHORT_ADDRESS,0x1234,destinaddress, set_capability_information(0x00,0x00,0x00,0x00,0x00,0x01),0);
}
}
return SUCCESS;
}
/*****************************************************************************************************/
/**************************************MLME-START*****************************************************/
/*****************************************************************************************************/
event error_t MLME_START.confirm(uint8_t status)
{
////printfUART("MLME_START.confirm\n", "");
if (device_type==COORDINATOR)
{
signal NLME_NETWORK_FORMATION.confirm(status);
joined=1;
}
else
{
signal NLME_START_ROUTER.confirm(status);
}
return SUCCESS;
}
/*****************************************************************************************************/
/********************** MLME-SET ******************************************/
/*****************************************************************************************************/
event error_t MLME_SET.confirm(uint8_t status,uint8_t PIBAttribute)
{
return SUCCESS;
}
/*****************************************************************************************************/
/************************* MLME-GET ******************************************/
/*****************************************************************************************************/
event error_t MLME_GET.confirm(uint8_t status,uint8_t PIBAttribute, uint8_t PIBAttributeValue[])
{
return SUCCESS;
}
/*****************************************************************************************************/
/**************************************MLME-ASSOCIATE*************************************************/
/*****************************************************************************************************/
event error_t MLME_ASSOCIATE.indication(uint32_t DeviceAddress[], uint8_t CapabilityInformation, bool SecurityUse, uint8_t ACLEntry)
{
//notification that an other device wants to associate
//THIS DEVICE IS EITHER A COORDINATOR OR A ROUTER
uint8_t cindex=0;
atomic{
//check the neighbour table
cindex = check_neighbortableentry(LONG_ADDRESS,DeviceAddress[0] , DeviceAddress[1]);
if( cindex > 0 )
{
call MLME_ASSOCIATE.response(DeviceAddress,neighbortable[cindex - 1].Network_Address,MAC_SUCCESS,0);
}
else
{
if(nwk_IB.nwkAvailableAddresses > 0)
{
//verify if the device is associating as a router or an end device
if ( get_alternate_PAN_coordinator(CapabilityInformation) == 1)
{
//add device to the neighbour table
add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],next_child_router_address,ROUTER,NEIGHBOR_IS_CHILD);
printfUART("An_cr %x\n",next_child_router_address);
//send response, this shall lead to confirm in child device
call MLME_ASSOCIATE.response(DeviceAddress,next_child_router_address,MAC_SUCCESS,0);
//calculate the next address
next_child_router_address = networkaddress + ((number_child_router-1) * cskip) +1 ;
//increment the number of associated routers
number_child_router++;
//decrese the number of available addresses //the available addresses are influenced by the network configurations
nwk_IB.nwkAvailableAddresses--;
printfUART("Dn_cr %x\n",next_child_router_address);
}
else
{
//verify if its possible to associate children in the address space
//the number of end devices must be greater than 1 and lesser or iqual to maximum children minus maximum routers
if (number_child_end_devices > 1 && number_child_end_devices > (MAXCHILDREN - MAXROUTERS))
{
call MLME_ASSOCIATE.response(DeviceAddress,0xffff,CMD_RESP_PAN_CAPACITY,0);
//return SUCCESS;
}
else
{
//CHECK COMMENTED ON SHORT VERSION
add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],nwk_IB.nwkNextAddress,END_DEVICE,NEIGHBOR_IS_CHILD);
//send response, this shall lead to confirm in child device
call MLME_ASSOCIATE.response(DeviceAddress,nwk_IB.nwkNextAddress,MAC_SUCCESS,0);
nwk_IB.nwkNextAddress=nwk_IB.nwkNextAddress + nwk_IB.nwkAddressIncrement;
number_child_end_devices++;
nwk_IB.nwkAvailableAddresses--;
}
}
if (nwk_IB.nwkAvailableAddresses == 0 )
{
call MLME_SET.request(MACASSOCIATIONPERMIT,(uint8_t *)0x00000000);
}
}
else
{
//if there are no available addresses the coordinator/router shall not respond to the association request
call MLME_ASSOCIATE.response(DeviceAddress,0xffff,CMD_RESP_PAN_CAPACITY,0);
}
}
}
return SUCCESS;
}
event error_t MLME_ASSOCIATE.confirm(uint16_t AssocShortAddress, uint8_t status)
{
uint8_t v_temp[2];
////printfUART("MLME_ASSOCIATE.confirm\n","");
if (AssocShortAddress == 0xffff)
{
//association failed
//printfUART("nwkass fail\n","");
signal NLME_JOIN.confirm(panid,NWK_NOT_PERMITTED);
}
else
{
networkaddress = AssocShortAddress;
//add_neighbortableentry(panid,DeviceAddress[0],DeviceAddress[1],nwk_IB.nwkNextAddress,END_DEVICE,NEIGHBOR_IS_CHILD);
//set the short address
if (status == MAC_SUCCESS)
{
joined = 0x01;
v_temp[0] = (uint8_t)(networkaddress >> 8);
v_temp[1] = (uint8_t)(networkaddress );
//call MLME_SET.request(MACSHORTADDRESS,(uint32_t*)(uint32_t)&networkaddress);
call MLME_SET.request(MACSHORTADDRESS,v_temp);
signal NLME_JOIN.confirm(panid, NWK_SUCCESS);
}
else
{
signal NLME_JOIN.confirm(panid,NWK_NOT_PERMITTED);
}
}
return SUCCESS;
}
/*****************************************************************************************************/
/**************************************MLME-DISASSOCIATE**********************************************/
/*****************************************************************************************************/
event error_t MLME_DISASSOCIATE.indication(uint32_t DeviceAddress[], uint8_t DisassociateReason, bool SecurityUse, uint8_t ACLEntry)
{
////printfUART("MLME_DISASSOCIATE.indication:SUCCESS\n", "");
signal NLME_LEAVE.confirm(DeviceAddress, NWK_SUCCESS);
return SUCCESS;
}
event error_t MLME_DISASSOCIATE.confirm(uint8_t status)
{
if (status == MAC_SUCCESS)
{
signal NLME_LEAVE.confirm(0, status);
////printfUART("MLME_DISASSOCIATE.confirm:SUCCESS\n", "");
}
else
{
signal NLME_LEAVE.confirm(0, NWK_LEAVE_UNCONFIRMED);
////printfUART("MLME_DISASSOCIATE.confirm:leave unconfirmed\n", "");
}
return SUCCESS;
}
/*****************************************************************************************************/
/*****************************************************************************************************/
/**************** MCPS EVENTS *************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/********************* MCPS-DATA ***************************************/
/*****************************************************************************************************/
event error_t MCPS_DATA.confirm(uint8_t msduHandle, uint8_t status)
{
////printfUART("MCPS_DATA.confirm\n", "");
signal NLDE_DATA.confirm(1,status);
return SUCCESS;
}
event error_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)
{
uint8_t payload[100];
uint32_t route_destination_address[2];
uint32_t net_addr[2];
uint8_t next_hop_index;
routing_fields *routing_fields_ptr;
routing_fields_ptr = (routing_fields *)&msdu[0];
net_addr[1] =(uint32_t) networkaddress;
net_addr[0]=0x00000000;
if ( routing_fields_ptr->destination_address == networkaddress)
{
//I am the destination
memcpy(&payload,&msdu[8],(msduLength-8)*sizeof(uint8_t));
//pass data on to APL layer
signal NLDE_DATA.indication(routing_fields_ptr->source_address,(uint16_t)(msduLength-8),payload, mpduLinkQuality);
return SUCCESS;
}
else
{
//I am not the destination
if(device_type != COORDINATOR)
{
if( (networkaddress < routing_fields_ptr->destination_address) && (routing_fields_ptr->destination_address < (networkaddress + cskip_routing ) ) )
{
printfUART("route down to appropriate child\n", "");
//check if destination is one of my children
next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
if (next_hop_index == 0)
{
//destination is not my child
route_destination_address[0]=0x00000000;
route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
}
else
{
//Im routing to my child
route_destination_address[0]=0x00000000;
route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
}
//send message
call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
}
else
{
//changes to meet with the BEACON SYNCHRONIZATION requirements
//route up to the parent
atomic{
route_destination_address[0]=0x00000000;
route_destination_address[1]=neighbortable[parent].Network_Address;
call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions_upstream(1,0,0,0,1));
}
}
}
else
{
//I AM THE PAN COORDINATOR
//THE COORDINATOR ALWAYS ROUTES DOWN
//route down to appropriate child
//check if destination is one of my children
next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
if (next_hop_index == 0 )
{
//no entry in neigbortable
//calculate route address
route_destination_address[0]=0x00000000;
route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
}
else
{
//is my child
route_destination_address[0]=0x00000000;
route_destination_address[1]=neighbortable[next_hop_index-1].Network_Address;
call MCPS_DATA.request(SHORT_ADDRESS, panid, net_addr, SHORT_ADDRESS, DestPANId, route_destination_address, msduLength, msdu,1,set_txoptions(1,0,0,0));
}
}
}
return SUCCESS;
}
/*************************************************************/
/*******************NLDE IMPLEMENTATION***********************/
/*************************************************************/
/*************************************************************/
/*************************NLDE - DATA*************************/
/*************************************************************/
//This primitive requests the transfer of a data PDU
//page 159-161
command error_t NLDE_DATA.request(uint16_t DstAddr, uint16_t NsduLength, uint8_t Nsdu[100], uint8_t NsduHandle, uint8_t Radius, uint8_t DiscoverRoute, uint8_t SecurityEnable)
{
uint32_t srcadd[2];
//prefixed size because the devices reset itself when there is an error in the length
uint8_t MSDU[100];
uint32_t route_destination_address[2];
routing_fields *routing_fields_ptr;
uint8_t next_hop_index;
routing_fields_ptr = (routing_fields *)&MSDU[0];
if(joined==0)
{
signal NLDE_DATA.confirm(NsduHandle, NWK_INVALID_REQUEST);
}
else
{
routing_fields_ptr->frame_control= set_route_frame_control(0x00,0x01,DiscoverRoute,SecurityEnable);
routing_fields_ptr->destination_address=DstAddr;
routing_fields_ptr->source_address=networkaddress;
routing_fields_ptr->radius=Radius;
routing_fields_ptr->sequence_number=nwk_IB.nwkSequenceNumber;
nwk_IB.nwkSequenceNumber++;
memcpy(&MSDU[8],&Nsdu[0],NsduLength*sizeof(uint8_t));
srcadd[0] = 0x00000000;
srcadd[1] = (uint32_t)networkaddress;
if (device_type == END_DEVICE)
{
//if the device is an end device always sends the message to the parent
route_destination_address[0]=0x00000000;
route_destination_address[1]=neighbortable[parent].Network_Address;
//ack
call MCPS_DATA.request(SHORT_ADDRESS, panid, srcadd, SHORT_ADDRESS,panid, route_destination_address, (NsduLength + 8), MSDU,1,set_txoptions(1,0,0,0));
return SUCCESS;
}
//send message if the device is the COORDINATOR or a ROUTER
if( (networkaddress < routing_fields_ptr->destination_address) && (routing_fields_ptr->destination_address < (networkaddress + cskip_routing ) ) )
{
//route down to appropriate child
//check if destination is one of my children
next_hop_index = check_neighbortableentry(SHORT_ADDRESS,(uint32_t)routing_fields_ptr->destination_address,0x00000000);
if (next_hop_index == 0)
{
//destination is not my child
route_destination_address[0]=0x00000000;
route_destination_address[1]=nexthopaddress(routing_fields_ptr->destination_address,depth);
}
else
{
//Im routing to my child
route_destination_address[0]=0x00000000;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -