/********************************************************************* * @fn zgInit * * @brief * * Initialize the Z-Stack Globals. If an item doesn't exist in * NV memory, write the system default into NV memory. But if * it exists, set the item to the value stored in NV memory. * * NOTE: The Startup Options (ZCD_NV_STARTUP_OPTION) indicate * that the Config state items (zgItemTable) need to be * set to defaults (ZCD_STARTOPT_DEFAULT_CONFIG_STATE). The * * @param none * * @return ZSUCCESS if successful, NV_ITEM_UNINIT if item did not * exist in NV, NV_OPER_FAILED if failure. */ uint8 zgInit( void ) { uint8 setDefault = FALSE; // Do we want to default the Config state values if ( zgReadStartupOptions() & ZCD_STARTOPT_DEFAULT_CONFIG_STATE ) { setDefault = TRUE; } #if defined ( FEATURE_SYSTEM_STATS ) // This sections tracks the number of resets uint16 bootCnt = 0; // Update the Boot Counter if ( osal_nv_item_init( ZCD_NV_BOOTCOUNTER, sizeof(bootCnt), &bootCnt ) == ZSUCCESS ) { // Get the old value from NV memory osal_nv_read( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt ); } // Increment the Boot Counter and store it into NV memory if ( setDefault ) { bootCnt = 0; } else { bootCnt++; } osal_nv_write( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt ); #endif // FEATURE_SYSTEM_STATS // Initialize the Extended PAN ID as my own extended address ZMacGetReq( ZMacExtAddr, zgExtendedPANID ); // Initialize the items table zgInitItems( setDefault ); #ifndef NONWK if ( ZG_SECURE_ENABLED ) { // Initialize the Pre-Configured Key to the default key zgPreconfigKeyInit( setDefault ); // Initialize NV items for all Keys: NWK, APS, TCLK and Master ZDSecMgrInitNVKeyTables( setDefault ); } #endif // NONWK // Clear the Config State default if ( setDefault ) { zgWriteStartupOptions( ZG_STARTUP_CLEAR, ZCD_STARTOPT_DEFAULT_CONFIG_STATE ); } return ( ZSUCCESS ); }
/********************************************************************* * @fn zclSampleTemperatureSensor_BasicResetCB * * @brief Callback from the ZCL General Cluster Library * to set all the Basic Cluster attributes to default values. * * @param none * * @return none */ static void zclSampleTemperatureSensor_BasicResetCB( void ) { // Put device back to factory default settings zgWriteStartupOptions( ZG_STARTUP_SET, 3 ); // bit set both default configuration and default network // restart device MT_SysCommandProcessing( aProcessCmd ); }
/********************************************************************* * @fn zgInit * * @brief * * Initialize the Z-Stack Globals. If an item doesn't exist in * NV memory, write the system default into NV memory. But if * it exists, set the item to the value stored in NV memory. * * NOTE: The Startup Options (ZCD_NV_STARTUP_OPTION) indicate * that the Config state items (zgItemTable) need to be * set to defaults (ZCD_STARTOPT_DEFAULT_CONFIG_STATE). The * * * @param none * * @return ZSUCCESS if successful, NV_ITEM_UNINIT if item did not * exist in NV, NV_OPER_FAILED if failure. */ uint8 zgInit( void ) { uint8 i = 0; uint8 setDefault = FALSE; // Do we want to default the Config state values if ( zgReadStartupOptions() & ZCD_STARTOPT_DEFAULT_CONFIG_STATE ) { setDefault = TRUE; } #if 0 // Enable this section if you need to track the number of resets // This section is normally disabled to minimize "wear" on NV memory uint16 bootCnt = 0; // Update the Boot Counter if ( osal_nv_item_init( ZCD_NV_BOOTCOUNTER, sizeof(bootCnt), &bootCnt ) == ZSUCCESS ) { // Get the old value from NV memory osal_nv_read( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt ); } // Increment the Boot Counter and store it into NV memory if ( setDefault ) bootCnt = 0; else bootCnt++; osal_nv_write( ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt ); #endif // Initialize the Extended PAN ID as my own extended address ZMacGetReq( ZMacExtAddr, zgExtendedPANID ); #ifndef NONWK // Initialize the Pre-Configured Key to the default key osal_memcpy( zgPreConfigKey, defaultKey, SEC_KEY_LEN ); // Do NOT Change!!! #endif // NONWK while ( zgItemTable[i].id != 0x00 ) { // Initialize the item zgItemInit( zgItemTable[i].id, zgItemTable[i].len, zgItemTable[i].buf, setDefault ); // Move on to the next item i++; } // Clear the Config State default if ( setDefault ) { zgWriteStartupOptions( ZG_STARTUP_CLEAR, ZCD_STARTOPT_DEFAULT_CONFIG_STATE ); } return ( ZSUCCESS ); }
void set_coordi(void) { uint8 logicalType; osal_nv_item_init( ZCD_NV_LOGICAL_TYPE, sizeof(logicalType), &logicalType ); logicalType = ZG_DEVICETYPE_COORDINATOR; if( osal_nv_write( ZCD_NV_LOGICAL_TYPE, 0 ,sizeof(logicalType), &logicalType) != ZSUCCESS) { uprint("set device to coordi failed"); }else { zgWriteStartupOptions (ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE); uprint("set device to coordi,restart it"); } return; }
/********************************************************************* * @fn restore_factory_setting * * @brief Restore the device to factory settings. * * @param none * * @return none * *********************************************************************/ void restore_factory_setting( void) { uint8 startOptions; NLME_InitNV(); NLME_SetDefaultNV(); zgWriteStartupOptions( ZG_STARTUP_SET,ZCD_STARTOPT_DEFAULT_NETWORK_STATE ); startOptions = ZCD_STARTOPT_CLEAR_STATE | ZCD_STARTOPT_CLEAR_CONFIG; osal_nv_write(ZCD_NV_STARTUP_OPTION, 0, sizeof(uint8),&startOptions); SystemReset(); }
/********************************************************************* * @fn zclCCServer_ResetToZBADefault * * @brief Reset the local device to ZBA Default. * * Note: This function will decommission the device to return * to the ZBA Default Settings with the default ZBA Key * Material and ZBA Extended PAN ID. * * @param none * * @return none */ void zclCCServer_ResetToZBADefault( void ) { // Set the NV startup option to force a "new" join. zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE ); // Perform a Leave on our old network if ( zclCCServer_SendLeaveReq() == ZSuccess ) { // Wait for Leave confirmation before resetting leaveInitiated = TO_JOIN_COMMISSIONING_NWK; } else { // Notify our task to reset the device osal_set_event( zclCCServer_TaskID, CCSERVER_RESET_TIMER_EVT ); } }
/********************************************************************* * @fn zclRouterVersion1_BasicResetCB * * @brief Callback from the ZCL General Cluster Library * to set all the Basic Cluster attributes to default values. * * @param none * * @return none */ static void zclRouterVersion1_BasicResetCB( void ) { NLME_LeaveReq_t leaveReq; // Set every field to 0 osal_memset( &leaveReq, 0, sizeof( NLME_LeaveReq_t ) ); // This will enable the device to rejoin the network after reset. leaveReq.rejoin = TRUE; // Set the NV startup option to force a "new" join. zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE ); // Leave the network, and reset afterwards if ( NLME_LeaveReq( &leaveReq ) != ZSuccess ) { // Couldn't send out leave; prepare to reset anyway ZDApp_LeaveReset( FALSE ); } }
void set_router(void) { uint8 logicalType; osal_nv_item_init( ZCD_NV_LOGICAL_TYPE, sizeof(logicalType), &logicalType ); logicalType = ZG_DEVICETYPE_ROUTER; if( osal_nv_write( ZCD_NV_LOGICAL_TYPE, 0 ,sizeof(logicalType), &logicalType) != ZSUCCESS) { uprint("set device to router failed"); }else { zgWriteStartupOptions (ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE); uprint("set device to router,restart it"); } // zgWriteStartupOptions (ZG_STARTUP_SET, 0x02); // zgInit(); // ZDOInitDevice( 0 ); // SystemReset(); return; }
/********************************************************************* * @fn zclHomelink_BasicResetCB * * @brief Callback from the ZCL General Cluster Library * to set all the Basic Cluster attributes to default values. * * @param none * * @return none */ static void zclHomelink_BasicResetCB( void ) { // Put device back to factory default settings zgWriteStartupOptions( ZG_STARTUP_SET, 3 ); // bit set both default configuration and default network }
void Serial_callBack(uint8 port, uint8 event) { char theMessageData[] = "Hello"; zAddrType_t dstAddr; zAddrType_t ZAddr; afAddrType_t myaddr; // use for p2p char pbuf[3]; char pbuf1[3]; uint16 cmd; uint8 buff[128]; uint8 readBytes = 0; uint16 short_ddr; uint16 panid; uint8 *ieeeAddr; uint16 *p1; byte cnt = 0; uint8 yy1; uint8 yy2; uint8 i; byte nr; uint16 devlist[ NWK_MAX_DEVICES + 1]; associated_devices_t *adp; // delete devices //short_ddr = GenericApp_DstAddr.addr.shortAddr; uint8 startOptions; uint8 logicalType; logicalType = (uint8)ZDO_Config_Node_Descriptor.LogicalType; readBytes = HalUARTRead(SER_PORT, buff, 127); if (readBytes > 0) { //HalUARTWrite( SER_PORT, "DataRead: ",10); // HalUARTWrite( SER_PORT, buff, readBytes); if(readBytes == 4) { if(buff[0] == 'p' && buff[1] == 'i' && buff[2]=='n' && buff[3] == 'g') { UART_Send_String( "pong", 4 ); } } if( readBytes == 1) { yy1 = 1; if(buff[0]== 's') { /// short address short_ddr = _NIB.nwkDevAddress; UART_Send_String( (uint8*)&short_ddr, 2); } if(buff[0] == 'p') { /// pan id panid = _NIB.nwkPanId; UART_Send_String( (uint8*)&panid, 2); } if(buff[0] == 'c')/// channel { yy2 = _NIB.nwkLogicalChannel; UART_Send_String( (uint8*)&yy2, 1); } if(buff[0] =='m') // mac address { ieeeAddr = NLME_GetExtAddr(); UART_Send_String( ieeeAddr, 8); } if( buff[0] ==0xc0) // coordinator { set_coordi(); return; } if( buff[0] ==0xe0) // router { set_router(); return; } if(buff[0] == 0xCA) { // 使命的招唤 // read self AssociatedDevList for(i=0;i< NWK_MAX_DEVICES; i++) { nr = AssociatedDevList[ i ].nodeRelation; if(nr > 0 && nr < 5) //CHILD_RFD CHILD_RFD_RX_IDLE CHILD_FFD CHILD_FFD_RX_IDLE //if( nr != 0XFF) { // myaddr.addrMode = (afAddrMode_t)Addr16Bit; // myaddr.endPoint = GENERICAPP_ENDPOINT; // if( AssociatedDevList[ i ].shortAddr != 0x0000) // { //if( AssocIsChild( AssociatedDevList[ i ].shortAddr ) != 1 || AssociatedDevList[ i ].age > NWK_ROUTE_AGE_LIMIT) //if( AssocIsChild( AssociatedDevList[ i ].shortAddr ) == 1 && AssociatedDevList[ i ].age > NWK_ROUTE_AGE_LIMIT ) // { // myaddr.addr.shortAddr = AssociatedDevList[ i ].shortAddr; /* if ( AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID,(byte)osal_strlen( theMessageData ) + 1, (byte *)&theMessageData, &GenericApp_TransID, AF_ACK_REQUEST, AF_DEFAULT_RADIUS ) != afStatus_SUCCESS ) { uprint("delete asso"); */ // delete_asso( AssociatedDevList[ i ]. addrIdx); // } // else // { devlist[yy1] = AssociatedDevList[ i ].shortAddr; yy1++; // } // } }//else {break;} } devlist[0] = BUILD_UINT16(0xce, AssocCount(1, 4) ); UART_Send_String( (uint8*)&devlist[0], yy1*2); //p1 = AssocMakeList( &cnt ); //UART_Send_String( (uint8*)p1, AssocCount(1, 4)*2); //osal_mem_free(p1); return; } } #if defined( ZDO_COORDINATOR ) // only coordinator can have this function if(readBytes == 3) { if( buff[0] == 0xCA || buff[0] == 0x6d) { // CA xx xx ,send CA to a node ,with it's short address ///uprint("it's CA "); short_ddr = BUILD_UINT16( buff[1], buff[2] ); myaddr.addrMode = (afAddrMode_t)Addr16Bit; myaddr.endPoint = GENERICAPP_ENDPOINT; myaddr.addr.shortAddr = short_ddr; if ( AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID, 1, (byte *)&buff, &GenericApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) != afStatus_SUCCESS ) { AF_DataRequest( &myaddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID, 1, (byte *)&buff, &GenericApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ); // send twice only, if it is still not ok, f**k it } return; } } #endif if( readBytes >= 2) { if( buff[0] == 'e' && buff[1] == '#') { uprint("EndDevice match"); HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF ); dstAddr.addrMode = Addr16Bit; dstAddr.addr.shortAddr = 0x0000; // Coordinator ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), GenericApp_epDesc.endPoint, GENERICAPP_PROFID, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, FALSE ); } if( buff[0] == 'r' && buff[1] == '#') { uprint("Router Device match"); HalLedSet ( HAL_LED_1, HAL_LED_MODE_FLASH ); dstAddr.addrMode = AddrBroadcast; dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR; ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR, GENERICAPP_PROFID, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, FALSE ); } if(readBytes == 6) { if(buff[0] == 'p' && buff[1]==':') // pan id { strncpy(pbuf, &buff[2],2); pbuf[2] = '\0'; strncpy(pbuf1, &buff[4],2); pbuf1[2] = '\0'; set_panid( BUILD_UINT16( strtol(pbuf1,NULL,16),strtol(pbuf,NULL,16) )); if(_NIB.nwkPanId == 0xffff) { zgWriteStartupOptions (ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE); SystemReset(); } //SystemResetSoft(); } if(buff[0] == 's' && buff[1]==':') // short address { /* strncpy(pbuf, &buff[2],2); pbuf[2] = '\0'; strncpy(pbuf1, &buff[4],2); pbuf1[2] = '\0'; _NIB.nwkDevAddress = BUILD_UINT16( strtol(pbuf1,NULL,16),strtol(pbuf,NULL,16)); */ } } cmd = BUILD_UINT16(buff[ 1 + 1], buff[1]); if( ( buff[ 0 ] == CPT_SOP) && (cmd == SYS_PING_REQUEST) ) { sysPingReqRcvd(); return; } if( readBytes == 2) { if( buff[0] == 0xcc ) { if( buff[1] > 0x0a && buff[1] < 0x1b ) { _NIB.nwkLogicalChannel = buff[1]; NLME_UpdateNV(0x01); ZMacSetReq( ZMacChannel, &buff[1]); osal_nv_item_init( ZCD_NV_CHANLIST, sizeof(zgDefaultChannelList), &zgDefaultChannelList); if( buff[1] == 0x0b) { zgDefaultChannelList = 0x00000800; } if (buff[1] == 0x0c ) { zgDefaultChannelList = 0x00001000; } if (buff[1] == 0x0d ) { zgDefaultChannelList = 0x00002000; } if (buff[1] == 0x0e ) { zgDefaultChannelList = 0x00004000; } if (buff[1] == 0x0f ) { zgDefaultChannelList = 0x00008000; } if (buff[1] == 0x10 ) { zgDefaultChannelList = 0x00010000; } if (buff[1] == 0x11 ) { zgDefaultChannelList = 0x00020000; } if (buff[1] == 0x12 ) { zgDefaultChannelList = 0x00040000; } if (buff[1] == 0x13 ) { zgDefaultChannelList = 0x00080000; } if (buff[1] == 0x14 ) { zgDefaultChannelList = 0x00100000; } if (buff[1] == 0x15 ) { zgDefaultChannelList = 0x00200000; } if (buff[1] == 0x16 ) { zgDefaultChannelList = 0x00400000; } if (buff[1] == 0x17 ) { zgDefaultChannelList = 0x00800000; } if (buff[1] == 0x18 ) { zgDefaultChannelList = 0x01000000; } if (buff[1] == 0x19 ) { zgDefaultChannelList = 0x02000000; } if (buff[1] == 0x1a ) { zgDefaultChannelList = 0x04000000; } osal_nv_write( ZCD_NV_CHANLIST, 0 ,sizeof(zgDefaultChannelList), &zgDefaultChannelList); UART_Send_String( (uint8*)&zgDefaultChannelList, sizeof(zgDefaultChannelList) ); /* _NIB.nwkLogicalChannel = buff[1]; NLME_UpdateNV(0x01); if( osal_nv_write(ZCD_NV_CHANLIST, 0, osal_nv_item_len( ZCD_NV_CHANLIST ), &tmp32) != ZSUCCESS) { uprint("change channel to nv failed"); } */ /* _NIB.nwkLogicalChannel = buff[1]; ZMacSetReq( ZMacChannel, &buff[1]); ZDApp_NwkStateUpdateCB(); _NIB.nwkTotalTransmissions = 0; nwkTransmissionFailures( TRUE ); */ } } }// readBytes==2 ser_process( buff,readBytes );// 中讯威易的协议最小也是2 字节 }//if( readBytes >= 2) AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID, readBytes, (byte *)&buff, &GenericApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ); } //if (readBytes > 0) }
/********************************************************************* * @fn zclServerApp_event_loop * * @brief Event Loop Processor for the ZCL Commissioing Cluster * Server Application. * * @param task_id - task id * @param events - event bitmap * * @return unprocessed events */ uint16 zclCCServer_event_loop( uint8 task_id, uint16 events ) { (void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) { afIncomingMSGPacket_t *MSGpkt; while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclCCServer_TaskID )) ) { switch ( MSGpkt->hdr.event ) { case ZCL_INCOMING_MSG: // Incoming ZCL Foundation command/response messages break; case ZDO_STATE_CHANGE: // Process State Change messages break; case KEY_CHANGE: zclCCServer_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); break; default: break; } // Release the memory osal_msg_deallocate( (uint8 *)MSGpkt ); } // return unprocessed events return (events ^ SYS_EVENT_MSG); } if ( events & CCSERVER_LEAVE_TIMER_EVT ) { if ( zcl_CCStartupMode( restartDevice.options ) == CC_STARTUP_MODE_REPLACE_RESTART ) { // Perform a Leave on our old network if ( zclCCServer_SendLeaveReq() == ZSuccess ) { // Wait for Leave confirmation before joining the new network leaveInitiated = TO_JOIN_OPERATIONAL_NWK; } else { // Notify our task to restart the network osal_set_event( zclCCServer_TaskID, CCSERVER_RESTART_TIMER_EVT ); } } else { // Notify our task to restart the network osal_set_event( zclCCServer_TaskID, CCSERVER_RESTART_TIMER_EVT ); } return ( events ^ CCSERVER_LEAVE_TIMER_EVT ); } if ( events & CCSERVER_RESTART_TIMER_EVT ) { if ( zcl_CCStartupMode( restartDevice.options ) == CC_STARTUP_MODE_REPLACE_RESTART ) { // Set the NV startup option to force a "new" join. zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE ); } else { // Reset the NV startup option to resume from NV by clearing // the "new" join option. zgWriteStartupOptions( ZG_STARTUP_CLEAR, ZCD_STARTOPT_DEFAULT_NETWORK_STATE ); } SystemResetSoft(); return ( events ^ CCSERVER_RESTART_TIMER_EVT ); } if ( events & CCSERVER_RESET_TIMER_EVT ) { // Set the NV startup option to default the Config state values zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_CONFIG_STATE ); // Set the NV startup option to force a "new" join. zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE ); SystemResetSoft(); return ( events ^ CCSERVER_RESET_TIMER_EVT ); } // Discard unknown events return 0; }