📄 switch.c
字号:
}/**************************************************************************** * * NAME: JZA_pu8AfMsgObject * * DESCRIPTION: * Receives incoming MSG data frames. * * PARAMETERS: Name RW Usage * eAddrMode R Address mode of incoming frame * u16AddrSrc R Network address of source node * u8SrcEP R Endpoint address of source node * u8LQI R Link Quality Indication * u8DstEP R Destination endpoint address * u8ClusterId R Cluster ID of incoming frame * *pu8ClusterIDRsp R Pointer to cluster ID of response frame * *puTransactionInd R Pointer to incoming frame * *puTransactionRsp R Pointer to response frame * RETURNS: * FALSE * ****************************************************************************/PUBLIC bool_t JZA_bAfMsgObject(APS_Addrmode_e eAddrMode, uint16 u16AddrSrc, uint8 u8SrcEP, uint8 u8LQI, uint8 u8DstEP, uint8 u8ClusterID, uint8 *pu8ClusterIDRsp, AF_Transaction_s *puTransactionInd, AF_Transaction_s *puTransactionRsp){ return FALSE;}/**************************************************************************** * * NAME: JZA_vZdpResponse * * DESCRIPTION: * Receives responses to Binding or MatchDescriptor requests * * PARAMETERS: Name RW Usage * u8Type R Type of response * u8LQI R Link Quality Indication * *pu8Payload R Pointer to response * u8PayloadLen R Length of response * * RETURNS: * void * ****************************************************************************/PUBLIC void JZA_vZdpResponse(uint8 u8Type, uint8 u8Lqi, uint8 *pu8Payload, uint8 u8PayloadLen){ /* if already matched with another node, ignore any incoming responses */ if (sSwitch.eBound != E_BIND_BINDING) { return; } switch (u8Type) { case ZDP_MatchDescRsp: /* this would be used to receive responses if a Match Descriptor Request had been used */ break; case ZDP_EndDeviceBindRsp: /* response to an End Device Bind request has been received. Can now start operating as a switch. Turn off LED to indicate successful matching */ if (pu8Payload[0] == ZDP_SUCCESS_VALID) { sSwitch.eBound = E_BIND_BOUND; vLedControl(LED2, FALSE); } else { sSwitch.eBound = E_BIND_NONE; } break; default: break; }}/**************************************************************************** * * NAME: JZA_vStackEvent * * DESCRIPTION: * Used to receive additional stack events such as Data Request confirm * * PARAMETERS: Name RW Usage * eEventId R ID number of stack event * *puStackEvent R Pointer to union containing * stack event data * * RETURNS: * void * ****************************************************************************/PUBLIC void JZA_vStackEvent(teJZS_EventIdentifier eEventId, tuJZS_StackEvent *puStackEvent){ if (eEventId == JZS_EVENT_NWK_JOINED_AS_ENDDEVICE) { sSwitch.bStackReady = TRUE; vAddDesc(); }}/****************************************************************************//*** Local Functions ***//****************************************************************************//**************************************************************************** * * NAME: vInit * * DESCRIPTION: * Initialises the hardware * * RETURNS: * void * ****************************************************************************/PRIVATE void vInit(void){ /* Initialise buttons, LEDs and program variables */ vButtonInitRfd(); vLedInitRfd(); vLedControl(LED1, TRUE); vLedControl(LED2, TRUE); sSwitch.eBound = E_BIND_NONE; sSwitch.u8Endpoint = 0x31; sSwitch.bAppTimerStarted = FALSE; sSwitch.bSwitchState = FALSE; sSwitch.bStackReady = FALSE;}/**************************************************************************** * * NAME: vCheckButtons * * DESCRIPTION: * Reads the buttons and acts accordingly. This function is called by the BOS * Timer every 100ms in order to de-bounce the buttons. * * PARAMETERS: Name RW Usage * *Message R Pointer to message passed by event handler * u8Length R Length of message * * RETURNS: * void * ****************************************************************************/PRIVATE void vCheckButtons(void *Message, uint8 u8Length){ PRIVATE uint8 u8LastButtons = 0; uint8 u8Buttons; uint8 u8TimerId; /* check that button state has changed since they were last checked, and if so check for any new button presses */ u8Buttons = u8ButtonReadRfd(); if (u8Buttons != u8LastButtons) { switch (sSwitch.eBound) { case E_BIND_NONE: /* Not yet bound: user pressing Button 1 so initiate the binding process */ if (u8Buttons == E_KEY_0) { vPerformEndBindRequest(sSwitch.u8Endpoint); } break; case E_BIND_BOUND: /* already bound so running as a light switch. User pressing button 2 so toggle the light: send the appropriate command to the light and toggle the Switch's internal state */ if (u8Buttons == E_KEY_1) { if (sSwitch.bSwitchState) { vTxData(VALUE_OFF); } else { vTxData(VALUE_ON); } sSwitch.bSwitchState = (!sSwitch.bSwitchState); } break; default: break; } } u8LastButtons = u8Buttons; /* re-start the BOS timer so that vCheckButtons is called again in 100ms */ bBosCreateTimer(vCheckButtons, NULL, 0, (APP_TICK_PERIOD_ms / 10), &u8TimerId);}/**************************************************************************** * * NAME: vPerformEndBindRequest * * DESCRIPTION: * Sends a request to the coordinator to bind the node with another. * * * RETURNS: * void * ****************************************************************************/PRIVATE void vPerformEndBindRequest(uint8 u8Endpoint){ AF_SIMPLE_DESCRIPTOR *pAfSimpleDesc; uint16 u16BindingTarget = 0x0000; /* recall endpoints simple descriptor from the application framework */ pAfSimpleDesc = afmeSearchEndpoint(u8Endpoint); /* create and send a request for binding */ if(pAfSimpleDesc) { zdpEndDeviceBindReq(u16BindingTarget, pAfSimpleDesc->u8EndPoint, pAfSimpleDesc->u16ProfileId, pAfSimpleDesc->u8OutClusterCount, pAfSimpleDesc->au8OutClusterList, pAfSimpleDesc->u8InClusterCount, pAfSimpleDesc->au8InClusterList, APS_TXOPTION_NONE); } /* update programs binding state variable */ sSwitch.eBound = E_BIND_BINDING;}/**************************************************************************** * * NAME: vTxData * * DESCRIPTION: * Creates a KVP data frame and sends it to the matched node * * PARAMETERS: Name RW Usage * u8SwitchValue R Value indicating switch position * * RETURNS: * void * ****************************************************************************/PRIVATE void vTxData(uint8 u8SwitchValue){ uint8 u8SrcEP = sSwitch.u8Endpoint; APS_Addrmode_e eAddrMode; uint16 u16DestAddr; uint8 u8DestEndpoint; AF_Transaction_s Transaction; uint8 transCount = 1; /* specify destination address as coordinator, and address mode as Device address not present as using indirect (Coordinator) binding */ eAddrMode = APS_ADDRMODE_NOT_PRESENT; u16DestAddr = 0x0000; u8DestEndpoint = 0x00; /* specify the transaction sequence number */ Transaction.u8SequenceNum = u8AfGetTransactionSequence(TRUE); /* We want to send data to an input, so use the SET command type */ Transaction.uFrame.sKvp.eCommandTypeID = KVP_SET; Transaction.uFrame.sKvp.eAttributeDataType = KVP_UNSIGNED_8BIT_INTEGER; /* use the OnOff attribute for a OnOffSrc cluster, as specified in the Home Control, Lighting ZigBee public profile */ Transaction.uFrame.sKvp.u16AttributeID = ATTRIBUTE_ON_OFF; Transaction.uFrame.sKvp.eErrorCode = KVP_SUCCESS; Transaction.uFrame.sKvp.uAttributeData.UnsignedInt8 = u8SwitchValue; /* send KVP data request */ afdeDataRequest(eAddrMode, u16DestAddr, u8DestEndpoint, u8SrcEP, PROFILEID_HC, CLUSTERID_ON_OFF_SRC, AF_KVP, transCount, &Transaction, APS_TXOPTION_NONE, ENABLE_ROUTE_DISCOVERY, 0);}/**************************************************************************** * * NAME: vAddDesc * * DESCRIPTION: * Adds simple descriptor * * PARAMETERS: Name RW Usage * * RETURNS: * void * ****************************************************************************/PRIVATE void vAddDesc(void){ /* Define simple descriptor for the Switch's endpoint according to the ZigBee Home Control, Lighting Public Profile */ uint8 u8DeviceVer = DEVICEVERSION; uint16 u16ProfileId = PROFILEID_HC; uint8 u8Flags = 0x00; uint8 u8EndPoint = sSwitch.u8Endpoint; uint16 u16DeviceId = DEVICEID_HCL_SRC; /* Switch has 1 endpoint, with 1 output cluster */ uint8 u8InputClusterCnt = 0; uint8 au8InputClusterList[] = {}; uint8 u8OutputClusterCnt = 1; uint8 au8OutputClusterList[] = {CLUSTERID_ON_OFF_SRC}; (void)afmeAddSimpleDesc(u8EndPoint, u16ProfileId, u16DeviceId, u8DeviceVer, u8Flags, u8InputClusterCnt, au8InputClusterList, u8OutputClusterCnt, au8OutputClusterList);}/****************************************************************************//*** END OF FILE ***//****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -