ZDApp.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  1. /**************************************************************************************************
  2. Filename: ZDApp.c
  3. Revised: $Date: 2009-03-12 16:25:22 -0700 (Thu, 12 Mar 2009) $
  4. Revision: $Revision: 19404 $
  5. Description: This file contains the interface to the Zigbee Device Application. This is the
  6. Application part that the use can change. This also contains the Task functions.
  7. Copyright 2004-2007 Texas Instruments Incorporated. All rights reserved.
  8. IMPORTANT: Your use of this Software is limited to those specific rights
  9. granted under the terms of a software license agreement between the user
  10. who downloaded the software, his/her employer (which must be your employer)
  11. and Texas Instruments Incorporated (the "License"). You may not use this
  12. Software unless you agree to abide by the terms of the License. The License
  13. limits your use, and you acknowledge, that the Software may not be modified,
  14. copied or distributed unless embedded on a Texas Instruments microcontroller
  15. or used solely and exclusively in conjunction with a Texas Instruments radio
  16. frequency transceiver, which is integrated into your product. Other than for
  17. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  18. works of, modify, distribute, perform, display or sell this Software and/or
  19. its documentation for any purpose.
  20. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  21. PROVIDED “AS IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  22. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  23. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  24. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  25. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  26. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  27. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  28. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  29. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  30. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  31. Should you have any questions regarding your right to use this Software,
  32. contact Texas Instruments Incorporated at www.TI.com.
  33. **************************************************************************************************/
  34. /*********************************************************************
  35. * INCLUDES
  36. */
  37. #include "ZComDef.h"
  38. #include "ZMac.h"
  39. #include "OSAL.h"
  40. #include "OSAL_Tasks.h"
  41. #include "OSAL_PwrMgr.h"
  42. #include "OSAL_Nv.h"
  43. #include "AF.h"
  44. #include "APSMEDE.h"
  45. #include "NLMEDE.h"
  46. #include "AddrMgr.h"
  47. #include "ZDProfile.h"
  48. #include "ZDObject.h"
  49. #include "ZDConfig.h"
  50. #include "ZDSecMgr.h"
  51. #include "ZDApp.h"
  52. #include "DebugTrace.h"
  53. #include "nwk_util.h"
  54. #include "OnBoard.h"
  55. #include "ZGlobals.h"
  56. #include "ZDNwkMgr.h"
  57. #include "rtg.h"
  58. #include "ssp.h"
  59. #if defined( MT_ZDO_FUNC )
  60. #include "MT_ZDO.h"
  61. #endif
  62. /* HAL */
  63. #include "hal_led.h"
  64. #include "hal_lcd.h"
  65. #include "hal_key.h"
  66. #if defined( MT_MAC_FUNC ) || defined( MT_MAC_CB_FUNC )
  67. #error "ERROR! MT_MAC functionalities should be disabled on ZDO devices"
  68. #endif
  69. /*********************************************************************
  70. * MACROS
  71. */
  72. /*********************************************************************
  73. * CONSTANTS
  74. */
  75. #if !defined( NWK_START_DELAY )
  76. #define NWK_START_DELAY 100 // in milliseconds
  77. #endif
  78. #if !defined( EXTENDED_JOINING_RANDOM_MASK )
  79. #define EXTENDED_JOINING_RANDOM_MASK 0x007F
  80. #endif
  81. #if !defined( BEACON_REQUEST_DELAY )
  82. #define BEACON_REQUEST_DELAY 100 // in milliseconds
  83. #endif
  84. #if !defined( BEACON_REQ_DELAY_MASK )
  85. #define BEACON_REQ_DELAY_MASK 0x007F
  86. #endif
  87. #define MAX_RESUME_RETRY 3
  88. #define MAX_DEVICE_UNAUTH_TIMEOUT 5000 // 5 seconds
  89. // Beacon Order Settings (see NLMEDE.h)
  90. #define DEFAULT_BEACON_ORDER BEACON_ORDER_NO_BEACONS
  91. #define DEFAULT_SUPERFRAME_ORDER DEFAULT_BEACON_ORDER
  92. #if !defined( MAX_NWK_FRAMECOUNTER_CHANGES )
  93. // The number of times the frame counter can change before
  94. // saving to NV
  95. #define MAX_NWK_FRAMECOUNTER_CHANGES 1000
  96. #endif
  97. // Leave control bits
  98. #define ZDAPP_LEAVE_CTRL_INIT 0
  99. #define ZDAPP_LEAVE_CTRL_SET 1
  100. #define ZDAPP_LEAVE_CTRL_RA 2
  101. // Standard time to update NWK NV data
  102. #define ZDAPP_UPDATE_NWK_NV_TIME 700
  103. // Address Manager Stub Implementation
  104. #define ZDApp_NwkWriteNVRequest AddrMgrWriteNVRequest
  105. /*********************************************************************
  106. * TYPEDEFS
  107. */
  108. /*********************************************************************
  109. * GLOBAL VARIABLES
  110. */
  111. #if defined( LCD_SUPPORTED )
  112. uint8 MatchRsps = 0;
  113. #endif
  114. uint8 zdoDiscCounter = 1;
  115. zAddrType_t ZDAppNwkAddr;
  116. uint8 zdappMgmtNwkDiscRspTransSeq;
  117. uint8 zdappMgmtNwkDiscReqInProgress = FALSE;
  118. zAddrType_t zdappMgmtNwkDiscRspAddr;
  119. uint8 zdappMgmtNwkDiscStartIndex;
  120. uint8 zdappMgmtSavedNwkState;
  121. uint16 nwkFrameCounterChanges = 0;
  122. uint8 continueJoining = TRUE;
  123. uint8 _tmpRejoinState;
  124. // The extended PanID used in ZDO layer for rejoin.
  125. uint8 ZDO_UseExtendedPANID[Z_EXTADDR_LEN];
  126. /*********************************************************************
  127. * EXTERNAL VARIABLES
  128. */
  129. /*********************************************************************
  130. * EXTERNAL FUNCTIONS
  131. */
  132. /*********************************************************************
  133. * LOCAL FUNCTIONS
  134. */
  135. void ZDApp_NetworkStartEvt( void );
  136. void ZDApp_DeviceAuthEvt( void );
  137. void ZDApp_SaveNetworkStateEvt( void );
  138. uint8 ZDApp_ReadNetworkRestoreState( void );
  139. uint8 ZDApp_RestoreNetworkState( void );
  140. void ZDAppDetermineDeviceType( void );
  141. void ZDApp_InitUserDesc( void );
  142. void ZDAppCheckForHoldKey( void );
  143. void ZDApp_ProcessOSALMsg( osal_event_hdr_t *msgPtr );
  144. void ZDApp_ProcessNetworkJoin( void );
  145. void ZDApp_SetCoordAddress( uint8 endPoint, uint8 dstEP );
  146. void ZDApp_SaveNwkKey( void );
  147. uint8 ZDApp_RestoreNwkKey( void );
  148. void ZDApp_SecInit( uint8 state );
  149. UINT16 ZDApp_ProcessSecEvent( uint8 task_id, UINT16 events );
  150. void ZDApp_ProcessSecMsg( osal_event_hdr_t *msgPtr );
  151. void ZDApp_SendMsg( uint8 taskID, uint8 cmd, uint8 len, uint8 *buf );
  152. void ZDApp_ResetTimerStart( uint16 delay );
  153. void ZDApp_ResetTimerCancel( void );
  154. void ZDApp_LeaveCtrlInit( void );
  155. void ZDApp_LeaveCtrlSet( uint8 ra );
  156. uint8 ZDApp_LeaveCtrlBypass( void );
  157. void ZDApp_LeaveCtrlStartup( devStates_t* state, uint16* startDelay );
  158. void ZDApp_LeaveReset( uint8 ra );
  159. void ZDApp_LeaveUpdate( uint16 nwkAddr, uint8* extAddr,
  160. uint8 removeChildren );
  161. void ZDApp_NodeProfileSync( ZDO_NetworkDiscoveryCfm_t* cfm );
  162. void ZDApp_ProcessMsgCBs( zdoIncomingMsg_t *inMsg );
  163. void ZDApp_RegisterCBs( void );
  164. /*********************************************************************
  165. * LOCAL VARIABLES
  166. */
  167. uint8 ZDAppTaskID;
  168. uint8 nwkStatus;
  169. endPointDesc_t *ZDApp_AutoFindMode_epDesc = (endPointDesc_t *)NULL;
  170. uint8 ZDApp_LeaveCtrl;
  171. #if defined( HOLD_AUTO_START )
  172. devStates_t devState = DEV_HOLD;
  173. #else
  174. devStates_t devState = DEV_INIT;
  175. #endif
  176. #if ( ZG_BUILD_RTRONLY_TYPE ) || ( ZG_BUILD_ENDDEVICE_TYPE )
  177. devStartModes_t devStartMode = MODE_JOIN; // Assume joining
  178. //devStartModes_t devStartMode = MODE_RESUME; // if already "directly joined"
  179. // to parent. Set to make the device do an Orphan scan.
  180. #else
  181. // Set the default to coodinator
  182. devStartModes_t devStartMode = MODE_HARD;
  183. #endif
  184. uint8 retryCnt;
  185. endPointDesc_t ZDApp_epDesc =
  186. {
  187. ZDO_EP,
  188. &ZDAppTaskID,
  189. (SimpleDescriptionFormat_t *)NULL, // No Simple description for ZDO
  190. (afNetworkLatencyReq_t)0 // No Network Latency req
  191. };
  192. uint16 ZDApp_SavedPollRate = POLL_RATE;
  193. /*********************************************************************
  194. * @fn ZDApp_Init
  195. *
  196. * @brief ZDApp Initialization function.
  197. *
  198. * @param task_id - ZDApp Task ID
  199. *
  200. * @return None
  201. */
  202. void ZDApp_Init( uint8 task_id )
  203. {
  204. // Save the task ID
  205. ZDAppTaskID = task_id;
  206. // Initialize the ZDO global device short address storage
  207. ZDAppNwkAddr.addrMode = Addr16Bit;
  208. ZDAppNwkAddr.addr.shortAddr = INVALID_NODE_ADDR;
  209. (void)NLME_GetExtAddr(); // Load the saveExtAddr pointer.
  210. // Check for manual "Hold Auto Start"
  211. ZDAppCheckForHoldKey();
  212. // Initialize ZDO items and setup the device - type of device to create.
  213. ZDO_Init();
  214. // Register the endpoint description with the AF
  215. // This task doesn't have a Simple description, but we still need
  216. // to register the endpoint.
  217. afRegister( (endPointDesc_t *)&ZDApp_epDesc );
  218. #if defined( ZDO_USERDESC_RESPONSE )
  219. ZDApp_InitUserDesc();
  220. #endif // ZDO_USERDESC_RESPONSE
  221. // Start the device?
  222. if ( devState != DEV_HOLD )
  223. {
  224. ZDOInitDevice( 0 );
  225. }
  226. else
  227. {
  228. // Blink LED to indicate HOLD_START
  229. HalLedBlink ( HAL_LED_4, 0, 50, 500 );
  230. }
  231. ZDApp_RegisterCBs();
  232. } /* ZDApp_Init() */
  233. /*********************************************************************
  234. * @fn ZDApp_SecInit
  235. *
  236. * @brief ZDApp initialize security.
  237. *
  238. * @param state - device initialization state
  239. *
  240. * @return none
  241. */
  242. void ZDApp_SecInit( uint8 state )
  243. {
  244. if ( ZG_SECURE_ENABLED && ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  245. {
  246. // Set the Trust Center bit
  247. ZDO_Config_Node_Descriptor.ServerMask |= PRIM_TRUST_CENTER;
  248. }
  249. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  250. {
  251. ZDO_Config_Node_Descriptor.CapabilityFlags |= CAPINFO_SECURITY_CAPABLE;
  252. }
  253. // Initialize ZigBee Device Security Manager
  254. ZDSecMgrInit();
  255. if ( ZG_SECURE_ENABLED )
  256. {
  257. if ( state != ZDO_INITDEV_RESTORED_NETWORK_STATE )
  258. {
  259. nwkFrameCounter = 0;
  260. if( _NIB.nwkKeyLoaded == FALSE )
  261. {
  262. if ( ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE ) ||
  263. ( ( zgSecurityMode == ZG_SECURITY_RESIDENTIAL ) && zgPreConfigKeys ) )
  264. {
  265. SSP_UpdateNwkKey( (byte*)zgPreConfigKey, 0);
  266. SSP_SwitchNwkKey( 0 );
  267. }
  268. }
  269. }
  270. }
  271. }
  272. /*********************************************************************
  273. * @fn ZDApp_event_loop()
  274. *
  275. * @brief Main event loop for Zigbee device objects task. This function
  276. * should be called at periodic intervals.
  277. *
  278. * @param task_id - Task ID
  279. * @param events - Bitmap of events
  280. *
  281. * @return none
  282. */
  283. UINT16 ZDApp_event_loop( uint8 task_id, UINT16 events )
  284. {
  285. uint8 *msg_ptr;
  286. if ( events & SYS_EVENT_MSG )
  287. {
  288. while ( (msg_ptr = osal_msg_receive( ZDAppTaskID )) )
  289. {
  290. ZDApp_ProcessOSALMsg( (osal_event_hdr_t *)msg_ptr );
  291. // Release the memory
  292. osal_msg_deallocate( msg_ptr );
  293. }
  294. // Return unprocessed events
  295. return (events ^ SYS_EVENT_MSG);
  296. }
  297. if ( events & ZDO_NETWORK_INIT )
  298. {
  299. // Initialize apps and start the network
  300. devState = DEV_INIT;
  301. ZDO_StartDevice( (uint8)ZDO_Config_Node_Descriptor.LogicalType, devStartMode,
  302. DEFAULT_BEACON_ORDER, DEFAULT_SUPERFRAME_ORDER );
  303. P1_0 = !P1_0; //add by 1305106
  304. // Return unprocessed events
  305. return (events ^ ZDO_NETWORK_INIT);
  306. }
  307. if ( ZSTACK_ROUTER_BUILD )
  308. {
  309. if ( events & ZDO_NETWORK_START )
  310. {
  311. ZDApp_NetworkStartEvt();
  312. // Return unprocessed events
  313. return (events ^ ZDO_NETWORK_START);
  314. }
  315. if ( events & ZDO_ROUTER_START )
  316. {
  317. if ( nwkStatus == ZSuccess )
  318. {
  319. if ( devState == DEV_END_DEVICE )
  320. devState = DEV_ROUTER;
  321. osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
  322. }
  323. else
  324. {
  325. // remain as end device!!
  326. }
  327. osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
  328. // Return unprocessed events
  329. return (events ^ ZDO_ROUTER_START);
  330. }
  331. }
  332. if ( events & ZDO_STATE_CHANGE_EVT )
  333. {
  334. ZDO_UpdateNwkStatus( devState );
  335. // Return unprocessed events
  336. return (events ^ ZDO_STATE_CHANGE_EVT);
  337. }
  338. if ( events & ZDO_COMMAND_CNF )
  339. {
  340. // User defined logic
  341. // Return unprocessed events
  342. return (events ^ ZDO_COMMAND_CNF);
  343. }
  344. if ( events & ZDO_NWK_UPDATE_NV )
  345. {
  346. ZDApp_SaveNetworkStateEvt();
  347. // Return unprocessed events
  348. return (events ^ ZDO_NWK_UPDATE_NV);
  349. }
  350. if ( events & ZDO_DEVICE_RESET )
  351. {
  352. // The device has been in the UNAUTH state, so reset
  353. // Note: there will be no return from this call
  354. SystemReset();
  355. }
  356. if ( ZG_SECURE_ENABLED )
  357. {
  358. return ( ZDApp_ProcessSecEvent( task_id, events ) );
  359. }
  360. else
  361. {
  362. // Discard or make more handlers
  363. return 0;
  364. }
  365. }
  366. /*********************************************************************
  367. * @fn ZDApp_ProcessSecEvent()
  368. *
  369. * @brief Process incoming security events.
  370. *
  371. * @param task_id - Task ID
  372. * @param events - Bitmap of events
  373. *
  374. * @return none
  375. */
  376. UINT16 ZDApp_ProcessSecEvent( uint8 task_id, UINT16 events )
  377. {
  378. (void)task_id; // Intentionally unreferenced parameter
  379. if ( ZSTACK_ROUTER_BUILD )
  380. {
  381. if ( events & ZDO_NEW_DEVICE )
  382. {
  383. // process the new device event
  384. if ( ZDSecMgrNewDeviceEvent() == TRUE )
  385. {
  386. osal_start_timerEx( ZDAppTaskID, ZDO_NEW_DEVICE, 1000 );
  387. }
  388. // Return unprocessed events
  389. return (events ^ ZDO_NEW_DEVICE);
  390. }
  391. }
  392. if ( (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH)
  393. || (ZG_CHECK_SECURITY_MODE == ZG_SECURITY_SE_STANDARD) )
  394. {
  395. if ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  396. {
  397. if ( events & ZDO_SECMGR_EVENT )
  398. {
  399. ZDSecMgrEvent();
  400. // Return unprocessed events
  401. return (events ^ ZDO_SECMGR_EVENT);
  402. }
  403. }
  404. }
  405. if ( events & ZDO_DEVICE_AUTH )
  406. {
  407. ZDApp_DeviceAuthEvt();
  408. // Return unprocessed events
  409. return (events ^ ZDO_DEVICE_AUTH);
  410. }
  411. if ( events & ZDO_FRAMECOUNTER_CHANGE )
  412. {
  413. if ( nwkFrameCounterChanges++ > MAX_NWK_FRAMECOUNTER_CHANGES )
  414. ZDApp_SaveNwkKey();
  415. // Return unprocessed events
  416. return (events ^ ZDO_FRAMECOUNTER_CHANGE);
  417. }
  418. // Discard or make more handlers
  419. return 0;
  420. }
  421. /*********************************************************************
  422. * Application Functions
  423. */
  424. /*********************************************************************
  425. * @fn ZDOInitDevice
  426. *
  427. * @brief Start the device in the network. This function will read
  428. * ZCD_NV_STARTUP_OPTION (NV item) to determine whether or not to
  429. * restore the network state of the device.
  430. *
  431. * @param startDelay - timeDelay to start device (in milliseconds).
  432. * There is a jitter added to this delay:
  433. * ((NWK_START_DELAY + startDelay)
  434. * + (osal_rand() & EXTENDED_JOINING_RANDOM_MASK))
  435. *
  436. * NOTE: If the application would like to force a "new" join, the
  437. * application should set the ZCD_STARTOPT_DEFAULT_NETWORK_STATE
  438. * bit in the ZCD_NV_STARTUP_OPTION NV item before calling
  439. * this function. "new" join means to not restore the network
  440. * state of the device. Use zgWriteStartupOptions() to set these
  441. * options.
  442. *
  443. * @return
  444. * ZDO_INITDEV_RESTORED_NETWORK_STATE - The device's network state was
  445. * restored.
  446. * ZDO_INITDEV_NEW_NETWORK_STATE - The network state was initialized.
  447. * This could mean that ZCD_NV_STARTUP_OPTION said to not restore, or
  448. * it could mean that there was no network state to restore.
  449. * ZDO_INITDEV_LEAVE_NOT_STARTED - Before the reset, a network leave was issued
  450. * with the rejoin option set to TRUE. So, the device was not
  451. * started in the network (one time only). The next time this
  452. * function is called it will start.
  453. */
  454. uint8 ZDOInitDevice( uint16 startDelay )
  455. {
  456. uint8 networkStateNV = ZDO_INITDEV_NEW_NETWORK_STATE;
  457. uint16 extendedDelay = 0;
  458. if ( devState == DEV_HOLD )
  459. {
  460. // Initialize the RAM items table, in case an NV item has been updated.
  461. zgInitItems( FALSE );
  462. }
  463. ZDConfig_InitDescriptors();
  464. //devtag.071807.todo - fix this temporary solution
  465. _NIB.CapabilityInfo = ZDO_Config_Node_Descriptor.CapabilityFlags;
  466. devState = DEV_INIT; // Remove the Hold state
  467. // Initialize leave control logic
  468. ZDApp_LeaveCtrlInit();
  469. // Check leave control reset settings
  470. ZDApp_LeaveCtrlStartup( &devState, &startDelay );
  471. // Leave may make the hold state come back
  472. if ( devState == DEV_HOLD )
  473. return ( ZDO_INITDEV_LEAVE_NOT_STARTED ); // Don't join - (one time).
  474. #if defined ( NV_RESTORE )
  475. // Get Keypad directly to see if a reset nv is needed.
  476. // Hold down the SW_BYPASS_NV key (defined in OnBoard.h)
  477. // while booting to skip past NV Restore.
  478. if ( HalKeyRead() == SW_BYPASS_NV )
  479. networkStateNV = ZDO_INITDEV_NEW_NETWORK_STATE;
  480. else
  481. {
  482. // Determine if NV should be restored
  483. networkStateNV = ZDApp_ReadNetworkRestoreState();
  484. }
  485. if ( networkStateNV == ZDO_INITDEV_RESTORED_NETWORK_STATE )
  486. {
  487. networkStateNV = ZDApp_RestoreNetworkState();
  488. }
  489. else
  490. {
  491. // Wipe out the network state in NV
  492. NLME_InitNV();
  493. NLME_SetDefaultNV();
  494. }
  495. #endif
  496. if ( networkStateNV == ZDO_INITDEV_NEW_NETWORK_STATE )
  497. {
  498. ZDAppDetermineDeviceType();
  499. // Only delay if joining network - not restoring network state
  500. extendedDelay = (uint16)((NWK_START_DELAY + startDelay)
  501. + (osal_rand() & EXTENDED_JOINING_RANDOM_MASK));
  502. }
  503. // Initialize the security for type of device
  504. ZDApp_SecInit( networkStateNV );
  505. // Trigger the network start
  506. ZDApp_NetworkInit( extendedDelay );
  507. // set broadcast address mask to support broadcast filtering
  508. NLME_SetBroadcastFilter( ZDO_Config_Node_Descriptor.CapabilityFlags );
  509. return ( networkStateNV );
  510. }
  511. /*********************************************************************
  512. * @fn ZDApp_ReadNetworkRestoreState
  513. *
  514. * @brief Read the ZCD_NV_STARTUP_OPTION NV Item to state whether
  515. * or not to restore the network state.
  516. * If the read value has the ZCD_STARTOPT_DEFAULT_NETWORK_STATE
  517. * bit set return the ZDO_INITDEV_NEW_NETWORK_STATE.
  518. *
  519. * @param none
  520. *
  521. * @return ZDO_INITDEV_NEW_NETWORK_STATE
  522. * or ZDO_INITDEV_RESTORED_NETWORK_STATE based on whether or
  523. * not ZCD_STARTOPT_DEFAULT_NETWORK_STATE bit is set in
  524. * ZCD_NV_STARTUP_OPTION
  525. */
  526. uint8 ZDApp_ReadNetworkRestoreState( void )
  527. {
  528. uint8 networkStateNV = ZDO_INITDEV_RESTORED_NETWORK_STATE;
  529. // Look for the New Network State option.
  530. if ( zgReadStartupOptions() & ZCD_STARTOPT_DEFAULT_NETWORK_STATE )
  531. {
  532. networkStateNV = ZDO_INITDEV_NEW_NETWORK_STATE;
  533. }
  534. return ( networkStateNV );
  535. }
  536. /*********************************************************************
  537. * @fn ZDAppDetermineDeviceType()
  538. *
  539. * @brief Determines the type of device to start.
  540. *
  541. * Looks at zgDeviceLogicalType and determines what type of
  542. * device to start. The types are:
  543. * ZG_DEVICETYPE_COORDINATOR
  544. * ZG_DEVICETYPE_ROUTER
  545. * ZG_DEVICETYPE_ENDDEVICE
  546. *
  547. * @param none
  548. *
  549. * @return none
  550. */
  551. void ZDAppDetermineDeviceType( void )
  552. {
  553. if ( zgDeviceLogicalType == ZG_DEVICETYPE_COORDINATOR )
  554. {
  555. devStartMode = MODE_HARD; // Start as a coordinator
  556. ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_COORDINATOR;
  557. }
  558. else
  559. {
  560. if ( zgDeviceLogicalType == ZG_DEVICETYPE_ROUTER )
  561. ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_ROUTER;
  562. else if ( zgDeviceLogicalType == ZG_DEVICETYPE_ENDDEVICE )
  563. ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_DEVICE;
  564. // If AIB_apsUseExtendedPANID is set to a non-zero value by commissioning
  565. // The device shall do rejoin the network. Otherwise, do normal join
  566. if ( nwk_ExtPANIDValid( AIB_apsUseExtendedPANID ) == false )
  567. {
  568. devStartMode = MODE_JOIN; // Assume joining
  569. }
  570. else
  571. {
  572. devStartMode = MODE_REJOIN;
  573. }
  574. }
  575. }
  576. /*********************************************************************
  577. * @fn ZDApp_NetworkStartEvt()
  578. *
  579. * @brief Process the Network Start Event
  580. *
  581. * @param none
  582. *
  583. * @return none
  584. */
  585. void ZDApp_NetworkStartEvt( void )
  586. {
  587. if ( nwkStatus == ZSuccess )
  588. {
  589. // Successfully started a ZigBee network
  590. if ( devState == DEV_COORD_STARTING )
  591. {
  592. devState = DEV_ZB_COORD;
  593. }
  594. osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
  595. osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
  596. }
  597. else
  598. {
  599. // Try again with a higher energy threshold !!
  600. if ( ( NLME_GetEnergyThreshold() + ENERGY_SCAN_INCREMENT ) < 0xff )
  601. {
  602. NLME_SetEnergyThreshold( (uint8)(NLME_GetEnergyThreshold() + ENERGY_SCAN_INCREMENT) );
  603. osal_set_event( ZDAppTaskID, ZDO_NETWORK_INIT );
  604. }
  605. else
  606. {
  607. // Failed to start network. Enter a dormant state (until user intervenes)
  608. devState = DEV_INIT;
  609. osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
  610. }
  611. }
  612. }
  613. /*********************************************************************
  614. * @fn ZDApp_DeviceAuthEvt()
  615. *
  616. * @brief Process the Device Authentic Event
  617. *
  618. * @param none
  619. *
  620. * @return none
  621. */
  622. void ZDApp_DeviceAuthEvt( void )
  623. {
  624. // received authentication from trust center
  625. if ( devState == DEV_END_DEVICE_UNAUTH )
  626. {
  627. // Stop the reset timer so it doesn't reset
  628. ZDApp_ResetTimerCancel();
  629. devState = DEV_END_DEVICE;
  630. osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
  631. // Set the Power Manager Device
  632. #if defined ( POWER_SAVING )
  633. osal_pwrmgr_device( PWRMGR_BATTERY );
  634. #endif
  635. if ( ZSTACK_ROUTER_BUILD )
  636. {
  637. if ( ZDO_Config_Node_Descriptor.LogicalType != NODETYPE_DEVICE )
  638. {
  639. // NOTE: first two parameters are not used, see NLMEDE.h for details
  640. NLME_StartRouterRequest( 0, 0, false );
  641. }
  642. }
  643. // Notify to save info into NV
  644. ZDApp_NVUpdate();
  645. // Save off the security
  646. ZDApp_SaveNwkKey();
  647. ZDApp_AnnounceNewAddress();
  648. if ( (ZDO_Config_Node_Descriptor.CapabilityFlags & CAPINFO_RCVR_ON_IDLE) == 0 )
  649. {
  650. NLME_SetPollRate( ZDApp_SavedPollRate );
  651. }
  652. }
  653. else
  654. {
  655. ZDApp_NVUpdate();
  656. }
  657. }
  658. /*********************************************************************
  659. * @fn ZDApp_SaveNetworkStateEvt()
  660. *
  661. * @brief Process the Save the Network State Event
  662. *
  663. * @param none
  664. *
  665. * @return none
  666. */
  667. void ZDApp_SaveNetworkStateEvt( void )
  668. {
  669. #if defined ( NV_RESTORE )
  670. #if defined ( NV_TURN_OFF_RADIO )
  671. // Turn off the radio's receiver during an NV update
  672. uint8 RxOnIdle;
  673. uint8 x = false;
  674. ZMacGetReq( ZMacRxOnIdle, &RxOnIdle );
  675. ZMacSetReq( ZMacRxOnIdle, &x );
  676. #endif
  677. // Update the Network State in NV
  678. NLME_UpdateNV( NWK_NV_NIB_ENABLE |
  679. NWK_NV_DEVICELIST_ENABLE |
  680. NWK_NV_BINDING_ENABLE |
  681. NWK_NV_ADDRMGR_ENABLE );
  682. // Reset the NV startup option to resume from NV by
  683. // clearing the "New" join option.
  684. zgWriteStartupOptions( FALSE, ZCD_STARTOPT_DEFAULT_NETWORK_STATE );
  685. #if defined ( NV_TURN_OFF_RADIO )
  686. ZMacSetReq( ZMacRxOnIdle, &RxOnIdle );
  687. #endif
  688. #endif // NV_RESTORE
  689. }
  690. /*********************************************************************
  691. * @fn ZDApp_RestoreNetworkState()
  692. *
  693. * @brief This function will restore the network state of the
  694. * device if the network state is stored in NV.
  695. *
  696. * @param none
  697. *
  698. * @return
  699. * ZDO_INITDEV_RESTORED_NETWORK_STATE - The device's network state was
  700. * restored.
  701. * ZDO_INITDEV_NEW_NETWORK_STATE - The network state was not used.
  702. * This could mean that zgStartupOption said to not restore, or
  703. * it could mean that there was no network state to restore.
  704. *
  705. */
  706. uint8 ZDApp_RestoreNetworkState( void )
  707. {
  708. uint8 nvStat;
  709. nwkActiveKeyItems keyItems;
  710. // Initialize NWK NV items
  711. nvStat = NLME_InitNV();
  712. if ( nvStat != NV_OPER_FAILED )
  713. {
  714. if ( NLME_RestoreFromNV() )
  715. {
  716. // Are we a coordinator
  717. ZDAppNwkAddr.addr.shortAddr = NLME_GetShortAddr();
  718. if ( ZDAppNwkAddr.addr.shortAddr == 0 )
  719. ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_COORDINATOR;
  720. devStartMode = MODE_RESUME;
  721. osal_cpyExtAddr( ZDO_UseExtendedPANID, _NIB.extendedPANID );
  722. }
  723. else
  724. nvStat = NV_ITEM_UNINIT;
  725. if ( ZG_SECURE_ENABLED )
  726. {
  727. nwkFrameCounterChanges = 0;
  728. osal_memset( &keyItems, 0, sizeof( nwkActiveKeyItems ) );
  729. osal_nv_item_init( ZCD_NV_NWKKEY, sizeof(nwkActiveKeyItems), (void *)&keyItems );
  730. if ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  731. {
  732. ZDApp_RestoreNwkKey();
  733. }
  734. }
  735. // The default for RxOnWhenIdle is true for Routers and false for end devices
  736. // [setup in the NLME_RestoreFromNV()]. Change it here if you want something
  737. // other than default.
  738. }
  739. if ( nvStat == ZSUCCESS )
  740. return ( ZDO_INITDEV_RESTORED_NETWORK_STATE );
  741. else
  742. return ( ZDO_INITDEV_NEW_NETWORK_STATE );
  743. }
  744. /*********************************************************************
  745. * @fn ZDApp_InitUserDesc()
  746. *
  747. * @brief Initialize the User Descriptor, the descriptor is read from NV
  748. * when needed. If you want to initialize the User descriptor to
  749. * something other than all zero, do it here.
  750. *
  751. * @param none
  752. *
  753. * @return none
  754. */
  755. void ZDApp_InitUserDesc( void )
  756. {
  757. UserDescriptorFormat_t ZDO_DefaultUserDescriptor;
  758. // Initialize the User Descriptor, the descriptor is read from NV
  759. // when needed. If you want to initialize the User descriptor to something
  760. // other than all zero, do it here.
  761. osal_memset( &ZDO_DefaultUserDescriptor, 0, sizeof( UserDescriptorFormat_t ) );
  762. if ( ZSUCCESS == osal_nv_item_init( ZCD_NV_USERDESC,
  763. sizeof(UserDescriptorFormat_t), (void*)&ZDO_DefaultUserDescriptor ) )
  764. {
  765. if ( ZSUCCESS == osal_nv_read( ZCD_NV_USERDESC, 0,
  766. sizeof(UserDescriptorFormat_t), (void*)&ZDO_DefaultUserDescriptor ) )
  767. {
  768. if ( ZDO_DefaultUserDescriptor.len != 0 )
  769. {
  770. ZDO_Config_Node_Descriptor.UserDescAvail = TRUE;
  771. }
  772. }
  773. }
  774. }
  775. /*********************************************************************
  776. * @fn ZDAppCheckForHoldKey()
  777. *
  778. * @brief Check for key to set the device into Hold Auto Start
  779. *
  780. * @param none
  781. *
  782. * @return none
  783. */
  784. void ZDAppCheckForHoldKey( void )
  785. {
  786. #if (defined HAL_KEY) && (HAL_KEY == TRUE)
  787. // Get Keypad directly to see if a HOLD_START is needed.
  788. // Hold down the SW_BYPASS_START key (see OnBoard.h)
  789. // while booting to avoid starting up the device.
  790. if ( HalKeyRead () == SW_BYPASS_START)
  791. {
  792. // Change the device state to HOLD on start up
  793. devState = DEV_HOLD;
  794. }
  795. #endif // HAL_KEY
  796. }
  797. /*********************************************************************
  798. * @fn ZDApp_ProcessOSALMsg()
  799. *
  800. * @brief Process the incoming task message.
  801. *
  802. * @param msgPtr - message to process
  803. *
  804. * @return none
  805. */
  806. void ZDApp_ProcessOSALMsg( osal_event_hdr_t *msgPtr )
  807. {
  808. // Data Confirmation message fields
  809. uint8 sentEP; // This should always be 0
  810. uint8 sentStatus;
  811. afDataConfirm_t *afDataConfirm;
  812. uint8 tmp;
  813. switch ( msgPtr->event )
  814. {
  815. // Incoming ZDO Message
  816. case AF_INCOMING_MSG_CMD:
  817. ZDP_IncomingData( (afIncomingMSGPacket_t *)msgPtr );
  818. break;
  819. case ZDO_CB_MSG:
  820. ZDApp_ProcessMsgCBs( (zdoIncomingMsg_t *)msgPtr );
  821. break;
  822. case AF_DATA_CONFIRM_CMD:
  823. // This message is received as a confirmation of a data packet sent.
  824. // The status is of ZStatus_t type [defined in NLMEDE.h]
  825. // The message fields are defined in AF.h
  826. afDataConfirm = (afDataConfirm_t *)msgPtr;
  827. sentEP = afDataConfirm->endpoint;
  828. sentStatus = afDataConfirm->hdr.status;
  829. // Action taken when confirmation is received.
  830. #if defined ( ZIGBEE_FREQ_AGILITY )
  831. if ( pZDNwkMgr_ProcessDataConfirm )
  832. pZDNwkMgr_ProcessDataConfirm( afDataConfirm );
  833. #endif
  834. (void)sentEP;
  835. (void)sentStatus;
  836. break;
  837. case ZDO_NWK_DISC_CNF:
  838. if (devState != DEV_NWK_DISC)
  839. break;
  840. if ( ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE )
  841. {
  842. if ( (((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->hdr.status == ZDO_SUCCESS) && (zdoDiscCounter > NUM_DISC_ATTEMPTS) )
  843. {
  844. if ( devStartMode == MODE_JOIN )
  845. {
  846. devState = DEV_NWK_JOINING;
  847. ZDApp_NodeProfileSync((ZDO_NetworkDiscoveryCfm_t *)msgPtr);
  848. if ( NLME_JoinRequest( ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->extendedPANID,
  849. BUILD_UINT16( ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->panIdLSB, ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->panIdMSB ),
  850. ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->logicalChannel,
  851. ZDO_Config_Node_Descriptor.CapabilityFlags ) != ZSuccess )
  852. {
  853. ZDApp_NetworkInit( (uint16)(NWK_START_DELAY
  854. + ((uint16)(osal_rand()& EXTENDED_JOINING_RANDOM_MASK))) );
  855. }
  856. }
  857. else if ( devStartMode == MODE_REJOIN )
  858. {
  859. devState = DEV_NWK_REJOIN;
  860. // Before trying to do rejoin, check if the device has a valid short address
  861. // If not, generate a random short address for itself
  862. if ( _NIB.nwkDevAddress == INVALID_NODE_ADDR )
  863. {
  864. _NIB.nwkDevAddress = osal_rand();
  865. ZMacSetReq( ZMacShortAddress, (byte*)&_NIB.nwkDevAddress );
  866. }
  867. // Check if the device has a valid PanID, if not, set it to the discovered Pan
  868. if ( _NIB.nwkPanId == INVALID_PAN_ID )
  869. {
  870. _NIB.nwkPanId = BUILD_UINT16( ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->panIdLSB, ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->panIdMSB );
  871. ZMacSetReq( ZMacPanId, (byte*)&(_NIB.nwkPanId) );
  872. }
  873. tmp = true;
  874. ZMacSetReq( ZMacRxOnIdle, &tmp ); // Set receiver always on during rejoin
  875. if ( NLME_ReJoinRequest( ZDO_UseExtendedPANID, ((ZDO_NetworkDiscoveryCfm_t *)msgPtr)->logicalChannel) != ZSuccess )
  876. {
  877. ZDApp_NetworkInit( (uint16)(NWK_START_DELAY
  878. + ((uint16)(osal_rand()& EXTENDED_JOINING_RANDOM_MASK))) );
  879. }
  880. }
  881. if ( ZDO_Config_Node_Descriptor.CapabilityFlags & CAPINFO_RCVR_ON_IDLE )
  882. {
  883. // The receiver is on, turn network layer polling off.
  884. NLME_SetPollRate( 0 );
  885. NLME_SetQueuedPollRate( 0 );
  886. NLME_SetResponseRate( 0 );
  887. }
  888. else
  889. {
  890. if ( (ZG_SECURE_ENABLED) && (devStartMode == MODE_JOIN) )
  891. {
  892. ZDApp_SavedPollRate = zgPollRate;
  893. NLME_SetPollRate( zgRejoinPollRate );
  894. }
  895. }
  896. }
  897. else
  898. {
  899. if ( continueJoining )
  900. {
  901. #if defined ( MANAGED_SCAN )
  902. ZDApp_NetworkInit( MANAGEDSCAN_DELAY_BETWEEN_SCANS );
  903. #else
  904. zdoDiscCounter++;
  905. ZDApp_NetworkInit( (uint16)(BEACON_REQUEST_DELAY
  906. + ((uint16)(osal_rand()& BEACON_REQ_DELAY_MASK))) );
  907. #endif
  908. }
  909. }
  910. }
  911. break;
  912. case ZDO_NWK_JOIN_IND:
  913. if ( ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE )
  914. {
  915. ZDApp_ProcessNetworkJoin();
  916. }
  917. break;
  918. case ZDO_NWK_JOIN_REQ:
  919. if ( ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE )
  920. {
  921. retryCnt = 0;
  922. devStartMode = MODE_RESUME;
  923. _tmpRejoinState = true;
  924. osal_cpyExtAddr( ZDO_UseExtendedPANID, _NIB.extendedPANID );
  925. zgDefaultStartingScanDuration = BEACON_ORDER_60_MSEC;
  926. ZDApp_NetworkInit( 0 );
  927. }
  928. break;
  929. default:
  930. if ( ZG_SECURE_ENABLED )
  931. ZDApp_ProcessSecMsg( msgPtr );
  932. break;
  933. }
  934. }
  935. /*********************************************************************
  936. * @fn ZDApp_ProcessMsgCBs()
  937. *
  938. * @brief Process response messages
  939. *
  940. * @param none
  941. *
  942. * @return none
  943. */
  944. void ZDApp_ProcessMsgCBs( zdoIncomingMsg_t *inMsg )
  945. {
  946. switch ( inMsg->clusterID )
  947. {
  948. #if defined ( ZDO_NWKADDR_REQUEST ) || defined ( ZDO_IEEEADDR_REQUEST ) || defined ( REFLECTOR )
  949. case NWK_addr_rsp:
  950. case IEEE_addr_rsp:
  951. {
  952. ZDO_NwkIEEEAddrResp_t *pAddrRsp;
  953. pAddrRsp = ZDO_ParseAddrRsp( inMsg );
  954. if ( pAddrRsp )
  955. {
  956. if ( pAddrRsp->status == ZSuccess )
  957. {
  958. ZDO_UpdateAddrManager( pAddrRsp->nwkAddr, pAddrRsp->extAddr );
  959. }
  960. osal_mem_free( pAddrRsp );
  961. }
  962. }
  963. break;
  964. #endif
  965. #if defined ( REFLECTOR )
  966. case Bind_req:
  967. case Unbind_req:
  968. {
  969. ZDO_BindUnbindReq_t bindReq;
  970. ZDO_ParseBindUnbindReq( inMsg, &bindReq );
  971. ZDO_ProcessBindUnbindReq( inMsg, &bindReq );
  972. }
  973. break;
  974. #endif
  975. #if defined ( ZDO_COORDINATOR )
  976. case Bind_rsp:
  977. case Unbind_rsp:
  978. if ( matchED )
  979. {
  980. ZDMatchSendState(
  981. (uint8)((inMsg->clusterID == Bind_rsp) ? ZDMATCH_REASON_BIND_RSP : ZDMATCH_REASON_UNBIND_RSP),
  982. ZDO_ParseBindRsp(inMsg), inMsg->TransSeq );
  983. }
  984. break;
  985. case End_Device_Bind_req:
  986. {
  987. ZDEndDeviceBind_t bindReq;
  988. ZDO_ParseEndDeviceBindReq( inMsg, &bindReq );
  989. ZDO_MatchEndDeviceBind( &bindReq );
  990. // Freeing the cluster lists - if allocated.
  991. if ( bindReq.numInClusters )
  992. osal_mem_free( bindReq.inClusters );
  993. if ( bindReq.numOutClusters )
  994. osal_mem_free( bindReq.outClusters );
  995. }
  996. break;
  997. #endif
  998. }
  999. }
  1000. /*********************************************************************
  1001. * @fn ZDApp_RegisterCBs()
  1002. *
  1003. * @brief Process response messages
  1004. *
  1005. * @param none
  1006. *
  1007. * @return none
  1008. */
  1009. void ZDApp_RegisterCBs( void )
  1010. {
  1011. #if defined ( ZDO_IEEEADDR_REQUEST ) || defined ( REFLECTOR )
  1012. ZDO_RegisterForZDOMsg( ZDAppTaskID, IEEE_addr_rsp );
  1013. #endif
  1014. #if defined ( ZDO_NWKADDR_REQUEST ) || defined ( REFLECTOR )
  1015. ZDO_RegisterForZDOMsg( ZDAppTaskID, NWK_addr_rsp );
  1016. #endif
  1017. #if defined ( ZDO_COORDINATOR )
  1018. ZDO_RegisterForZDOMsg( ZDAppTaskID, Bind_rsp );
  1019. ZDO_RegisterForZDOMsg( ZDAppTaskID, Unbind_rsp );
  1020. ZDO_RegisterForZDOMsg( ZDAppTaskID, End_Device_Bind_req );
  1021. #endif
  1022. #if defined ( REFLECTOR )
  1023. ZDO_RegisterForZDOMsg( ZDAppTaskID, Bind_req );
  1024. ZDO_RegisterForZDOMsg( ZDAppTaskID, Unbind_req );
  1025. #endif
  1026. }
  1027. /*********************************************************************
  1028. * @fn ZDApp_ProcessSecMsg()
  1029. *
  1030. * @brief Process the incoming security message.
  1031. *
  1032. * @param msgPtr - message to process
  1033. *
  1034. * @return none
  1035. */
  1036. void ZDApp_ProcessSecMsg( osal_event_hdr_t *msgPtr )
  1037. {
  1038. switch ( msgPtr->event )
  1039. {
  1040. case ZDO_ESTABLISH_KEY_CFM:
  1041. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1042. {
  1043. ZDSecMgrEstablishKeyCfm( (ZDO_EstablishKeyCfm_t*)msgPtr );
  1044. }
  1045. break;
  1046. case ZDO_ESTABLISH_KEY_IND:
  1047. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1048. {
  1049. if ( ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE )
  1050. {
  1051. ZDSecMgrEstablishKeyInd( (ZDO_EstablishKeyInd_t*)msgPtr );
  1052. }
  1053. }
  1054. break;
  1055. case ZDO_TRANSPORT_KEY_IND:
  1056. if ( ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE )
  1057. {
  1058. ZDSecMgrTransportKeyInd( (ZDO_TransportKeyInd_t*)msgPtr );
  1059. }
  1060. break;
  1061. case ZDO_UPDATE_DEVICE_IND:
  1062. if ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  1063. {
  1064. ZDSecMgrUpdateDeviceInd( (ZDO_UpdateDeviceInd_t*)msgPtr );
  1065. }
  1066. break;
  1067. case ZDO_REMOVE_DEVICE_IND:
  1068. if ( ZG_BUILD_RTRONLY_TYPE && ( zgDeviceLogicalType == ZG_DEVICETYPE_ROUTER ) )
  1069. {
  1070. ZDSecMgrRemoveDeviceInd( (ZDO_RemoveDeviceInd_t*)msgPtr );
  1071. }
  1072. break;
  1073. case ZDO_REQUEST_KEY_IND:
  1074. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1075. {
  1076. if ( ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE )
  1077. {
  1078. ZDSecMgrRequestKeyInd( (ZDO_RequestKeyInd_t*)msgPtr );
  1079. }
  1080. }
  1081. break;
  1082. case ZDO_SWITCH_KEY_IND:
  1083. if ( ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE )
  1084. {
  1085. ZDSecMgrSwitchKeyInd( (ZDO_SwitchKeyInd_t*)msgPtr );
  1086. }
  1087. break;
  1088. case ZDO_AUTHENTICATE_IND:
  1089. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1090. {
  1091. ZDSecMgrAuthenticateInd( (ZDO_AuthenticateInd_t*)msgPtr );
  1092. }
  1093. break;
  1094. case ZDO_AUTHENTICATE_CFM:
  1095. if ( ZG_CHECK_SECURITY_MODE == ZG_SECURITY_PRO_HIGH )
  1096. {
  1097. ZDSecMgrAuthenticateCfm( (ZDO_AuthenticateCfm_t*)msgPtr );
  1098. }
  1099. break;
  1100. default:
  1101. // Unsupported messages
  1102. break;
  1103. }
  1104. }
  1105. /*********************************************************************
  1106. * @fn ZDApp_ProcessNetworkJoin()
  1107. *
  1108. * @brief
  1109. *
  1110. * Save off the Network key information.
  1111. *
  1112. * @param none
  1113. *
  1114. * @return none
  1115. */
  1116. void ZDApp_ProcessNetworkJoin( void )
  1117. {
  1118. if ( (devState == DEV_NWK_JOINING) ||
  1119. ((devState == DEV_NWK_ORPHAN) &&
  1120. (ZDO_Config_Node_Descriptor.LogicalType == NODETYPE_ROUTER)) )
  1121. {
  1122. // Result of a Join attempt by this device.
  1123. if ( nwkStatus == ZSuccess )
  1124. {
  1125. osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
  1126. #if defined ( POWER_SAVING )
  1127. osal_pwrmgr_device( PWRMGR_BATTERY );
  1128. #endif
  1129. if ( ZG_SECURE_ENABLED && ( ZDApp_RestoreNwkKey() == false ) )
  1130. {
  1131. // wait for auth from trust center!!
  1132. devState = DEV_END_DEVICE_UNAUTH;
  1133. // Start the reset timer for MAX UNAUTH time
  1134. ZDApp_ResetTimerStart( 10000 );//MAX_DEVICE_UNAUTH_TIMEOUT );
  1135. }
  1136. else
  1137. {
  1138. if ( ZSTACK_ROUTER_BUILD )
  1139. {
  1140. if ( devState == DEV_NWK_ORPHAN
  1141. && ZDO_Config_Node_Descriptor.LogicalType != NODETYPE_DEVICE )
  1142. {
  1143. // Change NIB state to router for restore
  1144. _NIB.nwkState = NWK_ROUTER;
  1145. }
  1146. }
  1147. if ( devState == DEV_NWK_JOINING )
  1148. {
  1149. ZDApp_AnnounceNewAddress();
  1150. }
  1151. devState = DEV_END_DEVICE;
  1152. if ( ZSTACK_ROUTER_BUILD )
  1153. {
  1154. // NOTE: first two parameters are not used, see NLMEDE.h for details
  1155. if ( ZDO_Config_Node_Descriptor.LogicalType != NODETYPE_DEVICE )
  1156. {
  1157. NLME_StartRouterRequest( 0, 0, false );
  1158. }
  1159. }
  1160. }
  1161. }
  1162. else
  1163. {
  1164. if ( (devStartMode == MODE_RESUME) && (++retryCnt >= MAX_RESUME_RETRY) )
  1165. {
  1166. if ( _NIB.nwkPanId == 0xFFFF || _NIB.nwkPanId == INVALID_PAN_ID )
  1167. devStartMode = MODE_JOIN;
  1168. else
  1169. {
  1170. devStartMode = MODE_REJOIN;
  1171. _tmpRejoinState = true;
  1172. }
  1173. }
  1174. if ( (NLME_GetShortAddr() != INVALID_NODE_ADDR) ||
  1175. (_NIB.nwkDevAddress != INVALID_NODE_ADDR) )
  1176. {
  1177. uint16 addr = INVALID_NODE_ADDR;
  1178. // Invalidate nwk addr so end device does not use in its data reqs.
  1179. _NIB.nwkDevAddress = INVALID_NODE_ADDR;
  1180. ZMacSetReq( ZMacShortAddress, (uint8 *)&addr );
  1181. }
  1182. zdoDiscCounter = 1;
  1183. // ZDApp_NetworkInit( (uint16)
  1184. // ((NWK_START_DELAY * (osal_rand() & 0x0F)) +
  1185. // (NWK_START_DELAY * 5)) );
  1186. ZDApp_NetworkInit( (uint16)(NWK_START_DELAY
  1187. + ((uint16)(osal_rand()& EXTENDED_JOINING_RANDOM_MASK))) );
  1188. }
  1189. }
  1190. else if ( devState == DEV_NWK_ORPHAN || devState == DEV_NWK_REJOIN )
  1191. {
  1192. // results of an orphaning attempt by this device
  1193. if (nwkStatus == ZSuccess)
  1194. {
  1195. if ( ZG_SECURE_ENABLED )
  1196. {
  1197. ZDApp_RestoreNwkKey();
  1198. }
  1199. devState = DEV_END_DEVICE;
  1200. osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
  1201. // setup Power Manager Device
  1202. #if defined ( POWER_SAVING )
  1203. osal_pwrmgr_device( PWRMGR_BATTERY );
  1204. #endif
  1205. if ( ZDO_Config_Node_Descriptor.CapabilityFlags & CAPINFO_RCVR_ON_IDLE )
  1206. {
  1207. // The receiver is on, turn network layer polling off.
  1208. NLME_SetPollRate( 0 );
  1209. NLME_SetQueuedPollRate( 0 );
  1210. NLME_SetResponseRate( 0 );
  1211. }
  1212. if ( ZSTACK_ROUTER_BUILD )
  1213. {
  1214. // NOTE: first two parameters are not used, see NLMEDE.h for details
  1215. if ( ZDO_Config_Node_Descriptor.LogicalType != NODETYPE_DEVICE )
  1216. {
  1217. NLME_StartRouterRequest( 0, 0, false );
  1218. }
  1219. }
  1220. ZDApp_AnnounceNewAddress();
  1221. }
  1222. else
  1223. {
  1224. if ( devStartMode == MODE_RESUME )
  1225. {
  1226. if ( ++retryCnt <= MAX_RESUME_RETRY )
  1227. {
  1228. if ( _NIB.nwkPanId == 0xFFFF || _NIB.nwkPanId == INVALID_PAN_ID )
  1229. devStartMode = MODE_JOIN;
  1230. else
  1231. {
  1232. devStartMode = MODE_REJOIN;
  1233. _tmpRejoinState = true;
  1234. }
  1235. }
  1236. // Do a normal join to the network after certain times of rejoin retries
  1237. else if( AIB_apsUseInsecureJoin == true )
  1238. {
  1239. devStartMode = MODE_JOIN;
  1240. }
  1241. }
  1242. // setup a retry for later...
  1243. ZDApp_NetworkInit( (uint16)(NWK_START_DELAY
  1244. + (osal_rand()& EXTENDED_JOINING_RANDOM_MASK)) );
  1245. }
  1246. }
  1247. #if defined ( ZIGBEE_STOCHASTIC_ADDRESSING )
  1248. else
  1249. {
  1250. // Assume from address conflict
  1251. if ( _NIB.nwkAddrAlloc == NWK_ADDRESSING_STOCHASTIC )
  1252. {
  1253. // Notify the network
  1254. ZDApp_AnnounceNewAddress();
  1255. // Notify apps
  1256. osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
  1257. }
  1258. }
  1259. #endif
  1260. }
  1261. /*********************************************************************
  1262. * @fn ZDApp_SaveNwkKey()
  1263. *
  1264. * @brief Save off the Network key information.
  1265. *
  1266. * @param none
  1267. *
  1268. * @return none
  1269. */
  1270. void ZDApp_SaveNwkKey( void )
  1271. {
  1272. nwkActiveKeyItems keyItems;
  1273. SSP_ReadNwkActiveKey( &keyItems );
  1274. keyItems.frameCounter++;
  1275. osal_nv_write( ZCD_NV_NWKKEY, 0, sizeof( nwkActiveKeyItems ),
  1276. (void *)&keyItems );
  1277. nwkFrameCounterChanges = 0;
  1278. }
  1279. /*********************************************************************
  1280. * @fn ZDApp_ResetNwkKey()
  1281. *
  1282. * @brief Reset the Network key information in NV.
  1283. *
  1284. * @param none
  1285. *
  1286. * @return none
  1287. */
  1288. void ZDApp_ResetNwkKey( void )
  1289. {
  1290. nwkActiveKeyItems keyItems;
  1291. osal_memset( &keyItems, 0, sizeof( nwkActiveKeyItems ) );
  1292. osal_nv_write( ZCD_NV_NWKKEY, 0, sizeof( nwkActiveKeyItems ),
  1293. (void *)&keyItems );
  1294. }
  1295. /*********************************************************************
  1296. * @fn ZDApp_RestoreNwkKey()
  1297. *
  1298. * @brief
  1299. *
  1300. * Save off the Network key information.
  1301. *
  1302. * @param none
  1303. *
  1304. * @return true if restored from NV, false if not
  1305. */
  1306. uint8 ZDApp_RestoreNwkKey( void )
  1307. {
  1308. nwkActiveKeyItems keyItems;
  1309. uint8 ret = false;
  1310. if ( osal_nv_read( ZCD_NV_NWKKEY, 0, sizeof(nwkActiveKeyItems), (void*)&keyItems )
  1311. == ZSUCCESS )
  1312. {
  1313. if ( keyItems.frameCounter > 0 )
  1314. {
  1315. // Restore the key information
  1316. keyItems.frameCounter += MAX_NWK_FRAMECOUNTER_CHANGES;
  1317. nwkFrameCounter = keyItems.frameCounter;
  1318. ret = true;
  1319. }
  1320. // Force a save for the first frame counter increment
  1321. nwkFrameCounterChanges = MAX_NWK_FRAMECOUNTER_CHANGES + 1;
  1322. }
  1323. return ( ret );
  1324. }
  1325. /*********************************************************************
  1326. * @fn ZDApp_ResetTimerStart
  1327. *
  1328. * @brief Start the reset timer.
  1329. *
  1330. * @param delay - delay time(ms) before reset
  1331. *
  1332. * @return none
  1333. */
  1334. void ZDApp_ResetTimerStart( uint16 delay )
  1335. {
  1336. // Start the rest timer
  1337. osal_start_timerEx( ZDAppTaskID, ZDO_DEVICE_RESET, delay );
  1338. }
  1339. /*********************************************************************
  1340. * @fn ZDApp_ResetTimerCancel
  1341. *
  1342. * @brief Cancel the reset timer.
  1343. *
  1344. * @param none
  1345. *
  1346. * @return none
  1347. */
  1348. void ZDApp_ResetTimerCancel( void )
  1349. {
  1350. // Cancel the reset timer
  1351. osal_stop_timerEx( ZDAppTaskID, ZDO_DEVICE_RESET );
  1352. }
  1353. /*********************************************************************
  1354. * @fn ZDApp_LeaveCtrlInit
  1355. *
  1356. * @brief Initialize the leave control logic.
  1357. *
  1358. * @param none
  1359. *
  1360. * @return none
  1361. */
  1362. void ZDApp_LeaveCtrlInit( void )
  1363. {
  1364. uint8 status;
  1365. // Initialize control state
  1366. ZDApp_LeaveCtrl = ZDAPP_LEAVE_CTRL_INIT;
  1367. status = osal_nv_item_init( ZCD_NV_LEAVE_CTRL,
  1368. sizeof(ZDApp_LeaveCtrl),
  1369. &ZDApp_LeaveCtrl );
  1370. if ( status == ZSUCCESS )
  1371. {
  1372. // Read saved control
  1373. osal_nv_read( ZCD_NV_LEAVE_CTRL,
  1374. 0,
  1375. sizeof( uint8 ),
  1376. &ZDApp_LeaveCtrl);
  1377. }
  1378. }
  1379. /*********************************************************************
  1380. * @fn ZDApp_LeaveCtrlSet
  1381. *
  1382. * @brief Set the leave control logic.
  1383. *
  1384. * @param ra - reassociate flag
  1385. *
  1386. * @return none
  1387. */
  1388. void ZDApp_LeaveCtrlSet( uint8 ra )
  1389. {
  1390. ZDApp_LeaveCtrl = ZDAPP_LEAVE_CTRL_SET;
  1391. if ( ra == TRUE )
  1392. {
  1393. ZDApp_LeaveCtrl |= ZDAPP_LEAVE_CTRL_RA;
  1394. }
  1395. // Write the leave control
  1396. osal_nv_write( ZCD_NV_LEAVE_CTRL,
  1397. 0,
  1398. sizeof( uint8 ),
  1399. &ZDApp_LeaveCtrl);
  1400. }
  1401. /*********************************************************************
  1402. * @fn ZDApp_LeaveCtrlBypass
  1403. *
  1404. * @brief Check if NV restore should be skipped during a leave reset.
  1405. *
  1406. * @param none
  1407. *
  1408. * @return uint8 - (TRUE bypass:FALSE do not bypass)
  1409. */
  1410. uint8 ZDApp_LeaveCtrlBypass( void )
  1411. {
  1412. uint8 bypass;
  1413. if ( ZDApp_LeaveCtrl & ZDAPP_LEAVE_CTRL_SET )
  1414. {
  1415. bypass = TRUE;
  1416. }
  1417. else
  1418. {
  1419. bypass = FALSE;
  1420. }
  1421. return bypass;
  1422. }
  1423. /*********************************************************************
  1424. * @fn ZDApp_LeaveCtrlStartup
  1425. *
  1426. * @brief Check for startup conditions during a leave reset.
  1427. *
  1428. * @param state - devState_t determined by leave control logic
  1429. * @param startDelay - startup delay
  1430. *
  1431. * @return none
  1432. */
  1433. void ZDApp_LeaveCtrlStartup( devStates_t* state, uint16* startDelay )
  1434. {
  1435. *startDelay = 0;
  1436. if ( ZDApp_LeaveCtrl & ZDAPP_LEAVE_CTRL_SET )
  1437. {
  1438. if ( ZDApp_LeaveCtrl & ZDAPP_LEAVE_CTRL_RA )
  1439. {
  1440. *startDelay = 5000;
  1441. }
  1442. else
  1443. {
  1444. *state = DEV_HOLD;
  1445. }
  1446. // Set leave control to initialized state
  1447. ZDApp_LeaveCtrl = ZDAPP_LEAVE_CTRL_INIT;
  1448. // Write initialized control
  1449. osal_nv_write( ZCD_NV_LEAVE_CTRL,
  1450. 0,
  1451. sizeof( uint8 ),
  1452. &ZDApp_LeaveCtrl);
  1453. }
  1454. }
  1455. /*********************************************************************
  1456. * @fn ZDApp_LeaveReset
  1457. *
  1458. * @brief Setup a device reset due to a leave indication/confirm.
  1459. *
  1460. * @param ra - reassociate flag
  1461. *
  1462. * @return none
  1463. */
  1464. void ZDApp_LeaveReset( uint8 ra )
  1465. {
  1466. ZDApp_LeaveCtrlSet( ra );
  1467. ZDApp_ResetTimerStart( 5000 );
  1468. }
  1469. /*********************************************************************
  1470. * @fn ZDApp_LeaveUpdate
  1471. *
  1472. * @brief Update local device data related to leaving device.
  1473. *
  1474. * @param nwkAddr - NWK address of leaving device
  1475. * @param extAddr - EXT address of leaving device
  1476. * @param removeChildren - remove children of leaving device
  1477. *
  1478. * @return none
  1479. */
  1480. void ZDApp_LeaveUpdate( uint16 nwkAddr, uint8* extAddr,
  1481. uint8 removeChildren )
  1482. {
  1483. /*
  1484. AddrMgrEntry_t entry;
  1485. */
  1486. (void)nwkAddr; // Intentionally unreferenced parameter
  1487. // Remove if child
  1488. if ( ZSTACK_ROUTER_BUILD )
  1489. {
  1490. NLME_RemoveChild( extAddr, removeChildren );
  1491. }
  1492. /*
  1493. // Set NWK address to invalid
  1494. entry.user = ADDRMGR_USER_DEFAULT;
  1495. entry.nwkAddr = INVALID_NODE_ADDR;
  1496. AddrMgrExtAddrSet( entry.extAddr, extAddr );
  1497. AddrMgrEntryUpdate( &entry );
  1498. // Check
  1499. if ( removeChildren == TRUE )
  1500. {
  1501. // Set index to INVALID_NODE_ADDR to start search
  1502. entry.index = INVALID_NODE_ADDR;
  1503. // Get first entry
  1504. AddrMgrEntryGetNext( &entry );
  1505. // Remove all descendents
  1506. while ( entry.index != INVALID_NODE_ADDR )
  1507. {
  1508. // Check NWK address allocation algorithm
  1509. if ( RTG_ANCESTOR( entry.nwkAddr, thisAddr ) != 0 )
  1510. {
  1511. // Set NWK address to invalid
  1512. entry.nwkAddr = INVALID_NODE_ADDR;
  1513. AddrMgrEntryUpdate( &entry );
  1514. }
  1515. // Get next entry
  1516. AddrMgrEntryGetNext( &entry );
  1517. }
  1518. }
  1519. */
  1520. }
  1521. /*********************************************************************
  1522. * CALLBACK FUNCTIONS
  1523. */
  1524. /*********************************************************************
  1525. * @fn ZDApp_SendEventMsg()
  1526. *
  1527. * @brief
  1528. *
  1529. * Sends a Network Join message
  1530. *
  1531. * @param cmd - command ID
  1532. * @param len - length (in bytes) of the buf field
  1533. * @param buf - buffer for the rest of the message.
  1534. *
  1535. * @return none
  1536. */
  1537. void ZDApp_SendEventMsg( uint8 cmd, uint8 len, uint8 *buf )
  1538. {
  1539. ZDApp_SendMsg( ZDAppTaskID, cmd, len, buf );
  1540. }
  1541. /*********************************************************************
  1542. * @fn ZDApp_SendMsg()
  1543. *
  1544. * @brief Sends a OSAL message
  1545. *
  1546. * @param taskID - Where to send the message
  1547. * @param cmd - command ID
  1548. * @param len - length (in bytes) of the buf field
  1549. * @param buf - buffer for the rest of the message.
  1550. *
  1551. * @return none
  1552. */
  1553. void ZDApp_SendMsg( uint8 taskID, uint8 cmd, uint8 len, uint8 *buf )
  1554. {
  1555. osal_event_hdr_t *msgPtr;
  1556. // Send the address to the task
  1557. msgPtr = (osal_event_hdr_t *)osal_msg_allocate( len );
  1558. if ( msgPtr )
  1559. {
  1560. if ( (len > 0) && (buf != NULL) )
  1561. osal_memcpy( msgPtr, buf, len );
  1562. msgPtr->event = cmd;
  1563. osal_msg_send( taskID, (uint8 *)msgPtr );
  1564. }
  1565. }
  1566. /*********************************************************************
  1567. * Call Back Functions from NWK - API
  1568. */
  1569. /*********************************************************************
  1570. * @fn ZDO_NetworkDiscoveryConfirmCB
  1571. *
  1572. * @brief This function returns a choice of PAN to join.
  1573. *
  1574. * @param ResultCount - Number of routers discovered
  1575. * @param NetworkList - Pointer to list of network descriptors
  1576. *
  1577. * @return ZStatus_t
  1578. */
  1579. #define STACK_PROFILE_MAX 2
  1580. ZStatus_t ZDO_NetworkDiscoveryConfirmCB( uint8 ResultCount,
  1581. networkDesc_t *NetworkList )
  1582. {
  1583. networkDesc_t *pNwkDesc;
  1584. ZDO_NetworkDiscoveryCfm_t msg;
  1585. uint8 i;
  1586. uint8 stackProfile;
  1587. uint8 stackProfilePro;
  1588. uint8 selected;
  1589. #if defined ( ZDO_MGMT_NWKDISC_RESPONSE )
  1590. if ( zdappMgmtNwkDiscReqInProgress )
  1591. {
  1592. zdappMgmtNwkDiscReqInProgress = false;
  1593. ZDO_FinishProcessingMgmtNwkDiscReq( ResultCount, NetworkList );
  1594. return ( ZSuccess );
  1595. }
  1596. #endif
  1597. // process discovery results
  1598. stackProfilePro = FALSE;
  1599. selected = FALSE;
  1600. for ( stackProfile = 0; stackProfile < STACK_PROFILE_MAX; stackProfile++ )
  1601. {
  1602. pNwkDesc = NetworkList;
  1603. for ( i = 0; i < ResultCount; i++, pNwkDesc = pNwkDesc->nextDesc )
  1604. {
  1605. if ( zgConfigPANID != 0xFFFF )
  1606. {
  1607. // PAN Id is preconfigured. check if it matches
  1608. if ( pNwkDesc->panId != zgConfigPANID )
  1609. continue;
  1610. }
  1611. if ( nwk_ExtPANIDValid( ZDO_UseExtendedPANID) == true )
  1612. {
  1613. // If the extended Pan ID is commissioned to a non zero value
  1614. // Only join the Pan that has match EPID
  1615. if ( osal_ExtAddrEqual( ZDO_UseExtendedPANID, pNwkDesc->extendedPANID) == false )
  1616. continue;
  1617. }
  1618. // check that network is allowing joining
  1619. if ( ZSTACK_ROUTER_BUILD )
  1620. {
  1621. if ( stackProfilePro == FALSE )
  1622. {
  1623. if ( !pNwkDesc->routerCapacity )
  1624. {
  1625. continue;
  1626. }
  1627. }
  1628. else
  1629. {
  1630. if ( !pNwkDesc->deviceCapacity )
  1631. {
  1632. continue;
  1633. }
  1634. }
  1635. }
  1636. else if ( ZSTACK_END_DEVICE_BUILD )
  1637. {
  1638. if ( !pNwkDesc->deviceCapacity )
  1639. {
  1640. continue;
  1641. }
  1642. }
  1643. // check version of zigbee protocol
  1644. if ( pNwkDesc->version != _NIB.nwkProtocolVersion )
  1645. continue;
  1646. // check version of stack profile
  1647. if ( pNwkDesc->stackProfile != zgStackProfile )
  1648. {
  1649. if ( ((zgStackProfile == HOME_CONTROLS) && (pNwkDesc->stackProfile == ZIGBEEPRO_PROFILE))
  1650. || ((zgStackProfile == ZIGBEEPRO_PROFILE) && (pNwkDesc->stackProfile == HOME_CONTROLS)) )
  1651. stackProfilePro = TRUE;
  1652. if ( stackProfile == 0 )
  1653. {
  1654. continue;
  1655. }
  1656. }
  1657. // check if beacon order is the right value..
  1658. // if ( pNwkDesc->beaconOrder < ZDO_CONFIG_MAX_BO )
  1659. // continue;
  1660. // choose this pan for joining
  1661. break;
  1662. }
  1663. if (i < ResultCount)
  1664. {
  1665. selected = TRUE;
  1666. break;
  1667. }
  1668. // break if selected or stack profile pro wasn't found
  1669. if ( (selected == TRUE) || (stackProfilePro == FALSE) )
  1670. {
  1671. break;
  1672. }
  1673. }
  1674. if ( i == ResultCount )
  1675. {
  1676. msg.hdr.status = ZDO_FAIL; // couldn't find appropriate PAN to join !
  1677. }
  1678. else
  1679. {
  1680. msg.hdr.status = ZDO_SUCCESS;
  1681. msg.panIdLSB = LO_UINT16( pNwkDesc->panId );
  1682. msg.panIdMSB = HI_UINT16( pNwkDesc->panId );
  1683. msg.logicalChannel = pNwkDesc->logicalChannel;
  1684. msg.version = pNwkDesc->version;
  1685. osal_cpyExtAddr( msg.extendedPANID, pNwkDesc->extendedPANID );
  1686. }
  1687. ZDApp_SendMsg( ZDAppTaskID, ZDO_NWK_DISC_CNF, sizeof(ZDO_NetworkDiscoveryCfm_t), (uint8 *)&msg );
  1688. return (ZSuccess);
  1689. } // ZDO_NetworkDiscoveryConfirmCB
  1690. /*********************************************************************
  1691. * @fn ZDO_NetworkFormationConfirmCB
  1692. *
  1693. * @brief This function reports the results of the request to
  1694. * initialize a coordinator in a network.
  1695. *
  1696. * @param Status - Result of NLME_NetworkFormationRequest()
  1697. *
  1698. * @return none
  1699. */
  1700. void ZDO_NetworkFormationConfirmCB( ZStatus_t Status )
  1701. {
  1702. nwkStatus = (byte)Status;
  1703. if ( Status == ZSUCCESS )
  1704. {
  1705. // LED on shows Coordinator started
  1706. HalLedSet ( HAL_LED_3, HAL_LED_MODE_ON );
  1707. // LED off forgets HOLD_AUTO_START
  1708. HalLedSet (HAL_LED_4, HAL_LED_MODE_OFF);
  1709. #if defined ( ZBIT )
  1710. SIM_SetColor(0xd0ffd0);
  1711. #endif
  1712. if ( devState == DEV_HOLD )
  1713. {
  1714. // Began with HOLD_AUTO_START
  1715. devState = DEV_COORD_STARTING;
  1716. }
  1717. }
  1718. #if defined(BLINK_LEDS)
  1719. else
  1720. HalLedSet ( HAL_LED_3, HAL_LED_MODE_FLASH ); // Flash LED to show failure
  1721. #endif
  1722. osal_set_event( ZDAppTaskID, ZDO_NETWORK_START );
  1723. }
  1724. /*********************************************************************
  1725. * @fn ZDO_StartRouterConfirmCB
  1726. *
  1727. * @brief This function reports the results of the request to
  1728. * start functioning as a router in a network.
  1729. *
  1730. * @param Status - Result of NLME_StartRouterRequest()
  1731. *
  1732. * @return none
  1733. */
  1734. void ZDO_StartRouterConfirmCB( ZStatus_t Status )
  1735. {
  1736. nwkStatus = (byte)Status;
  1737. if ( Status == ZSUCCESS )
  1738. {
  1739. // LED on shows Router started
  1740. HalLedSet ( HAL_LED_3, HAL_LED_MODE_ON );
  1741. // LED off forgets HOLD_AUTO_START
  1742. HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF);
  1743. if ( devState == DEV_HOLD )
  1744. {
  1745. // Began with HOLD_AUTO_START
  1746. devState = DEV_END_DEVICE;
  1747. }
  1748. }
  1749. #if defined(BLINK_LEDS)
  1750. else
  1751. HalLedSet( HAL_LED_3, HAL_LED_MODE_FLASH ); // Flash LED to show failure
  1752. #endif
  1753. osal_set_event( ZDAppTaskID, ZDO_ROUTER_START );
  1754. }
  1755. /*********************************************************************
  1756. * @fn ZDO_JoinConfirmCB
  1757. *
  1758. * @brief This function allows the next hight layer to be notified
  1759. * of the results of its request to join itself or another
  1760. * device to a network.
  1761. *
  1762. * @param Status - Result of NLME_JoinRequest()
  1763. *
  1764. * @return none
  1765. */
  1766. void ZDO_JoinConfirmCB( uint16 PanId, ZStatus_t Status )
  1767. {
  1768. (void)PanId; // remove if this parameter is used.
  1769. nwkStatus = (byte)Status;
  1770. if ( Status == ZSUCCESS )
  1771. {
  1772. // LED on shows device joined
  1773. HalLedSet ( HAL_LED_3, HAL_LED_MODE_ON );
  1774. // LED off forgets HOLD_AUTO_START
  1775. HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF);
  1776. if ( (devState == DEV_HOLD) )
  1777. {
  1778. // Began with HOLD_AUTO_START
  1779. devState = DEV_NWK_JOINING;
  1780. }
  1781. if ( !ZG_SECURE_ENABLED )
  1782. {
  1783. // Notify to save info into NV
  1784. ZDApp_NVUpdate();
  1785. }
  1786. }
  1787. #if defined(BLINK_LEDS)
  1788. else
  1789. HalLedSet ( HAL_LED_3, HAL_LED_MODE_FLASH ); // Flash LED to show failure
  1790. #endif
  1791. // Notify ZDApp
  1792. ZDApp_SendMsg( ZDAppTaskID, ZDO_NWK_JOIN_IND, sizeof(osal_event_hdr_t), (byte*)NULL );
  1793. }
  1794. /*********************************************************************
  1795. * @fn ZDO_AddrChangeIndicationCB
  1796. *
  1797. * @brief This function notifies the application that this
  1798. * device's address has changed. Could happen in
  1799. * a network with stochastic addressing (PRO).
  1800. *
  1801. * @param newAddr - the new address
  1802. *
  1803. * @return none
  1804. */
  1805. void ZDO_AddrChangeIndicationCB( uint16 newAddr )
  1806. {
  1807. (void)newAddr; // remove if this parameter is used
  1808. // Notify to save info into NV
  1809. ZDApp_NVUpdate();
  1810. // Notify the applications
  1811. osal_set_event( ZDAppTaskID, ZDO_STATE_CHANGE_EVT );
  1812. // Send out a device announce
  1813. ZDApp_AnnounceNewAddress();
  1814. }
  1815. /*********************************************************************
  1816. * @fn ZDO_JoinIndicationCB
  1817. *
  1818. * @brief This function allows the next higher layer of a
  1819. * coordinator to be notified of a remote join request.
  1820. *
  1821. * @param ShortAddress - 16-bit address
  1822. * @param ExtendedAddress - IEEE (64-bit) address
  1823. * @param CapabilityInformation - Association Capability Information
  1824. * @param type - of joining -
  1825. * NWK_ASSOC_JOIN
  1826. * NWK_ASSOC_REJOIN_UNSECURE
  1827. * NWK_ASSOC_REJOIN_SECURE
  1828. *
  1829. * @return ZStatus_t
  1830. */
  1831. ZStatus_t ZDO_JoinIndicationCB( uint16 ShortAddress, uint8 *ExtendedAddress,
  1832. uint8 CapabilityInformation, uint8 type )
  1833. {
  1834. (void)ShortAddress;
  1835. (void)ExtendedAddress;
  1836. (void)CapabilityInformation;
  1837. // Notify to save info into NV
  1838. ZDApp_NVUpdate();
  1839. // send notification to TC of new device..
  1840. if ( ZG_SECURE_ENABLED )
  1841. {
  1842. if ( type == NWK_ASSOC_JOIN || type == NWK_ASSOC_REJOIN_UNSECURE )
  1843. {
  1844. osal_start_timerEx( ZDAppTaskID, ZDO_NEW_DEVICE, 600 );
  1845. }
  1846. }
  1847. return ( ZSuccess );
  1848. }
  1849. /*********************************************************************
  1850. * @fn ZDO_ConcentratorIndicationCB
  1851. *
  1852. * @brief This function allows the next higher layer of a
  1853. * device to be notified of existence of the concentrator.
  1854. *
  1855. * @param nwkAddr - 16-bit NWK address of the concentrator
  1856. *
  1857. * @return void
  1858. */
  1859. void ZDO_ConcentratorIndicationCB( uint16 nwkAddr )
  1860. {
  1861. // Write your code here to do something with the concentrator's
  1862. // address.
  1863. (void)nwkAddr;
  1864. }
  1865. /*********************************************************************
  1866. * @fn ZDO_LeaveCnf
  1867. *
  1868. * @brief This function allows the next higher layer to be
  1869. * notified of the results of its request for this or
  1870. * a child device to leave the network.
  1871. *
  1872. * @param cnf - NLME_LeaveCnf_t
  1873. *
  1874. * @return none
  1875. */
  1876. void ZDO_LeaveCnf( NLME_LeaveCnf_t* cnf )
  1877. {
  1878. // Check for this device
  1879. if ( osal_ExtAddrEqual( cnf->extAddr,
  1880. NLME_GetExtAddr() ) == TRUE )
  1881. {
  1882. // Prepare to leave with reset
  1883. ZDApp_LeaveReset( cnf->rejoin );
  1884. }
  1885. else if ( ZSTACK_ROUTER_BUILD )
  1886. {
  1887. // Remove device address(optionally descendents) from data
  1888. ZDApp_LeaveUpdate( cnf->dstAddr,
  1889. cnf->extAddr,
  1890. cnf->removeChildren );
  1891. }
  1892. }
  1893. /*********************************************************************
  1894. * @fn ZDO_LeaveInd
  1895. *
  1896. * @brief This function allows the next higher layer of a
  1897. * device to be notified of a remote leave request or
  1898. * indication.
  1899. *
  1900. * @param ind - NLME_LeaveInd_t
  1901. *
  1902. * @return none
  1903. */
  1904. void ZDO_LeaveInd( NLME_LeaveInd_t* ind )
  1905. {
  1906. uint8 leave;
  1907. // Parent is requesting the leave - NWK layer filters out illegal
  1908. // requests
  1909. if ( ind->request == TRUE )
  1910. {
  1911. // Notify network of leave
  1912. if ( ZSTACK_ROUTER_BUILD )
  1913. {
  1914. NLME_LeaveRsp_t rsp;
  1915. rsp.rejoin = ind->rejoin;
  1916. rsp.removeChildren = ind->removeChildren;
  1917. NLME_LeaveRsp( &rsp );
  1918. }
  1919. // Prepare to leave with reset
  1920. ZDApp_LeaveReset( ind->rejoin );
  1921. }
  1922. else
  1923. {
  1924. leave = FALSE;
  1925. // Check if this device needs to leave as a child or descendent
  1926. if ( ind->srcAddr == NLME_GetCoordShortAddr() )
  1927. {
  1928. if ( ( ind->removeChildren == TRUE ) ||
  1929. ( ZDO_Config_Node_Descriptor.LogicalType ==
  1930. NODETYPE_DEVICE ) )
  1931. {
  1932. leave = TRUE;
  1933. }
  1934. }
  1935. else if ( ind->removeChildren == TRUE )
  1936. {
  1937. // Check NWK address allocation algorithm
  1938. //leave = RTG_ANCESTOR(nwkAddr,thisAddr);
  1939. }
  1940. if ( leave == TRUE )
  1941. {
  1942. // Prepare to leave with reset
  1943. ZDApp_LeaveReset( ind->rejoin );
  1944. }
  1945. else
  1946. {
  1947. // Remove device address(optionally descendents) from data
  1948. ZDApp_LeaveUpdate( ind->srcAddr,
  1949. ind->extAddr,
  1950. ind->removeChildren );
  1951. }
  1952. }
  1953. }
  1954. /*********************************************************************
  1955. * @fn ZDO_SyncIndicationCB
  1956. *
  1957. * @brief This function allows the next higher layer of a
  1958. * coordinator to be notified of a loss of synchronization
  1959. * with the parent/child device.
  1960. *
  1961. * @param type: 0 - child; 1 - parent
  1962. *
  1963. *
  1964. * @return none
  1965. */
  1966. void ZDO_SyncIndicationCB( uint8 type, uint16 shortAddr )
  1967. {
  1968. (void)shortAddr; // Remove this line if this parameter is used.
  1969. if ( ZSTACK_END_DEVICE_BUILD
  1970. || (ZSTACK_ROUTER_BUILD && ((_NIB.CapabilityInfo & ZMAC_ASSOC_CAPINFO_FFD_TYPE) == 0)))
  1971. {
  1972. if ( type == 1 )
  1973. {
  1974. // We lost contact with our parent. Clear the neighbor Table.
  1975. nwkNeighborInitTable();
  1976. // Start the rejoin process.
  1977. ZDApp_SendMsg( ZDAppTaskID, ZDO_NWK_JOIN_REQ, sizeof(osal_event_hdr_t), NULL );
  1978. }
  1979. }
  1980. }
  1981. /*********************************************************************
  1982. * @fn ZDO_ManytoOneFailureIndicationCB
  1983. *
  1984. * @brief This function allows the next higher layer of a
  1985. * concentrator to be notified of a many-to-one route
  1986. * failure.
  1987. *
  1988. * @param none
  1989. *
  1990. *
  1991. * @return none
  1992. */
  1993. void ZDO_ManytoOneFailureIndicationCB()
  1994. {
  1995. // By default, the concentrator automatically redo many-to-one route
  1996. // discovery to update all many-to-one routes in the network
  1997. // If you want anything processing other than the default,
  1998. // please replace the following code.
  1999. if( zgConcentratorRC == true ) // concentrator with route cache
  2000. {
  2001. NLME_RouteDiscoveryRequest( 0x00, MTO_ROUTE, zgConcentratorRadius);
  2002. }
  2003. else
  2004. { // Concentrator with no route cache
  2005. NLME_RouteDiscoveryRequest( 0x00, MTO_ROUTE | NO_ROUTE_CACHE, zgConcentratorRadius);
  2006. }
  2007. }
  2008. /*********************************************************************
  2009. * @fn ZDO_PollConfirmCB
  2010. *
  2011. * @brief This function allows the next higher layer to be
  2012. * notified of a Poll Confirm.
  2013. *
  2014. * @param none
  2015. *
  2016. * @return none
  2017. */
  2018. void ZDO_PollConfirmCB( uint8 status )
  2019. {
  2020. (void)status; // Remove this line if this parameter is used.
  2021. return;
  2022. }
  2023. /******************************************************************************
  2024. * @fn ZDApp_NwkWriteNVRequest (stubs AddrMgrWriteNVRequest)
  2025. *
  2026. * @brief Stub routine implemented by NHLE. NHLE should call
  2027. * <AddrMgrWriteNV> when appropriate.
  2028. *
  2029. * @param none
  2030. *
  2031. * @return none
  2032. */
  2033. void ZDApp_NwkWriteNVRequest( void )
  2034. {
  2035. #if defined ( NV_RESTORE )
  2036. if ( !osal_get_timeoutEx( ZDAppTaskID, ZDO_NWK_UPDATE_NV ) )
  2037. {
  2038. // Trigger to save info into NV
  2039. ZDApp_NVUpdate();
  2040. }
  2041. #endif
  2042. }
  2043. /*********************************************************************
  2044. * Call Back Functions from Security - API
  2045. */
  2046. /*********************************************************************
  2047. * @fn ZDO_UpdateDeviceIndication
  2048. *
  2049. * @brief This function notifies the "Trust Center" of a
  2050. * network when a device joins or leaves the network.
  2051. *
  2052. * @param extAddr - pointer to 64 bit address of new device
  2053. * @param status - 0 if a new device joined securely
  2054. * - 1 if a new device joined un-securely
  2055. * - 2 if a device left the network
  2056. *
  2057. * @return true if newly joined device should be allowed to
  2058. * remain on network
  2059. */
  2060. ZStatus_t ZDO_UpdateDeviceIndication( uint8 *extAddr, uint8 status )
  2061. {
  2062. // can implement a network access policy based on the
  2063. // IEEE address of newly joining devices...
  2064. (void)extAddr;
  2065. (void)status;
  2066. return ZSuccess;
  2067. }
  2068. /*********************************************************************
  2069. * @fn ZDApp_InMsgCB
  2070. *
  2071. * @brief This function is called to pass up any message that is
  2072. * not yet supported. This allows for the developer to
  2073. * support features themselves..
  2074. *
  2075. * @return none
  2076. */
  2077. void ZDApp_InMsgCB( zdoIncomingMsg_t *inMsg )
  2078. {
  2079. if ( inMsg->clusterID & ZDO_RESPONSE_BIT )
  2080. {
  2081. // Handle the response message
  2082. }
  2083. else
  2084. {
  2085. // Handle the request message by sending a generic "not supported".
  2086. // Device Announce doesn't have a response.
  2087. if ( !(inMsg->wasBroadcast) && inMsg->clusterID != Device_annce )
  2088. {
  2089. ZDP_GenericRsp( inMsg->TransSeq, &(inMsg->srcAddr), ZDP_NOT_SUPPORTED, 0,
  2090. (uint16)(inMsg->clusterID | ZDO_RESPONSE_BIT), inMsg->SecurityUse );
  2091. }
  2092. }
  2093. }
  2094. /*********************************************************************
  2095. * @fn ZDApp_ChangeMatchDescRespPermission()
  2096. *
  2097. * @brief Changes the Match Descriptor Response permission.
  2098. *
  2099. * @param endpoint - endpoint to allow responses
  2100. * @param action - true to allow responses, false to not
  2101. *
  2102. * @return none
  2103. */
  2104. void ZDApp_ChangeMatchDescRespPermission( uint8 endpoint, uint8 action )
  2105. {
  2106. // Store the action
  2107. afSetMatch( endpoint, action );
  2108. }
  2109. /*********************************************************************
  2110. * @fn ZDApp_NetworkInit()
  2111. *
  2112. * @brief Used to start the network joining process
  2113. *
  2114. * @param delay - mSec delay to wait before starting
  2115. *
  2116. * @return none
  2117. */
  2118. void ZDApp_NetworkInit( uint16 delay )
  2119. {
  2120. if ( delay )
  2121. {
  2122. // Wait awhile before starting the device
  2123. osal_start_timerEx( ZDAppTaskID, ZDO_NETWORK_INIT, delay );
  2124. }
  2125. else
  2126. {
  2127. osal_set_event( ZDAppTaskID, ZDO_NETWORK_INIT );
  2128. }
  2129. }
  2130. /*********************************************************************
  2131. * @fn ZDApp_NwkStateUpdateCB()
  2132. *
  2133. * @brief This function notifies that this device's network
  2134. * state info has been changed.
  2135. *
  2136. * @param none
  2137. *
  2138. * @return none
  2139. */
  2140. void ZDApp_NwkStateUpdateCB( void )
  2141. {
  2142. // Notify to save info into NV
  2143. if ( !osal_get_timeoutEx( ZDAppTaskID, ZDO_NWK_UPDATE_NV ) )
  2144. {
  2145. // Trigger to save info into NV
  2146. ZDApp_NVUpdate();
  2147. }
  2148. }
  2149. /*********************************************************************
  2150. * @fn ZDApp_NodeProfileSync()
  2151. *
  2152. * @brief Sync node with stack profile.
  2153. *
  2154. * @param cfm - ZDO_NetworkDiscoveryCfm_t
  2155. *
  2156. * @return none
  2157. */
  2158. void ZDApp_NodeProfileSync( ZDO_NetworkDiscoveryCfm_t* cfm )
  2159. {
  2160. networkDesc_t* desc;
  2161. uint16 panID;
  2162. if ( ZDO_Config_Node_Descriptor.CapabilityFlags & CAPINFO_DEVICETYPE_FFD )
  2163. {
  2164. panID = BUILD_UINT16( cfm->panIdLSB, cfm->panIdMSB );
  2165. desc = nwk_getNetworkDesc( cfm->extendedPANID, panID, cfm->logicalChannel );
  2166. if (desc != NULL)
  2167. {
  2168. if ( desc->stackProfile != zgStackProfile )
  2169. {
  2170. ZDO_Config_Node_Descriptor.LogicalType = NODETYPE_DEVICE;
  2171. ZDO_Config_Node_Descriptor.CapabilityFlags = CAPINFO_DEVICETYPE_RFD | CAPINFO_POWER_AC | CAPINFO_RCVR_ON_IDLE;
  2172. NLME_SetBroadcastFilter( ZDO_Config_Node_Descriptor.CapabilityFlags );
  2173. }
  2174. }
  2175. }
  2176. }
  2177. /*********************************************************************
  2178. * @fn ZDApp_StartJoiningCycle()
  2179. *
  2180. * @brief Starts the joining cycle of a device. This will only
  2181. * continue an already started (or stopped) joining cycle.
  2182. *
  2183. * @param none
  2184. *
  2185. * @return TRUE if joining stopped, FALSE if joining or rejoining
  2186. */
  2187. uint8 ZDApp_StartJoiningCycle( void )
  2188. {
  2189. if ( devState == DEV_INIT || devState == DEV_NWK_DISC )
  2190. {
  2191. continueJoining = TRUE;
  2192. ZDApp_NetworkInit( 0 );
  2193. return ( TRUE );
  2194. }
  2195. else
  2196. return ( FALSE );
  2197. }
  2198. /*********************************************************************
  2199. * @fn ZDApp_StopJoiningCycle()
  2200. *
  2201. * @brief Stops the joining or rejoining process of a device.
  2202. *
  2203. * @param none
  2204. *
  2205. * @return TRUE if joining stopped, FALSE if joining or rejoining
  2206. */
  2207. uint8 ZDApp_StopJoiningCycle( void )
  2208. {
  2209. if ( devState == DEV_INIT || devState == DEV_NWK_DISC )
  2210. {
  2211. continueJoining = FALSE;
  2212. return ( TRUE );
  2213. }
  2214. else
  2215. return ( FALSE );
  2216. }
  2217. /*********************************************************************
  2218. * @fn ZDApp_AnnounceNewAddress()
  2219. *
  2220. * @brief Send Device Announce and hold all transmissions for
  2221. * new address timeout.
  2222. *
  2223. * @param none
  2224. *
  2225. * @return none
  2226. */
  2227. void ZDApp_AnnounceNewAddress( void )
  2228. {
  2229. #if defined ( ZIGBEE_NWK_UNIQUE_ADDR_CHECK )
  2230. // Turn off data request hold
  2231. APSME_HoldDataRequests( 0 );
  2232. #endif
  2233. ZDP_DeviceAnnce( NLME_GetShortAddr(), NLME_GetExtAddr(),
  2234. ZDO_Config_Node_Descriptor.CapabilityFlags, 0 );
  2235. #if defined ( ZIGBEE_NWK_UNIQUE_ADDR_CHECK )
  2236. // Setup the timeout
  2237. APSME_HoldDataRequests( ZDAPP_HOLD_DATA_REQUESTS_TIMEOUT );
  2238. #endif
  2239. }
  2240. /*********************************************************************
  2241. * @fn ZDApp_NVUpdate
  2242. *
  2243. * @brief Set the NV Update Timer.
  2244. *
  2245. * @param none
  2246. *
  2247. * @return none
  2248. */
  2249. void ZDApp_NVUpdate( void )
  2250. {
  2251. #if defined ( NV_RESTORE )
  2252. osal_start_timerEx( ZDAppTaskID, ZDO_NWK_UPDATE_NV, ZDAPP_UPDATE_NWK_NV_TIME );
  2253. #endif
  2254. }
  2255. /*********************************************************************
  2256. * @fn ZDApp_CoordStartPANIDConflictCB()
  2257. *
  2258. * @brief Returns a PAN ID for the network layer to use during
  2259. * a coordinator start and there is another network with
  2260. * the intended PANID.
  2261. *
  2262. * @param panid - the intended PAN ID
  2263. *
  2264. * @return PANID to try
  2265. */
  2266. uint16 ZDApp_CoordStartPANIDConflictCB( uint16 panid )
  2267. {
  2268. return ( panid + 1 );
  2269. }
  2270. /*********************************************************************
  2271. *********************************************************************/