void ReqBind(void) { HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF ); zAddrType_t dstAddr; // Initiate an End Device Bind Request for the mandatory endpoint dstAddr.addrMode = Addr16Bit; dstAddr.addr.shortAddr = 0x0000; // Coordinator #ifdef ZDO_COORDINATOR ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), SerialApp_epDesc.endPoint, SERIALAPP_PROFID, SERIALAPP_CENTER_OUT_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList, SERIALAPP_CENTER_IN_MAX_CLUSTERS, (cId_t *)SerialApp_CenterIn_EndOut_ClusterList, FALSE ); #else ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), SerialApp_epDesc.endPoint, SERIALAPP_PROFID, SERIALAPP_CENTER_IN_MAX_CLUSTERS, (cId_t *)SerialApp_CenterIn_EndOut_ClusterList, SERIALAPP_CENTER_OUT_MAX_CLUSTERS, (cId_t *)SerialApp_ClusterList, FALSE ); #endif }
/********************************************************************* * @fn zclSampleLight_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_4 * HAL_KEY_SW_3 * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void zclSampleLight_HandleKeys( byte shift, byte keys ) { zAddrType_t dstAddr; (void)shift; // Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_2 ) { // Initiate an End Device Bind Request, this bind request will // only use a cluster list that is important to binding. dstAddr.addrMode = afAddr16Bit; dstAddr.addr.shortAddr = 0; // Coordinator makes the match ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), SAMPLELIGHT_ENDPOINT, ZCL_HA_PROFILE_ID, ZCLSAMPLELIGHT_BINDINGLIST, bindingInClusters, 0, NULL, // No Outgoing clusters to bind TRUE ); } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } }
/*********************************************************** LCD send retry ***********************************************************/ static void LCD_SendRetry(){ // first get the short address uint16 srcAddr = NLME_GetShortAddr(); // new the frame byte frame[7] = {0}; // fill the header frame[0] = 0xFE; // fill the length frame[1] = 0; // fill the command frame[2] = LO_UINT16(LCD_RETRY_CMD); frame[3] = HI_UINT16(LCD_RETRY_CMD); // fill Addr frame[4] = LO_UINT16(srcAddr); frame[5] = HI_UINT16(srcAddr); // fill the fcs frame[6] = LCD_CalcFCS((byte*)&frame[1], 5); // Send the data to Coordinator AF_DataRequest( &LCD_DstAddr, &LCD_epDesc, ZIGBEE_COMMON_CLUSTER, 7, (byte *)frame, &LCD_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ); }
/********************************************************************* * @fn GenericApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_4 * HAL_KEY_SW_3 * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ void GenericApp_HandleKeys( byte shift, byte keys ) { zAddrType_t dstAddr; // Shift is used to make each button/switch dual purpose. if ( shift ) { if ( keys & HAL_KEY_SW_1 ) { } if ( keys & HAL_KEY_SW_2 ) { } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } } else { if ( keys & HAL_KEY_SW_1 ) { } if ( keys & HAL_KEY_SW_2 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate an End Device Bind Request for the mandatory endpoint 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 ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate a Match Description Request (Service Discovery) 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 ); } } }
/********************************************************************* * @fn zcl_SendBindRequest * * @brief Send the appropriate bind request based on DEV_TYPE. * * @param none * * @return none */ static void zcl_SendBindRequest( void ) { dstAddr.addrMode = afAddr16Bit; dstAddr.addr.shortAddr = 0; // Coordinator makes the match ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), ENDPOINT, ZCL_HA_PROFILE_ID, ZCL_BINDINGLIST, bindingClusters, ZCL_BINDINGLIST, bindingClusters, TRUE ); }
/********************************************************************* * @fn zclSampleSw_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_4 * HAL_KEY_SW_3 * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void zclSampleSw_HandleKeys( byte shift, byte keys ) { zAddrType_t dstAddr; (void)shift; // Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_1 ) { // Using this as the "Light Switch" #ifdef ZCL_ON_OFF zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, false, 0 ); #endif } if ( keys & HAL_KEY_SW_2 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate an End Device Bind Request, this bind request will // only use a cluster list that is important to binding. dstAddr.addrMode = afAddr16Bit; dstAddr.addr.shortAddr = 0; // Coordinator makes the match ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), SAMPLESW_ENDPOINT, ZCL_HA_PROFILE_ID, 0, NULL, // No incoming clusters to bind ZCLSAMPLESW_BINDINGLIST, bindingOutClusters, TRUE ); } if ( keys & HAL_KEY_SW_3 ) { #ifdef ZCL_LEVEL_CTRL uint16 potiVal = HalAdcRead ( SAMPLESW_POTI_CHANNEL, HAL_ADC_RESOLUTION_8 ); zclGeneral_SendLevelControlMoveToLevel( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, potiVal, SAMPLESW_STD_TRANSTIME, false, 0 ); #endif } if ( keys & HAL_KEY_SW_4 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate a Match Description Request (Service Discovery) dstAddr.addrMode = AddrBroadcast; dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR; ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR, ZCL_HA_PROFILE_ID, ZCLSAMPLESW_BINDINGLIST, bindingOutClusters, 0, NULL, // No incoming clusters to bind FALSE ); } }
/********************************************************************* * @fn App_SendSample * * @brief * * @param buf - pointer for the field * len - length of the field * * @return none */ void App_SendSample(unsigned char *buf, unsigned char len, uint16 option) { uint8 packetLen=0; app_msg_t packet; packet.head = 0xFE; // packet.nodeid = theNodeID; SendSeqno++; if(SendSeqno == 0xFE) { SendSeqno = 1; } packet.seqno = SendSeqno; memcpy(packet.IEEEAddr, NLME_GetExtAddr(), 8);//IEEE address 64 bit myAddr = NLME_GetShortAddr(); packet.myAddr = myAddr; packet.rssi = 0x00; packet.cmd = option; packet.len = len; memcpy(packet.data, buf, len); packetLen = sizeof(app_msg_t)-60 + len; // packet.end = 0xAA; // AF_SKIP_ROUTING - will cause the device to skip // routing and try to send the message directly if ( AF_DataRequest( &ParkingApp_Periodic_DstAddr, &ParkingApp_epDesc, PARKINGAPP_PERIODIC_CLUSTERID, packetLen,//sizeof(app_msg_t),//22, (byte*) &packet,//(byte *)tempbuf, &ParkingApp_TransID, #if (defined DATABROADCAST && DATABROADCAST == TRUE) AF_SKIP_ROUTING, // AF_DISCV_ROUTE ENDDEVICE_BCAST_RADIUS ) == afStatus_SUCCESS ) //AF_DEFAULT_RADIUS #else AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) #endif { // P0_0 = 0; } else { // Error occurred in request to send. } }
/********************************************************************* * * @fn LCD_SendTopoInfo * * * */ static void LCD_SendTopoInformation() { // Define the Frame unsigned char* srcExtAddr; srcExtAddr = NLME_GetExtAddr(); uint16 srcAddr = NLME_GetShortAddr(); uint16 parAddr = NLME_GetCoordShortAddr(); byte frame[19] = {0}; // Build the Frame // Fill SOF 0xFE frame[0] = 0xFE; // Fill len frame[1] = 12; // Fill CMD frame[2] = LO_UINT16(TOPOLOGY_CMD); frame[3] = HI_UINT16(TOPOLOGY_CMD); // Fill Addr frame[4] = LO_UINT16(srcAddr); frame[5] = HI_UINT16(srcAddr); // fill type frame[6] = LO_UINT16(LCD); frame[7] = HI_UINT16(LCD); // Fill Parent frame[8] = LO_UINT16(parAddr); frame[9] = HI_UINT16(parAddr); // fill the ieee64 address copyExtAddr(srcExtAddr, (byte*)&frame[10]); // Cal and fill FCS frame[18] = LCD_CalcFCS((byte*)&frame[1], 17); // Send the data to Coordinator AF_DataRequest( &LCD_DstAddr, &LCD_epDesc, ZIGBEE_COMMON_CLUSTER, 19, (byte *)frame, &LCD_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ); }
void Router_SendTopologyInformation() { // Define the Frame uint16 srcAddr = NLME_GetShortAddr(); uint16 parAddr = NLME_GetCoordShortAddr(); byte frame[11] = {0}; // Build the Frame // Fill SOF 0xFE frame[0] = 0xFE; // Fill len frame[1] = 4; // Fill CMD frame[2] = LO_UINT16(TOPOLOGY_CMD); frame[3] = HI_UINT16(TOPOLOGY_CMD); // Fill Addr frame[4] = LO_UINT16(srcAddr); frame[5] = HI_UINT16(srcAddr); // Fill type frame[6] = LO_UINT16(ROUTER); frame[7] = HI_UINT16(ROUTER); // Fill parent frame[8] = LO_UINT16(parAddr); frame[9] = HI_UINT16(parAddr); // Cal and fill FCS frame[10] = Router_CalcFCS((byte*)&frame[1], 9); // Send the data to Coordinator AF_DataRequest( &Router_DstAddr, & Router_epDesc, ZIGBEE_COMMON_CLUSTER, 11, (byte *) frame, &Router_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ); }
/********************************************************************* * @fn zclSampleSw_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_5 * HAL_KEY_SW_4 * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void zclSampleSw_HandleKeys( byte shift, byte keys ) { // toggle remote light if ( keys & HAL_KEY_SW_1 ) { giSwScreenMode = SW_MAINMODE; // remove help screen if there // Using this as the "Light Switch" #ifdef ZCL_ON_OFF zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, FALSE, 0 ); #endif #ifdef LCD_SUPPORTED HalLcdWriteString( (char *)sCmdSent, HAL_LCD_LINE_2 ); // clear message on screen after 3 seconds osal_start_timerEx( zclSampleSw_TaskID, SAMPLESW_MAIN_SCREEN_EVT, 3000 ); #endif } // invoke EZ-Mode if ( keys & HAL_KEY_SW_2 ) { giSwScreenMode = SW_MAINMODE; // remove help screen if there #ifdef ZCL_EZMODE { zclEZMode_InvokeData_t ezModeData; static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_GEN_ON_OFF }; // only bind on the on/off cluster // Invoke EZ-Mode ezModeData.endpoint = SAMPLESW_ENDPOINT; // endpoint on which to invoke EZ-Mode if ( (zclSampleSw_NwkState == DEV_ZB_COORD) || (zclSampleSw_NwkState == DEV_ROUTER) || (zclSampleSw_NwkState == DEV_END_DEVICE) ) { ezModeData.onNetwork = TRUE; // node is already on the network } else { ezModeData.onNetwork = FALSE; // node is not yet on the network } ezModeData.initiator = TRUE; // OnOffSwitch is an initiator ezModeData.numActiveOutClusters = 1; // active output cluster ezModeData.pActiveOutClusterIDs = clusterIDs; ezModeData.numActiveInClusters = 0; // no active input clusters ezModeData.pActiveInClusterIDs = NULL; zcl_InvokeEZMode( &ezModeData ); #ifdef LCD_SUPPORTED HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 ); #endif } #else // NOT ZCL_EZMODE // bind to remote light zAddrType_t dstAddr; HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate an End Device Bind Request, this bind request will // only use a cluster list that is important to binding. dstAddr.addrMode = afAddr16Bit; dstAddr.addr.shortAddr = 0; // Coordinator makes the match ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), SAMPLESW_ENDPOINT, ZCL_HA_PROFILE_ID, 0, NULL, // No incoming clusters to bind ZCLSAMPLESW_BINDINGLIST, bindingOutClusters, TRUE ); #endif // ZCL_EZMODE } // toggle permit join if ( keys & HAL_KEY_SW_4 ) { giSwScreenMode = SW_MAINMODE; // remove help screen if there if ( ( zclSampleSw_NwkState == DEV_ZB_COORD ) || ( zclSampleSw_NwkState == DEV_ROUTER ) ) { zAddrType_t tmpAddr; tmpAddr.addrMode = Addr16Bit; tmpAddr.addr.shortAddr = NLME_GetShortAddr(); // toggle permit join gPermitDuration = gPermitDuration ? 0 : 0xff; // Trust Center significance is always true ZDP_MgmtPermitJoinReq( &tmpAddr, gPermitDuration, TRUE, FALSE ); } } if ( shift && ( keys & HAL_KEY_SW_5 ) ) { zclSampleSw_BasicResetCB(); } else if ( keys & HAL_KEY_SW_5 ) { giSwScreenMode = giSwScreenMode ? SW_MAINMODE : SW_HELPMODE; #ifdef LCD_SUPPORTED HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 ); #endif } // update the display zclSampleSw_LcdDisplayUpdate(); }
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) }
afStatus_t AF_DataRequest( afAddrType_t *dstAddr, endPointDesc_t *srcEP, uint16 cID, uint16 len, uint8 *buf, uint8 *transID, uint8 options, uint8 radius ) { pDescCB pfnDescCB; ZStatus_t stat; APSDE_DataReq_t req; afDataReqMTU_t mtu; // Verify source end point if ( srcEP == NULL ) { return afStatus_INVALID_PARAMETER; } #if !defined( REFLECTOR ) if ( dstAddr->addrMode == afAddrNotPresent ) { return afStatus_INVALID_PARAMETER; } #endif // Validate broadcasting if ( ( dstAddr->addrMode == afAddr16Bit ) || ( dstAddr->addrMode == afAddrBroadcast ) ) { // Check for valid broadcast values if( ADDR_NOT_BCAST != NLME_IsAddressBroadcast( dstAddr->addr.shortAddr ) ) { // Force mode to broadcast dstAddr->addrMode = afAddrBroadcast; } else { // Address is not a valid broadcast type if ( dstAddr->addrMode == afAddrBroadcast ) { return afStatus_INVALID_PARAMETER; } } } else if ( dstAddr->addrMode != afAddr64Bit && dstAddr->addrMode != afAddrGroup && dstAddr->addrMode != afAddrNotPresent ) { return afStatus_INVALID_PARAMETER; } // Set destination address req.dstAddr.addrMode = dstAddr->addrMode; if ( dstAddr->addrMode == afAddr64Bit ) osal_cpyExtAddr( req.dstAddr.addr.extAddr, dstAddr->addr.extAddr ); else req.dstAddr.addr.shortAddr = dstAddr->addr.shortAddr; req.profileID = ZDO_PROFILE_ID; if ( (pfnDescCB = afGetDescCB( srcEP )) ) { uint16 *pID = (uint16 *)(pfnDescCB( AF_DESCRIPTOR_PROFILE_ID, srcEP->endPoint )); if ( pID ) { req.profileID = *pID; osal_mem_free( pID ); } } else if ( srcEP->simpleDesc ) { req.profileID = srcEP->simpleDesc->AppProfId; } req.txOptions = 0; if ( ( options & AF_ACK_REQUEST ) && ( req.dstAddr.addrMode != AddrBroadcast ) && ( req.dstAddr.addrMode != AddrGroup ) ) { req.txOptions |= APS_TX_OPTIONS_ACK; } if ( options & AF_SKIP_ROUTING ) { req.txOptions |= APS_TX_OPTIONS_SKIP_ROUTING; } if ( options & AF_EN_SECURITY ) { req.txOptions |= APS_TX_OPTIONS_SECURITY_ENABLE; mtu.aps.secure = TRUE; } else { mtu.aps.secure = FALSE; } mtu.kvp = FALSE; req.transID = *transID; req.srcEP = srcEP->endPoint; req.dstEP = dstAddr->endPoint; req.clusterID = cID; req.asduLen = len; req.asdu = buf; req.discoverRoute = AF_DataRequestDiscoverRoute;//(uint8)((options & AF_DISCV_ROUTE) ? 1 : 0); req.radiusCounter = radius; #if defined ( INTER_PAN ) req.dstPanId = dstAddr->panId; if ( StubAPS_InterPan( dstAddr->panId, dstAddr->endPoint ) ) { if ( len > INTERP_DataReqMTU() ) { stat = afStatus_INVALID_PARAMETER; } else { stat = INTERP_DataReq( &req ); } } else #endif // INTER_PAN { if (len > afDataReqMTU( &mtu ) ) { if (apsfSendFragmented) { stat = (*apsfSendFragmented)( &req ); } else { stat = afStatus_INVALID_PARAMETER; } } else { stat = APSDE_DataReq( &req ); } } /* * If this is an EndPoint-to-EndPoint message on the same device, it will not * get added to the NWK databufs. So it will not go OTA and it will not get * a MACCB_DATA_CONFIRM_CMD callback. Thus it is necessary to generate the * AF_DATA_CONFIRM_CMD here. Note that APSDE_DataConfirm() only generates one * message with the first in line TransSeqNumber, even on a multi message. * Also note that a reflected msg will not have its confirmation generated * here. */ if ( (req.dstAddr.addrMode == Addr16Bit) && (req.dstAddr.addr.shortAddr == NLME_GetShortAddr()) ) { afDataConfirm( srcEP->endPoint, *transID, stat ); } if ( stat == afStatus_SUCCESS ) { (*transID)++; } return (afStatus_t)stat; }
static void pulseUartRx(uint8 port, uint8 event) { #ifdef TVSA_DEMO uint8 ch; while (HalUARTRead(PULSE_PORT, &ch, 1)) { switch (pulseState) { case SOP_STATE: if (PULSE_SOP_VAL == ch) { pulseState = CMD_STATE; } break; case CMD_STATE: pulseCmd = ch; pulseState = FCS_STATE; break; case FCS_STATE: if (pulseCmd == ch) { if (pulseCmd == PULSE_CMD_BEG) { pulseAddr = NLME_GetShortAddr(); } else if (pulseCmd == PULSE_CMD_END) { pulseAddr = INVALID_NODE_ADDR; } (void)osal_set_event(pulseTaskId, PULSE_EVT_ANN); } pulseState = SOP_STATE; break; default: break; } } #else uint8 ch[5]; HalUARTRead(PULSE_PORT, ch, 5); if (ch[2]==0x21) //if statement to check for command from Z-Sensor Monitor { sysPingRsp(); //syncAttempted=TRUE; zapSync(); if(znpPanId == INVALID_PAN_ID){ //if still invalid id, there is no PAN so set one up by becomeing the coordinator if(zap_set_logicalType(ZG_DEVICETYPE_COORDINATOR)){ HalLcdWriteString("Should be Coord.",HAL_LCD_LINE_7); } else{ HalLcdWriteString("Problem",HAL_LCD_LINE_7); } } else{ pulseAddr = znpAddr; if (ZSuccess != osal_start_timerEx(pulseTaskId, PULSE_EVT_ANN, PULSE_DLY_ANN)) { (void)osal_set_event(pulseTaskId, PULSE_EVT_ANN); } } } #endif }
/********************************************************************* * @fn GenericApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_4 * HAL_KEY_SW_3 * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void GenericApp_HandleKeys( uint8 shift, uint8 keys ) { zAddrType_t dstAddr; // Shift is used to make each button/switch dual purpose. if ( shift ) { if ( keys & HAL_KEY_SW_1 ) { } if ( keys & HAL_KEY_SW_2 ) { } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } } else { if ( keys & HAL_KEY_SW_1 ) { // Since SW1 isn't used for anything else in this application... #if defined( SWITCH1_BIND ) // we can use SW1 to simulate SW2 for devices that only have one switch, keys |= HAL_KEY_SW_2; #elif defined( SWITCH1_MATCH ) // or use SW1 to simulate SW4 for devices that only have one switch keys |= HAL_KEY_SW_4; #endif } if ( keys & HAL_KEY_SW_2 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate an End Device Bind Request for the mandatory endpoint 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 ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate a Match Description Request (Service Discovery) 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 ); } } }
/********************************************************************* * @fn zclSampleTemperatureSensor_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_5 * HAL_KEY_SW_4 * HAL_KEY_SW_3 * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys ) { if ( keys & HAL_KEY_SW_1 ) { // increase temperature giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE; if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue ) { zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100; // considering using whole number value } else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue ) { zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue; } // Send temperature information zclSampleTemperatureSensor_SendTemp(); } if ( keys & HAL_KEY_SW_2 ) { if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) || ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) ) { giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE; #ifdef ZCL_EZMODE zclEZMode_InvokeData_t ezModeData; static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT }; // only bind on the Temperature Measurement cluster // Invoke EZ-Mode ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) || ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) || ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) ) { ezModeData.onNetwork = TRUE; // node is already on the network } else { ezModeData.onNetwork = FALSE; // node is not yet on the network } ezModeData.initiator = TRUE; // Temperature Sensor is an initiator ezModeData.numActiveInClusters = 1; ezModeData.pActiveInClusterIDs = clusterIDs; ezModeData.numActiveOutClusters = 0; // active output cluster ezModeData.pActiveOutClusterIDs = NULL; zcl_InvokeEZMode( &ezModeData ); #ifdef LCD_SUPPORTED HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 ); #endif // NOT ZCL_EZMODE, Use EndDeviceBind #else { zAddrType_t dstAddr; dstAddr.addrMode = Addr16Bit; dstAddr.addr.shortAddr = 0; // Coordinator makes the EDB match // Initiate an End Device Bind Request, this bind request will // only use a cluster list that is important to binding. HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), SAMPLETEMPERATURESENSOR_ENDPOINT, ZCL_HA_PROFILE_ID, 0, NULL, ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters, FALSE ); } #endif // ZCL_EZMODE } } if ( keys & HAL_KEY_SW_3 ) { giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE; // decrease the temperature if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue ) { zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100; // considering using whole number value } else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue ) { zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue; } // Send temperature information zclSampleTemperatureSensor_SendTemp(); } if ( keys & HAL_KEY_SW_4 ) { giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE; if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) || ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) ) { // toggle permit join gPermitDuration = gPermitDuration ? 0 : 0xff; NLME_PermitJoiningRequest( gPermitDuration ); } } if ( shift && ( keys & HAL_KEY_SW_5 ) ) { zclSampleTemperatureSensor_BasicResetCB(); } else if ( keys & HAL_KEY_SW_5 ) { if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) { giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE; } else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) { #ifdef LCD_SUPPORTED HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 ); #endif giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE; } } // update display zclSampleTemperatureSensor_LcdDisplayUpdate(); }
/*************************************************************************************************** * @fn MT_UtilGetDeviceInfo * * @brief The Get Device Info serial message. * * @param None. * * @return void ***************************************************************************************************/ void MT_UtilGetDeviceInfo(void) { uint8 *buf; uint8 *pBuf; uint8 bufLen = MT_UTIL_DEVICE_INFO_RESPONSE_LEN; uint16 *assocList = NULL; #if !defined NONWK uint8 assocCnt = 0; if (ZG_DEVICE_RTR_TYPE) { assocList = AssocMakeList( &assocCnt ); bufLen += (assocCnt * sizeof(uint16)); } #endif buf = osal_mem_alloc( bufLen ); if ( buf ) { pBuf = buf; *pBuf++ = ZSUCCESS; // Status osal_nv_read( ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, pBuf ); pBuf += Z_EXTADDR_LEN; #if defined NONWK // Skip past ZStack only parameters for NONWK *pBuf++ = 0; *pBuf++ = 0; *pBuf++ = 0; *pBuf++ = 0; *pBuf = 0; #else { uint16 shortAddr = NLME_GetShortAddr(); *pBuf++ = LO_UINT16( shortAddr ); *pBuf++ = HI_UINT16( shortAddr ); } /* Return device type */ *pBuf++ = ZSTACK_DEVICE_BUILD; /*Return device state */ *pBuf++ = (uint8)devState; if (ZG_DEVICE_RTR_TYPE) { *pBuf++ = assocCnt; if ( assocCnt ) { uint8 x; uint16 *puint16 = assocList; for ( x = 0; x < assocCnt; x++, puint16++ ) { *pBuf++ = LO_UINT16( *puint16 ); *pBuf++ = HI_UINT16( *puint16 ); } } } else { *pBuf++ = 0; } #endif MT_BuildAndSendZToolResponse( ((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_UTIL), MT_UTIL_GET_DEVICE_INFO, bufLen, buf ); osal_mem_free( buf ); } if ( assocList ) { osal_mem_free( assocList ); } }
afStatus_t AF_DataRequest( afAddrType_t *dstAddr, endPointDesc_t *srcEP, uint16 cID, uint16 len, uint8 *buf, uint8 *transID, uint8 options, uint8 radius ) { pDescCB pfnDescCB; ZStatus_t stat; APSDE_DataReq_t req; afDataReqMTU_t mtu; epList_t *pList; // Verify source end point if ( srcEP == NULL ) { return afStatus_INVALID_PARAMETER; } #if !defined( REFLECTOR ) if ( dstAddr->addrMode == afAddrNotPresent ) { return afStatus_INVALID_PARAMETER; } #endif // Check if route is available before sending data if ( options & AF_LIMIT_CONCENTRATOR ) { if ( dstAddr->addrMode != afAddr16Bit ) { return ( afStatus_INVALID_PARAMETER ); } // First, make sure the destination is not its self, then check for an existing route. if ( (dstAddr->addr.shortAddr != NLME_GetShortAddr()) && (RTG_CheckRtStatus( dstAddr->addr.shortAddr, RT_ACTIVE, (MTO_ROUTE | NO_ROUTE_CACHE) ) != RTG_SUCCESS) ) { // A valid route to a concentrator wasn't found return ( afStatus_NO_ROUTE ); } } // Validate broadcasting if ( ( dstAddr->addrMode == afAddr16Bit ) || ( dstAddr->addrMode == afAddrBroadcast ) ) { // Check for valid broadcast values if( ADDR_NOT_BCAST != NLME_IsAddressBroadcast( dstAddr->addr.shortAddr ) ) { // Force mode to broadcast dstAddr->addrMode = afAddrBroadcast; } else { // Address is not a valid broadcast type if ( dstAddr->addrMode == afAddrBroadcast ) { return afStatus_INVALID_PARAMETER; } } } else if ( dstAddr->addrMode != afAddr64Bit && dstAddr->addrMode != afAddrGroup && dstAddr->addrMode != afAddrNotPresent ) { return afStatus_INVALID_PARAMETER; } // Set destination address req.dstAddr.addrMode = dstAddr->addrMode; if ( dstAddr->addrMode == afAddr64Bit ) { osal_cpyExtAddr( req.dstAddr.addr.extAddr, dstAddr->addr.extAddr ); } else { req.dstAddr.addr.shortAddr = dstAddr->addr.shortAddr; } // This option is to use Wildcard ProfileID in outgoing packets if ( options & AF_WILDCARD_PROFILEID ) { req.profileID = ZDO_WILDCARD_PROFILE_ID; } else { req.profileID = ZDO_PROFILE_ID; if ( (pfnDescCB = afGetDescCB( srcEP )) ) { uint16 *pID = (uint16 *)(pfnDescCB( AF_DESCRIPTOR_PROFILE_ID, srcEP->endPoint )); if ( pID ) { req.profileID = *pID; osal_mem_free( pID ); } } else if ( srcEP->simpleDesc ) { req.profileID = srcEP->simpleDesc->AppProfId; } } req.txOptions = 0; if ( ( options & AF_ACK_REQUEST ) && ( req.dstAddr.addrMode != AddrBroadcast ) && ( req.dstAddr.addrMode != AddrGroup ) ) { req.txOptions |= APS_TX_OPTIONS_ACK; } if ( options & AF_SKIP_ROUTING ) { req.txOptions |= APS_TX_OPTIONS_SKIP_ROUTING; } if ( options & AF_EN_SECURITY ) { req.txOptions |= APS_TX_OPTIONS_SECURITY_ENABLE; mtu.aps.secure = TRUE; } else { mtu.aps.secure = FALSE; } if ( options & AF_PREPROCESS ) { req.txOptions |= APS_TX_OPTIONS_PREPROCESS; } mtu.kvp = FALSE; if ( options & AF_SUPRESS_ROUTE_DISC_NETWORK ) { req.discoverRoute = DISC_ROUTE_INITIATE; } else { req.discoverRoute = AF_DataRequestDiscoverRoute; } req.transID = *transID; req.srcEP = srcEP->endPoint; req.dstEP = dstAddr->endPoint; req.clusterID = cID; req.asduLen = len; req.asdu = buf; req.radiusCounter = radius; #if defined ( INTER_PAN ) req.dstPanId = dstAddr->panId; #endif // INTER_PAN // Look if there is a Callback function registered for this endpoint // The callback is used to control the AF Transaction ID used when sending messages pList = afFindEndPointDescList( srcEP->endPoint ); if ( ( pList != NULL ) && ( pList->pfnApplCB != NULL ) ) { pList->pfnApplCB( &req ); } #if defined ( INTER_PAN ) if ( StubAPS_InterPan( dstAddr->panId, dstAddr->endPoint ) ) { if ( len > INTERP_DataReqMTU() ) { stat = afStatus_INVALID_PARAMETER; } else { stat = INTERP_DataReq( &req ); } } else #endif // INTER_PAN { if (len > afDataReqMTU( &mtu ) ) { if (apsfSendFragmented) { stat = (*apsfSendFragmented)( &req ); } else { stat = afStatus_INVALID_PARAMETER; } } else { stat = APSDE_DataReq( &req ); } } /* * If this is an EndPoint-to-EndPoint message on the same device, it will not * get added to the NWK databufs. So it will not go OTA and it will not get * a MACCB_DATA_CONFIRM_CMD callback. Thus it is necessary to generate the * AF_DATA_CONFIRM_CMD here. Note that APSDE_DataConfirm() only generates one * message with the first in line TransSeqNumber, even on a multi message. * Also note that a reflected msg will not have its confirmation generated * here. */ if ( (req.dstAddr.addrMode == Addr16Bit) && (req.dstAddr.addr.shortAddr == NLME_GetShortAddr()) ) { afDataConfirm( srcEP->endPoint, *transID, stat ); } if ( stat == afStatus_SUCCESS ) { (*transID)++; } return (afStatus_t)stat; }