📄 sapi.c
字号:
/**************************************************************************************************
Filename: sapi.c
Revised: $Date: 2007-11-13 14:49:34 -0800 (Tue, 13 Nov 2007) $
Revision: $Revision: 15924 $
Description: Z-Stack Simple Application Interface.
Copyright 2007 Texas Instruments Incorporated. All rights reserved.
IMPORTANT: Your use of this Software is limited to those specific rights
granted under the terms of a software license agreement between the user
who downloaded the software, his/her employer (which must be your employer)
and Texas Instruments Incorporated (the "License"). You may not use this
Software unless you agree to abide by the terms of the License. The License
limits your use, and you acknowledge, that the Software may not be modified,
copied or distributed unless embedded on a Texas Instruments microcontroller
or used solely and exclusively in conjunction with a Texas Instruments radio
frequency transceiver, which is integrated into your product. Other than for
the foregoing purpose, you may not use, reproduce, copy, prepare derivative
works of, modify, distribute, perform, display or sell this Software and/or
its documentation for any purpose.
YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
PROVIDED 揂S 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
TEXAS INSTRUMENTS 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.
Should you have any questions regarding your right to use this Software,
contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/
/******************************************************************************
* INCLUDES
*/
#include "ZComDef.h"
#include "hal_drivers.h"
#include "OSAL.h"
#include "OSAL_Tasks.h"
#include "OSAL_Custom.h"
#if defined ( MT_TASK )
#include "MTEL.h"
#endif
#include "nwk.h"
#include "APS.h"
#include "ZDApp.h"
#include "osal_nv.h"
#include "NLMEDE.h"
#include "AF.h"
#include "OnBoard.h"
#include "nwk_util.h"
#include "ZDProfile.h"
#include "ZDObject.h"
#include "hal_led.h"
#include "hal_key.h"
#include "sapi.h"
#include "MT_SAPI.h"
#ifdef LCD_SUPPORTED
#include "lcd128_64.h"
#endif
extern uint8 zgStartDelay;
extern uint8 zgSapiEndpoint;
/*********************************************************************
* CONSTANTS
*/
// Message ID's for application user messages must be in 0xE0-0xEF range
#define ZB_USER_MSG 0xE0
#define SAPICB_DATA_CNF 0xE0
#define SAPICB_BIND_CNF 0xE1
#define SAPICB_START_CNF 0xE2
/*********************************************************************
* TYPEDEFS
*/
/*********************************************************************
* GLOBAL VARIABLES
*/
// The order in this table must be identical to the task initialization calls below in osalInitTask.
const pTaskEventHandlerFn tasksArr[] = {
macEventLoop,
nwk_event_loop,
Hal_ProcessEvent,
#if defined( MT_TASK )
MT_ProcessEvent,
#endif
APS_event_loop,
ZDApp_event_loop,
SAPI_ProcessEvent
};
const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
uint16 *tasksEvents;
endPointDesc_t sapi_epDesc;
uint8 sapi_TaskID;
static uint16 sapi_bindInProgress;
/*********************************************************************
* LOCAL FUNCTIONS
*/
void SAPI_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
static void SAPI_SendCback( uint8 event, uint8 status, uint16 data );
static void SAPI_StartConfirm( uint8 status );
static void SAPI_SendDataConfirm( uint8 handle, uint8 status );
static void SAPI_BindConfirm( uint16 commandId, uint8 status );
static void SAPI_FindDeviceConfirm( uint8 searchType,
uint8 *searchKey, uint8 *result );
static void SAPI_ReceiveDataIndication( uint16 source,
uint16 command, uint16 len, uint8 *pData );
static void SAPI_AllowBindConfirm( uint16 source );
/******************************************************************************
* @fn zb_SystemReset
*
* @brief The zb_SystemReset function reboots the ZigBee device. The
* zb_SystemReset function can be called after a call to
* zb_WriteConfiguration to restart Z-Stack with the updated
* configuration.
*
* @param none
*
* @return none
*/
void zb_SystemReset ( void )
{
SystemReset();
}
/******************************************************************************
* @fn zb_StartRequest
*
* @brief The zb_StartRequest function starts the ZigBee stack. When the
* ZigBee stack starts, the device reads configuration parameters
* from Nonvolatile memory and the device joins its network. The
* ZigBee stack calls the zb_StartConrifm callback function when
* the startup process completes.
*
* @param none
*
* @return none
*/
void zb_StartRequest()
{
uint8 logicalType;
// Start the device
// start delay = min(NWK_START_DELAY, zgStartDelay) + rand() - only for fresh start, not restore
if ( zgStartDelay < NWK_START_DELAY )
zgStartDelay = 0;
else
zgStartDelay -= NWK_START_DELAY;
// check that bad combinations of compile flag definitions and device type
zb_ReadConfiguration( ZCD_NV_LOGICAL_TYPE, sizeof(uint8), &logicalType );
if ( ( logicalType > ZG_DEVICETYPE_ENDDEVICE ) ||
#if defined( RTR_NWK )
#if defined( ZDO_COORDINATOR )
// Only RTR or Coord possible
( logicalType == ZG_DEVICETYPE_ENDDEVICE ) ||
#else
// Only RTR possible
( logicalType != ZG_DEVICETYPE_ROUTER ) ||
#endif
#else
#if defined( ZDO_COORDINATOR )
// Error
( 1 ) ||
#else
// only ED possible
( logicalType != ZG_DEVICETYPE_ENDDEVICE ) ||
#endif
#endif
( 0 ) )
{
// error configuration
SAPI_SendCback( SAPICB_START_CNF, ZInvalidParameter, 0 );
}
else
{
ZDOInitDevice(zgStartDelay);
}
return;
}
/******************************************************************************
* @fn zb_BindDevice
*
* @brief The zb_BindDevice function establishes or removes a 慴inding? * between two devices. Once bound, an application can send
* messages to a device by referencing the commandId for the
* binding.
*
* @param create - TRUE to create a binding, FALSE to remove a binding
* commandId - The identifier of the binding
* pDestination - The 64-bit IEEE address of the device to bind to
*
* @return The status of the bind operation is returned in the
* zb_BindConfirm callback.
*/
void zb_BindDevice ( uint8 create, uint16 commandId, uint8 *pDestination )
{
zAddrType_t destination;
uint8 ret = ZB_ALREADY_IN_PROGRESS;
if ( create )
{
if (sapi_bindInProgress == 0xffff)
{
if ( pDestination )
{
destination.addrMode = Addr64Bit;
osal_cpyExtAddr( destination.addr.extAddr, pDestination );
ret = APSME_BindRequest( sapi_epDesc.endPoint, commandId,
&destination, sapi_epDesc.endPoint );
if ( ret == ZSuccess )
{
// Find nwk addr
ZDP_NwkAddrReq(pDestination, ZDP_ADDR_REQTYPE_SINGLE, 0, 0 );
osal_start_timerEx( ZDAppTaskID, ZDO_NWK_UPDATE_NV, 250 );
}
}
else
{
ret = ZB_INVALID_PARAMETER;
destination.addrMode = Addr16Bit;
destination.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
if ( ZDO_AnyClusterMatches( 1, &commandId, sapi_epDesc.simpleDesc->AppNumOutClusters,
sapi_epDesc.simpleDesc->pAppOutClusterList ) )
{
// Try to match with a device in the allow bind mode
ret = ZDP_MatchDescReq( &destination, NWK_BROADCAST_SHORTADDR,
sapi_epDesc.simpleDesc->AppProfId, 1, &commandId, 0, (cId_t *)NULL, 0 );
}
else if ( ZDO_AnyClusterMatches( 1, &commandId, sapi_epDesc.simpleDesc->AppNumInClusters,
sapi_epDesc.simpleDesc->pAppInClusterList ) )
{
ret = ZDP_MatchDescReq( &destination, NWK_BROADCAST_SHORTADDR,
sapi_epDesc.simpleDesc->AppProfId, 0, (cId_t *)NULL, 1, &commandId, 0 );
}
if ( ret == ZB_SUCCESS )
{
// Set a timer to make sure bind completes
osal_start_timerEx(sapi_TaskID, ZB_BIND_TIMER, AIB_MaxBindingTime);
sapi_bindInProgress = commandId;
return; // dont send cback event
}
}
}
SAPI_SendCback( SAPICB_BIND_CNF, ret, commandId );
}
else
{
// Remove local bindings for the commandId
BindingEntry_t *pBind;
// Loop through bindings an remove any that match the cluster
while ( pBind = bindFind( sapi_epDesc.simpleDesc->EndPoint, commandId, 0 ) )
{
bindRemoveEntry(pBind);
}
osal_start_timerEx( ZDAppTaskID, ZDO_NWK_UPDATE_NV, 250 );
}
return;
}
/******************************************************************************
* @fn zb_PermitJoiningRequest
*
* @brief The zb_PermitJoiningRequest function is used to control the
* joining permissions and thus allow or disallow new devices from
* joining the network.
*
* @param destination - The destination parameter indicates the address
* of the device for which the joining permissions
* should be set. This is usually the local device
* address or the special broadcast address that denotes
* all routers and coordinator ( 0xFFFC ). This way
* the joining permissions of a single device or the
* whole network can be controlled.
* timeout - Indicates the amount of time in seconds for which
* the joining permissions should be turned on.
* If timeout is set to 0x00, the device will turn off the
* joining permissions indefinitely. If it is set to 0xFF,
* the joining permissions will be turned on indefinitely.
*
*
* @return ZB_SUCCESS or a failure code
*
*/
uint8 zb_PermitJoiningRequest ( uint16 destination, uint8 timeout )
{
#if defined( ZDO_MGMT_PERMIT_JOIN_REQUEST )
zAddrType_t dstAddr;
dstAddr.addrMode = Addr16Bit;
dstAddr.addr.shortAddr = destination;
return( (uint8) ZDP_MgmtPermitJoinReq( &dstAddr, timeout, 0, 0 ) );
#else
return ZUnsupportedMode;
#endif
}
/******************************************************************************
* @fn zb_AllowBind
*
* @brief The zb_AllowBind function puts the device into the
* Allow Binding Mode for a given period of time. A peer device
* can establish a binding to a device in the Allow Binding Mode
* by calling zb_BindDevice with a destination address of NULL
*
* @param timeout - The number of seconds to remain in the allow binding
* mode. Valid values range from 1 through 65.
* If 0, the Allow Bind mode will be set false without TO
* If greater than 64, the Allow Bind mode will be true
*
* @return ZB_SUCCESS if the device entered the allow bind mode, else
* an error code.
*/
void zb_AllowBind ( uint8 timeout )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -