/******************************************************************************
Filename: zcl_ota.c
Revised: $Date: 2011-07-18 05:10:28 -0700 (Mon, 18 Jul 2011) $
Revision: $Revision: 26827 $
Description: Zigbee Cluster Library - Over-the-Air Upgrade Cluster ( OTA )
Copyright 2010-2011 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 �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
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 "OSAL.h"
#include "zcl.h"
#include "zcl_general.h"
#include "zcl_ota.h"
#include "ota_common.h"
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_ota.h"
#include "MT_OTA.h"
#include "ZDProfile.h"
#include "ZDObject.h"
#if defined ( INTER_PAN )
#include "stub_aps.h"
#endif
#if defined OTA_MMO_SIGN
#include "ota_signature.h"
#endif
/******************************************************************************
* MACROS
*/
/******************************************************************************
* CONSTANTS
*/
#define OTA_MAX_TRANSACTIONS 4
#define OTA_TRANSACTION_EXPIRATION 1500
#define ZCL_OTA_HDR_LEN_OFFSET 6 // Header length location in OTA upgrade image
#define ZCL_OTA_STK_VER_OFFSET 18 // Stack version location in OTA upgrade image
/******************************************************************************
* GLOBAL VARIABLES
*/
// OTA attribute variables
uint8 zclOTA_UpgradeServerID[Z_EXTADDR_LEN];
uint32 zclOTA_FileOffset = 0xFFFFFFFF;
uint32 zclOTA_CurrentFileVersion = 0xFFFFFFFF;
uint16 zclOTA_CurrentZigBeeStackVersion;
uint32 zclOTA_DownloadedFileVersion = 0xFFFFFFFF;
uint16 zclOTA_DownloadedZigBeeStackVersion = 0xFFFF;
uint8 zclOTA_ImageUpgradeStatus;
// Other OTA variables
uint16 zclOTA_ManufacturerId; // Manufacturer ID
uint16 zclOTA_ImageType; // Image type
afAddrType_t zclOTA_serverAddr; // Server address
uint8 zclOTA_AppTask = 0xFF; // Callback Task ID
zclOTA_QueryImageRspParams_t queryResponse; // Global variable for sent query response
zclOTA_ImagePageReqParams_t Pagenextparam;
afAddrType_t PagesrcAddr;
uint16 PageCount;
uint32 Imagesize;
uint8 Maxdatasize;
uint8 Nodecnt=0;
zclOTA_PageReqParams_t Nodeinfo[7];
/******************************************************************************
* LOCAL VARIABLES
*/
// Other OTA variables
// Task ID
static uint8 zclOTA_TaskID;
// Sequence number
static uint8 zclOTA_SeqNo = 0;
static uint8 zclOTA_Permit = TRUE;
#if defined OTA_MMO_SIGN
static OTA_MmoCtrl_t zclOTA_MmoHash;
static uint8 zclOTA_DataToHash[OTA_MMO_HASH_SIZE];
static uint8 zclOTA_HashPos;
static uint8 zclOTA_SignerIEEE[Z_EXTADDR_LEN];
static uint8 zclOTA_SignatureData[OTA_SIGNATURE_LEN];
static uint8 zclOTA_Certificate[OTA_CERTIFICATE_LEN];
#endif // OTA_MMO_SIGN
#if (defined OTA_CLIENT) && (OTA_CLIENT == TRUE)
static uint32 zclOTA_DownloadedImageSize; // Downloaded image size
static uint16 zclOTA_HeaderLen; // Image header length
static uint16 zclOTA_UpdateDelay;
static zclOTA_FileID_t zclOTA_CurrentDlFileId;
static uint16 zclOTA_ElementTag;
static uint32 zclOTA_ElementLen;
static uint32 zclOTA_ElementPos;
// Retry counters
static uint8 zclOTA_BlockRetry;
static uint8 zclOTA_UpgradeEndRetry;
static uint8 zclOTA_ClientPdState;
// OTA Header Magic Number Bytes
static const uint8 zclOTA_HdrMagic[] = {0x1E, 0xF1, 0xEE, 0x0B};
#endif // OTA_CLIENT
/******************************************************************************
* LOCAL FUNCTIONS
*/
static ZStatus_t zclOTA_HdlIncoming( zclIncoming_t *pInMsg );
#if (defined OTA_CLIENT) && (OTA_CLIENT == TRUE)
static void zclOTA_StartTimer(uint16 eventId, uint32 minutes);
static ZStatus_t sendImageBlockReq(afAddrType_t *dstAddr);
static ZStatus_t sendImagePageReq(afAddrType_t *dstAddr);
static void zclOTA_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
static void zclOTA_ImageBlockWaitExpired(void);
static void zclOTA_UpgradeComplete(uint8 status);
static uint8 zclOTA_CmpFileId(zclOTA_FileID_t *f1, zclOTA_FileID_t *f2);
static uint8 zclOTA_ProcessImageData(uint8 *pData, uint8 len);
static ZStatus_t zclOTA_SendQueryNextImageReq( afAddrType_t *dstAddr, zclOTA_QueryNextImageReqParams_t *pParams );
static ZStatus_t zclOTA_SendImageBlockReq( afAddrType_t *dstAddr, zclOTA_ImageBlockReqParams_t *pParams );
static ZStatus_t zclOTA_SendImagePageReq( afAddrType_t *dstAddr, zclOTA_ImagePageReqParams_t *pParams );
static ZStatus_t zclOTA_SendUpgradeEndReq( afAddrType_t *dstAddr, zclOTA_UpgradeEndReqParams_t *pParams );
static ZStatus_t zclOTA_ClientHdlIncoming( zclIncoming_t *pInMsg );
#endif // OTA_CLIENT
#if (defined OTA_SERVER) && (OTA_SERVER == TRUE)
static ZStatus_t zclOTA_SendQueryNextImageRsp( afAddrType_t *dstAddr, zclOTA_QueryImageRspParams_t *pParams );
static ZStatus_t zclOTA_SendImageBlockRsp( afAddrType_t *dstAddr, zclOTA_ImageBlockRspParams_t *pParams );
static ZStatus_t zclOTA_SendUpgradeEndRsp( afAddrType_t *dstAddr, zclOTA_UpgradeEndRspParams_t *pParams );
static ZStatus_t zclOTA_SendQuerySpecificFileRsp( afAddrType_t *dstAddr, zclOTA_QueryImageRspParams_t *pParams );
static ZStatus_t zclOTA_Srv_QueryNextImageReq(afAddrType_t *pSrcAddr, zclOTA_QueryNextImageReqParams_t *pParam);
static ZStatus_t zclOTA_Srv_ImageBlockReq(afAddrType_t *pSrcAddr, zclOTA_ImageBlockReqParams_t *pParam);
static ZStatus_t zclOTA_Srv_ImagePageReq(afAddrType_t *pSrcAddr, zclOTA_ImagePageReqParams_t *pParam);
static ZStatus_t zclOTA_Srv_UpgradeEndReq(afAddrType_t *pSrcAddr, zclOTA_UpgradeEndReqParams_t *pParam);
static ZStatus_t zclOTA_Srv_QuerySpecificFileReq(afAddrType_t *pSrcAddr, zclOTA_QuerySpecificFileReqParams_t *pParam);
static void zclOTA_ProcessNextImgRsp(uint8* pMSGpkt, zclOTA_FileID_t *pFileId, afAddrType_t *pAddr);
static void zclOTA_ProcessF