bool CurrentDetectionT1_SendHeartBeatMessage(uint8* sendToG1Data) { SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_REALTIME_MASK, 1); if (nv_number() || nv_mem_number()) { SET_HEART_BIT_STATUS(sendToG1Data, HEART_BIT_STATUS_RECOVER_MASK, 1); } else { SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_RECOVER_MASK, 0); } ShowHeartBeatInfo(sendToG1Data); ShowRssiInfo(); if (AF_DataRequest( &CurrentDetectionT1_Periodic_DstAddr, &CurrentDetectionT1_epDesc, DTCT_PERIODIC_CLUSTERID, HEART_BIT_MSG_LEN, sendToG1Data, &CurrentDetectionT1_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS) == afStatus_SUCCESS) { HalLedBlink(HAL_LED_2, 2, 20, 50); return true; } return false; }
uint8* nv_mem_read(void) { if (nv_mem_number() == 0) { return NULL; } return rfData[_beginIndex].data; }
uint8* nv_mem_read_last(void) { if (nv_mem_number() == 0) { return NULL; } if (_endIndex == 0) { return rfData[MAX_RECOVER_MSG_IN_MEN - 1].data; } else { return rfData[_endIndex - 1].data; } }
bool CurrentDetectionT1_CheckDelta(uint8* sendToG1Data) { uint16 curAd1Value; uint16 lastAd1Value; if (nv_mem_number() == 0) { uint8 buf[NV_LEN]; if (!nv_read_last_msg(buf)) { return true; } lastAd1Value = (buf[TIME_LEN * 2] << 8) + buf[TIME_LEN * 2 + 1]; } else { uint8 * buf = nv_mem_read_last(); if (buf == NULL) { return true; } lastAd1Value = (buf[TIME_LEN * 2] << 8) + buf[TIME_LEN * 2 + 1]; } curAd1Value = sendToG1Data[SN_LEN + TIME_LEN * 2 + 1]; if ( (lastAd1Value > curAd1Value) && ((lastAd1Value - curAd1Value) > delta)) { return true; } else if ( (lastAd1Value < curAd1Value) && ((curAd1Value - lastAd1Value) > delta)) { return true; } return false; }
void CurrentDetectionT1_RecoverHeartBeatMessage(uint8* sendToG1Buf) { if (!inNetwork) { return; } if (nv_number()) { if (nv_read_msg(sendToG1Buf + SN_LEN)) { SET_HEART_BIT_STATUS(sendToG1Buf , HEART_BIT_STATUS_REALTIME_MASK, 0); if (AF_DataRequest( &CurrentDetectionT1_Periodic_DstAddr, &CurrentDetectionT1_epDesc, DTCT_PERIODIC_CLUSTERID, HEART_BIT_MSG_LEN, sendToG1Buf, &CurrentDetectionT1_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS) { nv_next(); if (nv_number() == 0) { nv_write_config(); } PrintRecover(sendToG1Buf, true); HalLedBlink(HAL_LED_2, 2, 20, 50); } else { PrintRecover(sendToG1Buf, false); } } } else if (nv_mem_number()) { memcpy(sendToG1Buf + SN_LEN, nv_mem_read(), NV_LEN); SET_HEART_BIT_STATUS(sendToG1Buf , HEART_BIT_STATUS_REALTIME_MASK, 0); if (AF_DataRequest( &CurrentDetectionT1_Periodic_DstAddr, &CurrentDetectionT1_epDesc, DTCT_PERIODIC_CLUSTERID, HEART_BIT_MSG_LEN, sendToG1Buf, &CurrentDetectionT1_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS) { nv_mem_next(); PrintRecover(sendToG1Buf, true); HalLedBlink(HAL_LED_2, 2, 20, 50); } else { PrintRecover(sendToG1Buf, false); } } }
uint16 CurrentDetectionT1_ProcessEvent(uint8 task_id, uint16 events) { static uint16 timerCount = 0; static uint32 heartBitFailNum = 0; //uint8 deltaInfo[] = "delta = 1\n"; afIncomingMSGPacket_t* MSGpkt; (void)task_id; // Intentionally unreferenced parameter if (events & SYS_EVENT_MSG) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(CurrentDetectionT1_TaskID); while (MSGpkt) { switch (MSGpkt->hdr.event) { case CMD_SERIAL_MSG: CurrentDetectionT1_SerialCMD((mtOSALSerialData_t *)MSGpkt); break; case AF_INCOMING_MSG_CMD: CurrentDetectionT1_RfCMD(MSGpkt->cmd.DataLength, MSGpkt->cmd.Data); HalLedBlink(HAL_LED_2, 2, 20, 50); break; // Received whenever the device changes state in the network case ZDO_STATE_CHANGE: CurrentDetectionT1_NwkState = (devStates_t)(MSGpkt->hdr.status); if (CurrentDetectionT1_NwkState == DEV_END_DEVICE) { HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); HalUARTWrite(0, EndDeviceStatus, strlen((char *)EndDeviceStatus)); inNetwork = true; } break; default: break; } // Release the memory osal_msg_deallocate((uint8 *)MSGpkt); // Next - if one is available MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(CurrentDetectionT1_TaskID); } // return unprocessed events return (events ^ SYS_EVENT_MSG); } // Send a message out - This event is generated by a timer // (setup in CurrentDetectionT1_Init()). if (events & DTCT_HEARTBEAT_MSG_EVT) { uint8 sendToG1Data[HEART_BIT_MSG_LEN]; sendToG1Data[HEART_BIT_MSG_LEN - 1] = 0; memcpy(sendToG1Data, serialNumber, SN_LEN); reset_osal_SystemClock(); if (lastNvTime_l + get_osal_SystemClock() < lastNvTime_l) { nv_write_config(); HAL_SYSTEM_RESET(); } if (CurrentDetectionT1_HandleSendRepairMessage(sendToG1Data)) { CurrentDetectionT1_SampleCurrentAdcValue(true); osal_start_timerEx( CurrentDetectionT1_TaskID, DTCT_HEARTBEAT_MSG_EVT, DTCT_TIMER_MSG_TIMEOUT); // The repairing status, no need to send ADC value any more. return (events ^ DTCT_HEARTBEAT_MSG_EVT); } CurrentDetectionT1_SampleCurrentAdcValue(false); ++timerCount; if ((timerCount == (480 * heartbitRate)) || (timerCount == (960 * heartbitRate)) || (timerCount == (1440 * heartbitRate))) { CurrentDetectionT1_RecoverHeartBeatMessage(sendToG1Data); } // send heart beat every 1.92s. else if (timerCount == (HEARTBIT_TIME_UINT * heartbitRate)) { CurrentDetectionT1_SetAverageCurrentAdcValue(sendToG1Data, CYCLE_NUM_IN_UINT * heartbitRate); SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_POWER_SUPPLY_MASK, 0); SET_HEART_BIT_STATUS(sendToG1Data , HEART_BIT_STATUS_BATTERY_CHARGING_MASK, 1); if (inNetwork) { // Send the Heartbeat Message if (!CurrentDetectionT1_SendHeartBeatMessage(sendToG1Data)) { ++heartBitFailNum; CurrentDetectionT1_StoreHeartBeatMessage(sendToG1Data); } else { heartBitFailNum = 0; } } else { ++heartBitFailNum; if (CurrentDetectionT1_CheckDelta(sendToG1Data)) { CurrentDetectionT1_StoreHeartBeatMessage(sendToG1Data); } } if (heartBitFailNum == FAIL_TIMES_TO_RESTART) { if (nv_mem_number()) { nv_write_msg(); } HAL_SYSTEM_RESET(); } timerCount = 0; } osal_start_timerEx( CurrentDetectionT1_TaskID, DTCT_HEARTBEAT_MSG_EVT, DTCT_TIMER_MSG_TIMEOUT); // return unprocessed events return (events ^ DTCT_HEARTBEAT_MSG_EVT); } if (events & DTCT_LED_WTD_EVT) { //Feed WatchDog WDCTL = 0xa0; WDCTL = 0x50; if (P2_0) { HalLedSet(HAL_LED_1, HAL_LED_MODE_ON); HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } else { checkLedStatus(); } osal_start_timerEx( CurrentDetectionT1_TaskID, DTCT_LED_WTD_EVT, DTCT_LED_WTD_EVT_TIMEOUT); return (events ^ DTCT_LED_WTD_EVT); } // Discard unknown events return 0; }
bool nv_mem_full(void) { return (nv_mem_number() == MAX_RECOVER_MSG_IN_MEN - 1); }