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 /* not defined in chre 1.0 */
32 #define CHRE_HOST_ENDPOINT_BROADCAST UINT16_C(0xFFFF)
33
34 #define SYSCALL_CHRE_API(name) \
35 SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_API, SYSCALL_CHRE_MAIN_API_ ## name)
36
chreGetAppId(void)37 uint64_t chreGetAppId(void)
38 {
39 uint64_t appId = 0;
40 (void)syscallDo1P(SYSCALL_CHRE_API(GET_APP_ID), &appId);
41 return appId;
42 }
43
chreGetInstanceId(void)44 uint32_t chreGetInstanceId(void)
45 {
46 return syscallDo0P(SYSCALL_CHRE_API(GET_INST_ID));
47 }
48
chreGetTime(void)49 uint64_t chreGetTime(void) {
50 uint64_t time_ns = 0;
51 (void)syscallDo1P(SYSCALL_CHRE_API(GET_TIME), &time_ns);
52 return time_ns;
53 }
54
chreLog(enum chreLogLevel level,const char * str,...)55 void chreLog(enum chreLogLevel level, const char *str, ...)
56 {
57 va_list vl;
58
59 va_start(vl, str);
60 (void)syscallDo3P(SYSCALL_CHRE_API(LOG), level, str, VA_LIST_TO_INTEGER(vl));
61 va_end(vl);
62 }
63
chreTimerSet(uint64_t duration,const void * cookie,bool oneShot)64 uint32_t chreTimerSet(uint64_t duration, const void* cookie, bool oneShot)
65 {
66 uint32_t dur_lo = duration;
67 uint32_t dur_hi = duration >> 32;
68 return syscallDo4P(SYSCALL_CHRE_API(TIMER_SET), dur_lo, dur_hi, cookie, oneShot);
69 }
70
chreTimerCancel(uint32_t timerId)71 bool chreTimerCancel(uint32_t timerId)
72 {
73 return syscallDo1P(SYSCALL_CHRE_API(TIMER_CANCEL), timerId);
74 }
75
chreAbort(uint32_t abortCode)76 void chreAbort(uint32_t abortCode)
77 {
78 (void)syscallDo1P(SYSCALL_CHRE_API(ABORT), abortCode);
79 }
80
chreHeapAlloc(uint32_t bytes)81 void* chreHeapAlloc(uint32_t bytes)
82 {
83 return (void *)syscallDo1P(SYSCALL_CHRE_API(HEAP_ALLOC), bytes);
84 }
85
chreHeapFree(void * ptr)86 void chreHeapFree(void* ptr)
87 {
88 (void)syscallDo1P(SYSCALL_CHRE_API(HEAP_FREE), ptr);
89 }
90
chreSensorFindDefault(uint8_t sensorType,uint32_t * handle)91 bool chreSensorFindDefault(uint8_t sensorType, uint32_t *handle)
92 {
93 return syscallDo2P(SYSCALL_CHRE_API(SENSOR_FIND_DEFAULT), sensorType, handle);
94 }
95
chreGetSensorInfo(uint32_t sensorHandle,struct chreSensorInfo * info)96 bool chreGetSensorInfo(uint32_t sensorHandle, struct chreSensorInfo *info)
97 {
98 return syscallDo2P(SYSCALL_CHRE_API(SENSOR_GET_INFO), sensorHandle, info);
99 }
100
chreGetSensorSamplingStatus(uint32_t sensorHandle,struct chreSensorSamplingStatus * status)101 bool chreGetSensorSamplingStatus(uint32_t sensorHandle,
102 struct chreSensorSamplingStatus *status)
103 {
104 return syscallDo2P(SYSCALL_CHRE_API(SENSOR_GET_STATUS), sensorHandle, status);
105 }
106
chreSensorConfigure(uint32_t sensorHandle,enum chreSensorConfigureMode mode,uint64_t interval,uint64_t latency)107 bool chreSensorConfigure(uint32_t sensorHandle,
108 enum chreSensorConfigureMode mode,
109 uint64_t interval, uint64_t latency)
110 {
111 uint32_t interval_lo = interval;
112 uint32_t interval_hi = interval >> 32;
113 uint32_t latency_lo = latency;
114 uint32_t latency_hi = latency >> 32;
115 return syscallDo6P(SYSCALL_CHRE_API(SENSOR_CONFIG), sensorHandle, mode,
116 interval_lo, interval_hi, latency_lo, latency_hi);
117 }
118
chreGetApiVersion(void)119 uint32_t chreGetApiVersion(void)
120 {
121 static uint32_t apiVersion = 0;
122 if (!apiVersion)
123 apiVersion = syscallDo0P(SYSCALL_CHRE_API(GET_OS_API_VERSION));
124 return apiVersion;
125 }
126
chreGetVersion(void)127 uint32_t chreGetVersion(void)
128 {
129 return syscallDo0P(SYSCALL_CHRE_API(GET_OS_VERSION));
130 }
131
chreGetPlatformId(void)132 uint64_t chreGetPlatformId(void)
133 {
134 uint64_t plat = 0;
135 (void)syscallDo1P(SYSCALL_CHRE_API(GET_PLATFORM_ID), &plat);
136 return plat;
137 }
138
chreSendEvent(uint16_t eventType,void * eventData,chreEventCompleteFunction * freeCallback,uint32_t targetInstanceId)139 bool chreSendEvent(uint16_t eventType, void *eventData,
140 chreEventCompleteFunction *freeCallback,
141 uint32_t targetInstanceId)
142 {
143 if (chreGetApiVersion() == CHRE_API_VERSION_1_0)
144 return syscallDo4P(SYSCALL_CHRE_API(SEND_EVENT), eventType, eventData, freeCallback, targetInstanceId);
145 else
146 return syscallDo4P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_SEND_EVENT), eventType, eventData, freeCallback, targetInstanceId);
147 }
148
chreSendMessageToHost(void * message,uint32_t messageSize,uint32_t reservedMessageType,chreMessageFreeFunction * freeCallback)149 bool chreSendMessageToHost(void *message, uint32_t messageSize,
150 uint32_t reservedMessageType,
151 chreMessageFreeFunction *freeCallback)
152 {
153 if (chreGetApiVersion() == CHRE_API_VERSION_1_0)
154 return syscallDo4P(SYSCALL_CHRE_API(SEND_MSG), message, messageSize, reservedMessageType, freeCallback);
155 else
156 return syscallDo5P(SYSCALL_NO(SYSCALL_DOMAIN_CHRE, SYSCALL_CHRE_MAIN, SYSCALL_CHRE_MAIN_EVENT, SYSCALL_CHRE_MAIN_EVENT_SEND_MSG), message, messageSize, reservedMessageType, CHRE_HOST_ENDPOINT_BROADCAST, freeCallback);
157 }
158