• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "osal_timer.h"
32 #include "los_hwi.h"
33 #include "los_swtmr.h"
34 #include "los_swtmr_pri.h"
35 #include "hdf_log.h"
36 #include "osal_mem.h"
37 
38 #define HDF_LOG_TAG osal_timer
39 
40 struct OsalLitetimer {
41     uintptr_t arg;
42     uint16_t timerID;
43     OsalTimerFunc func;
44     uint32_t interval;
45 };
46 
OsalTimerCreate(OsalTimer * timer,uint32_t interval,OsalTimerFunc func,uintptr_t arg)47 int32_t OsalTimerCreate(OsalTimer *timer, uint32_t interval, OsalTimerFunc func, uintptr_t arg)
48 {
49     struct OsalLitetimer *liteTimer = NULL;
50 
51     if (func == NULL || timer == NULL || interval == 0) {
52         HDF_LOGE("%s invalid para", __func__);
53         return HDF_ERR_INVALID_PARAM;
54     }
55 
56     timer->realTimer = NULL;
57 
58     liteTimer = (struct OsalLitetimer *)OsalMemCalloc(sizeof(*liteTimer));
59     if (liteTimer == NULL) {
60         HDF_LOGE("%s malloc fail", __func__);
61         return HDF_ERR_MALLOC_FAIL;
62     }
63     liteTimer->timerID = MAX_INVALID_TIMER_VID;
64     liteTimer->arg = arg;
65     liteTimer->func = func;
66     liteTimer->interval = interval;
67     timer->realTimer = (void *)liteTimer;
68 
69     return HDF_SUCCESS;
70 }
71 
OsalStartTimer(OsalTimer * timer,UINT8 mode)72 static int32_t OsalStartTimer(OsalTimer *timer, UINT8 mode)
73 {
74     uint32_t ret;
75     uint32_t intSave;
76     uint32_t interval;
77     uint16_t timerID = 0;
78     struct OsalLitetimer *liteTimer = NULL;
79 
80     if (timer == NULL || timer->realTimer == NULL) {
81         HDF_LOGE("%s invalid para %d", __func__, __LINE__);
82         return HDF_ERR_INVALID_PARAM;
83     }
84 
85     liteTimer = (struct OsalLitetimer *)timer->realTimer;
86     if (liteTimer->interval == 0 || liteTimer->func == NULL) {
87         HDF_LOGE("%s invalid para %d", __func__, __LINE__);
88         return HDF_ERR_INVALID_PARAM;
89     }
90 
91     interval = liteTimer->interval;
92     intSave = LOS_IntLock();
93     ret = LOS_SwtmrCreate(LOS_MS2Tick(interval), mode, (SWTMR_PROC_FUNC)liteTimer->func, &timerID, liteTimer->arg);
94     if (ret != LOS_OK) {
95         LOS_IntRestore(intSave);
96         HDF_LOGE("%s LOS_SwtmrCreate fail %u", __func__, ret);
97         return HDF_FAILURE;
98     }
99 
100     ret = LOS_SwtmrStart(timerID);
101     if (ret != LOS_OK) {
102         LOS_SwtmrDelete(timerID);
103         LOS_IntRestore(intSave);
104         HDF_LOGE("%s LOS_SwtmrStart fail %u", __func__, ret);
105         return HDF_FAILURE;
106     }
107     LOS_IntRestore(intSave);
108 
109     liteTimer->timerID = timerID;
110     return HDF_SUCCESS;
111 }
112 
OsalTimerStartLoop(OsalTimer * timer)113 int32_t OsalTimerStartLoop(OsalTimer *timer)
114 {
115     return OsalStartTimer(timer, LOS_SWTMR_MODE_PERIOD);
116 }
117 
OsalTimerStartOnce(OsalTimer * timer)118 int32_t OsalTimerStartOnce(OsalTimer *timer)
119 {
120     return OsalStartTimer(timer, LOS_SWTMR_MODE_NO_SELFDELETE);
121 }
122 
OsalTimerSetTimeout(OsalTimer * timer,uint32_t interval)123 int32_t OsalTimerSetTimeout(OsalTimer *timer, uint32_t interval)
124 {
125     struct OsalLitetimer *liteTimer = NULL;
126     uint32_t intSave;
127     uint32_t ret;
128 
129     if (timer == NULL || timer->realTimer == NULL || interval == 0) {
130         HDF_LOGE("%s invalid para", __func__);
131         return HDF_ERR_INVALID_PARAM;
132     }
133 
134     liteTimer = (struct OsalLitetimer *)timer->realTimer;
135     if (liteTimer->timerID == MAX_INVALID_TIMER_VID) {
136         HDF_LOGE("%s timer id invalid %u", __func__, liteTimer->timerID);
137         return HDF_FAILURE;
138     }
139 
140     if (liteTimer->interval == interval) {
141         return HDF_SUCCESS;
142     }
143 
144     liteTimer->interval = interval;
145 
146     intSave = LOS_IntLock();
147     ret = LOS_SwtmrDelete(liteTimer->timerID);
148     if (ret != LOS_OK) {
149         LOS_IntRestore(intSave);
150         HDF_LOGE("%s LOS_SwtmrDelete fail %u", __func__, ret);
151         return HDF_FAILURE;
152     }
153     LOS_IntRestore(intSave);
154 
155     return OsalTimerStartLoop(timer);
156 }
157 
OsalTimerDelete(OsalTimer * timer)158 int32_t OsalTimerDelete(OsalTimer *timer)
159 {
160     uint32_t intSave;
161     uint32_t ret;
162     struct OsalLitetimer *liteTimer = NULL;
163 
164     if (timer == NULL || timer->realTimer == NULL) {
165         HDF_LOGE("%s invalid para", __func__);
166         return HDF_ERR_INVALID_PARAM;
167     }
168 
169     liteTimer = (struct OsalLitetimer *)timer->realTimer;
170     if (liteTimer->timerID == MAX_INVALID_TIMER_VID) {
171         HDF_LOGE("%s timer id invalid %u", __func__, liteTimer->timerID);
172         OsalMemFree(timer->realTimer);
173         timer->realTimer = NULL;
174         return HDF_FAILURE;
175     }
176     intSave = LOS_IntLock();
177     ret = LOS_SwtmrDelete(liteTimer->timerID);
178     if (ret == LOS_OK) {
179         LOS_IntRestore(intSave);
180         OsalMemFree(timer->realTimer);
181         timer->realTimer = NULL;
182         return HDF_SUCCESS;
183     } else {
184         LOS_IntRestore(intSave);
185         HDF_LOGE("%s LOS_SwtmrDelete fail %u", __func__, ret);
186         return HDF_FAILURE;
187     }
188 }
189 
190