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