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_test.h"
10 #include "device_resource_if.h"
11 #include "hdf_base.h"
12 #include "hdf_device_desc.h"
13 #include "hdf_log.h"
14
15 #define HDF_LOG_TAG watchdog_driver_test_c
16
17 static struct WatchdogTestConfig g_config;
18
WatchdogTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)19 static int32_t WatchdogTestDispatch(struct HdfDeviceIoClient *client, int cmd,
20 struct HdfSBuf *data, struct HdfSBuf *reply)
21 {
22 if (cmd == 0) {
23 if (reply == NULL) {
24 HDF_LOGE("%s: reply is null!", __func__);
25 return HDF_ERR_INVALID_PARAM;
26 }
27 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
28 HDF_LOGE("%s: write reply failed", __func__);
29 return HDF_ERR_IO;
30 }
31 } else {
32 return HDF_ERR_NOT_SUPPORT;
33 }
34
35 return HDF_SUCCESS;
36 }
37
WatchdogTestReadConfig(struct WatchdogTestConfig * config,const struct DeviceResourceNode * node)38 static int32_t WatchdogTestReadConfig(struct WatchdogTestConfig *config, const struct DeviceResourceNode *node)
39 {
40 int32_t ret;
41 struct DeviceResourceIface *drsOps = NULL;
42 uint32_t temp;
43
44 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
45 if (drsOps == NULL || drsOps->GetUint32 == NULL) {
46 HDF_LOGE("%s: invalid drs ops", __func__);
47 return HDF_FAILURE;
48 }
49
50 ret = drsOps->GetUint32(node, "id", &temp, 0);
51 if (ret != HDF_SUCCESS) {
52 HDF_LOGE("%s: read id failed", __func__);
53 return ret;
54 }
55 config->id = temp;
56
57 ret = drsOps->GetUint32(node, "timeoutSet", &config->timeoutSet, 0);
58 if (ret != HDF_SUCCESS) {
59 HDF_LOGE("%s: read timeoutSet failed", __func__);
60 return ret;
61 }
62
63 ret = drsOps->GetUint32(node, "feedTime", &config->feedTime, 0);
64 if (ret != HDF_SUCCESS) {
65 HDF_LOGE("%s: read feedTime failed", __func__);
66 return ret;
67 }
68
69 return HDF_SUCCESS;
70 }
71
WatchdogTestBind(struct HdfDeviceObject * device)72 static int32_t WatchdogTestBind(struct HdfDeviceObject *device)
73 {
74 int32_t ret;
75 static struct IDeviceIoService service;
76
77 if (device == NULL || device->property == NULL) {
78 HDF_LOGE("%s: device or config is null!", __func__);
79 return HDF_ERR_IO;
80 }
81 ret = WatchdogTestReadConfig(&g_config, device->property);
82 if (ret != HDF_SUCCESS) {
83 HDF_LOGE("%s: read config failed", __func__);
84 return ret;
85 }
86 service.Dispatch = WatchdogTestDispatch;
87 device->service = &service;
88 return HDF_SUCCESS;
89 }
90
WatchdogTestInit(struct HdfDeviceObject * device)91 static int32_t WatchdogTestInit(struct HdfDeviceObject *device)
92 {
93 (void)device;
94 return HDF_SUCCESS;
95 }
96
WatchdogTestRelease(struct HdfDeviceObject * device)97 static void WatchdogTestRelease(struct HdfDeviceObject *device)
98 {
99 if (device != NULL) {
100 device->service = NULL;
101 }
102 HDF_LOGI("%s: Done!", __func__);
103 return;
104 }
105
106 struct HdfDriverEntry g_watchdogTestEntry = {
107 .moduleVersion = 1,
108 .Bind = WatchdogTestBind,
109 .Init = WatchdogTestInit,
110 .Release = WatchdogTestRelease,
111 .moduleName = "PLATFORM_WATCHDOG_TEST",
112 };
113 HDF_INIT(g_watchdogTestEntry);