1 /*
2 * Copyright (c) 2020-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_core.h"
10 #include "hdf_log.h"
11 #include "osal_mem.h"
12 #include "rtc_if.h"
13
14 #define HDF_LOG_TAG rtc_core_c
15
RtcHostReadTime(struct RtcHost * host,struct RtcTime * time)16 int32_t RtcHostReadTime(struct RtcHost *host, struct RtcTime *time)
17 {
18 if (host == NULL || time == NULL) {
19 HDF_LOGE("RtcHostReadTime: host or time is null!");
20 return HDF_ERR_INVALID_OBJECT;
21 }
22
23 if (host->method == NULL || host->method->ReadTime == NULL) {
24 HDF_LOGE("RtcHostReadTime: method or ReadTime is null!");
25 return HDF_ERR_NOT_SUPPORT;
26 }
27
28 return host->method->ReadTime(host, time);
29 }
30
RtcHostWriteTime(struct RtcHost * host,const struct RtcTime * time)31 int32_t RtcHostWriteTime(struct RtcHost *host, const struct RtcTime *time)
32 {
33 if (host == NULL || time == NULL) {
34 HDF_LOGE("RtcHostWriteTime: host or time is null!");
35 return HDF_ERR_INVALID_OBJECT;
36 }
37
38 if (host->method == NULL || host->method->WriteTime == NULL) {
39 HDF_LOGE("RtcHostWriteTime: method or WriteTime is null!");
40 return HDF_ERR_NOT_SUPPORT;
41 }
42
43 return host->method->WriteTime(host, time);
44 }
45
RtcHostReadAlarm(struct RtcHost * host,enum RtcAlarmIndex alarmIndex,struct RtcTime * time)46 int32_t RtcHostReadAlarm(struct RtcHost *host, enum RtcAlarmIndex alarmIndex, struct RtcTime *time)
47 {
48 if (host == NULL || time == NULL) {
49 HDF_LOGE("RtcHostReadAlarm: host is null!");
50 return HDF_ERR_INVALID_OBJECT;
51 }
52
53 if (host->method == NULL || host->method->ReadAlarm == NULL) {
54 HDF_LOGE("RtcHostReadAlarm: method or ReadAlarm is null!");
55 return HDF_ERR_NOT_SUPPORT;
56 }
57
58 return host->method->ReadAlarm(host, alarmIndex, time);
59 }
60
RtcHostWriteAlarm(struct RtcHost * host,enum RtcAlarmIndex alarmIndex,const struct RtcTime * time)61 int32_t RtcHostWriteAlarm(struct RtcHost *host, enum RtcAlarmIndex alarmIndex, const struct RtcTime *time)
62 {
63 if (host == NULL || time == NULL) {
64 HDF_LOGE("RtcHostWriteAlarm: host is null!");
65 return HDF_ERR_INVALID_OBJECT;
66 }
67
68 if (host->method == NULL || host->method->WriteAlarm == NULL) {
69 HDF_LOGE("RtcHostWriteAlarm: method or WriteAlarm is null!");
70 return HDF_ERR_NOT_SUPPORT;
71 }
72
73 return host->method->WriteAlarm(host, alarmIndex, time);
74 }
75
RtcHostRegisterAlarmCallback(struct RtcHost * host,enum RtcAlarmIndex alarmIndex,RtcAlarmCallback cb)76 int32_t RtcHostRegisterAlarmCallback(struct RtcHost *host, enum RtcAlarmIndex alarmIndex, RtcAlarmCallback cb)
77 {
78 if (host == NULL || cb == NULL) {
79 HDF_LOGE("RtcHostRegisterAlarmCallback: host or cb is null!");
80 return HDF_ERR_INVALID_OBJECT;
81 }
82
83 if (host->method == NULL || host->method->RegisterAlarmCallback == NULL) {
84 HDF_LOGE("RtcHostRegisterAlarmCallback: method or RegisterAlarmCallback is null!");
85 return HDF_ERR_NOT_SUPPORT;
86 }
87
88 return host->method->RegisterAlarmCallback(host, alarmIndex, cb);
89 }
90
RtcHostAlarmInterruptEnable(struct RtcHost * host,enum RtcAlarmIndex alarmIndex,uint8_t enable)91 int32_t RtcHostAlarmInterruptEnable(struct RtcHost *host, enum RtcAlarmIndex alarmIndex, uint8_t enable)
92 {
93 if (host == NULL) {
94 HDF_LOGE("RtcHostAlarmInterruptEnable: host is null!");
95 return HDF_ERR_INVALID_OBJECT;
96 }
97
98 if (host->method == NULL || host->method->AlarmInterruptEnable == NULL) {
99 HDF_LOGE("RtcHostAlarmInterruptEnable: method or AlarmInterruptEnable is null!");
100 return HDF_ERR_NOT_SUPPORT;
101 }
102
103 return host->method->AlarmInterruptEnable(host, alarmIndex, enable);
104 }
105
RtcHostGetFreq(struct RtcHost * host,uint32_t * freq)106 int32_t RtcHostGetFreq(struct RtcHost *host, uint32_t *freq)
107 {
108 if (host == NULL || freq == NULL) {
109 HDF_LOGE("RtcHostGetFreq: host or freq is null!");
110 return HDF_ERR_INVALID_OBJECT;
111 }
112
113 if (host->method == NULL || host->method->GetFreq == NULL) {
114 HDF_LOGE("RtcHostGetFreq: method or GetFreq is null!");
115 return HDF_ERR_NOT_SUPPORT;
116 }
117
118 return host->method->GetFreq(host, freq);
119 }
120
RtcHostSetFreq(struct RtcHost * host,uint32_t freq)121 int32_t RtcHostSetFreq(struct RtcHost *host, uint32_t freq)
122 {
123 if (host == NULL) {
124 HDF_LOGE("RtcHostSetFreq: host is null!");
125 return HDF_ERR_INVALID_OBJECT;
126 }
127
128 if (host->method == NULL || host->method->SetFreq == NULL) {
129 HDF_LOGE("RtcHostSetFreq: method or SetFreq is null!");
130 return HDF_ERR_NOT_SUPPORT;
131 }
132
133 return host->method->SetFreq(host, freq);
134 }
135
RtcHostReset(struct RtcHost * host)136 int32_t RtcHostReset(struct RtcHost *host)
137 {
138 if (host == NULL) {
139 HDF_LOGE("RtcHostReset: host is null!");
140 return HDF_ERR_INVALID_OBJECT;
141 }
142
143 if (host->method == NULL || host->method->Reset == NULL) {
144 HDF_LOGE("RtcHostReset: method or Reset is null!");
145 return HDF_ERR_NOT_SUPPORT;
146 }
147
148 return host->method->Reset(host);
149 }
150
RtcHostReadReg(struct RtcHost * host,uint8_t usrDefIndex,uint8_t * value)151 int32_t RtcHostReadReg(struct RtcHost *host, uint8_t usrDefIndex, uint8_t *value)
152 {
153 if (host == NULL || value == NULL) {
154 HDF_LOGE("RtcHostReadReg: host or value is null!");
155 return HDF_ERR_INVALID_OBJECT;
156 }
157
158 if (host->method == NULL || host->method->ReadReg == NULL) {
159 HDF_LOGE("RtcHostReadReg: method or ReadReg is null!");
160 return HDF_ERR_NOT_SUPPORT;
161 }
162
163 return host->method->ReadReg(host, usrDefIndex, value);
164 }
165
RtcHostWriteReg(struct RtcHost * host,uint8_t usrDefIndex,uint8_t value)166 int32_t RtcHostWriteReg(struct RtcHost *host, uint8_t usrDefIndex, uint8_t value)
167 {
168 if (host == NULL) {
169 HDF_LOGE("RtcHostWriteReg: host is null!");
170 return HDF_ERR_INVALID_OBJECT;
171 }
172
173 if (host->method == NULL || host->method->WriteReg == NULL) {
174 HDF_LOGE("RtcHostWriteReg: method or WriteReg is null!");
175 return HDF_ERR_NOT_SUPPORT;
176 }
177
178 return host->method->WriteReg(host, usrDefIndex, value);
179 }
180
RtcHostCreate(struct HdfDeviceObject * device)181 struct RtcHost *RtcHostCreate(struct HdfDeviceObject *device)
182 {
183 struct RtcHost *host = NULL;
184
185 if (device == NULL) {
186 HDF_LOGE("RtcHostCreate: device is null!");
187 return NULL;
188 }
189
190 host = (struct RtcHost *)OsalMemCalloc(sizeof(*host));
191 if (host == NULL) {
192 HDF_LOGE("RtcHostCreate: malloc host fail!");
193 return NULL;
194 }
195
196 host->device = device;
197 device->service = &(host->service);
198 host->method = NULL;
199 host->data = NULL;
200 host->device->service->Dispatch = RtcIoDispatch;
201 return host;
202 }
203
RtcHostDestroy(struct RtcHost * host)204 void RtcHostDestroy(struct RtcHost *host)
205 {
206 if (host == NULL) {
207 HDF_LOGE("RtcHostDestroy: host is null!");
208 return;
209 }
210
211 host->device = NULL;
212 host->method = NULL;
213 host->data = NULL;
214 OsalMemFree(host);
215 }
216