• 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 "cmsis_os2.h"
18 #include "securec.h"
19 
20 namespace OHOS {
21 namespace DistributedHardware {
22 namespace {
23 const int32_t MILL_SECONDS_PER_SECOND = 1000;
24 }
DmTimer(const std::string & name)25 DmTimer::DmTimer(const std::string &name)
26 {
27     if (name.empty()) {
28         LOGI("DmTimer name is null");
29         return;
30     }
31     mStatus_ = DmTimerStatus::DM_STATUS_INIT;
32     mTimeOutSec_ = 0;
33     mTimerName_ = name;
34 }
35 
~DmTimer()36 DmTimer::~DmTimer()
37 {
38     if (mTimerName_.empty()) {
39         LOGI("DmTimer is not init");
40         return;
41     }
42     LOGI("DmTimer %s Destroy in", mTimerName_.c_str());
43 }
44 
45 struct DmTimst {
46     TimeoutHandle handle;
47     DmTimer *dmtimer;
48     void *data;
49 };
50 
handler(void * data)51 static void handler(void *data)
52 {
53     struct DmTimst *myTimer = (struct DmTimst *)data;
54     myTimer->handle(myTimer->data, *(myTimer->dmtimer));
55 }
56 
Start(uint32_t timeOut,TimeoutHandle handle,void * data)57 DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data)
58 {
59     if (mTimerName_.empty() || handle == nullptr || data == nullptr) {
60         LOGI("DmTimer is not init or param empty");
61         return DmTimerStatus::DM_STATUS_FINISH;
62     }
63     LOGI("DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut);
64     if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) {
65         return DmTimerStatus::DM_STATUS_BUSY;
66     }
67 
68     static struct DmTimst myTimer = {
69         .handle = handle,
70         .dmtimer = this,
71         .data = data,
72     };
73     mTimeOutSec_ = timeOut;
74 
75     void *id = osTimerNew((osTimerFunc_t)handler, osTimerType_t::osTimerOnce, (void *)&myTimer, NULL);
76     if (id != NULL) {
77         LOGI("Get name of a timer success ");
78     } else {
79         LOGI("Get name of a timer failed");
80         return DmTimerStatus::DM_STATUS_CREATE_ERROR;
81     }
82 
83     timerId = id;
84     int stOk = osTimerStart(timerId, timeOut * osKernelGetTickFreq());
85     if (stOk == 0) {
86         LOGI("DmTimer %s start timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_);
87         mStatus_ = DmTimerStatus::DM_STATUS_RUNNING;
88     } else {
89         LOGI("Start a timer failed");
90         (void)osTimerDelete(timerId);
91     }
92 
93     return mStatus_;
94 }
95 
Stop(int32_t code)96 void DmTimer::Stop(int32_t code)
97 {
98     LOGI("DmTimer Stop code (%d)", code);
99     if (timerId != NULL) {
100         osTimerDelete(timerId);
101         timerId = NULL;
102     }
103     LOGI("DmTimer %s end timer at  (%d)s", mTimerName_.c_str(), mTimeOutSec_);
104     return;
105 }
106 
GetTimerName()107 std::string DmTimer::GetTimerName()
108 {
109     return mTimerName_;
110 }
111 } // namespace DistributedHardware
112 } // namespace OHOS
113