OTA_Dongle.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /**************************************************************************************************
  2. Filename: OTA_Dongle.c
  3. Revised: $Date: 2011-07-15 18:32:43 -0700 (Fri, 15 Jul 2011) $
  4. Revision: $Revision: 26809 $
  5. Description: Zigbee Cluster Library - sample device application.
  6. Copyright 2010-2011 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /*********************************************************************
  34. This device will be like a Light device. This application is not
  35. intended to be a Light device, but will use the device description
  36. to implement this sample code.
  37. *********************************************************************/
  38. /*********************************************************************
  39. * INCLUDES
  40. */
  41. #include "ZComDef.h"
  42. #include "OSAL.h"
  43. #include "AF.h"
  44. #include "ZDApp.h"
  45. #include "ZDProfile.h"
  46. #include "ZDObject.h"
  47. #include "AddrMgr.h"
  48. #include "zcl.h"
  49. #include "zcl_general.h"
  50. #include "zcl_ota.h"
  51. #include "OTA_Dongle.h"
  52. #include "ota_common.h"
  53. #include "onboard.h"
  54. /* HAL */
  55. #include "hal_lcd.h"
  56. #include "hal_led.h"
  57. #include "hal_key.h"
  58. #include "MT.h"
  59. #include "MT_APP.h"
  60. #include "MT_NWK.h"
  61. /*********************************************************************
  62. * MACROS
  63. */
  64. /*********************************************************************
  65. * CONSTANTS
  66. */
  67. /*********************************************************************
  68. * TYPEDEFS
  69. */
  70. /*********************************************************************
  71. * GLOBAL VARIABLES
  72. */
  73. uint8 OTA_Dongle_TaskID;
  74. uint8 OTA_Dongle_SeqNo;
  75. devStates_t OTA_Dongle_devState;
  76. /*********************************************************************
  77. * GLOBAL FUNCTIONS
  78. */
  79. /*********************************************************************
  80. * LOCAL VARIABLES
  81. */
  82. #define OTA_DONGLE_BINDINGLIST 2
  83. // Endpoint to allow SYS_APP_MSGs
  84. static endPointDesc_t ota_SysAppEp =
  85. {
  86. OTA_SYSAPP_ENDPOINT, // Sys App endpoint
  87. &OTA_Dongle_TaskID,
  88. (SimpleDescriptionFormat_t *) &OTA_Dongle_SimpleDesc,
  89. (afNetworkLatencyReq_t)0 // No Network Latency req
  90. };
  91. static endPointDesc_t ota_DongleEp =
  92. {
  93. OTA_DONGLE_ENDPOINT, // Sys App endpoint
  94. &zcl_TaskID,
  95. (SimpleDescriptionFormat_t*) &OTA_Dongle_SimpleDesc,
  96. (afNetworkLatencyReq_t)0 // No Network Latency req
  97. };
  98. /*********************************************************************
  99. * LOCAL FUNCTIONS
  100. */
  101. static void OTA_Dongle_HandleKeys( byte shift, byte keys );
  102. static void OTA_Dongle_BasicResetCB( void );
  103. static void OTA_Dongle_IdentifyCB( zclIdentify_t *pCmd );
  104. static void OTA_Dongle_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
  105. static void OTA_Dongle_ProcessIdentifyTimeChange( void );
  106. static void OTA_ProcessZDOMsgs(zdoIncomingMsg_t * pMsg);
  107. static void OTA_ProcSysAppMsg(mtSysAppMsg_t *pMsg);
  108. static void OTA_ProcessSysApp_ImageNotifyReq(uint8 *pData);
  109. static void OTA_ProcessSysApp_ReadAttrReq(uint8 *pData);
  110. static void OTA_ProcessSysApp_DiscoveryReq(uint8 *pData);
  111. static void OTA_ProcessSysApp_JoinReq(uint8 *pData);
  112. static void OTA_Send_DeviceInd(uint16 shortAddr);
  113. static void OTA_Send_JoinInd(void);
  114. static void OTA_Send_ReadAttrInd(uint16 cluster, uint16 shortAddr, zclReadRspStatus_t *pAttr);
  115. static void OTA_Send_EndpointInd(uint16 addr, uint8 endpoint);
  116. static void OTA_Send_DongleInd(void);
  117. // Functions to process ZCL Foundation incoming Command/Response messages
  118. static void OTA_Dongle_ProcessIncomingMsg( zclIncomingMsg_t *msg );
  119. #ifdef ZCL_READ
  120. static uint8 OTA_Dongle_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
  121. #endif
  122. #ifdef ZCL_WRITE
  123. static uint8 OTA_Dongle_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
  124. #endif
  125. static uint8 OTA_Dongle_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
  126. #ifdef ZCL_DISCOVER
  127. static uint8 OTA_Dongle_ProcessInDiscRspCmd( zclIncomingMsg_t *pInMsg );
  128. #endif
  129. /*********************************************************************
  130. * ZCL General Profile Callback table
  131. */
  132. static zclGeneral_AppCallbacks_t OTA_Dongle_CmdCallbacks =
  133. {
  134. OTA_Dongle_BasicResetCB, // Basic Cluster Reset command
  135. OTA_Dongle_IdentifyCB, // Identify command
  136. OTA_Dongle_IdentifyQueryRspCB, // Identify Query Response command
  137. NULL, // On/Off cluster command
  138. NULL, // Level Control Move to Level command
  139. NULL, // Level Control Move command
  140. NULL, // Level Control Step command
  141. NULL, // Group Response commands
  142. NULL, // Scene Store Request command
  143. NULL, // Scene Recall Request command
  144. NULL, // Scene Response command
  145. NULL, // Alarm (Response) command
  146. NULL, // RSSI Location commands
  147. NULL, // RSSI Location Response commands
  148. };
  149. /*********************************************************************
  150. * @fn OTA_Dongle_Init
  151. *
  152. * @brief Initialization function for the zclGeneral layer.
  153. *
  154. * @param none
  155. *
  156. * @return none
  157. */
  158. void OTA_Dongle_Init( byte task_id )
  159. {
  160. OTA_Dongle_TaskID = task_id;
  161. uint8 RxOnIdle = TRUE;
  162. OTA_Dongle_SeqNo = 0;
  163. ZMacSetReq( ZMacRxOnIdle, &RxOnIdle );
  164. // Register the ZCL General Cluster Library callback functions
  165. zclGeneral_RegisterCmdCallbacks( OTA_DONGLE_ENDPOINT, &OTA_Dongle_CmdCallbacks );
  166. // Register the application's attribute list
  167. zcl_registerAttrList( OTA_DONGLE_ENDPOINT, OTA_DONGLE_MAX_ATTRIBUTES, OTA_Dongle_Attrs );
  168. // Register the application's cluster option list
  169. zcl_registerClusterOptionList( OTA_DONGLE_ENDPOINT, OTA_DONGLE_MAX_OPTIONS, OTA_Dongle_Options );
  170. // Register the Application to receive the unprocessed Foundation command/response messages
  171. zcl_registerForMsg( OTA_Dongle_TaskID );
  172. // Register for all key events - This app will handle all key events
  173. RegisterForKeys( OTA_Dongle_TaskID );
  174. // Register endpoints
  175. afRegister( &ota_DongleEp );
  176. afRegister( &ota_SysAppEp );
  177. // Register with the ZDO to receive Match Descriptor Responses
  178. ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
  179. ZDO_RegisterForZDOMsg(task_id, Device_annce);
  180. // Start a timer to notify the console about the dongle
  181. osal_start_timerEx( OTA_Dongle_TaskID, OTA_DONGLE_DONGLE_NOTIFY_EVT, 4000 );
  182. }
  183. /*********************************************************************
  184. * @fn OTA_Dongle_event_loop
  185. *
  186. * @brief Event Loop Processor for zclGeneral.
  187. *
  188. * @param none
  189. *
  190. * @return none
  191. */
  192. uint16 OTA_Dongle_event_loop( uint8 task_id, uint16 events )
  193. {
  194. afIncomingMSGPacket_t *MSGpkt;
  195. (void)task_id; // Intentionally unreferenced parameter
  196. if ( events & SYS_EVENT_MSG )
  197. {
  198. while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( OTA_Dongle_TaskID )) )
  199. {
  200. switch ( MSGpkt->hdr.event )
  201. {
  202. case ZDO_CB_MSG:
  203. OTA_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
  204. break;
  205. case MT_SYS_APP_MSG:
  206. case MT_SYS_APP_RSP_MSG:
  207. OTA_ProcSysAppMsg((mtSysAppMsg_t *)MSGpkt);
  208. break;
  209. case ZCL_INCOMING_MSG:
  210. // Incoming ZCL Foundation command/response messages
  211. OTA_Dongle_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
  212. break;
  213. case KEY_CHANGE:
  214. OTA_Dongle_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
  215. break;
  216. case ZDO_STATE_CHANGE:
  217. OTA_Dongle_devState = (devStates_t)(MSGpkt->hdr.status);
  218. if ((OTA_Dongle_devState == DEV_END_DEVICE) || (OTA_Dongle_devState == DEV_ROUTER) || (OTA_Dongle_devState == DEV_ZB_COORD))
  219. OTA_Send_JoinInd();
  220. break;
  221. default:
  222. break;
  223. }
  224. // Release the memory
  225. osal_msg_deallocate( (uint8 *)MSGpkt );
  226. }
  227. // return unprocessed events
  228. return (events ^ SYS_EVENT_MSG);
  229. }
  230. if ( events & OTA_DONGLE_DONGLE_NOTIFY_EVT )
  231. {
  232. OTA_Send_DongleInd();
  233. osal_start_timerEx( OTA_Dongle_TaskID, OTA_DONGLE_DONGLE_NOTIFY_EVT, 4000 );
  234. return ( events ^ OTA_DONGLE_DONGLE_NOTIFY_EVT );
  235. }
  236. if ( events & OTA_DONGLE_IDENTIFY_TIMEOUT_EVT )
  237. {
  238. if ( OTA_Dongle_IdentifyTime > 0 )
  239. {
  240. OTA_Dongle_IdentifyTime--;
  241. }
  242. OTA_Dongle_ProcessIdentifyTimeChange();
  243. return ( events ^ OTA_DONGLE_IDENTIFY_TIMEOUT_EVT );
  244. }
  245. // Discard unknown events
  246. return 0;
  247. }
  248. /*********************************************************************
  249. * @fn OTA_ProcessSysApp_ImageNotifyReq
  250. *
  251. * @brief Handles app messages from the console application.
  252. *
  253. * @param pData - The data from the server.
  254. *
  255. * @return none
  256. */
  257. void OTA_ProcessSysApp_ImageNotifyReq(uint8 *pData)
  258. {
  259. zclOTA_ImageNotifyParams_t imgNotifyParams;
  260. afAddrType_t dstAddr;
  261. // Setup the destination address
  262. dstAddr.addr.shortAddr = BUILD_UINT16(pData[0], pData[1]);
  263. dstAddr.endPoint = pData[2];
  264. dstAddr.addrMode = afAddr16Bit;
  265. dstAddr.panId = _NIB.nwkPanId;
  266. // Fill the Send Image Notify Parameters
  267. imgNotifyParams.payloadType = pData[3];
  268. imgNotifyParams.queryJitter = pData[4];
  269. imgNotifyParams.fileId.manufacturer = BUILD_UINT16(pData[5], pData[6]);
  270. imgNotifyParams.fileId.type = BUILD_UINT16(pData[7], pData[8]);
  271. imgNotifyParams.fileId.version = BUILD_UINT32(pData[9], pData[10], pData[11], pData[12]);
  272. // Send the command
  273. zclOTA_SendImageNotify(&dstAddr, &imgNotifyParams);
  274. }
  275. /*********************************************************************
  276. * @fn OTA_ProcessSysApp_ReadAttrReq
  277. *
  278. * @brief Handles app messages from the console application.
  279. *
  280. * @param pData - The data from the server.
  281. *
  282. * @return none
  283. */
  284. void OTA_ProcessSysApp_ReadAttrReq(uint8 *pData)
  285. {
  286. uint8 readCmd[sizeof(zclReadCmd_t) + sizeof(uint16) * OTA_APP_MAX_ATTRIBUTES];
  287. zclReadCmd_t *pReadCmd = (zclReadCmd_t*) readCmd;
  288. afAddrType_t dstAddr;
  289. uint16 cluster;
  290. int8 i;
  291. // Setup the destination address
  292. dstAddr.addr.shortAddr = BUILD_UINT16(pData[0], pData[1]);
  293. dstAddr.endPoint = pData[2];
  294. dstAddr.addrMode = afAddr16Bit;
  295. dstAddr.panId = _NIB.nwkPanId;
  296. // Fill the Send Image Notify Parameters
  297. cluster = BUILD_UINT16(pData[3], pData[4]);
  298. pReadCmd->numAttr = pData[5];
  299. if (pReadCmd->numAttr > OTA_APP_MAX_ATTRIBUTES)
  300. pReadCmd->numAttr = OTA_APP_MAX_ATTRIBUTES;
  301. pData += 6;
  302. for (i=0; i<pReadCmd->numAttr; i++)
  303. {
  304. pReadCmd->attrID[i] = BUILD_UINT16(pData[i*2], pData[i*2+1]);
  305. }
  306. // Send the command
  307. zcl_SendRead(OTA_DONGLE_ENDPOINT, &dstAddr, cluster, pReadCmd,
  308. ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, OTA_Dongle_SeqNo++);
  309. }
  310. /*********************************************************************
  311. * @fn OTA_ProcessSysApp_DiscoveryReq
  312. *
  313. * @brief Handles app messages from the console application.
  314. *
  315. * @param pData - The data from the server.
  316. *
  317. * @return none
  318. */
  319. void OTA_ProcessSysApp_DiscoveryReq(uint8 *pData)
  320. {
  321. cId_t otaCluster = ZCL_CLUSTER_ID_OTA;
  322. zAddrType_t dstAddr;
  323. // Send out a match for the key establishment
  324. dstAddr.addrMode = AddrBroadcast;
  325. dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
  326. ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR, ZCL_OTA_SAMPLE_PROFILE_ID,
  327. 0, NULL, 1, &otaCluster, FALSE );
  328. }
  329. /*********************************************************************
  330. * @fn OTA_ProcessSysApp_JoinReq
  331. *
  332. * @brief Handles app messages from the console application.
  333. *
  334. * @param pData - The data from the server.
  335. *
  336. * @return none
  337. */
  338. void OTA_ProcessSysApp_JoinReq(uint8 *pData)
  339. {
  340. // Setup Z-Stack global configuration
  341. zgConfigPANID = BUILD_UINT16(pData[0], pData[1]);
  342. zgDefaultChannelList = 0x00000800;
  343. zgDefaultChannelList <<= (pData[2] - 11);
  344. zgDefaultStartingScanDuration = 0;
  345. // Make sure all NWK layer callbacks go to the stack
  346. _nwkCallbackSub = 0;
  347. // Start the stack. This will join the PAN specified above
  348. ZDOInitDevice(0);
  349. }
  350. /*********************************************************************
  351. * @fn OTA_Send_DeviceInd
  352. *
  353. * @brief Notifies the console about the existance of a device on the network.
  354. *
  355. * @param none.
  356. *
  357. * @return none
  358. */
  359. void OTA_Send_DeviceInd(uint16 shortAddr)
  360. {
  361. uint8 buffer[OTA_APP_DEVICE_IND_LEN];
  362. uint8 *pBuf = buffer;
  363. uint16 pan = _NIB.nwkPanId;
  364. *pBuf++ = OTA_SYSAPP_ENDPOINT;
  365. *pBuf++ = OTA_APP_DEVICE_IND;
  366. *pBuf++ = LO_UINT16(pan);
  367. *pBuf++ = HI_UINT16(pan);
  368. *pBuf++ = LO_UINT16(shortAddr);
  369. *pBuf++ = HI_UINT16(shortAddr);
  370. // Send the indication
  371. MT_BuildAndSendZToolResponse(MT_RPC_SYS_APP, MT_APP_MSG, OTA_APP_DEVICE_IND_LEN, buffer);
  372. }
  373. /*********************************************************************
  374. * @fn OTA_Send_ReadAttrInd
  375. *
  376. * @brief Notifies the console about attribute values for a device.
  377. *
  378. * @param none.
  379. *
  380. * @return none
  381. */
  382. void OTA_Send_ReadAttrInd(uint16 cluster, uint16 shortAddr, zclReadRspStatus_t *pAttr)
  383. {
  384. uint8 buffer[OTA_APP_READ_ATTRIBUTE_IND_LEN];
  385. uint8 *pBuf = buffer;
  386. uint8 len;
  387. *pBuf++ = OTA_SYSAPP_ENDPOINT;
  388. *pBuf++ = OTA_APP_READ_ATTRIBUTE_IND;
  389. *pBuf++ = LO_UINT16(_NIB.nwkPanId);
  390. *pBuf++ = HI_UINT16(_NIB.nwkPanId);
  391. *pBuf++ = LO_UINT16(cluster);
  392. *pBuf++ = HI_UINT16(cluster);
  393. *pBuf++ = LO_UINT16(shortAddr);
  394. *pBuf++ = HI_UINT16(shortAddr);
  395. *pBuf++ = LO_UINT16(pAttr->attrID);
  396. *pBuf++ = HI_UINT16(pAttr->attrID);
  397. *pBuf++ = pAttr->status;
  398. *pBuf++ = pAttr->dataType;
  399. len = zclGetDataTypeLength(pAttr->dataType);
  400. // We should not be reading attributes greater than 8 bytes in length
  401. if (len <= 8)
  402. {
  403. *pBuf++ = len;
  404. if (len)
  405. {
  406. uint8 *pStr;
  407. switch ( pAttr->dataType )
  408. {
  409. case ZCL_DATATYPE_UINT8:
  410. *pBuf = *((uint8 *)pAttr->data);
  411. break;
  412. case ZCL_DATATYPE_UINT16:
  413. *pBuf++ = LO_UINT16( *((uint16*)pAttr->data) );
  414. *pBuf++ = HI_UINT16( *((uint16*)pAttr->data) );
  415. break;
  416. case ZCL_DATATYPE_UINT32:
  417. pBuf = osal_buffer_uint32( pBuf, *((uint32*)pAttr->data) );
  418. break;
  419. case ZCL_DATATYPE_IEEE_ADDR:
  420. pStr = (uint8*)pAttr->data;
  421. osal_memcpy( pBuf, pStr, 8 );
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. }
  428. else
  429. *pBuf = 0;
  430. // Send the indication
  431. MT_BuildAndSendZToolResponse(MT_RPC_SYS_APP, MT_APP_MSG, OTA_APP_READ_ATTRIBUTE_IND_LEN, buffer);
  432. }
  433. /*********************************************************************
  434. * @fn OTA_ProcessSysApp_JoinReq
  435. *
  436. * @brief Notifies the console that the dognle has joined a network.
  437. *
  438. * @param none.
  439. *
  440. * @return none
  441. */
  442. void OTA_Send_JoinInd()
  443. {
  444. uint8 buffer[OTA_APP_JOIN_IND_LEN];
  445. uint8 *pBuf = buffer;
  446. uint16 pan = _NIB.nwkPanId;
  447. *pBuf++ = OTA_SYSAPP_ENDPOINT;
  448. *pBuf++ = OTA_APP_JOIN_IND;
  449. *pBuf++ = LO_UINT16(pan);
  450. *pBuf = HI_UINT16(pan);
  451. // Send the indication
  452. MT_BuildAndSendZToolResponse(MT_RPC_SYS_APP, MT_APP_MSG, OTA_APP_JOIN_IND_LEN, buffer);
  453. }
  454. /*********************************************************************
  455. * @fn OTA_ProcessSysApp_JoinReq
  456. *
  457. * @brief Notifies the console about the OTA endpoint on a device.
  458. *
  459. * @param none.
  460. *
  461. * @return none
  462. */
  463. void OTA_Send_EndpointInd(uint16 addr, uint8 endpoint)
  464. {
  465. uint8 buffer[OTA_APP_ENDPOINT_IND_LEN];
  466. uint8 *pBuf = buffer;
  467. *pBuf++ = OTA_SYSAPP_ENDPOINT;
  468. *pBuf++ = OTA_APP_ENDPOINT_IND;
  469. *pBuf++ = LO_UINT16(_NIB.nwkPanId);
  470. *pBuf++ = HI_UINT16(_NIB.nwkPanId);
  471. *pBuf++ = LO_UINT16(addr);
  472. *pBuf++ = HI_UINT16(addr);
  473. *pBuf = endpoint;
  474. // Send the indication
  475. MT_BuildAndSendZToolResponse(MT_RPC_SYS_APP, MT_APP_MSG, OTA_APP_ENDPOINT_IND_LEN, buffer);
  476. }
  477. /*********************************************************************
  478. * @fn OTA_Send_DongleInd
  479. *
  480. * @brief Notifies the console about the Dongle.
  481. *
  482. * @param none.
  483. *
  484. * @return none
  485. */
  486. void OTA_Send_DongleInd()
  487. {
  488. uint8 buffer[128];
  489. uint8 *pBuf = buffer;
  490. *pBuf++ = OTA_SYSAPP_ENDPOINT;
  491. *pBuf++ = OTA_APP_DONGLE_IND;
  492. *pBuf++ = zgDeviceLogicalType;
  493. *pBuf++ = LO_UINT16(_NIB.nwkPanId);
  494. *pBuf++ = HI_UINT16(_NIB.nwkPanId);
  495. *pBuf++ = LO_UINT16(_NIB.nwkDevAddress);
  496. *pBuf++ = HI_UINT16(_NIB.nwkDevAddress);
  497. *pBuf++ = ZCL_OTA_ENDPOINT;
  498. *pBuf++ = _NIB.nwkLogicalChannel;
  499. *pBuf = OTA_Dongle_devState;
  500. // Send the indication
  501. MT_BuildAndSendZToolResponse(MT_RPC_SYS_APP, MT_APP_MSG, pBuf - buffer, buffer);
  502. }
  503. /*********************************************************************
  504. * @fn OTA_ProcSysAppMsg
  505. *
  506. * @brief Handles sys app messages from the server application.
  507. *
  508. * @param pMsg - The message from the server.
  509. *
  510. * @return none
  511. */
  512. void OTA_ProcSysAppMsg(mtSysAppMsg_t *pMsg)
  513. {
  514. uint8 cmd;
  515. if (pMsg == NULL)
  516. return;
  517. cmd = *pMsg->appData++;
  518. switch(cmd)
  519. {
  520. case OTA_APP_READ_ATTRIBUTE_REQ:
  521. OTA_ProcessSysApp_ReadAttrReq(pMsg->appData);
  522. break;
  523. case OTA_APP_IMAGE_NOTIFY_REQ:
  524. OTA_ProcessSysApp_ImageNotifyReq(pMsg->appData);
  525. break;
  526. case OTA_APP_DISCOVERY_REQ:
  527. OTA_ProcessSysApp_DiscoveryReq(pMsg->appData);
  528. break;
  529. case OTA_APP_JOIN_REQ:
  530. OTA_ProcessSysApp_JoinReq(pMsg->appData);
  531. break;
  532. case OTA_APP_LEAVE_REQ:
  533. // Simulate a leave by rebooting the dongle
  534. SystemReset();
  535. default:
  536. break;
  537. }
  538. }
  539. /*********************************************************************
  540. * @fn OTA_ProcessZDOMsgs
  541. *
  542. * @brief Process messages from the ZDO layer.
  543. *
  544. * @param pMsg - The message from the server.
  545. *
  546. * @return none
  547. */
  548. void OTA_ProcessZDOMsgs(zdoIncomingMsg_t * pMsg)
  549. {
  550. if (pMsg)
  551. {
  552. if (pMsg->clusterID == Match_Desc_rsp)
  553. {
  554. ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( pMsg );
  555. if (pRsp)
  556. {
  557. // Notify the console application of the client device's OTA endpoint
  558. if (pRsp->cnt)
  559. OTA_Send_EndpointInd(pRsp->nwkAddr, pRsp->epList[0]);
  560. osal_mem_free(pRsp);
  561. }
  562. }
  563. else if (pMsg->clusterID == Device_annce)
  564. {
  565. cId_t otaCluster = ZCL_CLUSTER_ID_OTA;
  566. zAddrType_t dstAddr;
  567. ZDO_DeviceAnnce_t devAnnce;
  568. ZDO_ParseDeviceAnnce(pMsg, &devAnnce);
  569. OTA_Send_DeviceInd(devAnnce.nwkAddr);
  570. // Send out a match for the OTA cluster ID
  571. dstAddr.addrMode = Addr16Bit;
  572. dstAddr.addr.shortAddr = devAnnce.nwkAddr;
  573. ZDP_MatchDescReq( &dstAddr, devAnnce.nwkAddr, ZCL_OTA_SAMPLE_PROFILE_ID,
  574. 0, NULL, 1, &otaCluster, FALSE );
  575. }
  576. }
  577. }
  578. /*********************************************************************
  579. * @fn OTA_Dongle_HandleKeys
  580. *
  581. * @brief Handles all key events for this device.
  582. *
  583. * @param shift - true if in shift/alt.
  584. * @param keys - bit field for key events. Valid entries:
  585. * HAL_KEY_SW_4
  586. * HAL_KEY_SW_3
  587. * HAL_KEY_SW_2
  588. * HAL_KEY_SW_1
  589. *
  590. * @return none
  591. */
  592. static void OTA_Dongle_HandleKeys( byte shift, byte keys )
  593. {
  594. (void)shift; // Intentionally unreferenced parameter
  595. if ( keys & HAL_KEY_SW_1 )
  596. {
  597. ZDOInitDevice(0);
  598. }
  599. if ( keys & HAL_KEY_SW_2 )
  600. {
  601. }
  602. if ( keys & HAL_KEY_SW_3 )
  603. {
  604. }
  605. if ( keys & HAL_KEY_SW_4 )
  606. {
  607. }
  608. }
  609. /*********************************************************************
  610. * @fn OTA_Dongle_ProcessIdentifyTimeChange
  611. *
  612. * @brief Called to process any change to the IdentifyTime attribute.
  613. *
  614. * @param none
  615. *
  616. * @return none
  617. */
  618. static void OTA_Dongle_ProcessIdentifyTimeChange( void )
  619. {
  620. if ( OTA_Dongle_IdentifyTime > 0 )
  621. {
  622. osal_start_timerEx( OTA_Dongle_TaskID, OTA_DONGLE_IDENTIFY_TIMEOUT_EVT, 1000 );
  623. HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
  624. }
  625. else
  626. {
  627. osal_stop_timerEx( OTA_Dongle_TaskID, OTA_DONGLE_IDENTIFY_TIMEOUT_EVT );
  628. }
  629. }
  630. /*********************************************************************
  631. * @fn OTA_Dongle_BasicResetCB
  632. *
  633. * @brief Callback from the ZCL General Cluster Library
  634. * to set all the Basic Cluster attributes to default values.
  635. *
  636. * @param none
  637. *
  638. * @return none
  639. */
  640. static void OTA_Dongle_BasicResetCB( void )
  641. {
  642. // Reset all attributes to default values
  643. }
  644. /*********************************************************************
  645. * @fn OTA_Dongle_IdentifyCB
  646. *
  647. * @brief Callback from the ZCL General Cluster Library when
  648. * it received an Identity Command for this application.
  649. *
  650. * @param srcAddr - source address and endpoint of the response message
  651. * @param identifyTime - the number of seconds to identify yourself
  652. *
  653. * @return none
  654. */
  655. static void OTA_Dongle_IdentifyCB( zclIdentify_t *pCmd )
  656. {
  657. OTA_Dongle_IdentifyTime = pCmd->identifyTime;
  658. OTA_Dongle_ProcessIdentifyTimeChange();
  659. }
  660. /*********************************************************************
  661. * @fn OTA_Dongle_IdentifyQueryRspCB
  662. *
  663. * @brief Callback from the ZCL General Cluster Library when
  664. * it received an Identity Query Response Command for this application.
  665. *
  666. * @param srcAddr - requestor's address
  667. * @param timeout - number of seconds to identify yourself (valid for query response)
  668. *
  669. * @return none
  670. */
  671. static void OTA_Dongle_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
  672. {
  673. // Query Response (with timeout value)
  674. (void)pRsp;
  675. }
  676. /******************************************************************************
  677. *
  678. * Functions for processing ZCL Foundation incoming Command/Response messages
  679. *
  680. *****************************************************************************/
  681. /*********************************************************************
  682. * @fn OTA_Dongle_ProcessIncomingMsg
  683. *
  684. * @brief Process ZCL Foundation incoming message
  685. *
  686. * @param pInMsg - pointer to the received message
  687. *
  688. * @return none
  689. */
  690. static void OTA_Dongle_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
  691. {
  692. switch ( pInMsg->zclHdr.commandID )
  693. {
  694. #ifdef ZCL_READ
  695. case ZCL_CMD_READ_RSP:
  696. OTA_Dongle_ProcessInReadRspCmd( pInMsg );
  697. break;
  698. #endif
  699. #ifdef ZCL_WRITE
  700. case ZCL_CMD_WRITE_RSP:
  701. OTA_Dongle_ProcessInWriteRspCmd( pInMsg );
  702. break;
  703. #endif
  704. case ZCL_CMD_DEFAULT_RSP:
  705. OTA_Dongle_ProcessInDefaultRspCmd( pInMsg );
  706. break;
  707. #ifdef ZCL_DISCOVER
  708. case ZCL_CMD_DISCOVER_RSP:
  709. OTA_Dongle_ProcessInDiscRspCmd( pInMsg );
  710. break;
  711. #endif
  712. default:
  713. break;
  714. }
  715. if ( pInMsg->attrCmd )
  716. osal_mem_free( pInMsg->attrCmd );
  717. }
  718. #ifdef ZCL_READ
  719. /*********************************************************************
  720. * @fn OTA_Dongle_ProcessInReadRspCmd
  721. *
  722. * @brief Process the "Profile" Read Response Command
  723. *
  724. * @param pInMsg - incoming message to process
  725. *
  726. * @return none
  727. */
  728. static uint8 OTA_Dongle_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg )
  729. {
  730. zclReadRspCmd_t *readRspCmd;
  731. uint8 i;
  732. readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
  733. for (i = 0; i < readRspCmd->numAttr; i++)
  734. {
  735. OTA_Send_ReadAttrInd(pInMsg->clusterId, pInMsg->srcAddr.addr.shortAddr, &readRspCmd->attrList[i]);
  736. }
  737. return TRUE;
  738. }
  739. #endif // ZCL_READ
  740. #ifdef ZCL_WRITE
  741. /*********************************************************************
  742. * @fn OTA_Dongle_ProcessInWriteRspCmd
  743. *
  744. * @brief Process the "Profile" Write Response Command
  745. *
  746. * @param pInMsg - incoming message to process
  747. *
  748. * @return none
  749. */
  750. static uint8 OTA_Dongle_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg )
  751. {
  752. zclWriteRspCmd_t *writeRspCmd;
  753. uint8 i;
  754. writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
  755. for (i = 0; i < writeRspCmd->numAttr; i++)
  756. {
  757. // Notify the device of the results of the its original write attributes
  758. // command.
  759. }
  760. return TRUE;
  761. }
  762. #endif // ZCL_WRITE
  763. /*********************************************************************
  764. * @fn OTA_Dongle_ProcessInDefaultRspCmd
  765. *
  766. * @brief Process the "Profile" Default Response Command
  767. *
  768. * @param pInMsg - incoming message to process
  769. *
  770. * @return none
  771. */
  772. static uint8 OTA_Dongle_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg )
  773. {
  774. // zclDefaultRspCmd_t *defaultRspCmd = (zclDefaultRspCmd_t *)pInMsg->attrCmd;
  775. // Device is notified of the Default Response command.
  776. (void)pInMsg;
  777. return TRUE;
  778. }
  779. #ifdef ZCL_DISCOVER
  780. /*********************************************************************
  781. * @fn OTA_Dongle_ProcessInDiscRspCmd
  782. *
  783. * @brief Process the "Profile" Discover Response Command
  784. *
  785. * @param pInMsg - incoming message to process
  786. *
  787. * @return none
  788. */
  789. static uint8 OTA_Dongle_ProcessInDiscRspCmd( zclIncomingMsg_t *pInMsg )
  790. {
  791. zclDiscoverRspCmd_t *discoverRspCmd;
  792. uint8 i;
  793. discoverRspCmd = (zclDiscoverRspCmd_t *)pInMsg->attrCmd;
  794. for ( i = 0; i < discoverRspCmd->numAttr; i++ )
  795. {
  796. // Device is notified of the result of its attribute discovery command.
  797. }
  798. return TRUE;
  799. }
  800. #endif // ZCL_DISCOVER
  801. /****************************************************************************
  802. ****************************************************************************/