1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdarg.h>
18 
19 #include <gpio.h>
20 #include <osApi.h>
21 #include <sensors.h>
22 #include <seos.h>
23 #include <util.h>
24 
25 /* CHRE syscalls */
26 #include <chre.h>
27 #include <chreApi.h>
28 #include <syscall.h>
29 #include <syscall_defs.h>
30 
31 #define SYSCALL_CHRE_API(name) \
32     SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_API, SYSCALL_CHRE_MAIN_API_ ## name)
33 
chreGetAppId(void)34 uint64_t chreGetAppId(void)
35 {
36     uint64_t appId = 0;
37     (void)syscallDo1P(SYSCALL_CHRE_API(GET_APP_ID), &appId);
38     return appId;
39 }
40 
chreGetInstanceId(void)41 uint32_t chreGetInstanceId(void)
42 {
43     return syscallDo0P(SYSCALL_CHRE_API(GET_INST_ID));
44 }
45 
chreGetTime(void)46 uint64_t chreGetTime(void) {
47     uint64_t time_ns = 0;
48     (void)syscallDo1P(SYSCALL_CHRE_API(GET_TIME), &time_ns);
49     return time_ns;
50 }
51 
chreGetEstimatedHostTimeOffset(void)52 int64_t chreGetEstimatedHostTimeOffset(void) {
53     int64_t time_ns = 0;
54     (void)syscallDo1P(SYSCALL_CHRE_API(GET_HOST_TIME_OFFSET), &time_ns);
55     return time_ns;
56 }
57 
chreLog(enum chreLogLevel level,const char * str,...)58 void chreLog(enum chreLogLevel level, const char *str, ...)
59 {
60     va_list vl;
61 
62     va_start(vl, str);
63     (void)syscallDo3P(SYSCALL_CHRE_API(LOG), level, str, VA_LIST_TO_INTEGER(vl));
64     va_end(vl);
65 }
66 
chreTimerSet(uint64_t duration,const void * cookie,bool oneShot)67 uint32_t chreTimerSet(uint64_t duration, const void* cookie, bool oneShot)
68 {
69     uint32_t dur_lo = duration;
70     uint32_t dur_hi = duration >> 32;
71     return syscallDo4P(SYSCALL_CHRE_API(TIMER_SET), dur_lo, dur_hi, cookie, oneShot);
72 }
73 
chreTimerCancel(uint32_t timerId)74 bool chreTimerCancel(uint32_t timerId)
75 {
76     return syscallDo1P(SYSCALL_CHRE_API(TIMER_CANCEL), timerId);
77 }
78 
chreAbort(uint32_t abortCode)79 void chreAbort(uint32_t abortCode)
80 {
81     (void)syscallDo1P(SYSCALL_CHRE_API(ABORT), abortCode);
82 }
83 
chreHeapAlloc(uint32_t bytes)84 void* chreHeapAlloc(uint32_t bytes)
85 {
86     return (void *)syscallDo1P(SYSCALL_CHRE_API(HEAP_ALLOC), bytes);
87 }
88 
chreHeapFree(void * ptr)89 void chreHeapFree(void* ptr)
90 {
91     (void)syscallDo1P(SYSCALL_CHRE_API(HEAP_FREE), ptr);
92 }
93 
chreSensorFindDefault(uint8_t sensorType,uint32_t * handle)94 bool chreSensorFindDefault(uint8_t sensorType, uint32_t *handle)
95 {
96     return syscallDo2P(SYSCALL_CHRE_API(SENSOR_FIND_DEFAULT), sensorType, handle);
97 }
98 
chreGetSensorInfo(uint32_t sensorHandle,struct chreSensorInfo * info)99 bool chreGetSensorInfo(uint32_t sensorHandle, struct chreSensorInfo *info)
100 {
101     return syscallDo2P(SYSCALL_CHRE_API(SENSOR_GET_INFO), sensorHandle, info);
102 }
103 
chreGetSensorSamplingStatus(uint32_t sensorHandle,struct chreSensorSamplingStatus * status)104 bool chreGetSensorSamplingStatus(uint32_t sensorHandle,
105                                  struct chreSensorSamplingStatus *status)
106 {
107     return syscallDo2P(SYSCALL_CHRE_API(SENSOR_GET_STATUS), sensorHandle, status);
108 }
109 
chreSensorConfigure(uint32_t sensorHandle,enum chreSensorConfigureMode mode,uint64_t interval,uint64_t latency)110 bool chreSensorConfigure(uint32_t sensorHandle,
111                          enum chreSensorConfigureMode mode,
112                          uint64_t interval, uint64_t latency)
113 {
114     uint32_t interval_lo = interval;
115     uint32_t interval_hi = interval >> 32;
116     uint32_t latency_lo = latency;
117     uint32_t latency_hi = latency >> 32;
118     return syscallDo6P(SYSCALL_CHRE_API(SENSOR_CONFIG), sensorHandle, mode,
119                        interval_lo, interval_hi, latency_lo, latency_hi);
120 }
121 
chreGetApiVersion(void)122 uint32_t chreGetApiVersion(void)
123 {
124     static uint32_t apiVersion = 0;
125     if (!apiVersion)
126         apiVersion = syscallDo0P(SYSCALL_CHRE_API(GET_OS_API_VERSION));
127     return apiVersion;
128 }
129 
chreGetVersion(void)130 uint32_t chreGetVersion(void)
131 {
132     return syscallDo0P(SYSCALL_CHRE_API(GET_OS_VERSION));
133 }
134 
chreGetPlatformId(void)135 uint64_t chreGetPlatformId(void)
136 {
137     uint64_t plat = 0;
138     (void)syscallDo1P(SYSCALL_CHRE_API(GET_PLATFORM_ID), &plat);
139     return plat;
140 }
141 
chreSendEvent(uint16_t eventType,void * eventData,chreEventCompleteFunction * freeCallback,uint32_t targetInstanceId)142 bool chreSendEvent(uint16_t eventType, void *eventData,
143                    chreEventCompleteFunction *freeCallback,
144                    uint32_t targetInstanceId)
145 {
146     if (chreGetApiVersion() == CHRE_API_VERSION_1_0)
147         return syscallDo4P(SYSCALL_CHRE_API(SEND_EVENT), eventType, eventData, freeCallback, targetInstanceId);
148     else
149         return syscallDo4P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_SEND_EVENT), eventType, eventData, freeCallback, targetInstanceId);
150 }
151 
chreSendMessageToHost(void * message,uint32_t messageSize,uint32_t messageType,chreMessageFreeFunction * freeCallback)152 bool chreSendMessageToHost(void *message, uint32_t messageSize,
153                            uint32_t messageType,
154                            chreMessageFreeFunction *freeCallback)
155 {
156     if (chreGetApiVersion() == CHRE_API_VERSION_1_0)
157         return syscallDo4P(SYSCALL_CHRE_API(SEND_MSG), message, messageSize, messageType, freeCallback);
158     else
159         return syscallDo5P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_SEND_MSG), message, messageSize, messageType, CHRE_HOST_ENDPOINT_BROADCAST, freeCallback);
160 }
161 
chreSendMessageToHostEndpoint(void * message,size_t messageSize,uint32_t messageType,uint16_t hostEndpoint,chreMessageFreeFunction * freeCallback)162 bool chreSendMessageToHostEndpoint(void *message, size_t messageSize,
163                                    uint32_t messageType, uint16_t hostEndpoint,
164                                    chreMessageFreeFunction *freeCallback)
165 {
166     if (chreGetApiVersion() == CHRE_API_VERSION_1_0)
167         return syscallDo4P(SYSCALL_CHRE_API(SEND_MSG), message, messageSize, messageType, freeCallback);
168     else
169         return syscallDo5P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_SEND_MSG), message, messageSize, messageType, hostEndpoint, freeCallback);
170 }
171 
chreGetNanoappInfoByAppId(uint64_t appId,struct chreNanoappInfo * info)172 bool chreGetNanoappInfoByAppId(uint64_t appId, struct chreNanoappInfo *info)
173 {
174     uint32_t app_lo = appId;
175     uint32_t app_hi = appId >> 32;
176     return syscallDo3P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_INFO_BY_APP_ID), app_lo, app_hi, info);
177 }
178 
chreGetNanoappInfoByInstanceId(uint32_t instanceId,struct chreNanoappInfo * info)179 bool chreGetNanoappInfoByInstanceId(uint32_t instanceId, struct chreNanoappInfo *info)
180 {
181     return syscallDo2P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_INFO_BY_INST_ID), instanceId, info);
182 }
183 
chreConfigureNanoappInfoEvents(bool enable)184 void chreConfigureNanoappInfoEvents(bool enable)
185 {
186     syscallDo1P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_CFG_INFO), enable);
187 }
188 
chreConfigureHostSleepStateEvents(bool enable)189 void chreConfigureHostSleepStateEvents(bool enable)
190 {
191     syscallDo1P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_HOST_SLEEP), enable);
192 }
193 
chreIsHostAwake(void)194 bool chreIsHostAwake(void)
195 {
196     return syscallDo0P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_IS_HOST_AWAKE));
197 }
198 
chreGnssGetCapabilities(void)199 uint32_t chreGnssGetCapabilities(void)
200 {
201     return syscallDo0P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_GNSS, SYSCALL_CHRE_DRV_GNSS_GET_CAP));
202 }
203 
chreGnssLocationSessionStartAsync(uint32_t minIntervalMs,uint32_t minTimeToNextFixMs,const void * cookie)204 bool chreGnssLocationSessionStartAsync(uint32_t minIntervalMs, uint32_t minTimeToNextFixMs, const void *cookie)
205 {
206     return syscallDo3P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_GNSS, SYSCALL_CHRE_DRV_GNSS_LOC_START_ASYNC), minIntervalMs, minTimeToNextFixMs, cookie);
207 }
208 
chreGnssLocationSessionStopAsync(const void * cookie)209 bool chreGnssLocationSessionStopAsync(const void *cookie)
210 {
211     return syscallDo1P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_GNSS, SYSCALL_CHRE_DRV_GNSS_LOC_STOP_ASYNC), cookie);
212 }
213 
chreGnssMeasurementSessionStartAsync(uint32_t minIntervalMs,const void * cookie)214 bool chreGnssMeasurementSessionStartAsync(uint32_t minIntervalMs, const void *cookie)
215 {
216     return syscallDo2P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_GNSS, SYSCALL_CHRE_DRV_GNSS_MEAS_START_ASYNC), minIntervalMs, cookie);
217 }
218 
chreGnssMeasurementSessionStopAsync(const void * cookie)219 bool chreGnssMeasurementSessionStopAsync(const void *cookie)
220 {
221     return syscallDo1P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_GNSS, SYSCALL_CHRE_DRV_GNSS_MEAS_STOP_ASYNC), cookie);
222 }
223 
chreGnssConfigurePassiveLocationListener(bool enable)224 bool chreGnssConfigurePassiveLocationListener(bool enable)
225 {
226     return syscallDo1P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_GNSS, SYSCALL_CHRE_DRV_GNSS_CONF_PASV_LOC_LIS), enable);
227 }
228 
chreWifiGetCapabilities(void)229 uint32_t chreWifiGetCapabilities(void)
230 {
231     return syscallDo0P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_WIFI, SYSCALL_CHRE_DRV_WIFI_GET_CAP));
232 }
233 
chreWifiConfigureScanMonitorAsync(bool enable,const void * cookie)234 bool chreWifiConfigureScanMonitorAsync(bool enable, const void *cookie)
235 {
236     return syscallDo2P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_WIFI, SYSCALL_CHRE_DRV_WIFI_CONF_SCAN_MON_ASYNC), enable, cookie);
237 }
238 
chreWifiRequestScanAsync(const struct chreWifiScanParams * params,const void * cookie)239 bool chreWifiRequestScanAsync(const struct chreWifiScanParams *params, const void *cookie)
240 {
241     return syscallDo2P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_WIFI, SYSCALL_CHRE_DRV_WIFI_REQ_SCAN_ASYNC), params, cookie);
242 }
243 
chreWwanGetCapabilities(void)244 uint32_t chreWwanGetCapabilities(void)
245 {
246     return syscallDo0P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_WWAN, SYSCALL_CHRE_DRV_WWAN_GET_CAP));
247 }
248 
chreWwanGetCellInfoAsync(const void * cookie)249 bool chreWwanGetCellInfoAsync(const void *cookie)
250 {
251     return syscallDo1P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_WWAN, SYSCALL_CHRE_DRV_WWAN_GET_CELL_INFO_ASYNC), cookie);
252 }
253 
chreAudioGetSource(uint32_t handle,struct chreAudioSource * audioSource)254 bool chreAudioGetSource(uint32_t handle, struct chreAudioSource *audioSource)
255 {
256     return syscallDo2P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_AUDIO, SYSCALL_CHRE_DRV_AUDIO_GET_SRC), handle, audioSource);
257 }
258 
chreAudioConfigureSource(uint32_t handle,bool enable,uint64_t bufferDuration,uint64_t deliveryInterval)259 bool chreAudioConfigureSource(uint32_t handle, bool enable,
260                               uint64_t bufferDuration,
261                               uint64_t deliveryInterval)
262 {
263     uint32_t duration_lo = bufferDuration;
264     uint32_t duration_hi = bufferDuration >> 32;
265     uint32_t interval_lo = deliveryInterval;
266     uint32_t interval_hi = deliveryInterval >> 32;
267 
268     return syscallDo6P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_AUDIO, SYSCALL_CHRE_DRV_AUDIO_CONF_SRC), handle, enable, duration_lo, duration_hi, interval_lo, interval_hi);
269 }
270 
chreAudioGetStatus(uint32_t handle,struct chreAudioSourceStatus * status)271 bool chreAudioGetStatus(uint32_t handle, struct chreAudioSourceStatus *status)
272 {
273     return syscallDo2P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_DRIVERS, SYSCALL_CHRE_DRV_AUDIO, SYSCALL_CHRE_DRV_AUDIO_GET_STATUS), handle, status);
274 }
275