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 "rtc_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 #include "osal_time.h"
15 #include "rtc_base.h"
16 #include "rtc_if.h"
17
18 #define HDF_LOG_TAG hdf_rtc_driver_test_c
19
20 static struct RtcTestConfig g_config;
21
RtcTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)22 static int32_t RtcTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
23 {
24 if (cmd == 0) {
25 if (reply == NULL) {
26 HDF_LOGE("%s: reply is null!", __func__);
27 return HDF_ERR_INVALID_PARAM;
28 }
29 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
30 HDF_LOGE("%s: write reply failed", __func__);
31 return HDF_ERR_IO;
32 }
33 } else {
34 return HDF_ERR_NOT_SUPPORT;
35 }
36
37 return HDF_SUCCESS;
38 }
39
RtcTestReadConfig(struct RtcTestConfig * config,const struct DeviceResourceNode * node)40 static int32_t RtcTestReadConfig(struct RtcTestConfig *config, const struct DeviceResourceNode *node)
41 {
42 int32_t ret;
43 struct DeviceResourceIface *drsOps = NULL;
44
45 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
46 if (drsOps == NULL || drsOps->GetUint32 == NULL) {
47 HDF_LOGE("%s: invalid drs ops", __func__);
48 return HDF_FAILURE;
49 }
50 ret = drsOps->GetUint32(node, "time", &config->time, 0);
51 if (ret != HDF_SUCCESS) {
52 HDF_LOGE("%s: read time failed", __func__);
53 return ret;
54 }
55 ret = drsOps->GetUint32(node, "maxYear", &config->maxYear, 0);
56 if (ret != HDF_SUCCESS) {
57 HDF_LOGE("%s: read maxYear failed", __func__);
58 return ret;
59 }
60 ret = drsOps->GetUint32(node, "year", &config->year, 0);
61 if (ret != HDF_SUCCESS) {
62 HDF_LOGE("%s: read year failed", __func__);
63 return ret;
64 }
65 ret = drsOps->GetUint32(node, "month", &config->month, 0);
66 if (ret != HDF_SUCCESS) {
67 HDF_LOGE("%s: read month failed", __func__);
68 return ret;
69 }
70 ret = drsOps->GetUint32(node, "day", &config->day, 0);
71 if (ret != HDF_SUCCESS) {
72 HDF_LOGE("%s: read day failed", __func__);
73 return ret;
74 }
75 ret = drsOps->GetUint32(node, "hour", &config->hour, 0);
76 if (ret != HDF_SUCCESS) {
77 HDF_LOGE("%s: read hour failed", __func__);
78 return ret;
79 }
80 ret = drsOps->GetUint32(node, "minute", &config->minute, 0);
81 if (ret != HDF_SUCCESS) {
82 HDF_LOGE("%s: read minute failed", __func__);
83 return ret;
84 }
85 ret = drsOps->GetUint32(node, "second", &config->second, 0);
86 if (ret != HDF_SUCCESS) {
87 HDF_LOGE("%s: read second failed", __func__);
88 return ret;
89 }
90 ret = drsOps->GetUint32(node, "frequence", &config->frequence, 0);
91 if (ret != HDF_SUCCESS) {
92 HDF_LOGE("%s: read frequence failed", __func__);
93 return ret;
94 }
95 ret = drsOps->GetUint32(node, "userValue", &config->userValue, 0);
96 if (ret != HDF_SUCCESS) {
97 HDF_LOGE("%s: read userValue failed", __func__);
98 return ret;
99 }
100 ret = drsOps->GetUint32(node, "userMaxIndex", &config->userMaxIndex, 0);
101 if (ret != HDF_SUCCESS) {
102 HDF_LOGE("%s: read userMaxIndex failed", __func__);
103 return ret;
104 }
105 ret = drsOps->GetUint32(node, "waitTimeSecond", &config->waitTimeSecond, 0);
106 if (ret != HDF_SUCCESS) {
107 HDF_LOGE("%s: read waitTimeSecond failed", __func__);
108 return ret;
109 }
110 ret = drsOps->GetUint32(node, "writeWaitMillisecond", &config->writeWaitMillisecond, 0);
111 if (ret != HDF_SUCCESS) {
112 HDF_LOGE("%s: read writeWaitMillisecond failed", __func__);
113 return ret;
114 }
115 return HDF_SUCCESS;
116 }
117
RtcTestBind(struct HdfDeviceObject * device)118 static int32_t RtcTestBind(struct HdfDeviceObject *device)
119 {
120 int32_t ret;
121 static struct IDeviceIoService service;
122
123 if (device == NULL || device->property == NULL) {
124 HDF_LOGE("%s: device or config is null!", __func__);
125 return HDF_ERR_IO;
126 }
127
128 ret = RtcTestReadConfig(&g_config, device->property);
129 if (ret != HDF_SUCCESS) {
130 HDF_LOGE("%s: read config failed", __func__);
131 return ret;
132 }
133
134 service.Dispatch = RtcTestDispatch;
135 device->service = &service;
136
137 return HDF_SUCCESS;
138 }
139
RtcTestInit(struct HdfDeviceObject * device)140 static int32_t RtcTestInit(struct HdfDeviceObject *device)
141 {
142 (void)device;
143 return HDF_SUCCESS;
144 }
145
RtcTestRelease(struct HdfDeviceObject * device)146 static void RtcTestRelease(struct HdfDeviceObject *device)
147 {
148 if (device != NULL) {
149 device->service = NULL;
150 }
151 return;
152 }
153
154 struct HdfDriverEntry g_rtcTestEntry = {
155 .moduleVersion = 1,
156 .Bind = RtcTestBind,
157 .Init = RtcTestInit,
158 .Release = RtcTestRelease,
159 .moduleName = "PLATFORM_RTC_TEST",
160 };
161 HDF_INIT(g_rtcTestEntry);
162