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