📄 swszigbeenwk.c
字号:
}//end of mlmeBeaconNotifyIndication
//-------------------------------------------------------------------------------------------------------
// NWK_ENUM nlmeNetworkFormationRequest(DWORD scanChannels, UINT8 scanDuration, UINT8 beaconOrder,UINT16 panID,ZBOOL batteryLifeExtension)
// DESCRIPTION:
// Start a new Zigbee network with itself as the coordinator and subsequently,
// to make changes to its superframe configuration.
//
// PARAMETERS:
// DWORD scanChannels
// The channel index mask (0x07FFF800 are the legal values for 2.4 GHz channels)
// UINT8 scanDuration
// The scan duration defines the time spent scanning each channel
// UINT8 beaconOrder
// The beaconOrder of the network that the higher layers wish to form
// UINT16 panID
// An optional PAN identifier that may be supplied if higher layers wish to
// establish this network with a perdetermined identifier.
// ZBOOL batteryLifeExtension
// TRUE:the zigbee coordinator is started supporting battery life extension
// FALSE:the zigbee coordinator is not started supporting battery life extension
// RETURN VALUES:
// INVALID_REQUEST |STARTUP_FAILURE
//------------------------------------------------------------------------------------------------------
#if defined(NWK_OPT_ZIGBEE_PAN_COORDINATOR)
NWK_ENUM nlmeNetworkFormationRequest(DWORD scanChannels, UINT8 scanDuration, UINT8 beaconOrder,UINT8 superframeOrder,UINT16 panID,ZBOOL batteryLifeExtension)
{
UINT8 networkFormTaskNumber;
BYTE resetResult;
if(((scanChannels&0x7fff800)==0)||(scanDuration>14))
{
return NWK_INVALID_PARAMETER;
}
resetResult=nlmeResetRequest();
nwkFormInfo.scanChannels = scanChannels;
nwkFormInfo.scanDuration = scanDuration;
nwkFormInfo.beaconOrder = beaconOrder;
nwkFormInfo.superframeOrder=superframeOrder;
nwkFormInfo.panID=panID;
nwkFormInfo.batteryLifeExtension=batteryLifeExtension;
//beacon-enabled network:0<=SO<=BO<=14
if(((0<nwkFormInfo.superframeOrder)||(nwkFormInfo.superframeOrder==0))&&((nwkFormInfo.superframeOrder<nwkFormInfo.beaconOrder)||(nwkFormInfo.superframeOrder==nwkFormInfo.beaconOrder))&&((nwkFormInfo.beaconOrder<14)||(nwkFormInfo.beaconOrder==14)))
nwkFormInfo.networkType=BEACON_ENABLED_NETWORK;
else
nwkFormInfo.networkType=BEACON_DISABLED_NETWORK;
// Clear the complete flag
nwkFormInfo.nwkFormStatus= NWK_NETWORK_FORM_STATUS_ACTIVE;
printf("nlmeNetworkFormationRequest start !\n");
//create formNetwork Task
OSTaskCreateExt(nwkFormNetworkProcedure,
(void *)&networkFormState, //this is the parameter
(OS_STK *)&nwkFormNetworkProcedureStk[TASK_STK_SIZE-1],
TASK_START_PRIO+2,
TASK_START_PRIO+2,
(OS_STK *)&nwkFormNetworkProcedureStk[0],
TASK_STK_SIZE,
(void *)0,
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
//should wait some semaphore returned from nwkFormNetworkProcedure task!
for(;;)
{
if(nwkFormInfo.nwkFormStatus== NWK_NETWORK_FORM_STATUS_ACTIVE)
{
Sleep(2000);
printf("nwkFormInfo.nwkFormStatus not changed!\n");
continue;
}
else
{
printf("nwkFormInfo.nwkFormStatus changed!\n");
OSTaskDel(TASK_START_PRIO+1);
break;
}
}
printf("nwkFormNetworkProcedure 'returned!'\n");
// Return the result
if (nwkFormInfo.panID== 0x0000)
{
printf("succeed in forming network!\n");
return NWK_SUCCESS;
}
else
{
printf("failed in forming network!\n");
return NWK_STARTUP_FAILURE;
}
}
#endif
//-------------------------------------------------------------------------------------------------------------
// NWK_ENUM nlmeResetRequest();
//
// DESCRIPTION:
// Reset a device's NWK layer.
// PARAMETERS:
// NO
// RETURN VALUE:
// NWK_ENUM
// Always SUCCESS
//----------------------------------------------------------------------------------------------------------------
NWK_ENUM nlmeResetRequest()
{
BYTE i;
if(mlmeResetRequest(TRUE)==MAC_SUCCESS)
{
//set the NIB attribute to default
nnib.nwkSequcenceNumber=0x00;//should use the random generator function
nnib.nwkPassiveTimeout=0x03;
nnib.nwkMaxBroadcastRetries=0x03;
#ifdef NWK_OPT_FFD
nnib.nwkMaxChildren=0x07;
nnib.nwkMaxDepth=0x05;
nnib.nwkMaxRouters=0x05;
nnib.nwkNextAddress=0x0000;
nnib.ChildRFDs=0x03;
nnib.ChildRouters=0x03;
nnib.nextChildRFD=0x0000;
nnib.nextChildRouter=0x0000;
#endif
nnib.nwkNetworkBroadcastDeliveryTime=nnib.nwkPassiveTimeout*nnib.nwkMaxBroadcastRetries;
nnib.nwkReportConstantCost=0x00;
nnib.nwkRouteDiscoveryRetriesPermitted=nwkcDiscoveryRetryLimit;
nnib.nwkSymlink=FALSE;
nnib.nwkCapabilityInformation=0x00;
nnib.nwkUseTreeAddrAlloc=TRUE;
nnib.nwkUseTreeRouting=TRUE;
nnib.nwkAvailableAddresses=0x0000;
nnib.nwkAddressIncreament=0x0001;
nnib.nwkTransactionPersistenceTime=0x01f4;
//clear the neighbor table
neighborTableInit();
ntInitAddressMap();
//initialize the routing table
}
return NWK_SUCCESS;
}
//-------------------------------------------------------------------------------------------------------
// NWK_ENUM nlmePermitJoingingRequest(UINT8 permitDuration)
// DESCRIPTION:
// This function is called by the APS layer of the Zigbee coordinator or router
// the NWK layer of the device execute this function if it wants other device join to its network.
//
// PARAMETERS:
// UINT8 permitDuration
// The length of time in seconds during which the zigbee coordinator allow other
// devices to associate .
// RETURN VALUES:
// INVALID_REQUEST or any status returned from the mlmeSetRequest() function.
//-------------------------------------------------------------------------------------------------------
#undef NWK_OPT_RFD
NWK_ENUM nlmePermitJoingingRequest(UINT8 permitDuration)
{
//NWK_ENUM status;
#ifndef NWK_OPT_RFD
ZBOOL setPermitJoining;
MAC_ENUM status;
setPermitJoining=TRUE;
if(permitDuration==0x00)
{
status=mlmeSetRequest(MAC_ASSOCIATION_PERMIT, FALSE);
return (NWK_ENUM)status;
}
else
{
status=mlmeSetRequest(MAC_ASSOCIATION_PERMIT, &setPermitJoining);
if((status==SUCCESS)&& (permitDuration != 0xFF))
{
// Set up the join time countdown.
nwkPermitJoinInfo.joinPermitDuration = permitDuration;
nwkPermitJoinInfo.joinDurationStart =getCurrentime();
nwkPermitJoinInfo.bTimingJoinPermitDuration = 1;
}
ConfigBeaconPayload();
return SUCCESS;
}
#else
return NWK_INVALID_PARAMETER;
#endif
}
//---------------------------------------------------------------------------------
// void ConfigBeaconPayload( void );
//
// DESCRIPTION:
// The NWK layer uses the beacons of a MAC sub-layer beacon frame
// to convey NWK layer-specific information to neighboring device.The beacon
// payload enables the NWK layer to provide additional information to new devices
// that are performing network discovery.
// PARAMETER:
// NO
// RETURN-VALUE:
// NO
// note: Beacon Payload in MAC PIB is updated.
// Attributes with a size of more than one byte must be little endian and given as byte arrays.
//------------------------------------------------------------------------------------
//#ifndef NWK_OPT_RFD //if the device is RFD,it can't allow other device to join
void ConfigBeaconPayload( void )
{
BYTE pPibBeaconAttributeValue[3];
BYTE pPibBeaconLengthAttributeValue;
BYTE pPibAssociationAttributeValue;
MAC_ENUM getResult1,getResult2;
getResult1=mlmeGetRequest(MAC_BEACON_PAYLOAD, &pPibBeaconAttributeValue);
pPibBeaconAttributeValue[0]=0;
//this filed identifies the network layer protocols in use,based on the Zigbee Specification 1.0
//this field should always be set to 0;
pPibBeaconAttributeValue[1]=(nwkcProtocolVersion << 4) | SWS_ZIGBEE_STACK_PROFILE_ID;
pPibBeaconAttributeValue[2]= currentNeighborTableInfo.depth << 3;
getResult2=mlmeGetRequest(MAC_ASSOCIATION_PERMIT,&pPibAssociationAttributeValue);
if ((((BYTE)pPibAssociationAttributeValue)==TRUE)&&
(currentNeighborTableInfo.numChildren <nnib.nwkMaxChildren) &&
(currentNeighborTableInfo.neighborTableSize < NEIGHBOR_TABLE_ENTRIES))
{
#ifdef NWK_OPT_FFD
if (nnib.ChildRFDs<(nnib.nwkMaxChildren-nnib.nwkMaxRouters))
{
pPibBeaconAttributeValue[2] |= 0x80; // End Devices can join
}
if ((currentNeighborTableInfo.depth < (nnib.nwkMaxDepth-1)) &&
(currentNeighborTableInfo.numChildRouters < nnib.nwkMaxRouters) &&
(nnib.ChildRouters<nnib.nwkMaxRouters))
{
pPibBeaconAttributeValue[2] |= 0x04; // Routers can join
}
#endif
}
}
//#endif
//-----------------------------------------------------------------------------------------------------------
// NWK_ENUM nlmeDirectJoinRequest(ADDRESS * deviceAddress,BYTE capabilityInformation);
//
// DESCRIPTION:
// Join a device directly to a network.
// PARAMETERS:
// QWORD deviceAddress
// The IEEE address of the device to be directly joined.
// BYTE capabilityInformation
// The operating capabilities of the device being directly joined.
// RETURN VALUES:
// (NWK_SUCCESS | NWK_ALREADY_PRESENT | NWK_TABLE_FULL)
//------------------------------------------------------------------------------------------------------------
#ifndef NWK_OPT_RFD
NWK_ENUM nlmeDirectJoinRequest(ADDRESS *deviceAddress,BYTE capabilityInformation)
{
BYTE i;
NEIGHBOR_INDEX searchResult;
// #ifdef NWK_OPT_ZIGBEE_PAN_COORDINATOR
// return NWK_INVALID_REQUEST;
// #else
if (NWKLookupNodeByLongAddr( nwkDirectJoinInfo.deviceAddress.Extended) != INVALID_NEIGHBOR_INDEX)
{
// The node is already in our neighbor table.
return NWK_ALREADY_PRESENT;
}
if (!CanAddChildNode() || ((i = CanAddNeighborNode()) == INVALID_NEIGHBOR_INDEX))
{
// We do not have room to add this node to the neighbor table.
return NWK_TABLE_FULL;
}
else
{
// Add the node to our network
//add the device to the neighbor table
AddChildNode(); //if the device can implement the nlmeDirectJoinRequest function,this device is not RFD.
return NWK_SUCCESS;
}
// #endif
}
#endif
//-------------------------------------------------------------------------------------------------------
// void mlmeOrphanIndication(ADDRESS * orphanAddress, ZBOOL securityUse, UINT8 aclEntry)
//
// DESCRIPTION:
// Callback generated by the MAC sublayer to the higher layer upon reception of a
// orphan notification command frame
// Function must be implemented by the higher layer of a FFD device
//
// PARAMETERS:
// QWORD orphanAddress
// Extended address of the device notifying its orphan state
// ZBOOL securityUse
// Security enabled for the incoming frame?
// UINT8 aclEntry
// The macSecurityMode parameter value from the ACL entry associated with the sender of
// the data frame. This value is set to 0x08 if the sender of the data frame was not
// found in the ACL.
//-------------------------------------------------------------------------------------------------------
#ifndef NWK_OPT_RFD
void mlmeOrphanIndication(ADDRESS *orphanAddress, ZBOOL securityUse, UINT8 aclEntry)
{
BYTE i;
UINT16 shortAddress;
i = NWKLookupNodeByLongAddr(orphanAddress->Extended);
if (i != INVALID_NEIGHBOR_INDEX)
{
shortAddress=currentNeighborRecord.shortAddress;
mlmeOrphanResponse(orphanAddress, shortAddress, TRUE, FALSE);
}
else
{
mlmeOrphanResponse(orphanAddress, shortAddress, FALSE, FALSE);
}
}
#endif
#ifndef NWK_OPT_RFD
//-------------------------------------------------------------------------------------------------------
// void mlmeAssociateIndication(ADDRESS *deviceAddress, BYTE capabilityInformation, ZBOOL securityUse,...)
//
// DESCRIPTION:
// mlmeAssociateIndication is generated by the MAC layer upon reception of an associate request
// command frame. For this demo application, all devices are allowed to associate, however only one
// device at the time. The coordinator must be reset before a new device can associate.
//
// The short address is assigned from the associatedAddress variable, which should have been
// incremented if more devices could have joined.
//
// Function must be implemented by the higher layer
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -