• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "timer_if.h"
10 #include "hdf_io_service_if.h"
11 #include "hdf_log.h"
12 #include "osal_mem.h"
13 #include "securec.h"
14 
15 #define HDF_LOG_TAG timer_if_u
16 
17 #define TIMER_SERVICE_NAME "HDF_PLATFORM_TIMER_MANAGER"
TimerManagerGetService(void)18 static void *TimerManagerGetService(void)
19 {
20     static void *manager = NULL;
21 
22     if (manager != NULL) {
23         return manager;
24     }
25     manager = (void *)HdfIoServiceBind(TIMER_SERVICE_NAME);
26     if (manager == NULL) {
27         HDF_LOGE("%s: fail to get timer manager!", __func__);
28     }
29     return manager;
30 }
31 
HwTimerOpen(const uint32_t number)32 DevHandle HwTimerOpen(const uint32_t number)
33 {
34     int32_t ret;
35     struct HdfIoService *service = NULL;
36     struct HdfSBuf *data = NULL;
37     struct HdfSBuf *reply = NULL;
38     uint32_t handle;
39 
40     service = (struct HdfIoService *)TimerManagerGetService();
41     if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
42         HDF_LOGE("%s: service is invalid", __func__);
43         return NULL;
44     }
45 
46     data = HdfSbufObtainDefaultSize();
47     if (data == NULL) {
48         return NULL;
49     }
50     reply = HdfSbufObtainDefaultSize();
51     if (reply == NULL) {
52         HdfSbufRecycle(data);
53         return NULL;
54     }
55 
56     if (!HdfSbufWriteUint16(data, (uint16_t)number)) {
57         HDF_LOGE("%s: write number fail!", __func__);
58         HdfSbufRecycle(data);
59         HdfSbufRecycle(reply);
60         return NULL;
61     }
62 
63     ret = service->dispatcher->Dispatch(&service->object, TIMER_IO_OPEN, data, reply);
64     if (ret != HDF_SUCCESS) {
65         HDF_LOGE("%s: TIMER_IO_OPEN service process fail:%d", __func__, ret);
66         HdfSbufRecycle(data);
67         HdfSbufRecycle(reply);
68         return NULL;
69     }
70 
71     if (!HdfSbufReadUint32(reply, &handle)) {
72         HDF_LOGE("%s: read reply fail!", __func__);
73         HdfSbufRecycle(data);
74         HdfSbufRecycle(reply);
75         return NULL;
76     }
77     HdfSbufRecycle(data);
78     HdfSbufRecycle(reply);
79     return (DevHandle)(uintptr_t)handle;
80 }
81 
HwTimerClose(DevHandle handle)82 void HwTimerClose(DevHandle handle)
83 {
84     int32_t ret;
85     struct HdfIoService *service = NULL;
86     struct HdfSBuf *data = NULL;
87 
88     if (handle == NULL) {
89         HDF_LOGE("%s: handle is invalid", __func__);
90         return;
91     }
92 
93     service = (struct HdfIoService *)TimerManagerGetService();
94     if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
95         HDF_LOGE("%s: service is invalid", __func__);
96         return;
97     }
98 
99     data = HdfSbufObtainDefaultSize();
100     if (data == NULL) {
101         return;
102     }
103 
104     if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
105         HDF_LOGE("%s: write handle fail!", __func__);
106         HdfSbufRecycle(data);
107         return;
108     }
109 
110     ret = service->dispatcher->Dispatch(&service->object, TIMER_IO_CLOSE, data, NULL);
111     if (ret != HDF_SUCCESS) {
112         HDF_LOGE("%s: TIMER_IO_CLOSE service process fail:%d", __func__, ret);
113     }
114     HdfSbufRecycle(data);
115 }
116 
HwTimerStart(DevHandle handle)117 int32_t HwTimerStart(DevHandle handle)
118 {
119     int32_t ret;
120     struct HdfIoService *service = NULL;
121     struct HdfSBuf *data = NULL;
122 
123     if (handle == NULL) {
124         HDF_LOGE("%s: handle is invalid", __func__);
125         return HDF_FAILURE;
126     }
127     service = (struct HdfIoService *)TimerManagerGetService();
128     if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
129         HDF_LOGE("%s: service is invalid", __func__);
130         return HDF_ERR_INVALID_PARAM;
131     }
132 
133     data = HdfSbufObtainDefaultSize();
134     if (data == NULL) {
135         HDF_LOGE("%s: HdfSBufObtainDefaultSize fail!", __func__);
136         return HDF_FAILURE;
137     }
138 
139     if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
140         HDF_LOGE("%s: write handle fail!", __func__);
141         HdfSbufRecycle(data);
142         return HDF_FAILURE;
143     }
144 
145     ret = service->dispatcher->Dispatch(&service->object, TIMER_IO_START, data, NULL);
146     if (ret != HDF_SUCCESS) {
147         HDF_LOGE("%s: TIMER_IO_START service process fail:%d", __func__, ret);
148         HdfSbufRecycle(data);
149         return HDF_FAILURE;
150     }
151     HdfSbufRecycle(data);
152 
153     return HDF_SUCCESS;
154 }
155 
HwTimerStop(DevHandle handle)156 int32_t HwTimerStop(DevHandle handle)
157 {
158     int32_t ret;
159     struct HdfIoService *service = NULL;
160     struct HdfSBuf *data = NULL;
161 
162     if (handle == NULL) {
163         HDF_LOGE("%s: handle is invalid", __func__);
164         return HDF_FAILURE;
165     }
166 
167     service = (struct HdfIoService *)TimerManagerGetService();
168     if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
169         HDF_LOGE("%s: service is invalid", __func__);
170         return HDF_ERR_INVALID_PARAM;
171     }
172 
173     data = HdfSbufObtainDefaultSize();
174     if (data == NULL) {
175         HDF_LOGE("%s: HdfSbufObtainDefaultSize fail!", __func__);
176         return HDF_FAILURE;
177     }
178 
179     if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
180         HDF_LOGE("%s: write handle fail!", __func__);
181         HdfSbufRecycle(data);
182         return HDF_FAILURE;
183     }
184 
185     ret = service->dispatcher->Dispatch(&service->object, TIMER_IO_STOP, data, NULL);
186     if (ret != HDF_SUCCESS) {
187         HDF_LOGE("%s: TIMER_IO_STOP service process fail:%d", __func__, ret);
188         HdfSbufRecycle(data);
189         return HDF_FAILURE;
190     }
191     HdfSbufRecycle(data);
192 
193     return HDF_SUCCESS;
194 }
195 
HwTimerSet(DevHandle handle,uint32_t useconds,TimerHandleCb cb)196 int32_t HwTimerSet(DevHandle handle, uint32_t useconds, TimerHandleCb cb)
197 {
198     int32_t ret;
199     struct HdfIoService *service = NULL;
200     struct HdfSBuf *buf = NULL;
201     struct TimerConfig cfg;
202 
203     if (handle == NULL) {
204         HDF_LOGE("%s: handle is invalid", __func__);
205         return HDF_FAILURE;
206     }
207 
208     service = (struct HdfIoService *)TimerManagerGetService();
209     if (service == NULL || service->dispatcher == NULL
210         || service->dispatcher->Dispatch == NULL || cb == NULL) {
211         HDF_LOGE("%s:param is invalid", __func__);
212         return HDF_ERR_INVALID_PARAM;
213     }
214 
215     if (memset_s(&cfg, sizeof(cfg), 0, sizeof(cfg)) != EOK) {
216         HDF_LOGE("%s:memset_s FAIL", __func__);
217         return HDF_ERR_IO;
218     }
219     cfg.useconds = useconds;
220 
221     buf = HdfSbufObtainDefaultSize();
222     if (buf == NULL) {
223         HDF_LOGE("%s: failed to obtain buf", __func__);
224         return HDF_ERR_MALLOC_FAIL;
225     }
226     if (!HdfSbufWriteUint32(buf, (uint32_t)(uintptr_t)handle)) {
227         HDF_LOGE("%s: sbuf write handle failed", __func__);
228         return HDF_ERR_IO;
229     }
230     if (!HdfSbufWriteBuffer(buf, &cfg, sizeof(cfg))) {
231         HDF_LOGE("%s: sbuf write cfg failed", __func__);
232         HdfSbufRecycle(buf);
233         return HDF_ERR_IO;
234     }
235 
236     ret = service->dispatcher->Dispatch(&service->object, TIMER_IO_SET, buf, NULL);
237     if (ret != HDF_SUCCESS) {
238         HDF_LOGE("%s: TIMER_IO_SET service process fail:%d", __func__, ret);
239         HdfSbufRecycle(buf);
240         return HDF_FAILURE;
241     }
242     HdfSbufRecycle(buf);
243 
244     return HDF_SUCCESS;
245 }
246 
HwTimerSetOnce(DevHandle handle,uint32_t useconds,TimerHandleCb cb)247 int32_t HwTimerSetOnce(DevHandle handle, uint32_t useconds, TimerHandleCb cb)
248 {
249     int32_t ret;
250     struct HdfIoService *service = NULL;
251     struct HdfSBuf *buf = NULL;
252     struct TimerConfig cfg;
253 
254     if (handle == NULL) {
255         HDF_LOGE("%s: handle is invalid", __func__);
256         return HDF_FAILURE;
257     }
258 
259     service = (struct HdfIoService *)TimerManagerGetService();
260     if (service == NULL || service->dispatcher == NULL
261         || service->dispatcher->Dispatch == NULL || cb == NULL) {
262         HDF_LOGE("%s: service is invalid", __func__);
263         return HDF_ERR_INVALID_PARAM;
264     }
265 
266     if (memset_s(&cfg, sizeof(cfg), 0, sizeof(cfg)) != EOK) {
267         HDF_LOGE("%s:memset_s FAIL", __func__);
268         return HDF_ERR_IO;
269     }
270     cfg.useconds = useconds;
271 
272     buf = HdfSbufObtainDefaultSize();
273     if (buf == NULL) {
274         HDF_LOGE("%s: failed to obtain buf", __func__);
275         return HDF_ERR_MALLOC_FAIL;
276     }
277     if (!HdfSbufWriteUint32(buf, (uint32_t)(uintptr_t)handle)) {
278         HDF_LOGE("%s: sbuf write handle failed", __func__);
279         return HDF_ERR_IO;
280     }
281     if (!HdfSbufWriteBuffer(buf, &cfg, sizeof(cfg))) {
282         HDF_LOGE("%s: sbuf write cfg failed", __func__);
283         HdfSbufRecycle(buf);
284         return HDF_ERR_IO;
285     }
286 
287     ret = service->dispatcher->Dispatch(&service->object, TIMER_IO_SETONCE, buf, NULL);
288     if (ret != HDF_SUCCESS) {
289         HDF_LOGE("%s: TIMER_IO_SETONCE service process fail:%d", __func__, ret);
290         HdfSbufRecycle(buf);
291         return HDF_FAILURE;
292     }
293     HdfSbufRecycle(buf);
294 
295     return HDF_SUCCESS;
296 }
297 
HwTimerGet(DevHandle handle,uint32_t * useconds,bool * isPeriod)298 int32_t HwTimerGet(DevHandle handle, uint32_t *useconds, bool *isPeriod)
299 {
300     int32_t ret = HDF_SUCCESS;
301     struct HdfIoService *service = NULL;
302     struct HdfSBuf *data = NULL;
303     const void *rBuf = NULL;
304     struct HdfSBuf *reply = NULL;
305     struct TimerConfig cfg;
306     uint32_t rLen;
307 
308     if ((handle == NULL) || (useconds == NULL) || (isPeriod == NULL)) {
309         HDF_LOGE("%s: param is invalid", __func__);
310         return HDF_FAILURE;
311     }
312 
313     service = (struct HdfIoService *)TimerManagerGetService();
314     if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
315         HDF_LOGE("%s: service is invalid", __func__);
316         return HDF_ERR_INVALID_PARAM;
317     }
318 
319     data = HdfSbufObtainDefaultSize();
320     if (data == NULL) {
321         HDF_LOGE("%s: HdfSBufObtainDefaultSize fail!", __func__);
322         return HDF_FAILURE;
323     }
324 
325     reply = HdfSbufObtainDefaultSize();
326     if (reply == NULL) {
327         HDF_LOGE("%s: failed to obtain reply", __func__);
328         ret = HDF_ERR_MALLOC_FAIL;
329         goto __EXIT;
330     }
331 
332     if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
333         HDF_LOGE("%s: write handle fail!", __func__);
334         ret =  HDF_FAILURE;
335         goto __EXIT;
336     }
337 
338     ret = service->dispatcher->Dispatch(&service->object, TIMER_IO_GET, data, reply);
339     if (ret != HDF_SUCCESS) {
340         HDF_LOGE("%s: TIMER_IO_GET service process fail:%d", __func__, ret);
341         ret =  HDF_FAILURE;
342         goto __EXIT;
343     }
344 
345     if (!HdfSbufReadBuffer(reply, &rBuf, &rLen)) {
346         HDF_LOGE("%s: sbuf read buffer failed", __func__);
347         ret = HDF_ERR_IO;
348         goto __EXIT;
349     }
350     if (rLen != sizeof(struct TimerConfig)) {
351         HDF_LOGE("%s: sbuf read buffer len error %u != %zu", __func__, rLen, sizeof(struct TimerConfig));
352         ret = HDF_FAILURE;
353         goto __EXIT;
354     }
355     if (memcpy_s(&cfg, sizeof(struct TimerConfig), rBuf, rLen) != EOK) {
356         HDF_LOGE("%s: memcpy rBuf failed", __func__);
357         ret = HDF_ERR_IO;
358         goto __EXIT;
359     }
360 
361     *useconds = cfg.useconds;
362     *isPeriod = cfg.isPeriod;
363 
364 __EXIT:
365         HdfSbufRecycle(data);
366         HdfSbufRecycle(reply);
367         return ret;
368 }
369