1 /*
2 * Copyright (c) 2020-2023 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 "devsvc_manager_clnt.h"
11 #include "hdf_base.h"
12 #include "hdf_log.h"
13 #include "osal_mem.h"
14 #include "securec.h"
15 #include "watchdog_core.h"
16
17 #define HDF_LOG_TAG watchdog_if
18
19 #define WATCHDOG_ID_MAX 8
20 #define WATCHDOG_NAME_LEN 32
21
WatchdogGetById(int16_t wdtId)22 static struct WatchdogCntlr *WatchdogGetById(int16_t wdtId)
23 {
24 char *serviceName = NULL;
25 struct WatchdogCntlr *obj = NULL;
26
27 if (wdtId < 0 || wdtId >= WATCHDOG_ID_MAX) {
28 HDF_LOGE("WatchdogGetById: invalid id:%d", wdtId);
29 return NULL;
30 }
31 serviceName = OsalMemCalloc(WATCHDOG_NAME_LEN + 1);
32 if (serviceName == NULL) {
33 HDF_LOGE("WatchdogGetById: memcalloc fail!");
34 return NULL;
35 }
36 if (snprintf_s(serviceName, WATCHDOG_NAME_LEN + 1, WATCHDOG_NAME_LEN,
37 "HDF_PLATFORM_WATCHDOG_%d", wdtId) < 0) {
38 HDF_LOGE("WatchdogGetById: format service name fail!");
39 OsalMemFree(serviceName);
40 return NULL;
41 }
42 obj = (struct WatchdogCntlr *)DevSvcManagerClntGetService(serviceName);
43 if (obj == NULL) {
44 HDF_LOGE("WatchdogGetById: get obj fail!");
45 }
46 OsalMemFree(serviceName);
47 return obj;
48 }
49
WatchdogOpen(int16_t wdtId,DevHandle * handle)50 int32_t WatchdogOpen(int16_t wdtId, DevHandle *handle)
51 {
52 struct WatchdogCntlr *service = NULL;
53 int32_t ret;
54
55 if (handle == NULL) {
56 HDF_LOGE("WatchdogOpen: handle is null!");
57 return HDF_ERR_INVALID_OBJECT;
58 }
59
60 service = WatchdogGetById(wdtId);
61 if (service == NULL) {
62 *handle = NULL;
63 HDF_LOGE("WatchdogOpen: get watchdog fail!");
64 return HDF_ERR_INVALID_OBJECT;
65 }
66
67 ret = WatchdogGetPrivData(service);
68 if (ret == HDF_SUCCESS) {
69 *handle = (DevHandle)service;
70 HDF_LOGE("WatchdogOpen: get watchdog priv data fail!");
71 return ret;
72 }
73
74 return ret;
75 }
76
WatchdogClose(DevHandle handle)77 void WatchdogClose(DevHandle handle)
78 {
79 if (handle == NULL) {
80 HDF_LOGE("WatchdogClose: handle is null!");
81 return;
82 }
83 if (WatchdogReleasePriv((struct WatchdogCntlr *)handle) != HDF_SUCCESS) {
84 HDF_LOGE("WatchdogClose: release watchdog priv fail!");
85 return;
86 }
87 }
88
WatchdogGetStatus(DevHandle handle,int32_t * status)89 int32_t WatchdogGetStatus(DevHandle handle, int32_t *status)
90 {
91 if (handle == NULL) {
92 HDF_LOGE("WatchdogGetStatus: handle is null!");
93 return HDF_ERR_INVALID_OBJECT;
94 }
95 return WatchdogCntlrGetStatus((struct WatchdogCntlr *)handle, status);
96 }
97
WatchdogStart(DevHandle handle)98 int32_t WatchdogStart(DevHandle handle)
99 {
100 if (handle == NULL) {
101 HDF_LOGE("WatchdogStart: handle is null!");
102 return HDF_ERR_INVALID_OBJECT;
103 }
104 return WatchdogCntlrStart((struct WatchdogCntlr *)handle);
105 }
106
WatchdogStop(DevHandle handle)107 int32_t WatchdogStop(DevHandle handle)
108 {
109 if (handle == NULL) {
110 HDF_LOGE("WatchdogStop: handle is null!");
111 return HDF_ERR_INVALID_OBJECT;
112 }
113 return WatchdogCntlrStop((struct WatchdogCntlr *)handle);
114 }
115
WatchdogSetTimeout(DevHandle handle,uint32_t seconds)116 int32_t WatchdogSetTimeout(DevHandle handle, uint32_t seconds)
117 {
118 if (handle == NULL) {
119 HDF_LOGE("WatchdogSetTimeout: handle is null!");
120 return HDF_ERR_INVALID_OBJECT;
121 }
122 return WatchdogCntlrSetTimeout((struct WatchdogCntlr *)handle, seconds);
123 }
124
WatchdogGetTimeout(DevHandle handle,uint32_t * seconds)125 int32_t WatchdogGetTimeout(DevHandle handle, uint32_t *seconds)
126 {
127 if (handle == NULL) {
128 HDF_LOGE("WatchdogGetTimeout: handle is null!");
129 return HDF_ERR_INVALID_OBJECT;
130 }
131 return WatchdogCntlrGetTimeout((struct WatchdogCntlr *)handle, seconds);
132 }
133
WatchdogFeed(DevHandle handle)134 int32_t WatchdogFeed(DevHandle handle)
135 {
136 if (handle == NULL) {
137 HDF_LOGE("WatchdogFeed: handle is null!");
138 return HDF_ERR_INVALID_OBJECT;
139 }
140 return WatchdogCntlrFeed((struct WatchdogCntlr *)handle);
141 }
142