• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "hdf_log.h"
33 #include "los_interrupt.h"
34 #include "los_swtmr.h"
35 #include "osal_mem.h"
36 
37 #define HDF_LOG_TAG osal_timer
38 #define OSAL_INVALID_TIMER_ID UINT32_MAX
39 
40 struct OsalLitetimer {
41     uintptr_t arg;
42     uint32_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 = OSAL_INVALID_TIMER_ID;
64     liteTimer->arg = arg;
65     liteTimer->func = func;
66     liteTimer->interval = interval;
67     timer->realTimer = (void *)liteTimer;
68 
69     return HDF_SUCCESS;
70 }
OsalStartTimer(OsalTimer * timer,UINT8 mode)71 static int32_t OsalStartTimer(OsalTimer *timer, UINT8 mode)
72 {
73     uint32_t ret;
74     uint32_t intSave;
75     uint32_t interval;
76     uint32_t timerID = 0;
77     struct OsalLitetimer *liteTimer = NULL;
78 
79     if (timer == NULL || timer->realTimer == NULL) {
80         HDF_LOGE("%s invalid para %d", __func__, __LINE__);
81         return HDF_ERR_INVALID_PARAM;
82     }
83 
84     liteTimer = (struct OsalLitetimer *)timer->realTimer;
85     if (liteTimer->interval == 0 || liteTimer->func == NULL) {
86         HDF_LOGE("%s invalid para %d", __func__, __LINE__);
87         return HDF_ERR_INVALID_PARAM;
88     }
89 
90     interval = liteTimer->interval;
91     intSave = LOS_IntLock();
92 #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
93     ret = LOS_SwtmrCreate(interval, mode, (SWTMR_PROC_FUNC)liteTimer->func, &timerID, liteTimer->arg,
94         OS_SWTMR_ROUSES_IGNORE, OS_SWTMR_ALIGN_INSENSITIVE);
95 #else
96     ret = LOS_SwtmrCreate(interval, mode, (SWTMR_PROC_FUNC)liteTimer->func, &timerID, liteTimer->arg);
97 #endif
98     if (ret != LOS_OK) {
99         LOS_IntRestore(intSave);
100         HDF_LOGE("%s LOS_SwtmrCreate fail %u", __func__, ret);
101         return HDF_FAILURE;
102     }
103 
104     ret = LOS_SwtmrStart(timerID);
105     if (ret != LOS_OK) {
106         LOS_SwtmrDelete(timerID);
107         LOS_IntRestore(intSave);
108         HDF_LOGE("%s LOS_SwtmrStart fail %u", __func__, ret);
109         return HDF_FAILURE;
110     }
111     LOS_IntRestore(intSave);
112 
113     liteTimer->timerID = timerID;
114     return HDF_SUCCESS;
115 }
116 
OsalTimerStartLoop(OsalTimer * timer)117 int32_t OsalTimerStartLoop(OsalTimer *timer)
118 {
119     return OsalStartTimer(timer, LOS_SWTMR_MODE_PERIOD);
120 }
121 
OsalTimerStartOnce(OsalTimer * timer)122 int32_t OsalTimerStartOnce(OsalTimer *timer)
123 {
124     return OsalStartTimer(timer, LOS_SWTMR_MODE_NO_SELFDELETE);
125 }
126 
OsalTimerSetTimeout(OsalTimer * timer,uint32_t interval)127 int32_t OsalTimerSetTimeout(OsalTimer *timer, uint32_t interval)
128 {
129     struct OsalLitetimer *liteTimer = NULL;
130     uint32_t intSave;
131     uint32_t ret;
132 
133     if (timer == NULL || timer->realTimer == NULL || interval == 0) {
134         HDF_LOGE("%s invalid para", __func__);
135         return HDF_ERR_INVALID_PARAM;
136     }
137 
138     liteTimer = (struct OsalLitetimer *)timer->realTimer;
139     if (liteTimer->timerID == OSAL_INVALID_TIMER_ID) {
140         HDF_LOGE("%s timer id invalid %u", __func__, liteTimer->timerID);
141         return HDF_FAILURE;
142     }
143 
144     if (liteTimer->interval == interval) {
145         return HDF_SUCCESS;
146     }
147 
148     liteTimer->interval = interval;
149 
150     intSave = LOS_IntLock();
151     ret = LOS_SwtmrDelete(liteTimer->timerID);
152     if (ret != LOS_OK) {
153         LOS_IntRestore(intSave);
154         HDF_LOGE("%s LOS_SwtmrDelete fail %u", __func__, ret);
155         return HDF_FAILURE;
156     }
157     LOS_IntRestore(intSave);
158 
159     return OsalTimerStartLoop(timer);
160 }
161 
OsalTimerDelete(OsalTimer * timer)162 int32_t OsalTimerDelete(OsalTimer *timer)
163 {
164     uint32_t intSave;
165     uint32_t ret;
166     struct OsalLitetimer *liteTimer = NULL;
167 
168     if (timer == NULL || timer->realTimer == NULL) {
169         HDF_LOGE("%s invalid para", __func__);
170         return HDF_ERR_INVALID_PARAM;
171     }
172 
173     liteTimer = (struct OsalLitetimer *)timer->realTimer;
174     if (liteTimer->timerID == OSAL_INVALID_TIMER_ID) {
175         HDF_LOGE("%s timer id invalid %u", __func__, liteTimer->timerID);
176         OsalMemFree(timer->realTimer);
177         timer->realTimer = NULL;
178         return HDF_FAILURE;
179     }
180     intSave = LOS_IntLock();
181     ret = LOS_SwtmrDelete(liteTimer->timerID);
182     if (ret == LOS_OK) {
183         LOS_IntRestore(intSave);
184         OsalMemFree(timer->realTimer);
185         timer->realTimer = NULL;
186         return HDF_SUCCESS;
187     } else {
188         LOS_IntRestore(intSave);
189         HDF_LOGE("%s LOS_SwtmrDelete fail %u", __func__, ret);
190         return HDF_FAILURE;
191     }
192 }
193