• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hdf_log.h"
10 #include "hdf_device_desc.h"
11 #include "osal_mem.h"
12 #include "rtc_core.h"
13 #include "rtc_if.h"
14 
15 #define HDF_LOG_TAG rtc_service_c
16 
RtcServiceIoReadTime(struct RtcHost * host,struct HdfSBuf * reply)17 static int32_t RtcServiceIoReadTime(struct RtcHost *host, struct HdfSBuf *reply)
18 {
19     int32_t ret;
20     struct RtcTime time;
21 
22     ret = RtcHostReadTime(host, &time);
23     if (ret != HDF_SUCCESS) {
24         HDF_LOGE("%s: host read time fail!", __func__);
25         return ret;
26     }
27 
28     if (!HdfSbufWriteBuffer(reply, &time, sizeof(time))) {
29         HDF_LOGE("%s: write buffer fail!", __func__);
30         return HDF_ERR_IO;
31     }
32 
33     return HDF_SUCCESS;
34 }
35 
RtcServiceIoWriteTime(struct RtcHost * host,struct HdfSBuf * data)36 static int32_t RtcServiceIoWriteTime(struct RtcHost *host, struct HdfSBuf *data)
37 {
38     int32_t ret;
39     uint32_t len;
40     struct RtcTime *time = NULL;
41 
42     if (!HdfSbufReadBuffer(data, (const void **)&time, &len) || sizeof(*time) != len) {
43         HDF_LOGE("%s: read buffer fail!", __func__);
44         return HDF_ERR_IO;
45     }
46 
47     ret = RtcHostWriteTime(host, time);
48     if (ret != HDF_SUCCESS) {
49         HDF_LOGE("%s: host write time fail!", __func__);
50         return ret;
51     }
52 
53     return HDF_SUCCESS;
54 }
55 
RtcServiceIoReadAlarm(struct RtcHost * host,struct HdfSBuf * data,struct HdfSBuf * reply)56 static int32_t RtcServiceIoReadAlarm(struct RtcHost *host, struct HdfSBuf *data, struct HdfSBuf *reply)
57 {
58     int32_t ret;
59     uint32_t alarmIndex = 0;
60     struct RtcTime time;
61 
62     if (!HdfSbufReadUint32(data, &alarmIndex)) {
63         HDF_LOGE("%s: read alarmIndex fail!", __func__);
64         return HDF_ERR_IO;
65     }
66 
67     ret = RtcHostReadAlarm(host, (enum RtcAlarmIndex)alarmIndex, &time);
68     if (ret != HDF_SUCCESS) {
69         HDF_LOGE("%s: host read alarm fail!", __func__);
70         return ret;
71     }
72 
73     if (!HdfSbufWriteBuffer(reply, &time, sizeof(time))) {
74         HDF_LOGE("%s: write buffer fail!", __func__);
75         return HDF_ERR_IO;
76     }
77 
78     return HDF_SUCCESS;
79 }
80 
RtcServiceIoWriteAlarm(struct RtcHost * host,struct HdfSBuf * data)81 static int32_t RtcServiceIoWriteAlarm(struct RtcHost *host, struct HdfSBuf *data)
82 {
83     int32_t ret;
84     uint32_t len;
85     uint32_t alarmIndex = 0;
86     struct RtcTime *time = NULL;
87 
88     if (!HdfSbufReadUint32(data, &alarmIndex)) {
89         HDF_LOGE("%s: read alarmIndex fail!", __func__);
90         return HDF_ERR_IO;
91     }
92 
93     if (!HdfSbufReadBuffer(data, (const void **)&time, &len) || sizeof(*time) != len) {
94         HDF_LOGE("%s: read buffer fail!", __func__);
95         return HDF_ERR_IO;
96     }
97 
98     ret = RtcHostWriteAlarm(host, (enum RtcAlarmIndex)alarmIndex, time);
99     if (ret != HDF_SUCCESS) {
100         HDF_LOGE("%s: host write alarm fail!", __func__);
101         return ret;
102     }
103 
104     return HDF_SUCCESS;
105 }
106 
RtcServiceIoRegisterAlarmCallback(struct RtcHost * host,struct HdfSBuf * data)107 static int32_t RtcServiceIoRegisterAlarmCallback(struct RtcHost *host, struct HdfSBuf *data)
108 {
109     (void)host;
110     (void)data;
111     return HDF_SUCCESS;
112 }
113 
RtcServiceIoInterruptEnable(struct RtcHost * host,struct HdfSBuf * data)114 static int32_t RtcServiceIoInterruptEnable(struct RtcHost *host, struct HdfSBuf *data)
115 {
116     int32_t ret;
117     uint32_t alarmIndex = 0;
118     uint8_t enable = 0;
119 
120     if (!HdfSbufReadUint32(data, &alarmIndex))  {
121         HDF_LOGE("%s: read alarmIndex fail!", __func__);
122         return HDF_ERR_IO;
123     }
124 
125     if (!HdfSbufReadUint8(data, &enable)) {
126         HDF_LOGE("%s: read enable fail!", __func__);
127         return HDF_ERR_IO;
128     }
129 
130     ret = RtcHostAlarmInterruptEnable(host, (enum RtcAlarmIndex)alarmIndex, enable);
131     if (ret != HDF_SUCCESS) {
132         HDF_LOGE("%s: host alarm interrupt enable fail! ret :%d", __func__, ret);
133         return ret;
134     }
135 
136     return HDF_SUCCESS;
137 }
138 
RtcServiceIoGetFreq(struct RtcHost * host,struct HdfSBuf * reply)139 static int32_t RtcServiceIoGetFreq(struct RtcHost *host, struct HdfSBuf *reply)
140 {
141     int32_t ret;
142     uint32_t freq = 0;
143 
144     ret = RtcHostGetFreq(host, &freq);
145     if (ret != HDF_SUCCESS) {
146         HDF_LOGE("%s: host get freq fail! ret :%d", __func__, ret);
147         return ret;
148     }
149 
150     if (!HdfSbufWriteUint32(reply, freq)) {
151         HDF_LOGE("%s: write freq fail!", __func__);
152         return HDF_ERR_IO;
153     }
154 
155     return HDF_SUCCESS;
156 }
157 
RtcServiceIoSetFreq(struct RtcHost * host,struct HdfSBuf * data)158 static int32_t RtcServiceIoSetFreq(struct RtcHost *host, struct HdfSBuf *data)
159 {
160     int32_t ret;
161     uint32_t freq = 0;
162 
163     if (!HdfSbufReadUint32(data, &freq)) {
164         HDF_LOGE("%s: read freq fail", __func__);
165         return HDF_ERR_IO;
166     }
167 
168     ret = RtcHostSetFreq(host, freq);
169     if (ret != HDF_SUCCESS) {
170         HDF_LOGE("%s: host set freq fail! ret :%d", __func__, ret);
171         return ret;
172     }
173 
174     return HDF_SUCCESS;
175 }
176 
RtcServiceIoReset(struct RtcHost * host)177 static int32_t RtcServiceIoReset(struct RtcHost *host)
178 {
179     int32_t ret;
180 
181     ret = RtcHostReset(host);
182     if (ret != HDF_SUCCESS) {
183         HDF_LOGE("%s: host reset fail! ret :%d", __func__, ret);
184         return ret;
185     }
186 
187     return HDF_SUCCESS;
188 }
189 
RtcServiceIoReadReg(struct RtcHost * host,struct HdfSBuf * data,struct HdfSBuf * reply)190 static int32_t RtcServiceIoReadReg(struct RtcHost *host, struct HdfSBuf *data, struct HdfSBuf *reply)
191 {
192     int32_t ret;
193     uint8_t usrDefIndex = 0;
194     uint8_t value = 0;
195 
196     if (!HdfSbufReadUint8(data, &usrDefIndex)) {
197         HDF_LOGE("%s: read usrDefIndex fail!", __func__);
198         return HDF_ERR_IO;
199     }
200 
201     ret = RtcHostReadReg(host, usrDefIndex, &value);
202     if (ret != HDF_SUCCESS) {
203         HDF_LOGE("%s: host read reg fail! ret :%d", __func__, ret);
204         return ret;
205     }
206 
207     if (!HdfSbufWriteUint8(reply, value)) {
208         HDF_LOGE("%s: write value fail!", __func__);
209         return HDF_ERR_IO;
210     }
211 
212     return HDF_SUCCESS;
213 }
214 
RtcServiceIoWriteReg(struct RtcHost * host,struct HdfSBuf * data)215 static int32_t RtcServiceIoWriteReg(struct RtcHost *host, struct HdfSBuf *data)
216 {
217     int32_t ret;
218     uint8_t usrDefIndex = 0;
219     uint8_t value = 0;
220 
221     if (!HdfSbufReadUint8(data, &usrDefIndex)) {
222         HDF_LOGE("%s: read usrDefIndex fail!", __func__);
223         return HDF_ERR_IO;
224     }
225 
226     if (!HdfSbufReadUint8(data, &value)) {
227         HDF_LOGE("%s: read value fail!", __func__);
228         return HDF_ERR_IO;
229     }
230 
231     ret = RtcHostWriteReg(host, usrDefIndex, value);
232     if (ret != HDF_SUCCESS) {
233         HDF_LOGE("%s: host write reg fail! ret :%d", __func__, ret);
234         return ret;
235     }
236 
237     return HDF_SUCCESS;
238 }
239 
RtcIoDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)240 int32_t RtcIoDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
241 {
242     struct RtcHost *host = NULL;
243 
244     if (client == NULL) {
245         HDF_LOGE("%s: client is NULL", __func__);
246         return HDF_ERR_INVALID_OBJECT;
247     }
248 
249     if (client->device == NULL) {
250         HDF_LOGE("%s: client->device is NULL", __func__);
251         return HDF_ERR_INVALID_OBJECT;
252     }
253 
254     if (client->device->service == NULL) {
255         HDF_LOGE("%s: client->device->service is NULL", __func__);
256         return HDF_ERR_INVALID_OBJECT;
257     }
258 
259     host = (struct RtcHost *)client->device->service;
260 
261     switch (cmd) {
262         case RTC_IO_READTIME:
263             return RtcServiceIoReadTime(host, reply);
264         case RTC_IO_WRITETIME:
265             return RtcServiceIoWriteTime(host, data);
266         case RTC_IO_READALARM:
267             return RtcServiceIoReadAlarm(host, data, reply);
268         case RTC_IO_WRITEALARM:
269             return RtcServiceIoWriteAlarm(host, data);
270         case RTC_IO_REGISTERALARMCALLBACK:
271             return RtcServiceIoRegisterAlarmCallback(host, data);
272         case RTC_IO_ALARMINTERRUPTENABLE:
273             return RtcServiceIoInterruptEnable(host, data);
274         case RTC_IO_GETFREQ:
275             return RtcServiceIoGetFreq(host, reply);
276         case RTC_IO_SETFREQ:
277             return RtcServiceIoSetFreq(host, data);
278         case RTC_IO_RESET:
279             return RtcServiceIoReset(host);
280         case RTC_IO_READREG:
281             return RtcServiceIoReadReg(host, data, reply);
282         case RTC_IO_WRITEREG:
283             return RtcServiceIoWriteReg(host, data);
284         default:
285             return HDF_ERR_NOT_SUPPORT;
286     }
287 }