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