📄 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
**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -