1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <sstream> 16 #include <string> 17 #include "heartbeat.h" 18 #include "serial_struct.h" 19 namespace Hdc { HdcHeartbeat(void)20HdcHeartbeat::HdcHeartbeat(void) 21 { 22 heartbeatCount = 0; 23 messageCount = 0; 24 supportHeartbeat = false; 25 lastTime = std::chrono::steady_clock::now(); 26 } 27 AddHeartbeatCount(void)28void HdcHeartbeat::AddHeartbeatCount(void) 29 { 30 heartbeatCount++; 31 } 32 HandleMessageCount(void)33bool HdcHeartbeat::HandleMessageCount(void) 34 { 35 messageCount++; 36 #ifndef HDC_HOST 37 auto nowTime = std::chrono::steady_clock::now(); 38 auto duration = std::chrono::duration_cast<std::chrono::seconds>(nowTime - lastTime); 39 if (duration.count() > 3600) { //3600: seconds 40 return false; 41 } 42 #endif 43 return true; 44 } 45 GetHeartbeatCount(void) const46uint64_t HdcHeartbeat::GetHeartbeatCount(void) const 47 { 48 return heartbeatCount; 49 } 50 ToString(void) const51std::string HdcHeartbeat::ToString(void) const 52 { 53 std::stringstream ss; 54 ss << "heartbeat count is " << heartbeatCount << " and messages count is " << messageCount; 55 return ss.str(); 56 } 57 HandleRecvHeartbeatMsg(uint8_t * payload,int payloadSize)58std::string HdcHeartbeat::HandleRecvHeartbeatMsg(uint8_t *payload, int payloadSize) 59 { 60 if (payloadSize <= 0) { 61 return "invalid heartbeat message"; 62 } 63 lastTime = std::chrono::steady_clock::now(); 64 string s = string(reinterpret_cast<char *>(payload), payloadSize); 65 HdcSessionBase::HeartbeatMsg heartbeat; 66 SerialStruct::ParseFromString(heartbeat, s); 67 std::stringstream ss; 68 ss << "heartbeat count is " << heartbeat.heartbeatCount; 69 return ss.str(); 70 } 71 SetSupportHeartbeat(bool heartbeatStatus)72void HdcHeartbeat::SetSupportHeartbeat(bool heartbeatStatus) 73 { 74 supportHeartbeat = heartbeatStatus; 75 } 76 GetSupportHeartbeat()77bool HdcHeartbeat::GetSupportHeartbeat() 78 { 79 return supportHeartbeat; 80 } 81 } //namespace Hdc