www.pudn.com > lightlamp.rar > ZigBeeIf.cpp


/* 
** ============================================================================ 
** 
** FILE 
**  ZigBeeIf.cpp 
** 
** DESCRIPTION 
**  The CZigBeeIf class itself 
** 
** CREATED 
**  I.A.Marsden Integration UK Ltd 
** 
** COPYRIGHT 
** Copyright 2005 Integration Associates Inc.  All rights reserved. 
** 
** LIMITED USE LICENSE.  By using this software, the user agrees to the terms of the  
**                       following license.  If the user does not agree to these terms,  
**                       then this software should be returned within 30 days and a full  
**                       refund of the purchase price or license fee will provided.   
**                       Integration Associates hereby grants a license to the user on the  
**                       following terms and conditions:  The user may use, copy, modify,  
**                       revise, translate, abridge, condense, expand, collect, compile,  
**                       link, recast, distribute, transform or adapt this software solely  
**                       in connection with the development of products incorporating  
**                       integrated circuits sold by Integration Associates.  Any other use  
**                       for any other purpose is expressly prohibited with the prior written  
**                       consent of Integration Associates. 
** 
** Any copy or modification made must satisfy the following conditions: 
**  
** 1. Both the copyright notice and this permission notice appear in all copies of the software,  
**    derivative works or modified versions, and any portions thereof, and that both notices  
**    appear in supporting documentation. 
** 
** 2. All copies of the software shall contain the following acknowledgement: "Portions of this  
**    software are used under license from Integration Associates Inc. and are copyrighted." 
** 
** 3  Neither the name of Integration Associates Inc. nor any of its subsidiaries may be used  
**    to endorse or promote products derived from this software without specific prior written  
**    permission. 
** 
** THIS SOFTWARE IS PROVIDED BY "AS IS" AND ALL WARRANTIES OF ANY KIND, INCLUDING THE IMPLIED  
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR USE, ARE EXPRESSLY DISCLAIMED.  THE DEVELOPER  
** SHALL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.   
** THIS SOFTWARE MAY NOT BE USED IN PRODUCTS INTENDED FOR USE IN IMPLANTATION OR OTHER DIRECT  
** LIFE SUPPORT APPLICATIONS WHERE MALFUNCTION MAY RESULT IN THE DIRECT PHYSICAL HARM OR INJURY  
** TO PERSONS.  ALL SUCH IS USE IS EXPRESSLY PROHIBITED. 
** ============================================================================ 
*/ 
#include "stdafx.h" 
#include "ZigBeeIf.h" 
#include "ZBDLLApi.h" 
#include "bisync_tokens.h" 
 
/* 
** ============================================================================ 
** Callback Function Prototypes 
** ============================================================================ 
*/ 
void CALLBACK ZigBeeReceiveData( unsigned char msglength, unsigned char* msg ); 
void CALLBACK ZigBeeDebugData( unsigned char msglength, unsigned char* msg ); 
void CALLBACK ZigBeeTimerProc( HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime ); 
 
CZigBeeIf* m_ZigBeeIf = NULL; 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  CZigBeeIf 
** 
** DESCRIPTION 
**  Constructor, Reset all variables 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
CZigBeeIf::CZigBeeIf() 
{ 
  if ( m_ZigBeeIf != NULL ) return; // Single instatioation only 
  m_ZigBeeIf = this; // The single instantiation 
  hinstLib = NULL;  
  ZeroMemory( callback_table, 255 ); 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ~CZigBeeIf 
** 
** DESCRIPTION 
**  Destructor, close any open USB devices 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
CZigBeeIf::~CZigBeeIf() 
{ 
  ZBIFDisconnect(); // Disconnect the current device 
  m_ZigBeeIf = NULL; // Null the instance of the interface 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZBIFConnect 
** 
** DESCRIPTION 
**  Attempt to connect to the next free device 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
int CZigBeeIf::ZBIFConnect(HWND hWnd) 
{ 
  CHAR Address[30]; 
  ZeroMemory( Address, 30 ); // Memory used for receiving the IEEE Address of the device connected to 
 
  if (hinstLib != NULL ) return ZBIF_ERROR_DEVICE_ALREADY_CONNECTED; // Check if we are already connected 
 
  // Get a handle to the DLL module. 
  hinstLib = LoadLibrary("ZBDLL.dll");  
 
  // If the handle is valid, try to get the function address. 
  if (hinstLib != NULL)  
  {  
    ZBDLLPnpEvent = (ZBPnpEvent_t) GetProcAddress(hinstLib, "ZBPnpEvent");  
    ZBDLLConnect = (ZBConnect_t) GetProcAddress(hinstLib, "ZBConnect");  
    ZBDLLDisconnect = (ZBDisconnect_t) GetProcAddress(hinstLib, "ZBDisconnect");  
    ZBDLLSend = (ZBSend_t) GetProcAddress(hinstLib, "ZBSend");  
    ZBDLLTick32Hz = (ZBTick32Hz_t) GetProcAddress(hinstLib, "ZBTick32Hz");  
 
    // If the function address is valid, call the function. 
    if ((ZBDLLDisconnect == NULL) ||  
        (ZBDLLSend == NULL) || 
        (ZBDLLConnect == NULL) ||  
        (ZBDLLPnpEvent == NULL) || 
        (ZBDLLTick32Hz == NULL)) 
    { 
      // Free the DLL module. 
      FreeLibrary(hinstLib);  
      hinstLib = NULL;   
      return ZBIF_ERROR_FUNCTIONS_MISSING; 
    } 
  }  
  else 
  { 
    // Load library failed 
    return ZBIF_ERROR_LOAD_LIBARY; 
  } 
   
  ptr_Wnd = hWnd; // Save the handle to the parent window 
//  int retval = (ZBDLLConnect)( (LPARAM)hWnd, ZigBeeReceiveData, CONNECT_USB, &Address[0], ZigBeeDebugData ); 
  int retval = (ZBDLLConnect)( (LPARAM)hWnd, ZigBeeReceiveData, CONNECT_USB, &Address[0], NULL ); 
  if (retval >= STK_STATUS_BASE) // failed to find a device 
  { 
    // Failed to find a device 
    FreeLibrary(hinstLib);  
    hinstLib = NULL; 
    return retval; 
  } 
 
  // Successful so start the stack 
 	m_ZBTimer = SetTimer( ptr_Wnd, 0, 31, (TIMERPROC)ZigBeeTimerProc ); // Start the stack ticking 
  if ( m_ZBTimer == NULL ) return ZBIF_ERROR_CREATING_TIMER; 
 
// 	CFile LogFile; 
//  LogFile.Remove( "DebugLog.txt" ); 
 
  return STK_SUCCESS; 
 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZBIFDisconnect 
** 
** DESCRIPTION 
**  Attempt to disconnnect from the current device 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
int CZigBeeIf::ZBIFDisconnect(void) 
{ 
  int retval; 
 
  if (hinstLib == NULL ) return ZBIF_NO_DEVICE_CONNECTED; // Check to see if we are connected 
 
  KillTimer( ptr_Wnd, m_ZBTimer );// Kill the stack tick 
  retval = (ZBDLLDisconnect)(); // Disconnect from the device 
  FreeLibrary(hinstLib); // & release the library 
  hinstLib = NULL; 
  return retval; 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZBIFSubscribe 
** 
** DESCRIPTION 
**  Subscribe to a particular callback 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
int CZigBeeIf::ZBIFSubscribe( UCHAR primitive ) 
{ 
  callback_table[primitive] = 1; // Subscribed 
  return STK_SUCCESS; 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZigBeeReceiveData 
** 
** DESCRIPTION 
**  ZigBee Receive Data Callback 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CALLBACK ZigBeeReceiveData( unsigned char msglength, unsigned char* msg ) 
{ 
  // If application subscribed for this message 
  if ( m_ZigBeeIf->callback_table[msg[1]] == 1 ) 
  { 
    ::PostMessage( m_ZigBeeIf->ptr_Wnd, WM_USER+msg[1], (WPARAM) msglength, (LPARAM) msg); // Post Message 
  } 
  else 
  { 
    // Tidy up ourselves 
    delete [] msg; 
  } 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZigBeeDebugData 
** 
** DESCRIPTION 
**  ZigBee Debug Data Callback 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CALLBACK ZigBeeDebugData( unsigned char msglength, unsigned char* msg ) 
{ 
  CTime nTime = CTime::GetCurrentTime(); // Time of debug message 
 
 	CFile LogFile; // File to use 
  if ( LogFile.Open( "DebugLog.txt", CFile::modeWrite ) == 0 ) 
  { 
    if ( LogFile.Open( "DebugLog.txt", CFile::modeCreate | CFile::modeWrite ) == 0 ) 
    { 
      delete [] msg; // Failed to open the file so just tidy up 
      return; 
    } 
  } 
  LogFile.SeekToEnd(); // Seek to the end 
 
  char list_string[512]; 
  CString tempstring; 
  tempstring.Format( "%d:%d:%d:%d ", nTime.GetHour(), nTime.GetMinute(), nTime.GetSecond(), GetTickCount() ); // Print time 
  memcpy( &list_string[0], msg, msglength ); // Copy message 
  list_string[msglength] = '\0'; // Terminate it 
  tempstring += list_string; // Add to display string 
  tempstring += '\r'; 
  tempstring += '\n'; 
  LogFile.Write( tempstring, tempstring.GetLength() ); // Read the address 
	LogFile.Close(); // Close the file again 
 
  delete [] msg; 
 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZigBeeTimerProc 
** 
** DESCRIPTION 
**  ZigBee Timer Callback 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CALLBACK ZigBeeTimerProc( HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime ) 
{ 
  (m_ZigBeeIf->ZBDLLTick32Hz)(); // Tick the stack 
} 
 
/* 
** ============================================================================ 
** 
** ZIGBEE INTERFACE DOWNLINK MESSAGES 
** 
** ============================================================================ 
*/ 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  AF_DIRECT_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::AF_DIRECT_request 
( 
    unsigned short  DstShort, 
    unsigned char   DstEndpoint, 
    unsigned char   SrcEndpoint, 
    unsigned char   ClusterId, 
    unsigned char   afduLength, 
    unsigned char*  afdu, 
    unsigned char   TxOptions, 
    unsigned char   DiscoverRoute, 
    unsigned char   BroadcastRadius, 
	unsigned char   afduHandle 
) 
{ 
  unsigned char data[130]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = AF_DIRECT_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  data[i++] = DstEndpoint; 
  data[i++] = SrcEndpoint; 
  data[i++] = ClusterId; 
  data[i++] = afduLength; 
  for( ; afduLength>0 ; afduLength-- ) data[i++] = *afdu++; 
  data[i++] = TxOptions; 
  data[i++] = DiscoverRoute; 
  data[i++] = BroadcastRadius; 
  data[i++] = afduHandle; 
  data[0] = i - 1; 
   
  (ZBDLLSend)( i, &data[0] ); // Send the message 
     
} // End of AF_DIRECT_request 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  AF_INDIRECT_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::AF_INDIRECT_request 
( 
    unsigned char   SrcEndpoint, 
    unsigned char   ClusterId, 
    unsigned char   afduLength, 
    unsigned char*  afdu, 
    unsigned char   TxOptions, 
    unsigned char   DiscoverRoute, 
    unsigned char   BroadcastRadius, 
	unsigned char   afduHandle 
) 
{ 
  unsigned char data[130]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = AF_INDIRECT_REQUEST; 
  data[i++] = SrcEndpoint; 
  data[i++] = ClusterId; 
  data[i++] = afduLength; 
  for( ; afduLength>0 ; afduLength-- ) data[i++] = *afdu++; 
  data[i++] = TxOptions; 
  data[i++] = DiscoverRoute; 
  data[i++] = BroadcastRadius; 
  data[i++] = afduHandle; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
 
} // End of AF_INDIRECT_request 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_RESET_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_RESET_request 
(  
    unsigned char type  
) 
{ 
  unsigned char data[3]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_RESET_REQUEST; 
  data[i++] = type; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_SET_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_SET_request 
(  
    unsigned char Attribute,  
    unsigned char AttributeLength,  
    unsigned char *AttributeValue  
) 
{ 
  unsigned char data[128]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_SET_REQUEST; 
  data[i++] = Attribute; 
  data[i++] = AttributeLength; 
  for( ; AttributeLength>0 ; AttributeLength-- ) data[i++] = *AttributeValue++; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_GET_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_GET_request 
(  
    unsigned char Attribute  
) 
{ 
  unsigned char data[3]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_GET_REQUEST; 
  data[i++] = Attribute; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_START_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_START_request 
( 
    void 
) 
{ 
  unsigned char data[2]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_START_REQUEST; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_IEEE_ADDR_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_IEEE_ADDR_request 
(  
    unsigned short Network_Address,  
    unsigned char Request_type,  
    unsigned char Start_Index,  
    unsigned char TransactionType  
) 
{ 
  unsigned char data[7]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_IEEE_ADDR_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  data[i++] = Request_type; 
  data[i++] = Start_Index; 
  data[i++] = TransactionType; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_NODE_DESC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_NODE_DESC_request 
(  
    unsigned short Network_Address  
) 
{ 
  unsigned char data[4]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_NODE_DESC_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_POWER_DESC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_POWER_DESC_request 
(  
    unsigned short Network_Address  
) 
{ 
  unsigned char data[4]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_POWER_DESC_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_SIMPLE_DESC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_SIMPLE_DESC_request 
(  
    unsigned short Network_Address,  
    unsigned char Endpoint  
) 
{ 
  unsigned char data[5]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_SIMPLE_DESC_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  data[i++] = Endpoint; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_ACTIVE_EP_DESC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_ACTIVE_EP_DESC_request 
(  
    unsigned short Network_Address  
) 
{ 
  unsigned char data[4]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_ACTIVE_EP_DESC_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_MATCH_DESC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_MATCH_DESC_request 
(  
    unsigned short Network_Address,  
    unsigned short ProfileId,  
    unsigned char nic,  
    unsigned char* iclist,  
    unsigned char noc,  
    unsigned char* oclist  
) 
{ 
  unsigned char data[128]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_MATCH_DESC_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  data[i++] = ProfileId & 0xFF; 
  data[i++] = (ProfileId>>8) & 0xFF; 
  data[i++] = nic; 
  for( ; nic>0 ; nic-- ) data[i++] = *iclist++; 
  data[i++] = noc; 
  for( ; noc>0 ; noc-- ) data[i++] = *oclist++; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_COMPLEX_DESC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_COMPLEX_DESC_request 
(  
    unsigned short Network_Address  
) 
{ 
  unsigned char data[4]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_COMPLEX_DESC_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_USER_DESC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_USER_DESC_request 
(  
    unsigned short Network_Address  
) 
{ 
  unsigned char data[4]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_USER_DESC_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_DISCOVERY_REGISTER_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_DISCOVERY_REGISTER_request 
(  
    unsigned short Network_Address,  
    unsigned char* IEEE_Address  
) 
{ 
  unsigned char data[12]; 
  unsigned char i = 1; 
  unsigned char j; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_DISCOVERY_REGISTER_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = *IEEE_Address++; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_END_DEVICE_ANNCE_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_END_DEVICE_ANNCE_request 
(  
    unsigned short Network_Address,  
    unsigned char* IEEE_Address  
) 
{ 
  unsigned char data[12]; 
  unsigned char i = 1; 
  unsigned char j; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_END_DEVICE_ANNCE_REQUEST; 
  data[i++] = Network_Address & 0xFF; 
  data[i++] = (Network_Address>>8) & 0xFF; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = *IEEE_Address++; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_USER_DESC_SET_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_USER_DESC_SET_request 
(  
    unsigned short NWKAddr,  
    unsigned char length,  
    unsigned char* text  
) 
{ 
  unsigned char data[128]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_USER_DESC_SET_REQUEST; 
  data[i++] = NWKAddr & 0xFF; 
  data[i++] = (NWKAddr>>8) & 0xFF; 
  data[i++] = length; 
  for ( ; length>0 ; length-- ) data[i++] = *text++; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_PERMIT_JOINING_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_PERMIT_JOINING_request 
(  
    unsigned short DstShort,  
    unsigned char Significance,  
    unsigned char Duration  
) 
{ 
  unsigned char data[6]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_USER_DESC_SET_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  data[i++] = Significance; 
  data[i++] = Duration; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_END_DEVICE_BIND_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_END_DEVICE_BIND_request 
(  
    unsigned short LocalCoordinator, 
    unsigned char  DEP, 
    unsigned short ProfileID, 
    unsigned char  NumInClusters, 
    unsigned char* InClusterList, 
    unsigned char  NumOutClusters, 
    unsigned char* OutClusterList  
) 
{ 
  unsigned char data[128]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_END_DEVICE_BIND_REQUEST; 
  data[i++] = LocalCoordinator & 0xFF; 
  data[i++] = (LocalCoordinator>>8) & 0xFF; 
  data[i++] = DEP; 
  data[i++] = ProfileID & 0xFF; 
  data[i++] = (ProfileID>>8) & 0xFF; 
  data[i++] = NumInClusters; 
  for( ; NumInClusters>0 ; NumInClusters-- ) data[i++] = *InClusterList++; 
  data[i++] = NumOutClusters; 
  for( ; NumOutClusters>0 ; NumOutClusters-- ) data[i++] = *OutClusterList++; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_BIND_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_BIND_request 
(             
    unsigned short DstShort,  
    unsigned char* SrcAddress, 
    unsigned char  SrcEndPoint, 
    unsigned char  ClusterID, 
    unsigned char* DstAddress, 
    unsigned char  DstEndPoint  
) 
{ 
  unsigned char data[23]; 
  unsigned char i = 1; 
  unsigned char j; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_BIND_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = *SrcAddress++; 
  data[i++] = SrcEndPoint; 
  data[i++] = ClusterID; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = *DstAddress++; 
  data[i++] = DstEndPoint; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_UNBIND_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_UNBIND_request 
(           
    unsigned short DstShort,  
    unsigned char* SrcAddress, 
    unsigned char  SrcEndPoint, 
    unsigned char  ClusterID, 
    unsigned char* DstAddress, 
    unsigned char  DstEndPoint  
) 
{ 
  unsigned char data[23]; 
  unsigned char i = 1; 
  unsigned char j; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_UNBIND_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = *SrcAddress++; 
  data[i++] = SrcEndPoint; 
  data[i++] = ClusterID; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = *DstAddress++; 
  data[i++] = DstEndPoint; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_MGMT_NWK_DISC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_MGMT_NWK_DISC_request 
(  
    unsigned short DstShort,  
    unsigned long ScanChannels,  
    unsigned char ScanDuration,  
    unsigned char StartIndex 
) 
{ 
  unsigned char data[10]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_MGMT_NWK_DISC_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  data[i++] = (unsigned char)ScanChannels & 0xFF; 
  data[i++] = (unsigned char)(ScanChannels>>8) & 0xFF; 
  data[i++] = (unsigned char)(ScanChannels>>16) & 0xFF; 
  data[i++] = (unsigned char)(ScanChannels>>24) & 0xFF; 
  data[i++] = ScanDuration; 
  data[i++] = StartIndex; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_MGMT_LQI_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_MGMT_LQI_request 
(  
    unsigned short DstShort,  
    unsigned char StartIndex  
) 
{ 
  unsigned char data[5]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_MGMT_LQI_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  data[i++] = StartIndex; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_MGMT_RTG_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_MGMT_RTG_request 
(  
    unsigned short DstShort,  
    unsigned char StartIndex  
) 
{ 
  unsigned char data[5]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_MGMT_RTG_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  data[i++] = StartIndex; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_MGMT_BIND_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_MGMT_BIND_request 
(  
    unsigned short DstShort,  
    unsigned char StartIndex  
) 
{ 
  unsigned char data[5]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_MGMT_BIND_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  data[i++] = StartIndex; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_MGMT_LEAVE_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_MGMT_LEAVE_request 
(  
    unsigned short DstShort,  
    unsigned char DeviceAddress[8]  
) 
{ 
  unsigned char data[12]; 
  unsigned char i = 1; 
  unsigned char j; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_MGMT_LEAVE_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = DeviceAddress[j]; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_MGMT_DIRECT_JOIN_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_MGMT_DIRECT_JOIN_request 
(  
    unsigned short DstShort,  
    unsigned char DeviceAddress[8],  
    unsigned char MacCapability  
) 
{ 
  unsigned char data[13]; 
  unsigned char i = 1; 
  unsigned char j; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_MGMT_DIRECT_JOIN_REQUEST; 
  data[i++] = DstShort & 0xFF; 
  data[i++] = (DstShort>>8) & 0xFF; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = DeviceAddress[j]; 
  data[i++] = MacCapability; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_NLME_PERMIT_JOINING_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_NLME_PERMIT_JOINING_request 
(  
    unsigned char Duration  
) 
{ 
  unsigned char data[3]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_NLME_PERMIT_JOINING_REQUEST; 
  data[i++] = Duration; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_NLME_SYNC_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_NLME_SYNC_request 
(  
    unsigned char Track  
) 
{ 
  unsigned char data[3]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_NLME_SYNC_REQUEST; 
  data[i++] = Track; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_NLME_LEAVE_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_NLME_LEAVE_request 
(  
    unsigned char* Device,  
    unsigned char  RemoveChildren,  
    unsigned char  MacSecurityEnable 
) 
{ 
  unsigned char data[12]; 
  unsigned char i = 1; 
  unsigned char j; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_NLME_LEAVE_REQUEST; 
  for ( j=0 ; j<8 ; j++ ) data[i++] = Device[j]; 
  data[i++] = RemoveChildren; 
  data[i++] = MacSecurityEnable; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
} 
 
/* 
** ============================================================================ 
** 
** FUNCTION NAME: 
**  ZDO_NLME_NETWORK_DISCOVERY_request 
** 
** AUTHOR 
**  Ian Marsden 
** 
** ============================================================================ 
*/ 
void CZigBeeIf::ZDO_NLME_NETWORK_DISCOVERY_request 
(  
    unsigned long ScanChannels,  
    unsigned char ScanDuration  
) 
{ 
  unsigned char data[7]; 
  unsigned char i = 1; 
 
  if (hinstLib == NULL ) return; // Not connected so cannot send this message 
 
  // Build the message buffer 
  data[i++] = ZDO_MGMT_NWK_DISC_REQUEST; 
  data[i++] = (unsigned char)ScanChannels & 0xFF; 
  data[i++] = (unsigned char)(ScanChannels>>8) & 0xFF; 
  data[i++] = (unsigned char)(ScanChannels>>16) & 0xFF; 
  data[i++] = (unsigned char)(ScanChannels>>24) & 0xFF; 
  data[i++] = ScanDuration; 
  data[0] = i - 1; 
  (ZBDLLSend)( i, &data[0] ); // Send the message 
}