• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "dm_timer.h"
17 #include "securec.h"
18 
19 namespace OHOS {
20 namespace DistributedHardware {
21 namespace {
22 const int32_t MILL_SECONDS_PER_SECOND = 1000;
23 }
DmTimer(std::string & name)24 DmTimer::DmTimer(std::string &name)
25 {
26     mStatus_ = DmTimerStatus::DM_STATUS_INIT;
27     mTimeOutSec_ = 0;
28     mHandle_ = nullptr;
29     mHandleData_ = nullptr;
30     (void)memset_s(mTimeFd_, sizeof(mTimeFd_), 0, sizeof(mTimeFd_));
31     mEpFd_ = 0;
32     mTimerName_ = name;
33 }
34 
~DmTimer()35 DmTimer::~DmTimer()
36 {
37     DMLOG(DM_LOG_INFO, "DmTimer %s Destory in", mTimerName_.c_str());
38     Release();
39 }
40 
Start(uint32_t timeOut,TimeoutHandle handle,void * data)41 DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data)
42 {
43     DMLOG(DM_LOG_INFO, "DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut);
44     if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) {
45         return DmTimerStatus::DM_STATUS_BUSY;
46     }
47 
48     mTimeOutSec_ = timeOut;
49     mHandle_ = handle;
50     mHandleData_ = data;
51 
52     if (CreateTimeFd()) {
53         return DmTimerStatus::DM_STATUS_CREATE_ERROR;
54     }
55 
56     mStatus_ = DmTimerStatus::DM_STATUS_RUNNING;
57 
58     return mStatus_;
59 }
60 
Stop(int32_t code)61 void DmTimer::Stop(int32_t code)
62 {
63     DMLOG(DM_LOG_INFO, "DmTimer %s Stop code (%d)", mTimerName_.c_str(), code);
64     if (mTimeFd_[1]) {
65         char event = 'S';
66         if (write(mTimeFd_[1], &event, 1) < 0) {
67             DMLOG(DM_LOG_ERROR, "DmTimer %s Stop timer failed, errno %d", mTimerName_.c_str(), errno);
68             return;
69         }
70         DMLOG(DM_LOG_INFO, "DmTimer %s Stop success", mTimerName_.c_str());
71     }
72 
73     return;
74 }
75 
WiteforTimeout()76 void DmTimer::WiteforTimeout()
77 {
78     DMLOG(DM_LOG_INFO, "DmTimer %s start timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_);
79     mHandle_(mHandleData_);
80     Release();
81 
82     DMLOG(DM_LOG_ERROR, "DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_);
83     return;
84 }
85 
CreateTimeFd()86 int32_t DmTimer::CreateTimeFd()
87 {
88     DMLOG(DM_LOG_INFO, "DmTimer %s creatTimeFd", mTimerName_.c_str());
89     return 0;
90 }
91 
Release()92 void DmTimer::Release()
93 {
94     DMLOG(DM_LOG_INFO, "DmTimer %s Release in", mTimerName_.c_str());
95     if (mStatus_ == DmTimerStatus::DM_STATUS_INIT) {
96         DMLOG(DM_LOG_INFO, "DmTimer %s already Release", mTimerName_.c_str());
97         return;
98     }
99     mStatus_ = DmTimerStatus::DM_STATUS_INIT;
100     close(mTimeFd_[0]);
101     close(mTimeFd_[1]);
102     if (mEpFd_ >= 0) {
103         close(mEpFd_);
104     }
105     mTimeFd_[0] = 0;
106     mTimeFd_[1] = 0;
107     mEpFd_ = 0;
108 }
109 }
110 }
111