Professional Documents
Culture Documents
/*
Microchip ZigBee2006 Residential Stack
This demonstration shows how a ZigBee coordinator can be set up. This demo allows
the PICDEM Z/Explorer 16 Demostration Board to act as ZigBee protocol Coordinator
It is designed to interact with other ZigBee protocol devices - Routers and End Devices.
At startup the devices are not in any group, and the lit LEDs just indicate they are ready
and on the network.
*********************************************************************
* FileName: Rfd.c
* Dependencies:
* Processor: PIC18F/PIC24F
* Complier: MCC18 v3.20 or higher
* Complier: MCC30 v3.10 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright 2004-2007 Microchip Technology Inc. All rights reserved.
*
* Microchip licenses to you the right to use, copy and distribute Software
* only when embedded on a Microchip microcontroller or digital signal
* controller and used with a Microchip radio frequency transceiver, which
* are integrated into your product or third party product (pursuant to the
* sublicense terms in the accompanying license agreement). You may NOT
* modify or create derivative works of the Software.
*
* If you intend to use this Software in the development of a product for
* sale, you must be a member of the ZigBee Alliance. For more information,
* go to www.zigbee.org.
*
* You should refer to the license agreement accompanying this Software for
* additional information regarding your rights and obligations.
*
* SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
* OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
* PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED
* UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF
* WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR
* EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT,
* PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY
* THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER
* SIMILAR COSTS.
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* DF/KO 01/09/06 Microchip ZigBee Stack v1.0-3.5
* DL 08/01/08 Microchip ZigBee Stack v2.0-2.6
********************************************************************/
//******************************************************************************
// Header Files
//******************************************************************************
// Include the main ZigBee header file.
#include "zAPL.h"
#ifdef I_SUPPORT_SECURITY
#include "zSecurity.h"
#endif
//******************************************************************************
// Configuration Bits
//******************************************************************************
#pragma romdata
#elif defined(__PIC24F__)
// Explorer 16 board
_CONFIG2(FNOSC_PRI & POSCMOD_XT) // Primary XT OSC with 4X PLL
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF & WDTPS_PS512 ) // JTAG off, watchdog timer off,
//prescale 512(~2secs timeout on WDT)
#elif defined(__dsPIC33F__) || defined(__PIC24H__)
// Explorer 16 board
_FOSCSEL(FNOSC_PRI) // primary osc
_FOSC(OSCIOFNC_OFF & POSCMD_XT) // XT Osc
_FWDT(FWDTEN_OFF) // Disable Watchdog timer
// JTAG should be disabled as well
#elif defined(__dsPIC30F__)
// dsPICDEM 1.1 board
_FOSC(XT_PLL16) // XT Osc + 16X PLL
_FWDT(WDT_OFF) // Disable Watchdog timer
_FBORPOR(MCLR_EN & PBOR_OFF & PWRT_OFF)
#else
#error Other compilers are not yet supported.
#endif
//******************************************************************************
// Application Variables
//******************************************************************************
NETWORK_DESCRIPTOR *currentNetworkDescriptor;
ZIGBEE_PRIMITIVE currentPrimitive;
NETWORK_DESCRIPTOR *NetworkDescriptor;
BYTE orphanTries;
#ifdef I_SUPPORT_SECURITY
extern KEY_VAL KeyVal;
#ifdef USE_EXTERNAL_NVM
extern WORD trustCenterLongAddr;
extern NETWORK_KEY_INFO plainSecurityKey[2];
#else
extern ROM LONG_ADDR trustCenterLongAddr;
#endif
#endif
/* Menu System */
ROM char * const menu =
"\r\n 2: Request Data From Another Device"
"\r\n 3: Request Data From a Group of Devices"
"\r\n 4: Send Data To Another Device"
"\r\n 5: Send Data To a Group of Devices"
"\r\n 6: Add/Remove Device to/from a Group"
"\r\n 7: Dump Neighborhood Information"
;
//******************************************************************************
//******************************************************************************
int MEMORIA_MEDIDOR = 0;
//******************************************************************************
//******************************************************************************
WORD_VAL MSGPacketCount;
//******************************************************************************
// Constants
//******************************************************************************
#if defined(__C30__)
#define PB_LEFT_SWITCH PORTDbits.RD6
#define PB_RIGHT_SWITCH PORTDbits.RD7
#define GROUP_INDICATION LATAbits.LATA6
#define MESSAGE_INDICATION LATAbits.LATA7
#else
#define PB_LEFT_SWITCH PORTBbits.RB5
#define PB_RIGHT_SWITCH PORTBbits.RB4
#if defined(__18F4620)
#define GROUP_INDICATION LATAbits.LATA0
#define MESSAGE_INDICATION LATAbits.LATA1
#else
#define GROUP_INDICATION LATDbits.LATD0
#define MESSAGE_INDICATION LATDbits.LATD1
#endif
#endif
BYTE AllowJoin = 1;
//******************************************************************************
// Function Prototypes
//******************************************************************************
void ProcessMenu( void );
void PrintMenu( void );
BYTE GetHexDigit( void );
BYTE GetMACByte( void );
WORD GetShortAddressVal( void );
void HardwareInit(void);
void SendLightMessage( WORD destVal, BYTE message, BYTE rd );
void SendCountedPacket(BYTE seqNumber, BYTE len);
void ProcessNONZigBeeTasks(void);
void ProcessZigBeePrimitives(void);
static union
{
struct
{
BYTE bBroadcastSwitchToggled : 1;
BYTE bLightSwitchToggled : 1;
BYTE bTryingToBind : 1;
BYTE bIsBound : 1;
BYTE bDestinationAddressKnown : 1;
BYTE bBindSwitchToggled : 1;
} bits;
BYTE Val;
} myStatusFlags;
TICK PB_LEFT_press_time;
TICK PB_RIGHT_press_time;
TICK tickDifference;
TICK tick2Difference;
/* determines whether both the PIC and radio sleep, or just simulated in
* order to debug and use the menus with the uart
*/
//#define BOTH_MICRO_TRANSV_SLEEP
//******************************************************************************
//******************************************************************************
//******************************************************************************
#if defined(__C30__)
int main(void)
#else
void main(void)
#endif
{
#if defined(__18F87J10)
NOP();
NOP();
NOP();
NOP();
NOP();
OSCTUNEbits.PLLEN = 1;
NOP();
NOP();
NOP();
NOP();
NOP();
#endif
currentPrimitive = NO_PRIMITIVE;
NetworkDescriptor = NULL;
orphanTries = 3;
startPollingTime.Val = 0x00;
// *************************************************************************
// Perform any other initialization here
// *************************************************************************
NWKClearNeighborTable();
#if defined(I_SUPPORT_BINDING)
ClearBindingTable();
#endif
/* Clear the Group Table */
RemoveAllGroups();
#if defined(I_SUPPORT_BINDING)
RemoveAllBindings(macPIB.macShortAddress);
#endif
discoveredAddress.Val = 0x0000;
routeDiscovery = 0;
while (1)
{
CLRWDT();
/* do any non ZigBee related tasks and then go back to ZigBee tasks */
ProcessNONZigBeeTasks();
}
}
void ProcessZigBeePrimitives(void)
{
switch (currentPrimitive)
{
case NLME_NETWORK_DISCOVERY_confirm:
currentPrimitive = NO_PRIMITIVE;
if (!params.NLME_NETWORK_DISCOVERY_confirm.Status)
{
if (!params.NLME_NETWORK_DISCOVERY_confirm.NetworkCount)
{
ConsolePutROMString( (ROM char *)"No networks found. Trying
again...\r\n" );
}
else
{
// Save the descriptor list pointer so we can destroy it later.
NetworkDescriptor =
params.NLME_NETWORK_DISCOVERY_confirm.NetworkDescriptor;
SubmitJoinRequest:
params.NLME_JOIN_request.PANId = currentNetworkDescriptor->PanID;
params.NLME_JOIN_request.JoinAsRouter = FALSE;
params.NLME_JOIN_request.RejoinNetwork = FALSE;
params.NLME_JOIN_request.PowerSource = NOT_MAINS_POWERED;
params.NLME_JOIN_request.RxOnWhenIdle = FALSE;
params.NLME_JOIN_request.MACSecurity = FALSE;
currentPrimitive = NLME_JOIN_request;
case NLME_JOIN_confirm:
if (!params.NLME_JOIN_confirm.Status)
{
ConsolePutROMString( (ROM char *)"Join successful!\r\n" );
ZigBeeBlockTx();
TxBuffer[TxData++] = APSCounter++;
TxBuffer[TxData++] = macPIB.macShortAddress.v[0];
TxBuffer[TxData++] = macPIB.macShortAddress.v[1];
TxBuffer[TxData++] = myLongAddress.v[0];
TxBuffer[TxData++] = myLongAddress.v[1];
TxBuffer[TxData++] = myLongAddress.v[2];
TxBuffer[TxData++] = myLongAddress.v[3];
TxBuffer[TxData++] = myLongAddress.v[4];
TxBuffer[TxData++] = myLongAddress.v[5];
TxBuffer[TxData++] = myLongAddress.v[6];
TxBuffer[TxData++] = myLongAddress.v[7];
TxBuffer[TxData++] = MY_CAPABILITY_INFO;
params.APSDE_DATA_request.DstAddrMode = APS_ADDRESS_16_BIT;
params.APSDE_DATA_request.DstEndpoint = EP_ZDO;
params.APSDE_DATA_request.DstAddress.ShortAddr.Val = 0xFFFF;
//destinationAddress;
params.APSDE_DATA_request.ProfileId.Val = ZDO_PROFILE_ID;
params.APSDE_DATA_request.RadiusCounter = DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute = ROUTE_DISCOVERY_SUPPRESS;
params.APSDE_DATA_request.TxOptions.Val = 0;
params.APSDE_DATA_request.SrcEndpoint = EP_ZDO;
params.APSDE_DATA_request.ClusterId.Val = END_DEVICE_annce;
}
else
{
currentPrimitive = NO_PRIMITIVE;
PrintChar( params.NLME_JOIN_confirm.Status );
case NLME_LEAVE_indication:
{
#if defined(__C30__)
LONG_ADDR myLongAddr;
GetMACAddress(&myLongAddr);
if(!memcmppgm2ram( ¶ms.NLME_LEAVE_indication.DeviceAddress, &myLongAddr,
8 ))
#else
if (!memcmppgm2ram( ¶ms.NLME_LEAVE_indication.DeviceAddress, (ROM void
*)&macLongAddr, 8 ))
#endif
{
ConsolePutROMString( (ROM char *)"We have left the network.\r\n" );
}
else
{
ConsolePutROMString( (ROM char *)"Another node has left the network.\r\n"
);
}
}
currentPrimitive = NO_PRIMITIVE;
break;
case NLME_RESET_confirm:
ConsolePutROMString( (ROM char *)"ZigBee Stack has been reset.\r\n" );
currentPrimitive = NO_PRIMITIVE;
break;
case NLME_START_ROUTER_confirm:
if (!params.NLME_START_ROUTER_confirm.Status)
{
ConsolePutROMString( (ROM char *)"Router Started!\r\n" );
}
else
{
PrintChar( params.NLME_JOIN_confirm.Status );
ConsolePutROMString( (ROM char *)" Router start unsuccessful. We cannot route
frames.\r\n" );
}
break;
case APSDE_DATA_indication:
{
WORD_VAL attributeId;
BYTE command;
BYTE data;
BYTE frameHeader;
BYTE sequenceNumber;
BYTE transaction;
BYTE transByte;
switch (params.APSDE_DATA_indication.DstEndpoint)
{
case EP_ZDO:
#define dataLength command
frameHeader = 1;
for (transaction=0; transaction<frameHeader; transaction++)
{
sequenceNumber = APLGet();
transByte = 1; // Account for status byte
switch( params.APSDE_DATA_indication.ClusterId.Val )
{
//
********************************************************
// Put a case here to handle each ZDO response that we
requested.
//
********************************************************
case NWK_ADDR_rsp:
if (APLGet() == SUCCESS)
{
ConsolePutROMString( (ROM char *)" Receiving
NWK_ADDR_rsp.\r\n" );
#ifdef SUPPORT_END_DEVICE_BINDING
case END_DEVICE_BIND_rsp:
switch( APLGet() )
{
case SUCCESS:
ConsolePutROMString( (ROM char *)"End device
bind/unbind successful!\r\n" );
break;
case ZDO_NOT_SUPPORTED:
ConsolePutROMString( (ROM char *)"End device
bind/unbind not supported.\r\n" );
break;
case END_DEVICE_BIND_TIMEOUT:
ConsolePutROMString( (ROM char *)"End device
bind/unbind time out.\r\n" );
break;
case END_DEVICE_BIND_NO_MATCH:
ConsolePutROMString( (ROM char *)"End device
bind/unbind failed - no match.\r\n" );
break;
default:
ConsolePutROMString( (ROM char *)"End device
bind/unbind invalid response.\r\n" );
break;
}
default:
printf(" Got message on EndPoint zero ..");
break;
}
}
#undef dataLength
break;
//
************************************************************************
// Place a case for each user defined endpoint.
//
************************************************************************
default:
{
WORD_VAL clusterID = params.APSDE_DATA_indication.ClusterId;
frameHeader = 1;
for(transaction=0; transaction<frameHeader; transaction++)
{
BYTE PacketLen;
BYTE transactionNumber;
switch( clusterID.Val )
{
//*************************************************************************************************
********************
//*************************************************************************************************
********************
//*************************************************************************************************
********************
//*************************************************************************************************
********************
case TRANSMIT_COUNTED_PACKETS_CLUSTER:
{
WORD_VAL Seq;
PacketLen = APLGet();
Seq.v[0] = APLGet();
Seq.v[1] = APLGet();
if( Seq.Val > MSGPacketCount.Val )
{
MSGPacketCount.Val = Seq.Val;
}
for(i = 0; i < PacketLen-2; i++)
APLGet();
}
break;
case RESET_PACKET_COUNT_CLUSTER:
MSGPacketCount.Val = 0;
break;
case RETRIEVE_PACKET_COUNT_CLUSTER:
TxBuffer[TxData++] = sequenceNumber;
TxBuffer[TxData++] = 2;
TxBuffer[TxData++] = MSGPacketCount.v[0];
TxBuffer[TxData++] = MSGPacketCount.v[1];
MSGPacketCount.Val++;
transactionNumber = TxBuffer[frameHeaderIndex] &
APL_FRAME_COUNT_MASK;
TxBuffer[frameHeaderIndex] &= APL_FRAME_TYPE_MASK;
TxBuffer[frameHeaderIndex] |= (transactionNumber+1);
if( transactionNumber == 0 )
{
ZigBeeBlockTx();
params.APSDE_DATA_request.DstAddrMode =
params.APSDE_DATA_indication.SrcAddrMode;
params.APSDE_DATA_request.DstAddress.ShortAddr =
params.APSDE_DATA_indication.SrcAddress.ShortAddr;
params.APSDE_DATA_request.RadiusCounter =
DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute =
ROUTE_DISCOVERY_ENABLE;
#ifdef I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
i = params.APSDE_DATA_indication.SrcEndpoint;
params.APSDE_DATA_request.SrcEndpoint =
params.APSDE_DATA_indication.DstEndpoint;
params.APSDE_DATA_request.DstEndpoint = i;
params.APSDE_DATA_request.ClusterId.Val =
PACKET_COUNT_RESPONSE_CLUSTER;
currentPrimitive = APSDE_DATA_request;
}
break;
case PACKET_COUNT_RESPONSE_CLUSTER:
{
BYTE PC_LSB = APLGet();
BYTE PC_MSB = APLGet();
case BUFFER_TEST_REQUEST_CLUSTER:
{
BYTE SeqLen = APLGet();
printf("\r\n Internal message - I am in same
group!\r\n");
#ifdef I_SUPPORT_SECURITY
if( SeqLen < 66 )
#else
if( SeqLen < 84 )
#endif
{
TxBuffer[TxData++] = SeqLen;
TxBuffer[TxData++] = SUCCESS;
for(i = 0; i < SeqLen; i++)
{
TxBuffer[TxData++] = i;
}
} else {
TxBuffer[TxData++] = SeqLen;
TxBuffer[TxData++] = 0x01;
}
/* don't bother sending data to myself */
if(params.APSDE_DATA_indication.SrcAddress.ShortAddr.Val==(macPIB.macShortAddress.Val))
{
APSDiscardRx();
currentPrimitive = NO_PRIMITIVE;
break;
}
/* package and send response */
ZigBeeBlockTx();
params.APSDE_DATA_request.DstAddrMode =
params.APSDE_DATA_indication.SrcAddrMode;
params.APSDE_DATA_request.DstAddress.ShortAddr =
params.APSDE_DATA_indication.SrcAddress.ShortAddr;
params.APSDE_DATA_request.RadiusCounter =
DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute =
ROUTE_DISCOVERY_SUPPRESS;
#ifdef I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
i = params.APSDE_DATA_indication.SrcEndpoint;
params.APSDE_DATA_request.SrcEndpoint =
params.APSDE_DATA_indication.DstEndpoint;
params.APSDE_DATA_request.DstEndpoint = i;
params.APSDE_DATA_request.ClusterId.Val =
BUFFER_TEST_RESPONSE_CLUSTER;
currentPrimitive = APSDE_DATA_request;
}
/* Group 4 uses End Point 4 so here that application
* is Toggling LED1 it received a request from Group 4
*/
if(params.APSDE_DATA_indication.DstEndpoint == GROUP_ID4)
MESSAGE_INDICATION = !MESSAGE_INDICATION;
break;
case BUFFER_TEST_RESPONSE_CLUSTER:
{
BYTE len = APLGet();
printf("\r\nLen: ");
PrintChar(len);
printf("\r\n");
printf("From Address: ");
PrintChar(params.APSDE_DATA_indication.SrcAddress.ShortAddr.byte.MSB);
PrintChar(params.APSDE_DATA_indication.SrcAddress.ShortAddr.byte.LSB);
printf("\r\n");
for(i = 0; i < len+1; i++) {
PrintChar(APLGet());
}
printf("\r\n");
}
break;
case FREEFORM_MSG_REQUEST_CLUSTER:
{
BYTE requestType = APLGet();
TxBuffer[TxData++] = requestType; /* return it */
switch(requestType)
{
case 0x00:
TxBuffer[TxData++] = 0x42;
break;
case 0x01:
TxBuffer[TxData++] = 0x5a;
TxBuffer[TxData++] = 0x69;
TxBuffer[TxData++] = 0x67;
TxBuffer[TxData++] = 0x42;
TxBuffer[TxData++] = 0x65;
TxBuffer[TxData++] = 0x65;
break;
case 0x02:
TxBuffer[TxData++] = 0x12;
TxBuffer[TxData++] = 0x34;
TxBuffer[TxData++] = 0x56;
TxBuffer[TxData++] = 0x78;
break;
}
/* send response */
{
ZigBeeBlockTx();
params.APSDE_DATA_request.DstAddrMode =
params.APSDE_DATA_indication.SrcAddrMode;
params.APSDE_DATA_request.DstAddress.ShortAddr =
params.APSDE_DATA_indication.SrcAddress.ShortAddr;
params.APSDE_DATA_request.RadiusCounter =
DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute =
ROUTE_DISCOVERY_ENABLE;
#ifdef I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
i = params.APSDE_DATA_indication.SrcEndpoint;
params.APSDE_DATA_request.SrcEndpoint =
params.APSDE_DATA_indication.DstEndpoint;
params.APSDE_DATA_request.DstEndpoint = i;
params.APSDE_DATA_request.ClusterId.Val =
FREEFORM_MSG_RESPONSE_CLUSTER;
currentPrimitive = APSDE_DATA_request;
}
}
break;
case FREEFORM_MSG_RESPONSE_CLUSTER:
{
BYTE len = APLGet();
for(i = 0; i < len; i++)
APLGet();
break;
}
default:
break;
}
}
if( currentPrimitive != APSDE_DATA_request )
TxData = TX_DATA_START;
}
break;
}
APLDiscardRx();
}
break;
case APSDE_DATA_confirm:
if (params.APSDE_DATA_confirm.Status)
{
ConsolePutROMString( (ROM char *)"Error " );
PrintChar( params.APSDE_DATA_confirm.Status );
ConsolePutROMString( (ROM char *)" sending message.\r\n" );
}
else
{
ConsolePutROMString( (ROM char *)" Message sent successfully.\r\n" );
}
currentPrimitive = NO_PRIMITIVE;
break;
case APSME_ADD_GROUP_confirm:
case APSME_REMOVE_GROUP_confirm:
case APSME_REMOVE_ALL_GROUPS_confirm:
//ConsolePutROMString( (ROM char *)" Performed Group Operation.\r\n" );
currentPrimitive = NO_PRIMITIVE;
break;
case NO_PRIMITIVE:
if (!ZigBeeStatus.flags.bits.bNetworkJoined)
{
if (!ZigBeeStatus.flags.bits.bTryingToJoinNetwork)
{
if (ZigBeeStatus.flags.bits.bTryOrphanJoin)
{
ConsolePutROMString( (ROM char *)"Trying to join network as an
orphan...\r\n" );
params.NLME_JOIN_request.ScanDuration = 8;
params.NLME_JOIN_request.ScanChannels.Val = ALLOWED_CHANNELS;
params.NLME_JOIN_request.JoinAsRouter = FALSE;
params.NLME_JOIN_request.RejoinNetwork = TRUE;
params.NLME_JOIN_request.PowerSource = NOT_MAINS_POWERED;
params.NLME_JOIN_request.RxOnWhenIdle = FALSE;
params.NLME_JOIN_request.MACSecurity = FALSE;
currentPrimitive = NLME_JOIN_request;
}
else
{
ConsolePutROMString( (ROM char *)"Trying to join network as a new
device...\r\n" );
params.NLME_NETWORK_DISCOVERY_request.ScanDuration = 8;
params.NLME_NETWORK_DISCOVERY_request.ScanChannels.Val =
ALLOWED_CHANNELS;
currentPrimitive = NLME_NETWORK_DISCOVERY_request;
}
}
}
else
{
// Process the pushbutton interrupts
if( PB_RIGHT_SWITCH == 0 )
{
/* wait debounce time before taking any action again */
if(PB_RIGHT_pressed == FALSE)
{
/* release to capture another button press */
PB_RIGHT_pressed = TRUE;
currentPrimitive = APSME_ADD_GROUP_request;
GROUP_INDICATION = 1;
printf(" \r\nAdded node to group 4\r\n");
}
else
{
params.APSME_REMOVE_GROUP_request.Endpoint =
GROUP_ID4;
params.APSME_REMOVE_GROUP_request.GroupAddress.v[1] = 0x00;
params.APSME_REMOVE_GROUP_request.GroupAddress.v[0] =
GROUP_ID4;
currentPrimitive = APSME_REMOVE_GROUP_request;
GROUP_INDICATION = 0;
printf(" \r\nRemoved node from group 4\r\n");
}
group_state_ID4 = !group_state_ID4;
break;
}
}
else /* Debounce Timeout period calculation */
{
TICK t = TickGet();
tick2Difference.Val = TickGetDiff(t,PB_RIGHT_press_time);
if(tick2Difference.Val > DEBOUNCE_TIME)
{
PB_RIGHT_pressed = FALSE;
}
params.APSDE_DATA_request.SrcEndpoint = GROUP_ID4;
params.APSDE_DATA_request.RadiusCounter = DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute =
ROUTE_DISCOVERY_SUPPRESS;
#ifdef I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
break;
}
}
else /* Debounce Timeout period calculation */
{
TICK t = TickGet();
tickDifference.Val = TickGetDiff(t,PB_LEFT_press_time);
#if !defined(BOTH_MICRO_TRANSV_SLEEP)
/* check the menu to see if anything is there to process */
if (!ZigBeeStatus.flags.bits.bHasBackgroundTasks)
{
if ( ConsoleIsGetReady())
{
ProcessMenu();
}
}
#endif
}
/* Process when there are no primitives */
if (currentPrimitive == NO_PRIMITIVE)
{
if (!ZigBeeStatus.flags.bits.bDataRequestComplete)
{
// We have not received all data from our parent. If we are not
waiting
// for an answer from a data request, send a data request.
if (!ZigBeeStatus.flags.bits.bRequestingData)
{
if (ZigBeeReady())
{
// Our parent still may have data for us.
params.NLME_SYNC_request.Track = FALSE;
currentPrimitive = NLME_SYNC_request;
}
}
}
else
{
if (!ZigBeeStatus.flags.bits.bHasBackgroundTasks )
{
// There is no primitive to execute, all messages extracted
// from parent, the stack has no background tasks,
// and all application-specific processes are complete. Now
// go to sleep. Make sure that the UART is finished, turn off
the transceiver,
#if defined(BOTH_MICRO_TRANSV_SLEEP)
if(!ZigBeeStatus.flags.bits.bRadioIsSleeping)
{
while (!ConsoleIsPutReady());
#if defined(__18F4620)
INTCONbits.RBIE = 1;
#endif
#if defined(__PIC24F__)
IEC1bits.CNIE = 1;
#endif
MRF24J40Sleep();
SLEEP();
NOP();
currentTime = TickGet();
/* if the diff between current and the start time is greater
than
polling rate than initiate sync request */
/* Speed things up during EDB */
if(SendingEDBRequest == 0)
{
if( ( TickGetDiff( currentTime, startPollingTime ) ) >
(RFD_POLL_RATE))
{
params.NLME_SYNC_request.Track = FALSE;
currentPrimitive = NLME_SYNC_request;
}
else
{
/* Speed things up during End Device Binding */
if( ( TickGetDiff( currentTime, startPollingTime ) ) >
(ONE_SECOND)/4)
{
params.NLME_SYNC_request.Track = FALSE;
currentPrimitive = NLME_SYNC_request;
}
#endif
}
}
}
/* end block */
//}
break;
default:
//ConsolePutROMString( (ROM char *)" Unhandled primitive.\r\n" );
currentPrimitive = NO_PRIMITIVE;
break;
}
}
void ProcessNONZigBeeTasks(void)
{
// *********************************************************************
// Place any non-ZigBee related processing here. Be sure that the code
// will loop back and execute ZigBeeTasks() in a timely manner.
// *********************************************************************
{
}
}
BYTE extendedResponse = 0;
BYTE c;
SHORT_ADDR shortAddress;
DISABLE_WDT();
params.APSDE_DATA_request.DstAddrMode = APS_ADDRESS_16_BIT;
printf("\r\nWhat is the short address of device you want data from: ");
params.APSDE_DATA_request.DstAddress.ShortAddr.v[1] = GetMACByte();
params.APSDE_DATA_request.DstAddress.ShortAddr.v[0] = GetMACByte();
params.APSDE_DATA_request.RadiusCounter = DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute = ROUTE_DISCOVERY_SUPPRESS;
#ifdef I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
params.APSDE_DATA_request.TxOptions.bits.acknowledged = 1;
params.APSDE_DATA_request.SrcEndpoint = 1;
params.APSDE_DATA_request.DstEndpoint = 240;
params.APSDE_DATA_request.ProfileId.Val = 0x7f01;
params.APSDE_DATA_request.ClusterId.Val = BUFFER_TEST_REQUEST_CLUSTER;
currentPrimitive = APSDE_DATA_request;
break;
ZigBeeBlockTx();
params.APSDE_DATA_request.DstAddrMode = APS_ADDRESS_GROUP;
printf("\r\nPlease enter the Group ID of the Data Request: ");
params.APSDE_DATA_request.DstAddress.ShortAddr.v[1] = GetMACByte();
params.APSDE_DATA_request.DstAddress.ShortAddr.v[0] = GetMACByte();
params.APSDE_DATA_request.RadiusCounter = DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute = ROUTE_DISCOVERY_SUPPRESS;
#ifdef I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
params.APSDE_DATA_request.TxOptions.bits.acknowledged = 0;
params.APSDE_DATA_request.ProfileId.Val = 0x7f01;
params.APSDE_DATA_request.ClusterId.Val = BUFFER_TEST_REQUEST_CLUSTER;
currentPrimitive = APSDE_DATA_request;
break;
params.APSDE_DATA_request.DstAddrMode = APS_ADDRESS_16_BIT;
printf("\r\nPlease enter the short address of the destination device: ");
params.APSDE_DATA_request.DstAddress.ShortAddr.v[1] = GetMACByte();
params.APSDE_DATA_request.DstAddress.ShortAddr.v[0] = GetMACByte();
params.APSDE_DATA_request.SrcEndpoint = 1;
params.APSDE_DATA_request.DstEndpoint = 240;
params.APSDE_DATA_request.ProfileId.Val = MY_PROFILE_ID;
//params.APSDE_DATA_request.asduLength; TxData
params.APSDE_DATA_request.RadiusCounter = DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute = TRUE;
params.APSDE_DATA_request.TxOptions.bits.acknowledged = 1;
params.APSDE_DATA_request.DiscoverRoute = ROUTE_DISCOVERY_SUPPRESS;
#ifdef I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
params.APSDE_DATA_request.TxOptions.bits.acknowledged = 1;
params.APSDE_DATA_request.ClusterId.Val = TRANSMIT_COUNTED_PACKETS_CLUSTER;
ZigBeeBlockTx();
currentPrimitive = APSDE_DATA_request;
break;
case '5':
printf("\r\nPlease enter the number of bytes to send (hex): ");
temp2 = GetMACByte();
if(temp2 > 0x52)
temp2 = 0x52;
params.APSDE_DATA_request.DstAddrMode = APS_ADDRESS_GROUP;
printf("\r\nEnter the GroupID of devices to send data: ");
params.APSDE_DATA_request.DstAddress.ShortAddr.v[1] = GetMACByte();
params.APSDE_DATA_request.DstAddress.ShortAddr.v[0] = GetMACByte();
params.APSDE_DATA_request.SrcEndpoint = 1;
params.APSDE_DATA_request.DstEndpoint = 240;
params.APSDE_DATA_request.ProfileId.Val = MY_PROFILE_ID;
params.APSDE_DATA_request.RadiusCounter = DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute = TRUE;
params.APSDE_DATA_request.TxOptions.bits.acknowledged = 0;
params.APSDE_DATA_request.DiscoverRoute = ROUTE_DISCOVERY_SUPPRESS;
#ifdef I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
params.APSDE_DATA_request.ClusterId.Val = TRANSMIT_COUNTED_PACKETS_CLUSTER;
ZigBeeBlockTx();
currentPrimitive = APSDE_DATA_request;
break;
switch(c)
{
case '0':
case '1':
printf("\r\nEnter 16-bit Group ID (Hex): ");
params.APSME_ADD_GROUP_request.GroupAddress.v[1] = GetMACByte();
params.APSME_ADD_GROUP_request.GroupAddress.v[0] = GetMACByte();
if(c == '0')
{
/* Using a Fixed endpoint here to simplify things in this application */
params.APSME_ADD_GROUP_request.Endpoint = GROUP_ID4;
currentPrimitive = APSME_ADD_GROUP_request;
GROUP_INDICATION = 1;
}
else
{
params.APSME_REMOVE_GROUP_request.Endpoint = GROUP_ID4;
currentPrimitive = APSME_REMOVE_GROUP_request;
GROUP_INDICATION = 0;
}
break;
case '2':
params.APSME_REMOVE_ALL_GROUPS_request.Endpoint = GROUP_ID4;
currentPrimitive = APSME_REMOVE_ALL_GROUPS_request;
GROUP_INDICATION = 0;
break;
}
break;
case '7':
#ifdef USE_EXTERNAL_NVM
pCurrentNeighborRecord = neighborTable; //+ (WORD)neighborIndex *
(WORD)sizeof(NEIGHBOR_RECORD);
#else
pCurrentNeighborRecord = &(neighborTable[0]);
#endif
printf("\r\nShort MAC Type Rntlship ");
for ( i=0; i < MAX_NEIGHBORS; i++ )
{
printf(" | ");
if((currentNeighborRecord.deviceInfo.bits.deviceType == 0x01))
printf("RTR");
else
printf("UKN");
printf(" | ");
if(currentNeighborRecord.deviceInfo.bits.Relationship == 0x01)
printf("CHILD ");
else if(currentNeighborRecord.deviceInfo.bits.Relationship == 0x00)
printf("PARENT");
else
printf("UNKWN ");
}
#ifdef USE_EXTERNAL_NVM
pCurrentNeighborRecord += (WORD)sizeof(NEIGHBOR_RECORD);
#else
pCurrentNeighborRecord++;
#endif
}
printf("\r\n");
break;
params.NLME_ROUTE_DISCOVERY_request.Radius = DEFAULT_RADIUS;
ZigBeeBlockTx();
currentPrimitive = NLME_ROUTE_DISCOVERY_request;
break;
default:
break;
}
PrintMenu();
ENABLE_WDT();
while (!ConsoleIsGetReady());
c = ConsoleGet();
ConsolePut(c);
return c;
}
//ConsolePutROMString( (ROM char * const) "\r\n\r\nEnter last MAC byte in hex: " );
oneByte = GetHexDigit() << 4;
oneByte += GetHexDigit();
//ConsolePutROMString( (ROM char * const) "\r\n\r\n" );
return oneByte;
}
ConsolePutROMString( (ROM char * const) "\r\n\r\nEnter target short address in hex: " );
address.byte.MSB = GetHexDigit() << 4;
address.byte.MSB += GetHexDigit();
address.byte.LSB = GetHexDigit() << 4;
address.byte.LSB += GetHexDigit();
ConsolePutROMString( (ROM char * const) "\r\n\r\n" );
return address.Val;
}
#if defined(__C30__)
/*********************************************************************
* Function: void HardwareInit(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
*
* Overview: This function initializes the ZigBee hardware and is required
* before any stack operations can be performed
********************************************************************
All port directioning and SPI must be initialized before calling ZigBeeInit().
*******************************************************************************/
void HardwareInit(void)
{
SPI1CON1 = 0b0000000100111110;
SPI1STAT = 0x8000;
SPI2CON1 = 0b0000000100111110;
SPI2STAT = 0x8000;
#ifdef USE_EXTERNAL_NVM
EEPROM_nCS = 1;
EEPROM_nCS_TRIS = 0;
IFS2bits.SPI2IF = 1;
#endif
PHY_RESETn = 0;
PHY_RESETn_TRIS = 0;
PHY_CS = 1;
PHY_CS_TRIS = 0;
TRISAbits.TRISA6 = 0;
TRISAbits.TRISA7 = 0;
RFIF = 0;
RFIE = 1;
if(RF_INT_PIN == 0)
{
RFIF = 1;
}
TRISDbits.TRISD6 = 1;
TRISDbits.TRISD7 = 1;
CNEN1bits.CN15IE = 1;
CNEN2bits.CN16IE = 1;
CNPU1bits.CN15PUE = 1;
CNPU2bits.CN16PUE = 1;
IFS1bits.CNIF = 0;
IEC1bits.CNIE = 1;
PHY_WAKE = 1;
PHY_WAKE_TRIS = 0;
#else
void HardwareInit(void)
{
#ifdef USE_EXTERNAL_NVM
EEPROM_nCS = 1;
EEPROM_nCS_TRIS = 0;
#endif
#else
#error Unknown transceiver selected
#endif
RF_SSPSTAT_REG = 0x40;
RF_SSPCON1_REG = 0x21;
EE_SSPSTAT_REG = 0x40;
EE_SSPCON1_REG = 0x21;
#else
// Initialize the SPI pins and directions
LATCbits.LATC3 = 0; // SCK
LATCbits.LATC5 = 1; // SDO
TRISCbits.TRISC3 = 0; // SCK
TRISCbits.TRISC4 = 1; // SDI
TRISCbits.TRISC5 = 0; // SDO
SSPSTAT_REG = 0x40;
SSPCON1_REG = 0x20;
#endif
//-------------------------------------------------------------------------
// This section is required for application-specific hardware
// initialization.
//-------------------------------------------------------------------------
TRISBbits.TRISB4 = 1;
TRISBbits.TRISB5 = 1;
PHY_WAKE = 1;
PHY_WAKE_TRIS = 0;
}
#endif
/*******************************************************************************
User Interrupt Handler
The stack uses some interrupts for its internal processing. Once it is done
checking for its interrupts, the stack calls this function to allow for any
additional interrupt processing.
*******************************************************************************/
#if defined(__C30__)
void UserInterruptHandler(void)
{
}
IFS1bits.CNIF = 0;
}
}
#else
void UserInterruptHandler(void)
{
// *************************************************************************
// Place any application-specific interrupt processing here
// *************************************************************************
//*************************************************************************************************
********************
//*************************************************************************************************
********************
if (INTCON3bits.INT1IF == 1)
{
MEMORIA_MEDIDOR++;
printf("\r\nRecebido Pulso do medidor\r\n");
INTCON3bits.INT1IF = 0;
}
//*************************************************************************************************
********************
//*************************************************************************************************
********************
INTCONbits.RBIF = 0;
}
}
#endif
void SendCountedPacket(BYTE seqNumber, BYTE len)
{
params.APSDE_DATA_request.DstAddrMode = APS_ADDRESS_16_BIT;
params.APSDE_DATA_request.DstAddress.ShortAddr.v[1] = byteMSB;
params.APSDE_DATA_request.DstAddress.ShortAddr.v[0] = byteLSB;
params.APSDE_DATA_request.SrcEndpoint = 1;
params.APSDE_DATA_request.DstEndpoint = 240;
params.APSDE_DATA_request.ProfileId.Val = MY_PROFILE_ID;
//params.APSDE_DATA_request.asduLength; TxData
params.APSDE_DATA_request.RadiusCounter = DEFAULT_RADIUS;
params.APSDE_DATA_request.DiscoverRoute = TRUE;
params.APSDE_DATA_request.DiscoverRoute = ROUTE_DISCOVERY_ENABLE;
#ifdef
I_SUPPORT_SECURITY
params.APSDE_DATA_request.TxOptions.Val = 1;
#else
params.APSDE_DATA_request.TxOptions.Val = 0;
#endif
params.APSDE_DATA_request.TxOptions.bits.acknowledged = 1;
params.APSDE_DATA_request.ClusterId.Val = TRANSMIT_COUNTED_PACKETS_CLUSTER;
ZigBeeBlockTx();
currentPrimitive = APSDE_DATA_request;
}