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 "timer_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 timer_driver_test_c
16
17 static struct TimerTestConfig g_config;
18
TimerTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)19 static int32_t TimerTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
20 {
21 (void)client;
22 (void)data;
23 if (cmd == 0) {
24 if (reply == NULL) {
25 HDF_LOGE("TimerTestDispatch: reply is null!");
26 return HDF_ERR_INVALID_PARAM;
27 }
28 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
29 HDF_LOGE("TimerTestDispatch: write reply fail!");
30 return HDF_ERR_IO;
31 }
32 } else {
33 HDF_LOGE("TimerTestDispatch: cmd: %d is not support!", cmd);
34 return HDF_ERR_NOT_SUPPORT;
35 }
36
37 return HDF_SUCCESS;
38 }
39
TimerTestReadConfig(struct TimerTestConfig * config,const struct DeviceResourceNode * node)40 static int32_t TimerTestReadConfig(struct TimerTestConfig *config, const struct DeviceResourceNode *node)
41 {
42 int32_t ret;
43 struct DeviceResourceIface *face = NULL;
44
45 face = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
46 if (face == NULL) {
47 HDF_LOGE("TimerTestReadConfig: face is null!");
48 return HDF_FAILURE;
49 }
50 if (face->GetUint32 == NULL) {
51 HDF_LOGE("TimerTestReadConfig: GetUint32 not support!");
52 return HDF_ERR_NOT_SUPPORT;
53 }
54
55 ret = face->GetUint32(node, "number", &config->number, 0);
56 if (ret != HDF_SUCCESS) {
57 HDF_LOGE("TimerTestReadConfig: read id fail, ret: %d!", ret);
58 return ret;
59 }
60
61 ret = face->GetUint32(node, "useconds", &config->uSecond, 0);
62 if (ret != HDF_SUCCESS) {
63 HDF_LOGE("TimerTestReadConfig: read useconds fail, ret: %d!", ret);
64 return ret;
65 }
66
67 ret = face->GetUint32(node, "isPeriod", &config->isPeriod, 0);
68 if (ret != HDF_SUCCESS) {
69 HDF_LOGE("TimerTestReadConfig: read isPeriod fail, ret: %d!", ret);
70 return ret;
71 }
72
73 HDF_LOGD("timer test init:number[%u][%u][%d]", config->number, config->uSecond, config->isPeriod);
74
75 return HDF_SUCCESS;
76 }
77
TimerTestBind(struct HdfDeviceObject * device)78 static int32_t TimerTestBind(struct HdfDeviceObject *device)
79 {
80 int32_t ret;
81 static struct IDeviceIoService service;
82
83 if (device == NULL || device->property == NULL) {
84 HDF_LOGE("TimerTestBind: device or config is null!");
85 return HDF_ERR_INVALID_OBJECT;
86 }
87
88 ret = TimerTestReadConfig(&g_config, device->property);
89 if (ret != HDF_SUCCESS) {
90 HDF_LOGE("TimerTestBind: read config fail, ret: %d!", ret);
91 return ret;
92 }
93
94 service.Dispatch = TimerTestDispatch;
95 device->service = &service;
96 return HDF_SUCCESS;
97 }
98
TimerTestInit(struct HdfDeviceObject * device)99 static int32_t TimerTestInit(struct HdfDeviceObject *device)
100 {
101 (void)device;
102 return HDF_SUCCESS;
103 }
104
TimerTestRelease(struct HdfDeviceObject * device)105 static void TimerTestRelease(struct HdfDeviceObject *device)
106 {
107 if (device != NULL) {
108 device->service = NULL;
109 }
110 HDF_LOGI("TimerTestRelease: done!");
111 return;
112 }
113
114 struct HdfDriverEntry g_timerTestEntry = {
115 .moduleVersion = 1,
116 .Bind = TimerTestBind,
117 .Init = TimerTestInit,
118 .Release = TimerTestRelease,
119 .moduleName = "PLATFORM_TIMER_TEST",
120 };
121 HDF_INIT(g_timerTestEntry);