zcl_samplelight.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /**************************************************************************************************
  2. Filename: zcl_sampleLight.c
  3. Revised: $Date: 2009-03-18 15:56:27 -0700 (Wed, 18 Mar 2009) $
  4. Revision: $Revision: 19453 $
  5. Description: Zigbee Cluster Library - sample device application.
  6. Copyright 2006-2009 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 "zcl.h"
  46. #include "zcl_general.h"
  47. #include "zcl_ha.h"
  48. #include "zcl_samplelight.h"
  49. #include "onboard.h"
  50. /* HAL */
  51. #include "hal_lcd.h"
  52. #include "hal_led.h"
  53. #include "hal_key.h"
  54. /*********************************************************************
  55. * MACROS
  56. */
  57. /*********************************************************************
  58. * CONSTANTS
  59. */
  60. /*********************************************************************
  61. * TYPEDEFS
  62. */
  63. /*********************************************************************
  64. * GLOBAL VARIABLES
  65. */
  66. byte zclSampleLight_TaskID;
  67. /*********************************************************************
  68. * GLOBAL FUNCTIONS
  69. */
  70. /*********************************************************************
  71. * LOCAL VARIABLES
  72. */
  73. //static afAddrType_t zclSampleLight_DstAddr;
  74. #define ZCLSAMPLELIGHT_BINDINGLIST 2
  75. static cId_t bindingInClusters[ZCLSAMPLELIGHT_BINDINGLIST] =
  76. {
  77. ZCL_CLUSTER_ID_GEN_ON_OFF,
  78. ZCL_CLUSTER_ID_GEN_LEVEL_CONTROL
  79. };
  80. // Test Endpoint to allow SYS_APP_MSGs
  81. static endPointDesc_t sampleLight_TestEp =
  82. {
  83. 20, // Test endpoint
  84. &zclSampleLight_TaskID,
  85. (SimpleDescriptionFormat_t *)NULL, // No Simple description for this test endpoint
  86. (afNetworkLatencyReq_t)0 // No Network Latency req
  87. };
  88. /*********************************************************************
  89. * LOCAL FUNCTIONS
  90. */
  91. static void zclSampleLight_HandleKeys( byte shift, byte keys );
  92. static void zclSampleLight_BasicResetCB( void );
  93. static void zclSampleLight_IdentifyCB( zclIdentify_t *pCmd );
  94. static void zclSampleLight_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
  95. static void zclSampleLight_OnOffCB( uint8 cmd );
  96. static void zclSampleLight_ProcessIdentifyTimeChange( void );
  97. // Functions to process ZCL Foundation incoming Command/Response messages
  98. static void zclSampleLight_ProcessIncomingMsg( zclIncomingMsg_t *msg );
  99. #ifdef ZCL_READ
  100. static uint8 zclSampleLight_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
  101. #endif
  102. #ifdef ZCL_WRITE
  103. static uint8 zclSampleLight_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
  104. #endif
  105. static uint8 zclSampleLight_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
  106. #ifdef ZCL_DISCOVER
  107. static uint8 zclSampleLight_ProcessInDiscRspCmd( zclIncomingMsg_t *pInMsg );
  108. #endif
  109. /*********************************************************************
  110. * ZCL General Profile Callback table
  111. */
  112. static zclGeneral_AppCallbacks_t zclSampleLight_CmdCallbacks =
  113. {
  114. zclSampleLight_BasicResetCB, // Basic Cluster Reset command
  115. zclSampleLight_IdentifyCB, // Identify command
  116. zclSampleLight_IdentifyQueryRspCB, // Identify Query Response command
  117. zclSampleLight_OnOffCB, // On/Off cluster command
  118. NULL, // Level Control Move to Level command
  119. NULL, // Level Control Move command
  120. NULL, // Level Control Step command
  121. NULL, // Group Response commands
  122. NULL, // Scene Store Request command
  123. NULL, // Scene Recall Request command
  124. NULL, // Scene Response command
  125. NULL, // Alarm (Response) command
  126. NULL, // RSSI Location commands
  127. NULL, // RSSI Location Response commands
  128. };
  129. /*********************************************************************
  130. * @fn zclSampleLight_Init
  131. *
  132. * @brief Initialization function for the zclGeneral layer.
  133. *
  134. * @param none
  135. *
  136. * @return none
  137. */
  138. void zclSampleLight_Init( byte task_id )
  139. {
  140. zclSampleLight_TaskID = task_id;
  141. // Set destination address to indirect
  142. //zclSampleLight_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
  143. //zclSampleLight_DstAddr.endPoint = 0;
  144. //zclSampleLight_DstAddr.addr.shortAddr = 0;
  145. // This app is part of the Home Automation Profile
  146. zclHA_Init( &zclSampleLight_SimpleDesc );
  147. // Register the ZCL General Cluster Library callback functions
  148. zclGeneral_RegisterCmdCallbacks( SAMPLELIGHT_ENDPOINT, &zclSampleLight_CmdCallbacks );
  149. // Register the application's attribute list
  150. zcl_registerAttrList( SAMPLELIGHT_ENDPOINT, SAMPLELIGHT_MAX_ATTRIBUTES, zclSampleLight_Attrs );
  151. // Register the Application to receive the unprocessed Foundation command/response messages
  152. zcl_registerForMsg( zclSampleLight_TaskID );
  153. // Register for all key events - This app will handle all key events
  154. RegisterForKeys( zclSampleLight_TaskID );
  155. // Register for a test endpoint
  156. afRegister( &sampleLight_TestEp );
  157. }
  158. /*********************************************************************
  159. * @fn zclSample_event_loop
  160. *
  161. * @brief Event Loop Processor for zclGeneral.
  162. *
  163. * @param none
  164. *
  165. * @return none
  166. */
  167. uint16 zclSampleLight_event_loop( uint8 task_id, uint16 events )
  168. {
  169. afIncomingMSGPacket_t *MSGpkt;
  170. (void)task_id; // Intentionally unreferenced parameter
  171. if ( events & SYS_EVENT_MSG )
  172. {
  173. while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleLight_TaskID )) )
  174. {
  175. switch ( MSGpkt->hdr.event )
  176. {
  177. case ZCL_INCOMING_MSG:
  178. // Incoming ZCL Foundation command/response messages
  179. zclSampleLight_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
  180. break;
  181. case KEY_CHANGE:
  182. zclSampleLight_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
  183. break;
  184. default:
  185. break;
  186. }
  187. // Release the memory
  188. osal_msg_deallocate( (uint8 *)MSGpkt );
  189. }
  190. // return unprocessed events
  191. return (events ^ SYS_EVENT_MSG);
  192. }
  193. if ( events & SAMPLELIGHT_IDENTIFY_TIMEOUT_EVT )
  194. {
  195. if ( zclSampleLight_IdentifyTime > 0 )
  196. zclSampleLight_IdentifyTime--;
  197. zclSampleLight_ProcessIdentifyTimeChange();
  198. return ( events ^ SAMPLELIGHT_IDENTIFY_TIMEOUT_EVT );
  199. }
  200. // Discard unknown events
  201. return 0;
  202. }
  203. /*********************************************************************
  204. * @fn zclSampleLight_HandleKeys
  205. *
  206. * @brief Handles all key events for this device.
  207. *
  208. * @param shift - true if in shift/alt.
  209. * @param keys - bit field for key events. Valid entries:
  210. * HAL_KEY_SW_4
  211. * HAL_KEY_SW_3
  212. * HAL_KEY_SW_2
  213. * HAL_KEY_SW_1
  214. *
  215. * @return none
  216. */
  217. static void zclSampleLight_HandleKeys( byte shift, byte keys )
  218. {
  219. zAddrType_t dstAddr;
  220. (void)shift; // Intentionally unreferenced parameter
  221. if ( keys & HAL_KEY_SW_2 )
  222. {
  223. // Initiate an End Device Bind Request, this bind request will
  224. // only use a cluster list that is important to binding.
  225. dstAddr.addrMode = afAddr16Bit;
  226. dstAddr.addr.shortAddr = 0; // Coordinator makes the match
  227. ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
  228. SAMPLELIGHT_ENDPOINT,
  229. ZCL_HA_PROFILE_ID,
  230. ZCLSAMPLELIGHT_BINDINGLIST, bindingInClusters,
  231. 0, NULL, // No Outgoing clusters to bind
  232. TRUE );
  233. }
  234. if ( keys & HAL_KEY_SW_3 )
  235. {
  236. }
  237. if ( keys & HAL_KEY_SW_4 )
  238. {
  239. }
  240. }
  241. /*********************************************************************
  242. * @fn zclSampleLight_ProcessIdentifyTimeChange
  243. *
  244. * @brief Called to process any change to the IdentifyTime attribute.
  245. *
  246. * @param none
  247. *
  248. * @return none
  249. */
  250. static void zclSampleLight_ProcessIdentifyTimeChange( void )
  251. {
  252. if ( zclSampleLight_IdentifyTime > 0 )
  253. {
  254. osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_IDENTIFY_TIMEOUT_EVT, 1000 );
  255. HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
  256. }
  257. else
  258. {
  259. if ( zclSampleLight_OnOff )
  260. HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
  261. else
  262. HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
  263. osal_stop_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_IDENTIFY_TIMEOUT_EVT );
  264. }
  265. }
  266. /*********************************************************************
  267. * @fn zclSampleLight_BasicResetCB
  268. *
  269. * @brief Callback from the ZCL General Cluster Library
  270. * to set all the Basic Cluster attributes to default values.
  271. *
  272. * @param none
  273. *
  274. * @return none
  275. */
  276. static void zclSampleLight_BasicResetCB( void )
  277. {
  278. // Reset all attributes to default values
  279. }
  280. /*********************************************************************
  281. * @fn zclSampleLight_IdentifyCB
  282. *
  283. * @brief Callback from the ZCL General Cluster Library when
  284. * it received an Identity Command for this application.
  285. *
  286. * @param srcAddr - source address and endpoint of the response message
  287. * @param identifyTime - the number of seconds to identify yourself
  288. *
  289. * @return none
  290. */
  291. static void zclSampleLight_IdentifyCB( zclIdentify_t *pCmd )
  292. {
  293. zclSampleLight_IdentifyTime = pCmd->identifyTime;
  294. zclSampleLight_ProcessIdentifyTimeChange();
  295. }
  296. /*********************************************************************
  297. * @fn zclSampleLight_IdentifyQueryRspCB
  298. *
  299. * @brief Callback from the ZCL General Cluster Library when
  300. * it received an Identity Query Response Command for this application.
  301. *
  302. * @param srcAddr - requestor's address
  303. * @param timeout - number of seconds to identify yourself (valid for query response)
  304. *
  305. * @return none
  306. */
  307. static void zclSampleLight_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
  308. {
  309. // Query Response (with timeout value)
  310. (void)pRsp;
  311. }
  312. /*********************************************************************
  313. * @fn zclSampleLight_OnOffCB
  314. *
  315. * @brief Callback from the ZCL General Cluster Library when
  316. * it received an On/Off Command for this application.
  317. *
  318. * @param cmd - COMMAND_ON, COMMAND_OFF or COMMAND_TOGGLE
  319. *
  320. * @return none
  321. */
  322. static void zclSampleLight_OnOffCB( uint8 cmd )
  323. {
  324. // Turn on the light
  325. if ( cmd == COMMAND_ON )
  326. zclSampleLight_OnOff = LIGHT_ON;
  327. // Turn off the light
  328. else if ( cmd == COMMAND_OFF )
  329. zclSampleLight_OnOff = LIGHT_OFF;
  330. // Toggle the light
  331. else
  332. {
  333. if ( zclSampleLight_OnOff == LIGHT_OFF )
  334. zclSampleLight_OnOff = LIGHT_ON;
  335. else
  336. zclSampleLight_OnOff = LIGHT_OFF;
  337. }
  338. // In this sample app, we use LED4 to simulate the Light
  339. if ( zclSampleLight_OnOff == LIGHT_ON )
  340. HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
  341. else
  342. HalLedSet( HAL_LED_4, HAL_LED_MODE_OFF );
  343. }
  344. /******************************************************************************
  345. *
  346. * Functions for processing ZCL Foundation incoming Command/Response messages
  347. *
  348. *****************************************************************************/
  349. /*********************************************************************
  350. * @fn zclSampleLight_ProcessIncomingMsg
  351. *
  352. * @brief Process ZCL Foundation incoming message
  353. *
  354. * @param pInMsg - pointer to the received message
  355. *
  356. * @return none
  357. */
  358. static void zclSampleLight_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
  359. {
  360. switch ( pInMsg->zclHdr.commandID )
  361. {
  362. #ifdef ZCL_READ
  363. case ZCL_CMD_READ_RSP:
  364. zclSampleLight_ProcessInReadRspCmd( pInMsg );
  365. break;
  366. #endif
  367. #ifdef ZCL_WRITE
  368. case ZCL_CMD_WRITE_RSP:
  369. zclSampleLight_ProcessInWriteRspCmd( pInMsg );
  370. break;
  371. #endif
  372. #ifdef ZCL_REPORT
  373. // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
  374. case ZCL_CMD_CONFIG_REPORT:
  375. //zclSampleLight_ProcessInConfigReportCmd( pInMsg );
  376. break;
  377. case ZCL_CMD_CONFIG_REPORT_RSP:
  378. //zclSampleLight_ProcessInConfigReportRspCmd( pInMsg );
  379. break;
  380. case ZCL_CMD_READ_REPORT_CFG:
  381. //zclSampleLight_ProcessInReadReportCfgCmd( pInMsg );
  382. break;
  383. case ZCL_CMD_READ_REPORT_CFG_RSP:
  384. //zclSampleLight_ProcessInReadReportCfgRspCmd( pInMsg );
  385. break;
  386. case ZCL_CMD_REPORT:
  387. //zclSampleLight_ProcessInReportCmd( pInMsg );
  388. break;
  389. #endif
  390. case ZCL_CMD_DEFAULT_RSP:
  391. zclSampleLight_ProcessInDefaultRspCmd( pInMsg );
  392. break;
  393. #ifdef ZCL_DISCOVER
  394. case ZCL_CMD_DISCOVER_RSP:
  395. zclSampleLight_ProcessInDiscRspCmd( pInMsg );
  396. break;
  397. #endif
  398. default:
  399. break;
  400. }
  401. if ( pInMsg->attrCmd )
  402. osal_mem_free( pInMsg->attrCmd );
  403. }
  404. #ifdef ZCL_READ
  405. /*********************************************************************
  406. * @fn zclSampleLight_ProcessInReadRspCmd
  407. *
  408. * @brief Process the "Profile" Read Response Command
  409. *
  410. * @param pInMsg - incoming message to process
  411. *
  412. * @return none
  413. */
  414. static uint8 zclSampleLight_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg )
  415. {
  416. zclReadRspCmd_t *readRspCmd;
  417. uint8 i;
  418. readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
  419. for (i = 0; i < readRspCmd->numAttr; i++)
  420. {
  421. // Notify the originator of the results of the original read attributes
  422. // attempt and, for each successfull request, the value of the requested
  423. // attribute
  424. }
  425. return TRUE;
  426. }
  427. #endif // ZCL_READ
  428. #ifdef ZCL_WRITE
  429. /*********************************************************************
  430. * @fn zclSampleLight_ProcessInWriteRspCmd
  431. *
  432. * @brief Process the "Profile" Write Response Command
  433. *
  434. * @param pInMsg - incoming message to process
  435. *
  436. * @return none
  437. */
  438. static uint8 zclSampleLight_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg )
  439. {
  440. zclWriteRspCmd_t *writeRspCmd;
  441. uint8 i;
  442. writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
  443. for (i = 0; i < writeRspCmd->numAttr; i++)
  444. {
  445. // Notify the device of the results of the its original write attributes
  446. // command.
  447. }
  448. return TRUE;
  449. }
  450. #endif // ZCL_WRITE
  451. /*********************************************************************
  452. * @fn zclSampleLight_ProcessInDefaultRspCmd
  453. *
  454. * @brief Process the "Profile" Default Response Command
  455. *
  456. * @param pInMsg - incoming message to process
  457. *
  458. * @return none
  459. */
  460. static uint8 zclSampleLight_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg )
  461. {
  462. // zclDefaultRspCmd_t *defaultRspCmd = (zclDefaultRspCmd_t *)pInMsg->attrCmd;
  463. // Device is notified of the Default Response command.
  464. (void)pInMsg;
  465. return TRUE;
  466. }
  467. #ifdef ZCL_DISCOVER
  468. /*********************************************************************
  469. * @fn zclSampleLight_ProcessInDiscRspCmd
  470. *
  471. * @brief Process the "Profile" Discover Response Command
  472. *
  473. * @param pInMsg - incoming message to process
  474. *
  475. * @return none
  476. */
  477. static uint8 zclSampleLight_ProcessInDiscRspCmd( zclIncomingMsg_t *pInMsg )
  478. {
  479. zclDiscoverRspCmd_t *discoverRspCmd;
  480. uint8 i;
  481. discoverRspCmd = (zclDiscoverRspCmd_t *)pInMsg->attrCmd;
  482. for ( i = 0; i < discoverRspCmd->numAttr; i++ )
  483. {
  484. // Device is notified of the result of its attribute discovery command.
  485. }
  486. return TRUE;
  487. }
  488. #endif // ZCL_DISCOVER
  489. /****************************************************************************
  490. ****************************************************************************/