1 /*
2 * Copyright (c) 2020-2021 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 return NULL;
34 }
35 if (snprintf_s(serviceName, WATCHDOG_NAME_LEN + 1, WATCHDOG_NAME_LEN,
36 "HDF_PLATFORM_WATCHDOG_%d", wdtId) < 0) {
37 HDF_LOGE("WatchdogGetById: format service name fail!");
38 OsalMemFree(serviceName);
39 return NULL;
40 }
41 obj = (struct WatchdogCntlr *)DevSvcManagerClntGetService(serviceName);
42 if (obj == NULL) {
43 HDF_LOGE("WatchdogGetById: get obj fail!");
44 }
45 OsalMemFree(serviceName);
46 return obj;
47 }
48
WatchdogOpen(int16_t wdtId,DevHandle * handle)49 int32_t WatchdogOpen(int16_t wdtId, DevHandle *handle)
50 {
51 struct WatchdogCntlr *service = NULL;
52 int32_t ret;
53
54 if (handle == NULL) {
55 HDF_LOGE("%s: handle null", __func__);
56 return HDF_ERR_INVALID_OBJECT;
57 }
58
59 service = WatchdogGetById(wdtId);
60 if (service == NULL) {
61 *handle = NULL;
62 return HDF_ERR_INVALID_OBJECT;
63 }
64
65 ret = WatchdogGetPrivData(service);
66 if (ret == HDF_SUCCESS) {
67 *handle = (DevHandle)service;
68 return ret;
69 }
70
71 return ret;
72 }
73
WatchdogClose(DevHandle handle)74 void WatchdogClose(DevHandle handle)
75 {
76 if (handle == NULL) {
77 HDF_LOGE("%s handle null", __func__);
78 return;
79 }
80 if (WatchdogReleasePriv((struct WatchdogCntlr *)handle) != HDF_SUCCESS) {
81 HDF_LOGE("%s WatchdogReleasePriv fail", __func__);
82 return;
83 }
84 }
85
WatchdogGetStatus(DevHandle handle,int32_t * status)86 int32_t WatchdogGetStatus(DevHandle handle, int32_t *status)
87 {
88 if (handle == NULL) {
89 return HDF_ERR_INVALID_OBJECT;
90 }
91 return WatchdogCntlrGetStatus((struct WatchdogCntlr *)handle, status);
92 }
93
WatchdogStart(DevHandle handle)94 int32_t WatchdogStart(DevHandle handle)
95 {
96 if (handle == NULL) {
97 return HDF_ERR_INVALID_OBJECT;
98 }
99 return WatchdogCntlrStart((struct WatchdogCntlr *)handle);
100 }
101
WatchdogStop(DevHandle handle)102 int32_t WatchdogStop(DevHandle handle)
103 {
104 if (handle == NULL) {
105 return HDF_ERR_INVALID_OBJECT;
106 }
107 return WatchdogCntlrStop((struct WatchdogCntlr *)handle);
108 }
109
WatchdogSetTimeout(DevHandle handle,uint32_t seconds)110 int32_t WatchdogSetTimeout(DevHandle handle, uint32_t seconds)
111 {
112 if (handle == NULL) {
113 return HDF_ERR_INVALID_OBJECT;
114 }
115 return WatchdogCntlrSetTimeout((struct WatchdogCntlr *)handle, seconds);
116 }
117
WatchdogGetTimeout(DevHandle handle,uint32_t * seconds)118 int32_t WatchdogGetTimeout(DevHandle handle, uint32_t *seconds)
119 {
120 if (handle == NULL) {
121 return HDF_ERR_INVALID_OBJECT;
122 }
123 return WatchdogCntlrGetTimeout((struct WatchdogCntlr *)handle, seconds);
124 }
125
WatchdogFeed(DevHandle handle)126 int32_t WatchdogFeed(DevHandle handle)
127 {
128 if (handle == NULL) {
129 return HDF_ERR_INVALID_OBJECT;
130 }
131 return WatchdogCntlrFeed((struct WatchdogCntlr *)handle);
132 }
133