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 "watchdog_if.h"
10 #include "securec.h"
11 #include "hdf_io_service_if.h"
12 #include "hdf_log.h"
13 #include "osal_mem.h"
14
15 #define HDF_LOG_TAG watchdog_if_u
16
17 #define WATCHDOG_ID_MAX 8
18 #define WATCHDOG_NAME_LEN 32
19
WatchdogServiceGetById(int16_t wdtId)20 static void *WatchdogServiceGetById(int16_t wdtId)
21 {
22 char serviceName[WATCHDOG_NAME_LEN + 1] = {0};
23 void *service = NULL;
24
25 if (wdtId < 0 || wdtId >= WATCHDOG_ID_MAX) {
26 HDF_LOGE("%s: invalid id:%d", __func__, wdtId);
27 return NULL;
28 }
29
30 if (snprintf_s(serviceName, WATCHDOG_NAME_LEN + 1, WATCHDOG_NAME_LEN,
31 "HDF_PLATFORM_WATCHDOG_%d", wdtId) < 0) {
32 HDF_LOGE("%s: format service name fail!", __func__);
33 return NULL;
34 }
35
36 service = (void *)HdfIoServiceBind(serviceName);
37 if (service == NULL) {
38 HDF_LOGE("%s: get obj fail!", __func__);
39 }
40
41 return service;
42 }
43
WatchdogOpen(int16_t wdtId,DevHandle * handle)44 int32_t WatchdogOpen(int16_t wdtId, DevHandle *handle)
45 {
46 int32_t ret;
47 struct HdfSBuf *reply = NULL;
48 struct HdfIoService *service = NULL;
49
50 if (handle == NULL) {
51 HDF_LOGE("%s: handle null", __func__);
52 return HDF_ERR_INVALID_OBJECT;
53 }
54
55 service = WatchdogServiceGetById(wdtId);
56 if (service == NULL) {
57 HDF_LOGE("%s: get service fail", __func__);
58 return HDF_FAILURE;
59 }
60
61 if (service == NULL ||service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
62 HDF_LOGE("%s: service is invalid", __func__);
63 HdfIoServiceRecycle(service);
64 return HDF_FAILURE;
65 }
66
67 reply = HdfSbufObtainDefaultSize();
68 if (reply == NULL) {
69 HDF_LOGE("%s: failed to obtain reply", __func__);
70 HdfIoServiceRecycle(service);
71 return HDF_ERR_MALLOC_FAIL;
72 }
73
74 ret = service->dispatcher->Dispatch(&service->object, WATCHDOG_IO_GET_PRIV, NULL, reply);
75 if (ret != HDF_SUCCESS) {
76 HDF_LOGE("%s: get priv error, ret %d", __func__, ret);
77 HdfIoServiceRecycle(service);
78 HdfSbufRecycle(reply);
79 return HDF_FAILURE;
80 }
81
82 if (!HdfSbufReadInt32(reply, &ret)) {
83 HDF_LOGE("%s: sbuf read buffer failed", __func__);
84 HdfIoServiceRecycle(service);
85 HdfSbufRecycle(reply);
86 return HDF_ERR_IO;
87 }
88
89 HdfSbufRecycle(reply);
90 *handle = service;
91 return ret;
92 }
93
WatchdogClose(DevHandle handle)94 void WatchdogClose(DevHandle handle)
95 {
96 int32_t ret;
97 struct HdfIoService *service = NULL;
98
99 service = (struct HdfIoService *)handle;
100 if (service == NULL ||service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
101 HDF_LOGE("%s: service is invalid", __func__);
102 HdfIoServiceRecycle((struct HdfIoService *)handle);
103 return;
104 }
105
106 ret = service->dispatcher->Dispatch(&service->object, WATCHDOG_IO_RELEASE_PRIV, NULL, NULL);
107 if (ret != HDF_SUCCESS) {
108 HDF_LOGE("%s: WatchdogReleasePriv error, ret %d", __func__, ret);
109 }
110 HdfIoServiceRecycle((struct HdfIoService *)handle);
111 }
112
WatchdogGetStatus(DevHandle handle,int32_t * status)113 int32_t WatchdogGetStatus(DevHandle handle, int32_t *status)
114 {
115 int32_t ret;
116 struct HdfSBuf *reply = NULL;
117 struct HdfIoService *service = NULL;
118
119 if ((handle == NULL) || (status == NULL)) {
120 HDF_LOGE("%s: param is invalid", __func__);
121 return HDF_ERR_INVALID_OBJECT;
122 }
123
124 service = (struct HdfIoService *)handle;
125 if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
126 HDF_LOGE("%s: service is invalid", __func__);
127 return HDF_ERR_INVALID_OBJECT;
128 }
129
130 reply = HdfSbufObtainDefaultSize();
131 if (reply == NULL) {
132 HDF_LOGE("%s: failed to obtain reply", __func__);
133 return HDF_ERR_MALLOC_FAIL;
134 }
135
136 ret = service->dispatcher->Dispatch(&service->object, WATCHDOG_IO_GET_STATUS, NULL, reply);
137 if (ret != HDF_SUCCESS) {
138 HDF_LOGE("%s: failed to read, ret %d", __func__, ret);
139 HdfSbufRecycle(reply);
140 return HDF_FAILURE;
141 }
142
143 if (!HdfSbufReadInt32(reply, status)) {
144 HDF_LOGE("%s: sbuf read buffer failed", __func__);
145 HdfSbufRecycle(reply);
146 return HDF_ERR_IO;
147 }
148 HdfSbufRecycle(reply);
149 return HDF_SUCCESS;
150 }
151
WatchdogStart(DevHandle handle)152 int32_t WatchdogStart(DevHandle handle)
153 {
154 int32_t ret;
155 struct HdfIoService *service = NULL;
156 if (handle == NULL) {
157 HDF_LOGE("%s: handle is null", __func__);
158 return HDF_ERR_INVALID_OBJECT;
159 }
160
161 service = (struct HdfIoService *)handle;
162 if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
163 HDF_LOGE("%s: service is invalid", __func__);
164 return HDF_ERR_INVALID_OBJECT;
165 }
166
167 ret = service->dispatcher->Dispatch(&service->object, WATCHDOG_IO_START, NULL, NULL);
168 if (ret != HDF_SUCCESS) {
169 HDF_LOGE("%s: WatchdogCntlrStart error, ret %d", __func__, ret);
170 return ret;
171 }
172
173 return HDF_SUCCESS;
174 }
175
WatchdogStop(DevHandle handle)176 int32_t WatchdogStop(DevHandle handle)
177 {
178 int32_t ret;
179 struct HdfIoService *service = NULL;
180 if (handle == NULL) {
181 HDF_LOGE("%s: handle is null", __func__);
182 return HDF_ERR_INVALID_OBJECT;
183 }
184
185 service = (struct HdfIoService *)handle;
186 if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
187 HDF_LOGE("%s: service is invalid", __func__);
188 return HDF_ERR_INVALID_OBJECT;
189 }
190
191 ret = service->dispatcher->Dispatch(&service->object, WATCHDOG_IO_STOP, NULL, NULL);
192 if (ret != HDF_SUCCESS) {
193 HDF_LOGE("%s: WatchdogCntlrStop error, ret %d", __func__, ret);
194 return ret;
195 }
196
197 return HDF_SUCCESS;
198 }
199
WatchdogSetTimeout(DevHandle handle,uint32_t seconds)200 int32_t WatchdogSetTimeout(DevHandle handle, uint32_t seconds)
201 {
202 int32_t ret;
203 struct HdfSBuf *data = NULL;
204 struct HdfIoService *service = NULL;
205
206 service = (struct HdfIoService *)handle;
207 if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
208 HDF_LOGE("%s: service is invalid", __func__);
209 return HDF_ERR_INVALID_OBJECT;
210 }
211
212 data = HdfSbufObtainDefaultSize();
213 if (data == NULL) {
214 HDF_LOGE("%s: failed to obtain data", __func__);
215 return HDF_ERR_MALLOC_FAIL;
216 }
217
218 if (!HdfSbufWriteUint32(data, seconds)) {
219 HDF_LOGE("%s: sbuf write seconds failed", __func__);
220 HdfSbufRecycle(data);
221 return HDF_ERR_IO;
222 }
223
224 ret = service->dispatcher->Dispatch(&service->object, WATCHDOG_IO_SET_TIMEOUT, data, NULL);
225 if (ret != HDF_SUCCESS) {
226 HDF_LOGE("%s: failed to set timeout, ret %d", __func__, ret);
227 HdfSbufRecycle(data);
228 return HDF_FAILURE;
229 }
230 HdfSbufRecycle(data);
231 return HDF_SUCCESS;
232 }
233
WatchdogGetTimeout(DevHandle handle,uint32_t * seconds)234 int32_t WatchdogGetTimeout(DevHandle handle, uint32_t *seconds)
235 {
236 int32_t ret;
237 struct HdfSBuf *reply = NULL;
238 struct HdfIoService *service = NULL;
239
240 if ((handle == NULL) || (seconds == NULL)) {
241 HDF_LOGE("%s: param is invalid", __func__);
242 return HDF_ERR_INVALID_OBJECT;
243 }
244
245 service = (struct HdfIoService *)handle;
246 if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
247 HDF_LOGE("%s: service is invalid", __func__);
248 return HDF_ERR_INVALID_OBJECT;
249 }
250
251 reply = HdfSbufObtainDefaultSize();
252 if (reply == NULL) {
253 HDF_LOGE("%s: failed to obtain reply", __func__);
254 return HDF_ERR_MALLOC_FAIL;
255 }
256
257 ret = service->dispatcher->Dispatch(&service->object, WATCHDOG_IO_GET_TIMEOUT, NULL, reply);
258 if (ret != HDF_SUCCESS) {
259 HDF_LOGE("%s: failed to read, ret %d", __func__, ret);
260 HdfSbufRecycle(reply);
261 return HDF_FAILURE;
262 }
263
264 if (!HdfSbufReadUint32(reply, seconds)) {
265 HDF_LOGE("%s: sbuf read buffer failed", __func__);
266 HdfSbufRecycle(reply);
267 return HDF_ERR_IO;
268 }
269 HdfSbufRecycle(reply);
270 return HDF_SUCCESS;
271 }
272
WatchdogFeed(DevHandle handle)273 int32_t WatchdogFeed(DevHandle handle)
274 {
275 int32_t ret;
276 struct HdfIoService *service = NULL;
277
278 service = (struct HdfIoService *)handle;
279 if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
280 HDF_LOGE("%s: service is invalid", __func__);
281 return HDF_ERR_INVALID_OBJECT;
282 }
283
284 ret = service->dispatcher->Dispatch(&service->object, WATCHDOG_IO_FEED, NULL, NULL);
285 if (ret != HDF_SUCCESS) {
286 HDF_LOGE("%s: WatchdogCntlrFeed error, ret %d", __func__, ret);
287 return ret;
288 }
289
290 return HDF_SUCCESS;
291 }
292